From 7c38fbeadf2c0649229817d2d8fd61d90dc35e10 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Fri, 20 Jun 2008 17:44:31 +0000
Subject: [PATCH] Set up exception handling.

---
 .../default/controllers/ErrorController.php   | 65 ++++++++++++++++---
 .../default/views/scripts/error/error.phtml   | 10 ++-
 2 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/application/modules/default/controllers/ErrorController.php b/application/modules/default/controllers/ErrorController.php
index 116289f3..e8178eb8 100644
--- a/application/modules/default/controllers/ErrorController.php
+++ b/application/modules/default/controllers/ErrorController.php
@@ -2,20 +2,69 @@
 
 /**
  * ErrorController
- * 
+ *
  * @author
- * @version 
+ * @version
  */
 
 require_once 'Zend/Controller/Action.php';
 
-class ErrorController extends Unl_Controller_Action {
-	/**
-	 * The default action - show the home page
-	 */
+class ErrorController extends App_Controller_Action {
+
+    protected $_exceptions;
+
 	public function errorAction() {
-		$exceptions = $this->getResponse()->getException();
-		throw $exceptions[0];
+		$this->_exceptions = $this->getResponse()->getException();
+
+		if ($_SERVER['REMOTE_ADDR'] == '129.93.39.17') {
+		    header('Content-type: text/plain');
+		    print_r($this->_exceptions);
+		    exit;
+		}
+
+		$this->_sendEmail('tsteiner2@unl.edu');
+		$this->view->exceptions = $this->_exceptions;
 	}
 
+	protected function _sendEmail($address, $moreAddresses = '...')
+    {
+        $mail = new Zend_Mail('utf-8');
+        $mail->setFrom('php@' . $_SERVER['SERVER_NAME'], $_SERVER['SERVER_NAME']);
+        $mail->setSubject('Exception Report');
+
+        foreach(func_get_args() as $address)
+        {
+            if(is_array($address)) {
+                $mail->addTo($address[0], $address[1]);
+            } else {
+                $mail->addTo($address);
+            }
+        }
+
+        $date = date('Y-m-d');
+        $time = date('H:i');
+        $body = "The following exception occured on $date at $time.\n\n"
+              . print_r($this->_exceptions, true)
+              . "\nRequest URI: " . $_SERVER['REQUEST_URI'] . "\n"
+              . "User Agent: " . $_SERVER['HTTP_USER_AGENT'] . "\n"
+              . "User IP Address :" . $_SERVER['REMOTE_ADDR'] ."\n";
+
+        if(count($_GET) > 0) {
+            $body .= "\nGET variables: \n"
+                   . print_r($_GET, true);
+        }
+        if(count($_POST) > 0) {
+            $body .= "\nPOST variables: \n"
+                   . print_r($_POST, true);
+        }
+        /*
+        if(session_id() && ($_SESSION) > 0) {
+            $body .= "\nSESSION variables: \n"
+                   . print_r($_SESSION, true);
+        }
+        */
+
+        $mail->setBodyText($body);
+        $mail->send();
+    }
 }
diff --git a/application/modules/default/views/scripts/error/error.phtml b/application/modules/default/views/scripts/error/error.phtml
index 6cc117df..928c154a 100644
--- a/application/modules/default/views/scripts/error/error.phtml
+++ b/application/modules/default/views/scripts/error/error.phtml
@@ -1 +1,9 @@
-<h1>Error</h1>
\ No newline at end of file
+<h1>Unfortunately an unexpected error has occured.</h1>
+<h2>Details have been mailed to the administrator for analysis.</h2>
+<p>
+    The error message was: "<?php echo $this->exceptions[0]->getMessage(); ?>"
+</p>
+<p>
+    We appologize for the inconvience.
+    Please use the back button to return to your previous location
+</p>
\ No newline at end of file
-- 
GitLab