| @@ -1,21 +1,42 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * NginxFileProtection.php | |
| 4 | + * | |
| 5 | + * The NginxFileProtection class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 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 | + */ | |
| 5 | 15 | namespace UserAccessManager\File; |
| 6 | 16 | |
| 7 | -use Exception; | |
| 8 | 17 | use UserAccessManager\Object\ObjectHandler; |
| 9 | 18 | |
| 19 | +/** | |
| 20 | + * Class NginxFileProtection | |
| 21 | + * | |
| 22 | + * @package UserAccessManager\FileHandler | |
| 23 | + */ | |
| 10 | 24 | class NginxFileProtection extends FileProtection implements FileProtectionInterface |
| 11 | 25 | { |
| 12 | - public const FILE_NAME = 'uam.conf'; | |
| 26 | + const FILE_NAME = 'uam.conf'; | |
| 13 | 27 | |
| 14 | - protected function getLocation(string $directory): string | |
| 28 | + /** | |
| 29 | + * Returns the location. | |
| 30 | + * | |
| 31 | + * @param string $directory | |
| 32 | + * | |
| 33 | + * @return string | |
| 34 | + */ | |
| 35 | + protected function getLocation($directory) | |
| 15 | 36 | { |
| 16 | 37 | if ($this->mainConfig->getLockedDirectoryType() === 'wordpress') { |
| 17 | - return "^$directory" . $this->getDirectoryMatch(); | |
| 38 | + return "^{$directory}".$this->getDirectoryMatch(); | |
| 18 | 39 | } |
| 19 | 40 | |
| 20 | 41 | $directoryMatch = $this->getDirectoryMatch(); |
| 21 | 42 | return $directoryMatch === null ? $directory : $directoryMatch; |
| @@ -20,9 +41,18 @@ | ||
| 20 | 41 | $directoryMatch = $this->getDirectoryMatch(); |
| 21 | 42 | return $directoryMatch === null ? $directory : $directoryMatch; |
| 22 | 43 | } |
| 23 | 44 | |
| 24 | - private function getFileContent(string $absolutePath, string $directory, ?string $objectType): string | |
| 45 | + /** | |
| 46 | + * Creates the file content if permalinks are active. | |
| 47 | + * | |
| 48 | + * @param string $absolutePath | |
| 49 | + * @param string $directory | |
| 50 | + * @param string $objectType | |
| 51 | + * | |
| 52 | + * @return string | |
| 53 | + */ | |
| 54 | + private function getFileContent($absolutePath, $directory, $objectType) | |
| 25 | 55 | { |
| 26 | 56 | if ($objectType === null) { |
| 27 | 57 | $objectType = ObjectHandler::ATTACHMENT_OBJECT_TYPE; |
| 28 | 58 | } |
| @@ -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,26 +68,43 @@ | ||
| 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 | + * | |
| 75 | + * @param string null|$directory | |
| 76 | + * | |
| 77 | + * @return string | |
| 78 | + */ | |
| 79 | + public function getFileNameWithPath($directory = null) | |
| 43 | 80 | { |
| 44 | - return ABSPATH . self::FILE_NAME; | |
| 81 | + return ABSPATH.self::FILE_NAME; | |
| 45 | 82 | } |
| 46 | 83 | |
| 47 | - public function create(string $directory, ?string $objectType = null, ?string $absolutePath = ABSPATH): bool | |
| 84 | + /** | |
| 85 | + * Generates the conf file. | |
| 86 | + * | |
| 87 | + * @param string $directory | |
| 88 | + * @param string $objectType | |
| 89 | + * @param string $absolutePath | |
| 90 | + * | |
| 91 | + * @return bool | |
| 92 | + */ | |
| 93 | + public function create($directory, $objectType = null, $absolutePath = ABSPATH) | |
| 48 | 94 | { |
| 49 | - $directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; | |
| 50 | - $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; | |
| 95 | + $directory = rtrim($directory, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; | |
| 96 | + $absolutePath = rtrim($absolutePath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; | |
| 51 | 97 | $content = $this->getFileContent($absolutePath, $directory, $objectType); |
| 52 | 98 | |
| 53 | 99 | // save files |
| 54 | - $fileWithPath = $absolutePath . self::FILE_NAME; | |
| 100 | + $fileWithPath = $absolutePath.self::FILE_NAME; | |
| 55 | 101 | |
| 56 | 102 | 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 | |
| 103 | + file_put_contents($fileWithPath, $content); | |
| 104 | + return true; | |
| 105 | + } catch (\Exception $exception) { | |
| 106 | + // Because file_put_contents can throw exceptions we use this try catch block | |
| 60 | 107 | // to return the success result instead of an exception |
| 61 | 108 | } |
| 62 | 109 | |
| 63 | 110 | return false; |
| @@ -62,9 +109,16 @@ | ||
| 62 | 109 | |
| 63 | 110 | return false; |
| 64 | 111 | } |
| 65 | 112 | |
| 66 | - public function delete(string $directory): bool | |
| 113 | + /** | |
| 114 | + * Deletes the conf file. | |
| 115 | + * | |
| 116 | + * @param string $directory | |
| 117 | + * | |
| 118 | + * @return bool | |
| 119 | + */ | |
| 120 | + public function delete($directory) | |
| 67 | 121 | { |
| 68 | 122 | return $this->deleteFiles($directory); |
| 69 | 123 | } |
| 70 | 124 | } |