error_level(); $Error -> template(); $Error -> return_page(); $Error -> log_path(); $Error -> log_file(); $Error -> date(); $Error -> timestamp(); $Error -> admin_email(); # SET ERROR LEVEL 1: ONLY DISPLAY ERROR $Error -> error_level(1); # SET ERROR LEVEL 2: WRITE LOG $Error -> error_level(2); # SET ERROR LEVEL 3: SEND ADMIN EMAIL $Error -> error_level(3); # SET ERROR LEVEL 3: SEND ADMIN EMAIL AND WRITE LOG $Error -> error_level(4); # SET ERROR LEVEL 99: DEBUG MODE (same as $Error -> debug()) $Error -> error_level(99); $Error -> admin_email('mike@nextopia.com'); #optional (default: webmasters email) $Error -> error_level(0); #optional (default: 99 - debug) $Error -> template("http://theory.nextopia.com/dev/mike/php/include/error.php"); #optional (default: templates/error_template.html) #$Error -> template('templates/error_template.html'); #optional (default: templates/error_template.html) $Error -> title('Error'); #optional (default: Error $Error -> log_path('./logs/); #optional (default: ./logs/) $Error -> log_file('error.dat'); #optional (default: default_error_log.dat) $Error -> message('Error'); #optional (default: Error: $!) $Error -> log_message('Log Error'); #optional (default: Error: $!) $Error -> date(date("Y-m-d")); #optional (default: yyyy-mm-dd) $Error -> timestamp(date("H:i:s")); #optional (default: hh:nn:ss) $Error -> return_page('javascript:history.back()'); #optional (default: javascript:history.back()) DISPLAY ERROR METHOD NOTE: the DISPLAY method will NOT maintain all values within the object the values only exist within the DISPLAY method if values are not passed, the object will attempt to use previously set values $Error -> display('ERROR MESSAGE','LOG MESSAGE',99,'template','title','log file', 'return page'); OR $Error -> display('ERROR MESSAGE','LOG MESSAGE'); ################################################# returns the most recent error produced within the module NOTE: not necessarily a fatal error many module errors will not cause the module to fail $module_error = $Error -> get_module_error(); ################################################# debug the error handler $Error -> debug(); # exit $Error -> debug(1); # do not exit */ var $message; var $log_message; var $error_level; var $template; var $log_path; var $log_file; var $date; var $timestamp; var $title; var $return_page; var $admin_email; /* * void Error_Handler() * class constructor */ function Error_Handler() { global $HTTP_SERVER_VARS, $GLOBALS; $this->INFO_ARRAY = array( 'PACKAGE' => 'ERROR HANDLER', 'VERSION' => '1.6', 'CREATED' => '2002/07/31', 'MODIFIED' => '2003/02/06', 'WRITTEN BY' => 'Michael McInally (mike@nextopia.com)' ); $this->error_levels = array( 1 => 'ONLY DISPLAY' , 2 => 'SEND ADMIN EMAIL' , 3 => 'WRITE LOG FILE' , 4 => 'SEND ADMIN EMAIL AND WRITE LOG FILE' , 99 => 'DEBUGGING MODE' ); $this->message = ""; $this->log_message = ""; // set default error level to 1 (no logging) $this->error_level = "1"; # $this->template = "error_template.html"; if (!(file_exists($this->template))) { # $this->template = "./templates/error_template.html"; } $this->log_path = './logs/' . date("Y-m-d") . '/'; $this->log_file = date("Y-m-d") . ".error.dat"; $this->title = "Server Error"; $this->return_page = "javascript:history.back()"; $this->date = date("Y-m-d"); $this->timestamp = date("H:i:s"); // set default values for these settings $this->admin_email = $HTTP_SERVER_VARS['SERVER_ADMIN']; $this->script_name = $HTTP_SERVER_VARS['SCRIPT_FILENAME']; $this->request_method = $HTTP_SERVER_VARS['REQUEST_METHOD']; $this->query_string = $HTTP_SERVER_VARS['QUERY_STRING']; $this->browser = $HTTP_SERVER_VARS['HTTP_USER_AGENT']; $this->ip_address = $HTTP_SERVER_VARS['REMOTE_ADDR']; $this->host_name = $HTTP_SERVER_VARS['REMOTE_HOST']; if (empty($this->admin_email)) { $this->admin_email = $GLOBALS['SERVER_ADMIN']; } if (empty($this->script_name)) { $this->script_name = $GLOBALS['SCRIPT_FILENAME']; } if (empty($this->request_method)) { $this->request_method = $GLOBALS['REQUEST_METHOD']; } if (empty($this->query_string)) { $this->query_string = $GLOBALS['QUERY_STRING']; } if (empty($this->browser)) { $this->browser = $GLOBALS['HTTP_USER_AGENT']; } if (empty($this->ip_address)) { $this->ip_address = $GLOBALS['REMOTE_ADDR']; } if (empty($this->host_name)) { $this->host_name = $GLOBALS['REMOTE_HOST']; } $this->module_errors = 0; $this->module_error = array (); $this->type = 1; $this->cr = "
"; if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']) || isset($GLOBALS['REQUEST_METHOD'])) { $this->type = 1; $this->cr = "
"; } else { $this->type = 0; $this->cr = "\n"; } } /* * debug() * DEBUG THE CLASS * show all variables set within the class */ function debug() { $arg_array = func_get_args(); $arg_num = func_num_args(); $exit = 0; $message = ""; $log_message = ""; if ($arg_array[0] != "") {$exit = $arg_array[0];} if ($message == "") {$message = $this->message;} if ($log_message == "") {$log_message = $this->log_message;} ### default debugging mode if ($this->type) { echo "

