|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This driver serves TYPO3 instances (version 7.0 and up). It activates, if it |
| 5 | + * finds the characteristic typo3/ folder in the document root, serves both |
| 6 | + * frontend and backend scripts and prevents access to private resources. |
| 7 | + */ |
| 8 | +class Typo3ValetDriver extends ValetDriver |
| 9 | +{ |
| 10 | + /* |
| 11 | + |-------------------------------------------------------------------------- |
| 12 | + | Document Root Subdirectory |
| 13 | + |-------------------------------------------------------------------------- |
| 14 | + | |
| 15 | + | This subdirectory contains the public server resources, such as the |
| 16 | + | index.php, the typo3 and fileadmin system directories. Change it |
| 17 | + | to '', if you don't use a subdirectory but valet link directly. |
| 18 | + | |
| 19 | + */ |
| 20 | + protected $documentRoot = '/web'; |
| 21 | + |
| 22 | + /* |
| 23 | + |-------------------------------------------------------------------------- |
| 24 | + | Forbidden URI Patterns |
| 25 | + |-------------------------------------------------------------------------- |
| 26 | + | |
| 27 | + | All of these patterns won't be accessible from your web server. Instead, |
| 28 | + | the server will throw a 403 forbidden response, if you try to access |
| 29 | + | these files via the HTTP layer. Use regex syntax here and escape @. |
| 30 | + | |
| 31 | + */ |
| 32 | + protected $forbiddenUriPatterns = [ |
| 33 | + '_(recycler|temp)_/', |
| 34 | + '^/(typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(Resources/Private|Tests)/', |
| 35 | + '^/typo3/.+\.map$', |
| 36 | + '^/typo3temp/var/', |
| 37 | + '\.(htaccess|gitkeep|gitignore)$', |
| 38 | + ]; |
| 39 | + |
| 40 | + /** |
| 41 | + * Determine if the driver serves the request. For TYPO3, this is the |
| 42 | + * case, if a folder called "typo3" is present in the document root. |
| 43 | + * |
| 44 | + * @param string $sitePath |
| 45 | + * @param string $siteName |
| 46 | + * @param string $uri |
| 47 | + * @return bool |
| 48 | + */ |
| 49 | + public function serves($sitePath, $siteName, $uri) |
| 50 | + { |
| 51 | + $typo3Dir = $sitePath . $this->documentRoot . '/typo3'; |
| 52 | + return file_exists($typo3Dir) && is_dir($typo3Dir); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Determine if the incoming request is for a static file. That is, it is |
| 57 | + * no PHP script file and the URI points to a valid file (no folder) on |
| 58 | + * the disk. Access to those static files will be authorized. |
| 59 | + * |
| 60 | + * @param string $sitePath |
| 61 | + * @param string $siteName |
| 62 | + * @param string $uri |
| 63 | + * @return string|false |
| 64 | + */ |
| 65 | + public function isStaticFile($sitePath, $siteName, $uri) |
| 66 | + { |
| 67 | + // May the file contains a cache busting version string like filename.12345678.css |
| 68 | + // If that is the case, the file cannot be found on disk, so remove the version |
| 69 | + // identifier before retrying below. |
| 70 | + if (!$this->isActualFile($filePath = $sitePath . $this->documentRoot . $uri)) |
| 71 | + { |
| 72 | + $uri = preg_replace("@^(.+)\.(\d+)\.(js|css|png|jpg|gif|gzip)$@", "$1.$3", $uri); |
| 73 | + } |
| 74 | + |
| 75 | + // Now that any possible version string is cleared from the filename, the resulting |
| 76 | + // URI should be a valid file on disc. So assemble the absolut file name with the |
| 77 | + // same schema as above and if it exists, authorize access and return its path. |
| 78 | + if ($this->isActualFile($filePath = $sitePath . $this->documentRoot . $uri)) |
| 79 | + { |
| 80 | + return $this->isAccessAuthorized($uri) ? $filePath : false; |
| 81 | + } |
| 82 | + |
| 83 | + // This file cannot be found in the current project and thus cannot be served. |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determines if the given URI is blacklisted so that access is prevented. |
| 89 | + * |
| 90 | + * @param string $uri |
| 91 | + * @return boolean |
| 92 | + */ |
| 93 | + private function isAccessAuthorized($uri) |
| 94 | + { |
| 95 | + foreach ($this->forbiddenUriPatterns as $forbiddenUriPattern) |
| 96 | + { |
| 97 | + if (preg_match("@$forbiddenUriPattern@", $uri)) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get the fully resolved path to the application's front controller. |
| 107 | + * This can be the currently requested PHP script, a folder that |
| 108 | + * contains an index.php or the global index.php otherwise. |
| 109 | + * |
| 110 | + * @param string $sitePath |
| 111 | + * @param string $siteName |
| 112 | + * @param string $uri |
| 113 | + * @return string |
| 114 | + */ |
| 115 | + public function frontControllerPath($sitePath, $siteName, $uri) |
| 116 | + { |
| 117 | + // without modifying the URI, redirect if necessary |
| 118 | + $this->handleRedirectBackendShorthandUris($uri); |
| 119 | + |
| 120 | + // from now on, remove trailing / for convenience for all the following join operations |
| 121 | + $uri = rtrim($uri, '/'); |
| 122 | + |
| 123 | + // try to find the responsible script file for the requested folder / script URI |
| 124 | + if (file_exists($absoluteFilePath = $sitePath . $this->documentRoot . $uri)) |
| 125 | + { |
| 126 | + if (is_dir($absoluteFilePath)) |
| 127 | + { |
| 128 | + if (file_exists($absoluteFilePath . '/index.php')) |
| 129 | + { |
| 130 | + // this folder can be served by index.php |
| 131 | + return $this->serveScript($sitePath, $siteName, $uri . '/index.php'); |
| 132 | + } |
| 133 | + |
| 134 | + if (file_exists($absoluteFilePath . '/index.html')) |
| 135 | + { |
| 136 | + // this folder can be served by index.html |
| 137 | + return $absoluteFilePath . '/index.html'; |
| 138 | + } |
| 139 | + } |
| 140 | + else if (pathinfo($absoluteFilePath, PATHINFO_EXTENSION) === 'php') |
| 141 | + { |
| 142 | + // this file can be served directly |
| 143 | + return $this->serveScript($sitePath, $siteName, $uri); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // the global index.php will handle all other cases |
| 148 | + return $this->serveScript($sitePath, $siteName, '/index.php'); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Direct access to installtool via domain.dev/typo3/install/ will be redirected to |
| 153 | + * sysext install script. domain.dev/typo3 will be redirected to /typo3/, because |
| 154 | + * the generated JavaScript URIs on the login screen would be broken on /typo3. |
| 155 | + * |
| 156 | + * @param string $uri |
| 157 | + */ |
| 158 | + private function handleRedirectBackendShorthandUris($uri) |
| 159 | + { |
| 160 | + if (rtrim($uri, '/') === '/typo3/install') |
| 161 | + { |
| 162 | + header('Location: /typo3/sysext/install/Start/Install.php'); |
| 163 | + die(); |
| 164 | + } |
| 165 | + |
| 166 | + if ($uri === '/typo3') |
| 167 | + { |
| 168 | + header('Location: /typo3/'); |
| 169 | + die(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Configures the $_SERVER globals for serving the script at |
| 175 | + * the specified URI and returns it absolute file path. |
| 176 | + * |
| 177 | + * @param string $sitePath |
| 178 | + * @param string $siteName |
| 179 | + * @param string $uri |
| 180 | + * @param string $script |
| 181 | + * @return string |
| 182 | + */ |
| 183 | + private function serveScript($sitePath, $siteName, $uri) |
| 184 | + { |
| 185 | + $docroot = $sitePath . $this->documentRoot; |
| 186 | + $abspath = $docroot . $uri; |
| 187 | + |
| 188 | + $_SERVER['SERVER_NAME'] = $siteName . '.dev'; |
| 189 | + $_SERVER['DOCUMENT_ROOT'] = $docroot; |
| 190 | + $_SERVER['DOCUMENT_URI'] = $uri; |
| 191 | + $_SERVER['SCRIPT_FILENAME'] = $abspath; |
| 192 | + $_SERVER['SCRIPT_NAME'] = $uri; |
| 193 | + $_SERVER['PHP_SELF'] = $uri; |
| 194 | + |
| 195 | + return $abspath; |
| 196 | + } |
| 197 | +} |
0 commit comments