/** * The default implementation. */ function pie_errors($params) { extract($params); /** * @var Exception $exception */ if (!empty($exception)) { Pie_Response::addError($exception); $errors = Pie_Response::getErrors(); } $errors_array = Pie_Exception::toArray($errors); $exception_array = Pie_Exception::toArray($exception); // Simply return the errors, if this was an AJAX request if ($is_ajax = Pie_Request::isAjax()) { switch (strtolower($is_ajax)) { case 'json': default: $json = json_encode(array('errors' => $errors_array, 'exception' => $exception_array)); $callback = Pie_Request::callback(); echo $callback ? "{$callback}({$json})" : $json; } return; } // Forward internally, if it was requested if (isset($_REQUEST['_pie']['onErrors'])) { $uri = Pie_Dispatcher::uri(); $uri2 = Pie_Uri::from($_REQUEST['_pie']['onErrors']); if ($uri !== $uri2) { Pie_Dispatcher::forward($uri2); return; // we don't really need this, but it's here anyway } } if (Pie::eventStack('pie/response')) { // Errors happened while rendering response. Just render errors view. return Pie::view('pie/errors.php', $params); } try { // Try rendering the response, expecting it to // display the errors along with the rest. $ob = new Pie_OutputBuffer(); Pie::event('pie/response', compact('errors', 'exception', 'errors_array', 'exception_array')); $ob->endFlush(); } catch (Exception $exception) { $output = $ob->getClean(); return Pie::event('pie/exception', compact('exception')); } }
/** * Returns a <style> tag with the content of all the stylesheets included inline * * @param $styles * If not empty, this associative array contains styles which will be * included at the end of the generated <style> tag. * @param string $slot_name * Optional. If provided, returns only the stylesheets added while filling this slot. * * @return string * the style tags and their contents inline */ static function stylesheetsInline($styles = array(), $slot_name = null) { $styles = self::stylesInline($slot_name, false); if (empty($styles) and empty(self::$stylesheets)) { return ''; } $return = "<style type='text/css'>\n"; if (!empty(self::$stylesheets)) { foreach (self::$stylesheets as $stylesheet) { $href = ''; $media = 'screen, print'; $type = 'text/css'; extract($stylesheet, EXTR_IF_EXISTS); $ob = new Pie_OutputBuffer(); if (Pie_Valid::url($href)) { try { include $href; } catch (Exception $e) { } } else { list($href, $filename) = Pie_Html::themedUrlAndFilename($href); try { Pie::includeFile($filename); } catch (Exception $e) { } } $stylesheet = "\n/* Included inline from {$href} */\n" . $ob->getClean(); $return .= "{$stylesheet}\n"; } } $return .= "/* Included inline from Pie_Response::stylesInline() */\n"; $return .= $styles; $return .= "\n</style>"; return $return; }
/** * Dumps a variable. * Note: cannot show protected or private members of classes. * @param mixed $var * the variable to dump * @param integer $max_levels * the maximum number of levels to recurse * @param string $label * optional - label of the dumped variable. Defaults to $. * @param boolean $as_text * if true, dumps as text instead of markup */ static function var_dump($var, $max_levels = null, $label = '$', $return_content = null) { if ($return_content === 'text') { $as_text = true; } else { $as_text = Pie::textMode(); } $scope = false; $prefix = 'unique'; $suffix = 'value'; if ($scope) { $vals = $scope; } else { $vals = $GLOBALS; } $old = $var; $var = $new = $prefix . rand() . $suffix; $vname = FALSE; foreach ($vals as $key => $val) { if ($val === $new) { // ingenious way of finding a global var :) $vname = $key; } } $var = $old; if ($return_content) { $ob = new Pie_OutputBuffer(); } if ($as_text) { echo PHP_EOL; } else { echo "<pre style='margin: 0px 0px 10px 0px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>"; } if (!isset(self::$var_dump_max_levels)) { self::$var_dump_max_levels = Pie_Config::get('pie', 'var_dump_max_levels', 5); } $current_levels = self::$var_dump_max_levels; if (isset($max_levels)) { self::$var_dump_max_levels = $max_levels; } self::do_dump($var, $label . $vname, null, null, $as_text); if (isset($max_levels)) { self::$var_dump_max_levels = $current_levels; } if ($as_text) { echo PHP_EOL; } else { echo "</pre>"; } if ($return_content) { return $ob->getClean(); } }
/** * Dumps the result as a table in text mode * Side effect, though: can't fetch anymore until the cursor is closed. * @return string */ function __toString() { return "Db_Result"; try { $ob = new Pie_OutputBuffer(); $rows = $this->stmt->fetchAll(PDO::FETCH_ASSOC); Db::dump_table($rows); return $ob->getClean(); } catch (Exception $e) { return $e->getMessage(); } }
/** * Dispatches a URI for internal processing. * Usually called by a front controller. * @param mixed $uri * Optional. You can pass a custom URI to dispatch. Otherwise, PIE will attempt * to route the requested URL, if any. * @param array $check * Optional. Pass array() to skip checking whether the URI can be obtained * as a result of routing some URL. * @return boolean */ static function dispatch($uri = null, $check = array('accessible')) { if (!is_array($check)) { $check = array('accessible'); } if (isset($uri)) { if (in_array('accessible', $check)) { if (!Pie_Uri::url($uri)) { // We shouldn't dispatch to this URI $uri = Pie_Uri::from(array()); } } self::$uri = Pie_Uri::from($uri); } else { self::$uri = Pie_Request::uri(); } // if file or dir is requested, try to serve it $served = false; $skip = Pie_Config::get('pie', 'dispatcherSkipFilename', false); $filename = $skip ? false : Pie_Request::filename(); if ($filename) { if (is_dir($filename)) { $served = Pie::event("pie/dir", compact('filename', 'routed_uri')); $dir_was_served = true; } else { $served = Pie::event("pie/file", compact('filename', 'routed_uri')); $dir_was_served = false; } } // if response was served, then return if ($served) { self::result($dir_was_served ? "Dir served" : "File served"); return true; } // This loop is for forwarding $max_forwards = Pie_Config::get('pie', 'maxForwards', 10); for ($try = 0; $try < $max_forwards; ++$try) { // Make an array from the routed URI $routed_uri_array = array(); if (self::$uri instanceof Pie_Uri) { $routed_uri_array = self::$uri->toArray(); } // If no module was found, then respond with noModule and return if (!isset(self::$uri->module)) { Pie::event("pie/noModule", $routed_uri_array); // should echo things self::result("No module"); return false; } $module = self::$uri->module; // Implement restricting of modules we are allowed to access $routed_modules = Pie_Config::get('pie', 'routedModules', null); if (isset($routed_modules)) { if (!in_array($module, $routed_modules)) { Pie::event('pie/notFound', $routed_uri_array); // should echo things self::result("Unknown module"); return false; } } else { if (!Pie::realPath("handlers/{$module}")) { Pie::event('pie/notFound', $routed_uri_array); // should echo things self::result("Unknown module"); return false; } } try { // Fire a pure event, for aggregation etc if (!in_array('pie/prepare', self::$skip)) { Pie::event('pie/prepare', $routed_uri_array, true); } // Perform validation if (!in_array('pie/validate', self::$skip)) { Pie::event('pie/validate', $routed_uri_array); // Check if any errors accumulated if (Pie_Response::getErrors()) { // There were validation errors -- render a response self::errors(null, $module, null); self::result('Validation errors'); return false; } } // Time to instantiate some app objects from the request if (!in_array('pie/objects', self::$skip)) { Pie::event('pie/objects', $routed_uri_array, true); } // We might want to reroute the request if (!in_array('pie/reroute', self::$skip)) { $stop_dispatch = Pie::event('pie/reroute', $routed_uri_array, true); if ($stop_dispatch) { self::result("Stopped dispatch"); return false; } } if (Pie_Request::isPost()) { if (!in_array('pie/post', self::$skip)) { // Make some changes to server state, possibly Pie::event('pie/post', $routed_uri_array); } } // Time to instantiate some app objects from the request if (!in_array('pie/analytics', self::$skip)) { Pie::event('pie/analytics', $routed_uri_array, true); } // Start buffering the response, unless otherwise requested if ($handler = Pie_Response::isBuffered()) { $ob = new Pie_OutputBuffer($handler); } // Generate and render a response Pie::event("pie/response", $routed_uri_array); if (!empty($ob)) { $ob->endFlush(); } self::result("Served response"); return true; } catch (Pie_Exception_DispatcherForward $e) { // Go again, this time with a different URI. self::$uri = Pie_Uri::from($e->uri); if (is_array($e->skip)) { self::$skip = $e->skip; } else { // Don't process the POST fields this time around self::$skip = array('pie/post'); } // We'll be handling errors anew self::$handling_errors = false; } catch (Pie_Exception_DispatcherErrors $e) { if (!empty($ob)) { $partial_response = $ob->getClean(); } else { $partial_response = null; } self::errors(null, $module, $partial_response); self::result("Rendered errors"); return true; } catch (Exception $exception) { if (!empty($ob)) { $partial_response = $ob->getClean(); } else { $partial_response = null; } self::errors($exception, $module, $partial_response); self::result("Exception occurred"); return false; } } // If we are here, we have done forwarding too much throw new Pie_Exception_Recursion(array('function_name' => 'Dispatcher::forward()')); }
/** * Dumps the result as a table in text mode */ function __toString() { try { $ob = new Pie_OutputBuffer(); $results = array(); foreach ($this->fields as $key => $value) { $results[] = array('Field name:' => $key, 'Field value:' => $value, 'Field modified:' => $this->wasModified($key) ? 'Yes' : 'No'); } Db_Utils::dump_table($results); return $ob->getClean(); } catch (Exception $e) { return $e->getMessage(); } }