Esempio n. 1
0
 /**
  * Destruct Class.
  *
  * @date 20091030 mute possible error with @ (in case destruct is called before ob is started)
  * @author	greg
  * @version 1.0.1
  * @return nothing
  *
  */
 public function __destruct()
 {
     // Flush.
     PU_cleanBuffers(true);
 }
Esempio n. 2
0
 /**
  * Run default, custom or no template.
  *
  * @version 2.1.1
  *
  * @date 20120920 (v2.1.1) (greg) fixed a typo with $url
  * @date 20120312 (v2.1) (greg) added loggin of access errors (404 and such)
  * @date 20120223 (v2.0) (jason) rewrite
  * @date 20110308 (v1.2) (greg) allows the new style controller to alter the current template to be used
  * @date20100520 (v1.1) (greg) added merging with modules from the configuration array
  *
  * @author Jason Schoeman
  */
 public function startController()
 {
     $this->setDefaultNodeParams();
     try {
         ob_start();
         $this->db->startTransaction();
         $this->executeController();
         $this->db->endTransaction();
         if (empty($this->data)) {
             $this->data = ob_get_clean();
         } else {
             PU_cleanBuffers();
         }
     } catch (Exception $e) {
         PU_cleanBuffers();
         $this->themeFile = '';
         if (is_a($e, 'PHPDS_accessException')) {
             $logger = $this->factory('PHPDS_debug', 'PHPDS_accessException');
             $url = $this->configuration['absolute_url'] . $_SERVER['REQUEST_URI'];
             switch ($e->HTTPcode) {
                 case 401:
                     if (!PU_isAJAX()) {
                         $this->themeFile = 'login.php';
                     }
                     PU_silentHeader("HTTP/1.1 401 Unauthorized");
                     PU_silentHeader("Status: 401");
                     $logger->error('URL unauthorized: ' . $url, '401');
                     break;
                 case 404:
                     if (!PU_isAJAX()) {
                         $this->themeFile = '404.php';
                     }
                     PU_silentHeader("HTTP/1.1 404 Not Found");
                     PU_silentHeader("Status: 404");
                     $logger->error('URL not found: ' . $url, '404');
                     break;
                 case 403:
                     if (!PU_isAJAX()) {
                         $this->themeFile = '403.php';
                     }
                     PU_silentHeader("HTTP/1.1 403 Forbidden");
                     PU_silentHeader("Status: 403");
                     $logger->error('URL forbidden ' . $url, '403');
                     break;
                 case 418:
                     sleep(30);
                     // don't make spambot life live in the fast lane
                     if (!PU_isAJAX()) {
                         $this->themeFile = '418.php';
                     }
                     PU_silentHeader("HTTP/1.1 418 I'm a teapot and you're a spambot");
                     PU_silentHeader("Status: 418");
                     $logger->error('Spambot for ' . $url, '418');
                     break;
                 default:
                     throw $e;
             }
         } else {
             throw $e;
         }
     }
     // Only if we need a theme.
     if (!empty($this->themeFile)) {
         $this->loadTheme();
     } else {
         print $this->data;
     }
 }
 /**
  * Display an Exception
  *
  * This function will load a predefined template page (in PHP form) in order to warn the user something has gone wrong.
  *
  * If an exception is provided, it will be detailed as much as possible ; if not, only a generic message will be displayed
  *
  * @date 20100918
  * @date 20120511 (v1.1) (greg) output is captured in case we want to save it
  * @date 20120724 (v1.2) (greg) added "probable origin"
  * @version 1.2
  * @author greg <*****@*****.**>
  *
  * @return string the whole output
  *
  * @param Exception $e (optional) the exception to explain
  * @param boolean $detailed whether the details should be displayed or replaced y a generic message
  */
 public function showException(Exception $e = null, $detailed = true)
 {
     // we stop on the first unhandled error
     $this->I_give_up = true;
     if ($this->PHPDS_dependance()->isEmbedded()) {
         return;
     }
     PU_cleanBuffers();
     if (is_a($e, 'Exception')) {
         $lineno = $e->getLine();
         $filepath = $e->getFile();
         $trace = is_a($e, 'PHPDS_exception') ? $e->getExtendedTrace() : $e->getTrace();
         $ignore = is_a($e, 'PHPDS_exception') ? $e->getIgnoreLines() : -1;
         $filefragment = PHPDS_backtrace::fetchCodeFragment($filepath, $lineno);
         if (isset($trace[$ignore])) {
             $frame = $trace[$ignore];
             $framefragment = PHPDS_backtrace::fetchCodeFragment($frame['file'], $frame['line']);
         } else {
             $ignore = -1;
         }
         $message = $e->getMessage();
         $code = $e->getCode();
         $extendedMessage = is_a($e, 'PHPDS_exception') ? $e->getExtendedMessage() : '';
         $config = $this->configuration;
         if (!empty($config)) {
             if (isset($config['config_files_used'])) {
                 $conf['used'] = PU_dumpArray($config['config_files_used']);
             }
             if (isset($config['config_files_missing'])) {
                 $conf['missing'] = PU_dumpArray($config['config_files_missing']);
             }
         }
         $bt = PHPDS_backtrace::asHTML($ignore, $trace);
     } else {
         $message = "Unknown exception...";
         $code = null;
     }
     // now use the theme's error page to format the actual display
     $protocol = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
     // Need this for absolute URL configuration to be sef safe.
     $aurl = $protocol . $_SERVER['SERVER_NAME'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']);
     ob_start();
     // Load error page: $e is the handled exception
     require BASEPATH . 'themes/default/error.php';
     $output = ob_get_clean();
     if (!empty($this->crumbs)) {
         $output = str_replace('<crumbs/>', implode("\n", $this->crumbs), $output);
     }
     if (PU_isAJAX()) {
         // If the error occurred during an AJAX request, we'll send back a lightweight ouput
         $message = $this->display ? "{$message} - file {$filepath} line {$lineno}" : 'Error Concealed - Disabled in config';
         PU_silentHeader('Status: 500 ' . $message);
         PU_silentHeader('HTTP/1.1 500 ' . $message);
         print $message;
     } else {
         // for a regular request, we present a nicely formatted html page; if provided, an extended description of the error is displayed
         if ($detailed) {
             echo $output;
         } else {
             $message = '';
             require BASEPATH . 'themes/default/error.php';
             // $message being empty, only a genetic message is output
         }
     }
     return $output;
 }
Esempio n. 4
0
/**
 * Determines if the current request has been made by some kind of ajax call (i.e. XMLHttpRequest)
 *
 * @param boolean $json set to true if you want to force the request's result as json
 * @return boolean
 *
 * @version 1.0.1
 * @date 20110809 (v1.0.1) (greg) use PU_silentHeader to prevent unit tests from failing
 * @author greg <*****@*****.**>
 */
function PU_isAJAX($json = false)
{
    $ajax = !empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest');
    if ($ajax && $json) {
        PU_cleanBuffers();
        PU_silentHeader('Content-Type: application/json');
    }
    return $ajax;
}
 /**
  * Display an Exception
  *
  * This function will load a predefined template page (in PHP form) in order to warn the user something has gone wrong.
  *
  * If an exception is provided, it will be detailed as much as possible ; if not, only a generic message will be displayed
  *
  * @date 20100918
  * @date 20120511 (v1.1) (greg) output is captured in case we want to save it
  * @version 1.1
  * @author greg
  *
  * @return string the whole output
  *
  * @param Exception $e (optional)
  */
 public function showException(Exception $e = null, $display = true)
 {
     // we stop on the first unhandled error
     $this->I_give_up = true;
     if ($this->PHPDS_dependance()->isEmbedded()) {
         return;
     }
     PU_cleanBuffers();
     if (is_a($e, 'Exception')) {
         $lineno = $e->getLine();
         $filepath = $e->getFile();
         $trace = is_a($e, 'PHPDS_exception') ? $e->getExtendedTrace() : $e->getTrace();
         $ignore = is_a($e, 'PHPDS_exception') ? $e->getIgnoreLines() : 1;
         $filefragment = PHPDS_backtrace::fetchCodeFragment($filepath, $lineno);
         $message = $e->getMessage();
         $extendedMessage = is_a($e, 'PHPDS_exception') ? $e->getExtendedMessage() : '';
         $config = $this->configuration;
         if (!empty($config)) {
             if (isset($config['config_files_used'])) {
                 $conf['used'] = PU_dumpArray($config['config_files_used']);
             }
             if (isset($config['config_files_missing'])) {
                 $conf['missing'] = PU_dumpArray($config['config_files_missing']);
             }
         }
         $bt = PHPDS_backtrace::asHTML($ignore, $trace);
     }
     $protocol = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
     // Need this for absolute URL configuration to be sef safe.
     $aurl = $protocol . $_SERVER['SERVER_NAME'] . str_replace('/index.php', '', $_SERVER['PHP_SELF']);
     ob_start();
     // Load error page: $e is the handled exception
     require_once BASEPATH . 'themes/cloud/error.php';
     $output = ob_get_clean();
     if ($display) {
         echo $output;
     }
     return $output;
 }