| @@ -1,8 +1,19 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\App\Services\Libs; |
| 4 | 4 | |
| 5 | +/** | |
| 6 | + * @method string getDir() | |
| 7 | + * @method mixed get(string $file) | |
| 8 | + * @method string getAbsolutePathOfFile(string $file) | |
| 9 | + * @method array uploadFromRequest() | |
| 10 | + * @method array put(array $files) | |
| 11 | + * @method void delete(array|string $files) | |
| 12 | + * @method void overrideUploadDir() | |
| 13 | + * @method array setCustomUploadDir(array $param) | |
| 14 | + * @method array renameFileName(array $file) | |
| 15 | + */ | |
| 5 | 16 | class FileSystem |
| 6 | 17 | { |
| 7 | 18 | /** |
| 8 | 19 | * Read file content from custom upload dir of this application |
| @@ -11,9 +22,9 @@ | ||
| 11 | 22 | public function _get($file) |
| 12 | 23 | { |
| 13 | 24 | $arr = explode('/', $file); |
| 14 | 25 | $fileName = end($arr); |
| 15 | - return file_get_contents( $this->getDir() . '/' . $fileName ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 26 | + return file_get_contents($this->getDir() . '/' . $fileName); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 16 | 27 | } |
| 17 | 28 | |
| 18 | 29 | /** |
| 19 | 30 | * Get custom upload dir name of this application |
| @@ -60,11 +71,23 @@ | ||
| 60 | 71 | $this->overrideUploadDir(); |
| 61 | 72 | |
| 62 | 73 | $uploadOverrides = ['test_form' => false]; |
| 63 | 74 | |
| 75 | + $uploadedFiles = []; | |
| 76 | + | |
| 64 | 77 | foreach ((array)$files as $file) { |
| 65 | 78 | $filesArray = $file->toArray(); |
| 66 | - $uploadedFiles[] = \wp_handle_upload($filesArray, $uploadOverrides); | |
| 79 | + $originalName = $filesArray['name']; | |
| 80 | + $uploadedFile = \wp_handle_upload($filesArray, $uploadOverrides); | |
| 81 | + | |
| 82 | + if (isset($uploadedFile['error'])) { | |
| 83 | + $uploadedFiles[] = new \WP_Error('upload_error', $uploadedFile['error']); | |
| 84 | + continue; | |
| 85 | + } | |
| 86 | + | |
| 87 | + $uploadedFile['original_name'] = $originalName; | |
| 88 | + | |
| 89 | + $uploadedFiles[] = $uploadedFile; | |
| 67 | 90 | } |
| 68 | 91 | |
| 69 | 92 | return $uploadedFiles; |
| 70 | 93 | } |
| @@ -112,24 +135,24 @@ | ||
| 112 | 135 | { |
| 113 | 136 | |
| 114 | 137 | $folderName = apply_filters('fluent_community/upload_folder_name', FLUENT_COMMUNITY_UPLOAD_DIR); |
| 115 | 138 | |
| 116 | - $param['url'] = $param['baseurl'] .'/'. $folderName; | |
| 139 | + $param['url'] = $param['baseurl'] . '/' . $folderName; | |
| 117 | 140 | |
| 118 | - $param['path'] = $param['basedir'] .'/'. $folderName; | |
| 141 | + $param['path'] = $param['basedir'] . '/' . $folderName; | |
| 119 | 142 | |
| 120 | 143 | if (!is_dir($param['path'])) { |
| 121 | - mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir | |
| 122 | - $htaccessContent = '# END FluentCommunity\n# Disable parsing of PHP for some server configurations.\n<Files *>\nSetHandler none\nSetHandler default-handler\nOptions -ExecCGI\nRemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo\n</Files>\n<IfModule mod_php5.c>\nphp_flag engine off\n</IfModule>\n# END FluentCommunity'; | |
| 144 | + mkdir($param['path'], 0755); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir | |
| 145 | + $htaccessContent = '# END FluentCommunity\n# Disable parsing of PHP for some server configurations.\n<Files *>\nSetHandler none\nSetHandler default-handler\nOptions -ExecCGI\nRemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo\n</Files>\n<IfModule mod_php5.c>\nphp_flag engine off\n</IfModule>\n# END FluentCommunity'; | |
| 123 | 146 | |
| 124 | - file_put_contents( $param['basedir'].'/'.$folderName.'/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 147 | + file_put_contents($param['basedir'] . '/' . $folderName . '/.htaccess', $htaccessContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 125 | 148 | } |
| 126 | 149 | |
| 127 | - if(!file_exists($param['basedir'].'/'.$folderName.'/index.php')) { | |
| 150 | + if (!file_exists($param['basedir'] . '/' . $folderName . '/index.php')) { | |
| 128 | 151 | |
| 129 | 152 | $indexStubContent = "<?php\n// Silence is golden.\n"; |
| 130 | 153 | |
| 131 | - file_put_contents( $param['basedir'].'/'.$folderName.'/index.php', $indexStubContent ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 154 | + file_put_contents($param['basedir'] . '/' . $folderName . '/index.php', $indexStubContent); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents | |
| 132 | 155 | } |
| 133 | 156 | |
| 134 | 157 | return $param; |
| 135 | 158 | } |
| @@ -140,11 +163,12 @@ | ||
| 140 | 163 | * @return array $file |
| 141 | 164 | */ |
| 142 | 165 | public function _renameFileName($file) |
| 143 | 166 | { |
| 144 | - $prefix = 'fluentcom-' . md5(wp_generate_uuid4()) . '-fluentcom-'; | |
| 167 | + $prefix = 'fluentcom-' . wp_generate_password(32, false) . '-fluentcom-'; | |
| 168 | + $originalName = $file['name']; | |
| 145 | 169 | $file['name'] = $prefix . $file['name']; |
| 146 | - | |
| 170 | + $file['name'] = apply_filters('fluent_community/generated_upload_file_name', $file['name'], $originalName, $file); | |
| 147 | 171 | return $file; |
| 148 | 172 | } |
| 149 | 173 | |
| 150 | 174 | public static function __callStatic($method, $params) |