Esempio n. 1
0
 public static function cloneBackupDownload()
 {
     try {
         if (!isset($_POST['file'])) {
             throw new Exception(__('No download link given', 'mainwp-child'));
         }
         //            if (!isset($_POST['siteId'])) throw new Exception(__('No site given','mainwp-child'));
         $file = $_POST['file'];
         if (isset($_POST['siteId'])) {
             $siteId = $_POST['siteId'];
             $sitesToClone = get_option('mainwp_child_clone_sites');
             if (!is_array($sitesToClone) || !isset($sitesToClone[$siteId])) {
                 throw new Exception(__('Site not found', 'mainwp-child'));
             }
             $siteToClone = $sitesToClone[$siteId];
             $url = $siteToClone['url'];
             $key = $siteToClone['extauth'];
             $url = trailingslashit($url) . '?cloneFunc=dl&key=' . urlencode($key) . '&f=' . $file;
         } else {
             $url = $file;
         }
         MainWP_Helper::endSession();
         //Send request to the childsite!
         $split = explode('=', $file);
         $file = urldecode($split[count($split) - 1]);
         $filename = 'download-' . basename($file);
         $dirs = MainWP_Helper::getMainWPDir('backup', false);
         $backupdir = $dirs[0];
         if ($dh = opendir($backupdir)) {
             while (($file = readdir($dh)) !== false) {
                 if ('.' !== $file && '..' !== $file && MainWP_Helper::isArchive($file, 'download-')) {
                     @unlink($backupdir . $file);
                 }
             }
             closedir($dh);
         }
         $filename = $backupdir . $filename;
         $response = wp_remote_get($url, array('timeout' => 300000, 'stream' => true, 'filename' => $filename));
         if (is_wp_error($response)) {
             unlink($filename);
             return $response;
         }
         if (200 !== (int) wp_remote_retrieve_response_code($response)) {
             unlink($filename);
             return new WP_Error('http_404', trim(wp_remote_retrieve_response_message($response)));
         }
         $output = array('done' => $filename);
         //Delete backup on child
         try {
             if (isset($_POST['siteId'])) {
                 $siteId = $_POST['siteId'];
                 $sitesToClone = get_option('mainwp_child_clone_sites');
                 if (is_array($sitesToClone) && isset($sitesToClone[$siteId])) {
                     $siteToClone = $sitesToClone[$siteId];
                     MainWP_Helper::fetchUrl($siteToClone['url'], array('cloneFunc' => 'deleteCloneBackup', 'key' => $siteToClone['extauth'], 'f' => $_POST['file']));
                 }
             }
         } catch (Exception $e) {
             throw $e;
         }
     } catch (Exception $e) {
         $output = array('error' => $e->getMessage());
     }
     die(json_encode($output));
 }