|
| 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 publicly available from your web server. |
| 28 | + | Instead, the server will throw a 403 forbidden response, if you try |
| 29 | + | to access these files via the HTTP layer. Use regex syntax here. |
| 30 | + | |
| 31 | + */ |
| 32 | + protected $forbiddenUriPatterns = [ |
| 33 | + '_(recycler|temp)_/', |
| 34 | + '^/(typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(Resources/Private|Tests)/', |
| 35 | + '\.(htaccess|gitkeep|gitignore)', |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * Determine if the driver serves the request. For TYPO3, this is the |
| 40 | + * case, if a folder called "typo3" is present in the document root. |
| 41 | + * |
| 42 | + * @param string $sitePath |
| 43 | + * @param string $siteName |
| 44 | + * @param string $uri |
| 45 | + * @return bool |
| 46 | + */ |
| 47 | + public function serves($sitePath, $siteName, $uri) |
| 48 | + { |
| 49 | + $typo3Dir = $sitePath . $this->documentRoot . '/typo3'; |
| 50 | + return file_exists($typo3Dir) && is_dir($typo3Dir); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Determine if the incoming request is for a static file. That is, it is |
| 55 | + * no PHP script file and the URI points to a valid file (no folder) on |
| 56 | + * the disk. Access to those static files will be authorized. |
| 57 | + * |
| 58 | + * @param string $sitePath |
| 59 | + * @param string $siteName |
| 60 | + * @param string $uri |
| 61 | + * @return string|false |
| 62 | + */ |
| 63 | + public function isStaticFile($sitePath, $siteName, $uri) |
| 64 | + { |
| 65 | + if (file_exists($filePath = $sitePath . $this->documentRoot . $uri) |
| 66 | + && ! is_dir($filePath) |
| 67 | + && pathinfo($filePath)['extension'] !== 'php') |
| 68 | + { |
| 69 | + $this->authorizeAccess($uri); |
| 70 | + return $filePath; |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the fully resolved path to the application's front controller. |
| 77 | + * This can be the currently requested PHP script, a folder that |
| 78 | + * contains an index.php or the global index.php otherwise. |
| 79 | + * |
| 80 | + * @param string $sitePath |
| 81 | + * @param string $siteName |
| 82 | + * @param string $uri |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public function frontControllerPath($sitePath, $siteName, $uri) |
| 86 | + { |
| 87 | + $this->authorizeAccess($uri); |
| 88 | + $uri = rtrim($uri, '/'); |
| 89 | + $absoluteFilePath = $sitePath . $this->documentRoot . $uri; |
| 90 | + |
| 91 | + if (file_exists($absoluteFilePath)) |
| 92 | + { |
| 93 | + if (is_dir($absoluteFilePath)) |
| 94 | + { |
| 95 | + if (file_exists($absoluteFilePath . '/index.php')) |
| 96 | + { |
| 97 | + // this folder can be served by index.php |
| 98 | + return $this->serveScript($sitePath, $siteName, $uri . '/index.php'); |
| 99 | + } |
| 100 | + |
| 101 | + if (file_exists($absoluteFilePath . '/index.html')) |
| 102 | + { |
| 103 | + // this folder can be served by index.html |
| 104 | + return $absoluteFilePath . '/index.html'; |
| 105 | + } |
| 106 | + } |
| 107 | + else if (pathinfo($absoluteFilePath)['extension'] === 'php') |
| 108 | + { |
| 109 | + // this file can be served directly |
| 110 | + return $this->serveScript($sitePath, $siteName, $uri); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // the global index.php will handle all other cases |
| 115 | + return $this->serveScript($sitePath, $siteName, '/index.php'); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Configures the $_SERVER globals for serving the script at |
| 120 | + * the specified URI and returns it absolute file path. |
| 121 | + * |
| 122 | + * @param string $sitePath |
| 123 | + * @param string $siteName |
| 124 | + * @param string $uri |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + private function serveScript($sitePath, $siteName, $uri) |
| 128 | + { |
| 129 | + $absoluteDocumentRoot = $sitePath . $this->documentRoot; |
| 130 | + $absoluteFilePath = $absoluteDocumentRoot . $uri; |
| 131 | + |
| 132 | + $_SERVER['SERVER_NAME'] = $siteName . '.dev'; |
| 133 | + $_SERVER['DOCUMENT_ROOT'] = $absoluteDocumentRoot; |
| 134 | + $_SERVER['DOCUMENT_URI'] = $uri; |
| 135 | + $_SERVER['SCRIPT_FILENAME'] = $absoluteFilePath; |
| 136 | + $_SERVER['SCRIPT_NAME'] = $uri; |
| 137 | + $_SERVER['PHP_SELF'] = $uri; |
| 138 | + |
| 139 | + return $absoluteFilePath; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Interrupts execution with a 403 FORBIDDEN if the requested URI is on |
| 144 | + * the global blacklist of system files that should not be served. |
| 145 | + * |
| 146 | + * @param string $uri |
| 147 | + */ |
| 148 | + private function authorizeAccess($uri) |
| 149 | + { |
| 150 | + foreach ($this->forbiddenUriPatterns as $forbiddenUri) |
| 151 | + { |
| 152 | + if (preg_match("@$forbiddenUri@", $uri)) |
| 153 | + { |
| 154 | + header('HTTP/1.0 403 Forbidden'); |
| 155 | + die("You are forbidden to see $uri!"); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments