<?php

  // Namespace
  namespace BMI\Plugin\Progress;

  // Use
  use BMI\Plugin\BMI_Logger AS Logger;

  // Exit on direct access
  if (!defined('ABSPATH')) exit;

  /**
   * Main File Backup Logs
   */
  class BMI_ZipProgress {

    public $name;
    public $date;
    public $millis;
    public $cron;
    public $logfilename;
    public $latest;
    public $latest_progress;
    public $files;
    public $bytes;
    public $total_queries;
    public $file;
    public $progress;
    public $muted = false;
    public $clearEndCodes = false;

    public function __construct($backup_name, $files = 0, $bytes = 0, $cron = false, $reset = true, $clearEndCodes = false) {

      if (!file_exists(BMI_BACKUPS)) mkdir(BMI_BACKUPS, 0755, true);

      $this->name = $backup_name;
      $this->date = date('Y-m-d H:i:s');
      $this->millis = microtime(true);
      $this->cron = $cron;
      $this->logfilename = substr($backup_name, 0, -4) . '.log';
      $this->latest = BMI_BACKUPS . '/latest.' . BMI_LOGS_SUFFIX . '.log';
      $this->latest_progress = BMI_BACKUPS . '/latest_progress.' . BMI_LOGS_SUFFIX . '.log';
      $this->files = $files;
      $this->bytes = $bytes;
      $this->total_queries = 1;
      $this->clearEndCodes = $clearEndCodes;

      if ($reset == true) {
        if (file_exists($this->latest)) @unlink($this->latest);
        if (file_exists($this->latest_progress)) @unlink($this->latest_progress);
        file_put_contents($this->latest_progress, '0/100');
      }
      
      if ($this->clearEndCodes) {
        $this->removeAllEndCodes();
      }

    }

    public function createManifest($dbBackupEngine = 'v4') {

      global $table_prefix;

      $isBackupStreamed = apply_filters('bmip_streaming_backup_is_enabled', false);


      $manifest = array(
        'name' => $this->name,
        'date' => $this->date,
        'files' => $this->files,
        'bytes' => $this->bytes,
        'cron' => $this->cron,
        'total_queries' => $this->total_queries,
        'manifest' => date('Y-m-d H:i:s'),
        'millis_start' => $this->millis,
        'millis_end' => microtime(true),
        'version' => (defined('BMI_VERSION') ? BMI_VERSION : ''),
        'domain' => parse_url(home_url())['host'],
        'dbdomain' => get_option('siteurl'),
        'uid' => get_current_user_id(),
        'source_query_output' => (defined('BMI_DB_MAX_ROWS_PER_QUERY') ? BMI_DB_MAX_ROWS_PER_QUERY : ''),
        'db_backup_engine' => $dbBackupEngine,
        'multisite' => (defined('MULTISITE') ? MULTISITE : 'false'),
        'config' => array(
          'ABSPATH' => ABSPATH,
          'DB_NAME' => (defined('DB_NAME') ? DB_NAME : ''),
          'DB_USER' => (defined('DB_USER') ? DB_USER : ''),
          'DB_PASSWORD' => (defined('DB_PASSWORD') ? DB_PASSWORD : ''),
          'DB_HOST' => (defined('DB_HOST') ? DB_HOST : ''),
          'DB_CHARSET' => (defined('DB_CHARSET') ? DB_CHARSET : ''),
          'DB_COLLATE' => (defined('DB_COLLATE') ? DB_COLLATE : ''),
          'AUTH_KEY' => (defined('AUTH_KEY') ? AUTH_KEY : ''),
          'SECURE_AUTH_KEY' => (defined('SECURE_AUTH_KEY') ? SECURE_AUTH_KEY : ''),
          'LOGGED_IN_KEY' => (defined('LOGGED_IN_KEY') ? LOGGED_IN_KEY : ''),
          'NONCE_KEY' => (defined('NONCE_KEY') ? NONCE_KEY : ''),
          'AUTH_SALT' => (defined('AUTH_SALT') ? AUTH_SALT : ''),
          'SECURE_AUTH_SALT' => (defined('SECURE_AUTH_SALT') ? SECURE_AUTH_SALT : ''),
          'LOGGED_IN_SALT' => (defined('LOGGED_IN_SALT') ? LOGGED_IN_SALT : ''),
          'NONCE_SALT' => (defined('NONCE_SALT') ? NONCE_SALT : ''),
          'WP_DEBUG_LOG' => (defined('WP_DEBUG_LOG') ? WP_DEBUG_LOG : ''),
          'WP_CONTENT_URL' => (defined('WP_CONTENT_URL') ? WP_CONTENT_URL : ''),
          'WP_CONTENT_DIR' => (defined('WP_CONTENT_DIR') ? trailingslashit(WP_CONTENT_DIR) : ''),
          'table_prefix' => $table_prefix
        )
      );

      if ($isBackupStreamed) {
        $manifest['is_streamed'] = true;
        $manifest['chained_hash_chunk_size'] = (defined('CHAINED_HASH_CHUNK_SIZE') ? CHAINED_HASH_CHUNK_SIZE : 5 * 10485760); // 50MB by default
        $manifest['is_locked'] = $this->cron ? 'unlocked' : 'locked';
      }

      return json_encode($manifest);

    }

    public function start($muted = false) {

      $this->muted = $muted;

    }

    public function log($log = '', $level = 'INFO') {

      if (!$this->muted) {
        $this->file = fopen($this->latest, 'a');
        if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) {
          $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] [CLI] ' . $log . "\n";
        } else {
          $log_string = '[' . strtoupper($level) . '] [' . date('Y-m-d H:i:s') . '] ' . $log . "\n";
        }
        fwrite($this->file, $log_string);
        fclose($this->file);
        if (defined('BMI_USING_CLI_FUNCTIONALITY') && BMI_USING_CLI_FUNCTIONALITY === true) {
          echo esc_html( $log_string );
        }
      }

    }

    public function progress($progress = '0') {

      $this->progress = fopen($this->latest_progress, 'w') or die(esc_html__("Unable to open file!", 'backup-backup'));
      fwrite($this->progress, $progress);
      fclose($this->progress);

    }

    public function removeAllEndCodes() {
      
      $logs = file_get_contents($this->latest);
      $logs = explode("\n", $logs);
      
      foreach ($logs as $line => $string) {
        if (strpos($string, '[END-CODE]') !== false) {
          $code = substr($string, 10);
          $logs[$line] = "[VERBOSE] Logs from there starts as new process, it was continued by smart solution system after applying new plugin settings. (" . $code . ")";
        }
      }
      
      $logs = implode("\n", $logs);
      file_put_contents($this->latest, $logs);
      file_put_contents($this->latest_progress, '0/100');
      
    }

    public function end() {

      // fclose($this->file);

    }

  }
