/** * @param string $subject * The subject. May contain variable references to memebrs * of the $fields array. * @param string $view * The name of a view * @param array $fields * The fields referenced in the subject and/or view * @param array $optoins * Array of options. Can include: * "html" => Defaults to false. Whether to send as HTML email. * "name" => A human-readable name in addition to the address. * "from" => An array of email_address => human_readable_name */ function sendMessage($subject, $view, $fields = array(), $options = array()) { if (!isset($options['from'])) { $url_parts = parse_url(Pie_Request::baseUrl()); $domain = $url_parts['host']; $options['from'] = array("email_bot@" . $domain => $domain); } else { if (!is_array($options['from'])) { throw new Pie_Exception_WrongType(array('field' => '$options["from"]', 'type' => 'array')); } } // Set up the default mail transport $tr = new Zend_Mail_Transport_Sendmail('-f' . $this->address); Zend_Mail::setDefaultTransport($tr); $mail = new Zend_Mail(); $from_name = reset($options['from']); $mail->setFrom(key($options['from']), $from_name); if (isset($options['name'])) { $mail->addTo($this->address, $options['name']); } else { $mail->addTo($this->address); } $subject = Pie::expandString($subject, $fields); $body = Pie::view($view, $fields); $mail->setSubject($subject); if (empty($options['html'])) { $mail->setBodyText($body); } else { $mail->setBodyHtml($body); } $mail->send(); return true; }
static function execute() { // Fixes for different platforms: if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // ISAPI 3.0 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } // Define a constant if (!defined('PIE_CONTROLLER')) { define('PIE_CONTROLLER', 'Pie_WebController'); } try { Pie::log("Request for " . Pie_Request::url(true)); Pie_Dispatcher::dispatch(); $dispatch_result = Pie_Dispatcher::result(); if (!isset($dispatch_result)) { $dispatch_result = 'Ran dispatcher'; } $uri = Pie_Dispatcher::uri(); $module = $uri->module; $action = $uri->action; if ($module and $action) { $slot_names = Pie_Request::slotNames(); $requested_slots = empty($slot_names) ? '' : implode(',', array_keys($slot_names)); Pie::log("~" . ceil(Pie::microtime()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " {$dispatch_result} for {$module}/{$action}" . " ({$requested_slots})"); } else { Pie::log("~" . ceil(Pie::microtime()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " No route for " . $_SERVER['REQUEST_URI']); } } catch (Exception $exception) { Pie::event('pie/exception', compact('exception')); } }
/** * Default pie/notFound handler. * Just displays pie/notFound.php view. */ function pie_notFound($params) { header("HTTP/1.0 404 Not Found"); Pie_Dispatcher::result("Nothing found"); $url = Pie_Request::url(); echo Pie::view('pie/notFound.php', compact('url')); }
/** * Default pie/dir handler. * Just displays a simple directory listing, * and prevents further processing by returning true. */ function pie_dir() { $filename = Pie_Request::filename(); // TODO: show directory listing echo Pie::view('pie/dir.php', compact('filename')); return true; }
function pie_response_content() { $serve_fbml = Pie_Request::accepts('text/fbml'); if ($serve_fbml) { // add more fbjs files here } else { // the js files for your app Pie_Response::addScript('plugins/pie/js/Pie.js'); Pie_Response::addScript("http://cdn.jquerytools.org/1.2.3/jquery.tools.min.js"); Pie_Response::addScript('plugins/users/js/Users.js'); // See views/layout/html.php for a facebook script at the top of the <body> } Pie_Response::addStylesheet('plugins/pie/css/Ui.css'); $app = Pie_Config::expect('pie', 'app'); $url = Pie_Request::url(); $module = Pie_Dispatcher::uri()->module; if (empty($module)) { return Pie::event("{$app}/notFound/response/content"); } $action = Pie_Dispatcher::uri()->action; $event = "{$module}/{$action}/response/content"; if (!Pie::canHandle($event)) { return Pie::event("{$app}/notFound/response/content"); } // Go ahead and fire the event, returning the result. return Pie::event($event); }
function users_before_pie_redirect($params) { if (Pie_Request::accepts('text/fbml')) { // We are in an FBML canvas page, so redirect the facebook way Users::$facebook->redirect($params['url']); return true; } }
/** * Override pie/noModule handler. * just goes on to render our app's response, * which will echo a 404 view. */ function pie_noModule($params) { if (!Pie_Request::accepts('text/fbml')) { header("HTTP/1.0 404 Not Found"); } Pie_Dispatcher::uri()->module = Pie_Config::expect('pie', 'app'); Pie_Dispatcher::uri()->action = ''; Pie::event('pie/response', $params); }
function users_before_pie_init() { $facebook_apps = Pie_Config::get('users', 'facebookApps', array()); foreach ($facebook_apps as $app_id => $fb_info) { if (isset($fb_info['url'])) { $subpath = isset($fb_info['subpath']) ? $fb_info['subpath'] : ''; Pie_Config::set('pie', 'proxies', Pie_Request::baseUrl(true) . $subpath, $fb_info['url']); } } }
/** * This is a tool for selecting photos (to possibly add) * @param $facebook * Optional. You can provide instance of the Facebook class. * @param $upload * Defaults to false. If true, shows an option to upload, as well. * @param $action_uri * Defaults to 'items/addPhoto'. The URI to submit the form to. * @param $filter_visible * Optional string. Set to 'everyone' to only display albums visible to everyone. * @param $on_success * Optional string. The url to redirect to after a photo is added or uploaded. */ function items_addPhoto_tool($params) { if (isset(Users::$facebook)) { $facebook = Users::$facebook; } else { $app = Pie_Config::expect('pie', 'app'); if (!isset(Users::$facebooks[$app])) { throw new Pie_Exception_MissingObject(array('name' => 'Users::$facebooks[' . $app . ']')); } $facebook = Users::$facebooks[$app]; } $defaults = array('facebook' => $facebook, 'upload' => false, 'action_uri' => 'items/addPhoto', 'on_success' => Pie_Request::url()); extract(array_merge($defaults, $params)); if (!$facebook instanceof Facebook) { throw new Pie_Exception_WrongType(array('field' => '$facebook', 'type' => 'Facebook')); } if (isset($_REQUEST['_pie']['onSuccess'])) { $on_success = $_REQUEST['_pie']['onSuccess']; } $sn = Pie_Session::name(); $sid = Pie_Session::id(); $photos = array(); if (isset($aid)) { $photos = Items::facebookPhotos($facebook, $aid); return Pie::view('items/tool/addPhotoList.php', compact('photos')); } $facebook->require_login(); $album_rows = Items::facebookAlbums($facebook); $albums = array(); foreach ($album_rows as $ar) { if (isset($filter_visible) and $ar['visible'] != $filter_visible) { continue; } $albums[$ar['aid']] = $ar['name']; } $albums = $albums; if (count($album_rows)) { $row = reset($album_rows); $photos = Items::facebookPhotos($facebook, $row['aid']); } $throbber_url = Pie_Html::themedUrl('plugins/items/img/anim/throbber.gif'); $url_json = json_encode(Pie_Uri::url($action_uri)); Pie_Response::addStylesheet('plugins/items/css/Items.css'); if (Pie_Request::accepts('text/fbml')) { Pie_Response::addScript('plugins/items/fbjs/Items.fb.js'); } else { Pie_Response::addScript('plugins/items/js/Items.js'); } if (is_bool($upload)) { $upload = uniqid('up.', false); } $addPhoto_url_json = json_encode(Pie_Uri::url('items/addPhoto')); Pie_Response::addScriptLine("\tPie.Items.urls['items/addPhoto'] = {$addPhoto_url_json};"); return Pie::view('items/tool/addPhoto.php', compact('action_uri', 'on_success', 'on_added', 'albums', 'photos', 'throbber_url', 'upload')); }
static function execute() { // Fixes for different platforms: if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // ISAPI 3.0 $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } // Define a constant if (!defined('PIE_CONTROLLER')) { define('PIE_CONTROLLER', 'Pie_ActionController'); } try { $parts = explode('/', Pie_Request::tail()); $parts_len = count($parts); if ($parts_len >= 1) { $module = $parts[0]; } if ($parts_len >= 2) { $action = $parts[1]; } // Make sure the 'pie'/'web' config fields are set, // otherwise URLs will be formed pointing to the wrong // controller script. $ar = Pie_Config::get('pie', 'web', 'appRootUrl', null); if (!isset($ar)) { throw new Pie_Exception_MissingConfig(array('fieldpath' => 'pie/web/appRootUrl')); } $cs = Pie_Config::get('pie', 'web', 'controllerSuffix', null); if (!isset($cs)) { throw new Pie_Exception_MissingConfig(array('fieldpath' => 'pie/web/controllerSuffix')); } // Dispatch the request $uri = Pie_Uri::from(compact('module', 'action')); Pie_Dispatcher::dispatch($uri); $dispatch_result = Pie_Dispatcher::result(); if (!isset($dispatch_result)) { $dispatch_result = 'Ran dispatcher'; } $uri = Pie_Dispatcher::uri(); $module = $uri->module; $action = $uri->action; if ($module and $action) { $slot_names = Pie_Request::slotNames(); $requested_slots = empty($slot_names) ? '' : implode(',', array_keys($slot_names)); Pie::log("~" . ceil(Pie::microtime()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " {$dispatch_result} for {$module}/{$action}" . " ({$requested_slots})"); } else { Pie::log("~" . ceil(Pie::microtime()) . 'ms+' . ceil(memory_get_peak_usage() / 1000) . 'kb.' . " No route for " . $_SERVER['REQUEST_URI']); } } catch (Exception $exception) { Pie::event('pie/exception', compact('exception')); } }
static function init() { if (Pie_Config::get('pie', 'session', 'appendSuffix', false) or isset($_GET[self::name()])) { if (self::id()) { $s = '?' . self::name() . '=' . self::id(); $suffix = Pie_Uri::suffix(); $base_url = Pie_Request::baseUrl(); $suffix[$base_url] = isset($suffix[$base_url]) ? $suffix[$base_url] . $s : $s; Pie_Uri::suffix($suffix); } } self::$inited = true; }
function users_contact_tool($params) { $defaults = array('uri' => 'users/contact', 'omit' => array(), 'fields' => array(), 'title' => "Contact Info", 'collapsed' => false, 'toggle' => false, 'editing' => true, 'complete' => true, 'inProcess' => false, 'prompt' => "In order for things to work, we must be able to reach you.", 'button_content' => 'OK'); extract(array_merge($defaults, $params)); $default_fields = array('first_name' => array('type' => 'text', 'label' => 'First Name'), 'last_name' => array('type' => 'text', 'label' => 'Last Name'), 'email_address' => array('type' => 'text', 'label' => 'Email')); $fields = array_merge($default_fields, $fields); $user = Users::loggedInUser(); if (!$user) { throw new Users_Exception_NotLoggedIn(); } $email = null; $missing_fields = Users::accountStatus($email); if (isset($user->first_name)) { $fields['first_name']['value'] = $user->first_name; } if (isset($user->last_name)) { $fields['last_name']['value'] = $user->last_name; } if (isset($user->email_address)) { $fields['email_address']['value'] = $user->email_address; } else { if ($email) { $link = Pie_Html::a('#resend', array('class' => 'users_contact_tool_resend'), "You can re-send the activation email"); switch ($email->state) { case 'active': if ($email->user_id == $user->id) { $message = "Please confirm this email address.<br>{$link}"; } else { $message = "This email seems to belong to another user"; } break; case 'suspended': $message = "This address has been suspended."; break; case 'unsubscribed': $message = "The owner of this address has unsubscribed"; break; case 'unverified': default: $message = "Not verified yet.<br>{$link}"; break; } $fields['email_address']['value'] = $email->address; $fields['email_address']['message'] = $message; } } $on_success = isset($_REQUEST['_pie']['onSuccess']) ? $_REQUEST['_pie']['onSuccess'] : Pie_Request::url(); Pie_Response::addScript('plugins/users/js/Users.js'); $form = $static = compact('fields'); return Pie::tool('pie/panel', compact('uri', 'on_success', 'form', 'static', 'title', 'collapsed', 'toggle', 'complete', 'editing', 'inProcess', '_form_static')); }
/** * 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')); } }
function users_account_tool($params) { $defaults = array('uri' => 'users/account', 'omit' => array(), 'fields' => array(), 'title' => "Basic Info", 'editing' => true, 'complete' => true, 'inProcess' => false, 'collapsed' => false, 'toggle' => false); extract(array_merge($defaults, $params)); $default_fields = array('username' => array('type' => 'text', 'label' => 'Choose Username'), 'gender' => array('type' => 'select', 'label' => 'I am', 'options' => array('male' => 'a man', 'female' => 'a woman')), 'orientation' => array('type' => 'select', 'label' => 'Orientation', 'options' => array('straight' => 'straight', 'gay' => 'gay', 'bi' => 'bi')), 'relationship_status' => array('type' => 'select', 'label' => 'Status', 'options' => array('single' => "I'm single", 'open' => "I'm in an open relationship", 'relationship' => "I'm in a relationship", 'engaged' => "I'm engaged", 'married' => "I'm married", 'complicated' => "It's complicated", 'widowed' => "I'm widowed")), 'birthday' => array('type' => 'date', 'label' => 'My Birthday', 'options' => array('year_from' => '1920', 'year_to' => date('Y') - 16)), 'zipcode' => array('type' => 'text', 'label' => 'Zipcode', 'attributes' => array('maxlength' => 5))); $fields = array_merge($default_fields, $fields); $user = Users::loggedInUser(); if (!$user) { throw new Users_Exception_NotLoggedIn(); } if (isset($user->gender)) { $fields['gender']['value'] = $user->gender; } if (isset($user->desired_gender)) { if ($user->desired_gender == 'either') { $fields['orientation']['value'] = 'bi'; } else { if (isset($user->gender)) { $fields['orientation']['value'] = $user->gender != $user->desired_gender ? 'straight' : 'gay'; } } } if (isset($user->relationship_status)) { $fields['relationship_status']['value'] = $user->relationship_status; } if (isset($user->birthday)) { $fields['birthday']['value'] = date("Y-m-d", Users::db()->fromDate($user->birthday)); } if (isset($user->zipcode)) { $fields['zipcode']['value'] = $user->zipcode; } if (isset($user->username)) { $fields['username']['value'] = $user->username; } foreach ($omit as $v) { unset($fields[$v]); } $on_success = isset($_REQUEST['_pie']['onSuccess']) ? $_REQUEST['_pie']['onSuccess'] : Pie_Request::url(); Pie_Response::addScript('plugins/users/js/Users.js'); $form = $static = compact('fields'); return Pie::tool('pie/panel', compact('uri', 'title', 'form', 'static', 'on_success', 'complete', 'collapsed', 'toggle', 'editing', 'inProcess', '_form_static')); }
function pie_exception($params) { extract($params); /** * @var Exception $exception */ $message = $exception->getMessage(); $file = $exception->getFile(); $line = $exception->getLine(); if ($is_ajax = Pie_Request::isAjax()) { // Render a JSON layout for ajax switch (strtolower($is_ajax)) { case 'json': default: $json = json_encode(array('errors' => Pie_Exception::toArray(array($exception)))); $callback = Pie_Request::callback(); echo "{$callback}({$json})"; } } else { if (is_callable(array($exception, 'getTraceAsStringEx'))) { $trace_string = $exception->getTraceAsStringEx(); } else { $trace_string = $exception->getTraceAsString(); } if (Pie::textMode()) { $result = "{$message}\n" . "in {$file} ({$line})\n" . $trace_string; } else { if ($exception instanceof Pie_Exception_PhpError or !empty($exception->messageIsHtml)) { // do not sanitize $message } else { $message = Pie_Html::text($message); } $result = "<h1>{$message}</h1>" . "<h3>in {$file} ({$line})</h3>" . "<pre>" . $trace_string . "</pre>"; } echo $result; } $app = Pie_Config::get('pie', 'app', null); Pie::log("{$app}: Exception in " . ceil(Pie::microtime()) . "ms\n"); Pie::log("{$message}\n in {$file} ({$line})"); }
function pie_addScriptLines() { $app = Pie_Config::expect('pie', 'app'); $uri = Pie_Dispatcher::uri(); $proxies_json = json_encode(Pie_Config::get('pie', 'proxies', array())); $uri_json = json_encode($uri->toArray()); $url = Pie_Request::url(); $url_json = json_encode($url); $proxy_url_json = json_encode(Pie_Uri::url($url)); $base_url = json_encode(Pie_Request::baseUrl()); Pie_Response::addScriptLine(<<<EOT // pie {{ \t\tPie.info = { \t\t\t"proxies": {$proxies_json}, \t\t\t"uri": {$uri_json}, \t\t\t"url": {$url_json}, \t\t\t"proxyUrl": {$proxy_url_json}, \t\t\t"baseUrl": {$base_url} \t\t}; EOT ); $uris = Pie_Config::get('pie', 'javascript', 'uris', array()); $urls = array(); foreach ($uris as $u) { $urls["{$u}"] = Pie_Uri::url("{$u}"); } $urls_json = json_encode($urls); Pie_Response::addScriptLine("\t\tPie.urls = {$urls_json};"); // Export more variables to inline js $app = Pie_Config::expect('pie', 'app'); $app_json = json_encode($app); Pie_Response::addScriptLine("\t\tPie.app = {$app_json};\n" . "// }} pie"); $snf = Pie_Config::get('pie', 'session', 'nonceField', 'nonce'); $nonce = isset($_SESSION[$snf]) ? $_SESSION[$snf] : null; if ($nonce) { $nonce_json = json_encode($nonce); Pie_Response::addScriptLine("\t\tPie.nonce = {$nonce_json};"); } }
/** * Default pie/response handler. * 1. Gets some slots, depending on what was requested. * 2. Renders them in a layout * The layout expects "title", "dashboard" and "contents" slots to be filled. */ function pie_response($params) { extract($params); /** * @var Exception $exception * @var array $errors */ // Redirect to success page, if requested. $is_ajax = Pie_Request::isAjax(); if (empty($errors) and empty($exception)) { if (!$is_ajax and isset($_REQUEST['_pie']['onSuccess'])) { $on_success = $_REQUEST['_pie']['onSuccess']; if (Pie_Config::get('pie', 'response', 'onSuccessShowFrom', true)) { $on_success = Pie_Uri::url($on_success . '?_pie[fromSuccess]=' . Pie_Dispatcher::uri()); } Pie_Response::redirect($on_success); return; } } // Get the requested module $uri = Pie_Dispatcher::uri(); if (!isset($module)) { $module = $uri->module; if (!isset($module)) { $module = 'pie'; Pie_Dispatcher::uri()->module = 'pie'; } } // Get the main module (the app) $app = Pie_Config::expect('pie', 'app'); // Add some javascript to inform the front end of important URLs Pie::event('pie/addScriptLines'); // What to do if this is an AJAX request if ($is_ajax) { $slot_names = Pie_Request::slotNames(); if (!isset($slot_names)) { $slot_names = Pie_Config::get($module, 'response', 'slotNames', array('content' => null, 'dashboard' => null, 'title' => null, 'notices' => null)); } $slots = array(); $stylesheets = array(); $stylesInline = array(); $scripts = array(); $scriptLines = array(); if (is_array($slot_names)) { foreach ($slot_names as $slot_name => $v) { $slots[$slot_name] = Pie_Response::fillSlot($slot_name, 'default'); $stylesheets[$slot_name] = Pie_Response::stylesheetsArray($slot_name); $stylesInline[$slot_name] = Pie_Response::stylesInline($slot_name); $scripts[$slot_name] = Pie_Response::scriptsArray($slot_name); $scriptLines[$slot_name] = Pie_Response::scriptLines($slot_name); } } $timestamp = microtime(true); $echo = Pie_Request::contentToEcho(); // Render a JSON layout for ajax $to_encode = compact('slots', 'stylesheets', 'stylesInline', 'scripts', 'scriptLines', 'timestamp', 'echo'); // Cut down on the response size foreach (array('slots', 'stylesheets', 'stylesInline', 'scripts', 'scriptLines') as $f) { $is_empty = true; if (is_array($to_encode[$f])) { foreach ($to_encode[$f] as $k => $v) { if (isset($v)) { $is_empty = false; } else { unset($to_encode[$f][$k]); } } } else { if (!empty($to_encode[$f])) { $is_empty = false; } } if ($is_empty) { unset($to_encode[$f]); } } switch (strtolower($is_ajax)) { case 'json': default: $json = json_encode($to_encode); $callback = Pie_Request::callback(); echo $callback ? "{$callback}({$json})" : $json; } return; } // If this is a request for a regular webpage, // fill the usual slots and render a layout. // Attach stylesheets and scripts if (Pie_Request::accepts('text/fbml')) { Pie_Response::addStylesheet("css/fbml.css"); Pie_Response::addScript('plugins/pie/fbjs/Pie.fb.js'); } else { Pie_Response::addStylesheet("css/html.css"); Pie_Response::addScript('plugins/pie/js/Pie.js'); } // Get all the usual slots for a webpage $slot_names = Pie_Config::get($module, 'response', 'slotNames', array('content' => null, 'dashboard' => null, 'title' => null, 'notices' => null)); $slots = array(); foreach ($slot_names as $sn => $v) { $slots[$sn] = Pie_Response::fillSlot($sn, 'default'); } $output = Pie_Response::output(); if (isset($output)) { if ($output === true) { return; } if (is_string($output)) { echo $output; } return; } if (Pie_Request::accepts('text/fbml')) { // Render a full FBML layout $layout_view = Pie_Config::get($app, 'response', 'layout_fbml', "{$app}/layout/fbml.php"); echo Pie::view($layout_view, $slots); } else { // Render a full HTML layout $layout_view = Pie_Config::get($app, 'response', 'layout_html', "{$app}/layout/html.php"); echo Pie::view($layout_view, $slots); } }
/** * Default pie/noModule handler. * Just displays pie/notFound.php view. */ function pie_noModule($params) { header("HTTP/1.0 404 Not Found"); $url = Pie_Request::url(); echo Pie::view('pie/notFound.php', compact('url')); }
<?php echo Pie_Html::select('albums', array('id' => 'albums')); ?> <?php echo Pie_Html::options($albums); ?> </select> <?php echo Pie_Html::div('photos', 'items_addPhoto_tool_photos'); ?> <?php echo Pie::view('items/tool/addPhotoList.php', compact('photos')); ?> </div> <?php if (Pie_Request::accepts('text/fbml')) { ?> <fb:js-string var="fbml.<?php echo Pie_Html::getIdPrefix(); ?> throbber"> <div class="items_addPhoto_tool_throbber"> <img src="<?php echo $throbber_url; ?> " alt="loading..." /> </div> </fb:js-string> <?php } else { ?>
function myApp_notFound_response_content($params) { header("HTTP/1.0 404 Not Found"); $url = Pie_Request::url(); return Pie::view("myApp/content/notFound.php", compact('url')); }
/** * The current theme url applied to all "src" attributes (except for iframes) * rendered by Pie_Html. * @return string|null * The theme url that is currently at the end of the cascade, i.e. was pushed last. */ static function themeUrl() { return Pie_Config::get('pie', 'theme_url', Pie_Request::baseUrl()); }
static function uri() { if (!isset(self::$uri)) { self::$uri = Pie_Uri::from(self::url()); } return self::$uri; }
/** * Returns what the local filename of a local URL would typically be without any routing. * If not found under docroot, also checks various aliases. * * @param string $url * The url to translate, whether local or an absolute url beginning with the base URL * @return string * The complete filename of the file or directory. * It may not point to an actual file or directory, so use file_exists() or realpath();s */ static function filenamefromUrl($url) { if (Pie_Valid::url($url)) { // This is an absolute URL. Get only the part after the base URL // Run it through proxies first $url = self::proxyDestination($url); $local_url = Pie_Request::tail($url); } else { $local_url = $url; } $parts = explode('?', $local_url); $local_url = $parts[0]; if ($local_url == '' || $local_url[0] != '/') { $local_url = '/' . $local_url; } // Try various aliases first $aliases = Pie_Config::get('pie', 'aliases', array()); foreach ($aliases as $alias => $path) { $alias_len = strlen($alias); if (substr($local_url, 0, $alias_len) == $alias) { return $path . substr($local_url, $alias_len); } } // Otherwise, we should use the document root. $docroot_dir = self::documentRoot(); return $docroot_dir . $local_url; }
function pie_file($params) { $filename = Pie_Request::filename(); // TODO: serve the file, unless it's the front controller itself. }
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title><?php echo $title; ?> </title> <link rel="shortcut icon" href="<?php echo Pie_Request::baseUrl(); ?> /favicon.ico" type="image/x-icon"> <?php echo Pie_Response::stylesheets("\n\t"); ?> <?php echo Pie_Response::scripts("\n\t"); ?> <style type="text/css"> <?php echo Pie_Response::stylesInline(); ?> </style> <!--[if lt IE 7]> <?php // echo Pie_Html::tag('script', array('src' => 'js/unitpngfix.js'))
/** * Gets all the requested slots * (uses Pie_Request::slots()) */ static function getRequestedSlots() { $return = array(); $slot_names = Pie_Request::slotNames(); foreach ($slot_names as $sn) { $sn_parts = explode('.', $sn); $slot_name = end($sn_parts); $return[$slot_name] = self::fillSlot($slot_name); } return $return; }
/** * 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()')); }