xajax.inc.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. * This method can be used to create a new xajax.js file out of the
  760. * xajax_uncompressed.js file (which will only happen if xajax.js doesn't
  761. * already exist on the filesystem).
  762. *
  763. * @param string an optional argument containing the full server file path
  764. * of xajax.js.
  765. */
  766. function autoCompressJavascript($sJsFullFilename=NULL)
  767. {
  768. $sJsFile = "xajax_js/xajax.js";
  769. if ($sJsFullFilename) {
  770. $realJsFile = $sJsFullFilename;
  771. }
  772. else {
  773. $realPath = realpath(__DIR__);
  774. $realJsFile = $realPath . "/". $sJsFile;
  775. }
  776. // Create a compressed file if necessary
  777. if (!file_exists($realJsFile)) {
  778. $srcFile = str_replace(".js", "_uncompressed.js", $realJsFile);
  779. if (!file_exists($srcFile)) {
  780. trigger_error("The xajax uncompressed Javascript file could not be found in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  781. }
  782. require __DIR__."/xajaxCompress.php";
  783. $javaScript = implode('', file($srcFile));
  784. $compressedScript = xajaxCompressJavascript($javaScript);
  785. $fH = @fopen($realJsFile, "w");
  786. if (!$fH) {
  787. trigger_error("The xajax compressed javascript file could not be written in the <b>" . dirname($realJsFile) . "</b> folder. Error ", E_USER_ERROR);
  788. }
  789. else {
  790. fwrite($fH, $compressedScript);
  791. fclose($fH);
  792. }
  793. }
  794. }
  795. /**
  796. * Returns the current URL based upon the SERVER vars.
  797. *
  798. * @access private
  799. * @return string
  800. */
  801. function _detectURI() {
  802. $aURL = array();
  803. // Try to get the request URL
  804. if (!empty($_SERVER['REQUEST_URI'])) {
  805. $aURL = parse_url($_SERVER['REQUEST_URI']);
  806. }
  807. // Fill in the empty values
  808. if (empty($aURL['scheme'])) {
  809. if (!empty($_SERVER['HTTP_SCHEME'])) {
  810. $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
  811. } else {
  812. $aURL['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
  813. }
  814. }
  815. if (empty($aURL['host'])) {
  816. if (!empty($_SERVER['HTTP_HOST'])) {
  817. if (strpos($_SERVER['HTTP_HOST'], ':') > 0) {
  818. list($aURL['host'], $aURL['port']) = explode(':', $_SERVER['HTTP_HOST']);
  819. } else {
  820. $aURL['host'] = $_SERVER['HTTP_HOST'];
  821. }
  822. } else if (!empty($_SERVER['SERVER_NAME'])) {
  823. $aURL['host'] = $_SERVER['SERVER_NAME'];
  824. } else {
  825. print "xajax Error: xajax failed to automatically identify your Request URI.";
  826. print "Please set the Request URI explicitly when you instantiate the xajax object.";
  827. exit();
  828. }
  829. }
  830. if (empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) {
  831. $aURL['port'] = $_SERVER['SERVER_PORT'];
  832. }
  833. if (empty($aURL['path'])) {
  834. if (!empty($_SERVER['PATH_INFO'])) {
  835. $sPath = parse_url($_SERVER['PATH_INFO']);
  836. } else {
  837. $sPath = parse_url(api_get_self());
  838. }
  839. $aURL['path'] = $sPath['path'];
  840. unset($sPath);
  841. }
  842. if (!empty($aURL['query'])) {
  843. $aURL['query'] = '?'.$aURL['query'];
  844. }
  845. // Build the URL: Start with scheme, user and pass
  846. $sURL = api_get_self();
  847. // Add the path and the query string
  848. $sURL.= @$aURL['query'];
  849. // Clean up
  850. unset($aURL);
  851. return $sURL;
  852. }
  853. /**
  854. * Returns true if the function name is associated with an object callback,
  855. * false if not.
  856. *
  857. * @param string the name of the function
  858. * @access private
  859. * @return boolean
  860. */
  861. function _isObjectCallback($sFunction)
  862. {
  863. if (array_key_exists($sFunction, $this->aObjects)) return true;
  864. return false;
  865. }
  866. /**
  867. * Returns true if the function or object callback can be called, false if
  868. * not.
  869. *
  870. * @param string the name of the function
  871. * @access private
  872. * @return boolean
  873. */
  874. function _isFunctionCallable($sFunction)
  875. {
  876. if ($this->_isObjectCallback($sFunction)) {
  877. if (is_object($this->aObjects[$sFunction][0])) {
  878. return method_exists($this->aObjects[$sFunction][0], $this->aObjects[$sFunction][1]);
  879. }
  880. else {
  881. return is_callable($this->aObjects[$sFunction]);
  882. }
  883. }
  884. else {
  885. return function_exists($sFunction);
  886. }
  887. }
  888. /**
  889. * Calls the function, class method, or object method with the supplied
  890. * arguments.
  891. *
  892. * @param string the name of the function
  893. * @param array arguments to pass to the function
  894. * @access private
  895. * @return mixed the output of the called function or method
  896. */
  897. function _callFunction($sFunction, $aArgs)
  898. {
  899. if ($this->_isObjectCallback($sFunction)) {
  900. $mReturn = call_user_func_array($this->aObjects[$sFunction], $aArgs);
  901. }
  902. else {
  903. $mReturn = call_user_func_array($sFunction, $aArgs);
  904. }
  905. return $mReturn;
  906. }
  907. /**
  908. * Generates the Javascript wrapper for the specified PHP function.
  909. *
  910. * @param string the name of the function
  911. * @param mixed the request type
  912. * @access private
  913. * @return string
  914. */
  915. function _wrap($sFunction,$sRequestType=XAJAX_POST)
  916. {
  917. $js = "function ".$this->sWrapperPrefix."$sFunction(){return xajax.call(\"$sFunction\", arguments, ".$sRequestType.");}\n";
  918. return $js;
  919. }
  920. /**
  921. * Takes a string containing xajax xjxobj XML or xjxquery XML and builds an
  922. * array representation of it to pass as an argument to the PHP function
  923. * being called.
  924. *
  925. * @param string the root tag of the XML
  926. * @param string XML to convert
  927. * @access private
  928. * @return array
  929. */
  930. function _xmlToArray($rootTag, $sXml)
  931. {
  932. $aArray = array();
  933. $sXml = str_replace("<$rootTag>","<$rootTag>|~|",$sXml);
  934. $sXml = str_replace("</$rootTag>","</$rootTag>|~|",$sXml);
  935. $sXml = str_replace("<e>","<e>|~|",$sXml);
  936. $sXml = str_replace("</e>","</e>|~|",$sXml);
  937. $sXml = str_replace("<k>","<k>|~|",$sXml);
  938. $sXml = str_replace("</k>","|~|</k>|~|",$sXml);
  939. $sXml = str_replace("<v>","<v>|~|",$sXml);
  940. $sXml = str_replace("</v>","|~|</v>|~|",$sXml);
  941. $sXml = str_replace("<q>","<q>|~|",$sXml);
  942. $sXml = str_replace("</q>","|~|</q>|~|",$sXml);
  943. $this->aObjArray = explode("|~|",$sXml);
  944. $this->iPos = 0;
  945. $aArray = $this->_parseObjXml($rootTag);
  946. return $aArray;
  947. }
  948. /**
  949. * A recursive function that generates an array from the contents of
  950. * $this->aObjArray.
  951. *
  952. * @param string the root tag of the XML
  953. * @access private
  954. * @return array
  955. */
  956. function _parseObjXml($rootTag)
  957. {
  958. $aArray = array();
  959. if ($rootTag == "xjxobj")
  960. {
  961. while(!stristr($this->aObjArray[$this->iPos],"</xjxobj>"))
  962. {
  963. $this->iPos++;
  964. if(stristr($this->aObjArray[$this->iPos],"<e>"))
  965. {
  966. $key = "";
  967. $value = null;
  968. $this->iPos++;
  969. while(!stristr($this->aObjArray[$this->iPos],"</e>"))
  970. {
  971. if(stristr($this->aObjArray[$this->iPos],"<k>"))
  972. {
  973. $this->iPos++;
  974. while(!stristr($this->aObjArray[$this->iPos],"</k>"))
  975. {
  976. $key .= $this->aObjArray[$this->iPos];
  977. $this->iPos++;
  978. }
  979. }
  980. if(stristr($this->aObjArray[$this->iPos],"<v>"))
  981. {
  982. $this->iPos++;
  983. while(!stristr($this->aObjArray[$this->iPos],"</v>"))
  984. {
  985. if(stristr($this->aObjArray[$this->iPos],"<xjxobj>"))
  986. {
  987. $value = $this->_parseObjXml("xjxobj");
  988. $this->iPos++;
  989. }
  990. else
  991. {
  992. $value .= $this->aObjArray[$this->iPos];
  993. if ($this->bDecodeUTF8Input)
  994. {
  995. $value = $this->_decodeUTF8Data($value);
  996. }
  997. }
  998. $this->iPos++;
  999. }
  1000. }
  1001. $this->iPos++;
  1002. }
  1003. $aArray[$key]=$value;
  1004. }
  1005. }
  1006. }
  1007. if ($rootTag == "xjxquery")
  1008. {
  1009. $sQuery = "";
  1010. $this->iPos++;
  1011. while(!stristr($this->aObjArray[$this->iPos],"</xjxquery>"))
  1012. {
  1013. if (stristr($this->aObjArray[$this->iPos],"<q>") || stristr($this->aObjArray[$this->iPos],"</q>"))
  1014. {
  1015. $this->iPos++;
  1016. continue;
  1017. }
  1018. $sQuery .= $this->aObjArray[$this->iPos];
  1019. $this->iPos++;
  1020. }
  1021. parse_str($sQuery, $aArray);
  1022. if ($this->bDecodeUTF8Input)
  1023. {
  1024. foreach($aArray as $key => $value)
  1025. {
  1026. $aArray[$key] = $this->_decodeUTF8Data($value);
  1027. }
  1028. }
  1029. // If magic quotes is on, then we need to strip the slashes from the
  1030. // array values because of the parse_str pass which adds slashes
  1031. if (get_magic_quotes_gpc() == 1) {
  1032. $newArray = array();
  1033. foreach ($aArray as $sKey => $sValue) {
  1034. if (is_string($sValue))
  1035. $newArray[$sKey] = stripslashes($sValue);
  1036. else
  1037. $newArray[$sKey] = $sValue;
  1038. }
  1039. $aArray = $newArray;
  1040. }
  1041. }
  1042. return $aArray;
  1043. }
  1044. /**
  1045. * Decodes string data from UTF-8 to the current xajax encoding.
  1046. *
  1047. * @param string data to convert
  1048. * @access private
  1049. * @return string converted data
  1050. */
  1051. function _decodeUTF8Data($sData)
  1052. {
  1053. $sValue = $sData;
  1054. if ($this->bDecodeUTF8Input)
  1055. {
  1056. $sFuncToUse = NULL;
  1057. // An adaptation for the Dokeos LMS, 22-AUG-2009.
  1058. if (function_exists('api_convert_encoding'))
  1059. {
  1060. $sFuncToUse = "api_convert_encoding";
  1061. }
  1062. //if (function_exists('iconv'))
  1063. elseif (function_exists('iconv'))
  1064. //
  1065. {
  1066. $sFuncToUse = "iconv";
  1067. }
  1068. else if (function_exists('mb_convert_encoding'))
  1069. {
  1070. $sFuncToUse = "mb_convert_encoding";
  1071. }
  1072. else if ($this->sEncoding == "ISO-8859-1")
  1073. {
  1074. $sFuncToUse = "utf8_decode";
  1075. }
  1076. else
  1077. {
  1078. trigger_error("The incoming xajax data could not be converted from UTF-8", E_USER_NOTICE);
  1079. }
  1080. if ($sFuncToUse)
  1081. {
  1082. if (is_string($sValue))
  1083. {
  1084. if ($sFuncToUse == "iconv")
  1085. {
  1086. $sValue = iconv("UTF-8", $this->sEncoding.'//TRANSLIT', $sValue);
  1087. }
  1088. else if ($sFuncToUse == "mb_convert_encoding")
  1089. {
  1090. $sValue = mb_convert_encoding($sValue, $this->sEncoding, "UTF-8");
  1091. }
  1092. // Added code, an adaptation for the Dokeos LMS, 22-AUG-2009.
  1093. else if ($sFuncToUse == "api_convert_encoding")
  1094. {
  1095. $sValue = api_convert_encoding($sValue, $this->sEncoding, "UTF-8");
  1096. }
  1097. //
  1098. else
  1099. {
  1100. $sValue = utf8_decode($sValue);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. return $sValue;
  1106. }
  1107. }// end class xajax
  1108. /**
  1109. * This function is registered with PHP's set_error_handler() function if
  1110. * the xajax error handling system is turned on.
  1111. */
  1112. function xajaxErrorHandler($errno, $errstr, $errfile, $errline)
  1113. {
  1114. $errorReporting = error_reporting();
  1115. if (($errno & $errorReporting) == 0) return;
  1116. if ($errno == E_NOTICE) {
  1117. $errTypeStr = "NOTICE";
  1118. }
  1119. else if ($errno == E_WARNING) {
  1120. $errTypeStr = "WARNING";
  1121. }
  1122. else if ($errno == E_USER_NOTICE) {
  1123. $errTypeStr = "USER NOTICE";
  1124. }
  1125. else if ($errno == E_USER_WARNING) {
  1126. $errTypeStr = "USER WARNING";
  1127. }
  1128. else if ($errno == E_USER_ERROR) {
  1129. $errTypeStr = "USER FATAL ERROR";
  1130. }
  1131. else if ($errno == E_STRICT) {
  1132. return;
  1133. }
  1134. else {
  1135. $errTypeStr = "UNKNOWN: $errno";
  1136. }
  1137. $GLOBALS['xajaxErrorHandlerText'] .= "\n----\n[$errTypeStr] $errstr\nerror in line $errline of file $errfile";
  1138. }
  1139. ?>