MDL-15928 switched to zip PHP extensions

This commit is contained in:
skodak 2008-08-04 16:45:08 +00:00
parent 6b4d90cd58
commit ed94cb66c6
9 changed files with 84 additions and 181 deletions

View File

@ -249,11 +249,16 @@
<ON_CHECK message="xmlrpcrecommended" />
</FEEDBACK>
</PHP_EXTENSION>
<PHP_EXTENSION name="ctype" level="required">
<PHP_EXTENSION name="ctype" level="required">
<FEEDBACK>
<ON_ERROR message="ctyperequired" />
</FEEDBACK>
</PHP_EXTENSION>
<PHP_EXTENSION name="zip" level="required">
<FEEDBACK>
<ON_ERROR message="ziprequired" />
</FEEDBACK>
</PHP_EXTENSION>
</PHP_EXTENSIONS>
<CUSTOM_CHECKS>
<CUSTOM_CHECK file="lib/customcheckslib.php" function="php_check_register_globals" level="optional">

View File

@ -10,8 +10,6 @@ $temp = new admin_settingpage('systempaths', get_string('systempaths','admin'));
$temp->add(new admin_setting_configselect('gdversion', get_string('gdversion','admin'), get_string('configgdversion', 'admin'), check_gd_version(), array('0' => get_string('gdnot'),
'1' => get_string('gd1'),
'2' => get_string('gd2'))));
$temp->add(new admin_setting_configexecutable('zip', get_string('pathtozip','admin'), get_string('configzip', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('unzip', get_string('pathtounzip','admin'), get_string('configunzip', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('pathtodu', get_string('pathtodu', 'admin'), get_string('configpathtodu', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('aspellpath', get_string('aspellpath', 'admin'), get_string('edhelpaspellpath'), ''));
$ADMIN->add('server', $temp, 0);

View File

@ -473,12 +473,6 @@
fwrite ($bf,full_tag("DATE",2,false,$preferences->backup_unique_code));
//The original site wwwroot
fwrite ($bf,full_tag("ORIGINAL_WWWROOT",2,false,$CFG->wwwroot));
//The zip method used
if (!empty($CFG->zip)) {
$zipmethod = 'external';
} else {
$zipmethod = 'internal';
}
//Indicate if it includes external MNET users
$sql = "SELECT b.old_id
FROM {backup_ids} b
@ -488,7 +482,6 @@
if ($DB->record_exists_sql($sql, array($preferences->backup_unique_code, $CFG->mnet_localhost_id))) {
fwrite ($bf,full_tag("MNET_REMOTEUSERS",2,false,'true'));
}
fwrite ($bf,full_tag("ZIP_METHOD",2,false,$zipmethod));
//Te includes tag
fwrite ($bf,start_tag("DETAILS",2,true));
//Now, go to mod element of preferences to print its status

View File

@ -251,3 +251,4 @@ wwwroot
wwwrooterror
yourchoice
xmlrpcrecommended
ziprequired

View File

@ -783,5 +783,6 @@ $string['webproxy'] = 'Web proxy';
$string['webproxyinfo'] = 'Fill in following options if your Moodle server can not access internet directly. Internet access is required for download of environment data, language packs, RSS feeds, timezones, etc.<br /><em>PHP cURL extension is highly recommended.</em>';
$string['xmlrpcrecommended'] = 'Installing the optional xmlrpc extension is useful for Moodle Networking functionality.';
$string['xmlstrictheaders'] = 'XML strict headers';
$string['ziprequired'] = 'The Zip PHP extension is now required by Moodle, info-ZIP binaries or PclZip library are not used anymore.';
?>

View File

@ -566,6 +566,13 @@ function xmldb_main_upgrade($oldversion=0) {
upgrade_main_savepoint($result, 2008080400);
}
if ($result && $oldversion < 2008080401) {
// zip binaries not used anymore
unset_config('zip');
unset_config('unzip');
/// Main savepoint reached
upgrade_main_savepoint($result, 2008080401);
}
return $result;
}

View File

@ -389,6 +389,74 @@ function unzip_file($zipfile, $destination = '', $showstatus_ignored = true) {
return true;
}
/**
* Zip an array of files/dirs to a destination zip file
* Both parameters must be FULL paths to the files/dirs
*/
function zip_files ($originalfiles, $destination) {
global $CFG;
//Extract everything from destination
$path_parts = pathinfo(cleardoubleslashes($destination));
$destpath = $path_parts["dirname"]; //The path of the zip file
$destfilename = $path_parts["basename"]; //The name of the zip file
$extension = $path_parts["extension"]; //The extension of the file
//If no file, error
if (empty($destfilename)) {
return false;
}
//If no extension, add it
if (empty($extension)) {
$extension = 'zip';
$destfilename = $destfilename.'.'.$extension;
}
//Check destination path exists
if (!is_dir($destpath)) {
return false;
}
//Check destination path is writable. TODO!!
//Clean destination filename
$destfilename = clean_filename($destfilename);
//Now check and prepare every file
$files = array();
$origpath = NULL;
foreach ($originalfiles as $file) { //Iterate over each file
//Check for every file
$tempfile = cleardoubleslashes($file); // no doubleslashes!
//Calculate the base path for all files if it isn't set
if ($origpath === NULL) {
$origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
}
//See if the file is readable
if (!is_readable($tempfile)) { //Is readable
continue;
}
//See if the file/dir is in the same directory than the rest
if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
continue;
}
//Add the file to the array
$files[] = $tempfile;
}
$zipfiles = array();
$start = strlen($origpath)+1;
foreach($files as $file) {
$zipfiles[substr($file, $start)] = $file;
}
$packer = get_file_packer();
return $packer->zip_files_to_pathname($zipfiles, $destfilename);
}
/////////////////////////////////////////////////////////////
/// Old functions not used anymore - candidates for removal
/////////////////////////////////////////////////////////////

View File

@ -7227,176 +7227,6 @@ function cleardoubleslashes ($path) {
return preg_replace('/(\/|\\\){1,}/','/',$path);
}
function zip_files ($originalfiles, $destination) {
//Zip an array of files/dirs to a destination zip file
//Both parameters must be FULL paths to the files/dirs
global $CFG;
//Extract everything from destination
$path_parts = pathinfo(cleardoubleslashes($destination));
$destpath = $path_parts["dirname"]; //The path of the zip file
$destfilename = $path_parts["basename"]; //The name of the zip file
$extension = $path_parts["extension"]; //The extension of the file
//If no file, error
if (empty($destfilename)) {
return false;
}
//If no extension, add it
if (empty($extension)) {
$extension = 'zip';
$destfilename = $destfilename.'.'.$extension;
}
//Check destination path exists
if (!is_dir($destpath)) {
return false;
}
//Check destination path is writable. TODO!!
//Clean destination filename
$destfilename = clean_filename($destfilename);
//Now check and prepare every file
$files = array();
$origpath = NULL;
foreach ($originalfiles as $file) { //Iterate over each file
//Check for every file
$tempfile = cleardoubleslashes($file); // no doubleslashes!
//Calculate the base path for all files if it isn't set
if ($origpath === NULL) {
$origpath = rtrim(cleardoubleslashes(dirname($tempfile)), "/");
}
//See if the file is readable
if (!is_readable($tempfile)) { //Is readable
continue;
}
//See if the file/dir is in the same directory than the rest
if (rtrim(cleardoubleslashes(dirname($tempfile)), "/") != $origpath) {
continue;
}
//Add the file to the array
$files[] = $tempfile;
}
//Everything is ready:
// -$origpath is the path where ALL the files to be compressed reside (dir).
// -$destpath is the destination path where the zip file will go (dir).
// -$files is an array of files/dirs to compress (fullpath)
// -$destfilename is the name of the zip file (without path)
//print_object($files); //Debug
if (empty($CFG->zip)) { // Use built-in php-based zip function
include_once("$CFG->libdir/pclzip/pclzip.lib.php");
//rewrite filenames because the old method with PCLZIP_OPT_REMOVE_PATH does not work under win32
$zipfiles = array();
$start = strlen($origpath)+1;
foreach($files as $file) {
$tf = array();
$tf[PCLZIP_ATT_FILE_NAME] = $file;
$tf[PCLZIP_ATT_FILE_NEW_FULL_NAME] = substr($file, $start);
$zipfiles[] = $tf;
}
//create the archive
$archive = new PclZip(cleardoubleslashes("$destpath/$destfilename"));
if (($list = $archive->create($zipfiles) == 0)) {
notice($archive->errorInfo(true));
return false;
}
} else { // Use external zip program
$filestozip = "";
foreach ($files as $filetozip) {
$filestozip .= escapeshellarg(basename($filetozip));
$filestozip .= " ";
}
//Construct the command
$separator = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? ' &' : ' ;';
$command = 'cd '.escapeshellarg($origpath).$separator.
escapeshellarg($CFG->zip).' -r '.
escapeshellarg(cleardoubleslashes("$destpath/$destfilename")).' '.$filestozip;
//All converted to backslashes in WIN
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$command = str_replace('/','\\',$command);
}
Exec($command);
}
return true;
}
/**
* This function is used as callback in unzip_file() function
* to clean illegal characters for given platform and to prevent directory traversal.
* Produces the same result as info-zip unzip.
*/
function unzip_cleanfilename ($p_event, &$p_header) {
$p_header['filename'] = ereg_replace('[[:cntrl:]]', '', $p_header['filename']); //strip control chars first!
$p_header['filename'] = ereg_replace('\.\.+', '', $p_header['filename']); //directory traversal protection
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$p_header['filename'] = ereg_replace('[:*"?<>|]', '_', $p_header['filename']); //replace illegal chars
$p_header['filename'] = ereg_replace('^([a-zA-Z])_', '\1:', $p_header['filename']); //repair drive letter
} else {
//Add filtering for other systems here
// BSD: none (tested)
// Linux: ??
// MacosX: ??
}
$p_header['filename'] = cleardoubleslashes($p_header['filename']); //normalize the slashes/backslashes
return 1;
}
/**
* This function shows the results of the unzip execution
* depending of the value of the $CFG->zip, results will be
* text or an array of files.
*/
function unzip_show_status ($list,$removepath) {
global $CFG;
if (empty($CFG->unzip)) { // Use built-in php-based zip function
$strname = get_string("name");
$strsize = get_string("size");
$strmodified = get_string("modified");
$strstatus = get_string("status");
echo "<table width=\"640\">";
echo "<tr><th class=\"header\" scope=\"col\">$strname</th>";
echo "<th class=\"header\" align=\"right\" scope=\"col\">$strsize</th>";
echo "<th class=\"header\" align=\"right\" scope=\"col\">$strmodified</th>";
echo "<th class=\"header\" align=\"right\" scope=\"col\">$strstatus</th></tr>";
foreach ($list as $item) {
echo "<tr>";
$item['filename'] = str_replace(cleardoubleslashes($removepath).'/', "", $item['filename']);
print_cell("left", s($item['filename']));
if (! $item['folder']) {
print_cell("right", display_size($item['size']));
} else {
echo "<td>&nbsp;</td>";
}
$filedate = userdate($item['mtime'], get_string("strftimedatetime"));
print_cell("right", $filedate);
print_cell("right", $item['status']);
echo "</tr>";
}
echo "</table>";
} else { // Use external zip program
print_simple_box_start("center");
echo "<pre>";
foreach ($list as $item) {
echo s(str_replace(cleardoubleslashes($removepath.'/'), '', $item)).'<br />';
}
echo "</pre>";
print_simple_box_end();
}
}
/**
* Is current ip in give list?
* @param string $list

View File

@ -6,7 +6,7 @@
// This is compared against the values stored in the database to determine
// whether upgrades should be performed (see lib/db/*.php)
$version = 2008080400; // YYYYMMDD = date of the last version bump
$version = 2008080401; // YYYYMMDD = date of the last version bump
// XX = daily increments
$release = '2.0 dev (Build: 20080804)'; // Human-friendly version name