public function display()
 {
     if ($this->hasErrors()) {
         $this->tpl->setVar('errors', $this->getErrors());
     }
     if ($this->tpl->post === null) {
         $this->tpl->setVar('post', Http::getPost(), false, true);
     }
     if ($this->tpl->query === null) {
         $this->tpl->setVar('query', Http::getQuery(), false, true);
     }
     if ($this->tpl->cookie === null) {
         $this->tpl->setVar('cookie', Http::getCookie(), false, true);
     }
     $this->tpl->setVar('notifyInformation', $this->session->get('notifyInformation'), false, true);
     $this->tpl->setVar('notifyError', $this->session->get('notifyError'), false, true);
     $this->tpl->setVar('notifySuccess', $this->session->get('notifySuccess'), false, true);
     if ($this->_isAjax) {
         if ($this->hasErrors()) {
             $this->addAjaxDatas('errors', $this->getErrors());
         }
         if ($this->_ajaxAutoAddDatas['post'] && !array_key_exists('post', $this->_ajaxDatas)) {
             $this->addAjaxDatas('post', Http::getPost());
         }
         if ($this->_ajaxAutoAddDatas['query'] && !array_key_exists('query', $this->_ajaxDatas)) {
             $this->addAjaxDatas('query', Http::getQuery());
         }
         if ($this->_ajaxAutoAddDatas['cookie'] && !array_key_exists('cookie', $this->_ajaxDatas)) {
             $this->addAjaxDatas('cookie', Http::getCookie());
         }
         if ($this->_ajaxAutoAddDatas['content'] && !array_key_exists('content', $this->_ajaxDatas)) {
             $this->addAjaxDatas('content', $this->tpl->getContent());
         }
         if (!array_key_exists('notifyInformation', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifyInformation', $this->session->get('notifyInformation'));
         }
         if (!array_key_exists('notifyError', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifyError', $this->session->get('notifyError'));
         }
         if (!array_key_exists('notifySuccess', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifySuccess', $this->session->get('notifySuccess'));
         }
         // No cache
         if (!$this->_ajaxDatasCache) {
             Header::sentHeader('Cache-Control', 'no-cache, must-revalidate');
             Header::sentHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
         }
         switch ($this->_ajaxDatasType) {
             case self::HTML:
                 Header::sentHeader('Content-type', 'text/html');
                 foreach ($this->_ajaxDatas as $data) {
                     echo $data;
                 }
                 break;
             case self::XML:
                 Header::sentHeader('Content-type', 'text/xml');
                 foreach ($this->_ajaxDatas as $data) {
                     echo $data;
                 }
                 break;
             case self::JSON:
                 Header::sentHeader('Content-type', 'application/json');
                 echo json_encode((object) $this->_ajaxDatas, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
                 break;
             default:
                 throw new \Exception('Ajax datas type not valid');
         }
     } else {
         //display
         $this->tpl->display();
         $this->log->debug('Display template file : "' . $this->tpl->getFile() . '"', 'router');
     }
     // Delete stored messages
     $this->session->delete('notifyInformation', true);
     $this->session->delete('notifyError', true);
     $this->session->delete('notifySuccess', true);
 }
示例#2
0
 protected function _runController($controller, $methods = array(), $vars = array(), $requireSsl = false, $requireAjax = false, $autoSetAjax = true, $requireHttpMethod = null, $httpResponseStatusCode = null, $httpProtocol = null)
 {
     $controllerExplode = explode($this->getNamespaceSeparator(), (string) $controller);
     if (is_array($controllerExplode) && count($controllerExplode) > 1) {
         $controllerName = $this->getNamespaceSeparator() . ucfirst(array_pop($controllerExplode));
         $controller = implode($this->getNamespaceSeparator(), $controllerExplode) . $controllerName;
     } else {
         $controller = (string) ucfirst($controller);
     }
     Logger::getInstance()->debug('Run controller : "' . $controller . '"', 'router');
     $controllerClass = $this->getControllersNamespace(true) . $controller;
     // Check if controller exists (with controllers namespace)
     if (!class_exists($controllerClass)) {
         throw new \Exception('Controller "' . $controllerClass . '" not found');
     }
     $controller = $controllerClass;
     if (!is_array($vars)) {
         throw new \Exception('Controller : "' . $controller . '" vars must be an array');
     }
     if (!is_array($methods)) {
         throw new \Exception('Controller : "' . $controller . '" methodes must be an array');
     }
     $inst = new \ReflectionClass($controller);
     if ($inst->isInterface() || $inst->isAbstract()) {
         throw new \Exception('Controller "' . $controller . '" cannot be an interface of an abstract class');
     }
     $ctrl = $inst->newInstance();
     if ($ctrl->getAutoCallDisplay()) {
         if (!$inst->hasMethod('display')) {
             throw new \Exception('Controller "' . $controller . '" must be implement method "Diplay');
         }
         if (!$inst->hasMethod('initTemplate')) {
             throw new \Exception('Controller "' . $controller . '" must be implement method "initTemplate');
         }
     }
     if (!Cli::isCli()) {
         if (!Http::isHttps() && $requireSsl) {
             Logger::getInstance()->debug('Controller "' . $controller . '" need ssl http request', 'router');
             $this->show400(true);
         }
         if (!is_null($requireHttpMethod)) {
             if ($requireHttpMethod != Http::getMethod()) {
                 Logger::getInstance()->debug('Controller "' . $controller . '" invalid http method');
                 $this->show405(true);
             }
         }
         if (!Http::isAjax() && $requireAjax) {
             Logger::getInstance()->debug('Controller "' . $controller . '" need ajax http request');
             $this->show400(true);
         }
         if (Http::isAjax() && $autoSetAjax) {
             $ctrl->setAjaxController();
         }
         if (!is_null($httpResponseStatusCode) || !is_null($httpProtocol)) {
             Header::setResponseStatusCode(is_null($httpResponseStatusCode) ? 200 : $httpResponseStatusCode, true, true, $httpProtocol);
         }
     }
     if ($methods) {
         foreach ($methods as $methodName => $methodParams) {
             Logger::getInstance()->debug('Call method : "' . $methodName . '"', 'router');
             if (!method_exists($ctrl, $methodName) || !$inst->getMethod($methodName)->isPublic()) {
                 throw new \Exception('Method "' . $methodName . '" don\'t exists or isn\'t public on controller "' . $controller . '"');
             }
             $args = array();
             if (!is_array($methodParams)) {
                 $args[] = $methodParams;
             } else {
                 foreach ($methodParams as $parameter) {
                     //check if is [['key']] type, or direct value
                     if (stripos($parameter, '[[') === false) {
                         $args[] = $parameter;
                     } else {
                         if (count($vars) > 0) {
                             $key = (int) str_replace(array('[', ']'), '', $parameter);
                             if (array_key_exists($key, $vars)) {
                                 $args[] = $vars[$key];
                             }
                         } else {
                             $args[] = $parameter;
                         }
                     }
                 }
             }
             foreach ($args as $arg) {
                 Logger::getInstance()->debug('Add argument : "' . $arg . '"', 'router');
             }
             // Call method with $args
             \call_user_func_array(array($ctrl, $methodName), $args);
         }
     }
     $this->_controller = $ctrl;
     //call display only when have a template
     if ($ctrl->getAutoCallDisplay() && Template::getTemplate()) {
         Logger::getInstance()->debug('Call method "display"', 'router');
         $ctrl->display();
     }
 }
示例#3
0
 protected function _display($captchaType)
 {
     $this->create(Session::getInstance()->get($this->getFormName() . 'Captcha'), $captchaType);
     Header::sentHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
     Header::sentHeader('Cache-Control', 'post-check=0, pre-check=0', false);
     Header::sentHeader('Pragma', 'no-cache');
     Header::sentHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
     if ($captchaType == 'image') {
         Header::sentHeader('Content-Type', 'image/' . $this->_imageFormat);
         // display captcha
         switch ($this->_imageFormat) {
             case 'png':
                 imagepng($this->_imageContents);
                 break;
             case 'jpg':
                 imagejpeg($this->_imageContents);
                 break;
             case 'gif':
                 imagegif($this->_imageContents);
                 break;
         }
     } elseif ($captchaType == 'audio') {
         Header::sentHeader('Content-Type', 'audio/x-wav');
         Header::sentHeader('Content-Length', (string) strlen($this->_audioContents));
         // display captcha
         echo $this->_audioContents;
     }
 }