Example #1
0
 /**
  * Send one header.
  *
  * @return bool: Headers sent
  */
 public static function sendHeaders()
 {
     // headers already sent
     if (headers_sent($sFilename, $iLinenum)) {
         Days_Log::add("Headers not sent such as sended in file '{$sFilename}' on line {$iLinenum}");
         return false;
     }
     // on Ajax query
     if (Days_Request::isAjax()) {
         // JSON encoded data
         Header('Content-Type: text/javascript; charset=UTF-8');
         Header('Content-type: application/json; charset=UTF-8');
         self::_sendHeadersNocache();
         return true;
     }
     // send special headers only
     foreach (self::$_aHeaders as $sType => $sValue) {
         switch ((string) $sType) {
             case self::NOT_FOUND:
                 Header('HTTP/1.0 404 Not Found');
                 return true;
                 break;
             case '403':
                 Header('HTTP/1.1 403 Forbidden');
                 return true;
                 break;
             case self::PREV_PAGE:
                 // redirect on previous page
                 self::_sendHeadersRedirect(Days_Url::prev());
                 return true;
                 break;
             case self::REDIRECT:
             case self::RELOAD:
                 self::_sendHeadersRedirect($sValue);
                 return true;
                 break;
         }
     }
     // send all additional headers
     foreach (self::$_aHeaders as $sType => $sValue) {
         // send header info
         switch ((string) $sType) {
             case 'htm':
             case 'html':
             case 'xhtml':
                 Header('Content-Type: text/html; charset=UTF-8');
                 self::_sendHeadersNocache();
                 break;
             case 'xml':
                 Header("Content-Type: text/xml; charset=UTF-8");
                 self::_sendHeadersNocache();
                 break;
             case 'wml':
                 Header('Content-Type: text/vnd.wap.wml; charset=UTF-8');
                 self::_sendHeadersNocache();
                 break;
             case 'json':
                 Header('Content-Type: text/javascript; charset=UTF-8');
                 self::_sendHeadersNocache();
                 break;
             case 'js':
                 Header('Content-type: application:x-javascript; charset=UTF-8');
                 self::_sendHeadersNocache();
                 break;
             case 'jpg':
             case 'jpeg':
                 Header("Content-Type: image/jpeg");
                 self::_sendHeadersNocache();
                 break;
             case 'gif':
                 Header("Content-Type: image/gif");
                 self::_sendHeadersNocache();
                 break;
             case 'png':
                 Header("Content-Type: image/png");
                 self::_sendHeadersNocache();
                 break;
         }
     }
     // all headers sent
     return true;
 }
Example #2
0
 /**
  * Run application.
  *
  * @param string $appPath Path to application
  * @param string $mode Name of configuration file used in work
  */
 private function __construct($appPath, $mode)
 {
     // set pathes
     self::$_libPath = realpath(dirname(__FILE__) . '/..') . '/';
     self::$_appPath = realpath($appPath) . '/';
     self::$_publicPath = getcwd() . '/';
     set_include_path(get_include_path() . PATH_SEPARATOR . self::$_libPath);
     spl_autoload_register(array(__CLASS__, 'autoload'));
     Days_Session::init();
     // set config main file
     if (!empty($mode)) {
         Days_Config::setDefaultConfig($mode);
     }
     // set path for config
     Days_Config::setConfigPath(self::$_appPath . 'config/');
     // set debug mode
     self::$_isDebug = (bool) Days_Config::load()->get('engine/debug', false);
     // set error level and handler
     $iErrorLevel = self::isDebug() ? E_ALL | E_STRICT : E_ALL ^ E_NOTICE;
     error_reporting($iErrorLevel);
     setlocale(LC_ALL, 'ru_RU.UTF-8', 'RUS', 'RU');
     //set timezone
     if ($timezone = Days_Config::load()->get('engine/timezone', false)) {
         date_default_timezone_set($timezone);
     } else {
         date_default_timezone_set('Europe/Helsinki');
     }
     // not send execution errors to user
     ob_start();
     try {
         if (Days_Config::load()->get('engine/autorun', 1)) {
             $autorunClass = "Controller_System_Autorun";
             // run predefined class
             if (class_exists($autorunClass) and is_callable(array($autorunClass, 'run'))) {
                 call_user_func(array($autorunClass, 'run'));
             }
         }
         Days_Event::run('engine.start');
         // get url info
         $controller = Days_Url::getSpec('controller');
         $action = Days_Url::getSpec('action');
         $ext = Days_Url::getSpec('ext');
         Days_Event::run('controller.start');
         // set module path
         Days_Model::setPath(self::appPath() . 'Model/');
         // set controller params
         $controllerClass = "Controller_" . ucfirst($controller);
         // use index controller for non-exists controllers
         if (!class_exists($controllerClass) and Days_Config::load()->get('url/virtual')) {
             $controllerClass = "Controller_Index";
             $controller = 'index';
         }
         // set action name
         $actionMethod = Days_Request::isAjax() ? "{$action}AjaxAction" : "{$action}Action";
         // set template path
         $template = "content/{$controller}/{$action}.{$ext}";
         // create controller
         if (!class_exists($controllerClass)) {
             throw new Days_Exception("Controller '{$controllerClass}' not found");
         }
         $controllerObj = new $controllerClass($template);
         if (!$controllerObj instanceof Days_Controller) {
             throw new Days_Exception("Controller '{$controllerClass}' should be extended from 'Days_Controller'");
         }
         // call init() method for prepare object
         $controllerObj->init();
         Days_Event::run('controller.post.init');
         // execute PostAction before call specified action
         if (Days_Request::isPost()) {
             $actionPost = "{$action}PostAction";
             if (method_exists($controllerObj, $actionPost)) {
                 call_user_func(array($controllerObj, $actionPost));
             }
         }
         // call specified action
         if (!method_exists($controllerObj, $actionMethod)) {
             throw new Days_Exception("Action {$actionMethod} in controller {$controllerClass} not defined");
         }
         $actionResult = call_user_func(array($controllerObj, $actionMethod));
         // ajax query
         if (Days_Request::isAjax()) {
             if (is_null($actionResult)) {
                 $actionResult = array();
             }
             $content = $actionResult;
         } else {
             $controllerObj->setLayout($controller, false);
             $content = call_user_func(array($controllerObj, 'getContent'));
             Days_Response::addHeader($ext);
         }
         Days_Event::run('controller.end');
         // set data to response
         Days_Response::addContent($content);
     } catch (Exception $oEx) {
         // save error message about this query
         Days_Log::add($oEx->getMessage());
         // page not found
         Days_Response::addHeader(Days_Response::NOT_FOUND);
     }
     // save runtime errors
     for ($iObLevel = ob_get_level(); $iObLevel > 0; $iObLevel--) {
         $sError = ob_get_contents();
         if ('' != $sError) {
             Days_Log::add("This data printed in scripts: '{$sError}'");
         }
         // close output handler
         ob_end_clean();
     }
     // save errors
     Days_Log::save();
     Days_Event::run('engine.end');
     // send headers to user
     Days_Event::run('response.send.headers');
     Days_Response::sendHeaders();
     // send content to user
     Days_Event::run('response.send.content');
     Days_Response::sendContent();
 }