PageRenderTime 34ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/installation/includes/xajax/xajax.inc.php

https://github.com/adothompson/ucsc-identity-joomla
PHP | 856 lines | 589 code | 87 blank | 180 comment | 156 complexity | eb877418bfef7f84d2420b021f87a947 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // xajax.inc.php::Main xajax class and setup file
  4. //
  5. // xajax version 0.2
  6. // Copyright (C) 2005 - 2006 by Jared White & J. Max Wilson
  7. // http://xajax.sourceforge.net
  8. //
  9. // xajax is an open source PHP class library for easily creating powerful
  10. // PHP-driven, web-based AJAX Applications. Using xajax, you can asynchronously
  11. // call PHP functions and update the content of your your webpage without
  12. // reloading the page.
  13. //
  14. // xajax is released under the terms of the LGPL license
  15. // http://www.gnu.org/copyleft/lesser.html#SEC3
  16. //
  17. // This library is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU Lesser General Public
  19. // License as published by the Free Software Foundation; either
  20. // version 2.1 of the License, or (at your option) any later version.
  21. //
  22. // This library is distributed in the hope that it will be useful,
  23. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. // Lesser General Public License for more details.
  26. //
  27. // You should have received a copy of the GNU Lesser General Public
  28. // License along with this library; if not, write to the Free Software
  29. // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // Define XAJAX_DEFAULT_CHAR_ENCODING that is used by both
  32. // the xajax and xajaxResponse classes
  33. if (!defined ('XAJAX_DEFAULT_CHAR_ENCODING')) {
  34. define ('XAJAX_DEFAULT_CHAR_ENCODING', 'utf-8' );
  35. }
  36. require_once(dirname( __FILE__ ).DIRECTORY_SEPARATOR."xajaxResponse.inc.php");
  37. // Communication Method Defines
  38. if (!defined ('XAJAX_GET'))
  39. {
  40. define ('XAJAX_GET', 0);
  41. }
  42. if (!defined ('XAJAX_POST'))
  43. {
  44. define ('XAJAX_POST', 1);
  45. }
  46. // the xajax class generates the xajax javascript for your page including the
  47. // javascript wrappers for the PHP functions that you want to call from your page.
  48. // It also handles processing and executing the command messages in the xml responses
  49. // sent back to your page from your PHP functions.
  50. class xajax
  51. {
  52. var $aFunctions; // Array of PHP functions that will be callable through javascript wrappers
  53. var $aObjects; // Array of object callbacks that will allow Javascript to call PHP methods (key=function name)
  54. var $aFunctionRequestTypes; // Array of RequestTypes to be used with each function (key=function name)
  55. var $aFunctionIncludeFiles; // Array of Include Files for any external functions (key=function name)
  56. var $sCatchAllFunction; // Name of the PHP function to call if no callable function was found
  57. var $sPreFunction; // Name of the PHP function to call before any other function
  58. var $sRequestURI; // The URI for making requests to the xajax object
  59. var $bDebug; // Show debug messages (true/false)
  60. var $bExitAllowed; // Allow xajax to exit after processing a request (true/false)
  61. var $bErrorHandler; // Use an special xajax error handler so the errors are sent to the browser properly
  62. var $sLogFile; // Specify if xajax should log errors (and more information in a future release)
  63. var $sWrapperPrefix; // The prefix to prepend to the javascript wraper function name
  64. var $bStatusMessages; // Show debug messages (true/false)
  65. var $bWaitCursor; // Use wait cursor in browser (true/false)
  66. var $bCleanBuffer; // Clean all output buffers before outputting response (true/false)
  67. var $aObjArray; // Array for parsing complex objects
  68. var $iPos; // Position in $aObjArray
  69. var $sEncoding; // The Character Encoding to use
  70. // Contructor
  71. // $sRequestURI - defaults to the current page
  72. // $sWrapperPrefix - defaults to "xajax_";
  73. // $sEncoding - defaults to XAJAX_DEFAULT_CHAR_ENCODING defined above
  74. // $bDebug Mode - defaults to false
  75. // usage: $xajax = new xajax();
  76. function xajax($sRequestURI="",$sWrapperPrefix="xajax_",$sEncoding=XAJAX_DEFAULT_CHAR_ENCODING,$bDebug=false)
  77. {
  78. $this->aFunctions = array();
  79. $this->aObjects = array();
  80. $this->aFunctionIncludeFiles = array();
  81. $this->sRequestURI = $sRequestURI;
  82. if ($this->sRequestURI == "")
  83. $this->sRequestURI = $this->_detectURI();
  84. $this->sWrapperPrefix = $sWrapperPrefix;
  85. $this->setCharEncoding($sEncoding);
  86. $this->bDebug = $bDebug;
  87. $this->bWaitCursor = true;
  88. $this->bExitAllowed = true;
  89. $this->bErrorHandler = false;
  90. $this->sLogFile = "";
  91. $this->bCleanBuffer = true;
  92. }
  93. // setRequestURI() sets the URI to which requests will be made
  94. // usage: $xajax->setRequestURI("http://xajax.sourceforge.net");
  95. function setRequestURI($sRequestURI)
  96. {
  97. $this->sRequestURI = $sRequestURI;
  98. }
  99. // debugOn() enables debug messages for xajax
  100. function debugOn()
  101. {
  102. $this->bDebug = true;
  103. }
  104. // debugOff() disables debug messages for xajax (default behavior)
  105. function debugOff()
  106. {
  107. $this->bDebug = false;
  108. }
  109. // statusMessagesOn() enables messages in the statusbar for xajax
  110. function statusMessagesOn()
  111. {
  112. $this->bStatusMessages = true;
  113. }
  114. // statusMessagesOff() disables messages in the statusbar for xajax (default behavior)
  115. function statusMessagesOff()
  116. {
  117. $this->bStatusMessages = false;
  118. }
  119. // waitCursor() enables the wait cursor to be displayed in the browser (default behavior)
  120. function waitCursorOn()
  121. {
  122. $this->bWaitCursor = true;
  123. }
  124. // waitCursorOff() disables the wait cursor to be displayed in the browser
  125. function waitCursorOff()
  126. {
  127. $this->bWaitCursor = false;
  128. }
  129. // exitAllowedOn() enables xajax to exit immediately after processing a request
  130. // and sending the response back to the browser (default behavior)
  131. function exitAllowedOn()
  132. {
  133. $this->bExitAllowed = true;
  134. }
  135. // exitAllowedOff() disables xajax's default behavior of exiting immediately
  136. // after processing a request and sending the response back to the browser
  137. function exitAllowedOff()
  138. {
  139. $this->bExitAllowed = false;
  140. }
  141. // errorHandlerOn() turns on xajax's error handling system so that PHP errors
  142. // that occur during a request are trapped and pushed to the browser in the
  143. // form of a Javascript alert
  144. function errorHandlerOn()
  145. {
  146. $this->bErrorHandler = true;
  147. }
  148. // errorHandlerOff() turns off xajax's error handling system (default behavior)
  149. function errorHandlerOff()
  150. {
  151. $this->bErrorHandler = false;
  152. }
  153. // setLogFile() specifies a log file that will be written to by xajax during
  154. // a request (used only by the error handling system at present). If you don't
  155. // invoke this method, or you pass in "", then no log file will be written to.
  156. // usage: $xajax->setLogFile("/xajax_logs/errors.log");
  157. function setLogFile($sFilename)
  158. {
  159. $this->sLogFile = $sFilename;
  160. }
  161. // cleanBufferOn() causes xajax to clean out all output buffers before outputting
  162. // a response (default behavior)
  163. function cleanBufferOn()
  164. {
  165. $this->bCleanBuffer = true;
  166. }
  167. // cleanBufferOff() turns off xajax's output buffer cleaning
  168. function cleanBufferOff()
  169. {
  170. $this->bCleanBuffer = false;
  171. }
  172. // setWrapperPrefix() sets the prefix that will be appended to the Javascript
  173. // wrapper functions (default is "xajax_").
  174. function setWrapperPrefix($sPrefix)
  175. {
  176. $this->sWrapperPrefix = $sPrefix;
  177. }
  178. // setCharEncoding() sets the character encoding to be used by xajax
  179. // usage: $xajax->setCharEncoding("utf-8");
  180. // *Note: to change the default character encoding for all xajax responses, set
  181. // the XAJAX_DEFAULT_CHAR_ENCODING constant near the beginning of the xajax.inc.php file
  182. function setCharEncoding($sEncoding)
  183. {
  184. $this->sEncoding = $sEncoding;
  185. }
  186. // registerFunction() registers a PHP function or method to be callable through
  187. // xajax in your Javascript. If you want to register a function, pass in the name
  188. // of that function. If you want to register a static class method, pass in an array
  189. // like so:
  190. // array("myFunctionName", "myClass", "myMethod")
  191. // For an object instance method, use an object variable for the second array element
  192. // (and in PHP 4 make sure you put an & before the variable to pass the object by
  193. // reference). Note: the function name is what you call via Javascript, so it can be
  194. // anything as long as it doesn't conflict with any other registered function name.
  195. //
  196. // $mFunction is a string containing the function name or an object callback array
  197. // $sRequestType is the RequestType (XAJAX_GET/XAJAX_POST) that should be used
  198. // for this function. Defaults to XAJAX_POST.
  199. // usage: $xajax->registerFunction("myFunction");
  200. // or: $xajax->registerFunction(array("myFunctionName", &$myObject, "myMethod"));
  201. function registerFunction($mFunction,$sRequestType=XAJAX_POST)
  202. {
  203. if (is_array($mFunction)) {
  204. $this->aFunctions[$mFunction[0]] = 1;
  205. $this->aFunctionRequestTypes[$mFunction[0]] = $sRequestType;
  206. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  207. }
  208. else {
  209. $this->aFunctions[$mFunction] = 1;
  210. $this->aFunctionRequestTypes[$mFunction] = $sRequestType;
  211. }
  212. }
  213. // registerExternalFunction() registers a PHP function to be callable through xajax
  214. // which is located in some other file. If the function is requested the external
  215. // file will be included to define the function before the function is called
  216. // $mFunction is a string containing the function name or an object callback array
  217. // see registerFunction() for more info on object callback arrays
  218. // $sIncludeFile is a string containing the path and filename of the include file
  219. // $sRequestType is the RequestType (XAJAX_GET/XAJAX_POST) that should be used
  220. // for this function. Defaults to XAJAX_POST.
  221. // usage: $xajax->registerExternalFunction("myFunction","myFunction.inc.php",XAJAX_POST);
  222. function registerExternalFunction($mFunction,$sIncludeFile,$sRequestType=XAJAX_POST)
  223. {
  224. $this->registerFunction($mFunction, $sRequestType);
  225. if (is_array($mFunction)) {
  226. $this->aFunctionIncludeFiles[$mFunction[0]] = $sIncludeFile;
  227. }
  228. else {
  229. $this->aFunctionIncludeFiles[$mFunction] = $sIncludeFile;
  230. }
  231. }
  232. // registerCatchAllFunction() registers a PHP function to be called when xajax cannot
  233. // find the function being called via Javascript. Because this is technically
  234. // impossible when using "wrapped" functions, the catch-all feature is only useful
  235. // when you're directly using the xajax.call() Javascript method. Use the catch-all
  236. // feature when you want more dynamic ability to intercept unknown calls and handle
  237. // them in a custom way.
  238. // $mFunction is a string containing the function name or an object callback array
  239. // see registerFunction() for more info on object callback arrays
  240. // usage: $xajax->registerCatchAllFunction("myCatchAllFunction");
  241. function registerCatchAllFunction($mFunction)
  242. {
  243. if (is_array($mFunction)) {
  244. $this->sCatchAllFunction = $mFunction[0];
  245. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  246. }
  247. else {
  248. $this->sCatchAllFunction = $mFunction;
  249. }
  250. }
  251. // registerPreFunction() registers a PHP function to be called before xajax calls
  252. // the requested function. xajax will automatically add the request function's response
  253. // to the pre-function's response to create a single response. Another feature is
  254. // the ability to return not just a response, but an array with the first element
  255. // being false (a boolean) and the second being the response. In this case, the
  256. // pre-function's response will be returned to the browser without xajax calling
  257. // the requested function.
  258. // $mFunction is a string containing the function name or an object callback array
  259. // see registerFunction() for more info on object callback arrays
  260. // usage $xajax->registerPreFunction("myPreFunction");
  261. function registerPreFunction($mFunction)
  262. {
  263. if (is_array($mFunction)) {
  264. $this->sPreFunction = $mFunction[0];
  265. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  266. }
  267. else {
  268. $this->sPreFunction = $mFunction;
  269. }
  270. }
  271. // returns true if xajax can process the request, false if otherwise
  272. // you can use this to determine if xajax needs to process the request or not
  273. function canProcessRequests()
  274. {
  275. if ($this->getRequestMode() != -1) return true;
  276. return false;
  277. }
  278. // returns the current request mode, or -1 if there is none
  279. function getRequestMode()
  280. {
  281. if (!empty($_GET["xajax"]))
  282. return XAJAX_GET;
  283. if (!empty($_POST["xajax"]))
  284. return XAJAX_POST;
  285. return -1;
  286. }
  287. // processRequests() is the main communications engine of xajax
  288. // The engine handles all incoming xajax requests, calls the apporiate PHP functions
  289. // and passes the xml responses back to the javascript response handler
  290. // if your RequestURI is the same as your web page then this function should
  291. // be called before any headers or html has been sent.
  292. // usage: $xajax->processRequests()
  293. function processRequests()
  294. {
  295. $requestMode = -1;
  296. $sFunctionName = "";
  297. $bFoundFunction = true;
  298. $bFunctionIsCatchAll = false;
  299. $sFunctionNameForSpecial = "";
  300. $aArgs = array();
  301. $sPreResponse = "";
  302. $bEndRequest = false;
  303. $sResponse = "";
  304. $requestMode = $this->getRequestMode();
  305. if ($requestMode == -1) return;
  306. if ($requestMode == XAJAX_POST)
  307. {
  308. $sFunctionName = $_POST["xajax"];
  309. if (!empty($_POST["xajaxargs"]))
  310. $aArgs = $_POST["xajaxargs"];
  311. }
  312. else
  313. {
  314. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  315. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  316. header ("Cache-Control: no-cache, must-revalidate");
  317. header ("Pragma: no-cache");
  318. header("Content-type: text/xml");
  319. $sFunctionName = $_GET["xajax"];
  320. if (!empty($_GET["xajaxargs"]))
  321. $aArgs = $_GET["xajaxargs"];
  322. }
  323. // Use xajax error handler if necessary
  324. if ($this->bErrorHandler) {
  325. $GLOBALS['xajaxErrorHandlerText'] = "";
  326. set_error_handler("xajaxErrorHandler");
  327. }
  328. if ($this->sPreFunction) {
  329. if (!$this->_isFunctionCallable($this->sPreFunction)) {
  330. $bFoundFunction = false;
  331. $objResponse = new xajaxResponse();
  332. $objResponse->addAlert("Unknown Pre-Function ". $this->sPreFunction);
  333. $sResponse = $objResponse->getXML();
  334. }
  335. }
  336. //include any external dependencies associated with this function name
  337. if (array_key_exists($sFunctionName,$this->aFunctionIncludeFiles))
  338. {
  339. ob_start();
  340. include_once($this->aFunctionIncludeFiles[$sFunctionName]);
  341. ob_end_clean();
  342. }
  343. if ($bFoundFunction) {
  344. $sFunctionNameForSpecial = $sFunctionName;
  345. if (!array_key_exists($sFunctionName, $this->aFunctions))
  346. {
  347. if ($this->sCatchAllFunction) {
  348. $sFunctionName = $this->sCatchAllFunction;
  349. $bFunctionIsCatchAll = true;
  350. }
  351. else {
  352. $bFoundFunction = false;
  353. $objResponse = new xajaxResponse();
  354. $objResponse->addAlert("Unknown Function $sFunctionName.");
  355. $sResponse = $objResponse->getXML();
  356. }
  357. }
  358. else if ($this->aFunctionRequestTypes[$sFunctionName] != $requestMode)
  359. {
  360. $bFoundFunction = false;
  361. $objResponse = new xajaxResponse();
  362. $objResponse->addAlert("Incorrect Request Type.");
  363. $sResponse = $objResponse->getXML();
  364. }
  365. }
  366. if ($bFoundFunction)
  367. {
  368. for ($i = 0; $i < sizeof($aArgs); $i++)
  369. {
  370. // If magic quotes is on, then we need to strip the slashes from the args
  371. if (get_magic_quotes_gpc() == 1 && is_string($aArgs[$i])) {
  372. $aArgs[$i] = stripslashes($aArgs[$i]);
  373. }
  374. if (stristr($aArgs[$i],"<xjxobj>") != false)
  375. {
  376. $aArgs[$i] = $this->_xmlToArray("xjxobj",$aArgs[$i]);
  377. }
  378. else if (stristr($aArgs[$i],"<xjxquery>") != false)
  379. {
  380. $aArgs[$i] = $this->_xmlToArray("xjxquery",$aArgs[$i]);
  381. }
  382. }
  383. if ($this->sPreFunction) {
  384. $mPreResponse = $this->_callFunction($this->sPreFunction, array($sFunctionNameForSpecial, $aArgs));
  385. if (is_array($mPreResponse) && $mPreResponse[0] === false) {
  386. $bEndRequest = true;
  387. $sPreResponse = $mPreResponse[1];
  388. }
  389. else {
  390. $sPreResponse = $mPreResponse;
  391. }
  392. if (is_a($sPreResponse, "xajaxResponse")) {
  393. $sPreResponse = $sPreResponse->getXML();
  394. }
  395. if ($bEndRequest) $sResponse = $sPreResponse;
  396. }
  397. if (!$bEndRequest) {
  398. if (!$this->_isFunctionCallable($sFunctionName)) {
  399. $objResponse = new xajaxResponse();
  400. $objResponse->addAlert("The Registered Function $sFunctionName Could Not Be Found.");
  401. $sResponse = $objResponse->getXML();
  402. }
  403. else {
  404. if ($bFunctionIsCatchAll) {
  405. $aArgs = array($sFunctionNameForSpecial, $aArgs);
  406. }
  407. $sResponse = $this->_callFunction($sFunctionName, $aArgs);
  408. }
  409. if (is_a($sResponse, "xajaxResponse")) {
  410. $sResponse = $sResponse->getXML();
  411. }
  412. if (!is_string($sResponse) || strpos($sResponse, "<xjx>") === FALSE) {
  413. $objResponse = new xajaxResponse();
  414. $objResponse->addAlert("No XML Response Was Returned By Function $sFunctionName.");
  415. $sResponse = $objResponse->getXML();
  416. }
  417. else if ($sPreResponse != "") {
  418. $sNewResponse = new xajaxResponse();
  419. $sNewResponse->loadXML($sPreResponse);
  420. $sNewResponse->loadXML($sResponse);
  421. $sResponse = $sNewResponse->getXML();
  422. }
  423. }
  424. }
  425. $sContentHeader = "Content-type: text/xml;";
  426. if ($this->sEncoding && strlen(trim($this->sEncoding)) > 0)
  427. $sContentHeader .= " charset=".$this->sEncoding;
  428. header($sContentHeader);
  429. if ($this->bErrorHandler && !empty( $GLOBALS['xajaxErrorHandlerText'] )) {
  430. $sErrorResponse = new xajaxResponse();
  431. $sErrorResponse->addAlert("** PHP Error Messages: **" . $GLOBALS['xajaxErrorHandlerText']);
  432. if ($this->sLogFile) {
  433. $fH = @fopen($this->sLogFile, "a");
  434. if (!$fH) {
  435. $sErrorResponse->addAlert("** Logging Error **\n\nxajax was unable to write to the error log file:\n" . $this->sLogFile);
  436. }
  437. else {
  438. fwrite($fH, "** xajax Error Log - " . strftime("%b %e %Y %I:%M:%S %p") . " **" . $GLOBALS['xajaxErrorHandlerText'] . "\n\n\n");
  439. fclose($fH);
  440. }
  441. }
  442. $sErrorResponse->loadXML($sResponse);
  443. $sResponse = $sErrorResponse->getXML();
  444. }
  445. if ($this->bCleanBuffer) while (@ob_end_clean());
  446. print $sResponse;
  447. if ($this->bErrorHandler) restore_error_handler();
  448. if ($this->bExitAllowed)
  449. exit();
  450. }
  451. // printJavascript() prints the xajax javascript code into your page by printing
  452. // the output of the getJavascript() method. It should only be called between the
  453. // <head> </head> tags in your HTML page. Remember, if you only want to obtain the
  454. // result of this function, use getJavascript() instead.
  455. // $sJsURI is the relative address of the folder where xajax has been installed.
  456. // For instance, if your PHP file is "http://www.myserver.com/myfolder/mypage.php"
  457. // and xajax was installed in "http://www.myserver.com/anotherfolder", then
  458. // $sJsURI should be set to "../anotherfolder". Defaults to assuming xajax is in
  459. // the same folder as your PHP file.
  460. // $sJsFile is the relative folder/file pair of the xajax Javascript engine located
  461. // within the xajax installation folder. Defaults to xajax_js/xajax.js.
  462. // usage:
  463. // <head>
  464. // ...
  465. // < ?php $xajax->printJavascript(); ? >
  466. function printJavascript($sJsURI="", $sJsFile=NULL, $sJsFullFilename=NULL)
  467. {
  468. print $this->getJavascript($sJsURI, $sJsFile, $sJsFullFilename);
  469. }
  470. // getJavascript() returns the xajax javascript code that should be added to
  471. // your HTML page between the <head> </head> tags. See printJavascript()
  472. // for information about the function arguments.
  473. // usage:
  474. // < ?php $xajaxJSHead = $xajax->getJavascript(); ? >
  475. // <head>
  476. // ...
  477. // < ?php echo $xajaxJSHead; ? >
  478. function getJavascript($sJsURI="", $sJsFile=NULL, $sJsFullFilename=NULL)
  479. {
  480. if ($sJsFile == NULL) $sJsFile = "xajax_js/xajax.js";
  481. if ($sJsURI != "" && substr($sJsURI, -1) != "/") $sJsURI .= "/";
  482. $html = "\t<script type=\"text/javascript\">\n";
  483. $html .= "var xajaxRequestUri=\"".$this->sRequestURI."\";\n";
  484. $html .= "var xajaxDebug=".($this->bDebug?"true":"false").";\n";
  485. $html .= "var xajaxStatusMessages=".($this->bStatusMessages?"true":"false").";\n";
  486. $html .= "var xajaxWaitCursor=".($this->bWaitCursor?"true":"false").";\n";
  487. $html .= "var xajaxDefinedGet=".XAJAX_GET.";\n";
  488. $html .= "var xajaxDefinedPost=".XAJAX_POST.";\n";
  489. foreach($this->aFunctions as $sFunction => $bExists) {
  490. $html .= $this->_wrap($sFunction,$this->aFunctionRequestTypes[$sFunction]);
  491. }
  492. $html .= "</script>\n";
  493. // Create a compressed file if necessary
  494. if ($sJsFullFilename) {
  495. $realJsFile = $sJsFullFilename;
  496. }
  497. else {
  498. $realPath = realpath(dirname(__FILE__));
  499. $realJsFile = $realPath . "/". $sJsFile;
  500. }
  501. $srcFile = str_replace(".js", "_uncompressed.js", $realJsFile);
  502. if (!file_exists($srcFile)) {
  503. trigger_error("The xajax uncompressed Javascript file could not be found in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  504. }
  505. if ($this->bDebug) {
  506. if (!@copy($srcFile, $realJsFile)) {
  507. trigger_error("The xajax uncompressed javascript file could not be copied to the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  508. }
  509. }
  510. else if (!file_exists($realJsFile)) {
  511. require(dirname($realJsFile) . "/xajaxCompress.php");
  512. $javaScript = implode('', file($srcFile));
  513. $compressedScript = xajaxCompressJavascript($javaScript);
  514. $fH = @fopen($realJsFile, "w");
  515. if (!$fH) {
  516. trigger_error("The xajax compressed javascript file could not be written in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  517. }
  518. else {
  519. fwrite($fH, $compressedScript);
  520. fclose($fH);
  521. }
  522. }
  523. $html .= "\t<script type=\"text/javascript\" src=\"" . $sJsURI . $sJsFile . "\"></script>\n";
  524. return $html;
  525. }
  526. // _detectURL() returns the current URL based upon the SERVER vars
  527. // used internally
  528. function _detectURI() {
  529. $aURL = array();
  530. // Try to get the request URL
  531. if (!empty($_SERVER['REQUEST_URI'])) {
  532. $aURL = parse_url($_SERVER['REQUEST_URI']);
  533. }
  534. // Fill in the empty values
  535. if (empty($aURL['scheme'])) {
  536. if (!empty($_SERVER['HTTP_SCHEME'])) {
  537. $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
  538. } else {
  539. $aURL['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
  540. }
  541. }
  542. if (empty($aURL['host'])) {
  543. if (!empty($_SERVER['HTTP_HOST'])) {
  544. if (strpos($_SERVER['HTTP_HOST'], ':') > 0) {
  545. list($aURL['host'], $aURL['port']) = explode(':', $_SERVER['HTTP_HOST']);
  546. } else {
  547. $aURL['host'] = $_SERVER['HTTP_HOST'];
  548. }
  549. } else if (!empty($_SERVER['SERVER_NAME'])) {
  550. $aURL['host'] = $_SERVER['SERVER_NAME'];
  551. } else {
  552. print "xajax Error: xajax failed to automatically identify your Request URI.";
  553. print "Please set the Request URI explicitly when you instantiate the xajax object.";
  554. exit();
  555. }
  556. }
  557. if (empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) {
  558. $aURL['port'] = $_SERVER['SERVER_PORT'];
  559. }
  560. if (empty($aURL['path'])) {
  561. if (!empty($_SERVER['PATH_INFO'])) {
  562. $sPath = parse_url($_SERVER['PATH_INFO']);
  563. } else {
  564. $sPath = parse_url($_SERVER['PHP_SELF']);
  565. }
  566. $aURL['path'] = $sPath['path'];
  567. unset($sPath);
  568. }
  569. if (!empty($aURL['query'])) {
  570. $aURL['query'] = '?'.$aURL['query'];
  571. }
  572. // Build the URL: Start with scheme, user and pass
  573. $sURL = $aURL['scheme'].'://';
  574. if (!empty($aURL['user'])) {
  575. $sURL.= $aURL['user'];
  576. if (!empty($aURL['pass'])) {
  577. $sURL.= ':'.$aURL['pass'];
  578. }
  579. $sURL.= '@';
  580. }
  581. // Add the host
  582. $sURL.= $aURL['host'];
  583. // Add the port if needed
  584. if (!empty($aURL['port']) && (($aURL['scheme'] == 'http' && $aURL['port'] != 80) || ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) {
  585. $sURL.= ':'.$aURL['port'];
  586. }
  587. // Add the path and the query string
  588. $sURL.= $aURL['path'].@$aURL['query'];
  589. // Clean up
  590. unset($aURL);
  591. return $sURL;
  592. }
  593. // returns true if the function name is associated with an object callback,
  594. // false if not.
  595. // user internally
  596. function _isObjectCallback($sFunction)
  597. {
  598. if (array_key_exists($sFunction, $this->aObjects)) return true;
  599. return false;
  600. }
  601. // return true if the function or object callback can be called, false if not
  602. // user internally
  603. function _isFunctionCallable($sFunction)
  604. {
  605. if ($this->_isObjectCallback($sFunction)) {
  606. if (is_object($this->aObjects[$sFunction][0])) {
  607. return method_exists($this->aObjects[$sFunction][0], $this->aObjects[$sFunction][1]);
  608. }
  609. else {
  610. return is_callable($this->aObjects[$sFunction]);
  611. }
  612. }
  613. else {
  614. return function_exists($sFunction);
  615. }
  616. }
  617. // calls the function, class method, or object method with the supplied arguments
  618. // user internally
  619. function _callFunction($sFunction, $aArgs)
  620. {
  621. if ($this->_isObjectCallback($sFunction)) {
  622. $mReturn = call_user_func_array($this->aObjects[$sFunction], $aArgs);
  623. }
  624. else {
  625. $mReturn = call_user_func_array($sFunction, $aArgs);
  626. }
  627. return $mReturn;
  628. }
  629. // generates the javascript wrapper for the specified PHP function
  630. // used internally
  631. function _wrap($sFunction,$sRequestType=XAJAX_POST)
  632. {
  633. $js = "function ".$this->sWrapperPrefix."$sFunction(){return xajax.call(\"$sFunction\", arguments, ".$sRequestType.");}\n";
  634. return $js;
  635. }
  636. // _xmlToArray() takes a string containing xajax xjxobj xml or xjxquery xml
  637. // and builds an array representation of it to pass as an argument to
  638. // the php function being called. Returns an array.
  639. // used internally
  640. function _xmlToArray($rootTag, $sXml)
  641. {
  642. $aArray = array();
  643. $sXml = str_replace("<$rootTag>","<$rootTag>|~|",$sXml);
  644. $sXml = str_replace("</$rootTag>","</$rootTag>|~|",$sXml);
  645. $sXml = str_replace("<e>","<e>|~|",$sXml);
  646. $sXml = str_replace("</e>","</e>|~|",$sXml);
  647. $sXml = str_replace("<k>","<k>|~|",$sXml);
  648. $sXml = str_replace("</k>","|~|</k>|~|",$sXml);
  649. $sXml = str_replace("<v>","<v>|~|",$sXml);
  650. $sXml = str_replace("</v>","|~|</v>|~|",$sXml);
  651. $sXml = str_replace("<q>","<q>|~|",$sXml);
  652. $sXml = str_replace("</q>","|~|</q>|~|",$sXml);
  653. $this->aObjArray = explode("|~|",$sXml);
  654. $this->iPos = 0;
  655. $aArray = $this->_parseObjXml($rootTag);
  656. return $aArray;
  657. }
  658. // _parseObjXml() is a recursive function that generates an array from the
  659. // contents of $this->aObjArray. Returns an array.
  660. // used internally
  661. function _parseObjXml($rootTag)
  662. {
  663. $aArray = array();
  664. if ($rootTag == "xjxobj")
  665. {
  666. while(!stristr($this->aObjArray[$this->iPos],"</xjxobj>"))
  667. {
  668. $this->iPos++;
  669. if(stristr($this->aObjArray[$this->iPos],"<e>"))
  670. {
  671. $key = "";
  672. $value = null;
  673. $this->iPos++;
  674. while(!stristr($this->aObjArray[$this->iPos],"</e>"))
  675. {
  676. if(stristr($this->aObjArray[$this->iPos],"<k>"))
  677. {
  678. $this->iPos++;
  679. while(!stristr($this->aObjArray[$this->iPos],"</k>"))
  680. {
  681. $key .= $this->aObjArray[$this->iPos];
  682. $this->iPos++;
  683. }
  684. }
  685. if(stristr($this->aObjArray[$this->iPos],"<v>"))
  686. {
  687. $this->iPos++;
  688. while(!stristr($this->aObjArray[$this->iPos],"</v>"))
  689. {
  690. if(stristr($this->aObjArray[$this->iPos],"<xjxobj>"))
  691. {
  692. $value = $this->_parseObjXml("xjxobj");
  693. $this->iPos++;
  694. }
  695. else
  696. {
  697. $value .= $this->aObjArray[$this->iPos];
  698. }
  699. $this->iPos++;
  700. }
  701. }
  702. $this->iPos++;
  703. }
  704. $aArray[$key]=$value;
  705. }
  706. }
  707. }
  708. if ($rootTag == "xjxquery")
  709. {
  710. $sQuery = "";
  711. $this->iPos++;
  712. while(!stristr($this->aObjArray[$this->iPos],"</xjxquery>"))
  713. {
  714. if (stristr($this->aObjArray[$this->iPos],"<q>") || stristr($this->aObjArray[$this->iPos],"</q>"))
  715. {
  716. $this->iPos++;
  717. continue;
  718. }
  719. $sQuery .= $this->aObjArray[$this->iPos];
  720. $this->iPos++;
  721. }
  722. parse_str($sQuery, $aArray);
  723. // If magic quotes is on, then we need to strip the slashes from the
  724. // array values because of the parse_str pass which adds slashes
  725. if (get_magic_quotes_gpc() == 1) {
  726. $newArray = array();
  727. foreach ($aArray as $sKey => $sValue) {
  728. if (is_string($sValue))
  729. $newArray[$sKey] = stripslashes($sValue);
  730. else
  731. $newArray[$sKey] = $sValue;
  732. }
  733. $aArray = $newArray;
  734. }
  735. }
  736. return $aArray;
  737. }
  738. }// end class xajax
  739. // xajaxErrorHandler() is registered with PHP's set_error_handler() function if
  740. // the xajax error handling system is turned on
  741. // used by the xajax class
  742. function xajaxErrorHandler($errno, $errstr, $errfile, $errline)
  743. {
  744. $errorReporting = error_reporting();
  745. if ($errorReporting == 0) return;
  746. if ($errno == E_NOTICE) {
  747. $errTypeStr = "NOTICE";
  748. }
  749. else if ($errno == E_WARNING) {
  750. $errTypeStr = "WARNING";
  751. }
  752. else if ($errno == E_USER_NOTICE) {
  753. $errTypeStr = "USER NOTICE";
  754. }
  755. else if ($errno == E_USER_WARNING) {
  756. $errTypeStr = "USER WARNING";
  757. }
  758. else if ($errno == E_USER_ERROR) {
  759. $errTypeStr = "USER FATAL ERROR";
  760. }
  761. else if ($errno == E_STRICT) {
  762. return;
  763. }
  764. else {
  765. $errTypeStr = "UNKNOWN: $errno";
  766. }
  767. $GLOBALS['xajaxErrorHandlerText'] .= "\n----\n[$errTypeStr] $errstr\nerror in line $errline of file $errfile";
  768. }
  769. ?>