PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sahana/3rd/xajax/xajax.inc.php

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