-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
146 lines (140 loc) · 4.54 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
if (!isset($argv) && preg_match('/\.(css|ico)$/', $_SERVER["REQUEST_URI"])) {
return false; // serve the requested resource as-is.
}
require_once 'vendor/autoload.php';
require_once 'src/commonfunctions.php';
require_once 'src/cache.php';
require_once 'src/settings.php';
require_once 'src/twigext.php';
function loadModules($path, $mod) {
for ($dir = opendir($path); $file = readdir($dir); ) {
if (substr($file, -4) == ".php") {
require_once $path . $file;
$modClass = substr($file,0, -4);
$coremagicvalues[$modClass::magic] = $modClass;
}
}
$mainmod = 'game';
if (isset($coremagicvalues[$mod]))
$mainmod = $coremagicvalues[$mod];
return $mainmod;
}
if (count($_GET) > 0) {
$options = '';
if (isset($_GET['coremod']))
$options[] = $_GET['coremod'];
if (isset($_GET['param']))
$options[] = $_GET['param'];
foreach ($_GET as $key => $val)
if (($key != 'param') && ($key != 'coremod')) {
if ($val == 'true')
$options[] = $key;
else if ($val != null)
$options[] = sprintf('%s=%s', $key, $val);
}
header(sprintf('Location: http://%s/%s/',$_SERVER['HTTP_HOST'], urlencode(implode('/', $options))));
die();
}
$metadata['time_start'] = microtime(true);
$metadata['rootdir'] = '/';
if (isset($_SERVER['DOCUMENT_URI']))
$metadata['rootdir'] = dirname($_SERVER['DOCUMENT_URI']).'/';
$settings = new settings('settings.yml');
if ($settings['debug']) {
ini_set('xdebug.var_display_max_depth', -1);
}
$cache = new cache();
if ($settings['cacheclear'])
$cache->clear();
header('Content-Type: text/html; charset=utf-8');
$loader = new Twig_Loader_Filesystem('src/templates');
$twig = new Twig_Environment($loader, array('debug' => $settings['debug']));
$twig->addExtension(new Twig_Extension_Debug());
$twig->addExtension(new Penguin_Twig_Extensions());
if (!isset($argv)) {
$console = false;
if ($metadata['rootdir'] != '/')
$uristring = str_replace($metadata['rootdir'], '', $_SERVER['REQUEST_URI']);
else
$uristring = $_SERVER['REQUEST_URI'];
$argv = array_slice(explode('/', $uristring),1);
} else {
$console = true;
array_shift($argv);
}
if (count($argv) > 0) {
debugvar(substr($argv[count($argv)-1], 0, strrpos($argv[count($argv)-1], '.')), 'extension detection');
if ((substr($argv[count($argv)-1], 0, strrpos($argv[count($argv)-1], '.')) != $argv[count($argv)-1]) && (substr($argv[count($argv)-1], 0, strrpos($argv[count($argv)-1], '.')) != ''))
$argv[count($argv)-1] = substr($argv[count($argv)-1], 0, strrpos($argv[count($argv)-1], '.'));
}
$metadata['options'] = array();
for ($i = 2; $i < count($argv); $i++) {
$v = explode('=', $argv[$i]);
if (isset($v[1]))
$metadata['options'][$v[0]] = urldecode($v[1]);
else
$metadata['options'][$v[0]] = true;
}
$types = array();
if (function_exists('yaml_emit')) {
$types['yml'] = 'yml';
$types['yaml'] = 'yml';
}
if (function_exists('json_encode'))
$types['json'] = 'json';
if ($console)
$format = 'console';
else {
$v = substr(strrchr($_SERVER['REQUEST_URI'], '.'),1);
if (!isset($types[$v]))
$format = 'html';
else
$format = $types[$v];
}
//Options!
debugvar($argv, 'args');
debugvar($metadata['options'], 'options');
debugmessage("Loading Core Modules", 'info');
//Load Modules
if ($settings['gamemenu']) {
for ($dir = opendir('./games/'); $file = readdir($dir); ) {
if (($file[0] != '.') && is_dir('./games/'.$file)) {
$gamedetails = yaml_parse_file('./games/'.$file.'/'.$file.'.yml', 0);
$metadata['gamelist'][$file] = gametitle($gamedetails);
}
}
asort($metadata['gamelist']);
}
$mainmod = loadModules('./src/mods/', $argv[0]);
debugvar($mainmod, 'Main Module');
$mod = new $mainmod();
$mod->setMetadata($metadata);
$data = $mod->getData($argv);
//Display stuff
$displaydata = array_merge($metadata, $mod->getMetadata());
switch($format) {
case 'yml':
header('Content-Type: text/plain; charset=UTF-8');
if ($data !== null)
echo yaml_emit($data, YAML_UTF8_ENCODING, YAML_ANY_BREAK);
break;
case 'json':
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
break;
case 'console':
debugvar($mod->getMetadata()['template'], 'displaymode');
$displaydata['data'] = $data;
echo $twig->render($displaydata['template'].'_console.tpl', $displaydata);
break;
default:
debugvar($mod->getMetadata()['template'], 'displaymode');
header('Content-Type: text/html; charset=UTF-8');
$displaydata['data'] = $data;
if (isset($GLOBALS['ERRORS']))
$displaydata['errors'] = $GLOBALS['ERRORS'];
echo $twig->render($displaydata['template'].'.tpl', $displaydata);
break;
}
?>