function uploadFile($file, $offset = 0)
 {
     $dirs = MainWP_Helper::getMainWPDir('backup');
     $backupdir = $dirs[0];
     header('Content-Description: File Transfer');
     header('Content-Description: File Transfer');
     if (MainWP_Helper::endsWith($file, '.tar.gz')) {
         header('Content-Type: application/x-gzip');
         header("Content-Encoding: gzip'");
     } else {
         header('Content-Type: application/octet-stream');
     }
     header('Content-Disposition: attachment; filename="' . basename($file) . '"');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($backupdir . $file));
     while (@ob_end_flush()) {
     }
     $this->readfile_chunked($backupdir . $file, $offset);
 }
 public static function validateMainWPDir()
 {
     $done = false;
     $dir = MainWP_Helper::getMainWPDir();
     $dir = $dir[0];
     if (MainWP_Helper::getWPFilesystem()) {
         global $wp_filesystem;
         try {
             MainWP_Helper::checkDir($dir, false);
         } catch (Exception $e) {
         }
         if (!empty($wp_filesystem)) {
             if ($wp_filesystem->is_writable($dir)) {
                 $done = true;
             }
         }
     }
     if (!$done) {
         if (!file_exists($dir)) {
             @mkdirs($dir);
         }
         if (is_writable($dir)) {
             $done = true;
         }
     }
     return $done;
 }
 public static function cloneBackupExtract()
 {
     try {
         MainWP_Helper::endSession();
         $file = isset($_POST['f']) ? $_POST['f'] : $_POST['file'];
         $testFull = false;
         if ('' === $file) {
             $dirs = MainWP_Helper::getMainWPDir('backup', false);
             $backupdir = $dirs[0];
             $files = glob($backupdir . 'download-*');
             $archiveFile = false;
             foreach ($files as $file) {
                 if (MainWP_Helper::isArchive($file, 'download-')) {
                     $archiveFile = $file;
                     break;
                 }
             }
             if (false === $archiveFile) {
                 throw new Exception(__('No download file found', 'mainwp-child'));
             }
             $file = $archiveFile;
         } else {
             if (file_exists($file)) {
                 $testFull = true;
             } else {
                 $file = ABSPATH . $file;
                 if (!file_exists($file)) {
                     throw new Exception(__('Backup file not found', 'mainwp-child'));
                 }
                 $testFull = true;
             }
         }
         //return size in kb
         $cloneInstall = new MainWP_Clone_Install($file);
         //todo: RS: refactor to get those plugins after install (after .18 release)
         $cloneInstall->readConfigurationFile();
         $plugins = get_option('mainwp_temp_clone_plugins');
         $themes = get_option('mainwp_temp_clone_themes');
         if ($testFull) {
             $cloneInstall->testDownload();
         }
         $cloneInstall->removeConfigFile();
         $cloneInstall->extractBackup();
         $pubkey = get_option('mainwp_child_pubkey');
         $uniqueId = get_option('mainwp_child_uniqueId');
         $server = get_option('mainwp_child_server');
         $nonce = get_option('mainwp_child_nonce');
         $nossl = get_option('mainwp_child_nossl');
         $nossl_key = get_option('mainwp_child_nossl_key');
         $sitesToClone = get_option('mainwp_child_clone_sites');
         $cloneInstall->install();
         $cloneInstall->updateWPConfig();
         //            $cloneInstall->update_option('mainwp_child_pubkey', $pubkey);
         //            $cloneInstall->update_option('mainwp_child_uniqueId', $uniqueId);
         //            $cloneInstall->update_option('mainwp_child_server', $server);
         //            $cloneInstall->update_option('mainwp_child_nonce', $nonce);
         //            $cloneInstall->update_option('mainwp_child_nossl', $nossl);
         //            $cloneInstall->update_option('mainwp_child_nossl_key', $nossl_key);
         //            $cloneInstall->update_option('mainwp_child_clone_sites', $sitesToClone);
         //            $cloneInstall->update_option('mainwp_child_clone_permalink', true);
         MainWP_Helper::update_option('mainwp_child_pubkey', $pubkey, 'yes');
         MainWP_Helper::update_option('mainwp_child_uniqueId', $uniqueId);
         MainWP_Helper::update_option('mainwp_child_server', $server);
         MainWP_Helper::update_option('mainwp_child_nonce', $nonce);
         MainWP_Helper::update_option('mainwp_child_nossl', $nossl, 'yes');
         MainWP_Helper::update_option('mainwp_child_nossl_key', $nossl_key);
         MainWP_Helper::update_option('mainwp_child_clone_sites', $sitesToClone);
         if (!MainWP_Helper::startsWith(basename($file), 'download-backup-')) {
             MainWP_Helper::update_option('mainwp_child_restore_permalink', true, 'yes');
         } else {
             MainWP_Helper::update_option('mainwp_child_clone_permalink', true, 'yes');
         }
         $cloneInstall->clean();
         if (false !== $plugins) {
             $out = array();
             if (is_array($plugins)) {
                 $dir = WP_CONTENT_DIR . '/plugins/';
                 $fh = @opendir($dir);
                 while ($entry = @readdir($fh)) {
                     if (!is_dir($dir . $entry)) {
                         continue;
                     }
                     if ('.' === $entry || '..' === $entry) {
                         continue;
                     }
                     if (!in_array($entry, $plugins)) {
                         MainWP_Helper::delete_dir($dir . $entry);
                     }
                 }
                 @closedir($fh);
             }
             delete_option('mainwp_temp_clone_plugins');
         }
         if (false !== $themes) {
             $out = array();
             if (is_array($themes)) {
                 $dir = WP_CONTENT_DIR . '/themes/';
                 $fh = @opendir($dir);
                 while ($entry = @readdir($fh)) {
                     if (!is_dir($dir . $entry)) {
                         continue;
                     }
                     if ('.' === $entry || '..' === $entry) {
                         continue;
                     }
                     if (!in_array($entry, $themes)) {
                         MainWP_Helper::delete_dir($dir . $entry);
                     }
                 }
                 @closedir($fh);
             }
             delete_option('mainwp_temp_clone_themes');
         }
         $output = array('result' => 'ok');
         //todo: remove old tables if other prefix?
         wp_logout();
         wp_set_current_user(0);
     } catch (Exception $e) {
         $output = array('error' => $e->getMessage());
     }
     //return size in kb
     die(json_encode($output));
 }
 public function clean()
 {
     $files = glob(WP_CONTENT_DIR . '/dbBackup*.sql');
     foreach ($files as $file) {
         @unlink($file);
     }
     if (file_exists(ABSPATH . 'clone/config.txt')) {
         @unlink(ABSPATH . 'clone/config.txt');
     }
     if (MainWP_Helper::is_dir_empty(ABSPATH . 'clone')) {
         @rmdir(ABSPATH . 'clone');
     }
     try {
         $dirs = MainWP_Helper::getMainWPDir('backup', false);
         $backupdir = $dirs[0];
         $files = glob($backupdir . '*');
         foreach ($files as $file) {
             if (MainWP_Helper::isArchive($file)) {
                 @unlink($file);
             }
         }
     } catch (Exception $e) {
     }
 }
 protected static function checkDirectoryMainWPDirectory($write = true)
 {
     $branding_title = 'MainWP';
     if (MainWP_Child_Branding::is_branding()) {
         $branding_title = MainWP_Child_Branding::get_branding();
     }
     $branding_title .= ' upload directory';
     try {
         $dirs = MainWP_Helper::getMainWPDir(null, false);
         $path = $dirs[0];
     } catch (Exception $e) {
         return self::renderDirectoryRow($branding_title, '', 'Writable', $e->getMessage(), false);
     }
     if (!is_dir(dirname($path))) {
         if ($write) {
             return self::renderDirectoryRow($branding_title, $path, 'Writable', 'Directory not found', false);
         } else {
             return false;
         }
     }
     $hasWPFileSystem = MainWP_Helper::getWPFilesystem();
     global $wp_filesystem;
     if ($hasWPFileSystem && !empty($wp_filesystem)) {
         if (!$wp_filesystem->is_writable($path)) {
             if ($write) {
                 return self::renderDirectoryRow($branding_title, $path, 'Writable', 'Directory not writable', false);
             } else {
                 return false;
             }
         }
     } else {
         if (!is_writable($path)) {
             if ($write) {
                 return self::renderDirectoryRow($branding_title, $path, 'Writable', 'Directory not writable', false);
             } else {
                 return false;
             }
         }
     }
     if ($write) {
         return self::renderDirectoryRow($branding_title, $path, 'Writable', 'Writable', true);
     } else {
         return true;
     }
 }
 /**
  * Create full backup
  */
 public function createFullBackup($excludes, $filePrefix = '', $addConfig = false, $includeCoreFiles = false, $file_descriptors = 0, $fileSuffix = false, $excludezip = false, $excludenonwp = false, $loadFilesBeforeZip = true, $ext = 'zip', $pid = false, $append = false)
 {
     $this->file_descriptors = $file_descriptors;
     $this->loadFilesBeforeZip = $loadFilesBeforeZip;
     $dirs = MainWP_Helper::getMainWPDir('backup');
     $backupdir = $dirs[0];
     if (!defined('PCLZIP_TEMPORARY_DIR')) {
         define('PCLZIP_TEMPORARY_DIR', $backupdir);
     }
     if (false !== $pid) {
         $pid = trailingslashit($backupdir) . 'backup-' . $pid . '.pid';
     }
     //Verify if another backup is running, if so, return an error
     $files = glob($backupdir . '*.pid');
     foreach ($files as $file) {
         if (basename($file) == basename($pid)) {
             continue;
         }
         if (time() - filemtime($file) < 160) {
             MainWP_Helper::error('Another backup process is running, try again later');
         }
     }
     $timestamp = time();
     if ('' !== $filePrefix) {
         $filePrefix .= '-';
     }
     if ('zip' == $ext) {
         $this->archiver = null;
         $ext = '.zip';
     } else {
         $this->archiver = new Tar_Archiver($this, $ext, $pid);
         $ext = $this->archiver->getExtension();
     }
     //        throw new Exception('Test 1 2 : ' . print_r($append,1));
     if (false !== $fileSuffix && !empty($fileSuffix)) {
         $file = $fileSuffix . (true === $append ? '' : $ext);
         //Append already contains extension!
     } else {
         $file = 'backup-' . $filePrefix . $timestamp . $ext;
     }
     $filepath = $backupdir . $file;
     $fileurl = $file;
     //        if (!$append)
     //        {
     //            if ($dh = opendir($backupdir))
     //            {
     //                while (($file = readdir($dh)) !== false)
     //                {
     //                    if ($file != '.' && $file != '..' && preg_match('/(.*).(zip|tar|tar.gz|tar.bz2|pid|done)$/', $file))
     //                    {
     //                        @unlink($backupdir . $file);
     //                    }
     //                }
     //                closedir($dh);
     //            }
     //        }
     if (!$addConfig) {
         if (!in_array(str_replace(ABSPATH, '', WP_CONTENT_DIR), $excludes) && !in_array('wp-admin', $excludes) && !in_array(WPINC, $excludes)) {
             $addConfig = true;
             $includeCoreFiles = true;
         }
     }
     $this->timeout = 20 * 60 * 60;
     /*20 minutes*/
     $mem = '512M';
     // @codingStandardsIgnoreStart
     @ini_set('memory_limit', $mem);
     @set_time_limit($this->timeout);
     @ini_set('max_execution_time', $this->timeout);
     // @codingStandardsIgnoreEnd
     if (null !== $this->archiver) {
         $success = $this->archiver->createFullBackup($filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp, $append);
     } else {
         if ($this->checkZipSupport()) {
             $success = $this->createZipFullBackup($filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp);
         } else {
             if ($this->checkZipConsole()) {
                 $success = $this->createZipConsoleFullBackup($filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp);
             } else {
                 $success = $this->createZipPclFullBackup2($filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp);
             }
         }
     }
     return $success ? array('timestamp' => $timestamp, 'file' => $fileurl, 'filesize' => filesize($filepath)) : false;
 }