{"id":5281,"date":"2013-05-08T11:58:35","date_gmt":"2013-05-08T15:58:35","guid":{"rendered":"https:\/\/www.caskeys.com\/dc\/?p=5281"},"modified":"2017-01-15T15:21:58","modified_gmt":"2017-01-15T20:21:58","slug":"class-error-2","status":"publish","type":"post","link":"https:\/\/www.caskeys.com\/dc\/class-error-2\/","title":{"rendered":"Class &#8211; Error"},"content":{"rendered":"<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\n&lt;?php\r\n\r\nclass class_error\r\n{    \r\n\r\n\t\/*\r\n\tclass_error\r\n\tDamon Vaughn Caskey\r\n\t2012_12_28\r\n\t\r\n\tError handler.\r\n\t*\/\t\r\n\t\r\n\tconst \tc_cDBEHost\t\t= &quot;box406.bluehost.com&quot;;\t\/\/Error log DB host.\r\n\tconst \tc_cDBELName\t\t= &quot;caskeysc_uk&quot;;\t\t\t\/\/Error log DB logical name.\r\n\tconst \tc_cDBEUser\t\t= &quot;caskeysc_ehsinfo&quot;;\t\t\/\/Error log DB user.\r\n\tconst \tc_cDBEPword\t\t= &quot;caskeysc_ehsinfo_user&quot;;\t\/\/Error log DB password.\r\n\tconst\tc_iETScript\t\t= 0;\t\t\t\t\t\t\/\/Error type; general script errors.\r\n\tconst\tc_iETDB\t\t\t= 1;\t\t\t\t\t\t\/\/Error type; datbase error.\r\n\t\r\n\tprivate $cIP\t\t\t= NULL;\t\t\t\t\t\t\/\/$_SERVER&#x5B;'REMOTE_ADDR']\r\n\tprivate $cSource\t\t= NULL; \t\t\t\t\t\/\/$_SERVER&#x5B;'PHP_SELF']\r\n\tprivate $debug\t\t\t= 0;\r\n\tprivate $oMail\t\t\t= NULL;\t\t\t\t\t\t\/\/Class mail hanlder.\r\n\tprivate $oUtl\t\t\t= NULL;\t\t\t\t\t\t\/\/Utility functions.\r\n\t\r\n\tpublic\t$cErrType\t\t= NULL;\t\t\t\t\t\t\/\/Error number or user type.\r\n\tpublic \t$cErrCode\t\t= NULL;\t\t\t\t\t\t\/\/Error code.\r\n\tpublic \t$cErrDetail\t\t= NULL;\t\t\t\t\t\t\/\/Error detail (SQL string, parameters, user defined data...).\r\n\tpublic \t$cErrFile\t\t= NULL;\t\t\t\t\t\t\/\/File running at error time.\r\n\tpublic\t$cErrLine\t\t= NULL; \t\t\t\t\t\/\/Error line.\r\n\tpublic \t$cErrMsg\t\t= NULL;\t\t\t\t\t\t\/\/Error message.\r\n\tpublic\t$cErrState\t\t= NULL;\t\t\t\t\t\t\/\/State of server (ex. SQL State).\r\n\tpublic\t$cErrTOE\t\t= NULL;\t\t\t\t\t\t\/\/Time of error.\r\n\tpublic\t$cErrVars\t\t= NULL;\t\t\t\t\t\t\/\/String dump of variables.\r\n\t\r\n\tpublic function __construct($oDep, $debug = 0)\r\n    {\r\n        \/* Import object dependencies. *\/\r\n\t\t$this-&gt;oMail = $oDep&#x5B;'Mail'];\r\n\t\t$this-&gt;oUtl = $oDep&#x5B;'Utl'];\r\n\t\t\t\t\r\n\t\t\/* Verify object dependencies. *\/\r\n\t\tif(!$this-&gt;oMail)\ttrigger_error(&quot;Missing object dependency: Mail.&quot;, E_USER_ERROR);\r\n\t\tif(!$this-&gt;oUtl)\ttrigger_error(&quot;Missing object dependency: Utility.&quot;, E_USER_ERROR);\r\n\t\t\r\n\t\t$this-&gt;debug = $debug;\r\n        set_error_handler(array($this, 'error_handle_start'));\r\n\t\r\n\t\tregister_shutdown_function(array(&amp;$this, 'error_shutdown'));\r\n\t}\r\n\t\r\n\tpublic function error_fatal()\r\n\t{\t\r\n\t\t\/*\r\n\t\terror_fatal\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_30\r\n\t\t\r\n\t\tRun final actions before exit on a fatal error.\r\n\t\t*\/\r\n\t\t\r\n\t\t\/*\r\n\t\tIf headers haven't been sent, redirect user to an error page. Otherwise we'll just have to die and settle for a plain text message.\r\n\t\t*\/\r\n\t\tif($this-&gt;oUtl-&gt;utl_redirect(&quot;\/a_errors\/php.php&quot;)===FALSE)\r\n\t\t{ \r\n\t\t\tdie(&quot;I'm sorry; it appears an internal error has occurred while processing your request. The webmaster has been alerted and will resolve this issue as soon as possible.&quot;);\r\n\t\t}\r\n\t\t\r\n\t\texit;\r\n\t\t\t\r\n\t}\r\n\t\r\n\tpublic function error_handle_start($cCode=NULL, $cMsg=NULL, $cFile=NULL, $cLine=NULL)\r\n\t{\r\n\t\t$this-&gt;error_handle($cCode, $cMsg, $cFile, $cLine, self::c_iETScript, NULL, NULL);\r\n\t\t\r\n\t\treturn true;\t\t\r\n\t}\r\n\t\r\n\tpublic function error_handle($cCode=NULL, $cMsg=NULL, $cFile=NULL, $cLine=NULL, $cType=self::c_iETScript, $cState=NULL, $cDetail=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\terror_run\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_28\r\n\t\t\r\n\t\tRun error mail and and log in single call.\r\n\t\t\t\t\r\n\t\t$cCode:\t\tError code\/number.\r\n\t\t$cMsg:\t\tError message.\r\n\t\t$cFile:\t\tPHP generated file name.\r\n\t\t$cLine:\t\tCode line location.\r\n\t\t$cType:\t\tUser defined error type.\r\n\t\t$cState:\tServer state (mostly for SQL errors).\r\n\t\t$cDetail:\tUser added detail.\r\n\t\t*\/\r\n\t\t\r\n\t\t$iLevel = NULL;\r\n\t\t$value\t= NULL;\r\n\t\t$key\t= NULL;\r\n\t\t$i\t\t= 0;\r\n\t\t\t\t\r\n\t\t$this-&gt;cErrTOE\t\t= date(constants::c_cDateF);\r\n\t\t$this-&gt;cIP\t\t\t= $_SERVER&#x5B;'REMOTE_ADDR'];\r\n\t\t$this-&gt;cSource\t\t= $_SERVER&#x5B;'PHP_SELF'];\r\n\t\t$this-&gt;cErrType\t\t= $cType;\r\n\t\t$this-&gt;cErrFile\t\t= $cFile;\r\n\t\t$this-&gt;cErrLine\t\t= $cLine;\t\r\n\t\t$this-&gt;cErrState\t= $cState;\r\n\t\t$this-&gt;cErrCode\t\t= $cCode;\r\n\t\t$this-&gt;cErrMsg\t\t= $cMsg;\r\n\t\t$this-&gt;cErrDetail\t= $cDetail;\r\n\t\t$this-&gt;cErrVars\t\t= NULL;\r\n\t\t\t\t\t\t\r\n\t\t\/*\r\n\t\tIf logging in (\/authenticate_0001.php) and error is suppressed then exit and do nothing.\r\n\t\t\r\n\t\tLDAP libraries are bugged. EX: ldap_bind throws error 49 on bad password instead of returning FALSE as documented. \r\n\t\tIn PHP this can only be worked around by suppressing the error. Otherwise suppressing errors with @ is bad practice \r\n\t\tthat should be avoided at all costs. Its use will be ignored within any other file.\r\n\t\t*\/\r\n\t\t$iLevel = error_reporting();\r\n\t\t\r\n\t\tif (($iLevel == 0 || ($iLevel &amp; $cCode) == 0) &amp;&amp; $this-&gt;cSource == &quot;\/authenticate_0001.php&quot;)\r\n\t\t{\r\n\t\t\treturn true;\r\n\t\t}\r\n\t\t\t\t\r\n\t\tif($this-&gt;cErrCode)\r\n\t\t{\r\n\t\t\t\/*\r\n\t\t\tLog error to database.\r\n\t\t\t*\/\r\n\t\t\t\/\/$this-&gt;error_log_db();\r\n\t\t\t\r\n\t\t\t\/*\r\n\t\t\tIf error is any type other than a notice then immediately end script and send an email alert to webmaster.\r\n\t\t\t*\/\r\n\t\t\tswitch ($this-&gt;cErrCode)\r\n\t\t\t{\r\n\t\t\t\tcase E_USER_ERROR:\r\n\t\t\t\tcase E_USER_WARNING:\r\n\t\t\t\tcase E_ERROR:\r\n\t\t\t\tcase E_CORE_ERROR:\r\n\t\t\t\tcase E_COMPILE_ERROR:\r\n\t\t\t\tdefault:\r\n\t\t\t\t\t\r\n\t\t\t\t\tif(isset($_GET))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tforeach($_GET as $key =&gt; $value)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t$this-&gt;cErrVars .= &quot;GET&#x5B;&quot;.$key.&quot;]: &quot;.$value.&quot; || &quot;;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\r\n\t\t\t\t\tif(isset($_POST))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tforeach($_POST as $key =&gt; $value)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t$this-&gt;cErrVars .= &quot;POST&#x5B;&quot;.$key.&quot;]: &quot;.$value.&quot; || &quot;;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\t\r\n\t\t\t\t\tif(isset($_SESSION))\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tforeach($_SESSION as $key =&gt; $value)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t$this-&gt;cErrVars .= &quot;SESSION&#x5B;&quot;.$key.&quot;]: &quot;.$value.&quot; || &quot;;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\r\n\t\t\t\t\t$this-&gt;error_mail();\r\n\t\t\t\t\t$this-&gt;error_fatal();\r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase E_USER_NOTICE:\r\n\t\t\t\tcase E_NOTICE:\r\n\t\t\t\t\tbreak;\t\t\t\t\r\n\t\t\t}\t\t\r\n\t\t}\r\n\t}\r\n\t\t\r\n   \tpublic function error_log_db()\r\n\t{\r\n\t\t\/*\r\n\t\terror_db_log\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_28\r\n\t\t\t\t\r\n\t\tAttempt to log error detail to database. Self contained to avoid recursive calls to database class.\r\n\t\t*\/\r\n\t\t\t\t\r\n\t\t$rDBConn\t\t= NULL;\t\/\/Connection reference to DB error log.\r\n\t\t$cQuery\t\t\t= NULL; \/\/Error query string.\r\n\t\t$rDBStatement\t= NULL;\t\/\/Prepared query reference.\r\n\t\t\r\n\t\t$rDBConn = new mysqli(self::c_cDBEHost, self::c_cDBEUser, self::c_cDBEPword, self::c_cDBELName);\r\n\t\t\r\n\t\t\/* If the error log database connection was successful, insert each error to table. *\/\r\n\t\tif (!$rDBConn-&gt;connect_error) \r\n\t\t{\t\t\t\t\r\n\t\t\t\/* Build query string. *\/ \t\t\r\n\t\t\t$cQuery = &quot;INSERT INTO tbl_gen_errors (toe, ip, type, source, file, line, state, code, vars, msg, details) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)&quot;;\r\n\t\t\t$rDBStatement = $rDBConn-&gt;prepare($cQuery);\r\n\t\t\t\r\n\t\t\t\/* Bind parameters. *\/\r\n\t\t\t$rDBStatement-&gt;bind_param(&quot;sssssssssss&quot;, $this-&gt;cErrTOE, $this-&gt;cIP, $this-&gt;cErrType, $this-&gt;cSource, $this-&gt;cErrFile, $this-&gt;cErrLine, $this-&gt;cErrState, $this-&gt;cErrCode, $this-&gt;cErrVars, $this-&gt;cErrMsg, $this-&gt;cErrDetail);\r\n\t\t\t\r\n\t\t\t\/* Execute and close query. *\/ \r\n\t\t\tif($rDBStatement != false)\r\n\t\t\t{\t\t\t\t\t\t\t\r\n\t\t\t\t$rDBStatement-&gt;execute();\r\n\t\t\t\t$rDBStatement-&gt;close();\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\t\/* Close DB connection. *\/\r\n\t\t\t$rDBConn-&gt;close();\t\t\t\r\n\t\t}\t\t\t\t\t\t\r\n\t\t\r\n\t}\r\n\t\r\n\tpublic function error_mail()\r\n\t{\r\n\t\t\/*\r\n\t\terror_mail\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_31\r\n\t\t~2012_01_02: Array list upgrade.\r\n\t\t\r\n\t\tPrepare and send an email error alert.\r\n\t\t*\/\r\n\t\t\r\n\t\t$cMsg \t= NULL;\r\n\t\t\r\n\t\t$cMsg = array(\r\n\t\t\t&quot;Time&quot;\t\t\t\t=&gt;\t$this-&gt;cErrTOE,\r\n\t\t\t&quot;Type&quot;\t\t\t\t=&gt;\t$this-&gt;cErrType,\r\n\t\t\t&quot;IP&quot;\t\t\t\t=&gt;\t$this-&gt;cIP,\r\n\t\t\t&quot;Def. Source File&quot;\t=&gt;\t$this-&gt;cSource,\r\n\t\t\t&quot;Source File&quot;\t\t=&gt;\t$this-&gt;cErrFile,\r\n\t\t\t&quot;Line&quot;\t\t\t\t=&gt;\t$this-&gt;cErrLine,\r\n\t\t\t&quot;State&quot;\t\t\t\t=&gt;\t$this-&gt;cErrState,\r\n\t\t\t&quot;Code&quot;\t\t\t\t=&gt;\t$this-&gt;cErrCode,\r\n\t\t\t&quot;Message&quot;\t\t\t=&gt;\t$this-&gt;cErrMsg,\r\n\t\t\t&quot;Variables&quot;\t\t\t=&gt;\t$this-&gt;cErrVars,\r\n\t\t\t&quot;Details&quot;\t\t\t=&gt;\t$this-&gt;cErrDetail\r\n\t\t);\r\n\t\t\t\t\r\n\t\t$this-&gt;oMail-&gt;mail_send($cMsg, &quot;Error Report&quot;);\r\n\t}\r\n\t\t\t\r\n\tpublic function error_shutdown()\r\n\t{\r\n\t\t\/*\r\n\t\terror_shutdown\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012_12_31\r\n\t\t\r\n\t\tShutdown function to capture error types PHP will not normally allow custom error handlers to deal with.\r\n\t\t*\/\r\n\t\t\r\n\t\t$cError\t= NULL;\t\t\/\/Last error status.\r\n\t\t\r\n\t\t\/*\r\n\t\tGet last error status.\r\n\t\t*\/\r\n        $cError = error_get_last();\r\n\t\t\r\n\t\t$this-&gt;error_handle($cError&#x5B;'type'], $cError&#x5B;'message'], $cError&#x5B;'file'], $cError&#x5B;'line']);\r\n    }\t\r\n}\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Error class.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[71],"tags":[27,234],"class_list":["post-5281","post","type-post","status-publish","format-standard","hentry","category-technology-temerity","tag-coding","tag-coding-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lNM5-1nb","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/comments?post=5281"}],"version-history":[{"count":2,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5281\/revisions"}],"predecessor-version":[{"id":6184,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5281\/revisions\/6184"}],"wp:attachment":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media?parent=5281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/categories?post=5281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/tags?post=5281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}