From b2accf6655dce9584f3ea1485a22c58dcc9dd08f Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 10 Dec 2008 15:57:33 +0000 Subject: it::file_get, it::file_put, shorter shell error format, it::mail docs --- it.class | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'it.class') diff --git a/it.class b/it.class index c3d0a0c..179ee75 100644 --- a/it.class +++ b/it.class @@ -215,12 +215,12 @@ function error($p = array(), $body = null, $to = null) # $body and $to deprecate else if ($_SERVER['REMOTE_ADDR']) # toscreen mode: web echo "
{$p['title']}\n".rtrim($body).""; else # toscreen mode: shell (outputs to stderr) - error_log("it::error: " . $p['title'] . " in " . ($trace ? $trace : "{$p['file']}:{$p['line']}") . " Url: $url " . (EDC('verbose') ? D($p['locals']) : "")); + error_log($p['title'] . " in " . ($trace ? $trace : "{$p['file']}:{$p['line']} Url: $url") . " " . (EDC('verbose') ? D($p['locals']) : "")); } if (($fh = fopen("/tmp/alertdata/alert.log", "a"))) { - fputs($fh, it::date() . " " . $p['title'] . " in " . ($trace ? $trace : "{$p['file']}:{$p['line']}") . " Url: $url\n"); + fputs($fh, it::date() . " " . $p['title'] . " in " . ($trace ? $trace : "{$p['file']}:{$p['line']} Url: $url") . "\n"); fclose($fh); @chmod("/tmp/alertdata/alert.log", 0777); } @@ -668,7 +668,8 @@ function map($expression, $array) } /** - * Send a mail. Expects array for Header => Content pairs with Body => for the mail body + * Send a mail. Expects array for Header => Content pairs with Body => for the mail body. + * @return nothing */ function mail($p) { @@ -676,8 +677,42 @@ function mail($p) unset($p['Body']); $mail = new it_mail($p); $mail->add_body($body); + $mail->send(); +} + +/** + * Reads a file and returns it as string or in one of several formats. Two params: filename and flags + * @param $filename name of file to read + * @param $p['keyval'] each line of the file is one tab-separated key/value pair, return assoc array + */ +function file_get($filename, $p = array()) +{ + if ($p['keyval']) + { + foreach (explode("\n", rtrim(file_get_contents($filename), "\n")) as $line) + { + $arr = explode("\t", $line, 2); + $result[$arr[0]] = $arr[1]; + } + } + else + $result = file_get_contents($filename); + + return $result; +} + +/** + * Write data to a file with several serialization modes + * @param $filename name of file to write to + * @param $data data to write + * @param $p['keyval'] $data must be an assoc array and is written as tab-separated lines + */ +function file_put($filename, $data, $p = array()) +{ + if ($p['keyval']) + $data = join("", it::map('"$k\t$v\n"', $data)); - return $mail->send(); + return file_put_contents($filename, $data); } } -- cgit v1.2.3