zip.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | // Namespace |
| 4 | namespace BMI\Plugin\Progress; |
| 5 | |
| 6 | // Use |
| 7 | use BMI\Plugin\BMI_Logger AS Logger; |
| 8 | |
| 9 | // Exit on direct access |
| 10 | if (!defined('ABSPATH')) exit; |
| 11 | |
| 12 | /** |
| 13 | * Main File Backup Logs |
| 14 | */ |
| 15 | class BMI_ZipProgress { |
| 16 | |
| 17 | public $name; |
| 18 | public $date; |
| 19 | public $millis; |
| 20 | public $cron; |
| 21 | public $logfilename; |
| 22 | public $latest; |
| 23 | public $latest_progress; |
| 24 | public $files; |
| 25 | public $bytes; |
| 26 | public $total_queries; |
| 27 | public $file; |
| 28 | public $progress; |
| 29 | public $muted = false; |
| 30 | public $clearEndCodes = false; |
| 31 | |
| 32 | public function __construct($backup_name, $files = 0, $bytes = 0, $cron = false, $reset = true, $clearEndCodes = false) { |
| 33 | |
| 34 | if (!file_exists(BMI_BACKUPS)) mkdir(BMI_BACKUPS, 0755, true); |
| 35 | |
| 36 | $this->name = $backup_name; |
| 37 | $this->date = date('Y-m-d H:i:s'); |
| 38 | $this->millis = microtime(true); |
| 39 | $this->cron = $cron; |
| 40 | $this->logfilename = substr($backup_name, 0, -4) . '.log'; |
| 41 | $this->latest = BMI_BACKUPS . '/latest.log'; |
| 42 | $this->latest_progress = BMI_BACKUPS . '/latest_progress.log'; |
| 43 | $this->files = $files; |
| 44 | $this->bytes = $bytes; |
| 45 | $this->total_queries = 1; |
| 46 | $this->clearEndCodes = $clearEndCodes; |
| 47 | |
| 48 | if ($reset == true) { |
| 49 | if (file_exists($this->latest)) @unlink($this->latest); |
| 50 | if (file_exists($this->latest_progress)) @unlink($this->latest_progress); |
| 51 | file_put_contents($this->latest_progress, '0/100'); |
| 52 | } |
| 53 | |
| 54 | if ($this->clearEndCodes) { |
| 55 | $this->removeAllEndCodes(); |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |
| 60 | public function createManifest($dbBackupEngine = 'v4') { |
| 61 | |
| 62 | global $table_prefix; |
| 63 | |
| 64 | $manifest = array( |
| 65 | 'name' => $this->name, |
| 66 | 'date' => $this->date, |
| 67 | 'files' => $this->files, |
| 68 | 'bytes' => $this->bytes, |
| 69 | 'cron' => $this->cron, |
| 70 | 'total_queries' => $this->total_queries, |
| 71 | 'manifest' => date('Y-m-d H:i:s'), |
| 72 | 'millis_start' => $this->millis, |
| 73 | 'millis_end' => microtime(true), |
| 74 | 'version' => (defined('BMI_VERSION') ? BMI_VERSION : ''), |
| 75 | 'domain' => parse_url(home_url())['host'], |
| 76 | 'dbdomain' => get_option('siteurl'), |
| 77 | 'uid' => get_current_user_id(), |
| 78 | 'source_query_output' => (defined('BMI_DB_MAX_ROWS_PER_QUERY') ? BMI_DB_MAX_ROWS_PER_QUERY : ''), |
| 79 | 'db_backup_engine' => $dbBackupEngine, |
| 80 | 'multisite' => (defined('MULTISITE') ? MULTISITE : 'false'), |
| 81 | 'config' => array( |
| 82 | 'ABSPATH' => ABSPATH, |
| 83 | 'DB_NAME' => (defined('DB_NAME') ? DB_NAME : ''), |
| 84 | 'DB_USER' => (defined('DB_USER') ? DB_USER : ''), |
| 85 | 'DB_PASSWORD' => (defined('DB_PASSWORD') ? DB_PASSWORD : ''), |
| 86 | 'DB_HOST' => (defined('DB_HOST') ? DB_HOST : ''), |
| 87 | 'DB_CHARSET' => (defined('DB_CHARSET') ? DB_CHARSET : ''), |
| 88 | 'DB_COLLATE' => (defined('DB_COLLATE') ? DB_COLLATE : ''), |
| 89 | 'AUTH_KEY' => (defined('AUTH_KEY') ? AUTH_KEY : ''), |
| 90 | 'SECURE_AUTH_KEY' => (defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : ''), |
| 91 | 'LOGGED_IN_KEY' => (defined('LOGGED_IN_KEY') ? LOGGED_IN_KEY : ''), |
| 92 | 'NONCE_KEY' => (defined('NONCE_KEY') ? NONCE_KEY : ''), |
| 93 | 'AUTH_SALT' => (defined('AUTH_SALT') ? AUTH_SALT : ''), |
| 94 | 'SECURE_AUTH_SALT' => (defined('SECURE_AUTH_SALT') ? SECURE_AUTH_SALT : ''), |
| 95 | 'LOGGED_IN_SALT' => (defined('LOGGED_IN_SALT') ? LOGGED_IN_SALT : ''), |
| 96 | 'NONCE_SALT' => (defined('NONCE_SALT') ? NONCE_SALT : ''), |
| 97 | 'WP_DEBUG_LOG' => (defined('WP_DEBUG_LOG') ? WP_DEBUG_LOG : ''), |
| 98 | 'WP_CONTENT_URL' => (defined('WP_CONTENT_URL') ? WP_CONTENT_URL : ''), |
| 99 | 'WP_CONTENT_DIR' => (defined('WP_CONTENT_DIR') ? trailingslashit(WP_CONTENT_DIR) : ''), |
| 100 | 'table_prefix' => $table_prefix |
| 101 | ) |
| 102 | ); |
| 103 | |
| 104 | return json_encode($manifest); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | public function start($muted = false) { |
| 109 | |
| 110 | $this->muted = $muted; |
| 111 | |
| 112 | } |
| 113 | |
| 114 | public function log($log = '', $level = 'INFO') { |
| 115 | |
| 116 | if (!$this->muted) { |
| 117 | $this->file = fopen($this->latest, 'a'); |
| 118 | if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) { |
| 119 | $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] [CLI] ' . $log . "\n"; |
| 120 | } else { |
| 121 | $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] ' . $log . "\n"; |
| 122 | } |
| 123 | fwrite($this->file, $log_string); |
| 124 | fclose($this->file); |
| 125 | if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) { |
| 126 | echo $log_string; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | public function progress($progress = '0') { |
| 133 | |
| 134 | $this->progress = fopen($this->latest_progress, 'w') or die(__("Unable to open file!", 'backup-backup')); |
| 135 | fwrite($this->progress, $progress); |
| 136 | fclose($this->progress); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | public function removeAllEndCodes() { |
| 141 | |
| 142 | $logs = file_get_contents($this->latest); |
| 143 | $logs = explode("\n", $logs); |
| 144 | |
| 145 | foreach ($logs as $line => $string) { |
| 146 | if (strpos($string, '[END-CODE]') !== false) { |
| 147 | $code = substr($string, 10); |
| 148 | $logs[$line] = "[VERBOSE] Logs from there starts as new process, it was continued by smart solution system after applying new plugin settings. (" . $code . ")"; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | $logs = implode("\n", $logs); |
| 153 | file_put_contents($this->latest, $logs); |
| 154 | file_put_contents($this->latest_progress, '0/100'); |
| 155 | |
| 156 | } |
| 157 | |
| 158 | public function end() { |
| 159 | |
| 160 | // fclose($this->file); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | } |
| 165 |