All pastes #637897 Raw Edit

Untitled

public text v1 · immutable
#637897 ·published 2007-07-29 09:58 UTC
rendered paste body
<?php
/*
|---------------------------------------------------------------
| engine class - template again class which gets files
| Created: 27th July 2007
| Henry Legge
|
| Modified: 28th July 2007
| By: Henry Legge
|---------------------------------------------------------------
*/

	if(   !defined(   'IN_BOARD'   )   ) {
	
		die(   '<h2>File not used for direct access</h2>'   );
	
	}
	
	class engine extends engine_log
	{
	
/*
Clean the file to stop any attacks
*/

		function engine_clean( $text )
		{
			$old = array(
				"'#\<?php(.*?)?\>#se'",
				"'#\<?(.*?)?\>#se'"
			);
			
			$new = array(
				'',
				''
			);
			
			$text = preg_replace( $old, $new, $text );
			return $text;
		}
		
/*
The main engine function, this ties all the other functions together does the necciasry checks cleans it and outputs the file. If there is an error it gets logged by the complex loggin class
*/

		function get_contents( $filename )
		{
		
			$file = ( isset( $filename ) ) ? $filename.'.tpl' : NULL;
			
			if( empty( $file )
			{
				return parent::file_log( $file, '400' );
			}
			elseif( !file_exists( $file ) )
			{
				return parent::file_log( $file, '404' );
			}
			elseif ( !@is_readable( $file ) )
			{
				parent::file_log( $file, '403' );
				
				if ( !@chmod( $file, 0777 ) )
				{
					die( parent::file_log( $file, '500' ) );
				}
				else
				{
					$contents = file_get_contents( $file );
				
					if( empty( $contents ) )
					{
						return parent::file_log( $file, '411' );
					}
					else
					{
						$contents = $this->engine_clean( $contents );
					
						return $contents;
					}
				}
			}
			else
			{
				$contents = file_get_contents( $file );
				
				if( empty( $contents ) )
				{
					return parent::file_log( $file, '411' );
				}
				else
				{
					$contents = $this->engine_clean( $contents );
					
					return $contents;
				}
			}
		}

	}

/*
|---------------------------------------------------------------
| List of possible errors
|---------------------------------------------------------------
| 302 - Redirect
| 400 - Bad Request
| 401 - Authorization Required
| 402 - Payment Required
| 403 - Forbidden
| 404 - File Not Found
| 405 - Method Not Allowed
| 406 - Not Acceptable
| 407 - Proxy Authentication Required
| 408 - Request Time-out
| 409 - Conflict
| 410 - Gone
| 411 - Length Required
| 412 - Precondition Failed
| 413 - Request Entity Too Large
| 414 - Request-URI Too Large
| 415 - Unsupported Media Type
| 500 - Internal Server Error
| 501 - Method Not Implemented
| 502 - Bad Gateway
| 503 - Service Temporarily Unavailable
| 504 - Gateway Time-out
| 505 - HTTP Version Not Supported
|---------------------------------------------------------------
| 600 - No error
*/

/*
|---------------------------------------------------------------
| engine_log class - Log verything that happens in the template engine.
| Created: 27th July 2007
| Henry Legge
|
| Modified: 28th July 2007
| By: Henry Legge
|---------------------------------------------------------------
*/

	class engine_log
	{
	
		var $base_dir = BASEDIR;
		var $log_dir = 'logs/';
		var $log_file_name = 'template.engin.error.log';
		
/*
Set up the log
*/

		function __construct()
		{
			echo empty( $this->create_log() ) ? NULL : $this->create_log();
		}

/*
Create the log file
*/

		function create_log()
		{
			if( !is_dir( $this->base_dir . $this->log_dir ) )
			{
				if( !mkdir( $this->base_dir . $this->log_dir, 0777 ) )
				{
					return '<strong>Error</strong><br />The log directory can\'t be created. Please contact an administrator.';
				}
			}
			elseif( is_dir( $this->base_dir . $this->log_dir ) )
			{
				if ( !is_readable( $this->base_dir . $this->log_dir ) || !is_writable( $this->base_dir . $this->log_dir ) )
				{
					if( !chmod( $this->base_dir . $this->log_dir, 0777 ) )
					{
						return '<strong>Error</strong><br />The log directory chmod can\'t be changed. Please contact an administrator.';
					}
				}
				elseif( is_readable( $this->base_dir . $this->log_dir ) and is_writable( $this->base_dir . $this->log_dir ) )
				{
					if( !file_exists( $this->base_dir . $this->log_dir . $this->log_file_name ) )
					{
						if( !fopen( $this->base_dir . $this->log_dir . $this->log_file_name, "x+" ) )
						{
							return '<strong>Error</strong><br />The log directory can\'t be created. Please contact an administrator.';
						}
					}
					elseif( file_exists( $this->base_dir . $this->log_dir . $this->log_file_name ) )
					{
						if( !is_readable( $this->base_dir . $this->log_dir . $this->log_file_name ) || !is_writable( $this->base_dir . $this->log_dir . $this->log_file_name ) )
						{
							if( !chmod( $this->base_dir . $this->log_dir . $this->log_file_name, 0777 ) )
							{
								return '<strong>Error</strong><br />The log file chmod can\'t be changed. Please contact an administrator.';
							}
						}
					}
				}
			}
		}
		
/*
Log all the errors
*/

		function file_log( $file, $error, $line = 0 )
		{
		
			if( empty( $error ) )
			{
				return '<strong>Error</strong><br />There is an error in the script. Please contact an administrator.';
				$error = 'Script error';
			}
			elseif( $error == 400 )
			{
				return '<strong>Error</strong><br />There was no filename supplied. Please contact an administrator.';
				$error = 'No filename supplied';
			}
			elseif( $error == 403 )
			{
				return '<strong>Error</strong><br />The file isn\'t readable. Please contact an administrator.';
				$error = 'File isn\'t readable';
			}
			elseif( $error == 404 )
			{
				return '<strong>Error</strong><br />The file is missing. Please contact an administrator.';
				$error = 'File is missing';
			}
			elseif( $error == 411 )
			{
				return '<strong>Error</strong><br />The file is empty. Please contact an administrator.';
				$error = 'File is empty';
			}
			elseif( $error == 500 )
			{
				return '<strong>Error</strong><br />The file chmod can\'t be changed. Please contact an administrator.';
				$error = 'Cannot chmod file';
			}
			
			$final_error = "[" . time() . "] Line: " . $line . ", File: " . $file . " => " . $error . ". \r\n";
			return isset( $this->write_log( $final_error ) ) ? $this->write_log( $final_error ) : NULL;
		}
		
/*
Write the log
*/

		function write_log( $error )
		{
			if( !$handle = fopen( $this->base_dir . $this->log_dir . $this->log_file_name, 'a' ) )
			{
				return '<strong>Error</strong><br />Cannot open the log file for writing. Please contact an administrator.';
			}
			elseif( $handle = fopen( $this->base_dir . $this->log_dir . $this->log_file_name, 'a' ) )
			{
				if( fwrite( $handle, $error ) === FALSE )
				{
					return '<strong>Error</strong><br />Cannot write the file. Please contact an administrator.';
				}
			}
		}

	}
?>