/** * New messenger method: * Uses an expandable array of options and supports direct template rendering and subject prepending. * * @param array $message_options An array of message options. * @return bool|void */ public static function send($message_options) { $di = \Phalcon\Di::getDefault(); $config = $di->get('config'); $default_options = array('reply_to' => NULL, 'delivery_date' => NULL, 'options' => NULL); $options = array_merge($default_options, $message_options); // Render the template as the message if a template is specified. if (isset($options['template'])) { $previous_sp_setting = $di['url']->getSchemePrefixSetting(); $di['url']->forceSchemePrefix(TRUE); $view = \FA\Phalcon\View::getView(array('views_dir' => 'messages', 'layouts_dir' => '../templates', 'layout' => 'message')); $view->subject = $options['subject']; $view->setVars((array) $options['vars']); $options['message'] = $view->getRender('', $options['template']); $di['url']->forceSchemePrefix($previous_sp_setting); } else { if (isset($options['body']) && !isset($options['message'])) { $options['message'] = $options['body']; unset($options['body']); } } // Append the system name as a prefix to the message. if (!isset($options['no_prefix']) || $options['no_prefix'] == FALSE) { $app_name = $config->application->name; $options['subject'] = $app_name . ': ' . $options['subject']; } return self::sendMail($options); }
public static function handle(\Exception $e, \Phalcon\DiInterface $di) { if ($e instanceof \FA\Exception\NotLoggedIn) { // Redirect to login page for not-logged-in users. $flash = $di['flash']; $flash->addMessage('<b>Error:</b> You must be logged in to access this page!', 'red'); // Set referrer for login redirection. $session = $di['session']; $session = $session->get('referrer_login'); $session->url = $di['url']->current(); // Redirect to login page. $login_url = $di['url']->get('account/login'); $response = $di->get('response'); $response->redirect($login_url, 302); $response->send(); return; } elseif ($e instanceof \FA\Exception\PermissionDenied) { // Bounce back to homepage for permission-denied users. $di['flash']->addMessage('You do not have permission to access this portion of the site.', \FA\Flash::ERROR); $home_url = $di['url']->get(''); $response = $di['response']; $response->redirect($home_url, 302); $response->send(); return; } elseif ($e instanceof \Phalcon\Mvc\Dispatcher\Exception) { // Handle 404 page not found exception if ($di->has('view')) { $view = $di['view']; $view->disable(); } $view = \FA\Phalcon\View::getView(array()); $result = $view->getRender('error', 'pagenotfound'); $response = $di['response']; $response->setStatusCode(404, "Not Found"); $response->setContent($result); $response->send(); return; } elseif ($e instanceof \FA\Exception\Bootstrap) { // Bootstrapping error; cannot render template for error display. if (FA_APPLICATION_ENV != 'production') { self::renderPretty($e, $di); return; } else { $response = $di->get('response'); $response->setStatusCode(500, "Internal Server Error"); $exception_msg = "<b>Application core exception: </b>\n<blockquote>" . $e->getMessage() . "</blockquote>" . "\n" . "on line <b>" . $e->getLine() . "</b> of <i>" . $e->getFile() . "</i>"; $response->setContent($exception_msg); $response->send(); return; } } else { if ($di->has('view')) { $view = $di->get('view'); $view->disable(); } $show_debug = false; if ($di->has('acl')) { $acl = $di->get('acl'); if ($acl->isAllowed('administer all')) { $show_debug = true; } } if (FA_APPLICATION_ENV != 'production') { $show_debug = true; } if ($show_debug) { self::renderPretty($e, $di); return; } else { $view = \FA\Phalcon\View::getView(array()); $view->setVar('exception', $e); $result = $view->getRender('error', 'general'); $response = $di->get('response'); $response->setStatusCode(500, "Internal Server Error"); $response->setContent($result); $response->send(); return; } } }
/** * @param \Phalcon\DiInterface $di */ public function registerServices(\Phalcon\DiInterface $di = null) { // Set up MVC dispatcher. $controller_class = 'Modules\\' . $this->_module_class_name . '\\Controllers'; $di['dispatcher'] = function () use($controller_class) { $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) { // Set odd/even pairs as the key and value of parameters, respectively. $keyParams = array(); $params = $dispatcher->getParams(); foreach ($params as $position => $value) { if (is_int($position)) { if ($position & 1) { $keyParams[$params[$position - 1]] = urldecode($value); } } else { // The "name" parameter is internal to routes. if ($position != 'name') { $keyParams[$position] = urldecode($value); } } } $dispatcher->setParams($keyParams); // Detect filename in controller and convert to "format" parameter. $controller_name = $dispatcher->getControllerName(); if (strstr($controller_name, '.') !== false) { list($controller_clean, $format) = explode('.', $controller_name, 2); $dispatcher->setControllerName($controller_clean); $dispatcher->setParam('format', $format); } // Detect filename in action and convert to "format" parameter. $action_name = $dispatcher->getActionName(); if (strstr($action_name, '.') !== false) { list($action_clean, $format) = explode('.', $action_name, 2); $dispatcher->setActionName($action_clean); $dispatcher->setParam('format', $format); } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($eventsManager); $dispatcher->setDefaultNamespace($controller_class); return $dispatcher; }; // Set up module-specific configuration. $module_base_name = strtolower($this->_module_class_name); $module_config = $di->get('module_config'); $di->setShared('current_module_config', function () use($module_base_name, $module_config) { if (isset($module_config[$module_base_name])) { return $module_config[$module_base_name]; } else { return null; } }); // Set up the view component and shared templates. $views_dir = 'modules/' . $module_base_name . '/views/scripts/'; $di['view'] = function () use($views_dir) { return \FA\Phalcon\View::getView(array('views_dir' => $views_dir)); }; }