| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * NginxFileProtection.php | |
| 4 | + * | |
| 5 | + * The NginxFileProtection class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\File; |
| @@ -6,16 +19,26 @@ | ||
| 6 | 19 | |
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Object\ObjectHandler; |
| 9 | 22 | |
| 23 | +/** | |
| 24 | + * Class NginxFileProtection | |
| 25 | + * | |
| 26 | + * @package UserAccessManager\FileHandler | |
| 27 | + */ | |
| 10 | 28 | class NginxFileProtection extends FileProtection implements FileProtectionInterface |
| 11 | 29 | { |
| 12 | - public const FILE_NAME = 'uam.conf'; | |
| 30 | + const FILE_NAME = 'uam.conf'; | |
| 13 | 31 | |
| 32 | + /** | |
| 33 | + * Returns the location. | |
| 34 | + * @param string $directory | |
| 35 | + * @return string | |
| 36 | + */ | |
| 14 | 37 | protected function getLocation(string $directory): string |
| 15 | 38 | { |
| 16 | 39 | if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') { |
| 17 | - return "^$directory" . $this->getDirectoryMatch(); | |
| 40 | + return "^{$directory}" . $this->getDirectoryMatch(); | |
| 18 | 41 | } |
| 19 | 42 | |
| 20 | 43 | $directoryMatch = $this->getDirectoryMatch(); |
| 21 | 44 | return $directoryMatch === null ? $directory : $directoryMatch; |
| @@ -20,8 +43,15 @@ | ||
| 20 | 43 | $directoryMatch = $this->getDirectoryMatch(); |
| 21 | 44 | return $directoryMatch === null ? $directory : $directoryMatch; |
| 22 | 45 | } |
| 23 | 46 | |
| 47 | + /** | |
| 48 | + * Creates the file content if permalinks are active. | |
| 49 | + * @param string $absolutePath | |
| 50 | + * @param string $directory | |
| 51 | + * @param string|null $objectType | |
| 52 | + * @return string | |
| 53 | + */ | |
| 24 | 54 | private function getFileContent(string $absolutePath, string $directory, ?string $objectType): string |
| 25 | 55 | { |
| 26 | 56 | if ($objectType === null) { |
| 27 | 57 | $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE; |
| @@ -28,12 +58,12 @@ | ||
| 28 | 58 | } |
| 29 | 59 | |
| 30 | 60 | $location = $this->getLocation(str_replace($absolutePath, '/', $directory)); |
| 31 | 61 | |
| 32 | - $content = "location ~ \"$location\" {\n"; | |
| 33 | - $content .= "rewrite ^([^?]*)$ /index.php?uamfiletype=$objectType&uamgetfile=$1 last;\n"; | |
| 62 | + $content = "location {$location} {\n"; | |
| 63 | + $content .= "rewrite ^([^?]*)$ /index.php?uamfiletype={$objectType}&uamgetfile=$1 last;\n"; | |
| 34 | 64 | $content .= "rewrite ^(.*)\\?(((?!uamfiletype).)*)$ "; |
| 35 | - $content .= "/index.php?uamfiletype=$objectType&uamgetfile=$1&$2 last;\n"; | |
| 65 | + $content .= "/index.php?uamfiletype={$objectType}&uamgetfile=$1&$2 last;\n"; | |
| 36 | 66 | $content .= "rewrite ^(.*)\\?(.*)$ /index.php?uamgetfile=$1&$2 last;\n"; |
| 37 | 67 | $content .= "}\n"; |
| 38 | 68 | |
| 39 | 69 | return $content; |
| @@ -38,13 +68,25 @@ | ||
| 38 | 68 | |
| 39 | 69 | return $content; |
| 40 | 70 | } |
| 41 | 71 | |
| 42 | - public function getFileNameWithPath(?string $directory = null): string | |
| 72 | + /** | |
| 73 | + * Returns the nginx config file name with path. | |
| 74 | + * @param string null|$directory | |
| 75 | + * @return string | |
| 76 | + */ | |
| 77 | + public function getFileNameWithPath($directory = null): string | |
| 43 | 78 | { |
| 44 | 79 | return ABSPATH . self::FILE_NAME; |
| 45 | 80 | } |
| 46 | 81 | |
| 82 | + /** | |
| 83 | + * Generates the conf file. | |
| 84 | + * @param string $directory | |
| 85 | + * @param string|null $objectType | |
| 86 | + * @param string|null $absolutePath | |
| 87 | + * @return bool | |
| 88 | + */ | |
| 47 | 89 | public function create(string $directory, ?string $objectType = null, ?string $absolutePath = ABSPATH): bool |
| 48 | 90 | { |
| 49 | 91 | $directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
| 50 | 92 | $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
| @@ -53,11 +95,12 @@ | ||
| 53 | 95 | // save files |
| 54 | 96 | $fileWithPath = $absolutePath . self::FILE_NAME; |
| 55 | 97 | |
| 56 | 98 | try { |
| 57 | - return file_put_contents($fileWithPath, $content) !== false; | |
| 58 | - } catch (Exception) { | |
| 59 | - // Because file_put_contents can throw exceptions, we use this try catch block | |
| 99 | + file_put_contents($fileWithPath, $content); | |
| 100 | + return true; | |
| 101 | + } catch (Exception $exception) { | |
| 102 | + // Because file_put_contents can throw exceptions we use this try catch block | |
| 60 | 103 | // to return the success result instead of an exception |
| 61 | 104 | } |
| 62 | 105 | |
| 63 | 106 | return false; |
| @@ -62,8 +105,13 @@ | ||
| 62 | 105 | |
| 63 | 106 | return false; |
| 64 | 107 | } |
| 65 | 108 | |
| 109 | + /** | |
| 110 | + * Deletes the conf file. | |
| 111 | + * @param string $directory | |
| 112 | + * @return bool | |
| 113 | + */ | |
| 66 | 114 | public function delete(string $directory): bool |
| 67 | 115 | { |
| 68 | 116 | return $this->deleteFiles($directory); |
| 69 | 117 | } |