function unlink_dir($dir)
{
    global $filesystem;
    $dir = $dir . "/";
    $d = dir($dir);
    while (false !== ($entry = $d->read())) {
        if (is_subdir($dir . $entry)) {
            unlink_dir($dir . $entry);
        }
        if (file_exists($dir . $entry) && is_file($dir . $entry) && filemtime($dir . $entry) < time() - 60 * 60) {
            $filesystem->unlink($dir . $entry);
        }
    }
    $d->close();
}
示例#2
0
 /**
  * Initiates the backup procedure.
  *
  * @global string  $wp_version WordPress version
  */
 public function do_backup($id = '')
 {
     global $wp_version;
     $used_memory = memory_get_usage(true);
     // Get the memory usage before we do anything.
     if ($id) {
         $this->log_file = $this->options['backup_list'][$id]['log'];
     } else {
         $id = base_convert($this->time, 10, 36);
         $this->log_file = $this->local_folder . '/' . $id . '.log';
         file_put_contents($this->log_file, "#Fields:\tdate\ttime\ttype\tmessage\n");
         $this->options['backup_list'][$id] = array('timestamp' => $this->time, 'title' => $this->options['backup_title'] . ' (' . $id . ')', 'file_path' => '', 'drive_id' => '', 'log' => $this->log_file, 'attempt' => 0);
         // Save info about our new backup now, so we can have access to the log file even if MySQL connection is stolen.
         update_option('backup_options', $this->options);
         // Log environment information.
         if (!($z = phpversion('zip'))) {
             $z = 'false';
         }
         $c = '';
         if (in_array('curl', $this->options['enabled_transports']) && function_exists('curl_version')) {
             $c = curl_version();
             $c = '; CURL ' . $c['version'];
         }
         $env = "Environment: Backup " . $this->version . "; WordPress " . $wp_version . "; PHP " . phpversion() . "; SAPI " . php_sapi_name() . "; OS " . PHP_OS . "; ZIP " . $z . $c;
         if ((bool) ini_get('safe_mode')) {
             $env .= "; Safe mode ON";
         }
         $env .= "; Time limit " . ini_get('max_execution_time') . "s" . "; Memory limit " . ini_get('memory_limit');
         $this->log("NOTICE", $env);
     }
     // We can't hook to WP's shudown hook because we need to pass the $id.
     register_shutdown_function(array(&$this, 'handle_timeout'), $id);
     $file_name = sanitize_file_name($this->options['backup_list'][$id]['title']) . '.zip';
     $file_path = $this->local_folder . '/' . $file_name;
     if (@is_file($file_path)) {
         $this->options['backup_list'][$id]['file_path'] = $file_path;
     }
     if (empty($this->options['backup_list'][$id]['file_path'])) {
         // Check if the backup folder is writable
         if (!@is_dir($this->local_folder) && !$this->create_dir($this->local_folder) || !@is_writable($this->local_folder)) {
             $this->log('ERROR', sprintf(__("The directory '%s' does not exist or is not writable.", $this->text_domain), esc_html($this->local_folder)));
             $this->reschedule_backup($id);
         }
         // Create database dump sql file.
         if (in_array('database', $this->options['source_list'])) {
             $this->log('NOTICE', __('Attempting to dump database.', $this->text_domain));
             if (is_wp_error($dump_time = db_dump($this->dump_file))) {
                 $this->log_wp_error($dump_time);
                 $this->reschedule_backup($id);
             }
             $this->log('NOTICE', sprintf(__('The database dump was completed successfully in %s seconds.', $this->text_domain), number_format_i18n($dump_time, 3)));
         }
         // Merge the default exclude list with the user provided one and make them absolute paths.
         $exclude = array_merge($this->options['exclude_list'], $this->exclude);
         foreach ($exclude as $i => $path) {
             if (false !== strpos($path, '/') || false !== strpos($path, "\\")) {
                 $exclude[$i] = absolute_path($path, ABSPATH);
             }
         }
         // Create the source list from the user selected sources.
         $sources = array();
         foreach ($this->options['source_list'] as $source) {
             $sources[] = $this->sources[$source]['path'];
         }
         // Remove subdirectories from the sources.
         $count = count($sources);
         for ($i = 0; $i < $count; $i++) {
             for ($j = 0; $j < $count; $j++) {
                 if ($j != $i && isset($sources[$i]) && isset($sources[$j]) && is_subdir($sources[$j], $sources[$i]) && $this->sources['database']['path'] != $sources[$j]) {
                     unset($sources[$j]);
                 }
             }
         }
         // Transform include paths to absolute paths.
         $include = $this->options['include_list'];
         foreach ($include as $i => $path) {
             $include[$i] = absolute_path($path, ABSPATH);
         }
         // Merge the include list and the sources
         $sources = array_merge($sources, $include);
         // Create archive from the sources list.
         $this->log('NOTICE', sprintf(__("Attempting to create archive '%s'.", $this->text_domain), esc_html($file_name)));
         if (!phpversion('zip')) {
             define('PCLZIP_TEMPORARY_DIR', $this->local_folder);
         }
         if (is_wp_error($zip = zip($sources, $file_path, $exclude))) {
             $this->log_wp_error($zip);
             delete_path($this->dump_file);
             $this->reschedule_backup($id);
         }
         delete_path($this->dump_file);
         $this->log('NOTICE', sprintf(__('Successfully archived %1$s files in %2$s seconds. Archive file size is %3$s.', $this->text_domain), number_format_i18n($zip['count']), number_format_i18n($zip['time'], 3), size_format(filesize($file_path), 2)));
         $this->options['backup_list'][$id]['file_path'] = $file_path;
     }
     if ($this->options['drive_number'] > 0 && $this->goauth->is_authorized()) {
         if (is_wp_error($e = $this->need_gdocs())) {
             $this->log_wp_error($e);
             $this->reschedule_backup($id);
         }
         if (empty($this->options['backup_list'][$id]['location'])) {
             $this->log('NOTICE', __("Attempting to upload archive to Google Drive.", $this->text_domain));
             $location = $this->gdocs->prepare_upload($this->options['backup_list'][$id]['file_path'], $this->options['backup_list'][$id]['title'], $this->options['drive_folder']);
         } else {
             $this->log('NOTICE', __('Attempting to resume upload.', $this->text_domain));
             $location = $this->gdocs->resume_upload($this->options['backup_list'][$id]['file_path'], $this->options['backup_list'][$id]['location']);
         }
         if (is_wp_error($location)) {
             $this->log_wp_error($location);
             $this->reschedule_backup($id);
         }
         if (is_string($location)) {
             $res = $location;
             $this->log('NOTICE', sprintf(__("Uploading file with title '%s'.", $this->text_domain), esc_html($this->options['backup_list'][$id]['title'])));
             $d = 0;
             echo '<div id="progress">';
             do {
                 $this->options['backup_list'][$id]['location'] = $res;
                 $res = $this->gdocs->upload_chunk();
                 $p = $this->gdocs->get_upload_percentage();
                 if ($p - $d >= 1) {
                     $b = intval($p - $d);
                     echo '<span style="width:' . $b . '%"></span>';
                     $d += $b;
                 }
                 $this->options['backup_list'][$id]['percentage'] = $p;
                 $this->options['backup_list'][$id]['speed'] = $this->gdocs->get_upload_speed();
             } while (is_string($res));
             echo '</div>';
             if (is_wp_error($res)) {
                 $this->log_wp_error($res);
                 $this->reschedule_backup($id);
             }
             $this->log('NOTICE', sprintf(__('The file was successfully uploaded to Google Drive in %1$s seconds at an upload speed of %2$s/s.', $this->text_domain), number_format_i18n($this->gdocs->time_taken(), 3), size_format($this->gdocs->get_upload_speed())));
             unset($this->options['backup_list'][$id]['location'], $this->options['backup_list'][$id]['attempt']);
         } elseif (true === $location) {
             $this->log('WARNING', sprintf(__("The file '%s' is already uploaded.", $this->text_domain), esc_html($this->options['backup_list'][$id]['file_path'])));
         }
         $this->options['backup_list'][$id]['drive_id'] = $this->gdocs->get_file_id();
         unset($this->options['backup_list'][$id]['percentage'], $this->options['backup_list'][$id]['speed']);
         $this->update_quota();
         if (empty($this->options['user_info'])) {
             $this->set_user_info();
         }
     }
     $this->options['backup_list'][$id]['status'] = 1;
     $this->purge_backups();
     $this->log('NOTICE', sprintf(__('Backup process completed in %1$s seconds.' . ' Initial PHP memory usage was %2$s and the backup process used another %3$s of RAM.', $this->text_domain), number_format_i18n(microtime(true) - $this->time, 3), size_format($used_memory, 2), size_format(memory_get_peak_usage(true) - $used_memory, 2)));
 }
