diff options
author | Christian Schneider | 2007-10-11 00:39:30 +0000 |
---|---|---|
committer | Christian Schneider | 2007-10-11 00:39:30 +0000 |
commit | 35fe33f7364329dacf415c950bff01b6de9ef88e (patch) | |
tree | b0e6b018b50038ca20266723c53750268f508df5 /itjs.class | |
parent | 1f95711ff3e9697cd85a54545ab42e5fd3611317 (diff) | |
download | itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.gz itools-35fe33f7364329dacf415c950bff01b6de9ef88e.tar.bz2 itools-35fe33f7364329dacf415c950bff01b6de9ef88e.zip |
Populated release branch
Diffstat (limited to 'itjs.class')
-rw-r--r-- | itjs.class | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/itjs.class b/itjs.class new file mode 100644 index 0000000..8828d78 --- /dev/null +++ b/itjs.class @@ -0,0 +1,153 @@ +<?php +/* +** $Id$ +** +** ITools - the Internet Tools Library +** +** Copyright (C) 1995-2004 by the ITools Authors. +** This program is free software; you can redistribute it and/or +** modify it under the terms of either the GNU General Public License +** or the GNU Lesser General Public License, as published by the Free +** Software Foundation. See http://www.gnu.org/licenses/ for details. +** +** itjs.class - functions to handle javascript inclusion +*/ + +class itjs +{ + +/** + * Send HTTP headers (content-type) to transmit javascript code + */ +function send_headers() +{ + if (!preg_match('/Opera/', $_SERVER['HTTP_USER_AGENT']) && !$_REQUEST['itjs_iframe']) # text/plain breaks Opera 8.51/Linux and IFrame fallback + header('Content-Type: text/plain; charset=iso-8859-1'); # Berni reported some Firewalls to require this + + header('Expires: ' . gmdate('D, d M Y H:i:s', time()+10) . ' GMT'); # prevent broken data on IE reloads +} + +/** + * Serialize the result into a javascript script + * @param $values Array with values to be serialized + * @param $envelope Encapsulate the data when callback function is provided (iframe solution) + * @return String with javascript code to be sent to client + */ +function serialize($values, $envelope = false) +{ + if (($envelope || isset($values['eof'])) && ($callback = it::replace(array('[^\w.]' => ""), $_REQUEST['itjs_call']))) + { + $target = $_REQUEST['itjs_iframe'] ? "parent" : "window"; + $header = "$target.it_loader && $target.$callback && $target.$callback.dataReady("; + $footer = "," . intval($_REQUEST['itjs_callid']) . ");"; + + if ($_REQUEST['itjs_iframe']) # iframe-based loading required by Opera 7 + { + $header = '<script type="text/javascript">' . $header; + $footer .= "</script>"; + } + } + + return $header . itjs::encode($values) . $footer; +} + +/* + * Encode the result into a javascript array to transfer and eval() in client + */ +function encode($values) +{ + $texts = ($values === array_values($values)) ? "[]0 " : "{}1\n"; # Numerical or associative array + + $result = $texts{0}; + + foreach ($values as $key => $value) + { + $result .= $separator; + + if ($texts{2}) + { + if (!preg_match('/^[a-z_]\w*$/i', $key)) + $key = "'$key'"; + + $result .= "$key:"; + } + + if ($value === true) + $result .= 'true'; + else if ($value === false) + $result .= 'false'; + else if (!is_array($value)) + { + $quote = (strval(intval($value)) === strval($value)) ? "" : '"'; + $result .= $quote . preg_replace('/([\xa0-\xff])/e', 'sprintf("\\u%04x", ord("\\1"))', strtr($value, array("\0" => '\\0', '"' => '\\"', "</"=>"<\\/", "\n" => '\\n', "\r" => '\\r', "\t" => '\\t', "\\" => '\\\\'))) . $quote; + } + else + $result .= itjs::encode($value); + + $separator = "," . $texts{3}; + } + + $result .= $texts{1}; + + return $result; +} + +function filenames($filelist) +{ + $result = array(); + + $files = array_flip(explode(",", it::replace(array('\?.*' => ""), $filelist))); + $itools = array_flip(array("it.js", "loader.js", "state.js", "timer.js")); + $plainfiles = array_flip(array("boot.js", "state.html", "error.gif")); + + if (isset($files['itools'])) + { + unset($files['itools']); + $files = $itools + $files; # Order is important! + } + + foreach (array_keys($files) as $file) + { + if (isset($itools[$file]) || isset($plainfiles[$file])) + $result[] = "/www/server/phpinclude/itools/itjs/$file"; + else + $result[] = $GLOBALS['ULTRAHOME'] . "/itjs/$file"; + } + + return $result; +} + +/** + * Strip comments and trim lines + * @param $code String containing javascript code to be stripped + * @return Naked code + */ +function strip($code) +{ + if (!preg_match("/^devel/", $GLOBALS['ULTRASERVERTYPE'])) + $code = preg_replace(array('|\s//.*$|m', '|/\*.*?\*/|s', '|^\s+|m'), array(), $code); + + return $code; +} + +/** + * Compute checksum for list of files + * @param $files Array of filenames to calculate checksum for + * @return Checksum for given files + */ +function checksum($files = array()) +{ + foreach (array_merge($files, array('itjs.php', 'itjs.class')) as $file) + { + if (!preg_match("/^devel/", $GLOBALS['ULTRASERVERTYPE'])) + $result += @filesize($file); + else + $result = md5($result . @file_get_contents($file)); + } + + return $result; +} + +} + +?> |