PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/fortic-php/lib/php/xajax/xajax_core/xajaxResponse.inc.php

https://bitbucket.org/forticpdm/fortic
PHP | 1756 lines | 824 code | 132 blank | 800 comment | 67 complexity | b2d0ba7915be7b84b00ef9b450fc1892 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. File: xajaxResponse.inc.php
  4. Contains the response class.
  5. Title: xajax response class
  6. Please see <copyright.inc.php> for a detailed description, copyright
  7. and license information.
  8. */
  9. /*
  10. @package xajax
  11. @version $Id: xajaxResponse.inc.php 361 2007-05-24 12:48:14Z calltoconstruct $
  12. @copyright Copyright (c) 2005-2006 by Jared White & J. Max Wilson
  13. @license http://www.xajaxproject.org/bsd_license.txt BSD License
  14. */
  15. /*
  16. Class: xajaxResponse
  17. Collect commands to be sent back to the browser in response to a xajax
  18. request. Commands are encoded and packaged in a format that is acceptable
  19. to the response handler from the javascript library running on the client
  20. side.
  21. Common commands include:
  22. - <xajaxResponse->assign>: Assign a value to an elements property.
  23. - <xajaxResponse->append>: Append a value on to an elements property.
  24. - <xajaxResponse->script>: Execute a portion of javascript code.
  25. - <xajaxResponse->call>: Execute an existing javascript function.
  26. - <xajaxResponse->alert>: Display an alert dialog to the user.
  27. Elements are identified by the value of the HTML id attribute. If you do
  28. not see your updates occuring on the browser side, ensure that you are
  29. using the correct id in your response.
  30. */
  31. class xajaxResponse
  32. {
  33. /**#@+
  34. * @access protected
  35. */
  36. /*
  37. Array: aCommands
  38. Stores the commands that will be sent to the browser in the response.
  39. */
  40. var $aCommands;
  41. /*
  42. String: sCharacterEncoding
  43. The name of the encoding method you wish to use when dealing with
  44. special characters. See <xajax->setEncoding> for more information.
  45. */
  46. var $sCharacterEncoding;
  47. /*
  48. Boolean: bOutputEntities
  49. Convert special characters to the HTML equivellent. See also
  50. <xajax->bOutputEntities> and <xajax->setFlag>.
  51. */
  52. var $bOutputEntities;
  53. /*
  54. Mixed: returnValue
  55. A string, array or integer value to be returned to the caller when
  56. using 'synchronous' mode requests. See <xajax->setMode> for details.
  57. */
  58. var $returnValue;
  59. /*
  60. Object: objPluginManager
  61. A reference to the global plugin manager.
  62. */
  63. var $objPluginManager;
  64. /**#@-*/
  65. /*
  66. Constructor: xajaxResponse
  67. Create and initialize a xajaxResponse object.
  68. */
  69. function xajaxResponse()
  70. {
  71. //SkipDebug
  72. if (0 < func_num_args()) {
  73. $objLanguageManager =& xajaxLanguageManager::getInstance();
  74. trigger_error(
  75. $objLanguageManager->getText('XJXRSP:EDERR:01')
  76. , E_USER_ERROR
  77. );
  78. }
  79. //EndSkipDebug
  80. $this->aCommands = array();
  81. $objResponseManager =& xajaxResponseManager::getInstance();
  82. $this->sCharacterEncoding = $objResponseManager->getCharacterEncoding();
  83. $this->bOutputEntities = $objResponseManager->getOutputEntities();
  84. $this->objPluginManager =& xajaxPluginManager::getInstance();
  85. }
  86. /*
  87. Function: setCharacterEncoding
  88. Overrides the default character encoding (or the one specified in the
  89. constructor) to the specified character encoding.
  90. sCharacterEncoding - (string): The encoding method to use for this response.
  91. See also, <xajaxResponse->xajaxResponse>()
  92. Returns:
  93. object - The xajaxResponse object.
  94. */
  95. function setCharacterEncoding($sCharacterEncoding)
  96. {
  97. $this->sCharacterEncoding = $sCharacterEncoding;
  98. return $this;
  99. }
  100. /*
  101. Function: setOutputEntities
  102. Convert special characters to their HTML equivellent automatically
  103. (only works if the mb_string extension is available).
  104. bOption - (boolean): Convert special characters
  105. Returns:
  106. object - The xajaxResponse object.
  107. */
  108. function setOutputEntities($bOutputEntities)
  109. {
  110. $this->bOutputEntities = (boolean)$bOutputEntities;
  111. return $this;
  112. }
  113. /*
  114. Function: plugin
  115. Provides access to registered response plugins. If you are using PHP
  116. 4 or 5, pass the plugin name as the first argument, the plugin method
  117. name as the second argument and subsequent arguments (if any) to be
  118. passed along to the plugin.
  119. Optionally, if you use PHP 5, you can pass just the plugin name as the
  120. first argument and the plugin object will be returned. You can then
  121. access the methods of the plugin directly.
  122. sName - (string): Name of the plugin.
  123. sFunction - (string, optional): The name of the method to call.
  124. arg1...argn - (mixed, optional): Additional arguments to pass on to
  125. the plugin function.
  126. Returns:
  127. object - The plugin specified by sName.
  128. */
  129. function &plugin()
  130. {
  131. $aArgs = func_get_args();
  132. $nArgs = func_num_args();
  133. //SkipDebug
  134. if (false == (0 < $nArgs)) {
  135. $objLanguageManager =& xajaxLanguageManager::getInstance();
  136. trigger_error(
  137. $objLanguageManager->getText('XJXRSP:MPERR:01')
  138. , E_USER_ERROR
  139. );
  140. }
  141. //EndSkipDebug
  142. $sName = array_shift($aArgs);
  143. $objPlugin =& $this->objPluginManager->getPlugin($sName);
  144. if (false === $objPlugin)
  145. {
  146. $bReturn = false;
  147. return $bReturn;
  148. }
  149. $objPlugin->setResponse($this);
  150. if (0 < count($aArgs))
  151. {
  152. $sMethod = array_shift($aArgs);
  153. $aFunction = array(&$objPlugin, $sMethod);
  154. call_user_func_array($aFunction, $aArgs);
  155. }
  156. return $objPlugin;
  157. }
  158. /*
  159. Function: __get
  160. Magic function for PHP 5. Used to permit plugins to be called as if they
  161. where native members of the xajaxResponse instance.
  162. sPluginName - (string): The name of the plugin.
  163. Returns:
  164. object - The plugin specified by sPluginName.
  165. */
  166. function &__get($sPluginName)
  167. {
  168. $objPlugin =& $this->plugin($sPluginName);
  169. return $objPlugin;
  170. }
  171. /*
  172. Function: confirmCommands
  173. Response command that prompts user with [ok] [cancel] style
  174. message box. If the user clicks cancel, the specified
  175. number of response commands following this one, will be
  176. skipped.
  177. iCmdNumber - (integer): The number of commands to skip upon cancel.
  178. sMessage - (string): The message to display to the user.
  179. Returns:
  180. object - The xajaxResponse object.
  181. */
  182. function confirmCommands($iCmdNumber, $sMessage)
  183. {
  184. return $this->addCommand(
  185. array(
  186. 'n'=>'cc',
  187. 't'=>$iCmdNumber
  188. ),
  189. $sMessage
  190. );
  191. }
  192. /*
  193. Function: assign
  194. Response command indicating that the specified value should be
  195. assigned to the given element's attribute.
  196. sTarget - (string): The id of the html element on the browser.
  197. sAttribute - (string): The property to be assigned.
  198. sData - (string): The value to be assigned to the property.
  199. Returns:
  200. object - The <xajaxResponse> object.
  201. Example:
  202. $objResponse->assign("contentDiv", "innerHTML", "Some Text");
  203. */
  204. function assign($sTarget,$sAttribute,$sData)
  205. {
  206. return $this->addCommand(
  207. array(
  208. 'n'=>'as',
  209. 't'=>$sTarget,
  210. 'p'=>$sAttribute
  211. ),
  212. $sData
  213. );
  214. }
  215. /*
  216. Function: append
  217. Response command that indicates the specified data should be appended
  218. to the given element's property.
  219. sTarget - (string): The id of the element to be updated.
  220. sAttribute - (string): The name of the property to be appended to.
  221. sData - (string): The data to be appended to the property.
  222. Returns:
  223. object - The <xajaxResponse> object.
  224. */
  225. function append($sTarget,$sAttribute,$sData)
  226. {
  227. return $this->addCommand(
  228. array(
  229. 'n'=>'ap',
  230. 't'=>$sTarget,
  231. 'p'=>$sAttribute
  232. ),
  233. $sData
  234. );
  235. }
  236. /*
  237. Function: prepend
  238. Response command to prepend the specified value onto the given
  239. element's property.
  240. sTarget - (string): The id of the element to be updated.
  241. sAttribute - (string): The property to be updated.
  242. sData - (string): The value to be prepended.
  243. Returns:
  244. object - The <xajaxResponse> object.
  245. */
  246. function prepend($sTarget,$sAttribute,$sData)
  247. {
  248. return $this->addCommand(
  249. array(
  250. 'n'=>'pp',
  251. 't'=>$sTarget,
  252. 'p'=>$sAttribute
  253. ),
  254. $sData
  255. );
  256. }
  257. /*
  258. Function: replace
  259. Replace a specified value with another value within the given
  260. element's property.
  261. sTarget - (string): The id of the element to update.
  262. sAttribute - (string): The property to be updated.
  263. sSearch - (string): The needle to search for.
  264. sData - (string): The data to use in place of the needle.
  265. */
  266. function replace($sTarget,$sAttribute,$sSearch,$sData)
  267. {
  268. return $this->addCommand(
  269. array(
  270. 'n'=>'rp',
  271. 't'=>$sTarget,
  272. 'p'=>$sAttribute
  273. ),
  274. array(
  275. 's' => $sSearch,
  276. 'r' => $sData
  277. )
  278. );
  279. }
  280. /*
  281. Function: clear
  282. Response command used to clear the specified property of the
  283. given element.
  284. sTarget - (string): The id of the element to be updated.
  285. sAttribute - (string): The property to be clared.
  286. Returns:
  287. object - The <xajaxResponse> object.
  288. */
  289. function clear($sTarget,$sAttribute)
  290. {
  291. return $this->assign(
  292. $sTarget,
  293. $sAttribute,
  294. ''
  295. );
  296. }
  297. /*
  298. Function: contextAssign
  299. Response command used to assign a value to a member of a
  300. javascript object (or element) that is specified by the context
  301. member of the request. The object is referenced using the 'this' keyword
  302. in the sAttribute parameter.
  303. sAttribute - (string): The property to be updated.
  304. sData - (string): The value to assign.
  305. Returns:
  306. object - The <xajaxResponse> object.
  307. */
  308. function contextAssign($sAttribute, $sData)
  309. {
  310. return $this->addCommand(
  311. array(
  312. 'n'=>'c:as',
  313. 'p'=>$sAttribute
  314. ),
  315. $sData
  316. );
  317. }
  318. /*
  319. Function: contextAppend
  320. Response command used to append a value onto the specified member
  321. of the javascript context object (or element) specified by the context
  322. member of the request. The object is referenced using the 'this' keyword
  323. in the sAttribute parameter.
  324. sAttribute - (string): The member to be appended to.
  325. sData - (string): The value to append.
  326. Returns:
  327. object - The <xajaxResponse> object.
  328. */
  329. function contextAppend($sAttribute, $sData)
  330. {
  331. return $this->addCommand(
  332. array(
  333. 'n'=>'c:ap',
  334. 'p'=>$sAttribute
  335. ),
  336. $sData
  337. );
  338. }
  339. /*
  340. Function: contextPrepend
  341. Response command used to prepend the speicified data to the given
  342. member of the current javascript object specified by context in the
  343. current request. The object is access via the 'this' keyword in the
  344. sAttribute parameter.
  345. sAttribute - (string): The member to be updated.
  346. sData - (string): The value to be prepended.
  347. Returns:
  348. object - The <xajaxResponse> object.
  349. */
  350. function contextPrepend($sAttribute, $sData)
  351. {
  352. return $this->addCommand(
  353. array(
  354. 'n'=>'c:pp',
  355. 'p'=>$sAttribute
  356. ),
  357. $sData
  358. );
  359. }
  360. /*
  361. Function: contextClear
  362. Response command used to clear the value of the property specified
  363. in the sAttribute parameter. The member is access via the 'this'
  364. keyword and can be used to update a javascript object specified
  365. by context in the request parameters.
  366. sAttribute - (string): The member to be cleared.
  367. Returns:
  368. object - The <xajaxResponse> object.
  369. */
  370. function contextClear($sAttribute)
  371. {
  372. return $this->contextAssign(
  373. $sAttribute,
  374. ''
  375. );
  376. }
  377. /*
  378. Function: alert
  379. Response command that is used to display an alert message to the user.
  380. sMsg - (string): The message to be displayed.
  381. Returns:
  382. object - The <xajaxResponse> object.
  383. */
  384. function alert($sMsg)
  385. {
  386. return $this->addCommand(
  387. array(
  388. 'n'=>'al'
  389. ),
  390. $sMsg
  391. );
  392. }
  393. function debug($sMessage)
  394. {
  395. return $this->addCommand(
  396. array(
  397. 'n'=>'dbg'
  398. ),
  399. $sMessage
  400. );
  401. }
  402. /*
  403. Function: redirect
  404. Response command that causes the browser to navigate to the specified
  405. URL.
  406. sURL - (string): The relative or fully qualified URL.
  407. iDelay - (integer, optional): Number of seconds to delay before
  408. the redirect occurs.
  409. Returns:
  410. object - The <xajaxResponse> object.
  411. */
  412. function redirect($sURL, $iDelay=0)
  413. {
  414. //we need to parse the query part so that the values are rawurlencode()'ed
  415. //can't just use parse_url() cos we could be dealing with a relative URL which
  416. // parse_url() can't deal with.
  417. $queryStart = strpos($sURL, '?', strrpos($sURL, '/'));
  418. if ($queryStart !== FALSE)
  419. {
  420. $queryStart++;
  421. $queryEnd = strpos($sURL, '#', $queryStart);
  422. if ($queryEnd === FALSE)
  423. $queryEnd = strlen($sURL);
  424. $queryPart = substr($sURL, $queryStart, $queryEnd-$queryStart);
  425. parse_str($queryPart, $queryParts);
  426. $newQueryPart = "";
  427. if ($queryParts)
  428. {
  429. $first = true;
  430. foreach($queryParts as $key => $value)
  431. {
  432. if ($first)
  433. $first = false;
  434. else
  435. $newQueryPart .= ini_get('arg_separator.output');
  436. $newQueryPart .= rawurlencode($key).'='.rawurlencode($value);
  437. }
  438. } else if ($_SERVER['QUERY_STRING']) {
  439. //couldn't break up the query, but there's one there
  440. //possibly "http://url/page.html?query1234" type of query?
  441. //just encode it and hope it works
  442. $newQueryPart = rawurlencode($_SERVER['QUERY_STRING']);
  443. }
  444. $sURL = str_replace($queryPart, $newQueryPart, $sURL);
  445. }
  446. if ($iDelay)
  447. $this->script(
  448. 'window.setTimeout("window.location = \''
  449. . $sURL
  450. . '\';",'
  451. . ($iDelay*1000)
  452. . ');'
  453. );
  454. else
  455. $this->script(
  456. 'window.location = "'
  457. . $sURL
  458. . '";'
  459. );
  460. return $this;
  461. }
  462. /*
  463. Function: script
  464. Response command that is used to execute a portion of javascript on
  465. the browser. The script runs in it's own context, so variables declared
  466. locally, using the 'var' keyword, will no longer be available after the
  467. call. To construct a variable that will be accessable globally, even
  468. after the script has executed, leave off the 'var' keyword.
  469. sJS - (string): The script to execute.
  470. Returns:
  471. object - The <xajaxResponse> object.
  472. */
  473. function script($sJS)
  474. {
  475. return $this->addCommand(
  476. array(
  477. 'n'=>'js'
  478. ),
  479. $sJS
  480. );
  481. }
  482. /*
  483. Function: call
  484. Response command that indicates that the specified javascript
  485. function should be called with the given (optional) parameters.
  486. arg1 - (string): The name of the function to call.
  487. arg2 .. argn - arguments to be passed to the function.
  488. Returns:
  489. object - The <xajaxResponse> object.
  490. */
  491. function call() {
  492. $aArgs = func_get_args();
  493. $sFunc = array_shift($aArgs);
  494. return $this->addCommand(
  495. array(
  496. 'n'=>'jc',
  497. 'f'=>$sFunc
  498. ),
  499. $aArgs
  500. );
  501. }
  502. /*
  503. Function: remove
  504. Response command used to remove an element from the document.
  505. sTarget - (string): The id of the element to be removed.
  506. Returns:
  507. object - The <xajaxResponse> object.
  508. */
  509. function remove($sTarget)
  510. {
  511. return $this->addCommand(
  512. array(
  513. 'n'=>'rm',
  514. 't'=>$sTarget),
  515. ''
  516. );
  517. }
  518. /*
  519. Function: create
  520. Response command used to create a new element on the browser.
  521. sParent - (string): The id of the parent element.
  522. sTag - (string): The tag name to be used for the new element.
  523. sId - (string): The id to assign to the new element.
  524. sType - (string, optional): The type of tag, deprecated, use
  525. <xajaxResponse->createInput> instead.
  526. Returns:
  527. object - The <xajaxResponse> object.
  528. */
  529. function create($sParent, $sTag, $sId, $sType=null)
  530. {
  531. //SkipDebug
  532. if (false === (null === $sType)) {
  533. $objLanguageManager =& xajaxLanguageManager::getInstance();
  534. trigger_error(
  535. $objLanguageManager->getText('XJXRSP:CPERR:01')
  536. , E_USER_WARNING
  537. );
  538. }
  539. //EndSkipDebug
  540. return $this->addCommand(
  541. array(
  542. 'n'=>'ce',
  543. 't'=>$sParent,
  544. 'p'=>$sId
  545. ),
  546. $sTag
  547. );
  548. }
  549. /*
  550. Function: insert
  551. Response command used to insert a new element just prior to the specified
  552. element.
  553. sBefore - (string): The element used as a reference point for the
  554. insertion.
  555. sTag - (string): The tag to be used for the new element.
  556. sId - (string): The id to be used for the new element.
  557. Returns:
  558. object - The <xajaxResponse> object.
  559. */
  560. function insert($sBefore, $sTag, $sId)
  561. {
  562. return $this->addCommand(
  563. array(
  564. 'n'=>'ie',
  565. 't'=>$sBefore,
  566. 'p'=>$sId
  567. ),
  568. $sTag
  569. );
  570. }
  571. /*
  572. Function: insertAfter
  573. Response command used to insert a new element after the specified
  574. one.
  575. sAfter - (string): The id of the element that will be used as a reference
  576. for the insertion.
  577. sTag - (string): The tag name to be used for the new element.
  578. sId - (string): The id to be used for the new element.
  579. Returns:
  580. object - The <xajaxResponse> object.
  581. */
  582. function insertAfter($sAfter, $sTag, $sId)
  583. {
  584. return $this->addCommand(
  585. array(
  586. 'n'=>'ia',
  587. 't'=>$sAfter,
  588. 'p'=>$sId
  589. ),
  590. $sTag
  591. );
  592. }
  593. /*
  594. Function: createInput
  595. Response command used to create an input element on the browser.
  596. sParent - (string): The id of the parent element.
  597. sType - (string): The type of the new input element.
  598. sName - (string): The name of the new input element.
  599. sId - (string): The id of the new element.
  600. Returns:
  601. object - The <xajaxResponse> object.
  602. */
  603. function createInput($sParent, $sType, $sName, $sId)
  604. {
  605. return $this->addCommand(
  606. array(
  607. 'n'=>'ci',
  608. 't'=>$sParent,
  609. 'p'=>$sId,
  610. 'c'=>$sType
  611. ),
  612. $sName
  613. );
  614. }
  615. /*
  616. Function: insertInput
  617. Response command used to insert a new input element preceeding the
  618. specified element.
  619. sBefore - (string): The id of the element to be used as the reference
  620. point for the insertion.
  621. sType - (string): The type of the new input element.
  622. sName - (string): The name of the new input element.
  623. sId - (string): The id of the new input element.
  624. Returns:
  625. object - The <xajaxResponse> object.
  626. */
  627. function insertInput($sBefore, $sType, $sName, $sId)
  628. {
  629. return $this->addCommand(
  630. array(
  631. 'n'=>'ii',
  632. 't'=>$sBefore,
  633. 'p'=>$sId,
  634. 'c'=>$sType
  635. ),
  636. $sName
  637. );
  638. }
  639. /*
  640. Function: insertInputAfter
  641. Response command used to insert a new input element after the
  642. specified element.
  643. sAfter - (string): The id of the element that is to be used
  644. as the insertion point for the new element.
  645. sType - (string): The type of the new input element.
  646. sName - (string): The name of the new input element.
  647. sId - (string): The id of the new input element.
  648. Returns:
  649. object - The <xajaxResponse> object.
  650. */
  651. function insertInputAfter($sAfter, $sType, $sName, $sId)
  652. {
  653. return $this->addCommand(
  654. array(
  655. 'n'=>'iia',
  656. 't'=>$sAfter,
  657. 'p'=>$sId,
  658. 'c'=>$sType
  659. ),
  660. $sName
  661. );
  662. }
  663. /*
  664. Function: setEvent
  665. Response command used to set an event handler on the browser.
  666. sTarget - (string): The id of the element that contains the event.
  667. sEvent - (string): The name of the event.
  668. sScript - (string): The javascript to execute when the event is fired.
  669. Returns:
  670. object - The <xajaxResponse> object.
  671. */
  672. function setEvent($sTarget,$sEvent,$sScript)
  673. {
  674. return $this->addCommand(
  675. array(
  676. 'n'=>'ev',
  677. 't'=>$sTarget,
  678. 'p'=>$sEvent
  679. ),
  680. $sScript
  681. );
  682. }
  683. function addEvent($sTarget,$sEvent,$sScript)
  684. {
  685. return $this->setEvent(
  686. $sTarget,
  687. $sEvent,
  688. $sScript
  689. );
  690. }
  691. /*
  692. Function: addHandler
  693. Response command used to install an event handler on the specified element.
  694. sTarget - (string): The id of the element.
  695. sEvent - (string): The name of the event to add the handler to.
  696. sHandler - (string): The javascript function to call when the event is fired.
  697. You can add more than one event handler to an element's event using this method.
  698. Returns:
  699. object - The <xajaxResponse> object.
  700. */
  701. function addHandler($sTarget,$sEvent,$sHandler)
  702. {
  703. return $this->addCommand(
  704. array(
  705. 'n'=>'ah',
  706. 't'=>$sTarget,
  707. 'p'=>$sEvent
  708. ),
  709. $sHandler
  710. );
  711. }
  712. /*
  713. Function: removeHandler
  714. Response command used to remove an event handler from an element.
  715. sTarget - (string): The id of the element.
  716. sEvent - (string): The name of the event.
  717. sHandler - (string): The javascript function that is called when the
  718. event is fired.
  719. Returns:
  720. object - The <xajaxResponse> object.
  721. */
  722. function removeHandler($sTarget,$sEvent,$sHandler)
  723. {
  724. return $this->addCommand(
  725. array(
  726. 'n'=>'rh',
  727. 't'=>$sTarget,
  728. 'p'=>$sEvent
  729. ),
  730. $sHandler);
  731. }
  732. /*
  733. Function: setFunction
  734. Response command used to construct a javascript function on the browser.
  735. sFunction - (string): The name of the function to construct.
  736. sArgs - (string): Comma separated list of parameter names.
  737. sScript - (string): The javascript code that will become the body of the
  738. function.
  739. Returns:
  740. object - The <xajaxResponse> object.
  741. */
  742. function setFunction($sFunction, $sArgs, $sScript)
  743. {
  744. return $this->addCommand(
  745. array(
  746. 'n'=>'sf',
  747. 'f'=>$sFunction,
  748. 'p'=>$sArgs
  749. ),
  750. $sScript
  751. );
  752. }
  753. /*
  754. Function: wrapFunction
  755. Response command used to construct a wrapper function around
  756. and existing javascript function on the browser.
  757. sFunction - (string): The name of the existing function to wrap.
  758. sArgs - (string): The comma separated list of parameters for the function.
  759. aScripts - (array): An array of javascript code snippets that will
  760. be used to build the body of the function. The first piece of code
  761. specified in the array will occur before the call to the original
  762. function, the second will occur after the original function is called.
  763. sReturnValueVariable - (string): The name of the variable that will
  764. retain the return value from the call to the original function.
  765. Returns:
  766. object - The <xajaxResponse> object.
  767. */
  768. function wrapFunction($sFunction, $sArgs, $aScripts, $sReturnValueVariable)
  769. {
  770. return $this->addCommand(
  771. array(
  772. 'n'=>'wpf',
  773. 'f'=>$sFunction,
  774. 'p'=>$sArgs,
  775. 'c'=>$sReturnValueVariable
  776. ),
  777. $aScripts
  778. );
  779. }
  780. /*
  781. Function: includeScript
  782. Response command used to load a javascript file on the browser.
  783. sFileName - (string): The relative or fully qualified URI of the
  784. javascript file.
  785. Returns:
  786. object - The <xajaxResponse> object.
  787. */
  788. function includeScript($sFileName)
  789. {
  790. return $this->addCommand(
  791. array(
  792. 'n'=>'in'
  793. ),
  794. $sFileName
  795. );
  796. }
  797. /*
  798. Function: includeScriptOnce
  799. Response command used to include a javascript file on the browser
  800. if it has not already been loaded.
  801. sFileName - (string): The relative for fully qualified URI of the
  802. javascript file.
  803. Returns:
  804. object - The <xajaxResponse> object.
  805. */
  806. function includeScriptOnce($sFileName)
  807. {
  808. return $this->addCommand(
  809. array(
  810. 'n'=>'ino'
  811. ),
  812. $sFileName
  813. );
  814. }
  815. /*
  816. Function: removeScript
  817. Response command used to remove a SCRIPT reference to a javascript
  818. file on the browser. Optionally, you can call a javascript function
  819. just prior to the file being unloaded (for cleanup).
  820. sFileName - (string): The relative or fully qualified URI of the
  821. javascript file.
  822. sUnload - (string): Name of a javascript function to call prior
  823. to unlaoding the file.
  824. Returns:
  825. object - The <xajaxResponse> object.
  826. */
  827. function removeScript($sFileName, $sUnload = '') {
  828. $this->addCommand(
  829. array(
  830. 'n'=>'rjs'
  831. ),
  832. ('' != $sUnload)
  833. ? array($sFileName, $sUnload)
  834. : $sFileName
  835. );
  836. return $this;
  837. }
  838. /*
  839. Function: includeCSS
  840. Response command used to include a LINK reference to
  841. the specified CSS file on the browser. This will cause the
  842. browser to load and apply the style sheet.
  843. sFileName - (string): The relative or fully qualified URI of
  844. the css file.
  845. Returns:
  846. object - The <xajaxResponse> object.
  847. */
  848. function includeCSS($sFileName)
  849. {
  850. return $this->addCommand(
  851. array(
  852. 'n'=>'css'
  853. ),
  854. $sFileName
  855. );
  856. }
  857. /*
  858. Function: removeCSS
  859. Response command used to remove a LINK reference to
  860. a CSS file on the browser. This causes the browser to
  861. unload the style sheet, effectively removing the style
  862. changes it caused.
  863. sFileName - (string): The relative or fully qualified URI
  864. of the css file.
  865. Returns:
  866. object - The <xajaxResponse> object.
  867. */
  868. function removeCSS($sFileName)
  869. {
  870. return $this->addCommand(
  871. array(
  872. 'n'=>'rcss'
  873. ),
  874. $sFileName
  875. );
  876. }
  877. /*
  878. Function: waitForCSS
  879. Response command instructing xajax to pause while the CSS
  880. files are loaded. The browser is not typically a multi-threading
  881. application, with regards to javascript code. Therefore, the
  882. CSS files included or removed with <xajaxResponse->includeCSS> and
  883. <xajaxResponse->removeCSS> respectively, will not be loaded or
  884. removed until the browser regains control from the script. This
  885. command returns control back to the browser and pauses the execution
  886. of the response until the CSS files, included previously, are
  887. loaded.
  888. iTimeout - (integer): The number of 1/10ths of a second to pause
  889. before timing out and continuing with the execution of the
  890. response commands.
  891. Returns:
  892. object - The <xajaxResponse> object.
  893. */
  894. function waitForCSS($iTimeout = 600) {
  895. $sData = "";
  896. $this->addCommand(
  897. array(
  898. 'n'=>'wcss',
  899. 'p'=>$iTimeout
  900. ),
  901. $sData
  902. );
  903. return $this;
  904. }
  905. /*
  906. Function: waitFor
  907. Response command instructing xajax to delay execution of the response
  908. commands until a specified condition is met. Note, this returns control
  909. to the browser, so that other script operations can execute. xajax
  910. will continue to monitor the specified condition and, when it evaulates
  911. to true, will continue processing response commands.
  912. script - (string): A piece of javascript code that evaulates to true
  913. or false.
  914. tenths - (integer): The number of 1/10ths of a second to wait before
  915. timing out and continuing with the execution of the response
  916. commands.
  917. Returns:
  918. object - The <xajaxResponse> object.
  919. */
  920. function waitFor($script, $tenths) {
  921. return $this->addCommand(
  922. array(
  923. 'n'=>'wf',
  924. 'p'=>$tenths
  925. ),
  926. $script
  927. );
  928. }
  929. /*
  930. Function: sleep
  931. Response command which instructs xajax to pause execution
  932. of the response commands, returning control to the browser
  933. so it can perform other commands asynchronously. After
  934. the specified delay, xajax will continue execution of the
  935. response commands.
  936. tenths - (integer): The number of 1/10ths of a second to
  937. sleep.
  938. Returns:
  939. object - The <xajaxResponse> object.
  940. */
  941. function sleep($tenths) {
  942. $this->addCommand(
  943. array(
  944. 'n'=>'s',
  945. 'p'=>$tenths
  946. ),
  947. ''
  948. );
  949. return $this;
  950. }
  951. /*
  952. Function: setReturnValue
  953. Stores a value that will be passed back as part of the response.
  954. When making synchronous requests, the calling javascript can
  955. obtain this value immediately as the return value of the
  956. <xajax.call> function.
  957. value - (mixed): Any value.
  958. Returns:
  959. object - The <xajaxResponse> object.
  960. */
  961. function setReturnValue($value) {
  962. $this->returnValue = $this->_encodeArray($value);
  963. return $this;
  964. }
  965. /*
  966. Function: getContentType
  967. Returns the current content type that will be used for the
  968. response packet. (typically: "text/xml")
  969. Returns:
  970. string - The content type.
  971. */
  972. function getContentType()
  973. {
  974. return 'text/xml';
  975. }
  976. /*
  977. Function: getOutput
  978. */
  979. function getOutput()
  980. {
  981. ob_start();
  982. $this->_printHeader_XML();
  983. $this->_printResponse_XML();
  984. return ob_get_clean();
  985. }
  986. /*
  987. Function: printOutput
  988. Prints the output, generated from the commands added to the response,
  989. that will be sent to the browser.
  990. Returns:
  991. string - The textual representation of the response commands.
  992. */
  993. function printOutput()
  994. {
  995. $this->_sendHeaders();
  996. $this->_printHeader_XML();
  997. $this->_printResponse_XML();
  998. }
  999. /*
  1000. Function: _sendHeaders
  1001. Used internally to generate the response headers.
  1002. */
  1003. function _sendHeaders()
  1004. {
  1005. $objArgumentManager =& xajaxArgumentManager::getInstance();
  1006. if (XAJAX_METHOD_GET == $objArgumentManager->getRequestMethod())
  1007. {
  1008. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  1009. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  1010. header ("Cache-Control: no-cache, must-revalidate");
  1011. header ("Pragma: no-cache");
  1012. }
  1013. $sCharacterSet = '';
  1014. if ($this->sCharacterEncoding && 0 < strlen(trim($this->sCharacterEncoding))) {
  1015. $sCharacterSet = '; charset="' . trim($this->sCharacterEncoding) . '"';
  1016. }
  1017. $sContentType = $this->getContentType();
  1018. header('content-type: ' . $sContentType . ' ' . $sCharacterSet);
  1019. }
  1020. /*
  1021. Function: getCommandCount
  1022. Returns:
  1023. integer - The number of commands in the response.
  1024. */
  1025. function getCommandCount()
  1026. {
  1027. return count($this->aCommands);
  1028. }
  1029. /*
  1030. Function: loadCommands
  1031. Merges the response commands from the specified <xajaxResponse>
  1032. object with the response commands in this <xajaxResponse> object.
  1033. mCommands - (object): <xajaxResponse> object.
  1034. bBefore - (boolean): Add the new commands to the beginning
  1035. of the list.
  1036. */
  1037. function loadCommands($mCommands, $bBefore=false)
  1038. {
  1039. if (is_a($mCommands, 'xajaxResponse')) {
  1040. $this->returnValue = $mCommands->returnValue;
  1041. if ($bBefore) {
  1042. $this->aCommands = array_merge($mCommands->aCommands, $this->aCommands);
  1043. }
  1044. else {
  1045. $this->aCommands = array_merge($this->aCommands, $mCommands->aCommands);
  1046. }
  1047. }
  1048. else if (is_array($mCommands)) {
  1049. if ($bBefore) {
  1050. $this->aCommands = array_merge($mCommands, $this->aCommands);
  1051. }
  1052. else {
  1053. $this->aCommands = array_merge($this->aCommands, $mCommands);
  1054. }
  1055. }
  1056. else {
  1057. //SkipDebug
  1058. if (!empty($mCommands)) {
  1059. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1060. trigger_error(
  1061. $objLanguageManager->getText('XJXRSP:LCERR:01')
  1062. , E_USER_ERROR
  1063. );
  1064. }
  1065. //EndSkipDebug
  1066. }
  1067. }
  1068. function absorb($objResponse)
  1069. {
  1070. $this->loadCommands($objResponse);
  1071. }
  1072. /*
  1073. Function: addPluginCommand
  1074. Adds a response command that is generated by a plugin.
  1075. objPlugin - (object): A reference to a plugin object.
  1076. aAttributes - (array): Array containing the attributes for this
  1077. response command.
  1078. mData - (mixed): The data to be sent with this command.
  1079. Returns:
  1080. object - The <xajaxResponse> object.
  1081. */
  1082. function addPluginCommand($objPlugin, $aAttributes, $mData)
  1083. {
  1084. $aAttributes['plg'] = $objPlugin->getName();
  1085. return $this->addCommand($aAttributes, $mData);
  1086. }
  1087. /*
  1088. Function: addCommand
  1089. Add a response command to the array of commands that will
  1090. be sent to the browser.
  1091. aAttributes - (array): Associative array of attributes that
  1092. will describe the command.
  1093. mData - (mixed): The data to be associated with this command.
  1094. Returns:
  1095. object - The <xajaxResponse> command.
  1096. */
  1097. function addCommand($aAttributes, $mData)
  1098. {
  1099. $aAttributes['data'] = $this->_encodeArray($mData);
  1100. $this->aCommands[] = $aAttributes;
  1101. return $this;
  1102. }
  1103. /*
  1104. Function: _printHeader_XML
  1105. Used internally to print XML start tag.
  1106. */
  1107. function _printHeader_XML()
  1108. {
  1109. print '<';
  1110. print '?';
  1111. print 'xml version="1.0"';
  1112. $sEncoding = trim($this->sCharacterEncoding);
  1113. if ($this->sCharacterEncoding && 0 < strlen($sEncoding)) {
  1114. print ' encoding="';
  1115. print $sEncoding;
  1116. print '"';
  1117. }
  1118. print ' ?';
  1119. print '>';
  1120. }
  1121. /*
  1122. Function: _printResponse_XML
  1123. Used internally to generate the command output.
  1124. */
  1125. function _printResponse_XML()
  1126. {
  1127. print '<';
  1128. print 'xjx>';
  1129. if (null !== $this->returnValue)
  1130. {
  1131. print '<';
  1132. print 'xjxrv>';
  1133. $this->_printArray_XML($this->returnValue);
  1134. print '<';
  1135. print '/xjxrv>';
  1136. }
  1137. foreach(array_keys($this->aCommands) as $sKey)
  1138. $this->_printCommand_XML($this->aCommands[$sKey]);
  1139. print '<';
  1140. print '/xjx>';
  1141. }
  1142. /*
  1143. Function: _printCommand_XML
  1144. Prints an XML representation of the command.
  1145. aAttributes - (array): Associative array of attributes for this
  1146. command.
  1147. */
  1148. function _printCommand_XML(&$aAttributes)
  1149. {
  1150. print '<';
  1151. print 'cmd';
  1152. $mData = '';
  1153. foreach (array_keys($aAttributes) as $sKey) {
  1154. if ($sKey) {
  1155. if ('data' != $sKey) {
  1156. print ' ';
  1157. print $sKey;
  1158. print '="';
  1159. print $aAttributes[$sKey];
  1160. print '"';
  1161. } else
  1162. $mData =& $aAttributes[$sKey];
  1163. }
  1164. }
  1165. print '>';
  1166. $this->_printArray_XML($mData);
  1167. print '<';
  1168. print '/cmd>';
  1169. }
  1170. /*
  1171. Function: _printArray_XML
  1172. Prints an XML representation of a php array suitable
  1173. for inclusion in the response to the browser. Arrays
  1174. sent via this method will be converted into a javascript
  1175. array on the browser.
  1176. mArray - (array): Array to be converted.
  1177. */
  1178. function _printArray_XML(&$mArray) {
  1179. if ('object' == gettype($mArray))
  1180. $mArray = get_object_vars($mArray);
  1181. if (false == is_array($mArray)) {
  1182. $this->_printEscapedString_XML($mArray);
  1183. return;
  1184. }
  1185. print '<';
  1186. print 'xjxobj>';
  1187. foreach (array_keys($mArray) as $sKey) {
  1188. if (is_array($mArray[$sKey])) {
  1189. print '<';
  1190. print 'e>';
  1191. foreach (array_keys($mArray[$sKey]) as $sInnerKey) {
  1192. //SkipDebug
  1193. if (htmlspecialchars($sInnerKey, ENT_COMPAT, 'UTF-8') != $sInnerKey) {
  1194. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1195. trigger_error(
  1196. $objLanguageManager->getText('XJXRSP:AKERR:01')
  1197. , E_USER_ERROR
  1198. );
  1199. }
  1200. //EndSkipDebug
  1201. if ('k' == $sInnerKey || 'v' == $sInnerKey) {
  1202. print '<';
  1203. print $sInnerKey;
  1204. print '>';
  1205. $this->_printArray_XML($mArray[$sKey][$sInnerKey]);
  1206. print '<';
  1207. print '/';
  1208. print $sInnerKey;
  1209. print '>';
  1210. } else {
  1211. //SkipDebug
  1212. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1213. trigger_error(
  1214. $objLanguageManager->getText('XJXRSP:IEAERR:01')
  1215. , E_USER_ERROR
  1216. );
  1217. //EndSkipDebug
  1218. }
  1219. }
  1220. print '<';
  1221. print '/e>';
  1222. } else {
  1223. //SkipDebug
  1224. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1225. trigger_error(
  1226. $objLanguageManager->getText('XJXRSP:NEAERR:01')
  1227. , E_USER_ERROR
  1228. );
  1229. //EndSkipDebug
  1230. }
  1231. }
  1232. print '<';
  1233. print '/xjxobj>';
  1234. }
  1235. /*
  1236. Function: _printEscapedString_XML
  1237. Escape the specified data if necessary, so special characters in the
  1238. command data does not interfere with the structure of the response.
  1239. This could be overridden to allow for transport encodings other than
  1240. XML.
  1241. sData - (string): The data to be escaped.
  1242. Returns:
  1243. string - The escaped data.
  1244. */
  1245. function _printEscapedString_XML(&$sData)
  1246. {
  1247. if (null === $sData) {
  1248. print '';
  1249. return;
  1250. }
  1251. if ($this->bOutputEntities) {
  1252. //SkipDebug
  1253. if (false === function_exists('mb_convert_encoding')) {
  1254. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1255. trigger_error(
  1256. $objLanguageManager->getText('XJXRSP:MBEERR:01')
  1257. , E_USER_NOTICE
  1258. );
  1259. }
  1260. //EndSkipDebug
  1261. print call_user_func_array('mb_convert_encoding', array(&$sData, 'HTML-ENTITIES', $this->sCharacterEncoding));
  1262. return;
  1263. }
  1264. $nCDATA = 0;
  1265. $bNoOpenCDATA = (false === strpos($sData, '<'.'![CDATA['));
  1266. if ($bNoOpenCDATA) {
  1267. $bNoCloseCDATA = (false === strpos($sData, ']]>'));
  1268. if ($bNoCloseCDATA) {
  1269. $bSpecialChars = (htmlspecialchars($sData, ENT_COMPAT, 'UTF-8') != $sData);
  1270. if ($bSpecialChars)
  1271. $nCDATA = 1;
  1272. } else
  1273. $nCDATA = 2;
  1274. } else
  1275. $nCDATA = 2;
  1276. if (0 < $nCDATA) {
  1277. print '<';
  1278. print '![CDATA[';
  1279. if (1 < $nCDATA) {
  1280. $aSegments = explode('<'.'![CDATA[', $sData);
  1281. $aOutput = array();
  1282. $nOutput = 0;
  1283. foreach (array_keys($aSegments) as $keySegment) {
  1284. $aFragments = explode(']]>', $aSegments[$keySegment]);
  1285. $aStack = array();
  1286. $nStack = 0;
  1287. foreach (array_keys($aFragments) as $keyFragment) {
  1288. if (0 < $nStack)
  1289. array_push($aStack, ']]]]><', '![CDATA[>', $aFragments[$keyFragment]);
  1290. else
  1291. $aStack[] = $aFragments[$keyFragment];
  1292. ++$nStack;
  1293. }
  1294. if (0 < $nOutput)
  1295. array_push($aOutput, '<', '![]]><', '![CDATA[CDATA[', implode('', $aStack));
  1296. else
  1297. $aOutput[] = implode('', $aStack);
  1298. ++$nOutput;
  1299. }
  1300. print implode('', $aOutput);
  1301. } else
  1302. print $sData;
  1303. print ']]>';
  1304. } else
  1305. print $sData;
  1306. }
  1307. /*
  1308. Function: _encodeArray
  1309. Recursively serializes a data structure in an array so that it can
  1310. be sent to the browser. This can be thought of as the opposite of
  1311. <xajaxRequestProcessorPlugin->_parseObjXml>.
  1312. mData - (mixed): The data to be evaluated.
  1313. Returns:
  1314. mixed - The object constructed from the data.
  1315. */
  1316. function _encodeArray(&$mData) {
  1317. if ('object' === gettype($mData))
  1318. $mData = get_object_vars($mData);
  1319. if (false === is_array($mData))
  1320. return $mData;
  1321. $aData = array();
  1322. foreach (array_keys($mData) as $sKey)
  1323. $aData[] = array(
  1324. // key does not need to be encoded
  1325. 'k'=>$sKey,
  1326. 'v'=>$this->_encodeArray($mData[$sKey])
  1327. );
  1328. return $aData;
  1329. }
  1330. }// end class xajaxResponse
  1331. class xajaxCustomResponse
  1332. {
  1333. var $sOutput;
  1334. var $sContentType;
  1335. var $sCharacterEncoding;
  1336. var $bOutputEntities;
  1337. function xajaxCustomResponse($sContentType)
  1338. {
  1339. $this->sOutput = '';
  1340. $this->sContentType = $sContentType;
  1341. $objResponseManager =& xajaxResponseManager::getInstance();
  1342. $this->sCharacterEncoding = $objResponseManager->getCharacterEncoding();
  1343. $this->bOutputEntities = $objResponseManager->getOutputEntities();
  1344. }
  1345. function setCharacterEncoding($sCharacterEncoding)
  1346. {
  1347. $this->sCharacterEncoding = $sCharacterEncoding;
  1348. }
  1349. function setOutputEntities($bOutputEntities)
  1350. {
  1351. $this->bOutputEntities = $bOutputEntities;
  1352. }
  1353. function clear()
  1354. {
  1355. $this->sOutput = '';
  1356. }
  1357. function append($sOutput)
  1358. {
  1359. $this->sOutput .= $sOutput;
  1360. }
  1361. function absorb($objResponse)
  1362. {
  1363. //SkipDebug
  1364. if (false == is_a($objResponse, 'xajaxCustomResponse')) {
  1365. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1366. trigger_error(
  1367. $objLanguageManager->getText('XJXRSP:MXRTERR')
  1368. , E_USER_ERROR
  1369. );
  1370. }
  1371. if ($objResponse->getContentType() != $this->getContentType()) {
  1372. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1373. trigger_error(
  1374. $objLanguageManager->getText('XJXRSP:MXCTERR')
  1375. , E_USER_ERROR
  1376. );
  1377. }
  1378. if ($objResponse->getCharacterEncoding() != $this->getCharacterEncoding()) {
  1379. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1380. trigger_error(
  1381. $objLanguageManager->getText('XJXRSP:MXCEERR')
  1382. , E_USER_ERROR
  1383. );
  1384. }
  1385. if ($objResponse->getOutputEntities() != $this->getOutputEntities()) {
  1386. $objLanguageManager =& xajaxLanguageManager::getInstance();
  1387. trigger_error(
  1388. $objLanguageManager->getText('XJXRSP:MXOEERR')
  1389. , E_USER_ERROR
  1390. );
  1391. }
  1392. //EndSkipDebug
  1393. $this->sOutput .= $objResponse->getOutput();
  1394. }
  1395. function getContentType()
  1396. {
  1397. return $this->sContentType;
  1398. }
  1399. function getCharacterEncoding()
  1400. {
  1401. return $this->sCharacterEncoding;
  1402. }
  1403. function getOutputEntities()
  1404. {
  1405. return $this->bOutputEntities;
  1406. }
  1407. function getOutput()
  1408. {
  1409. return $this->sOutput;
  1410. }
  1411. function printOutput()
  1412. {
  1413. $sContentType = $this->sContentType;
  1414. $sCharacterSet = $this->sCharacterEncoding;
  1415. header("content-type: {$sContentType}; charset={$sCharacterSet}");
  1416. print $this->sOutput;
  1417. }
  1418. }