function backup($pWrite = true)
 {
     $timeout = 20 * 60 * 60;
     //20minutes
     @set_time_limit($timeout);
     @ini_set('max_execution_time', $timeout);
     MainWP_Helper::endSession();
     //Cleanup pid files!
     $dirs = MainWP_Helper::getMainWPDir('backup');
     $backupdir = trailingslashit($dirs[0]);
     /** @var $wp_filesystem WP_Filesystem_Base */
     global $wp_filesystem;
     MainWP_Helper::getWPFilesystem();
     $files = glob($backupdir . '*');
     //Find old files (changes > 3 hr)
     foreach ($files as $file) {
         if (MainWP_Helper::endsWith($file, '/index.php') | MainWP_Helper::endsWith($file, '/.htaccess')) {
             continue;
         }
         if (time() - filemtime($file) > 60 * 60 * 3) {
             @unlink($file);
         }
     }
     $fileName = isset($_POST['fileUID']) ? $_POST['fileUID'] : '';
     if ('full' === $_POST['type']) {
         $excludes = isset($_POST['exclude']) ? explode(',', $_POST['exclude']) : array();
         $excludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/mainwp';
         $uploadDir = MainWP_Helper::getMainWPDir();
         $uploadDir = $uploadDir[0];
         $excludes[] = str_replace(ABSPATH, '', $uploadDir);
         $excludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/object-cache.php';
         if (function_exists('posix_uname')) {
             $uname = @posix_uname();
             if (is_array($uname) && isset($uname['nodename'])) {
                 if (stristr($uname['nodename'], 'hostgator')) {
                     if (!isset($_POST['file_descriptors']) || '0' == $_POST['file_descriptors'] || $_POST['file_descriptors'] > 1000) {
                         $_POST['file_descriptors'] = 1000;
                     }
                     $_POST['file_descriptors_auto'] = 0;
                     $_POST['loadFilesBeforeZip'] = false;
                 }
             }
         }
         $file_descriptors = isset($_POST['file_descriptors']) ? $_POST['file_descriptors'] : 0;
         $file_descriptors_auto = isset($_POST['file_descriptors_auto']) ? $_POST['file_descriptors_auto'] : 0;
         if (1 === (int) $file_descriptors_auto) {
             if (function_exists('posix_getrlimit')) {
                 $result = @posix_getrlimit();
                 if (isset($result['soft openfiles'])) {
                     $file_descriptors = $result['soft openfiles'];
                 }
             }
         }
         $loadFilesBeforeZip = isset($_POST['loadFilesBeforeZip']) ? $_POST['loadFilesBeforeZip'] : true;
         $newExcludes = array();
         foreach ($excludes as $exclude) {
             $newExcludes[] = rtrim($exclude, '/');
         }
         $excludebackup = isset($_POST['excludebackup']) && '1' == $_POST['excludebackup'];
         $excludecache = isset($_POST['excludecache']) && '1' == $_POST['excludecache'];
         $excludezip = isset($_POST['excludezip']) && '1' == $_POST['excludezip'];
         $excludenonwp = isset($_POST['excludenonwp']) && '1' == $_POST['excludenonwp'];
         if ($excludebackup) {
             //Backup buddy
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backupbuddy_backups';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backupbuddy_temp';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/pb_backupbuddy';
             //ManageWP
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/managewp';
             //InfiniteWP
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/infinitewp';
             //WordPress Backup to Dropbox
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //BackUpWordpress
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //BackWPUp
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/uploads/backwpup*';
             //WP Complete Backup
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/plugins/wp-complete-backup/storage';
             //WordPress EZ Backup
             //This one may be hard to do since they add random text at the end for example, feel free to skip if you need to
             ///backup_randomkyfkj where kyfkj is random
             //Online Backup for WordPress
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/backups';
             //XCloner
             $newExcludes[] = '/administrator/backups';
         }
         if ($excludecache) {
             //W3 Total Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/w3tc-cache';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/w3tc';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/config';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/minify';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/page_enhanced';
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/tmp';
             //WP Super Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/supercache';
             //Quick Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/quick-cache';
             //Hyper Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/hyper-cache/cache';
             //WP Fastest Cache
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/all';
             //WP-Rocket
             $newExcludes[] = str_replace(ABSPATH, '', WP_CONTENT_DIR) . '/cache/wp-rocket';
         }
         $file = false;
         if (isset($_POST['f'])) {
             $file = $_POST['f'];
         } else {
             if (isset($_POST['file'])) {
                 $file = $_POST['file'];
             }
         }
         $ext = 'zip';
         if (isset($_POST['ext'])) {
             $ext = $_POST['ext'];
         }
         $pid = false;
         if (isset($_POST['pid'])) {
             $pid = $_POST['pid'];
         }
         $append = isset($_POST['append']) && '1' == $_POST['append'];
         $res = MainWP_Backup::get()->createFullBackup($newExcludes, $fileName, true, true, $file_descriptors, $file, $excludezip, $excludenonwp, $loadFilesBeforeZip, $ext, $pid, $append);
         if (!$res) {
             $information['full'] = false;
         } else {
             $information['full'] = $res['file'];
             $information['size'] = $res['filesize'];
         }
         $information['db'] = false;
     } else {
         if ('db' == $_POST['type']) {
             $ext = 'zip';
             if (isset($_POST['ext'])) {
                 $ext = $_POST['ext'];
             }
             $res = $this->backupDB($fileName, $ext);
             if (!$res) {
                 $information['db'] = false;
             } else {
                 $information['db'] = $res['file'];
                 $information['size'] = $res['filesize'];
             }
             $information['full'] = false;
         } else {
             $information['full'] = false;
             $information['db'] = false;
         }
     }
     if ($pWrite) {
         MainWP_Helper::write($information);
     }
     return $information;
 }
 public static function remove_readme($force = false)
 {
     if ($force || self::get_security_option('readme')) {
         if (@file_exists(ABSPATH . 'readme.html')) {
             if (!@unlink(ABSPATH . 'readme.html')) {
                 MainWP_Helper::getWPFilesystem();
                 global $wp_filesystem;
                 if (!empty($wp_filesystem)) {
                     $wp_filesystem->delete(ABSPATH . 'readme.html');
                 }
             }
         }
     }
 }
 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 renderNormalRestore()
    {
        $uploadError = false;
        $uploadFile = false;
        if (isset($_REQUEST['upload'])) {
            if (isset($_FILES['file'])) {
                if (!function_exists('wp_handle_upload')) {
                    require_once ABSPATH . 'wp-admin/includes/file.php';
                }
                $uploadedfile = $_FILES['file'];
                $upload_overrides = array('test_form' => false);
                $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
                if ($movefile) {
                    $uploadFile = str_replace(ABSPATH, '', $movefile['file']);
                } else {
                    $uploadError = __('File could not be uploaded.', 'mainwp-child');
                }
            } else {
                $uploadError = __('File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.', 'mainwp-child');
            }
        }
        $uploadSizeInBytes = min(MainWP_Helper::return_bytes(ini_get('upload_max_filesize')), MainWP_Helper::return_bytes(ini_get('post_max_size')));
        $uploadSize = MainWP_Helper::human_filesize($uploadSizeInBytes);
        self::renderHeader();
        ?>
        <div id="icon-options-general" class="icon32"><br></div><h2>
		<strong><?php 
        esc_html_e('Option 1:', 'mainwp-child');
        ?>
</strong> <?php 
        esc_html_e('Restore', 'mainwp-child');
        ?>
</h2><?php 
        MainWP_Helper::getWPFilesystem();
        global $wp_filesystem;
        if (!empty($wp_filesystem) && !$wp_filesystem->is_writable(WP_CONTENT_DIR) || empty($wp_filesystem) && !is_writable(WP_CONTENT_DIR)) {
            echo '<div class="mainwp-child_info-box-red"><strong>' . esc_html__('Your content directory is not writable. Please set 0755 permission to ', 'mainwp-child') . esc_html(basename(WP_CONTENT_DIR)) . '. (' . esc_html(WP_CONTENT_DIR) . ')</strong></div>';
            $error = true;
        }
        ?>
        <div class="mainwp-child_info-box-green"
		     style="display: none;"><?php 
        esc_html_e('Restore process completed successfully! You will now need to click ', 'mainwp-child');
        ?>
			<a href="<?php 
        echo esc_attr(admin_url('options-permalink.php'));
        ?>
"><?php 
        esc_html_e('here', 'mainwp-child');
        ?>
</a><?php 
        esc_html_e(' to re-login to the admin and re-save permalinks.', 'mainwp-child');
        ?>
        </div>

		<?php 
        if ($uploadFile) {
            esc_html_e('Upload successful.', 'mainwp-child');
            ?>
 <a href="#" id="mainwp-child_uploadclonebutton"
                                                              class="button-primary"
			                                                  file="<?php 
            echo esc_attr($uploadFile);
            ?>
"><?php 
            esc_html_e('Restore Website', 'mainwp-child');
            ?>
</a><?php 
        } else {
            if ($uploadError) {
                ?>
				<div class="mainwp-child_info-box-red"><?php 
                echo esc_html($uploadError);
                ?>
</div><?php 
            }
            ?>
			<?php 
            esc_html_e('Upload backup in .zip format (Maximum filesize for your server settings: ', 'mainwp-child');
            echo esc_html($uploadSize);
            ?>
)
            <br/>
			<i><?php 
            esc_html_e('If you have a FULL backup created by your Network dashboard you may restore it by uploading here.', 'mainwp-child');
            ?>
                <br/>
				<?php 
            esc_html_e('A database only backup will not work.', 'mainwp-child');
            ?>
</i><br/><br/>
			<form action="<?php 
            echo esc_attr(admin_url('admin.php?page=MainWPRestore&upload=yes'));
            ?>
" method="post"
                  enctype="multipart/form-data"><input type="file" name="file" id="file"/> <input type="submit"
                                                                                                  name="submit"
                                                                                                  id="filesubmit"
                                                                                                  disabled="disabled"
			                                                                                      value="<?php 
            esc_html_e('Restore Website', 'mainwp-child');
            ?>
"/>
            </form>
			<?php 
        }
        self::renderCloneFromServer();
        self::renderJavaScript();
    }
 public function extractWPZipBackup()
 {
     MainWP_Helper::getWPFilesystem();
     global $wp_filesystem;
     $tmpdir = ABSPATH;
     if ('ftpext' === $wp_filesystem->method && defined('FTP_BASE')) {
         $ftpBase = FTP_BASE;
         $ftpBase = trailingslashit($ftpBase);
         $tmpdir = str_replace(ABSPATH, $ftpBase, $tmpdir);
     }
     unzip_file($this->file, $tmpdir);
     return true;
 }
 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;
     }
 }