public static function inExcludes($excludes, $value)
 {
     if (empty($value)) {
         return false;
     }
     if (null != $excludes) {
         foreach ($excludes as $exclude) {
             if (MainWP_Helper::endsWith($exclude, '*')) {
                 if (MainWP_Helper::startsWith($value, substr($exclude, 0, strlen($exclude) - 1))) {
                     return true;
                 }
             } else {
                 if ($value == $exclude) {
                     return true;
                 } else {
                     if (MainWP_Helper::startsWith($value, $exclude . '/')) {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
 public function compare($b, $a)
 {
     $pathA = $a->__toString();
     $pathB = $b->__toString();
     $dirnameA = is_file($pathA) ? dirname($pathA) : $pathA;
     $dirnameB = is_file($pathB) ? dirname($pathB) : $pathB;
     //if both are in the same folder, first show the files, then the directories
     if (dirname($pathA) == dirname($pathB)) {
         if (is_file($pathA) && !is_file($pathB)) {
             return -1;
         } else {
             if (!is_file($pathA) && is_file($pathB)) {
                 return 1;
             }
         }
         return strcmp($pathA, $pathB);
     } else {
         if ($dirnameA == $dirnameB) {
             return strcmp($pathA, $pathB);
         } else {
             if (MainWP_Helper::startsWith($dirnameA, $dirnameB)) {
                 return 1;
             } else {
                 if (MainWP_Helper::startsWith($dirnameB, $dirnameA)) {
                     return -1;
                 } else {
                     $cmp = strcmp($dirnameA, $dirnameB);
                     if (0 == $cmp) {
                         return strcmp($pathA, $pathB);
                     }
                     return $cmp;
                 }
             }
         }
     }
 }
 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 createZipPclFullBackup2($filepath, $excludes, $addConfig, $includeCoreFiles, $excludezip, $excludenonwp)
 {
     //Create backup folder
     $backupFolder = dirname($filepath) . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR;
     // @codingStandardsIgnoreStart
     @mkdir($backupFolder);
     // @codingStandardsIgnoreEnd
     //Create DB backup
     $db_files = $this->createBackupDB($backupFolder . 'dbBackup');
     //Copy installation to backup folder
     $nodes = glob(ABSPATH . '*');
     if (!$includeCoreFiles) {
         $coreFiles = array('favicon.ico', 'index.php', 'license.txt', 'readme.html', 'wp-activate.php', 'wp-app.php', 'wp-blog-header.php', 'wp-comments-post.php', 'wp-config.php', 'wp-config-sample.php', 'wp-cron.php', 'wp-links-opml.php', 'wp-load.php', 'wp-login.php', 'wp-mail.php', 'wp-pass.php', 'wp-register.php', 'wp-settings.php', 'wp-signup.php', 'wp-trackback.php', 'xmlrpc.php');
         foreach ($nodes as $key => $node) {
             if (MainWP_Helper::startsWith($node, ABSPATH . WPINC)) {
                 unset($nodes[$key]);
             } else {
                 if (MainWP_Helper::startsWith($node, ABSPATH . basename(admin_url('')))) {
                     unset($nodes[$key]);
                 } else {
                     foreach ($coreFiles as $coreFile) {
                         if (ABSPATH . $coreFile == $node) {
                             unset($nodes[$key]);
                         }
                     }
                 }
             }
         }
         unset($coreFiles);
     }
     $this->copy_dir($nodes, $excludes, $backupFolder, $excludenonwp, true);
     // to fix bug wrong folder
     // @codingStandardsIgnoreStart
     foreach ($db_files as $db_file) {
         @copy($db_file, $backupFolder . basename(WP_CONTENT_DIR) . '/' . basename($db_file));
         @unlink($db_file);
     }
     // @codingStandardsIgnoreEnd
     unset($nodes);
     //Zip this backup folder..
     require_once ABSPATH . 'wp-admin/includes/class-pclzip.php';
     $this->zip = new PclZip($filepath);
     $this->zip->create($backupFolder, PCLZIP_OPT_REMOVE_PATH, $backupFolder);
     if ($addConfig) {
         global $wpdb;
         $string = base64_encode(serialize(array('siteurl' => get_option('siteurl'), 'home' => get_option('home'), 'abspath' => ABSPATH, 'prefix' => $wpdb->prefix, 'lang' => WPLANG)));
         $this->addFileFromStringToPCLZip('clone/config.txt', $string, $filepath);
     }
     //Remove backup folder
     MainWP_Helper::delete_dir($backupFolder);
     return true;
 }