summaryrefslogtreecommitdiff
path: root/it.class
blob: 24653cdad12c8731d45716eba68f2b1b808f01f3 (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
<?php
/*
**	$Id$
**
**	ITools - the Internet Tools Library
**
**	Copyright (C) 1995-2006 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.
**
**	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";

	if ($fh = fopen($GLOBALS['ULTRAHOME'] . "/log/$name-" . date('Ymd'), "a"))
	{
		fputs($fh, $line);
		fclose($fh);
	}	
}


/**
 * 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 ok indicates no error happened
 * @param named parameter ok_key indicates which variable to store failure counts in
 * @param named parameter ok_maxfail number of failures needed for alert; default 2. only 1 failure per 60 seconds counted
 */
function error($p = array(), $body = "", $to = "")
{
	$p += array('ok_key' => "it_error", 'ok_maxfail' => 2);

	if (is_array($p))
	{
		$title = $p['title'];
		$body = $p['body'];
		$to = $p['to'];
	}
	else
	{
		$title = $p;
		unset($p);
	}

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

	if (isset($p['ok']))
	{
		$counterfn = $GLOBALS['ULTRAHOME'] . "/tmp/" . urlencode($p['ok_key']);

		if (time() - @filemtime($counterfn) >= 60)	# only modify counter if unmodified for 60 secs
		{
			$oldcounter = intval(@file_get_contents($counterfn));
			$newcounter = $p['ok'] ? 0 : $oldcounter + 1;

			if ($newcounter != $oldcounter)
			{
				$fh = fopen($counterfn, "w");
				fputs($fh, "$newcounter\n");
				fclose($fh);

				if ($newcounter >= $p['ok_maxfail'] && (time() - @filemtime("$counterfn.mailsent")) >= 86400)	# 1 mail per day
				{
					$sendalert = true;

					if (!ereg('twin|devel', $GLOBALS['ULTRASERVERTYPE']))
						touch("$counterfn.mailsent");
				}
			}
		}
	}
	else
		$sendalert = true;

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

		$body .= "\nUrl: $url\n\n\$_REQUEST: ".print_r($_REQUEST, true). "\nStack:\n" . print_r(array_slice(debug_backtrace(), 1), true) . "\n\$_SERVER: " . print_r($_SERVER, true);

		if (ereg('twin|devel', $GLOBALS['ULTRASERVERTYPE']))
		{
			echo "<pre>$title\n$body\n</pre>";
		}
		else
		{
			$cmd = "alert -l " . escapeshellarg($to) . " 4 " . escapeshellarg($title);

			EDC('exec', $cmd);
			if (($pipe = popen($cmd, "w")))
			{
				fputs($pipe, $body);
				pclose($pipe);
			}

			error_log("it::error: ".$title);
		}
	}
}


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


/**
 * Return "db4" or "db2" depending on availability
 */
function dba_handler()
{
	return in_array("db4", dba_handlers()) ? "db4" : "db2";
}


/**
 * 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'));
}

}

?>