/** * Does security check and runs controller. * * @version 1.1 * @author greg <*****@*****.**> * * @date 20110307 (v1.1) (greg) calls via Ajax don't exit anymore but empty the template output instead * * @return mixed */ public function run() { is_object($this->security) ? $this->security->securityIni() : exit('Access Denied!'); $result = null; if (PU_isAJAX()) { /** * This allows to load a widget/ajax theme controller via ajax without triggering the runAjax. * Now runAjax can still be used within the widget/ajax menu type controller. */ if ($this->core->ajaxType == true || !empty($this->_GET['widget']) || !empty($this->_GET['ajax']) || !empty($this->_GET['lightbox'])) { $result = $this->execute(); } else { $result = $this->runAJAX(); } } else { $result = $this->execute(); } return $result; }
/** * 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; } }
public function testHandleResult_JsonRequest() { $PHPDS = PHPDSlib::instance(); $_SERVER["HTTP_X_REQUESTED_WITH"] = 'XMLHttpRequest'; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $data = array('test' => true); $this->assertTrue(PU_isAJAX()); $this->assertEquals('{"test":true}', PU_isJSON($data)); // set up the template with fake data and call the method under test $template = $PHPDS->PHPDS_template(); $core = $PHPDS->PHPDS_core(); $this->assertType('PHPDS_template', $template); $this->assertType('PHPDS_core', $core); $core->themeFile = 'testTemplate'; $core->data = 'testController'; // TODO: decide what is the correct behavior // test a controller result of false: error /*$result = $this->object->handleResult(false); $this->assertFalse($result); $this->assertEquals('testTemplate', $core->themeFile); $this->assertEquals('testController', $template->controller);*/ // test a controller result of null: standard handling $result = $this->object->handleResult(null); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('null', $core->data); // test a controller result of true: don't do anything $result = $this->object->handleResult(true); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('true', $core->data); // test a controller result of some data $result = $this->object->handleResult($data); $this->assertTrue($result); $this->assertEquals('', $core->themeFile); $this->assertEquals('{"test":true}', $core->data); // TODO: should this throw an exception? // test an invalid controller result /*$this->setExpectedException('PHPDS_exception'); $result = $this->object->handleResult(array('test' => true));*/ }
public function testIsJson() { $data = array('test' => true); $_SERVER["HTTP_X_REQUESTED_WITH"] = ''; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $this->assertFalse(PU_isAJAX()); $this->assertEquals('', PU_isJSON($data)); $_SERVER["HTTP_X_REQUESTED_WITH"] = 'XMLHttpRequest'; $_SERVER["HTTP_X_REQUESTED_TYPE"] = 'json'; $this->assertTrue(PU_isAJAX()); $this->assertEquals('{"test":true}', PU_isJSON($data)); }
<?php /** * This ajax will be displayed whenever an unhandled error or exception occurs in PHPDevShell */ // If the error occured during an AJAX request, we'll send back a lightweight ouput if (PU_isAJAX()) { if (!empty($message)) { print "{$message} - file {$filepath} line {$lineno}"; } else { print 'Unknown error'; } } else { // for a regular request, we present a nicely formatted html page; if provided, an extended description of the error is displayed $skin = $this->configuration['skin']; ?> <!DOCTYPE HTML> <html lang="en"> <head> <title>Serious Error Encountered</title> <meta charset=UTF-8> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="keywords" content="critical, error"> <meta name="description" content="We encountered an error"> <link rel="stylesheet" href="<?php echo $aurl; ?> /themes/cloud/css/reset.css" type="text/css" media="screen, projection" /> <link rel="stylesheet" href="<?php echo $aurl; ?>
/** * OBSOLETE don't use * * @param $data */ function PU_exitToAJAX($data) { PU_isAJAX(true); print json_encode($data); exit; }
/** * 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; }