コード例 #1
0
/**
 * Verify that destination files can be overwritten
 *
 * @param string source directory
 * @param string destination directory
 * @param string action name
 * @param boolean overwrite
 * @param array read only file list
 */
function verify_overwrite($src, $dest, $action = '', $overwrite = true, &$read_only_list)
{
    global $basepath;
    /**
     * Result of this function is FALSE when some error was detected
     * @var boolean
     */
    $result = true;
    $dir = opendir($src);
    if ($dir === false) {
        // $dir is not a valid directory or it can not be opened due to permission restrictions
        echo '<div class="red">The &laquo;' . htmlspecialchars($src) . '&raquo; is not a valid direcotry or the directory can not be opened due to permission restrictions or filesystem errors.</div>';
        return false;
    }
    $dir_list = array();
    $file_list = array();
    while (false !== ($file = readdir($dir))) {
        if ($file != '.' && $file != '..') {
            $srcfile = $src . '/' . $file;
            $destfile = $dest . '/' . $file;
            if (isset($read_only_list) && file_exists($destfile) && !is_writable($destfile)) {
                // Folder or file is not writable
                $read_only_list[] = $destfile;
            }
            if (is_dir($srcfile)) {
                $dir_list[$srcfile] = $destfile;
            } elseif ($overwrite) {
                // Add to overwrite
                $file_list[$srcfile] = $destfile;
            }
        }
    }
    $config_ignore_files = get_upgrade_config('ignore');
    $config_softmove_files = get_upgrade_config('softmove');
    $config_forcemove_files = get_upgrade_config('forcemove');
    if (!empty($action) && $action == 'Copying') {
        // Display errors about config file or the unknown and incorrect commands from config file
        $config_has_errors = false;
        if (is_string($config_ignore_files)) {
            // Config file has some errors, but the upgrade should not fail because of that
            echo '<div class="red">' . $config_ignore_files . '</div>';
            $config_has_errors = true;
        } else {
            $config_unknown_commands = get_upgrade_config('unknown');
            $config_incorrect_commands = get_upgrade_config('incorrect');
            if (!empty($config_unknown_commands) && is_array($config_unknown_commands)) {
                // Unknown commands
                foreach ($config_unknown_commands as $config_unknown_command) {
                    echo '<div class="red">' . sprintf(T_('Unknown policy command: %s'), $config_unknown_command) . '</div>';
                }
                $config_has_errors = true;
            }
            if (!empty($config_incorrect_commands) && is_array($config_incorrect_commands)) {
                // Incorrect commands
                foreach ($config_incorrect_commands as $config_incorrect_command) {
                    echo '<div class="red">' . sprintf(T_('Incorrect policy command: %s'), $config_incorrect_command) . '</div>';
                }
                $config_has_errors = true;
            }
        }
        if ($config_has_errors) {
            // The upgrade config file contains the errors, Stop the upgrading process
            echo '<div class="red">' . sprintf(T_('To continue the upgrade process please fix the issues of the file %s or delete it.'), '&laquo;<b>upgrade_policy.conf</b>&raquo;') . '</div>';
            return false;
        }
    }
    foreach ($dir_list as $src_dir => $dest_dir) {
        $dest_dir_name = str_replace($basepath, '', $dest_dir);
        // Detect if we should ignore this folder
        $ignore_dir = $overwrite && is_array($config_ignore_files) && in_array($dest_dir_name, $config_ignore_files);
        $dir_success = false;
        if (!empty($action)) {
            if ($ignore_dir) {
                // Ignore folder
                echo '<div class="orange">' . sprintf(T_('Ignoring %s because of upgrade_policy.conf'), '&laquo;<b>' . $dest_dir . '</b>&raquo;') . '</div>';
            } else {
                // progressive display of what backup is doing
                echo $action . ' &laquo;<strong>' . $dest_dir . '</strong>&raquo;...';
                $dir_success = true;
            }
            evo_flush();
        } elseif ($ignore_dir) {
            // This subfolder must be ingored, Display message about this
            echo '<div class="orange">' . sprintf(T_('Ignoring %s because of upgrade_policy.conf'), '&laquo;<b>' . $dest_dir_name . '</b>&raquo;') . '</div>';
            $dir_success = false;
            evo_flush();
        }
        if ($ignore_dir) {
            // Skip the ignored folder
            continue;
        }
        if ($overwrite && !file_exists($dest_dir)) {
            // Create destination directory
            if (!evo_mkdir($dest_dir)) {
                // No permission to create a folder
                echo '<div class="red">' . sprintf(T_('Unavailable creating of folder %s, probably no permissions.'), '&laquo;<b>' . $dest_dir_name . '</b>&raquo;') . '</div>';
                $result = false;
                $dir_success = false;
                evo_flush();
                continue;
            }
        }
        if ($dir_success) {
            echo ' OK.<br />';
            evo_flush();
        }
        $result = $result && verify_overwrite($src_dir, $dest_dir, '', $overwrite, $read_only_list);
    }
    foreach ($file_list as $src_file => $dest_file) {
        // Overwrite destination file
        $dest_file_name = str_replace($basepath, '', $dest_file);
        if (is_array($config_ignore_files) && in_array($dest_file_name, $config_ignore_files)) {
            // Ignore this file
            echo '<div class="orange">' . sprintf(T_('Ignoring %s because of upgrade_policy.conf'), '&laquo;<b>' . $dest_file_name . '</b>&raquo;') . '</div>';
            evo_flush();
            continue;
        }
        if (is_array($config_softmove_files) && !empty($config_softmove_files[$dest_file_name])) {
            // Action 'softmove': This file should be copied to other location with saving old file
            $copy_file_name = $config_softmove_files[$dest_file_name];
            // Don't rewrite old file
            $rewrite_old_file = false;
        }
        if (is_array($config_forcemove_files) && !empty($config_forcemove_files[$dest_file_name])) {
            // Action 'forcemove': This file should be copied to other location with rewriting old file
            $copy_file_name = $config_forcemove_files[$dest_file_name];
            // Rewrite old file
            $rewrite_old_file = true;
        }
        if (!empty($copy_file_name)) {
            // This file is marked in config to copy to other location
            $copy_file = $basepath . $copy_file_name;
            if (!$rewrite_old_file && file_exists($copy_file)) {
                // Display warning if we cannot rewrite an existing file
                echo '<div class="orange">' . sprintf(T_('Ignoring softmove of %s because %s is already in place (see upgrade_policy.conf)'), '&laquo;<b>' . $dest_file_name . '</b>&raquo;', '&laquo;<b>' . $copy_file_name . '</b>&raquo;') . '</div>';
                evo_flush();
                unset($copy_file_name);
                continue;
                // Skip this file
            } else {
                // We can copy this file to other location
                echo '<div class="orange">' . sprintf(T_('Moving %s to %s as stated in upgrade_policy.conf'), '&laquo;<b>' . $dest_file_name . '</b>&raquo;', '&laquo;<b>' . $copy_file_name . '</b>&raquo;') . '</div>';
                evo_flush();
                // Set new location for a moving file
                $dest_file = $copy_file;
                $dest_file_name = $copy_file_name;
                unset($copy_file_name);
            }
        }
        // Copying
        if (!@copy($src_file, $dest_file)) {
            // Display error if a copy command is unavailable
            echo '<div class="red">' . sprintf(T_('Unavailable copying to %s, probably no permissions.'), '&laquo;<b>' . $dest_file_name . '</b>&raquo;') . '</div>';
            $result = false;
            evo_flush();
        }
    }
    closedir($dir);
    return $result;
}
コード例 #2
0
ファイル: upgrade.ctrl.php プロジェクト: ldanielz/uesp.blog
 if ($success && empty($read_only_list)) {
     // We can backup files and database
     // Load Backup class (PHP4) and backup all of the folders and files
     load_class('maintenance/model/_backup.class.php', 'Backup');
     $Backup = new Backup();
     $Backup->include_all();
     if (!function_exists('gzopen')) {
         $Backup->pack_backup_files = false;
     }
     // Start backup
     if ($success = $Backup->start_backup()) {
         // We can upgrade files and database
         // Copying new folders and files
         echo '<h4>' . T_('Copying new folders and files...') . '</h4>';
         evo_flush();
         $success = verify_overwrite($upgrade_folder_path, no_trailing_slash($basepath), 'Copying', true, $read_only_list);
         if (!$success || !empty($read_only_list)) {
             // In case if something was changed before the previous verify_overwrite check
             echo '<p style="color:red"><strong>' . T_('The files and database backup was created successfully but all folders and files could not be overwritten');
             if (empty($read_only_list)) {
                 // There was some error in the verify_overwrite() function, but the corresponding error message was already displayed.
                 echo '.</strong></p>';
             } else {
                 // Some file/folder could not be overwritten, display it
                 echo ':</strong></p>';
                 foreach ($read_only_list as $read_only_file) {
                     echo $read_only_file . '<br/>';
                 }
             }
             echo '<p style="color:red"><strong>' . sprintf(T_('Please restore the backup files from the &laquo;%s&raquo; package. The database was not changed.'), $backup_path) . '</strong></p>';
             evo_flush();