summaryrefslogtreecommitdiff
path: root/it.class
blob: 837e33c0a08e3f3c69692c8d9640b8fe40837d04 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<?php
/*
**	$Id$
**
**	Copyright (C) 1995-2007 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/>.
**
**	it.class - static functions
*/

class it
{
/**
 * Clone an object and return copy, works for all PHP versions
 */
function &cloneobj(&$object)
{
	$result = (is_object($object) && version_compare(zend_version(), 2, '>=')) ? clone($object) : $object;

	return $result;	# PHP internals need a tmp var to return by ref
}


/**
 * Append a line to a logfile in log/. Date will be added to filename and line
 * @param $name Name of logfile
 * @param $line1 Line to append (varargs)
 */
function log($name /* ... */)
{
	$args = func_get_args();
	$line = date("Y-m-d H:i:s") . "\t" . implode("\t", array_slice($args, 1)) . "\n";
	$fn = $GLOBALS['ULTRAHOME'] . "/log/$name-" . date('Ymd');

	$existed = file_exists($fn);

	if (isset($GLOBALS['ULTRAHOME']) && ($fh = fopen($fn, "a")))
	{
		fputs($fh, $line);
		fclose($fh);

		if (!$existed)
		{
			@chgrp($fn, "www");
			@symlink($fn, $GLOBALS['ULTRAHOME'] . "/log/$name");
		}
	}	
}


/**
 * Store timings for appending to log/timer_log-* in auto_append.php
 */
function timerlog($label = '')
{
	if ($GLOBALS['debug_timerlog'])
	{
		$s = $GLOBALS['ULTRATIME'];
		$e = gettimeofday();
		$msec= ($e['sec'] - $s['sec']) * 1000 + ($e['usec'] - $s['usec']) / 1000;
		$GLOBALS['ULTRATIMERLOG'] .= sprintf(" %s:%d", $label, $msec);
	}
}


/**
 * Send error report either to browser (on devel/twin machines) or by email to .diffnotice guys
 * @parma $title (optional) error title, one line
 * @param $body (optional) error body, multiline
 * @param $to (optional) comma separated recipient list
 * @param named parameter id identifier of error. if given, only subsequent errors of same id will trigger message
 * @param named parameter graceperiod number of seconds within which additional errors are ignored if id is set
 * @param named parameter timewindow number of seconds after graceperiod within which the second error must occur if id is set
 */
function error($p = array(), $body = "", $to = "")
{
	if (is_array($p))
	{
		$title = $p['title'];
		$body = $p['body'];
		$to = $p['to'];
	}
	else
	{
		$title = $p;
		$p = array();
	}

	$p += array('timewindow' => 25*3600, 'graceperiod' => 60, 'backtraceskip' => 0);

	if (!$to)
		$to = strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>',', ' '=>''));

	if ($p['id'])
	{
		$stampfn = $GLOBALS['ULTRAHOME'] . "/tmp/errstamp_" . urlencode($p['id']);
		$stampage = time() - @filemtime($stampfn);
		$lastsentfn = "/tmp/alertdata/" . $p['id'];

		if ($stampage >= $p['graceperiod'] && $stampage <= $p['graceperiod'] + $p['timewindow']) 
			$sendalert = true;

		if ($stampage >= $p['graceperiod']) # constantly occurring errors should not suppress mail
			touch($stampfn);
	}
	else
	{
		$lastsentfn = "/tmp/alertdata/" . getmyuid() . "." . $to;
		$sendalert = true;
	}

	$toscreen = !ereg('live', $GLOBALS['ULTRASERVERTYPE']);

	if ($sendalert && ($toscreen || (time() - @filemtime($lastsentfn)) > 3600))
	{
		@mkdir("/tmp/alertdata");
		@chmod("/tmp/alertdata", 0777);
		touch($lastsentfn);
		@chmod($lastsentfn, 0666);

		if ($_SERVER['REMOTE_ADDR'])
			$url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
		if (!$title)
			$title="Error in " . trim($url);

		if (!function_exists('memory_get_usage') || (memory_get_usage() < 50000000))
			$trace = it_debug::backtrace($p['backtraceskip']);

		$body .= ($url ? "\nUrl: $url" : "") . ($trace ? "\n\nTrace: " . $trace : "");

		if (!$toscreen)
		{
			$cmd = "alert -l " . escapeshellarg($to) . " 4 " . escapeshellarg($title);

			EDC('exec', $cmd);
			if (($pipe = popen($cmd, "w")))
			{
				$body .= "\n\nLocals" . print_r($p['locals'], true) . "\n\n\$_REQUEST: ".print_r($_REQUEST, true) . "\n\$_SERVER: " . print_r($_SERVER, true) . "\n\nStack:\n" . print_r(debug_backtrace(), true);
				fputs($pipe, $body);
				pclose($pipe);
			}
		}
		else if ($_SERVER['REMOTE_ADDR'])
			echo "<pre>$title\n$body\n</pre>";
	}

	error_log("it::error: ".$title . ": " . $trace . (EDC('verbose') ? D($p['locals']) : ""));
}