"; echo "PACKAGE: ".$this->INFO_ARRAY['PACKAGE']."$this->cr"; echo "VERSION: ".$this->INFO_ARRAY['VERSION']."$this->cr"; echo "CREATED: ".$this->INFO_ARRAY['CREATED']."$this->cr"; echo "MODIFIED: ".$this->INFO_ARRAY['MODIFIED']."$this->cr"; echo "WRITTEN BY: ".$this->INFO_ARRAY['WRITTEN BY']."$this->cr$this->cr"; echo "

"; echo "

"; echo "Debugging Mode:$this->cr"; echo "line: ".__LINE__." in ".__FILE__."$this->cr$this->cr"; echo "

"; echo "Template: $this->template$this->cr$this->cr"; echo "Title: $this->title$this->cr$this->cr"; echo "Error Message: $message$this->cr$this->cr"; echo "Log Message: $log_message$this->cr$this->cr"; echo "Log Path: $this->log_path$this->cr$this->cr"; echo "Log File: $this->log_file$this->cr$this->cr"; echo "Log File Date: $this->date$this->cr$this->cr"; echo "Log File Timestamp: $this->timestamp$this->cr$this->cr"; echo "Return Page URL: $this->return_page$this->cr$this->cr"; echo "Error Level: $this->error_level: ".$this->error_levels[$this->error_level]." $this->cr$this->cr"; echo "Admin Email: $this->admin_email$this->cr$this->cr"; echo "$this->cr"; while (list ($key, $value) = each ($this->module_error)) { echo ("Module Error $key: $value$this->cr$this->cr
"); } /* foreach my $key (sort sortnum keys %{$this->module_error}) { echo "Module Error $key: $this->module_error{$key}$this->cr$this->cr"; } */ echo "$this->cr"; } else { echo "PACKAGE: ".$this->INFO_ARRAY['PACKAGE']."$this->cr"; echo "VERSION: ".$this->INFO_ARRAY['VERSION']."$this->cr"; echo "CREATED: ".$this->INFO_ARRAY['CREATED']."$this->cr"; echo "MODIFIED: ".$this->INFO_ARRAY['MODIFIED']."$this->cr"; echo "WRITTEN BY: ".$this->INFO_ARRAY['WRITTEN BY']."$this->cr$this->cr"; echo "Debugging Mode:$this->cr"; echo "line: ".__LINE__." in ".__FILE__."$this->cr$this->cr"; echo "Template: $this->template$this->cr$this->cr"; echo "Title: $this->title$this->cr$this->cr"; echo "Error Message: $message$this->cr$this->cr"; echo "Log Message: $log_message$this->cr$this->cr"; echo "Log Path: $this->log_path$this->cr$this->cr"; echo "Log File: $this->log_file$this->cr$this->cr"; echo "Log File Date: $this->date$this->cr$this->cr"; echo "Log File Timestamp: $this->timestamp$this->cr$this->cr"; echo "Return Page URL: $this->return_page$this->cr$this->cr"; echo "Error Level: $this->error_level: ".$this->error_levels[$this->error_level]." $this->cr$this->cr"; echo "Admin Email: $this->admin_email$this->cr$this->cr"; echo "$this->cr"; while (list ($key, $value) = each ($this->module_error)) { echo ("Module Error $key: $value$this->cr$this->cr
"); } /* foreach my $key (sort sortnum keys %{$this->module_error}) { echo "Module Error $key: $this->module_error{$key}$this->cr$this->cr"; } */ echo "$this->cr"; } if (!$exit) { exit; } } /* * message() * set the error handler message value */ function message() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->message == "") { $this->module_error("No message provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->message != "") { return ($this->message); } else { return (0); } } else { $this->message = $arg_array[0]; return (1); } } /* * log_message() * set the error handler log message value */ function log_message() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->log_message == "") { $this->module_error("No log_message provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->log_message != "") { return ($this->log_message); } else { return (0); } } else { $this->log_message = $arg_array[0]; } } /* * error_level() * set the error level value */ function error_level() { $arg_array = func_get_args(); $arg_num = func_num_args(); if ($arg_array[0] < 1 || $arg_array[0] > 99) { if ($this->error_level < 1 || $this->error_level > 99) { $this->module_error("No error_level provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->error_level >= 1 && $this->error_level <= 99) { return ($this->error_level); } else { return (0); } } else { $this->error_level = $arg_array[0]; } } /* * template() * set the error handler template file value */ function template() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->template == "") { $this->module_error("No template provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->template != "") { return ($this->template); } else { return (0); } } else { $this->template = $arg_array[0]; } } /* * template_file() * set the error handler template file value */ function template_file() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!($this->template($arg_array[0]))) { return (0); } else { return (1); } } /* * title() * set the error handler title value */ function title() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->title == "") { $this->module_error("No title provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->title != "") { return ($this->title); } else { return (0); } } else { $this->title = $arg_array[0]; } } /* * log_path() * set the error handler log path value */ function log_path() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->log_path == "") { $this->module_error("No log_path provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->log_path != "") { return ($this->log_path); } else { return (0); } } else { $this->log_path = $arg_array[0]; } } /* * log_file() * set the error handler log file value */ function log_file() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->log_file == "") { $this->module_error("No log_file provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->log_file != "") { return ($this->log_file); } else { return (0); } } else { $this->log_file = $arg_array[0]; } } /* * date() * set the error handler log date value */ function date() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->date == "") { $this->module_error("No date provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->date != "") { return ($this->date); } else { return (0); } } else { $this->date = $arg_array[0]; } } /* * log_date() * set the error handler log date value */ function log_date() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!($this->date($arg_array[0]))) { return (0); } else { return (1); } } /* * timestamp() * set the error handler log time value */ function timestamp() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->timestamp == "") { $this->module_error("No timestamp provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->timestamp != "") { return ($this->timestamp); } else { return (0); } } else { $this->timestamp = $arg_array[0]; } } /* * log_time() * set the error handler log date value */ function log_time() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!($this->timestamp($arg_array[0]))) { return (0); } else { return (1); } } /* * return_page() * set the error handler return page value */ function return_page() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->return_page == "") { $this->module_error("No return_page provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->return_page != "") { return ($this->return_page); } else { return (0); } } else { $this->return_page = $arg_array[0]; } } /* * admin_email() * set the error handler administrator email value */ function admin_email() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!(isset($arg_array[0]))) { if ($this->admin_email == "") { $this->module_error("No admin_email provided. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); } if ($this->admin_email != "") { return ($this->admin_email); } else { return (0); } } else { $this->admin_email = $arg_array[0]; } } /* * display_error() * display() -> synonym for display_error * displays the error_handler error to the user * checks error level of error and: * 1: no error logging * 2: emails the admin * 3: logs error to a log file * 4: emails the admin and logs the error to a log file */ function display() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!($this->display_error($arg_array[0],$arg_array[1],$arg_array[2],$arg_array[3],$arg_array[4],$arg_array[5],$arg_array[6],$arg_array[7],$arg_array[8]))) { return (0); } } function display_error() { $arg_array = func_get_args(); $arg_num = func_num_args(); $message = ""; $log_message = ""; $error_level = ""; $template = ""; $title = ""; $log_path = ""; $log_file = ""; $return_page = ""; $admin_email = ""; if ($arg_array[0] != "") {$message = $arg_array[0];} if ($arg_array[1] != "") {$log_message = $arg_array[1];} if ($arg_array[2] != "") {$error_level = $arg_array[2];} if ($arg_array[3] != "") {$template = $arg_array[3];} if ($arg_array[4] != "") {$title = $arg_array[4];} if ($arg_array[5] != "") {$log_path = $arg_array[5];} if ($arg_array[6] != "") {$log_file = $arg_array[6];} if ($arg_array[7] != "") {$return_page = $arg_array[7];} if ($arg_array[8] != "") {$admin_email = $arg_array[8];} if ($log_message == "") { $log_message = $message; } if ($this->message == "") { $this->message = $message; } if ($this->log_message == "") { $this->log_message = $log_message; } if ($this->error_level != "99") { if ($this->error_level < 1 || $this->error_level > 99) { $this->error_level = $error_level; } } if ($this->template == "") { $this->template = $template; } if ($this->title == "") { $this->title = $title; } if ($this->log_path == "") { $this->log_path = $log_path; } if ($this->log_file == "") { $this->log_file = $log_file; } if ($this->return_page == "") { $this->return_page = $return_page; } if ($this->admin_email == "") { $this->admin_email = $admin_email; } if ($message == "") { $message = $this->message; } if ($log_message == "") { $log_message = $this->log_message; } if ($this->error_level == "99") { $error_level = "99"; } else { if ($error_level < 1 || $error_level > 99) { $error_level = $this->error_level; } } if ($template == "") { $template = $this->template; } if ($title == "") { $title = $this->title; } if ($log_path == "") { $log_path = $this->log_path; } if ($log_file == "") { $log_file = $this->log_file; } if ($return_page == "") { $return_page = $this->return_page; } if ($admin_email == "") { $admin_email = $this->admin_email; } if ($message == "") { $message = "Unknown error has occured."; } if ($this->error_level == "99") { $error_level = "99"; } if ($error_level == "") { $error_level = 1; } if ($error_level == "99") { $this->debug(); exit; } // log the error message if needed if (!($this->log_error($log_message,$error_level,$template,$title,$log_path,$log_file,$return_page,$admin_email))) { //$this->debug(); //exit; } if (preg_match("/http/i", $template)) { if (!(headers_sent())) { header("Location:".$template."?message=".urlencode($message)."&return_page=".urlencode($return_page)); } else { ?> Redirect

Page failed to redirect

Please click on the link below to be properly redirected.

Click Here!

module_error("Failed to open: $template. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); $this->message($message); $this->log_message($log_message); $this->debug(); } $html = preg_replace("/\[title\]/i", $title, $html); $html = preg_replace("/\[message\]/i", $message, $html); //$html = preg_replace("/\[log_file\]/i", $log_file, $html); $html = preg_replace("/\[log_message\]/i", $log_message, $html); $html = preg_replace("/\[return_page\]/i", "Return", $html); echo($html); exit; } } /* * log_error() * log() -> synonym for log_error() * checks error level of error and: * 1: no error logging * 2: emails the admin * 3: logs error to a log file * 4: emails the admin and logs the error to a log file */ function log() { $arg_array = func_get_args(); $arg_num = func_num_args(); if (!($this->log_error($arg_array[0],$arg_array[1],$arg_array[2],$arg_array[3],$arg_array[4],$arg_array[5],$arg_array[6],$arg_array[7],$arg_array[8]))) { return (0); } } function log_error() { $arg_array = func_get_args(); $arg_num = func_num_args(); $log_message = ""; $error_level = ""; $template = ""; $title = ""; $log_path = ""; $log_file = ""; $return_page = ""; $admin_email = ""; if ($arg_array[0] != "") {$log_message = $arg_array[0];} if ($arg_array[1] >= 0 && $arg_array[2] <= 99) {$error_level = $arg_array[1];} if ($arg_array[2] != "") {$template = $arg_array[2];} if ($arg_array[3] != "") {$title = $arg_array[3];} if ($arg_array[4] != "") {$log_path = $arg_array[4];} if ($arg_array[5] != "") {$log_file = $arg_array[5];} if ($arg_array[6] != "") {$return_page = $arg_array[6];} if ($arg_array[7] != "") {$admin_email = $arg_array[7];} if ($this->log_message == "") { $this->log_message = $log_message; } if ($this->error_level == "" && $this->error_level != 0) { $this->error_level = $error_level; } if ($this->template == "") { $this->template = $template; } if ($this->title == "") { $this->title = $title; } if ($this->log_path == "") { $this->log_path = $log_path; } if ($this->log_file == "") { $this->log_file = $log_file; } if ($this->return_page == "") { $this->return_page = $return_page; } if ($this->admin_email == "") { $this->admin_email = $admin_email; } if ($log_message == "") { $log_message = $this->log_message; } if ($error_level == "" && $error_level != 0) { $error_level = $this->error_level; } if ($template == "") { $template = $this->template; } if ($title == "") { $title = $this->title; } if ($log_path == "") { $log_path = $this->log_path; } if ($log_file == "") { $log_file = $this->log_file; } if ($return_page == "") { $return_page = $this->return_page; } if ($admin_email == "") { $admin_email = $this->admin_email; } if ($log_message == "") { $log_message = "Unknown error has occured."; } if ($error_level == "" || $error_level == 0) { $error_level = 1; } switch($error_level) { case 1: break; case 2: $new_log_message = $this->date . "\n"; $new_log_message .= $this->timestamp . "\n"; $new_log_message .= $log_message . "\n"; $new_log_message .= $this->script_name . "\n"; $new_log_message .= $this->request_method . "\n"; $new_log_message .= $this->query_string . "\n"; $new_log_message .= $this->browser . "\n"; $new_log_message .= $this->ip_address . "\n"; $new_log_message .= $this->host_name . "\n"; //$status = @error_log($new_log_message, 1, $admin_email, "Script: $this->script_name"); if (!($status = @error_log($new_log_message, 1, $admin_email, "Script: $this->script_name"))) { $this->module_error("Failed send mail to: $admin_email. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } break; case 3: $new_log_message = $this->date . "\t"; $new_log_message .= $this->timestamp . "\t"; $new_log_message .= $log_message . "\t"; $new_log_message .= $this->script_name . "\t"; $new_log_message .= $this->request_method . "\t"; $new_log_message .= $this->query_string . "\t"; $new_log_message .= $this->browser . "\t"; $new_log_message .= $this->ip_address . "\t"; $new_log_message .= $this->host_name . "\n"; if (!(@file_exists($log_path))) { if (!(@mkdir($log_path, 0777))) { $this->module_error("Failed create directory: |$log_path|. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } } //$status = @error_log($new_log_message, 3, $log_path.$log_file); if (!($status = @error_log($new_log_message, 3, $log_path.$log_file))) { $this->module_error("Failed write error log: $log_path.$log_file. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } break; case 4: $new_log_message = $this->date . "\n"; $new_log_message .= $this->timestamp . "\n"; $new_log_message .= $log_message . "\n"; $new_log_message .= $this->script_name . "\n"; $new_log_message .= $this->request_method . "\n"; $new_log_message .= $this->query_string . "\n"; $new_log_message .= $this->browser . "\n"; $new_log_message .= $this->ip_address . "\n"; $new_log_message .= $this->host_name . "\n"; if (!(file_exists($log_path))) { if (!(mkdir($log_path, 0777))) { $this->module_error("Failed create directory: $log_path. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } } //$status = @error_log($new_log_message, 1, $admin_email, "Script: $this->script_name"); if (!($status = @error_log($new_log_message, 1, $admin_email, "Script: $this->script_name"))) { $this->module_error("Failed send mail to: $admin_email. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } $new_log_message = $this->date . "\t"; $new_log_message .= $this->timestamp . "\t"; $new_log_message .= $log_message . "\t"; $new_log_message .= $this->script_name . "\t"; $new_log_message .= $this->request_method . "\t"; $new_log_message .= $this->query_string . "\t"; $new_log_message .= $this->browser . "\t"; $new_log_message .= $this->ip_address . "\t"; $new_log_message .= $this->host_name . "\n"; //$status = @error_log($new_log_message, 3, $log_path.$log_file); if (!($status = @error_log($new_log_message, 3, $log_path.$log_file))) { $this->module_error("Failed write error log: $log_path.$log_file. MODULE ERROR AT LINE: ".__LINE__." in ".__FILE__."."); return (0); } break; default: break; } return (1); } /* * get_module_error() * retrieve the most recent module error */ function get_module_error() { return ($this->module_error[$this->module_errors]); } /* * module_error() * set the module error */ function module_error() { $arg_array = func_get_args(); $arg_num = func_num_args(); $this->module_errors++; $this->module_error[$this->module_errors] = "$arg_array[0]"; for ($i = 0;$i < ($this->module_errors - 10);$i++) { if (isset($this->module_error[$i])) { unset($this->module_error[$i]); } } return (1); } }; // end of class ?>