I have been having problems with WordPress Updates for a while. No error messages, just the WordPress Update, or WordPress Plugin Update, hangs.

Then I found out that the WordPress backup plugin I am using, BackWPUp, failed if I configured it to use any compression.

Then I found out that the problems I had been having with random-seeming 500 Server Errors was from the WP-Super-Cache plugin configured with, hmm, compression.

WordPress Updates would create new folders, but no files in them.

I wrote a PHP program, outside of WordPress, to test if the problem was in WordPress, or in the version of PHP used on my server.

The problem is in PHP. The PHP unzip routines lock up. No error message on screen, in the PHP log, or in the system log.

WordPress 3.6.0 and 3.6.1, Multi-Site, Apache, PHP Version 5.2.17, [HTTP_ACCEPT_ENCODING] => gzip, deflate

PHP itself has a bug in Zip/Unzip in old versions. Hangs without any error message displayed or in logs. Affects WordPress Update, and Plugin/Theme Updates, and any plugins that use compression (e.g. WP-Super-Cache or BackWPup)

PHP Change Log http://www.php.net/ChangeLog-5.php#5.3.4 says
PHP Version 5.3.4 released 09-Dec-2010 – Fixed crash in zip extract method

Note: PHP versions are numbered so version 5.3.10 is higher than 5.3.9 (5.3.1 is lower)


These PHP Unzip methods hang:
$zip->extractTo($path.'/folder') or copy("zip://".$zipFullPath.'#'.$filename, $destPath.$destName)

I have not found a direct PHP method of testing it, beyond observing the program hangs. Neither command returns a result, they just hang.

The .zip file is being opened, I can read the number of files in it, and read the file names. The .zip routines report the file is valid (as does 7-Zip on Windows), and the file is downloaded directly from WordPress.org, and other files hang the same way.

I recommend WordPress adding a test for PHP unzip routines working, and if not, use a WordPress substitute (the PHP unzip code from a newer version of PHP?).

Many of the “WordPress Update Hangs” problems reported for years, might disappear with this change.

Looks like this PHP bug might have been fixed in PHP Version 5.3.4 09-Dec-2010 – “Fixed crash in zip extract method” www.php.net/ChangeLog-5.php#5.3.4

My hosting account (LunarPages) already had PHP 5.3.24 (released 11-April-2013) installed, changing one line in my .htaccess file switched me to the newer version:
AddHandler application/x-httpd-php5 .php5 .php4 .php .php3 .php2 .phtml

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Unzip (for WordPress, etc. Updates)</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW, NOARCHIVE">
<?php 
/* extract contents of a zip file to the zip files folder */
 
/* "Error Notes:Premature end of script headers:" == Your program is not sending proper HTML page headers 
or changing folder permissions from 755 to 775 gives identical error */
*/
 
/* Call with URL like this:
http://www.glerner.com/junk/unzip-gl.php?wpfile=wordpress-3.6.1.zip 
*/
 
$docroot = getenv("DOCUMENT_ROOT");
$ip = getenv("REMOTE_ADDR"); 
 
$logfile = "$docroot/error_log";
ini_set("error_log" , $logfile);
ini_set("log_errors" , "1");
 
if ($ip == '12.34.56.78') {
	ini_set("display_errors","2");
	ini_set("display_startup_errors","1");
	ini_set("ignore_repeated_errors","0");
	ini_set("html_errors","1");
	error_reporting(E_ALL | E_NOTICE | E_STRICT);
} else {
	ini_set("display_errors","0");
	error_reporting(0); 
}
 
?>
<style>
code { display:inline; }
</style>
</HEAD>
 
<BODY>
<h1>George's UnZip</h1>
<?php 
echo "<H2>IP: $ip, PHP Version:" .phpversion()."</h2>";
 
$file = $_GET["wpfile"];
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zipFullPath = $path.'/'.pathinfo(realpath($file), PATHINFO_FILENAME).'.'.pathinfo(realpath($file), PATHINFO_EXTENSION);
$zipFolder = 'wordpress'; /* folder in the zip file, normally would extract to public_html/wordpress, I want to extract to public_html */
$folderNameLen = strlen($zipFolder) + 1;
 
