| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Bluz PHP Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @link      https://github.com/bluzphp/skeleton | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Application\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Bluz\Application\Application; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Bluz\Auth\AuthException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Bluz\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Bluz\Http\Exception\ForbiddenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Bluz\Http\Exception\RedirectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Bluz\Proxy\Auth as AuthProxy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Bluz\Proxy\Layout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Bluz\Proxy\Logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Bluz\Proxy\Messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Bluz\Proxy\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Bluz\Proxy\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Bluz\Proxy\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Bluz\Proxy\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Bootstrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @category Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @package  Bootstrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * @author   Anton Shevchuk | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * @created  20.07.11 17:38 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | class Bootstrap extends Application | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param Controller $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws \Application\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @throws \Bluz\Auth\AuthException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function preDispatch($controller): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         // example of setup default title | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         Layout::title('Bluz Skeleton'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (!AuthProxy::getIdentity() && $controller->getModule() !== Router::getErrorModule()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if ($token = Request::getCookie('Auth-Token')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 // try to login by token from cookies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     Auth\Provider\Cookie::authenticate($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 } catch (AuthException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                     $this->getResponse()->setCookie('Auth-Token', '', 1, '/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             } elseif ($token = Request::getHeader('Auth-Token')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 // try to login by token from headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 Auth\Provider\Token::authenticate($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         parent::preDispatch($controller); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Denied access | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param ForbiddenException $exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return \Bluz\Controller\Controller|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function forbidden(ForbiddenException $exception): ?Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         // for AJAX and API calls (over JSON) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $jsonOrApi = Request::isXmlHttpRequest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             || (Request::checkAccept([Request::TYPE_HTML, Request::TYPE_JSON]) === Request::TYPE_JSON); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // for guest, for requests | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (!$jsonOrApi && !AuthProxy::getIdentity()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             // save URL to session and redirect make sense if presentation is null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             Session::set('rollback', Request::getUri()->__toString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // add error notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             Messages::addError('You don\'t have permissions, please sign in'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             // redirect to Sign In page | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             $redirect = new RedirectException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $redirect->setUrl(Router::getUrl('users', 'signin')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return $this->redirect($redirect); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $this->error($exception); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Render with debug headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function render(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         Logger::info('app:render'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         Logger::info('app:files:' . count(get_included_files())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if ($this->isDebug()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             if (!headers_sent()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->sendInfoHeaders(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             if (ob_get_level() > 0 && ob_get_length() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 Logger::error('Output has been sent previously'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         parent::render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Finish it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function end(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if ($errors = Logger::get('error')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $this->sendErrors($errors); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * Send information headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     protected function sendInfoHeaders(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $debugString = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             '%fsec; %skb', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             ceil(memory_get_usage() / 1024) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $debugString .= '; ' . Request::getModule() . '/' . Request::getController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         Response::setHeader('Bluz-Debug', $debugString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($info = Logger::get('info')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             Response::setHeader('Bluz-Bar', json_encode($info)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             Response::setHeader('Bluz-Bar', '{"!":"Logger is disabled"}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * sendErrorBody | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @param  array $errors | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 158 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 159 |  |  |     protected function sendErrors($errors): void | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |         foreach ($errors as $message) { | 
            
                                                        
            
                                    
            
            
                | 162 |  |  |             errorLog(new \ErrorException($message, 0, E_USER_ERROR)); | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |