summaryrefslogtreecommitdiff
path: root/it_auto_prepend.php
blob: d65bc7bfad49cd50c07f723e0b3fabcea1bf44d5 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/*
**	Copyright (C) 1995-2021 by the ITools Authors.
**	This file is part of ITools - the Internet Tools Library
**
**	ITools is free software; you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation; either version 3 of the License, or
**	(at your option) any later version.
**
**	ITools is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$GLOBALS['ULTRATIME'] = microtime(true);
unset($GLOBALS['IT_SYNTAXCONVERTER_DIR']);	# Security measure for register_globals on

#$debug_itclassloader = true;
if ($it_convertedmainfile = it_initialize())
{
	require($it_convertedmainfile);
	exit;
}

function it_initialize()
{
	$result = false;
	static $it_initrecursion;

	if (!$it_initrecursion++)
	{
		register_shutdown_function('it_shutdown');
		$error_reporting = error_reporting();
		error_reporting($error_reporting & ~E_NOTICE);	# ITools is not (unpatched) E_NOTICE safe
		$it_path = dirname(__FILE__);

		if ($webmode = $_SERVER['REMOTE_ADDR'])	# Web?
		{
			$GLOBALS['ULTRAHOME'] = dirname($_SERVER['DOCUMENT_ROOT']);
			umask(0002); # Work around bugs.php.net/bug.php?id=28401
			$_SERVER['HTTP_HOST'] = strtolower($_SERVER['HTTP_HOST']);
		}
		else	# Shell
			$GLOBALS['ULTRAHOME'] = dirname(dirname(preg_match('|^/|', $argv[0]) ? $argv[0] : getcwd() . '/' . $argv[0]));

		$include_path = ini_get('include_path');

		@set_error_handler("it_errorhandler", E_USER_ERROR | E_RECOVERABLE_ERROR | E_WARNING | E_USER_WARNING | E_NOTICE | E_USER_NOTICE);
		error_reporting($error_reporting);	# Restore user setting once we installed error handler

		if (function_exists('spl_autoload_register') && spl_autoload_register('it_classloader'))
		{
			ini_set('include_path', $it_path . PATH_SEPARATOR . $include_path);
			require_once("$it_path/auto_prepend.php");

			if (file_exists("$it_path/auto_prepend_local.php"))
				require_once("$it_path/auto_prepend_local.php");
		}
		else
		{
			ini_set('include_path', "$it_path/.." . PATH_SEPARATOR . $include_path);
			require("itools.lib");	# PHP 4 fallback
		}

		# IT_HOME is recommended variable name for applications
		$GLOBALS['IT_HOME'] = $GLOBALS['ULTRAHOME'] = it_untaint($GLOBALS['ULTRAHOME'], TC_ALL);

		try { $needsconvert = $GLOBALS['IT_SYNTAXCONVERTER_DIR'] !== false && !@eval("return is_array(42=>69,);"); } catch (Error $needsconvert) {}	# Check if PHP is patched to support our syntax, see http://cschneid.com/php/

		if ($needsconvert && !$GLOBALS['IT_SYNTAXCONVERTER_DIR'])
		{
			$GLOBALS['IT_SYNTAXCONVERTER_DIR'] = $GLOBALS['ULTRAHOME'] . "/tmp";

			if (!is_writeable($GLOBALS['IT_SYNTAXCONVERTER_DIR']) || !function_exists("posix_geteuid") || posix_geteuid() != fileowner($GLOBALS['IT_SYNTAXCONVERTER_DIR']))
				die(($webmode ? "<pre>" : "") . "Seems to be running in shared environment, manually set\n\$GLOBALS['IT_SYNTAXCONVERTER_DIR'] in $it_path/auto_prepend_local.php\nto either:\n  a) FALSE (syntax conversion disabled) or\n  b) the path to a writeable directory (NOTE: THIS IS UNSAFE!) or\n  c) install the PHP patch from http://cschneid.com/php/\n" . ($webmode ? "</pre>" : ""));
		}

		ini_set('include_path', ($GLOBALS['IT_SYNTAXCONVERTER_DIR'] ? $GLOBALS['IT_SYNTAXCONVERTER_DIR'] . "/it_syntaxconverter" . PATH_SEPARATOR : '') . $it_path . PATH_SEPARATOR . $include_path);
		$user_includes = explode(PATH_SEPARATOR, $include_path);

		# XXX Note: Comment this out if you want system wide include path converted and auto_prepend.php considered
		$user_includes = array_diff($user_includes, array_diff(explode(PATH_SEPARATOR, get_cfg_var('include_path')), array(".")));

		foreach (array_reverse($user_includes) as $include)
		{
			if ($include == ".")
				$include = dirname($_SERVER['SCRIPT_FILENAME']);

			if ($needsconvert)
				it_convert($include);

			if (@file_exists($autoprepend = "$include/auto_prepend.php"))
			{
				if ($needsconvert && ($include != $it_path))
					$autoprepend = it_convert($autoprepend);

				require_once($autoprepend);

				if (file_exists("$include/auto_prepend_local.php"))
					require_once("$include/auto_prepend_local.php");
			}
		}

		if (!isset($GLOBALS['it_html']))
			new it_html;

		if ($needsconvert)
		{
			# Convert syntax (we will return name of converted script to global context to start it there)
			$result = it_convert(it_untaint($_SERVER['SCRIPT_FILENAME'], TC_SELF));
		}
	}

	return $result;
}

function it_convert($source)
{
	$sourcerelpath = it::replace(array('^(/|\w:)' => ""), $source);	# Remove leading / or C:

	if (@is_dir($source))
	{
		if (!@is_link($source) && !file_exists("$source/.no_it_syntaxconvert"))
		{
			ini_set('include_path',  $GLOBALS['IT_SYNTAXCONVERTER_DIR'] . "/it_syntaxconverter/$sourcerelpath" . PATH_SEPARATOR . ini_get('include_path'));
			foreach (glob("$source/*") as $file)
				it_convert($file);
		}
	}
	else if (@is_readable($source))
	{
		$converted = $GLOBALS['IT_SYNTAXCONVERTER_DIR'] . "/it_syntaxconverter/$sourcerelpath";
		$stat = @lstat($converted);
		$mtimeconverted = $stat[9];

		if ($mtimeconverted < filemtime($source))
		{
			if ($changed = filesize($source) < 200000)	# Do not attempt to convert anything above 200k to avoid large memory consumption
			{
				if (substr($code = file_get_contents($source), 0, 3) == '#!/')
					$code = preg_replace('/^[^\r\n]*[\r\n]+/', '', $code);	# Remove shebang line when converting syntax
				$converter = new it_syntaxconverter($code);
				$parts = explode("/", dirname($converted));

				for ($i = 1; $i <= count($parts); $i++)
					@mkdir(implode("/", array_slice($parts, 0, $i)), 0700);

				if ($changed = $converter->changes)
				{
					@unlink($converted);
					if ($output = fopen($converted, "w"))
					{
						fputs($output, $converter->output);
						fclose($output);
					}
				}
			}

			if (!$changed)	# Link to original file if no changes made
			{
				@unlink($converted);
				$symlink = function_exists("symlink") ? "symlink" : "copy";	# symlink() does not exist on Windows
				$symlink(realpath($source), $converted);
			}

			clearstatcache();
		}
	}

	return $converted;
}

function it_classloader($classname)
{
	if (!preg_match('/:/', $classname) && ($file = @fopen("$classname.class", "r", true)))	# Check if file is local (does not contain ':', i.e. no protocol like http:) for file in include path, do not use @include to get failures on inheritance
	{
		include("$classname.class");
		fclose($file);
	}
	else
		it_dbi::createclass($classname);

	EDC('ultraclassloader', $classname, it_debug::backtrace());
}

function it_errorhandler($errno, $errstr, $errfile, $errline, $errcontext = [])
{
	$error_reporting = error_reporting(0);	# Disable error reporting while handling error

	if ($result = $error_reporting & $errno)	# Is this error enabled?
	{
		static $it_errnames = array();

		if (!$it_errnames)
		{
			foreach (get_defined_constants() as $name => $no)
			{
				if (preg_match('/^E_/', $name))
					$it_errnames[$no] = "$name: ";
			}
		}

		$error = array(
			'title' => $it_errnames[$errno] . $errstr,
			'locals' => $errcontext,
			'file' => $errfile,
			'line' => $errline,
			'backtraceskip' => 1,
		);

		switch ($errno)
		{
		case E_USER_ERROR: case E_RECOVERABLE_ERROR:
			it::fatal($error);
			break;

		case E_NOTICE:
			if ((dirname($errfile) == dirname(__FILE__)) && preg_match('/undefined|non-object/i', $errstr))
			{
				$result = true;	# Silence E_NOTICE about undefined stuff in ITools files
				break;
			}
			# FALLTHROUGH

		default:
			it::error($error);
			break;
		}
	}

	error_reporting($error_reporting);

	return $result;	# True means do not execute standard PHP error handler
}

function it_shutdown()
{
	if (isset($GLOBALS['debug_timerlog']) && !empty($GLOBALS['ULTRATIMERLOG']))
		it::log('timerlog', $_SERVER['REQUEST_URI'] . "\t" . substr($GLOBALS['ULTRATIMERLOG'], 1));
}