CMS  Version 3.9
error_log_manager.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
40 {
41  static $enableLogging = false;
42  static $loggingDestination = "";
43  static $errorPageTemplate = "";
44 
45  static function setDefaults()
46  {
47  Settings::setDefaultValue("error_log", "enable_logging", 0, Boolean);
48  Settings::setDefaultValue("error_log", "logging_destination", "Database", String, "", "", "Database\nLog File");
49  Settings::setDefaultValue("error_log", "error_page_template", "/fakoli/error.tpl", String,"Specify the file name of your custom error page template, if you are using one. Leave this blank to use the default error reporting template");
50  }
51 
52  static function enableLogging()
53  {
54  ErrorLogManager::$enableLogging = Settings::getValue("error_log", "enable_logging");
55  ErrorLogManager::$loggingDestination = Settings::getValue("error_log", "logging_destination");
56  ErrorLogManager::$errorPageTemplate = Settings::getValue("error_log", "error_page_template");
57 
58  set_exception_handler(array("ErrorLogManager", "exceptionHandler"));
59 
60  }
61 
68  static function logError($exception)
69  {
70  global $user;
71 
72  try
73  {
74  //Getting exception information
75  $errMsg = $exception->getMessage();
76  $errLevel = $exception->getCode();
77  $errFile = $exception->getFile();
78  $errLine = $exception->getLine();
79  $uri = $_SERVER['REQUEST_URI'];
80  $referer = $_SERVER['HTTP_REFERER'];
81 
82  if ($errLevel & (E_NOTICE | E_WARNING | E_USER_WARNING | E_STRICT)) return;
83 
84  $log = new ErrorLog();
85  $log->code = $errLevel;
86  $log->message = $errMsg;
87  $log->file = $errFile;
88  $log->line = $errLine;
89  $log->details = $exception->getTraceAsString();
90  $log->user_id = $user ? $user->get($user->getPrimaryKey()) : 0;
91  $log->uri = $uri;
92  $log->referer = $referer;
93  $log->session = session_id();
94 
96  {
97  try
98  {
100  {
101  case "Database":
102 
103 
104  $log->save();
105 
106  break;
107 
108  case "Log File":
109 
110  trace($errMsg, 2, $exception->getTrace());
111  break;
112 
113  default:
114 
115  break;
116  }
117  }
118  catch(Exception $e)
119  {
120  //echo $e->getMessage();
121  }
122 
123  }
124  }
125  catch (Exception $ex)
126  {
127  echo $ex->getMessage();
128  //Do nothing.
129  //Avoiding error from error handling...
130  }
131 
132  }
133 
134  static function exceptionHandler($exception)
135  {
136  global $user;
137  global $isAction;
138 
139  try
140  {
141  //Getting exception information
142  $errMsg = $exception->getMessage();
143  $errLevel = $exception->getCode();
144  $errFile = $exception->getFile();
145  $errLine = $exception->getLine();
146  $uri = $_SERVER['REQUEST_URI'];
147  $referer = $_SERVER['HTTP_REFERER'];
148 
149  if ($errLevel & (E_NOTICE | E_WARNING | E_USER_WARNING | E_STRICT)) return;
150 
151  while(ob_end_clean()); // Clear all output buffers.
152 
153  $log = new ErrorLog();
154  $log->code = $errLevel;
155  $log->message = $errMsg;
156  $log->file = $errFile;
157  $log->line = $errLine;
158  $log->details = $exception->getTraceAsString();
159  $log->user_id = $user ? $user->get($user->getPrimaryKey()) : 0;
160  $log->uri = $uri;
161  $log->referer = $referer;
162  $log->session = session_id();
163 
165  {
166  try
167  {
169  {
170  case "Database":
171 
172 
173  $log->save();
174 
175  break;
176 
177  case "Log File":
178 
179  trace($errMsg, 2, $exception->getTrace());
180  break;
181 
182  default:
183 
184  break;
185  }
186  }
187  catch(Exception $e)
188  {
189  //echo $e->getMessage();
190  }
191 
192  }
193 
195  $template = file_get_contents($err);
196  $out = (!$isAction) ? $log->format($template) : $log->message;
197  echo $out;
198  }
199  catch (Exception $ex)
200  {
201  echo $ex->getMessage();
202  //Do nothing.
203  //Avoiding error from error handling...
204  }
205 
206  /* Don't execute PHP internal error handler */
207  return true;
208  }
209 
210  static function upgradeComponent($version)
211  {
213  $mgr->upgrade($version);
214  }
215 }
216 ?>
$err
Definition: error_log.inc:9
$out
Definition: page.inc:66
static logError($exception)
Manually log an exception in the error log.
static exceptionHandler($exception)
static upgradeComponent($version)
static resolveResource($resource, $component="")
Resolves the path to a web resource based on the PHP include path.
Definition: core.inc:850
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
global $user
if($config["default_content_type"]) $isAction
Definition: core.inc:1584
$uri
Definition: tearoff.inc:4