helperFunctions.php
280 lines
| 1 | <?php |
| 2 | |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | |
| 5 | /** |
| 6 | * @param $path |
| 7 | * @param array $data |
| 8 | * @return string |
| 9 | */ |
| 10 | function loadTemplate($path, array $data) { |
| 11 | ob_start(); |
| 12 | $fullPath = plugin_dir_path( __DIR__ ) . "src" . DIRECTORY_SEPARATOR . "Views" . DIRECTORY_SEPARATOR . $path . ".php"; |
| 13 | include($fullPath); |
| 14 | $content = ob_get_contents(); |
| 15 | ob_end_clean(); |
| 16 | return $content; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Get the view path |
| 21 | * |
| 22 | * @param $path |
| 23 | * @return string |
| 24 | */ |
| 25 | function getTransferitoViewPath($path) { |
| 26 | return plugin_dir_path( __DIR__ ) . "src" . DIRECTORY_SEPARATOR . "Views" . DIRECTORY_SEPARATOR . $path . ".php"; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Pull the html path |
| 31 | * |
| 32 | * @param $path |
| 33 | * @param $data |
| 34 | * |
| 35 | * @todo Modify to replace strings as template variables & merge with loadTemplate |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | function getHTMLPart($path, $data = []) { |
| 40 | $fullPath = plugin_dir_path( __DIR__ ) . "src" . DIRECTORY_SEPARATOR . "Views" . DIRECTORY_SEPARATOR . "html" . DIRECTORY_SEPARATOR . $path . ".html.php"; |
| 41 | $template = file_get_contents($fullPath); |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | */ |
| 46 | if (count($data)) { |
| 47 | $templateKeys = array_keys($data); |
| 48 | $templateLiterals = array_map(function($val) { return '[' . $val . ']'; }, $templateKeys); |
| 49 | $templateValues = array_values($data); |
| 50 | return str_replace($templateLiterals, $templateValues, $template); |
| 51 | } |
| 52 | |
| 53 | return $template; |
| 54 | } |
| 55 | |
| 56 | function readableFileSize($bytes) { |
| 57 | $maxByteAllowance = TRANSFERITO_MAX_ALLOWED; |
| 58 | $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); |
| 59 | $factor = floor((strlen($bytes) - 1) / 3); |
| 60 | $roundedAmount = number_format($bytes / pow(1024, $factor), 2); |
| 61 | $byteAmount = $size[$factor]; |
| 62 | |
| 63 | /** |
| 64 | * Set the size |
| 65 | */ |
| 66 | set_transient('transferito_size_size_in_bytes', $bytes); |
| 67 | set_transient('transferito_readable_size_size_in_bytes', $roundedAmount . '' . $byteAmount); |
| 68 | |
| 69 | return [ |
| 70 | 'amount' => $roundedAmount, |
| 71 | 'factor' => $byteAmount, |
| 72 | 'maxSizeExceeded' => ($bytes > $maxByteAllowance) |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | function getDBSize() { |
| 77 | $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); |
| 78 | $query = "SELECT table_schema , SUM(data_length + index_length) 'size' FROM information_schema.tables WHERE table_schema='" . DB_NAME . "' GROUP BY table_schema"; |
| 79 | $information = $mysqli->query($query)->fetch_assoc(); |
| 80 | return intval($information['size']); |
| 81 | } |
| 82 | |
| 83 | function getDirectorySize($path) { |
| 84 | $databaseSize = getDBSize(); |
| 85 | $totalFileAmount = 0; |
| 86 | $byteTotal = 0; |
| 87 | $errors = array(); |
| 88 | |
| 89 | $path = realpath($path); |
| 90 | if ($path !==false && $path != '' && file_exists($path)) { |
| 91 | foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) { |
| 92 | try { |
| 93 | $byteTotal += $object->getSize(); |
| 94 | if (!$object->isDir()){ |
| 95 | $totalFileAmount++; |
| 96 | } |
| 97 | } catch (\Exception $exception) { |
| 98 | $errors[] = $exception->getMessage(); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Total size |
| 106 | * Including the database |
| 107 | */ |
| 108 | $totalSize = $databaseSize + $byteTotal; |
| 109 | |
| 110 | /** |
| 111 | * Set the size |
| 112 | */ |
| 113 | set_transient('transferito_installation_size', [ |
| 114 | 'amountOfFiles' => $totalFileAmount, |
| 115 | 'database' => $databaseSize, |
| 116 | 'codebase' => $byteTotal, |
| 117 | 'totalSize' => $totalSize, |
| 118 | 'databasePercentage' => round(($databaseSize / $totalSize) * 100), |
| 119 | 'codebasePercentage' => round(($byteTotal / $totalSize) * 100), |
| 120 | 'errors' => $errors |
| 121 | ]); |
| 122 | |
| 123 | /** |
| 124 | * |
| 125 | */ |
| 126 | return readableFileSize($totalSize); |
| 127 | } |
| 128 | |
| 129 | function buildErrorReporting($reason) { |
| 130 | $tokenTransient = get_transient('transferito_migration_token'); |
| 131 | $timestampTransient = get_transient('transferito_migration_timestamp'); |
| 132 | $token = $tokenTransient ? $tokenTransient : null; |
| 133 | $timestamp = $timestampTransient ? $timestampTransient : null; |
| 134 | |
| 135 | $curlInfo = curl_version(); |
| 136 | |
| 137 | return [ |
| 138 | 'pluginURL' => site_url(), |
| 139 | 'pluginVersion' => TRANSFERITO_VERSION, |
| 140 | 'destinationURL' => get_transient('transferito_destination_url'), |
| 141 | 'siteSizeInBytes' => get_transient('transferito_size_size_in_bytes'), |
| 142 | 'siteSizeReadable' => get_transient('transferito_readable_size_size_in_bytes'), |
| 143 | 'phpVersion' => phpversion(), |
| 144 | 'wpVersion' => get_bloginfo('version'), |
| 145 | 'cURLVersion' => $curlInfo['version'], |
| 146 | 'openSSLVersion' => $curlInfo['ssl_version'], |
| 147 | 'failureReason' => $reason, |
| 148 | 'token' => $token, |
| 149 | 'timestamp' => $timestamp, |
| 150 | ]; |
| 151 | } |
| 152 | |
| 153 | function createArchiveLimit() { |
| 154 | $archiveLimit = 32 * pow(1024, 2); |
| 155 | $memoryLimitSplit = preg_split('/(?<=[0-9])(?=[a-z]+)/i', ini_get('memory_limit')); |
| 156 | $memoryLimitInteger = intval($memoryLimitSplit[0]); |
| 157 | |
| 158 | /** |
| 159 | * Check that the memory limit exists & the array has 2 elements |
| 160 | */ |
| 161 | if ($memoryLimitInteger > 1 && count($memoryLimitSplit) === 2) { |
| 162 | $memoryLimitSize = $memoryLimitSplit[1]; |
| 163 | $sizePOWMap = [ 'M' => 2, 'G' => 3 ]; |
| 164 | |
| 165 | /** |
| 166 | * Map the pow to the size & use half the memory |
| 167 | */ |
| 168 | if ($sizePOWMap[$memoryLimitSize]) { |
| 169 | $memoryLimitBytes = $memoryLimitInteger * pow(1024, $sizePOWMap[$memoryLimitSize]); |
| 170 | $archiveLimit = $memoryLimitBytes / 2; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return $archiveLimit; |
| 175 | } |
| 176 | |
| 177 | function transferitoConvertToBytes(array $size) { |
| 178 | $bytes = 0; |
| 179 | $sizePreByteConversion = intval($size[0]); |
| 180 | |
| 181 | /** |
| 182 | * If there is only one element in the array |
| 183 | * Then return the amount as bytes |
| 184 | */ |
| 185 | if (count($size) === 1) { |
| 186 | return $sizePreByteConversion; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * The pow map |
| 191 | */ |
| 192 | $sizePOWMap = ['K' => 1, 'M' => 2, 'G' => 3 ]; |
| 193 | |
| 194 | /** |
| 195 | * Get the size POW index |
| 196 | */ |
| 197 | $sizePOWIndex = $size[1]; |
| 198 | |
| 199 | /** |
| 200 | * Check that the pow map exists |
| 201 | */ |
| 202 | if (isset($sizePOWMap[$sizePOWIndex])) { |
| 203 | $bytes = $sizePreByteConversion * pow(1024, $sizePOWMap[$sizePOWIndex]); |
| 204 | } |
| 205 | |
| 206 | return $bytes; |
| 207 | } |
| 208 | |
| 209 | function checkJobHasCompleted($pid) { |
| 210 | if (!$pid) { |
| 211 | return true; |
| 212 | } |
| 213 | exec(sprintf('kill -0 %d; echo $?', $pid), $output); |
| 214 | return $output[0] === '1'; |
| 215 | } |
| 216 | |
| 217 | function transferitoGetLastLine($file) { |
| 218 | $line = ''; |
| 219 | $progressFileHandle = fopen($file, 'r'); |
| 220 | $cursor = -1; |
| 221 | fseek($progressFileHandle, $cursor, SEEK_END); |
| 222 | $char = fgetc($progressFileHandle); |
| 223 | //Trim trailing newline characters in the file |
| 224 | while ($char === "\n" || $char === "\r") { |
| 225 | fseek($progressFileHandle, $cursor--, SEEK_END); |
| 226 | $char = fgetc($progressFileHandle); |
| 227 | } |
| 228 | //Read until the next line of the file begins or the first newline char |
| 229 | while ($char !== false && $char !== "\n" && $char !== "\r") { |
| 230 | //Prepend the new character |
| 231 | $line = $char . $line; |
| 232 | fseek($progressFileHandle, $cursor--, SEEK_END); |
| 233 | $char = fgetc($progressFileHandle); |
| 234 | } |
| 235 | return $line; |
| 236 | } |
| 237 | |
| 238 | function useZipArchive() { |
| 239 | $settings = get_option('transferito_settings_option'); |
| 240 | $forceTarArchiveCreation = isset($settings['transferito_force_tar_backup']) |
| 241 | ? $settings['transferito_force_tar_backup'] |
| 242 | : false; |
| 243 | |
| 244 | /** |
| 245 | * If this is set to true |
| 246 | * We default the archive creation type to TAR & return a falsey value |
| 247 | */ |
| 248 | if ($forceTarArchiveCreation) { |
| 249 | set_transient('transferito_archive_extension', 'tar'); |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * If the tar archive creation hasn't been forced |
| 255 | * Check see if we've reached the ZIP limit |
| 256 | */ |
| 257 | $sizeInformation = get_transient('transferito_installation_size'); |
| 258 | $isZipArchive = $sizeInformation['totalSize'] < TRANSFERITO_ZIP_FILE_LIMIT; |
| 259 | $extension = $isZipArchive ? 'zip' : 'tar'; |
| 260 | |
| 261 | set_transient('transferito_archive_extension', $extension); |
| 262 | |
| 263 | return $isZipArchive; |
| 264 | } |
| 265 | |
| 266 | function getObjectCacheFilterData() { |
| 267 | return [ |
| 268 | 'text' => "apply_filters( 'enable_loading_object_cache_dropin', false );", |
| 269 | 'path' => get_template_directory() . DIRECTORY_SEPARATOR . 'functions.php' |
| 270 | ]; |
| 271 | } |
| 272 | |
| 273 | function getPrependOptionNameData() { |
| 274 | return [ |
| 275 | 'text' => 'auto_prepend_file', |
| 276 | 'commented' => '; auto_prepend_file', |
| 277 | 'path' => ABSPATH . '.user.ini' |
| 278 | ]; |
| 279 | } |
| 280 |