/**
 * Same as error(), plus exit
 */
function fatal($title='', $body='', $to='')
{
	it::error($title, $body, $to);
	exit(1);
}


/**
 * Print message to stderr and exit with error code
 */
function bail($message = "Bailed.\n")
{
	fputs(STDERR, $message);
	exit(1);
}


/**
 * Convert a string to ASCII-only chars, map/strip ISO-8859-1 accents
 * @param $text Text to convert
 * @return mapped/stripped version of $text contains only chars [0..127]
 */
function toascii($text)
{
	return strtr(strtr($text,
		'ÇéâàåçêëèïîìÅÉôòûùÿøØáíóúñÑÁÂÀãÃÊËÈÍÎÏÓÔõÕÚÛÙýÝ',
		'CeaaaceeeiiiAEoouuyooaiounNAAAaAEEEIIIOOoOUUUyY'),
		array('ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'æ' => 'ae', 'Æ' => 'Ae', 'ß' => 'ss'));
}


/**
 * Convert regex for preg (adds and escapes delimiter, adds modifiers)
 * @param $pattern Regex to convert
 * @param named parameter casesensitive Regex is case sensitive (omit modifier i)
 * @param named parameter multiline add modifier m
 * @param named parameter singleline add modifier s
 * @param named parameter utf8 add modifier u
 * @param named parameter extended add modifier x
 * @return converted regex to use with preg
 */
function convertregex($pattern, $p = array())
{
	$pattern = preg_replace('|/|', '\/', $pattern); 
	$modifiers = '';

	if (!$p['casesensitive'])
		$modifiers .= 'i';

	foreach (array(
			'multiline'  => 'm',
			'singleline' => 's',
			'utf8'       => 'u',
			'extended'   => 'x',
		) as $key => $mod)
	{
		if ($p[$key])
			$modifiers .= $mod;
	}

	return  "/$pattern/$modifiers";
}

/**
 * Try to match string against regex. See convertregex for additional named parameters.
 * @param $pattern Regex to match against
 * @param $string String to match
 * @param named parameter offset_capture Set flag preg_offset_capture (returns offsets with the matches).
 * @param named parameter all Return every match as array instead of first match.
 * @return Matched string or false 
 */
function match($pattern, $string, $p = array())
{
	$flags = 0;

	if($p['offset_capture'])
		$flags |= PREG_OFFSET_CAPTURE;

	$oldlocale = setlocale( LC_CTYPE, 0 );
	setlocale(LC_CTYPE, 'de_CH');

	if ($p['all'])
		$r = preg_match_all(it::convertregex($pattern, $p), $string, $m, $flags | PREG_PATTERN_ORDER, $p['offset']);
	else
		$r = preg_match(it::convertregex($pattern, $p), $string, $m, $flags, $p['offset']);

	setlocale(LC_CTYPE, $oldlocale);

	if (!$r)	# no match
		$result = $p['all'] ? array() : null;
	else if (count($m) == 1)	# no capture
		$result = $m[0];
	else if (count($m) == 2)	# one capture
		$result = $m[1];
	else if ($p['all'] && !$p['pattern_order'])	# captures, reorder pattern_order to set_order but without first element
		$result = call_user_func_array('array_map', array_merge(array(null), array_slice($m, 1)));
	else	# captures, don't return first element (matched string)
		$result = array_slice($m, 1);

	return $result;
}

/**
 * Replace parts of a string matched by a pattern with according replacement string. See convertregex for named parameters.
 * @param $replacementes Array with patterns as keys and replacement strings as values.
 * @param $string String to change.
 * @return New string.
 */
function replace($replacements, $string, $p = array())
{
	$patterns = array();

	foreach (array_keys($replacements) as $pattern)
		$patterns[] = it::convertregex($pattern, $p);

	$oldlocale = setlocale(LC_CTYPE, 0);
	setlocale(LC_CTYPE, 'de_CH');
	$result = preg_replace($patterns, array_values($replacements), $string, isset($p['limit']) ? $p['limit'] : -1);
	setlocale(LC_CTYPE, $oldlocale);

	return $result;
}

/**
 * Extract key => value pairs from assoc array by key
 * @param $array array to filter
 * @param $keys array of keys to keep
 */
function filter_keys($array, $keys)
{
	$result = array();
	$keep = array_flip($keys);

	foreach ($array as $key => $val)
		if (isset($keep[$key]))
			$result[$key] = $val;

	return $result;
}


/**
 * Construct shell command, log it, execute it and return output as string.
 * Keywords {keyword} are replace a la ET(), {-opts} takes an array and
 * inserts options a la it_html attributes (value, true, false or null)
 * @param $cmd Format string with {keywords} replace a la ET()
 * @param $values (zero, one or more arrays can be passed)
 * @return output of command. shell errors not detectable, see error_log in /www/server/logs
 */
function exec(/* $cmd, $values1 = array(), ... */)
{
	$args = func_get_args();
	$cmd = array_shift($args);
	$values = array();

	# Merge values into one array
	foreach ($args as $arg)
		$values += (array)$arg;

	while (list($tag, $option, $key) = it::match('({(-?)(\w+)})', $cmd))
	{
		$parts = array();

		if ($option)
		{
			foreach ((array)$values["-$key"] as $key => $value)
			{
				if ($value === true || $value === false || $value === null)
					$parts[] = $value ? $key : "";
				else
					$parts[] = "$key " . it::_exec_quotevalue($value);
			}
		}
		else
		{
			foreach ((array)$values[$key] as $value)
				$parts[] = it::_exec_quotevalue($value);
		}

		$cmd = str_replace($tag, join(" ", $parts), $cmd);
	}

	$s = gettimeofday();
	$result = EDC('noexec') ? "" : (string)shell_exec($cmd);
	$e = gettimeofday();
	$msec= intval(($e['sec'] - $s['sec']) * 1000 + ($e['usec'] - $s['usec']) / 1000);

	it::log('exec', "$msec\t$cmd");

	return $result;
}

function _exec_quotevalue($value)
{
	$result = strval($value);

	if (it::match('^-', $result))
		it::fatal("leading - in value");

	return escapeshellarg($result);
}


/**
 * Convert an image to a given size and type (ensures input is an image)
 * @param $p['in']	Input filename (mandatory)
 * @param $p['out']	Output filename (mandatory)
 * @param $p['size']	Width x height of resulting image, e.g. "160x60"
 * @param $p['type']	Output file type, e.g. "jpg"
 * @param $p['types']	Comma separated list of accepted input types, default "gif,jpg,png,bmp,tif,jp2"
 * @return Success of convert as true/false
 */
function imageconvert($p)
{
	$result = false;
	$imagetype = @exif_imagetype($p['in']);

	if (!function_exists("image_type_to_extension") || !($type = it::replace(array("jpeg" => "jpg", "tiff" => "tif"), image_type_to_extension($imagetype, false))))
	{
		# Fallback for PHP4
		$knowntypes = array(IMAGETYPE_GIF => "gif", IMAGETYPE_JPEG => "jpg", IMAGETYPE_PNG => "png", IMAGETYPE_BMP => "bmp");
		$type = $knowntypes[$imagetype];
	}

	$p += array('type' => $type, 'types' => "gif,jpg,png,bmp,tif,jp2");

	if (it::match(",$type,", ",{$p['types']},"))	# Valid type?
		$result = it::exec("convert 2>&1 -flatten -quality 75 {-opts} {in} {type}:{out}", array('-opts' => array('-thumbnail' => $p['size'])), $p) === "";

	return $result;
}


/**
 * Parse command line options with Usage given as template. Example:
 * Usage: myprogram.php [OPTIONS] PATTERN
 *  -s, --short      Use short ouput
 *  -f, --file=FILE  Use FILE for input
 *  -x EXTENSION     Ignore EXTENSION
 * Value always stored in long opt. Use double blank before explanation. Non-option text ignored.
 * Mandatory non-option arguments are stored under their lowercased name.
 * @param $helplines Usage parsed to determine options
 * @return Associative array of opts: Boolean options are set to true, remaining args returned in 'args'
 */
function getopt($helplines)
{
	$GLOBALS['it_stdin'] = array(
		'fd' => null,
		'args' => array(),
		'filename' => "",
		'line' => 0,
	);

	$result = array('args' => array());

	if ($indentation = it::match('^\s+', $helplines))
		$helplines = it::replace(array($indentation => "\n"), $helplines);

	foreach(explode("\n", trim($helplines)) as $helpline)
	{
		$shortoptname = $shortoptarg = $longoptname = $longoptarg = "";
		foreach (explode(',', $helpline) as $optdesc)
		{
			$optdesc = trim($optdesc);
			if ($matches = (array)it::match('^--(\w[\w-]*)(=[A-Z])?', $optdesc))
				list($longoptname, $longoptarg) = $matches;
			elseif ($matches = (array)it::match('^-(\w)( [A-Z])?', $optdesc))
				list($shortoptname, $shortoptarg) = $matches;
		}	

		if ($longoptname || $shortoptname)
		{
			if ($longoptname && $shortoptname)
				$alias[$shortoptname] = $longoptname;

			$witharg[$longoptname ? $longoptname : $shortoptname] = $longoptarg || $shortoptarg;
		}
	}

	$mandatoryargs = array();
	if ($tmp = trim(it::replace(array("\n.*" => "", "^\S+\s+\S+\s*" => "", "\[.*?\]\s*" => ""), trim($helplines))))
		$mandatoryargs = preg_split('/\s+/', $tmp);

	foreach (array_slice($_SERVER['argv'], 1) as $arg)
	{
		if ($eat)
		{
			if (it::match('^--?\w', $arg))	# Already next option => Missing argument?
				$err = true;
			else
				$result[array_shift($eat)] = $arg;
		}
		elseif ($matches = (array)it::match('^--(\w[\w-]*)(=\S*)?', $arg))
		{
			list($optname, $val) = $matches;
			if (!isset($witharg[$optname]) || isset($val) && !$witharg[$optname])
				$err = true;
			else if($witharg[$optname] && !$val)
				$eat[] = $optname;
			else
				$result[$optname] = $val ? substr($val, 1) : true;
		}
		else if ($letters = it::match('^-(\w+)', $arg))
		{
			foreach (explode("\n", trim(chunk_split($letters, 1, "\n"))) as $letter)
			{
				$optname = $alias[$letter] ? $alias[$letter] : $letter;
				if ($witharg[$optname])
					$eat[] = $optname;
				else if (!isset($witharg[$optname]))
					$err = true;
				else
					$result[$optname] = true;
			}
		}
		elseif($mandatoryargs)
			$result[strtolower(array_shift($mandatoryargs))] = $arg;
		else
			$result['args'][] = $arg;
	}

	if ($err || $eat || $result['h'] || $result['help'] || $mandatoryargs)
	{
		fputs(($result['h'] || $result['help'] ? STDOUT : STDERR), trim($helplines) . "\n");
		exit(1);
	}

	$GLOBALS['it_stdin']['args'] = $result['args'] ? $result['args'] : array("-");
	it::_stdin_next();

	return $result;
}

function _stdin_next()
{
	if ($result = $GLOBALS['it_stdin']['args'])
	{
		$GLOBALS['it_stdin']['filename'] = array_shift($GLOBALS['it_stdin']['args']);
		$GLOBALS['it_stdin']['fd'] = ($GLOBALS['it_stdin']['filename'] == "-") ? STDIN : @fopen($GLOBALS['it_stdin']['filename'], "r");
		$GLOBALS['it_stdin']['line'] = 0;
	}

	return $result;
}

/**
 * Get one line from stdin (or files given on command line) a la Perl <>.
 * Note: You need to call getopt() before using this function.
 * @return Line (including newline) or false on eof
 */
function gets()
{
	do {
		$result = fgets($GLOBALS['it_stdin']['fd']);
	} while (($result === false) && it::_stdin_next());

	$GLOBALS['it_stdin']['line']++;
	return $result;
}

}

?>