Ejemplo n.º 1
0
 /**
  * Sends the response as a JSON encoded string
  *
  * @param	mixed	The data (usually an array) to send
  */
 public function sendAsJson($data)
 {
     //This function needs to be kept in sync with the implmentation in applicationlight.php
     if (headers_sent($file, $line)) {
         throw new Exception("Cannot send response, headers already sent. File: {$file} Line: {$line}");
     }
     // We need to convert $data charset if we're not using UTF-8
     if (vB5_String::getTempCharset() != 'UTF-8') {
         $data = vB5_String::toCharset($data, vB5_String::getTempCharset(), 'UTF-8');
     }
     //If this is IE9, IE10, or IE11 -- we also need to work around the deliberate attempt to break "is IE" logic by the
     //IE dev team -- we need to send type "text/plain". Yes, we know that's not the standard.
     if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false or strpos($_SERVER['HTTP_USER_AGENT'], 'Trident') !== false)) {
         header('Content-type: text/plain; charset=UTF-8');
     } else {
         header('Content-type: application/json; charset=UTF-8');
     }
     // IE will cache ajax requests, and we need to prevent this - VBV-148
     header('Cache-Control: max-age=0,no-cache,no-store,post-check=0,pre-check=0');
     header('Expires: Sat, 1 Jan 2000 01:00:00 GMT');
     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     header("Pragma: no-cache");
     if (isset($data['template']) and !empty($data['template'])) {
         $data['template'] = $this->outputPage($data['template'], false);
     }
     echo vB5_String::jsonEncode($data);
 }
 /**
  * @static
  * When current lang charset isn't the one in http content_type header
  * this method will convert All Ajax $_POST data into current language charset
  */
 protected static function ajaxCharsetConvert()
 {
     $requestcharset = '';
     if (isset($_SERVER["CONTENT_TYPE"]) and $_SERVER["CONTENT_TYPE"]) {
         if (($pos = strpos(strtoupper($_SERVER["CONTENT_TYPE"]), 'CHARSET')) !== false) {
             $requestcharset = substr(strtoupper($_SERVER["CONTENT_TYPE"]), $pos);
             $temp = explode('=', $requestcharset);
             if (!empty($temp[1])) {
                 $requestcharset = trim($temp[1]);
             }
         }
     }
     if ($requestcharset and vB5_String::getTempCharset() != $requestcharset) {
         $routestring = isset($_REQUEST['routestring']) ? $_REQUEST['routestring'] : null;
         $_COOKIE = vB5_String::toCharset($_COOKIE, $requestcharset);
         $_GET = vB5_String::toCharset($_GET, $requestcharset);
         $_POST = vB5_String::toCharset($_POST, $requestcharset);
         $_REQUEST = vB5_String::toCharset($_REQUEST, $requestcharset);
         if ($routestring !== null) {
             // Preserve the utf-8 encoded route string
             $_REQUEST['routestring'] = $routestring;
             if (isset($_GET['routestring'])) {
                 $_GET['routestring'] = $routestring;
             }
             if (isset($_POST['routestring'])) {
                 $_POST['routestring'] = $routestring;
             }
         }
         // Note we're not doing file names here because they're set via multipart form.
         // So $_SERVER["CONTENT_TYPE"] won't have CHARSET in it.
     }
 }