xajax.inc.php 34 KB

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