All pastes #718714 Raw Edit

set_error_handler error

public php v1 · immutable
#718714 ·published 2007-09-28 17:17 UTC
rendered paste body
<?phpclass patch{	var $old = null;  // the file to be patched	var $new = null;  // the patch file	var $ext = null;  // the backup extension		# Constructor	function patch ($old, $new, $ext=".bak")	{		if(!file_exists($new))			exit("Error: Couldn't find $new!"); // $old is allowed to not exist		$this->old = $old;		$this->new = $new;				if(!is_string($ext) || !ereg("^\..*", $ext))			exit('Error: Please specify a valid backup extension - for example ".bak"');		$this->ext = $ext;				set_error_handler($this->patchErrorHandler);	}		function apply($simulate=true)	{					# On vérifie les droits sur les fichiers:			if(!$this->is_patchable($this->new, $this->old))				exit();			 			# Backupons:			if(file_exists($this->old.$this->ext))				trigger_error("Previous backup file $old$this->ext has been found. It was overwritten!", E_USER_NOTICE);			if(!$simulate && file_exists($this->old))				copy($this->old, $this->old.$this->ext);						# Patchons:			if(!$simulate)			{				copy($this->new, $this->old);				trigger_error("$this->old patched! [OK]", E_USER_NOTICE);			}	}		function unapply($simulate=true)	{			# On vérifie les droits sur les fichiers:			if(!$this->is_patchable($this->old.$this->ext, $this->old))				break;							# Dé-patchons:			if(!$simulate)			{				if(file_exists($this->old.$this->ext))				{					copy($this->old.$this->ext, $this->old);					trigger_error("$this->old unpatched! [OK]", E_USER_NOTICE);				}				else				{					trigger_error("$this->old couldn't be unpatched no backup of it was found!", E_USER_WARNING);				}			}			 			# Dé-backupons:			if(!file_exists($this->old.$this->ext))				trigger_error("Backup file $this->old$this->ext already deleted!", E_USER_WARNING);			!$simulate && unlink($this->old.$this->ext);	}		function is_patchable($source, $dest)	{		# Accès en lecture aux fichiers: les 2 devant etre 'readable' sauf si $dest n'existe pas		if(!is_readable($source) || (file_exists($dest) && !is_readable($dest)))		{			trigger_error("At least one of the files is not readable!", E_USER_ERROR);			return false;		}				# Accès en écriture au dossier de destination:		$dest_dir = dirname($dest);		if(!is_writable($dest_dir))		{			trigger_error("Error: The destination directory $dest_dir is not writable!", E_USER_ERROR);			return false;		}					return true;	}		function patchErrorHandler($errno, $errstr, $errfile, $errline)	{    switch ($errno) {	    case E_USER_ERROR:	        echo "<b>Patch error</b> [$errno] $errstr<br />\n";	        echo "  Fatal error on line $errline in file $errfile";	        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";	        echo "Aborting...<br />\n";	        exit(1);	        break;		    case E_USER_WARNING:	        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";	        break;		    case E_USER_NOTICE:	        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";	        break;		    default:	        echo "Unknown error type: [$errno] $errstr<br />\n";	        break;    }    /* Don't execute PHP internal error handler */    return true;	}}?>