zip.php
139 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 $muted = false; |
| 29 | |
| 30 | public function __construct($backup_name, $files = 0, $bytes = 0, $cron = false, $reset = true) { |
| 31 | |
| 32 | if (!file_exists(BMI_BACKUPS)) mkdir(BMI_BACKUPS, 0755, true); |
| 33 | |
| 34 | $this->name = $backup_name; |
| 35 | $this->date = date('Y-m-d H:i:s'); |
| 36 | $this->millis = microtime(true); |
| 37 | $this->cron = $cron; |
| 38 | $this->logfilename = substr($backup_name, 0, -4) . '.log'; |
| 39 | $this->latest = BMI_BACKUPS . '/latest.log'; |
| 40 | $this->latest_progress = BMI_BACKUPS . '/latest_progress.log'; |
| 41 | $this->files = $files; |
| 42 | $this->bytes = $bytes; |
| 43 | $this->total_queries = 1; |
| 44 | |
| 45 | if ($reset == true) { |
| 46 | if (file_exists($this->latest)) @unlink($this->latest); |
| 47 | if (file_exists($this->latest_progress)) @unlink($this->latest_progress); |
| 48 | file_put_contents($this->latest_progress, '0/100'); |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
| 53 | public function createManifest($dbBackupEngine = 'v4') { |
| 54 | |
| 55 | global $table_prefix; |
| 56 | |
| 57 | $manifest = array( |
| 58 | 'name' => $this->name, |
| 59 | 'date' => $this->date, |
| 60 | 'files' => $this->files, |
| 61 | 'bytes' => $this->bytes, |
| 62 | 'cron' => $this->cron, |
| 63 | 'total_queries' => $this->total_queries, |
| 64 | 'manifest' => date('Y-m-d H:i:s'), |
| 65 | 'millis_start' => $this->millis, |
| 66 | 'millis_end' => microtime(true), |
| 67 | 'version' => BMI_VERSION, |
| 68 | 'domain' => parse_url(home_url())['host'], |
| 69 | 'dbdomain' => get_option('siteurl'), |
| 70 | 'uid' => get_current_user_id(), |
| 71 | 'source_query_output' => BMI_DB_MAX_ROWS_PER_QUERY, |
| 72 | 'db_backup_engine' => $dbBackupEngine, |
| 73 | 'config' => array( |
| 74 | 'ABSPATH' => ABSPATH, |
| 75 | 'DB_NAME' => DB_NAME, |
| 76 | 'DB_USER' => DB_USER, |
| 77 | 'DB_PASSWORD' => DB_PASSWORD, |
| 78 | 'DB_HOST' => DB_HOST, |
| 79 | 'DB_CHARSET' => (defined('DB_CHARSET') ? DB_CHARSET : ''), |
| 80 | 'DB_COLLATE' => (defined('DB_COLLATE') ? DB_COLLATE : ''), |
| 81 | 'AUTH_KEY' => (defined('AUTH_KEY') ? AUTH_KEY : ''), |
| 82 | 'SECURE_AUTH_KEY' => (defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : ''), |
| 83 | 'LOGGED_IN_KEY' => (defined('LOGGED_IN_KEY') ? LOGGED_IN_KEY : ''), |
| 84 | 'NONCE_KEY' => (defined('NONCE_KEY') ? NONCE_KEY : ''), |
| 85 | 'AUTH_SALT' => (defined('AUTH_SALT') ? AUTH_SALT : ''), |
| 86 | 'SECURE_AUTH_SALT' => (defined('SECURE_AUTH_SALT') ? SECURE_AUTH_SALT : ''), |
| 87 | 'LOGGED_IN_SALT' => (defined('LOGGED_IN_SALT') ? LOGGED_IN_SALT : ''), |
| 88 | 'NONCE_SALT' => (defined('NONCE_SALT') ? NONCE_SALT : ''), |
| 89 | 'WP_DEBUG_LOG' => WP_DEBUG_LOG, |
| 90 | 'WP_CONTENT_URL' => WP_CONTENT_URL, |
| 91 | 'WP_CONTENT_DIR' => trailingslashit(WP_CONTENT_DIR), |
| 92 | 'table_prefix' => $table_prefix |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | return json_encode($manifest); |
| 97 | |
| 98 | } |
| 99 | |
| 100 | public function start($muted = false) { |
| 101 | |
| 102 | $this->muted = $muted; |
| 103 | |
| 104 | } |
| 105 | |
| 106 | public function log($log = '', $level = 'INFO') { |
| 107 | |
| 108 | if (!$this->muted) { |
| 109 | $this->file = fopen($this->latest, 'a'); |
| 110 | if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) { |
| 111 | $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] [CLI] ' . $log . "\n"; |
| 112 | } else { |
| 113 | $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] ' . $log . "\n"; |
| 114 | } |
| 115 | fwrite($this->file, $log_string); |
| 116 | fclose($this->file); |
| 117 | if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) { |
| 118 | echo $log_string; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | public function progress($progress = '0') { |
| 125 | |
| 126 | $this->progress = fopen($this->latest_progress, 'w') or die(__("Unable to open file!", 'backup-backup')); |
| 127 | fwrite($this->progress, $progress); |
| 128 | fclose($this->progress); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | public function end() { |
| 133 | |
| 134 | // fclose($this->file); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } |
| 139 |