示例#3
0
function process_upload()
{
    /*
    	Process the upload file
    */
    global $CFG;
    $ret = array();
    // Select the upload dir
    $upl_dir = $CFG->imgUploadDir;
    if (isset($_POST['dir']) && $_POST['dir'] !== '') {
        $upl_dir = realpath($CFG->imgUploadDir . $_POST['dir']);
        if ((file_exists($upl_dir) && is_dir($upl_dir) && is_subdir($CFG->imgUploadDir, $upl_dir) === true) === false) {
            $upl_dir = $CFG->imgUploadDir;
        }
    }
    $upl_dir = fix_path($upl_dir);
    // Create the list of uploaded files, support the one and couple files inputs as array (name like "file[1]")
    if (!is_array($_FILES['file']['name'])) {
        $upl_files[1] = $_FILES['file'];
    } else {
        $arr_len = count($_FILES['file']['name']);
        foreach ($_FILES['file'] as $key => $val) {
            $i = 1;
            foreach ($val as $v) {
                $upl_files[$i][$key] = $v;
                $i++;
            }
        }
    }
    // Process upload for all uploaded files
    foreach ($upl_files as $key => $upl_file) {
        // Allow process upload for new file in list
        $upload = true;
        // Fix the upload file name
        $upload_file = fix_name(strtolower(basename($upl_file['name'])));
        $file_ext = pathinfo($upload_file, PATHINFO_EXTENSION);
        // Get file name without the ext
        $name_wo_ext = empty($file_ext) ? $upload_file : substr($upload_file, 0, -(strlen($file_ext) + 1));
        // Get the target upload file path
        if (!empty($CFG->uploadNameFormat)) {
            $upload_file_path = $upl_dir . str_replace('n', $name_wo_ext, date($CFG->uploadNameFormat)) . '.' . $file_ext;
        } else {
            $upload_file_path = $upl_dir . $upload_file;
        }
        // Check if tagret file exist and create owerwrite is disabled - then grenerate the new file name
        if (!$CFG->overwriteFile && file_exists($upload_file_path)) {
            $upload_file_path = get_free_file_name($upload_file_path);
            // If can't get free file name - stop upload
            if ($upload_file_path === false) {
                $upload = false;
            }
        }
        // Check file extension
        if (!in_array($file_ext, $CFG->uploadExt)) {
            $upload = false;
        }
        // Get max upload file size
        $phpmaxsize = trim(ini_get('upload_max_filesize'));
        $last = strtolower($phpmaxsize[strlen($phpmaxsize) - 1]);
        switch ($last) {
            case 'g':
                $phpmaxsize *= 1024;
            case 'm':
                $phpmaxsize *= 1024;
            case 'k':
                $phpmaxsize *= 1024;
        }
        $cfgmaxsize = trim($CFG->maxUploadFileSize);
        $last = strtolower($cfgmaxsize[strlen($cfgmaxsize) - 1]);
        switch ($last) {
            case 'g':
                $cfgmaxsize *= 1024;
            case 'm':
                $cfgmaxsize *= 1024;
            case 'k':
                $cfgmaxsize *= 1024;
        }
        $cfgmaxsize = (int) $cfgmaxsize;
        // Check upload file size
        if ($cfgmaxsize > 0 && $upl_file['size'] > $cfgmaxsize || $upl_file['size'] > $phpmaxsize) {
            $upload = false;
        }
        // Check upload dir is writable
        if (!is_writable($upl_dir)) {
            $upload = false;
        }
        // If all OK then move upload file
        if ($upload) {
            move_uploaded_file($upl_file['tmp_name'], $upload_file_path);
            $ret[] = $upload_file_path;
            // Resize section
            if (isset($_POST['resize'][$key]) && $_POST['resize'][$key] !== '') {
                $newsize = $_POST['resize'][$key];
                settype($newsize, 'integer');
                $newsize = $newsize < 0 ? $newsize * -1 : $newsize;
                if ($newsize > $CFG->maxImgResize) {
                    $newsize = $CFG->maxImgResize;
                }
                if ($newsize > 0) {
                    if (!function_exists('resize_img')) {
                        require_once 'img_function.php';
                    }
                    if (function_exists('resize_img')) {
                        resize_img($upload_file_path, $upload_file_path, $newsize);
                    }
                }
            }
        } else {
        }
    }
    return $ret;
}
示例#4
0
// Image process change
if (isset($_POST['cmd']) && $_POST['cmd'] === 'edit' && isset($_POST['src']) && isset($_POST['dst'])) {
    // Normalize the varitable
    $src_img = (string) $_POST['src'];
    $dst_img = (string) $_POST['dst'];
    $flip_type = isset($_POST['flip']) ? (int) $_POST['flip'] : -1;
    $rotation_angle = isset($_POST['rotate']) ? (int) $_POST['rotate'] : 0;
    // Fix and check the path
    $src_img = realpath(str_replace($CFG->imgURL, $CFG->imgUploadDir, $src_img));
    // Only edit exist image
    $dst_img = $src_img;
    if (!in_array(strtolower(pathinfo($src_img, PATHINFO_EXTENSION)), $CFG->fileExt)) {
        echo 'File not allow to edit: ' . str_replace($CFG->imgUploadDir, $CFG->imgURL, $src_img) . "\n";
        exit;
    }
    if (!(file_exists($src_img) && is_file($src_img) && is_subdir($CFG->imgUploadDir, $src_img) === true)) {
        echo 'File not found: ' . str_replace($CFG->imgUploadDir, $CFG->imgURL, $src_img) . "\n";
        exit;
    }
    if (isset($_POST['resize'])) {
        // Input like "width,height"
        $resize_size = explode(',', $_POST['resize']);
        $resize_size = array_pad($resize_size, 2, 0);
        $resize_size[0] = (int) $resize_size[0];
        $resize_size[1] = (int) $resize_size[1];
        if ($resize_size[0] <= 0 && $resize_size[1] <= 0) {
            // Notfing to resize
            $resize_size = null;
        }
    }
    if (isset($_POST['crop'])) {