function output() { global $application; $parameters = func_get_args(); $this->asc_ctor($parameters[1]); $image_data = modApiFunc("Images", "getImageData", $this->image_obj->get_id()); $image_data['image_obj'] = $this->image_obj; loadCoreFile('JsHttpRequest.php'); $this->_Template_Contents = array("IIId" => $parameters[0], "IIimage_data" => JsHttpRequest::php2js($image_data), "EMPTY_IMAGE_SRC" => modApiFunc("Images", "getAZImageSRC", EMPTY_IMAGE_BASENAME), "EMPTY_IMAGE_WIDTH" => EMPTY_IMAGE_WIDTH, "EMPTY_IMAGE_HEIGHT" => EMPTY_IMAGE_HEIGHT, "IMAGE_THUMB_SIZE" => IMAGE_THUMB_SIZE, "EMPTY_IMAGE_ALT_TEXT" => getMsg('IMG', 'EMPTY_IMAGE_ALT_TEXT')); $application->registerAttributes($this->_Template_Contents); $this->mTmplFiller = new TmplFiller(dirname(dirname(__FILE__)) . '/templates_az/'); $res = $this->mTmplFiller->fill("image_input/", "js.tpl.html", array()); $res .= $this->mTmplFiller->fill("image_input/", "container.tpl.html", array()); return $res; }
/** * Convert PHP scalar, array or hash to JS scalar/array/hash. */ function php2js($a) { if (is_null($a)) { return 'null'; } if ($a === false) { return 'false'; } if ($a === true) { return 'true'; } if (is_scalar($a)) { $a = addslashes($a); $a = str_replace("\n", '\\n', $a); $a = str_replace("\r", '\\r', $a); $a = preg_replace('{(</)(script)}i', "\$1'+'\$2", $a); // for FORM loader return "'{$a}'"; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) { $result[] = JsHttpRequest::php2js($v); } return '[ ' . join(', ', $result) . ' ]'; } else { foreach ($a as $k => $v) { $result[] = JsHttpRequest::php2js($k) . ': ' . JsHttpRequest::php2js($v); } return '{ ' . join(', ', $result) . ' }'; } }
/** * Convert a PHP scalar, array or hash to JS scalar/array/hash. This function is * an analog of json_encode(), but it can work with a non-UTF8 input and does not * analyze the passed data. Output format must be fully JSON compatible. * * @param mixed $a Any structure to convert to JS. * @return string JavaScript equivalent structure. */ function php2js($a = false) { if (is_null($a)) { return 'null'; } if ($a === false) { return 'false'; } if ($a === true) { return 'true'; } if (is_scalar($a)) { if (is_float($a)) { // Always use "." for floats. $a = str_replace(",", ".", strval($a)); } // All scalars are converted to strings to avoid indeterminism. // PHP's "1" and 1 are equal for all PHP operators, but // JS's "1" and 1 are not. So if we pass "1" or 1 from the PHP backend, // we should get the same result in the JS frontend (string). // Character replacements for JSON. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\\"')); return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) { $result[] = JsHttpRequest::php2js($v); } return '[ ' . join(', ', $result) . ' ]'; } else { foreach ($a as $k => $v) { $result[] = JsHttpRequest::php2js($k) . ': ' . JsHttpRequest::php2js($v); } return '{ ' . join(', ', $result) . ' }'; } }
/** * Convert a PHP scalar, array or hash to JS scalar/array/hash. This function is * an analog of json_encode(), but it can work with a non-UTF8 input and does not * analyze the passed data. Output format must be fully JSON compatible. * * @param mixed $a Any structure to convert to JS. * @return string JavaScript equivalent structure. */ function php2js($a = false) { if (is_null($a)) { return 'null'; } if ($a === false) { return 'false'; } if ($a === true) { return 'true'; } if (is_scalar($a)) { if (is_float($a)) { $a = str_replace(",", ".", strval($a)); } static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\\"')); return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; } $isList = true; for ($i = 0, reset($a); $i < count($a); $i++, next($a)) { if (key($a) !== $i) { $isList = false; break; } } $result = array(); if ($isList) { foreach ($a as $v) { $result[] = JsHttpRequest::php2js($v); } return '[ ' . join(', ', $result) . ' ]'; } else { foreach ($a as $k => $v) { $result[] = JsHttpRequest::php2js($k) . ': ' . JsHttpRequest::php2js($v); } return '{ ' . join(', ', $result) . ' }'; } }
<?php // Turn on all errors. error_reporting(E_ALL); ini_set('log_errors', true); session_start(); require_once "../../../lib/JsHttpRequest/JsHttpRequest.php"; $JsHttpRequest =& new JsHttpRequest("utf-8"); $q = @$_REQUEST['q']; $_RESULT = array("q" => JsHttpRequest::php2js($q), "hello" => 'Это строка в UTF-8');
} $nItr = 100; // Test iconv() + json_encode(). $t0 = array_sum(explode(" ", microtime())); for ($i = 0; $i < $nItr; $i++) { $s = var_export($a, true); $s = iconv('windows-1251', 'UTF-8', $s); $unp = eval("return {$s};"); $s = json_encode($unp); } $t1 = array_sum(explode(" ", microtime())); echo sprintf("iconv + var_export + eval + json_encode(): %.4f s/itr<br>\n", $k1 = ($t1 - $t0) / $nItr); // Test php2js(). $t0 = array_sum(explode(" ", microtime())); for ($i = 0; $i < $nItr; $i++) { $s = JsHttpRequest::php2js($a); } $t1 = array_sum(explode(" ", microtime())); echo sprintf("php2js(): %.4f s/itr<br>\n", $k2 = ($t1 - $t0) / $nItr); // Test array_walk_recursive(). $BRK = ''; for ($i = 128; $i < 256; $i++) { $BRK .= chr($i); } function toUtf(&$v, $k, $fromEnc) { //if (strpbrk($k, $GLOBALS['BRK']) !== false) return; //if (@iconv($fromEnc, 'UTF-8', $k) !== $k) return; $v = iconv($fromEnc, 'UTF-8', $v); } $t0 = array_sum(explode(" ", microtime()));
// Получаем запрос. $q = @$_REQUEST['q']; // Test when no $_RESULT is assigned, only stdout exists. if (@strpos($q, 'just_stdout') !== false) { echo "some stdout"; exit; } if (@strpos($q, 'empty_stdout') !== false) { exit; } if (@strpos($q, 'null_result') !== false) { $_RESULT = null; exit; } // Формируем результат прямо в виде PHP-массива! $_RESULT = array("q" => JsHttpRequest::php2js($q), "md5" => md5(is_array($q) ? serialize($q) : $q), "hello" => isset($_SESSION['hello']) ? $_SESSION['hello'] : null, "upload" => print_r($_FILES, 1)); if ($q == "session-set") { $_SESSION['test'] = "test_value"; } // Демонстрация отладочных сообщений. if (@strpos($q, 'warning_and_error') !== false) { file_get_contents("non-existent"); callUndefinedFunction(); } if (@strpos($q, 'warning_in_result') !== false) { ob_start(); echo "Test"; file_get_contents("non-existent"); $_RESULT['custom'] = ob_get_clean(); } if (@strpos($q, 'error') !== false) {
<script type="text/javascript" language="JavaScript" src="JsTest/JsTestIterator.js"></script> <script> // Run this function on timeout or on button press - for old Opera // (need time to initialize IFRAME). JsTest.analyzed = true; // workaround for init.php function runAllTests() { <?php $urls = glob('*.phpt'); foreach ($urls as $k => $v) { $urls[$k] = dirname($_SERVER['SCRIPT_NAME']) . "/{$v}" . (isset($_GET['debug']) ? "?debug" : ''); } ?> JsTestIterator.run( window.frames.result, <?php echo JsHttpRequest::php2js($urls); ?> , function(text) { var form = document.getElementById('result_form'); form.result.value = text; form.submit(); } ); } if ((""+document.location).match(/immediate/)) { setTimeout("runAllTests()", 500); } </script> <input type="checkbox" onclick="JsTestIterator.stopOnError = this.checked">Stop on the first error<br>