Show ajax.php syntax highlighted
<?php
if (!defined('TINYAJAX_PATH'))
define('TINYAJAX_PATH', 'ext/tinyajax_095');
require_once (TINYAJAX_PATH . '/TinyAjax.php');
class NoAjaxException extends Exception {
};
class Ajax {
private static $tinyajax = null;
private static $export = array ();
private static $clientExport = array ();
private static $tabsBuffer = array ();
private static $tab = null;
private static $clientCall = false;
private static $ajaxFunction = null;
private static $loadOnDemand = array ();
static function isClientCall() {
if (isset ($_POST["rs"]) || self :: $clientCall === true) {
self :: $clientCall = true;
return true;
} else {
return false;
}
}
private static function _processTabsBuffer() {
if (sizeof(self :: $tabsBuffer)) {
$JavaScriptOnDemand = array ();
foreach (self :: $tabsBuffer as $behavior) {
if ($behavior[0] == 'TabJS') {
$JavaScriptOnDemand[] = $behavior;
} else {
self :: $tab->add($behavior);
}
}
foreach ($JavaScriptOnDemand as $behavior) {
self :: $tab->add($behavior);
}
}
}
public static function ThrowException() {
if (Ajax :: isClientCall()) {
self :: _processTabsBuffer();
throw new AjaxFlushException(self :: $tab->getString());
} else {
throw new NoAjaxException;
}
}
public static function getBehaviorsNum() {
return sizeof(self :: $tabsBuffer);
}
public static function sendNewContent() {
if (self :: $ajaxFunction === null)
self :: Init();
if (self :: isClientCall() && self :: $ajaxFunction == 'SendContent')
return true;
else
return false;
}
public static function loadOnDemand($script) {
self :: $loadOnDemand[] = $script;
}
public static function getScriptsOnDemand() {
return self :: $loadOnDemand;
}
public static function Init() {
global $Config;
if (self :: $tinyajax !== null)
return self :: $tinyajax;
self :: isClientCall();
if (isset ($_POST["rs"])) {
self :: $ajaxFunction = $_POST["rs"];
}
self :: $tinyajax = new TinyAjax(true);
self :: $tinyajax->setRequestType("POST");
self :: $tinyajax->setRequestUri($Config->getMaster() . 'index.php');
self :: $tab = new TinyAjaxBehavior(true);
self :: Export('ECP->VerifyUser', array (
'callFunction'
));
self :: Export('ECP->VerifyCode', 'verify_code');
self :: Export('ECP->SendContent', array (
'_ecp_number',
'_link',
'_content',
'_template_file'
));
self :: Export('ECP->GetPage', 'location');
return self :: $tinyajax;
}
public static function Export($methodName, $sourceId = null, $callback = null, $class = null) {
if ($class != null && is_object($class) && $class instanceof Module) {
if (method_exists($class, $methodName)) {
$methodName = $class->getInstance() . '->' . $methodName;
}
}
$object = new stdClass();
$object->functionName = $methodName;
$object->sourceId = serialize($sourceId);
$object->callback = $callback;
$object->className = $class;
self :: $export[] = $object;
}
private static function clientExported($object) {
foreach (self :: $clientExport as $export) {
if ($export->functionName == $object->functionName && $export->sourceId == $object->sourceId && $export->callback == $object->callback && $export->className == $object->className) {
return true;
}
}
return false;
}
public static function Process($clientProcess = false) {
global $XCS;
$TA = new TA();
$TA->addQuery(TA :: SELECT, "ecp_engine_ajax");
$TA->Execute();
while ($command = $TA->Result()->FetchObject()) {
if (empty ($command->sourceId))
$command->sourceId = null;
if (empty ($command->callback))
$command->callback = null;
if (empty ($command->className))
$command->className = null;
self :: $clientExport[] = $command;
}
$TA->removeQuery();
if (!$clientProcess) {
foreach (self :: $export as $command) {
if (!self :: clientExported($command)) {
$TA->addQuery(TA :: DELETE, "ecp_engine_ajax");
$TA->WHERE("`functionName`='" . $command->functionName . "'");
$TA->LIMIT(1);
$TA->addQuery(TA :: INSERT, "ecp_engine_ajax");
$TA->addParam('functionName', TA :: STRING, $command->functionName);
$TA->addParam('sourceId', TA :: STRING, $command->sourceId);
$TA->addParam('className', TA :: STRING, $command->className);
$object = explode("->", $command->functionName);
if (sizeof($object) == 2 && strpos($object[1], 'on') === 0) {
$command->jshandler = substr($object[1], 2);
$command->jshandler = ucfirst($object[0]) . $command->jshandler;
$TA->addParam('jshandler', TA :: STRING, $command->jshandler);
$command->callback = ucfirst($object[0]);
}
$TA->addParam('callback', TA :: STRING, $command->callback);
}
}
}
foreach (self :: $clientExport as $command) {
if (false !== (strpos($command->functionName, '->'))) {
$object = explode("->", $command->functionName);
if ($object[0] == 'XCS') {
global $XCS;
$command->className = $XCS;
} else
if ($object[0] == 'ECP') {
global $ECP;
$command->className = $ECP;
} else {
try {
$command->className = ModulesManager :: loadModule($object[0]);
} catch (NoModuleException $e) {
}
}
if (isset ($command->className->object)) {
$command->className = $command->className->object;
} else
if (!$command->className) {
global ${ $object[0] };
$command->className = ${$object[0]};
}
$command->functionName = $object[1];
}
$unserialized = unserialize($command->sourceId);
if ($unserialized !== false) {
$command->sourceId = $unserialized;
}
self :: $tinyajax->exportFunction($command->functionName, $command->sourceId, null, $command->className, $command->jshandler, $command->callback);
}
self :: $tinyajax->process();
if (!$clientProcess && sizeof(self :: $export)) {
$TA->Execute();
return true;
}
if ($clientProcess) {
self :: drawJavascript();
exit;
}
}
public static function getFunctionNameByJSHandler($jshandler) {
$TA = new TA();
$TA->addQuery(TA :: SELECT, "ecp_engine_ajax");
$TA->addParam('functionName');
$TA->WHERE("`jshandler`='" . $jshandler . "'");
$TA->LIMIT(1);
$TA->Execute();
$func_name = $TA->Result()->FetchObject()->functionName;
$object = explode("->", $func_name);
if (sizeof($object) == 2)
return $object[1];
else
return $func_name;
}
public static function getJavascript() {
$content = self :: $tinyajax->getJavaScript();
return $content;
}
public static function drawJavascript() {
self :: $tinyajax->drawJavaScript(true, false);
}
public static function getPostData() {
if (Ajax :: isClientCall()) {
if (isset ($_POST['rsargs'])) {
$rsargs = $_POST['rsargs'];
if ($rsargs[sizeof($rsargs) - 1] == 'post_data') {
$encodedPostData = $rsargs[sizeof($rsargs) - 2];
return TinyAjax :: getPostData($encodedPostData);
}
}
}
return $_POST;
}
public static function Action($behavior) {
if (!self :: isClientCall()) {
throw new NoAjaxException;
}
self :: $tabsBuffer[] = $behavior;
}
public static function ReturnValue($value = false) {
if (self :: isClientCall()) {
return self :: $tab->getString();
} else {
return $value;
}
}
public static function getActionIdsFromMethod($method) {
$myMethod = new XCSReflectionMethod($method);
return self :: getActionIds($myMethod->getLines());
}
private static function getActionIds($lines) {
$ids = array ();
foreach ($lines as $line) {
if (false !== strpos($line, "Ajax :: InnerHTMLe('") && false === strpos($line, "//") && false !== strpos($line, "',")) {
$source = explode("'", $line);
if (isset ($source[1]))
$ids[] = $source[1];
}
}
return $ids;
}
public static function getActionIdsFromFile($file) {
return self :: getActionIds(file($file));
}
static function generateID($action, $prefix = false) {
if ($prefix === false)
$prefix = 'ajax';
return $prefix . '_' . String :: removeSpecialChars($action, '');
}
/**
* Sets new $content to automaticaly generated element container.
*
*/
static function InnerHTMLe($id, $content) {
if (strpos($id, '$') !== false) {
if (Renderer :: getFlag('currentModule'))
$prefix = Renderer :: getFlag('currentModule')->getBase();
else
$prefix = false;
$id = self :: generateID($id, $prefix);
}
$id = self :: generateID($id, false);
self :: Action(TabInnerHTML :: getBehavior($id, $content));
}
/**
* Sets new $content to element by $id.
*/
static function InnerHTML($id, $content) {
self :: Action(TabInnerHTML :: getBehavior($id, $content));
}
static function setValue($id, $content) {
self :: Action(TabSetValue :: getBehavior($id, $content));
}
static function getClientFunction() {
return self :: $ajaxFunction;
}
}
?>
See more files for this project here