summaryrefslogtreecommitdiff
path: root/it.class
diff options
context:
space:
mode:
Diffstat (limited to 'it.class')
-rw-r--r--it.class133
1 files changed, 133 insertions, 0 deletions
diff --git a/it.class b/it.class
new file mode 100644
index 0000000..2dad792
--- /dev/null
+++ b/it.class
@@ -0,0 +1,133 @@
+<?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.
+**
+** 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);
+ }
+}
+
+
+/**
+ * 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_delay gives number of seconds in which consecutive failures dont count
+ */
+function error($p=array(), $body='', $to='')
+{
+ 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 ($p['ok']) {
+ system("alert -o " . escapeshellarg($p['ok_key']) . "=1" . escapeshellarg($to) . " 4 'everything ok'");
+ } else {
+ $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI'];
+ $referer = $_SERVER['HTTP_REFERER'];
+ if (!$title)
+ $title="Error in $url";
+
+ $body .= "\nUrl: $url\n\nReferer: $referer\n\n\$_REQUEST: ".print_r($_REQUEST, true). "\nStack:\n" . print_r(array_slice(debug_backtrace(), 1), true);
+
+ if (ereg('twin|devel', $GLOBALS['ULTRASERVERTYPE'])) {
+ echo "<pre>$title\n$body\n</pre>";
+ } else {
+ $okdelay = $p['ok_delay'] ? "-d " . escapeshellarg($p['ok_delay']) : "";
+ $okmode = $p['ok_key'] ? "-o " . escapeshellarg($p['ok_key']) . "=0" : "";
+ $cmd = "alert $okdelay $okmode -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'));
+}
+
+}
+
+?>