| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace fkooman\RemoteStorage\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use fkooman\RemoteStorage\ApiModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use fkooman\RemoteStorage\Http\Exception\HttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use fkooman\RemoteStorage\OAuth\BearerAuthentication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use fkooman\RemoteStorage\OAuth\OAuthModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use fkooman\RemoteStorage\OAuth\TokenStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use fkooman\RemoteStorage\RandomInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use fkooman\RemoteStorage\RemoteStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use fkooman\RemoteStorage\TplInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use fkooman\RemoteStorage\UiModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use fkooman\RemoteStorage\WebfingerModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $tpl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $formAuth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $bearerAuth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $oauthModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $apiModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $uiModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $webfingerModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function __construct(TplInterface $tpl, SessionInterface $session, TokenStorage $tokenStorage, RandomInterface $random, RemoteStorage $remoteStorage, array $userPass) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->tpl = $tpl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $serverMode = 'development'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->formAuth = new FormAuthentication($session, $tpl, $userPass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->bearerAuth = new BearerAuthentication($tokenStorage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->oauthModule = new OAuthModule($tpl, $random, $tokenStorage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->apiModule = new ApiModule($remoteStorage, $serverMode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->uiModule = new UiModule($remoteStorage, $tpl, $tokenStorage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->webfingerModule = new WebfingerModule($serverMode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function run(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             switch ($request->getRequestMethod()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 case 'GET': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     return $this->handleGet($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 case 'POST': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     return $this->handlePost($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 case 'PUT': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $tokenInfo = $this->bearerAuth->requireAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     return $this->apiModule->put($request, $tokenInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 case 'DELETE': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                     $tokenInfo = $this->bearerAuth->requireAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     return $this->apiModule->delete($request, $tokenInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 case 'OPTIONS': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     return $this->apiModule->options($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 case 'HEAD': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     $tokenInfo = $this->bearerAuth->optionalAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     return $this->apiModule->head($request, $tokenInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     throw new HttpException('method not allowed', 405); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |            } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } catch (HttpException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             if ($request->isBrowser()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 $response = new Response($e->getCode(), 'text/html'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $response->setBody( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     $this->tpl->render( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         'errorPage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             'code' => $e->getCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             'message' => $e->getMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                         ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 // not a browser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $response = new Response($e->getCode(), 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $response->setBody(json_encode(['error' => $e->getMessage()])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             foreach ($e->getResponseHeaders() as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 $response->addHeader($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     private function handleGet(Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         switch ($request->getPathInfo()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             case '/.well-known/webfinger': | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                 return $this->webfingerModule->getWebfinger($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             case '/_oauth/authorize': | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 $userId = $this->formAuth->requireAuth($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                 if ($userId instanceof Response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                     return $userId; | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 return $this->oauthModule->getAuthorize($request, $userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             case '/': | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 $userId = $this->formAuth->optionalAuth($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 return $this->uiModule->getRootPage($request, $userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             case '/account': | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 $userId = $this->formAuth->requireAuth($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 if ($userId instanceof Response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                     return $userId; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 return $this->uiModule->getAccountPage($request, $userId); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 $tokenInfo = $this->bearerAuth->optionalAuth($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 return $this->apiModule->get($request, $tokenInfo); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     private function handlePost(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         switch ($request->getPathInfo()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             case '/account': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $userId = $this->formAuth->requireAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 return $this->uiModule->postAccountPage($request, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             case '/_oauth/authorize': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 $userId = $this->formAuth->requireAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 return $this->oauthModule->postAuthorize($request, $userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             case '/_auth/form/verify': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 return $this->formAuth->verifyAuth($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             case '/_auth/form/logout': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 return $this->formAuth->logout($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 throw new HttpException('not found', 404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.