$startDestFolder = '/';
 
echo "<p>Extracting the /$zipFolder/ folder of the zip file to $path</p>";
 
$groupName   = get_current_user();
$groupID = getmyuid();
echo "<p>Group ID of current process: $groupID, Name: $groupName</p>"; 
 
$zip = new ZipArchive;
$result = $zip->open($file, ZIPARCHIVE::CHECKCONS);
if ($result === TRUE) {
	echo "<h2>".$zip->numFiles." files total in zip file</h2>";
	$zip->close();
} else {
	echo "<h2>$file could not be opened</h2>";
	exit;
}
 
// If the archive is broken(or just another file renamed to *.zip) the function will return error on httpd under windows, so it's good to check if the archive is ok with ZIPARCHIVE::CHECKCONS 
if ($result === TRUE) {
	$zip = new ZipArchive;
	$result = $zip->open($file);
	echo "<p>";
	switch($result) {
		case TRUE : { echo " Opened Successfully, no errors in Zip file"; break; }
		case ZIPARCHIVE::ER_EXISTS : { echo " ER_EXISTS"; break; }
		case ZIPARCHIVE::ER_INCONS : { echo " ER_INCONS"; break; }
		case ZIPARCHIVE::ER_INVAL : { echo " ER_INVAL"; break; }
		case ZIPARCHIVE::ER_MEMORY : { echo " ER_MEMORY"; break; }
		case ZIPARCHIVE::ER_NOENT : { echo " ER_NOENT"; break; }
		case ZIPARCHIVE::ER_NOZIP : { echo " ER_NOZIP"; break; }
		case ZIPARCHIVE::ER_OPEN : { echo " ER_OPEN"; break; }
		case ZIPARCHIVE::ER_READ : { echo " ER_READ"; break; }
		case ZIPARCHIVE::ER_SEEK : { echo " ER_SEEK"; break; }
		default: echo " Unknown error";
	}
	echo "</p>";
 
echo "<h2>About to extract result via System Unzip, to $path/asdf (permission 744 works) :</h2>";
$execute = "unzip -u -a $zipFullPath $zipFolder/* -d asdf ";  /* ascii convert, update, directory for output*/
echo "<p>System command: $execute</p><code>";
$escaped_command = escapeshellcmd($execute);
system($escaped_command,$retval);
echo "</code><P>Retval: $retval</p>";
 
/*	echo "<p>About to extract same file via PHP zip->extractTo, to $path/asdf :</p>";
	// $result = $zip->extractTo($path.'/asdf');
	$result = $zip->extractTo($path);
	echo "<h2>Full extract result to $path/asdf : $result</h2>";
*/
 
function getFileOwnership($file){
global $docroot;
$owner = fileowner($file);
	$stat = stat($file);
	if($stat && (stristr($docroot,"xampp") == FALSE) ) {
		$group = posix_getgrgid($stat[5]);
		$user = posix_getpwuid($stat[4]);
		return compact('user', 'group');
	} else {
		return false;
	}
}
 
echo "<h2>About to extract same file via PHP Unzip & single file copy, to $path:</h2>";
echo "<p>(permission 755 fails, 775 gives 500 Server Error)</p>";
echo "<p>Note: Should extract same number of files, on some versions of PHP hangs before extracts a single file. Notice if says 'extracted', and notice if it says 'Done extracting'.</p>";
 
	// extract it to the path we determined above
    $files = array();
	for($i = 0; $i < $zip->numFiles; $i++) {
		$entry = $zip->getNameIndex($i);
		//Use strpos() to check if the entry name contains the directory we want to extract
		if (strpos($entry, "$zipFolder/") == 0 ) { /* remove starting wordpress folder, could be subfolders also called wordpress */
			//Add the entry to our array if it in in our desired directory
			$newEntry = substr_replace($entry, '', 0 , $folderNameLen);
			if ($newEntry == '') continue;
			// $files[] = $newEntry;
			// $result = $zip->extractTo($path, array($zip->getNameIndex($i)));	/* can't use newEntry, must use getNameIndex, in array */
 
			$filename = $zip->getNameIndex($i);
			$fileinfo = pathinfo($filename);
			$destFolder = substr_replace($fileinfo['dirname'], '', 0 , $folderNameLen) . '/';
			$destFolder = str_replace('//','/', $destFolder);
			$destName = $fileinfo['basename'];
			/* if ($destFolder !== $startDestFolder) { */
//			echo " DestFolder:$destFolder, Entry $entry, <code>$zipFullPath#$entry</code> to <code>Path:$path Folder:$destFolder Name:$destName</code> ";
			if (strrpos($entry,'/',0) === strlen($entry) - 1 ) {
				$destFolder = $fileinfo['dirname'].'/'.$fileinfo['basename'];
				$destFolder = substr_replace($destFolder, '', 0 , $folderNameLen);
				$newPath = $path.'/'.$destFolder; $newPath = str_replace('//','/', $newPath);
				if ( file_exists($newPath) && filetype($newPath) == "dir" ) {
					echo " <span style=\"color:green; background:white;\">directory $newPath already made</span>";
				} else {
					if (!mkdir($newPath, 0755, TRUE) ) {
						echo " <span style=\"color:red; background:white;\">Failed to create folders $newPath</span>";
					} else { 
						echo " <span style=\"color:green; background:white;\">New Folder $newPath Created</span>"; 
					}
				}
				echo " $newPath Permissions: ". substr(sprintf('%o', fileperms($newPath)), -4);
			} else {
				$newPath = $path.'/'.$destFolder; $newPath = str_replace('//','/', $newPath);
				$fileOwner = getFileOwnership($newPath);
				$fileOwnerUserName = $fileOwner['user']['name'];
				$fileOwnerUserID = $fileOwner['user']['gid'];
				$fileOwnerGroupName = $fileOwner['group']['name'];
				$fileOwnerGroupID = $fileOwner['group']['gid'];
				echo " <b>Extracting <code>$zipFullPath#$entry</code><br> to Path:<code>$newPath</b> (Permissions: ". substr(sprintf('%o', fileperms($newPath)), -4). ")</code> ";
				echo " Ownership: $fileOwnerUserName Group: $fileOwnerGroupName ID: $fileOwnerUserID GroupID: $fileOwnerGroupID";
				echo " <b>Name:<code>$destName</code></b>";
				$result = copy("zip://".$zipFullPath.'#'.$entry, $newPath.$destName);
				if ($result === TRUE) {
					echo " extracted ";
 
					$stat = $zip->statIndex($i);
					$oldCrc = $stat['crc']; 
					$newCrc = hexdec(hash_file("crc32b", $newPath.$destName)); 
					// Have to test both cases as the unsigned CRC from within the zip might appear negative as a signed int.
					if($newCrc !== $oldCrc && ($oldCrc + 4294967296) !== $newCrc) {
						echo "<span style=\"color:red; background:white;\">The files don't match!</span>";
					} else { echo " (match)"; }
				} else {
					echo " <span style=\"color:red; background:#eeeeee\">$entry NOT extracted</span> to ".$newPath.$destName;
				}
			}
		}
		echo "</p>";
	}
	echo "<br><p>Done extraction to $path</p>";
}
 
echo "</p>\n";
 
$result = $zip->close();
if ($result === TRUE) {
	echo "<p>Extraction successful, $file extracted to $path</p>";
} else {
	echo "<p>Error with $file. Error code: $result</p>";
}
 
/* 
PHP Change Log http://www.php.net/ChangeLog-5.php#5.5.4
PHP Version 5.3.4 released 09-Dec-2010 - Fixed crash in zip extract method (possible CWE-170).
PHP Version 5.2.17 released 06-Jan-2011
*/
 
?>
</BODY>
</HTML>

Pin It on Pinterest

Share This