xajax.inc.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. <?php
  2. /**
  3. * xajax.inc.php :: Main xajax class and setup file
  4. *
  5. * xajax version 0.2.4
  6. * copyright (c) 2005 by Jared White & J. Max Wilson
  7. * http://www.xajaxproject.org
  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. * @package chamilo.include.xajax
  32. * @version $Id: xajax.inc.php,v 1.1 2006/07/21 15:29:46 elixir_inter Exp $
  33. * @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
  34. * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License
  35. */
  36. /*
  37. ----------------------------------------------------------------------------
  38. | Online documentation for this class is available on the xajax wiki at: |
  39. | http://wiki.xajaxproject.org/Documentation:xajax.inc.php |
  40. ----------------------------------------------------------------------------
  41. */
  42. /**
  43. * Define XAJAX_DEFAULT_CHAR_ENCODING that is used by both
  44. * the xajax and xajaxResponse classes
  45. */
  46. if (!defined ('XAJAX_DEFAULT_CHAR_ENCODING'))
  47. {
  48. define ('XAJAX_DEFAULT_CHAR_ENCODING', 'utf-8' );
  49. }
  50. require_once(dirname(__FILE__)."/xajaxResponse.inc.php");
  51. /**
  52. * Communication Method Defines
  53. */
  54. if (!defined ('XAJAX_GET'))
  55. {
  56. define ('XAJAX_GET', 0);
  57. }
  58. if (!defined ('XAJAX_POST'))
  59. {
  60. define ('XAJAX_POST', 1);
  61. }
  62. /**
  63. * The xajax class generates the xajax javascript for your page including the
  64. * Javascript wrappers for the PHP functions that you want to call from your page.
  65. * It also handles processing and executing the command messages in the XML responses
  66. * sent back to your page from your PHP functions.
  67. *
  68. * @package chamilo.include.xajax
  69. */
  70. class xajax
  71. {
  72. /**#@+
  73. * @access protected
  74. */
  75. /**
  76. * @var array Array of PHP functions that will be callable through javascript wrappers
  77. */
  78. var $aFunctions;
  79. /**
  80. * @var array Array of object callbacks that will allow Javascript to call PHP methods (key=function name)
  81. */
  82. var $aObjects;
  83. /**
  84. * @var array Array of RequestTypes to be used with each function (key=function name)
  85. */
  86. var $aFunctionRequestTypes;
  87. /**
  88. * @var array Array of Include Files for any external functions (key=function name)
  89. */
  90. var $aFunctionIncludeFiles;
  91. /**
  92. * @var string Name of the PHP function to call if no callable function was found
  93. */
  94. var $sCatchAllFunction;
  95. /**
  96. * @var string Name of the PHP function to call before any other function
  97. */
  98. var $sPreFunction;
  99. /**
  100. * @var string The URI for making requests to the xajax object
  101. */
  102. var $sRequestURI;
  103. /**
  104. * @var string The prefix to prepend to the javascript wraper function name
  105. */
  106. var $sWrapperPrefix;
  107. /**
  108. * @var boolean Show debug messages (default false)
  109. */
  110. var $bDebug;
  111. /**
  112. * @var boolean Show messages in the client browser's status bar (default false)
  113. */
  114. var $bStatusMessages;
  115. /**
  116. * @var boolean Allow xajax to exit after processing a request (default true)
  117. */
  118. var $bExitAllowed;
  119. /**
  120. * @var boolean Use wait cursor in browser (default true)
  121. */
  122. var $bWaitCursor;
  123. /**
  124. * @var boolean Use an special xajax error handler so the errors are sent to the browser properly (default false)
  125. */
  126. var $bErrorHandler;
  127. /**
  128. * @var string Specify what, if any, file xajax should log errors to (and more information in a future release)
  129. */
  130. var $sLogFile;
  131. /**
  132. * @var boolean Clean all output buffers before outputting response (default false)
  133. */
  134. var $bCleanBuffer;
  135. /**
  136. * @var string String containing the character encoding used
  137. */
  138. var $sEncoding;
  139. /**
  140. * @var boolean Decode input request args from UTF-8 (default false)
  141. */
  142. var $bDecodeUTF8Input;
  143. /**
  144. * @var boolean Convert special characters to HTML entities (default false)
  145. */
  146. var $bOutputEntities;
  147. /**
  148. * @var array Array for parsing complex objects
  149. */
  150. var $aObjArray;
  151. /**
  152. * @var integer Position in $aObjArray
  153. */
  154. var $iPos;
  155. /**#@-*/
  156. /**
  157. * Constructor. You can set some extra xajax options right away or use
  158. * individual methods later to set options.
  159. *
  160. * @param string defaults to the current browser URI
  161. * @param string defaults to "xajax_";
  162. * @param string defaults to XAJAX_DEFAULT_CHAR_ENCODING defined above
  163. * @param boolean defaults to false
  164. */
  165. function xajax($sRequestURI="",$sWrapperPrefix="xajax_",$sEncoding=XAJAX_DEFAULT_CHAR_ENCODING,$bDebug=false)
  166. {
  167. $this->aFunctions = array();
  168. $this->aObjects = array();
  169. $this->aFunctionIncludeFiles = array();
  170. $this->sRequestURI = $sRequestURI;
  171. if ($this->sRequestURI == "")
  172. $this->sRequestURI = $this->_detectURI();
  173. $this->sWrapperPrefix = $sWrapperPrefix;
  174. $this->bDebug = $bDebug;
  175. $this->bStatusMessages = false;
  176. $this->bWaitCursor = true;
  177. $this->bExitAllowed = true;
  178. $this->bErrorHandler = false;
  179. $this->sLogFile = "";
  180. $this->bCleanBuffer = false;
  181. $this->setCharEncoding($sEncoding);
  182. $this->bDecodeUTF8Input = false;
  183. $this->bOutputEntities = false;
  184. }
  185. /**
  186. * Sets the URI to which requests will be made.
  187. * <i>Usage:</i> <kbd>$xajax->setRequestURI("http://www.xajaxproject.org");</kbd>
  188. *
  189. * @param string the URI (can be absolute or relative) of the PHP script
  190. * that will be accessed when an xajax request occurs
  191. */
  192. function setRequestURI($sRequestURI)
  193. {
  194. $this->sRequestURI = $sRequestURI;
  195. }
  196. /**
  197. * Sets the prefix that will be appended to the Javascript wrapper
  198. * functions (default is "xajax_").
  199. *
  200. * @param string
  201. */
  202. //
  203. function setWrapperPrefix($sPrefix)
  204. {
  205. $this->sWrapperPrefix = $sPrefix;
  206. }
  207. /**
  208. * Enables debug messages for xajax.
  209. * */
  210. function debugOn()
  211. {
  212. $this->bDebug = true;
  213. }
  214. /**
  215. * Disables debug messages for xajax (default behavior).
  216. */
  217. function debugOff()
  218. {
  219. $this->bDebug = false;
  220. }
  221. /**
  222. * Enables messages in the browser's status bar for xajax.
  223. */
  224. function statusMessagesOn()
  225. {
  226. $this->bStatusMessages = true;
  227. }
  228. /**
  229. * Disables messages in the browser's status bar for xajax (default behavior).
  230. */
  231. function statusMessagesOff()
  232. {
  233. $this->bStatusMessages = false;
  234. }
  235. /**
  236. * Enables the wait cursor to be displayed in the browser (default behavior).
  237. */
  238. function waitCursorOn()
  239. {
  240. $this->bWaitCursor = true;
  241. }
  242. /**
  243. * Disables the wait cursor to be displayed in the browser.
  244. */
  245. function waitCursorOff()
  246. {
  247. $this->bWaitCursor = false;
  248. }
  249. /**
  250. * Enables xajax to exit immediately after processing a request and
  251. * sending the response back to the browser (default behavior).
  252. */
  253. function exitAllowedOn()
  254. {
  255. $this->bExitAllowed = true;
  256. }
  257. /**
  258. * Disables xajax's default behavior of exiting immediately after
  259. * processing a request and sending the response back to the browser.
  260. */
  261. function exitAllowedOff()
  262. {
  263. $this->bExitAllowed = false;
  264. }
  265. /**
  266. * Turns on xajax's error handling system so that PHP errors that occur
  267. * during a request are trapped and pushed to the browser in the form of
  268. * a Javascript alert.
  269. */
  270. function errorHandlerOn()
  271. {
  272. $this->bErrorHandler = true;
  273. }
  274. /**
  275. * Turns off xajax's error handling system (default behavior).
  276. */
  277. function errorHandlerOff()
  278. {
  279. $this->bErrorHandler = false;
  280. }
  281. /**
  282. * Specifies a log file that will be written to by xajax during a request
  283. * (used only by the error handling system at present). If you don't invoke
  284. * this method, or you pass in "", then no log file will be written to.
  285. * <i>Usage:</i> <kbd>$xajax->setLogFile("/xajax_logs/errors.log");</kbd>
  286. */
  287. function setLogFile($sFilename)
  288. {
  289. $this->sLogFile = $sFilename;
  290. }
  291. /**
  292. * Causes xajax to clean out all output buffers before outputting a
  293. * response (default behavior).
  294. */
  295. function cleanBufferOn()
  296. {
  297. $this->bCleanBuffer = true;
  298. }
  299. /**
  300. * Turns off xajax's output buffer cleaning.
  301. */
  302. function cleanBufferOff()
  303. {
  304. $this->bCleanBuffer = false;
  305. }
  306. /**
  307. * Sets the character encoding for the HTTP output based on
  308. * <kbd>$sEncoding</kbd>, which is a string containing the character
  309. * encoding to use. You don't need to use this method normally, since the
  310. * character encoding for the response gets set automatically based on the
  311. * <kbd>XAJAX_DEFAULT_CHAR_ENCODING</kbd> constant.
  312. * <i>Usage:</i> <kbd>$xajax->setCharEncoding("utf-8");</kbd>
  313. *
  314. * @param string the encoding type to use (utf-8, iso-8859-1, etc.)
  315. */
  316. function setCharEncoding($sEncoding)
  317. {
  318. $this->sEncoding = $sEncoding;
  319. }
  320. /**
  321. * Causes xajax to decode the input request args from UTF-8 to the current
  322. * encoding if possible. Either the iconv or mb_string extension must be
  323. * present for optimal functionality.
  324. */
  325. function decodeUTF8InputOn()
  326. {
  327. $this->bDecodeUTF8Input = true;
  328. }
  329. /**
  330. * Turns off decoding the input request args from UTF-8 (default behavior).
  331. */
  332. function decodeUTF8InputOff()
  333. {
  334. $this->bDecodeUTF8Input = false;
  335. }
  336. /**
  337. * Tells the response object to convert special characters to HTML entities
  338. * automatically (only works if the mb_string extension is available).
  339. */
  340. function outputEntitiesOn()
  341. {
  342. $this->bOutputEntities = true;
  343. }
  344. /**
  345. * Tells the response object to output special characters intact. (default
  346. * behavior).
  347. */
  348. function outputEntitiesOff()
  349. {
  350. $this->bOutputEntities = false;
  351. }
  352. /**
  353. * Registers a PHP function or method to be callable through xajax in your
  354. * Javascript. If you want to register a function, pass in the name of that
  355. * function. If you want to register a static class method, pass in an
  356. * array like so:
  357. * <kbd>array("myFunctionName", "myClass", "myMethod")</kbd>
  358. * For an object instance method, use an object variable for the second
  359. * array element (and in PHP 4 make sure you put an & before the variable
  360. * to pass the object by reference). Note: the function name is what you
  361. * call via Javascript, so it can be anything as long as it doesn't
  362. * conflict with any other registered function name.
  363. *
  364. * <i>Usage:</i> <kbd>$xajax->registerFunction("myFunction");</kbd>
  365. * or: <kbd>$xajax->registerFunction(array("myFunctionName", &$myObject, "myMethod"));</kbd>
  366. *
  367. * @param mixed contains the function name or an object callback array
  368. * @param mixed request type (XAJAX_GET/XAJAX_POST) that should be used
  369. * for this function. Defaults to XAJAX_POST.
  370. */
  371. function registerFunction($mFunction,$sRequestType=XAJAX_POST)
  372. {
  373. if (is_array($mFunction)) {
  374. $this->aFunctions[$mFunction[0]] = 1;
  375. $this->aFunctionRequestTypes[$mFunction[0]] = $sRequestType;
  376. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  377. }
  378. else {
  379. $this->aFunctions[$mFunction] = 1;
  380. $this->aFunctionRequestTypes[$mFunction] = $sRequestType;
  381. }
  382. }
  383. /**
  384. * Registers a PHP function to be callable through xajax which is located
  385. * in some other file. If the function is requested the external file will
  386. * be included to define the function before the function is called.
  387. *
  388. * <i>Usage:</i> <kbd>$xajax->registerExternalFunction("myFunction","myFunction.inc.php",XAJAX_POST);</kbd>
  389. *
  390. * @param string contains the function name or an object callback array
  391. * ({@link xajax::registerFunction() see registerFunction} for
  392. * more info on object callback arrays)
  393. * @param string contains the path and filename of the include file
  394. * @param mixed the RequestType (XAJAX_GET/XAJAX_POST) that should be used
  395. * for this function. Defaults to XAJAX_POST.
  396. */
  397. function registerExternalFunction($mFunction,$sIncludeFile,$sRequestType=XAJAX_POST)
  398. {
  399. $this->registerFunction($mFunction, $sRequestType);
  400. if (is_array($mFunction)) {
  401. $this->aFunctionIncludeFiles[$mFunction[0]] = $sIncludeFile;
  402. }
  403. else {
  404. $this->aFunctionIncludeFiles[$mFunction] = $sIncludeFile;
  405. }
  406. }
  407. /**
  408. * Registers a PHP function to be called when xajax cannot find the
  409. * function being called via Javascript. Because this is technically
  410. * impossible when using "wrapped" functions, the catch-all feature is
  411. * only useful when you're directly using the xajax.call() Javascript
  412. * method. Use the catch-all feature when you want more dynamic ability to
  413. * intercept unknown calls and handle them in a custom way.
  414. *
  415. * <i>Usage:</i> <kbd>$xajax->registerCatchAllFunction("myCatchAllFunction");</kbd>
  416. *
  417. * @param string contains the function name or an object callback array
  418. * ({@link xajax::registerFunction() see registerFunction} for
  419. * more info on object callback arrays)
  420. */
  421. function registerCatchAllFunction($mFunction)
  422. {
  423. if (is_array($mFunction)) {
  424. $this->sCatchAllFunction = $mFunction[0];
  425. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  426. }
  427. else {
  428. $this->sCatchAllFunction = $mFunction;
  429. }
  430. }
  431. /**
  432. * Registers a PHP function to be called before xajax calls the requested
  433. * function. xajax will automatically add the request function's response
  434. * to the pre-function's response to create a single response. Another
  435. * feature is the ability to return not just a response, but an array with
  436. * the first element being false (a boolean) and the second being the
  437. * response. In this case, the pre-function's response will be returned to
  438. * the browser without xajax calling the requested function.
  439. *
  440. * <i>Usage:</i> <kbd>$xajax->registerPreFunction("myPreFunction");</kbd>
  441. *
  442. * @param string contains the function name or an object callback array
  443. * ({@link xajax::registerFunction() see registerFunction} for
  444. * more info on object callback arrays)
  445. */
  446. function registerPreFunction($mFunction)
  447. {
  448. if (is_array($mFunction)) {
  449. $this->sPreFunction = $mFunction[0];
  450. $this->aObjects[$mFunction[0]] = array_slice($mFunction, 1);
  451. }
  452. else {
  453. $this->sPreFunction = $mFunction;
  454. }
  455. }
  456. /**
  457. * Returns true if xajax can process the request, false if otherwise.
  458. * You can use this to determine if xajax needs to process the request or
  459. * not.
  460. *
  461. * @return boolean
  462. */
  463. function canProcessRequests()
  464. {
  465. if ($this->getRequestMode() != -1) return true;
  466. return false;
  467. }
  468. /**
  469. * Returns the current request mode (XAJAX_GET or XAJAX_POST), or -1 if
  470. * there is none.
  471. *
  472. * @return mixed
  473. */
  474. function getRequestMode()
  475. {
  476. if (!empty($_GET["xajax"]))
  477. return XAJAX_GET;
  478. if (!empty($_POST["xajax"]))
  479. return XAJAX_POST;
  480. return -1;
  481. }
  482. /**
  483. * This is the main communications engine of xajax. The engine handles all
  484. * incoming xajax requests, calls the apporiate PHP functions (or
  485. * class/object methods) and passes the XML responses back to the
  486. * Javascript response handler. If your RequestURI is the same as your Web
  487. * page then this function should be called before any headers or HTML has
  488. * been sent.
  489. */
  490. function processRequests()
  491. {
  492. $requestMode = -1;
  493. $sFunctionName = "";
  494. $bFoundFunction = true;
  495. $bFunctionIsCatchAll = false;
  496. $sFunctionNameForSpecial = "";
  497. $aArgs = array();
  498. $sPreResponse = "";
  499. $bEndRequest = false;
  500. $sResponse = "";
  501. $requestMode = $this->getRequestMode();
  502. if ($requestMode == -1) return;
  503. if ($requestMode == XAJAX_POST)
  504. {
  505. $sFunctionName = $_POST["xajax"];
  506. if (!empty($_POST["xajaxargs"]))
  507. $aArgs = $_POST["xajaxargs"];
  508. }
  509. else
  510. {
  511. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  512. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  513. header ("Cache-Control: no-cache, must-revalidate");
  514. header ("Pragma: no-cache");
  515. $sFunctionName = $_GET["xajax"];
  516. if (!empty($_GET["xajaxargs"]))
  517. $aArgs = $_GET["xajaxargs"];
  518. }
  519. // Use xajax error handler if necessary
  520. if ($this->bErrorHandler) {
  521. $GLOBALS['xajaxErrorHandlerText'] = "";
  522. set_error_handler("xajaxErrorHandler");
  523. }
  524. if ($this->sPreFunction) {
  525. if (!$this->_isFunctionCallable($this->sPreFunction)) {
  526. $bFoundFunction = false;
  527. $objResponse = new xajaxResponse();
  528. $objResponse->addAlert("Unknown Pre-Function ". $this->sPreFunction);
  529. $sResponse = $objResponse->getXML();
  530. }
  531. }
  532. //include any external dependencies associated with this function name
  533. if (array_key_exists($sFunctionName,$this->aFunctionIncludeFiles))
  534. {
  535. ob_start();
  536. include_once($this->aFunctionIncludeFiles[$sFunctionName]);
  537. ob_end_clean();
  538. }
  539. if ($bFoundFunction) {
  540. $sFunctionNameForSpecial = $sFunctionName;
  541. if (!array_key_exists($sFunctionName, $this->aFunctions))
  542. {
  543. if ($this->sCatchAllFunction) {
  544. $sFunctionName = $this->sCatchAllFunction;
  545. $bFunctionIsCatchAll = true;
  546. }
  547. else {
  548. $bFoundFunction = false;
  549. $objResponse = new xajaxResponse();
  550. $objResponse->addAlert("Unknown Function $sFunctionName.");
  551. $sResponse = $objResponse->getXML();
  552. }
  553. }
  554. else if ($this->aFunctionRequestTypes[$sFunctionName] != $requestMode)
  555. {
  556. $bFoundFunction = false;
  557. $objResponse = new xajaxResponse();
  558. $objResponse->addAlert("Incorrect Request Type.");
  559. $sResponse = $objResponse->getXML();
  560. }
  561. }
  562. if ($bFoundFunction)
  563. {
  564. for ($i = 0; $i < sizeof($aArgs); $i++)
  565. {
  566. // If magic quotes is on, then we need to strip the slashes from the args
  567. if (get_magic_quotes_gpc() == 1 && is_string($aArgs[$i])) {
  568. $aArgs[$i] = stripslashes($aArgs[$i]);
  569. }
  570. if (stristr($aArgs[$i],"<xjxobj>") != false)
  571. {
  572. $aArgs[$i] = $this->_xmlToArray("xjxobj",$aArgs[$i]);
  573. }
  574. else if (stristr($aArgs[$i],"<xjxquery>") != false)
  575. {
  576. $aArgs[$i] = $this->_xmlToArray("xjxquery",$aArgs[$i]);
  577. }
  578. else if ($this->bDecodeUTF8Input)
  579. {
  580. $aArgs[$i] = $this->_decodeUTF8Data($aArgs[$i]);
  581. }
  582. }
  583. if ($this->sPreFunction) {
  584. $mPreResponse = $this->_callFunction($this->sPreFunction, array($sFunctionNameForSpecial, $aArgs));
  585. if (is_array($mPreResponse) && $mPreResponse[0] === false) {
  586. $bEndRequest = true;
  587. $sPreResponse = $mPreResponse[1];
  588. }
  589. else {
  590. $sPreResponse = $mPreResponse;
  591. }
  592. if (is_a($sPreResponse, "xajaxResponse")) {
  593. $sPreResponse = $sPreResponse->getXML();
  594. }
  595. if ($bEndRequest) $sResponse = $sPreResponse;
  596. }
  597. if (!$bEndRequest) {
  598. if (!$this->_isFunctionCallable($sFunctionName)) {
  599. $objResponse = new xajaxResponse();
  600. $objResponse->addAlert("The Registered Function $sFunctionName Could Not Be Found.");
  601. $sResponse = $objResponse->getXML();
  602. }
  603. else {
  604. if ($bFunctionIsCatchAll) {
  605. $aArgs = array($sFunctionNameForSpecial, $aArgs);
  606. }
  607. $sResponse = $this->_callFunction($sFunctionName, $aArgs);
  608. }
  609. if (is_a($sResponse, "xajaxResponse")) {
  610. $sResponse = $sResponse->getXML();
  611. }
  612. if (!is_string($sResponse) || strpos($sResponse, "<xjx>") === FALSE) {
  613. $objResponse = new xajaxResponse();
  614. $objResponse->addAlert("No XML Response Was Returned By Function $sFunctionName.");
  615. $sResponse = $objResponse->getXML();
  616. }
  617. else if ($sPreResponse != "") {
  618. $sNewResponse = new xajaxResponse($this->sEncoding, $this->bOutputEntities);
  619. $sNewResponse->loadXML($sPreResponse);
  620. $sNewResponse->loadXML($sResponse);
  621. $sResponse = $sNewResponse->getXML();
  622. }
  623. }
  624. }
  625. $sContentHeader = "Content-type: text/xml;";
  626. if ($this->sEncoding && strlen(trim($this->sEncoding)) > 0)
  627. $sContentHeader .= " charset=".$this->sEncoding;
  628. header($sContentHeader);
  629. if ($this->bErrorHandler && !empty( $GLOBALS['xajaxErrorHandlerText'] )) {
  630. $sErrorResponse = new xajaxResponse();
  631. $sErrorResponse->addAlert("** PHP Error Messages: **" . $GLOBALS['xajaxErrorHandlerText']);
  632. if ($this->sLogFile) {
  633. $fH = @fopen($this->sLogFile, "a");
  634. if (!$fH) {
  635. $sErrorResponse->addAlert("** Logging Error **\n\nxajax was unable to write to the error log file:\n" . $this->sLogFile);
  636. }
  637. else {
  638. fwrite($fH, "** xajax Error Log - " . strftime("%b %e %Y %I:%M:%S %p") . " **" . $GLOBALS['xajaxErrorHandlerText'] . "\n\n\n");
  639. fclose($fH);
  640. }
  641. }
  642. $sErrorResponse->loadXML($sResponse);
  643. $sResponse = $sErrorResponse->getXML();
  644. }
  645. if ($this->bCleanBuffer) while (@ob_end_clean());
  646. print $sResponse;
  647. if ($this->bErrorHandler) restore_error_handler();
  648. if ($this->bExitAllowed)
  649. exit();
  650. }
  651. /**
  652. * Prints the xajax Javascript header and wrapper code into your page by
  653. * printing the output of the getJavascript() method. It should only be
  654. * called between the <pre><head> </head></pre> tags in your HTML page.
  655. * Remember, if you only want to obtain the result of this function, use
  656. * {@link xajax::getJavascript()} instead.
  657. *
  658. * <i>Usage:</i>
  659. * <code>
  660. * <head>
  661. * ...
  662. * < ?php $xajax->printJavascript(); ? >
  663. * </code>
  664. *
  665. * @param string the relative address of the folder where xajax has been
  666. * installed. For instance, if your PHP file is
  667. * "http://www.myserver.com/myfolder/mypage.php"
  668. * and xajax was installed in
  669. * "http://www.myserver.com/anotherfolder", then $sJsURI
  670. * should be set to "../anotherfolder". Defaults to assuming
  671. * xajax is in the same folder as your PHP file.
  672. * @param string the relative folder/file pair of the xajax Javascript
  673. * engine located within the xajax installation folder.
  674. * Defaults to xajax_js/xajax.js.
  675. */
  676. function printJavascript($sJsURI="", $sJsFile=NULL)
  677. {
  678. print $this->getJavascript($sJsURI, $sJsFile);
  679. }
  680. /**
  681. * Returns the xajax Javascript code that should be added to your HTML page
  682. * between the <kbd><head> </head></kbd> tags.
  683. *
  684. * <i>Usage:</i>
  685. * <code>
  686. * < ?php $xajaxJSHead = $xajax->getJavascript(); ? >
  687. * <head>
  688. * ...
  689. * < ?php echo $xajaxJSHead; ? >
  690. * </code>
  691. *
  692. * @param string the relative address of the folder where xajax has been
  693. * installed. For instance, if your PHP file is
  694. * "http://www.myserver.com/myfolder/mypage.php"
  695. * and xajax was installed in
  696. * "http://www.myserver.com/anotherfolder", then $sJsURI
  697. * should be set to "../anotherfolder". Defaults to assuming
  698. * xajax is in the same folder as your PHP file.
  699. * @param string the relative folder/file pair of the xajax Javascript
  700. * engine located within the xajax installation folder.
  701. * Defaults to xajax_js/xajax.js.
  702. * @return string
  703. */
  704. function getJavascript($sJsURI="", $sJsFile=NULL)
  705. {
  706. $html = $this->getJavascriptConfig();
  707. $html .= $this->getJavascriptInclude($sJsURI, $sJsFile);
  708. return $html;
  709. }
  710. /**
  711. * Returns a string containing inline Javascript that sets up the xajax
  712. * runtime (typically called internally by xajax from get/printJavascript).
  713. *
  714. * @return string
  715. */
  716. function getJavascriptConfig()
  717. {
  718. $html = "\t<script type=\"text/javascript\">\n";
  719. $html .= "var xajaxRequestUri=\"".$this->sRequestURI."\";\n";
  720. $html .= "var xajaxDebug=".($this->bDebug?"true":"false").";\n";
  721. $html .= "var xajaxStatusMessages=".($this->bStatusMessages?"true":"false").";\n";
  722. $html .= "var xajaxWaitCursor=".($this->bWaitCursor?"true":"false").";\n";
  723. $html .= "var xajaxDefinedGet=".XAJAX_GET.";\n";
  724. $html .= "var xajaxDefinedPost=".XAJAX_POST.";\n";
  725. $html .= "var xajaxLoaded=false;\n";
  726. foreach($this->aFunctions as $sFunction => $bExists) {
  727. $html .= $this->_wrap($sFunction,$this->aFunctionRequestTypes[$sFunction]);
  728. }
  729. $html .= "\t</script>\n";
  730. return $html;
  731. }
  732. /**
  733. * Returns a string containing a Javascript include of the xajax.js file
  734. * along with a check to see if the file loaded after six seconds
  735. * (typically called internally by xajax from get/printJavascript).
  736. *
  737. * @param string the relative address of the folder where xajax has been
  738. * installed. For instance, if your PHP file is
  739. * "http://www.myserver.com/myfolder/mypage.php"
  740. * and xajax was installed in
  741. * "http://www.myserver.com/anotherfolder", then $sJsURI
  742. * should be set to "../anotherfolder". Defaults to assuming
  743. * xajax is in the same folder as your PHP file.
  744. * @param string the relative folder/file pair of the xajax Javascript
  745. * engine located within the xajax installation folder.
  746. * Defaults to xajax_js/xajax.js.
  747. * @return string
  748. */
  749. function getJavascriptInclude($sJsURI="", $sJsFile=NULL)
  750. {
  751. if ($sJsFile == NULL) $sJsFile = "xajax_js/xajax.js";
  752. if ($sJsURI != "" && substr($sJsURI, -1) != "/") $sJsURI .= "/";
  753. $html = "\t<script type=\"text/javascript\" src=\"" . $sJsURI . $sJsFile . "\"></script>\n";
  754. $html .= "\t<script type=\"text/javascript\">\n";
  755. $html .= "window.setTimeout(function () { if (!xajaxLoaded) { alert('Error: the xajax Javascript file could not be included. Perhaps the URL is incorrect?\\nURL: {$sJsURI}{$sJsFile}'); } }, 6000);\n";
  756. $html .= "\t</script>\n";
  757. return $html;
  758. }
  759. /**
  760. * This method can be used to create a new xajax.js file out of the
  761. * xajax_uncompressed.js file (which will only happen if xajax.js doesn't
  762. * already exist on the filesystem).
  763. *
  764. * @param string an optional argument containing the full server file path
  765. * of xajax.js.
  766. */
  767. function autoCompressJavascript($sJsFullFilename=NULL)
  768. {
  769. $sJsFile = "xajax_js/xajax.js";
  770. if ($sJsFullFilename) {
  771. $realJsFile = $sJsFullFilename;
  772. }
  773. else {
  774. $realPath = realpath(dirname(__FILE__));
  775. $realJsFile = $realPath . "/". $sJsFile;
  776. }
  777. // Create a compressed file if necessary
  778. if (!file_exists($realJsFile)) {
  779. $srcFile = str_replace(".js", "_uncompressed.js", $realJsFile);
  780. if (!file_exists($srcFile)) {
  781. trigger_error("The xajax uncompressed Javascript file could not be found in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  782. }
  783. require(dirname(__FILE__)."/xajaxCompress.php");
  784. $javaScript = implode('', file($srcFile));
  785. $compressedScript = xajaxCompressJavascript($javaScript);
  786. $fH = @fopen($realJsFile, "w");
  787. if (!$fH) {
  788. trigger_error("The xajax compressed javascript file could not be written in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  789. }
  790. else {
  791. fwrite($fH, $compressedScript);
  792. fclose($fH);
  793. }
  794. }
  795. }
  796. /**
  797. * Returns the current URL based upon the SERVER vars.
  798. *
  799. * @access private
  800. * @return string
  801. */
  802. function _detectURI() {
  803. $aURL = array();
  804. // Try to get the request URL
  805. if (!empty($_SERVER['REQUEST_URI'])) {
  806. $aURL = parse_url($_SERVER['REQUEST_URI']);
  807. }
  808. // Fill in the empty values
  809. if (empty($aURL['scheme'])) {
  810. if (!empty($_SERVER['HTTP_SCHEME'])) {
  811. $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
  812. } else {
  813. $aURL['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
  814. }
  815. }
  816. if (empty($aURL['host'])) {
  817. if (!empty($_SERVER['HTTP_HOST'])) {
  818. if (strpos($_SERVER['HTTP_HOST'], ':') > 0) {
  819. list($aURL['host'], $aURL['port']) = explode(':', $_SERVER['HTTP_HOST']);
  820. } else {
  821. $aURL['host'] = $_SERVER['HTTP_HOST'];
  822. }
  823. } else if (!empty($_SERVER['SERVER_NAME'])) {
  824. $aURL['host'] = $_SERVER['SERVER_NAME'];
  825. } else {
  826. print "xajax Error: xajax failed to automatically identify your Request URI.";
  827. print "Please set the Request URI explicitly when you instantiate the xajax object.";
  828. exit();
  829. }
  830. }
  831. if (empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) {
  832. $aURL['port'] = $_SERVER['SERVER_PORT'];
  833. }
  834. if (empty($aURL['path'])) {
  835. if (!empty($_SERVER['PATH_INFO'])) {
  836. $sPath = parse_url($_SERVER['PATH_INFO']);
  837. } else {
  838. $sPath = parse_url(api_get_self());
  839. }
  840. $aURL['path'] = $sPath['path'];
  841. unset($sPath);
  842. }
  843. if (!empty($aURL['query'])) {
  844. $aURL['query'] = '?'.$aURL['query'];
  845. }
  846. // Build the URL: Start with scheme, user and pass
  847. $sURL = $aURL['scheme'].'://';
  848. if (!empty($aURL['user'])) {
  849. $sURL.= $aURL['user'];
  850. if (!empty($aURL['pass'])) {
  851. $sURL.= ':'.$aURL['pass'];
  852. }
  853. $sURL.= '@';
  854. }
  855. // Add the host
  856. $sURL.= $aURL['host'];
  857. // Add the port if needed
  858. if (!empty($aURL['port']) && (($aURL['scheme'] == 'http' && $aURL['port'] != 80) || ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) {
  859. $sURL.= ':'.$aURL['port'];
  860. }
  861. // Add the path and the query string
  862. $sURL.= $aURL['path'].@$aURL['query'];
  863. // Clean up
  864. unset($aURL);
  865. return $sURL;
  866. }
  867. /**
  868. * Returns true if the function name is associated with an object callback,
  869. * false if not.
  870. *
  871. * @param string the name of the function
  872. * @access private
  873. * @return boolean
  874. */
  875. function _isObjectCallback($sFunction)
  876. {
  877. if (array_key_exists($sFunction, $this->aObjects)) return true;
  878. return false;
  879. }
  880. /**
  881. * Returns true if the function or object callback can be called, false if
  882. * not.
  883. *
  884. * @param string the name of the function
  885. * @access private
  886. * @return boolean
  887. */
  888. function _isFunctionCallable($sFunction)
  889. {
  890. if ($this->_isObjectCallback($sFunction)) {
  891. if (is_object($this->aObjects[$sFunction][0])) {
  892. return method_exists($this->aObjects[$sFunction][0], $this->aObjects[$sFunction][1]);
  893. }
  894. else {
  895. return is_callable($this->aObjects[$sFunction]);
  896. }
  897. }
  898. else {
  899. return function_exists($sFunction);
  900. }
  901. }
  902. /**
  903. * Calls the function, class method, or object method with the supplied
  904. * arguments.
  905. *
  906. * @param string the name of the function
  907. * @param array arguments to pass to the function
  908. * @access private
  909. * @return mixed the output of the called function or method
  910. */
  911. function _callFunction($sFunction, $aArgs)
  912. {
  913. if ($this->_isObjectCallback($sFunction)) {
  914. $mReturn = call_user_func_array($this->aObjects[$sFunction], $aArgs);
  915. }
  916. else {
  917. $mReturn = call_user_func_array($sFunction, $aArgs);
  918. }
  919. return $mReturn;
  920. }
  921. /**
  922. * Generates the Javascript wrapper for the specified PHP function.
  923. *
  924. * @param string the name of the function
  925. * @param mixed the request type
  926. * @access private
  927. * @return string
  928. */
  929. function _wrap($sFunction,$sRequestType=XAJAX_POST)
  930. {
  931. $js = "function ".$this->sWrapperPrefix."$sFunction(){return xajax.call(\"$sFunction\", arguments, ".$sRequestType.");}\n";
  932. return $js;
  933. }
  934. /**
  935. * Takes a string containing xajax xjxobj XML or xjxquery XML and builds an
  936. * array representation of it to pass as an argument to the PHP function
  937. * being called.
  938. *
  939. * @param string the root tag of the XML
  940. * @param string XML to convert
  941. * @access private
  942. * @return array
  943. */
  944. function _xmlToArray($rootTag, $sXml)
  945. {
  946. $aArray = array();
  947. $sXml = str_replace("<$rootTag>","<$rootTag>|~|",$sXml);
  948. $sXml = str_replace("</$rootTag>","</$rootTag>|~|",$sXml);
  949. $sXml = str_replace("<e>","<e>|~|",$sXml);
  950. $sXml = str_replace("</e>","</e>|~|",$sXml);
  951. $sXml = str_replace("<k>","<k>|~|",$sXml);
  952. $sXml = str_replace("</k>","|~|</k>|~|",$sXml);
  953. $sXml = str_replace("<v>","<v>|~|",$sXml);
  954. $sXml = str_replace("</v>","|~|</v>|~|",$sXml);
  955. $sXml = str_replace("<q>","<q>|~|",$sXml);
  956. $sXml = str_replace("</q>","|~|</q>|~|",$sXml);
  957. $this->aObjArray = explode("|~|",$sXml);
  958. $this->iPos = 0;
  959. $aArray = $this->_parseObjXml($rootTag);
  960. return $aArray;
  961. }
  962. /**
  963. * A recursive function that generates an array from the contents of
  964. * $this->aObjArray.
  965. *
  966. * @param string the root tag of the XML
  967. * @access private
  968. * @return array
  969. */
  970. function _parseObjXml($rootTag)
  971. {
  972. $aArray = array();
  973. if ($rootTag == "xjxobj")
  974. {
  975. while(!stristr($this->aObjArray[$this->iPos],"</xjxobj>"))
  976. {
  977. $this->iPos++;
  978. if(stristr($this->aObjArray[$this->iPos],"<e>"))
  979. {
  980. $key = "";
  981. $value = null;
  982. $this->iPos++;
  983. while(!stristr($this->aObjArray[$this->iPos],"</e>"))
  984. {
  985. if(stristr($this->aObjArray[$this->iPos],"<k>"))
  986. {
  987. $this->iPos++;
  988. while(!stristr($this->aObjArray[$this->iPos],"</k>"))
  989. {
  990. $key .= $this->aObjArray[$this->iPos];
  991. $this->iPos++;
  992. }
  993. }
  994. if(stristr($this->aObjArray[$this->iPos],"<v>"))
  995. {
  996. $this->iPos++;
  997. while(!stristr($this->aObjArray[$this->iPos],"</v>"))
  998. {
  999. if(stristr($this->aObjArray[$this->iPos],"<xjxobj>"))
  1000. {
  1001. $value = $this->_parseObjXml("xjxobj");
  1002. $this->iPos++;
  1003. }
  1004. else
  1005. {
  1006. $value .= $this->aObjArray[$this->iPos];
  1007. if ($this->bDecodeUTF8Input)
  1008. {
  1009. $value = $this->_decodeUTF8Data($value);
  1010. }
  1011. }
  1012. $this->iPos++;
  1013. }
  1014. }
  1015. $this->iPos++;
  1016. }
  1017. $aArray[$key]=$value;
  1018. }
  1019. }
  1020. }
  1021. if ($rootTag == "xjxquery")
  1022. {
  1023. $sQuery = "";
  1024. $this->iPos++;
  1025. while(!stristr($this->aObjArray[$this->iPos],"</xjxquery>"))
  1026. {
  1027. if (stristr($this->aObjArray[$this->iPos],"<q>") || stristr($this->aObjArray[$this->iPos],"</q>"))
  1028. {
  1029. $this->iPos++;
  1030. continue;
  1031. }
  1032. $sQuery .= $this->aObjArray[$this->iPos];
  1033. $this->iPos++;
  1034. }
  1035. parse_str($sQuery, $aArray);
  1036. if ($this->bDecodeUTF8Input)
  1037. {
  1038. foreach($aArray as $key => $value)
  1039. {
  1040. $aArray[$key] = $this->_decodeUTF8Data($value);
  1041. }
  1042. }
  1043. // If magic quotes is on, then we need to strip the slashes from the
  1044. // array values because of the parse_str pass which adds slashes
  1045. if (get_magic_quotes_gpc() == 1) {
  1046. $newArray = array();
  1047. foreach ($aArray as $sKey => $sValue) {
  1048. if (is_string($sValue))
  1049. $newArray[$sKey] = stripslashes($sValue);
  1050. else
  1051. $newArray[$sKey] = $sValue;
  1052. }
  1053. $aArray = $newArray;
  1054. }
  1055. }
  1056. return $aArray;
  1057. }
  1058. /**
  1059. * Decodes string data from UTF-8 to the current xajax encoding.
  1060. *
  1061. * @param string data to convert
  1062. * @access private
  1063. * @return string converted data
  1064. */
  1065. function _decodeUTF8Data($sData)
  1066. {
  1067. $sValue = $sData;
  1068. if ($this->bDecodeUTF8Input)
  1069. {
  1070. $sFuncToUse = NULL;
  1071. // An adaptation for the Dokeos LMS, 22-AUG-2009.
  1072. if (function_exists('api_convert_encoding'))
  1073. {
  1074. $sFuncToUse = "api_convert_encoding";
  1075. }
  1076. //if (function_exists('iconv'))
  1077. elseif (function_exists('iconv'))
  1078. //
  1079. {
  1080. $sFuncToUse = "iconv";
  1081. }
  1082. else if (function_exists('mb_convert_encoding'))
  1083. {
  1084. $sFuncToUse = "mb_convert_encoding";
  1085. }
  1086. else if ($this->sEncoding == "ISO-8859-1")
  1087. {
  1088. $sFuncToUse = "utf8_decode";
  1089. }
  1090. else
  1091. {
  1092. trigger_error("The incoming xajax data could not be converted from UTF-8", E_USER_NOTICE);
  1093. }
  1094. if ($sFuncToUse)
  1095. {
  1096. if (is_string($sValue))
  1097. {
  1098. if ($sFuncToUse == "iconv")
  1099. {
  1100. $sValue = iconv("UTF-8", $this->sEncoding.'//TRANSLIT', $sValue);
  1101. }
  1102. else if ($sFuncToUse == "mb_convert_encoding")
  1103. {
  1104. $sValue = mb_convert_encoding($sValue, $this->sEncoding, "UTF-8");
  1105. }
  1106. // Added code, an adaptation for the Dokeos LMS, 22-AUG-2009.
  1107. else if ($sFuncToUse == "api_convert_encoding")
  1108. {
  1109. $sValue = api_convert_encoding($sValue, $this->sEncoding, "UTF-8");
  1110. }
  1111. //
  1112. else
  1113. {
  1114. $sValue = utf8_decode($sValue);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. return $sValue;
  1120. }
  1121. }// end class xajax
  1122. /**
  1123. * This function is registered with PHP's set_error_handler() function if
  1124. * the xajax error handling system is turned on.
  1125. */
  1126. function xajaxErrorHandler($errno, $errstr, $errfile, $errline)
  1127. {
  1128. $errorReporting = error_reporting();
  1129. if (($errno & $errorReporting) == 0) return;
  1130. if ($errno == E_NOTICE) {
  1131. $errTypeStr = "NOTICE";
  1132. }
  1133. else if ($errno == E_WARNING) {
  1134. $errTypeStr = "WARNING";
  1135. }
  1136. else if ($errno == E_USER_NOTICE) {
  1137. $errTypeStr = "USER NOTICE";
  1138. }
  1139. else if ($errno == E_USER_WARNING) {
  1140. $errTypeStr = "USER WARNING";
  1141. }
  1142. else if ($errno == E_USER_ERROR) {
  1143. $errTypeStr = "USER FATAL ERROR";
  1144. }
  1145. else if ($errno == E_STRICT) {
  1146. return;
  1147. }
  1148. else {
  1149. $errTypeStr = "UNKNOWN: $errno";
  1150. }
  1151. $GLOBALS['xajaxErrorHandlerText'] .= "\n----\n[$errTypeStr] $errstr\nerror in line $errline of file $errfile";
  1152. }
  1153. ?>