| @@ -1,0 +1,230 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +defined('ABSPATH') || die('Restricted Access'); | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * @return bool|null|string | |
| 7 | + */ | |
| 8 | +function acym_fileGetContent(string $url, int $timeout = 10) | |
| 9 | +{ | |
| 10 | + if (strpos($url, '_custom.ini') !== false && !file_exists($url)) { | |
| 11 | + return ''; | |
| 12 | + } | |
| 13 | + | |
| 14 | + ob_start(); | |
| 15 | + $data = ''; | |
| 16 | + | |
| 17 | + $allowUrlFopen = ini_get('allow_url_fopen'); | |
| 18 | + | |
| 19 | + if (function_exists('file_get_contents') && (!empty($allowUrlFopen) || strpos($url, 'http') !== 0)) { | |
| 20 | + $options = [ | |
| 21 | + 'ssl' => [ | |
| 22 | + 'verify_peer' => false, | |
| 23 | + 'verify_peer_name' => false, | |
| 24 | + ], | |
| 25 | + ]; | |
| 26 | + if (!empty($timeout)) { | |
| 27 | + $options['http']['timeout'] = $timeout; | |
| 28 | + } | |
| 29 | + $streamContext = stream_context_create($options); | |
| 30 | + | |
| 31 | + $data = file_get_contents($url, false, $streamContext); | |
| 32 | + } | |
| 33 | + | |
| 34 | + if (empty($data) && strpos($url, 'http') === 0 && class_exists('WP_Http') && method_exists('WP_Http', 'request')) { | |
| 35 | + $args = ['timeout' => $timeout]; | |
| 36 | + $request = new WP_Http(); | |
| 37 | + $data = $request->request($url, $args); | |
| 38 | + $data = (empty($data) || !is_array($data) || empty($data['body'])) ? '' : $data['body']; | |
| 39 | + } | |
| 40 | + | |
| 41 | + if (empty($data) && strpos($url, 'http') !== 0) { | |
| 42 | + $data = acym_getInternalFileContents($url); | |
| 43 | + } | |
| 44 | + $warnings = ob_get_clean(); | |
| 45 | + | |
| 46 | + if (acym_isDebug()) { | |
| 47 | + echo esc_html($warnings); | |
| 48 | + } | |
| 49 | + | |
| 50 | + return $data; | |
| 51 | +} | |
| 52 | + | |
| 53 | +function acym_extractArchive(string $archive, string $destination): bool | |
| 54 | +{ | |
| 55 | + if (substr($archive, strlen($archive) - 4) !== '.zip') { | |
| 56 | + return false; | |
| 57 | + } | |
| 58 | + | |
| 59 | + WP_Filesystem(); | |
| 60 | + | |
| 61 | + return true === unzip_file($archive, $destination); | |
| 62 | +} | |
| 63 | + | |
| 64 | +function acym_deleteFolder(string $path, bool $report = true): bool | |
| 65 | +{ | |
| 66 | + global $wp_filesystem; | |
| 67 | + if (empty($wp_filesystem)) { | |
| 68 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 69 | + WP_Filesystem(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + $path = acym_cleanPath($path); | |
| 73 | + if (!is_dir($path)) { | |
| 74 | + if ($report) { | |
| 75 | + acym_enqueueMessage(acym_translationSprintf('ACYM_IS_NOT_A_FOLDER', $path), 'error'); | |
| 76 | + } | |
| 77 | + | |
| 78 | + return false; | |
| 79 | + } | |
| 80 | + | |
| 81 | + if (!empty($wp_filesystem) && $wp_filesystem->rmdir($path, true)) { | |
| 82 | + return true; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if ($report) { | |
| 86 | + acym_enqueueMessage(acym_translationSprintf('ACYM_COULD_NOT_DELETE_FOLDER', $path), 'error'); | |
| 87 | + } | |
| 88 | + | |
| 89 | + return false; | |
| 90 | +} | |
| 91 | + | |
| 92 | +function acym_createFolder(string $path = ''): bool | |
| 93 | +{ | |
| 94 | + $path = acym_cleanPath($path); | |
| 95 | + if (empty($path)) { | |
| 96 | + return false; | |
| 97 | + } | |
| 98 | + | |
| 99 | + if (file_exists($path)) { | |
| 100 | + return true; | |
| 101 | + } | |
| 102 | + | |
| 103 | + return wp_mkdir_p($path); | |
| 104 | +} | |
| 105 | + | |
| 106 | +function acym_uploadFile(string $src, string $dest): bool | |
| 107 | +{ | |
| 108 | + global $wp_filesystem; | |
| 109 | + if (empty($wp_filesystem)) { | |
| 110 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 111 | + WP_Filesystem(); | |
| 112 | + } | |
| 113 | + | |
| 114 | + $dest = acym_cleanPath($dest); | |
| 115 | + | |
| 116 | + $baseDir = dirname($dest); | |
| 117 | + if (!file_exists($baseDir)) { | |
| 118 | + acym_createFolder($baseDir); | |
| 119 | + } | |
| 120 | + | |
| 121 | + if (!is_uploaded_file($src)) { | |
| 122 | + acym_enqueueMessage(acym_translation('ACYM_FILE_REJECTED_SAFETY_REASON'), 'error'); | |
| 123 | + | |
| 124 | + return false; | |
| 125 | + } | |
| 126 | + | |
| 127 | + if ( | |
| 128 | + !empty($wp_filesystem) | |
| 129 | + && $wp_filesystem->is_writable($baseDir) | |
| 130 | + && $wp_filesystem->move($src, $dest, true) | |
| 131 | + ) { | |
| 132 | + // Short circuit to prevent file permission errors | |
| 133 | + if (acym_chmod($dest, FS_CHMOD_FILE)) { | |
| 134 | + return true; | |
| 135 | + } else { | |
| 136 | + acym_enqueueMessage(acym_translation('ACYM_FILE_REJECTED_SAFETY_REASON'), 'error'); | |
| 137 | + } | |
| 138 | + } else { | |
| 139 | + acym_enqueueMessage(acym_translationSprintf('ACYM_COULD_NOT_UPLOAD_FILE_PERMISSION', $baseDir), 'error'); | |
| 140 | + } | |
| 141 | + | |
| 142 | + return false; | |
| 143 | +} | |
| 144 | + | |
| 145 | +function acym_moveFile(string $src, string $dest, string $path = ''): bool | |
| 146 | +{ | |
| 147 | + global $wp_filesystem; | |
| 148 | + if (empty($wp_filesystem)) { | |
| 149 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 150 | + WP_Filesystem(); | |
| 151 | + } | |
| 152 | + | |
| 153 | + if (!empty($path)) { | |
| 154 | + $src = acym_cleanPath($path.'/'.$src); | |
| 155 | + $dest = acym_cleanPath($path.'/'.$dest); | |
| 156 | + } | |
| 157 | + | |
| 158 | + if (empty($wp_filesystem) || !$wp_filesystem->is_readable($src)) { | |
| 159 | + acym_enqueueMessage(acym_translationSprintf('ACYM_COULD_NOT_FIND_FILE_SOURCE_PERMISSION', $src), 'error'); | |
| 160 | + | |
| 161 | + return false; | |
| 162 | + } | |
| 163 | + | |
| 164 | + if (!$wp_filesystem->move($src, $dest, true)) { | |
| 165 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_MOVE_FILE'), 'error'); | |
| 166 | + | |
| 167 | + return false; | |
| 168 | + } | |
| 169 | + | |
| 170 | + return true; | |
| 171 | +} | |
| 172 | + | |
| 173 | +function acym_isWritable(string $path = ''): bool | |
| 174 | +{ | |
| 175 | + global $wp_filesystem; | |
| 176 | + if (empty($wp_filesystem)) { | |
| 177 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 178 | + WP_Filesystem(); | |
| 179 | + } | |
| 180 | + | |
| 181 | + return $wp_filesystem->is_writable($path); | |
| 182 | +} | |
| 183 | + | |
| 184 | +function acym_chmod(string $path, int $mode): bool | |
| 185 | +{ | |
| 186 | + global $wp_filesystem; | |
| 187 | + if (empty($wp_filesystem)) { | |
| 188 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 189 | + WP_Filesystem(); | |
| 190 | + } | |
| 191 | + | |
| 192 | + $applied = $wp_filesystem instanceof \WP_Filesystem_Base && $wp_filesystem->chmod($path, $mode); | |
| 193 | + | |
| 194 | + clearstatcache(true, $path); | |
| 195 | + $currentMode = @fileperms($path); | |
| 196 | + | |
| 197 | + if ($applied && $currentMode !== false && ($currentMode & 0777) === $mode) { | |
| 198 | + return true; | |
| 199 | + } | |
| 200 | + | |
| 201 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod -- Fallback | |
| 202 | + return @chmod($path, $mode); | |
| 203 | +} | |
| 204 | + | |
| 205 | +function acym_deleteFile(string $file): bool | |
| 206 | +{ | |
| 207 | + if (!file_exists($file)) { | |
| 208 | + return true; | |
| 209 | + } | |
| 210 | + | |
| 211 | + $deleted = wp_delete_file($file); | |
| 212 | + | |
| 213 | + return ($deleted || is_null($deleted)); | |
| 214 | +} | |
| 215 | + | |
| 216 | + | |
| 217 | +function acym_getInternalFileContents(string $path) | |
| 218 | +{ | |
| 219 | + global $wp_filesystem; | |
| 220 | + if (empty($wp_filesystem)) { | |
| 221 | + require_once ABSPATH.'wp-admin/includes/file.php'; | |
| 222 | + WP_Filesystem(); | |
| 223 | + } | |
| 224 | + | |
| 225 | + if ($wp_filesystem instanceof \WP_Filesystem_Base && $wp_filesystem->exists($path)) { | |
| 226 | + return $wp_filesystem->get_contents($path); | |
| 227 | + } | |
| 228 | + | |
| 229 | + return false; | |
| 230 | +} | |