Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file AjaxUtil.class.php

Documentation is available at AjaxUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2001, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: AjaxUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  *
  10.  * @package Zikula_Core
  11.  */
  12.  
  13. /**
  14.  * AjaxUtil
  15.  *
  16.  * @package Zikula_Core
  17.  * @subpackage AjaxUtil
  18.  * @author Frank Schummertz
  19.  */
  20. class AjaxUtil
  21. {
  22.     /**
  23.      * error()
  24.      *
  25.      * Immediately stops execution and returns an error message
  26.      *
  27.      * @param error - error text
  28.      * @param code - optional error code, default '400 Bad data'
  29.      * @author Frank Schummertz
  30.      *
  31.      */
  32.     function error($error=''$code='400 Bad data')
  33.     {
  34.         if (!empty($error)) {
  35.             header('HTTP/1.0 ' $code);
  36.             echo DataUtil::convertToUTF8($error);
  37.             pnShutDown();
  38.         }
  39.     }
  40.  
  41.     /**
  42.      * encode data in JSON and return
  43.      * This functions can add a new authid if requested to do so (default).
  44.      * If the supplied args is not an array, it will be converted to an
  45.      * array with 'data' as key.
  46.      * Authid field will always be named 'authid'. Any other field 'authid'
  47.      * will be overwritten!
  48.      * Script execution stops here
  49.      *
  50.      * @param args - string or array of data
  51.      * @param createauthid - create a new authid and send it back to the calling javascript
  52.      * @param xjsonheader - send result in X-JSON: header for prototype.js
  53.      * @author Frank Schummertz
  54.      *
  55.      */
  56.     function output($args$createauthid false$xjsonheader false)
  57.     {
  58.         // check if an error message is set
  59.         $msgs LogUtil::getErrorMessagesText ('<br />');
  60.         if ($msgs != false && !empty($msgs)) {
  61.             AjaxUtil::error($msgs);
  62.         }
  63.  
  64.         // now check if a status message is set
  65.         $msgs LogUtil::getStatusMessagesText ('<br />');
  66.  
  67.         if (!is_array($args)) {
  68.             $data array('data' => $args);
  69.         else {
  70.             $data $args;
  71.         }
  72.         $data['statusmsg'$msgs;
  73.  
  74.         if ($createauthid == true{
  75.             $data['authid'SecurityUtil::generateAuthKey(pnModGetName());
  76.         }
  77.  
  78.         // set locale to en_US to ensure correct decimal delimiters
  79.         if (stristr(getenv('OS')'windows')) {
  80.             setlocale(LC_ALL'eng');
  81.         else {
  82.             setlocale(LC_ALL'en_US');
  83.         }
  84.  
  85.         // convert the data to UTF-8 if not already encoded as such
  86.         // Note: this isn't strict test but relying on the site language pack encoding seems to be a good compromise
  87.         if (_CHARSET != 'UTF-8'{
  88.             $data DataUtil::convertToUTF8($data);
  89.         }
  90.         // correct, but wrong: check PHP version and use internal json_encode if >=5.2.0
  91.         // better in order to satisfy some weird webhosters (like goneo - forget them) who think they know PHP better 
  92.         // than the PHP guys and install >=5.2.0 without JSON-support: check if json_encode() exists
  93.         if (function_exists('json_encode')) {
  94.             // found - use built-in json encoding
  95.             $output json_encode($data);
  96.         else {
  97.             // not found - use external JSON library
  98.             Loader::requireOnce('includes/classes/JSON/JSON.php');
  99.             $json new Services_JSON();
  100.             $output $json->encode($data);
  101.         }
  102.  
  103.         header('HTTP/1.0 200 OK');
  104.         if ($xjsonheader == true{
  105.             header('X-JSON:(' $output ')');
  106.         }
  107.         echo $output;
  108.         pnShutDown();
  109.     }
  110.  
  111. }

Documentation generated on Fri, 18 Jul 2008 21:43:07 +0200 by phpDocumentor 1.4.1