Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The variable '$php_errormsg' is deprecated since PHP 7.2; Using error_get_last() instead #1144

Merged
merged 8 commits into from
Oct 5, 2019
6 changes: 4 additions & 2 deletions classes/phing/system/io/FileOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ class FileOutputStream extends OutputStream
*/
public function __construct($file, $append = false)
{
global $php_errormsg;
if ($file instanceof PhingFile) {
$this->file = $file;
} elseif (is_string($file)) {
$this->file = new PhingFile($file);
} else {
throw new Exception("Invalid argument type for \$file.");
}
error_clear_last();
if ($append) {
$stream = @fopen($this->file->getAbsolutePath(), "ab");
} else {
$stream = @fopen($this->file->getAbsolutePath(), "wb");
}
if ($stream === false) {
throw new IOException("Unable to open " . $this->file->__toString() . " for writing: " . $php_errormsg);
$lastError = error_get_last();
$errormsg = $lastError['message'];
throw new IOException("Unable to open " . $this->file->__toString() . " for writing: " . $errormsg);
}
parent::__construct($stream);
}
Expand Down
100 changes: 68 additions & 32 deletions classes/phing/system/io/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ public function getLastModifiedTime(PhingFile $f)
}

@clearstatcache();
error_clear_last();
$strPath = (string) $f->getPath();

if (@is_link($strPath)) {
Expand All @@ -275,7 +276,9 @@ public function getLastModifiedTime(PhingFile $f)
}

if (false === $mtime) {
$msg = "FileSystem::getLastModifiedTime() FAILED. Can not get modified time of $strPath. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::getLastModifiedTime() FAILED. Can not get modified time of $strPath. $errormsg";
throw new IOException($msg);
}

Expand All @@ -293,13 +296,16 @@ public function getLastModifiedTime(PhingFile $f)
*/
public function getLength(PhingFile $f)
{
error_clear_last();
$strPath = (string) $f->getAbsolutePath();
$fs = filesize((string) $strPath);
if ($fs !== false) {
return $fs;
}

$msg = "FileSystem::Read() FAILED. Cannot get filesize of $strPath. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::Read() FAILED. Cannot get filesize of $strPath. $errormsg";
throw new IOException($msg);
}

Expand Down Expand Up @@ -393,11 +399,14 @@ public function createDirectory(&$f, $mode = 0755)
*/
public function rename(PhingFile $f1, PhingFile $f2)
{
error_clear_last();
// get the canonical paths of the file to rename
$src = $f1->getAbsolutePath();
$dest = $f2->getAbsolutePath();
if (false === @rename($src, $dest)) {
$msg = "Rename FAILED. Cannot rename $src to $dest. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "Rename FAILED. Cannot rename $src to $dest. $errormsg";
throw new IOException($msg);
}
}
Expand All @@ -414,10 +423,13 @@ public function rename(PhingFile $f1, PhingFile $f2)
*/
public function setLastModifiedTime(PhingFile $f, $time)
{
error_clear_last();
$path = $f->getPath();
$success = @touch($path, $time);
if (!$success) {
throw new IOException("Could not touch '" . $path . "' due to: $php_errormsg");
$lastError = error_get_last();
$errormsg = $lastError['message'];
throw new IOException("Could not touch '" . $path . "' due to: $errormsg");
}
}

