lwb5-in-2025/game/scripts/lib/ajax.inc

85 lines
2 KiB
PHP
Raw Permalink Normal View History

2016-01-10 11:01:49 +01:00
<?php
class ajax {
static $method = array();
static $fHandler = array();
static $fTheme = array();
static $init = "";
private static function getTheme() {
2016-01-10 11:01:49 +01:00
$f = getLayoutDirectory(input::$game->version->id) . "/ajax.inc";
if (!file_exists($f)) {
return array();
}
return include($f);
}
static function init() {
$handler = handler::$h;
// Get AJAX functions from handler
$a1 = is_array($handler->ajax) ? $handler->ajax : array();
if (isset($a1['func'])) {
2016-01-10 11:01:49 +01:00
ajax::$fHandler = $a1['func'];
$ml = is_array($a1['method'] ?? null) ? $a1['method'] : array();
2016-01-10 11:01:49 +01:00
foreach ($a1['func'] as $f) {
$m = ($ml[$f] ?? "" == "") ? ($handler->ajaxPOST ?? false ? "POST" : "GET") : $ml[$f];
2016-01-10 11:01:49 +01:00
ajax::$method[$f] = $m;
}
}
// Get theme-specific AJAX functions
$a2 = ajax::getTheme();
if (isset($a2['func'])) {
2016-01-10 11:01:49 +01:00
ajax::$fTheme = $a2['func'];
$ml = is_array($a2['method'] ?? null) ? $a2['method'] : array();
2016-01-10 11:01:49 +01:00
foreach ($a2['func'] as $f) {
$m = ($ml[$f] ?? "" == "") ? ($handler->ajaxPOST ?? false ? "POST" : "GET") : $ml[$f];
2016-01-10 11:01:49 +01:00
ajax::$method[$f] = $m;
}
}
// Create init string
ajax::$init = ($a2['init'] ?? '') . ($a1['init'] ?? '');
2016-01-10 11:01:49 +01:00
}
static function canCall($function) {
return in_array($function, ajax::$fTheme) || in_array($function, ajax::$fHandler);
}
static function call($function, $args) {
// We don't want an error in that part of the code to make the
// client-side JS script go insane
ob_start();
// Call the function
if (in_array($function, ajax::$fTheme)) {
$res = call_user_func_array("thm_{$function}", $args);
} else if (in_array($function, ajax::$fHandler)) {
$res = call_user_func_array(array(handler::$h, $function), $args);
} else {
$res = null;
}
// Log any error / warning / whatever
$buffer = ob_get_contents();
if ($buffer != '') {
$b = explode("\n", $buffer);
foreach ($b as $line) {
if ($line != '') {
l::warning("AJAX ($function): $line");
}
}
}
ob_end_clean();
return $res;
}
}
?>