summaryrefslogtreecommitdiff
path: root/itjs.class
blob: ac56032ac8491e94211626fbe88d9c74d7a7d18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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
{

/**
 * Serialize the result into a javascript script
 * @param $values Array with values to be serialized
 * @param $callback Javascript function to call with data (iframe solution)
 * @return String with javascript code to be sent to client
 */
function serialize($values)
{
	if ($callback = $_REQUEST['itjs_call'])
	{
		$header = "<script type='text/javascript'>$callback(";
		$footer = "," . intval($_REQUEST['itjs_callid']) . ")</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";	# 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 (!is_array($value))
		{
			$quote = (strval(intval($value)) == strval($value)) ? "" : '"';
			$result .= $quote . strtr($value, array("\0" => '\\0', '"' => '\\"', "</"=>"<\\/", "\n" => '\\n', "\r" => '\\r', "\t" => '\\t', "\\" => '\\\\')) . $quote;
		}
		else
			$result .= itjs::encode($value);

		$separator = ",\n";
	}

	$result .= $texts{1};

	return $result;
}

function filenames($filelist)
{
	$result = array();

	$files = array_flip(explode(",", $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;	
}

}

?>