Expand Down Expand Up @@ -474,8 +486,6 @@ public function compare(PhingFile $f1, PhingFile $f2)
*/
public function copy(PhingFile $src, PhingFile $dest)
{
global $php_errormsg;

// Recursively copy a directory
if ($src->isDirectory()) {
$this->copyr($src->getAbsolutePath(), $dest->getAbsolutePath());
Expand All @@ -484,9 +494,12 @@ public function copy(PhingFile $src, PhingFile $dest)
$srcPath = $src->getAbsolutePath();
$destPath = $dest->getAbsolutePath();

error_clear_last();
if (false === @copy($srcPath, $destPath)) { // Copy FAILED. Log and return err.
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::copy() FAILED. Cannot copy $srcPath to $destPath. $php_errormsg";
// Add error from php to end of log message. $errormsg.
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::copy() FAILED. Cannot copy $srcPath to $destPath. $errormsg";
throw new IOException($msg);
}

Expand Down Expand Up @@ -552,8 +565,11 @@ public function copyr($source, $dest)
*/
public function chown($pathname, $user)
{
error_clear_last();
if (false === @chown($pathname, $user)) { // FAILED.
$msg = "FileSystem::chown() FAILED. Cannot chown $pathname. User $user." . (isset($php_errormsg) ? ' ' . $php_errormsg : "");
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::chown() FAILED. Cannot chown $pathname. User $user." . (isset($errormsg) ? ' ' . $errormsg : "");
throw new IOException($msg);
}
}
Expand All @@ -569,8 +585,11 @@ public function chown($pathname, $user)
*/
public function chgrp($pathname, $group)
{
error_clear_last();
if (false === @chgrp($pathname, $group)) { // FAILED.
$msg = "FileSystem::chgrp() FAILED. Cannot chown $pathname. Group $group." . (isset($php_errormsg) ? ' ' . $php_errormsg : "");
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::chgrp() FAILED. Cannot chown $pathname. Group $group." . (isset($errormsg) ? ' ' . $errormsg : "");
throw new IOException($msg);
}
}
Expand All @@ -588,9 +607,12 @@ public function chgrp($pathname, $group)
*/
public function chmod($pathname, $mode)
{
error_clear_last();
$str_mode = decoct($mode); // Show octal in messages.
if (false === @chmod($pathname, $mode)) { // FAILED.
$msg = "FileSystem::chmod() FAILED. Cannot chmod $pathname. Mode $str_mode." . (isset($php_errormsg) ? ' ' . $php_errormsg : "");
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::chmod() FAILED. Cannot chmod $pathname. Mode $str_mode." . (isset($errormsg) ? ' ' . $errormsg : "");
throw new IOException($msg);
}
}
Expand Down Expand Up @@ -641,9 +663,11 @@ public function unlock(PhingFile $f)
*/
public function unlink($file)
{
global $php_errormsg;
error_clear_last();
if (false === @unlink($file)) {
$msg = "FileSystem::unlink() FAILED. Cannot unlink '$file'. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::unlink() FAILED. Cannot unlink '$file'. $errormsg";
throw new IOException($msg);
}
}
Expand All @@ -661,12 +685,15 @@ public function unlink($file)
public function symlink($target, $link)
{

error_clear_last();
// If Windows OS then symlink() will report it is not supported in
// the build. Use this error instead of checking for Windows as the OS.

if (false === @symlink($target, $link)) {
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::Symlink() FAILED. Cannot symlink '$target' to '$link'. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
// Add error from php to end of log message.
$msg = "FileSystem::Symlink() FAILED. Cannot symlink '$target' to '$link'. $errormsg";
throw new IOException($msg);
}
}
Expand All @@ -681,17 +708,18 @@ public function symlink($target, $link)
*/
public function touch($file, $time = null)
{
global $php_errormsg;

error_clear_last();
if (null === $time) {
$error = @touch($file);
} else {
$error = @touch($file, $time);
}

if (false === $error) { // FAILED.
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::touch() FAILED. Cannot touch '$file'. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
// Add error from php to end of log message.
$msg = "FileSystem::touch() FAILED. Cannot touch '$file'. $errormsg";
throw new Exception($msg);
}
}
Expand All @@ -709,20 +737,24 @@ public function touch($file, $time = null)
*/
public function rmdir($dir, $children = false)
{
global $php_errormsg;
error_clear_last();

// If children=FALSE only delete dir if empty.
if (false === $children) {
if (false === @rmdir($dir)) { // FAILED.
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::rmdir() FAILED. Cannot rmdir $dir. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
// Add error from php to end of log message.
$msg = "FileSystem::rmdir() FAILED. Cannot rmdir $dir. $errormsg";
throw new Exception($msg);
}
} else { // delete contents and dir.
$handle = @opendir($dir);
$lastError = error_get_last();
$errormsg = $lastError['message'];

if (false === $handle) { // Error.
$msg = "FileSystem::rmdir() FAILED. Cannot opendir() $dir. $php_errormsg";
$msg = "FileSystem::rmdir() FAILED. Cannot opendir() $dir. $errormsg";
throw new Exception($msg);
}
// Read from handle.
Expand Down Expand Up @@ -764,9 +796,12 @@ public function rmdir($dir, $children = false)
// Don't error on closedir()
@closedir($handle);

error_clear_last();
if (false === @rmdir($dir)) { // FAILED.
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::rmdir() FAILED. Cannot rmdir $dir. $php_errormsg";
// Add error from php to end of log message.
$lastError = error_get_last();
$errormsg = $lastError['message'];
$msg = "FileSystem::rmdir() FAILED. Cannot rmdir $dir. $errormsg";
throw new Exception($msg);
}
}
Expand All @@ -784,17 +819,18 @@ public function rmdir($dir, $children = false)
*/
public function umask($mode)
{
global $php_errormsg;

error_clear_last();
// CONSIDERME:
// Throw a warning if mode is 0. PHP converts illegal octal numbers to
// 0 so 0 might not be what the user intended.

$str_mode = decoct($mode); // Show octal in messages.

if (false === @umask($mode)) { // FAILED.
// Add error from php to end of log message. $php_errormsg.
$msg = "FileSystem::Umask() FAILED. Value $mode. $php_errormsg";
$lastError = error_get_last();
$errormsg = $lastError['message'];
// Add error from php to end of log message.
$msg = "FileSystem::Umask() FAILED. Value $mode. $errormsg";
throw new Exception($msg);
}
}
Expand All @@ -818,18 +854,18 @@ public function compareMTimes($file1, $file2)
$mtime2 = filemtime($file2);

if ($mtime1 === false) { // FAILED. Log and return err.
// Add error from php to end of log message. $php_errormsg.
// Add error from php to end of log message.
$msg = "FileSystem::compareMTimes() FAILED. Cannot can not get modified time of $file1.";
throw new Exception($msg);
}

if ($mtime2 === false) { // FAILED. Log and return err.
// Add error from php to end of log message. $php_errormsg.
// Add error from php to end of log message.
$msg = "FileSystem::compareMTimes() FAILED. Cannot can not get modified time of $file2.";
throw new Exception($msg);
}

// Worked. Log and return compare.
// Worked. Log and return compare.
// Compare mtimes.
if ($mtime1 == $mtime2) {
return 0;
Expand Down
5 changes: 4 additions & 1 deletion classes/phing/system/io/InputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,12 @@ public function close()
if ($this->stream === null) {
return;
}
error_clear_last();
if (false === @fclose($this->stream)) {
$lastError = error_get_last();
$errormsg = $lastError['message'];
// FAILED.
$msg = "Cannot fclose " . $this->__toString() . " $php_errormsg";
$msg = "Cannot fclose " . $this->__toString() . " $errormsg";
throw new IOException($msg);
}
$this->stream = null;
Expand Down
10 changes: 8 additions & 2 deletions classes/phing/system/io/OutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ public function close()
return;
}
$this->flush();
error_clear_last();
if (false === @fclose($this->stream)) {
$lastError = error_get_last();
$errormsg = $lastError['message'];
$metaData = stream_get_meta_data($this->stream);
$resource = $metaData["uri"];
$msg = "Cannot close " . $resource . ": $php_errormsg";
$msg = "Cannot close " . $resource . ": $errormsg";
throw new IOException($msg);
}
$this->stream = null;
Expand All @@ -71,8 +74,11 @@ public function close()
*/
public function flush()
{
error_clear_last();
if (false === @fflush($this->stream)) {
throw new IOException("Could not flush stream: " . $php_errormsg);
$lastError = error_get_last();
$errormsg = $lastError['message'];
throw new IOException("Could not flush stream: " . $errormsg);
}
}

Expand Down
2 changes: 0 additions & 2 deletions classes/phing/system/io/UnixFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ public function compare(PhingFile $f1, PhingFile $f2)
*/
public function copy(PhingFile $src, PhingFile $dest)
{
global $php_errormsg;

if (!$src->isLink()) {
parent::copy($src, $dest);
return;
Expand Down