<?php
//echo ini_get('upload_max_filesize');


/**
 * @version 1.0 $Id: prime-check.php 1308 2011-07-07 11:55:49Z stell $
 * @package PRIME
 * @copyright (C) 2010 B01 - Tom Bohacek
 * @license http://www.prime-real.com Copyrighted Commercial Software
 */

class PRequirements {
	
	var $_results = array();
	
	var $_required_extensions = array(
		array('name' => 'JSON', 'product' => 'All', 'extension' => 'json', 'info' => 'Check http://de.php.net/manual/en/book.json.php')
	);

	var $_recommended_extensions = array(
		// array('name' => 'cURL', 'extension' => 'curl', 'info' => 'cURL is required for Facebook Connect to work.')
	);
	
	var $_required_functions = array( 
		array('function' => 'zend_loader_enabled', 'product' => 'Business, Agency, Portal', 'info' => 'Change your settings to zend_optimizer.enable_loader = 1 in php.ini'),
		array('function' => 'imagegd', 'product' => 'All', 'info' => 'Check http://www.php.net/manual/en/image.installation.php'),
		array('function' => 'simplexml_load_string', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/function.simplexml-load-file.php'),
		array('function' => 'simplexml_load_file', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/function.simplexml-load-string.php'),
		array('function' => 'dom_import_simplexml', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/function.dom-import-simplexml.php'),
		array('function' => 'simplexml_import_dom', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/de/function.simplexml-import-dom.php'),		
		array('function' => 'zend_loader_enabled', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/de/function.simplexml-import-dom.php')
			
		// array('function' => 'zend_optimizer_version', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/de/function.simplexml-import-dom.php')
	);
	
	var $_recommended_functions = array(
		array('function' => 'zip_open', 'product' => 'Agency, Portal', 'info' => 'http://php.net/manual/en/function.zip-open.php'),
		array('function' => 'zip_read', 'product' => 'Agency, Portal', 'info' => 'http://php.net/manual/en/function.zip-read.php')
	
	);
	
	var $_required_classes = array(
		array('class' => 'SimpleXMLElement', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/book.simplexml.php'),
		array('class' => 'DOMNode', 'product' => 'Agency, Portal', 'info' => 'http://de.php.net/manual/en/book.dom.php'),		
		array('class' => 'ArrayObject', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/class.arrayobject.php'),
		array('class' => 'XMLReader', 'product' => 'Agency, Portal', 'info' => 'Check http://de.php.net/manual/en/class.xmlreader.php')
	);
	

	var $_required_values = array(
		array('name' => 'upload_max_filesize', 'value' => '8', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/en/ini.core.php'),		
		array('name' => 'max_input_time', 'value' => '30', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/en/ini.core.php')	,	
		array('name' => 'max_execution_time', 'value' => '30', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/en/ini.core.php')	
		//array('name' => 'zend_optimizer.enable_loader', 'value' => '30', 'product' => 'Agency, Portal', 'info' => 'Check http://php.net/manual/en/ini.core.php')		
	);	
	
	
	var $_recommended_classes = array();
	
	function checkPHPsmaller52() {
		return !version_compare(PHP_VERSION, '5.2', '<');
	}	
	
	function checkPHP53() {
		return !function_exists('array_replace');
	}
	
	
	
	function checkSafeMode() {
		return !ini_get('safe_mode');
	}
	
	
	function checkZendOptimizer() {
		// reading php info		 
		ob_start();
		phpinfo(INFO_GENERAL | INFO_CONFIGURATION | INFO_MODULES);		
		$buffer = ob_get_contents();
		while (@ob_end_clean());
		
		$hosting_info_data_array['Zend Optimizer'] = false;
		$hosting_info_data_array['ionCube'] = false;
		$hosting_info_data_array['Perl Version']     = false;
		$hosting_info_data_array['Python Version'] = false;
		
		// check for Zend Optimizer
		$search_regex  = '#Zend(?:\s|&nbsp;)+Optimizer(?:\s|&nbsp;)+v(\d+\.\d+\.\d+)#si'; // with Zend Optimizer v3.0.2
		if (preg_match($search_regex, $buffer, $matches)) {
		    return $hosting_info_data_array['Zend Optimizer'] = $matches[1];
		}	
		return false;	
		
	}
	
	
	function checkMemoryLimit() {
		$memory_limit = ini_get('memory_limit');

		return $memory_limit == '-1' ? true : $this->_return_bytes($memory_limit) >= 67108864;
	}
	
	function _return_bytes ($size_str) {
	    switch (substr ($size_str, -1)) {
	        case 'M': case 'm': return (int)$size_str * 1048576;
	        case 'K': case 'k': return (int)$size_str * 1024;
	        case 'G': case 'g': return (int)$size_str * 1073741824;
	        default: return $size_str;
	    }
	}
	
	function checkRequirements() {
		$this->_results = array();
		
		$this->_checkRequired();
		$this->_checkRecommended();
		
		return $result;
	}
	
	function _checkRequired() {

		// check php smaller 5.2
		$status = $this->checkPHPsmaller52();
		$info 	= 'PRIME requires PHP 5.2. Please change your PHP version (http://www.php.net).';
		$this->_addResult('required', 'All', 'PHP 5.2', $status, $info);

		// check php 5.3
		//$status = $this->checkPHP53();
		//$info 	= 'PRIME will not run under PHP 5.3+. Please aktivate a PHP 5.2+ version.';
		//$this->_addResult('required', 'All', 'PHP 5.3 ', $status, $info);

		
		$status = $this->checkZendOptimizer();
		$info 	= '';
		$this->_addResult('required', 'Business, Agency, Portal', 'Zend Optimizer', $status, $info);
		
		if(function_exists('zend_loader_enabled')) {
			$status = zend_loader_enabled();
			$info 	= 'Change your settings to zend_optimizer.enable_loader = 1 in php.ini';
			$this->_addResult('required', 'Business, Agency, Portal', 'Zend Loader Enabled = 1', $status, $info);
		}

		
		

			
		foreach ($this->_required_functions as $function) {
			$status = function_exists($function['function']);
			$this->_addResult('required', $function['product'], 'Function: ' . $function['function'], $status, $function['info']);
		}

		foreach ($this->_required_extensions as $extension) {
			$status = extension_loaded($extension['extension']);
			$this->_addResult('required', $extension['product'], 'Extension: ' . $extension['name'], $status, $extension['info']);
		}				
		
		foreach ($this->_required_classes as $class) {
			$status = class_exists($class['class']);
			$this->_addResult('required', $class['product'], 'Class: ' . $class['class'], $status, $class['info']);
		}
		
		
				
		
		foreach ($this->_results as $return) {
			if (!$return['status']) {
				return false;
			}
		}

		
		
		
		return true;
	}
	
	function _checkRecommended() {
		
	
		
		foreach ($this->_recommended_functions as $function) {
			$status = function_exists($function['function']);
			$this->_addResult('recommend', $function['product'], 'Function: ' . $function['function'], $status, $function['info']);
		}
		foreach ($this->_recommended_extensions as $extension) {
			$status = extension_loaded($extension['extension']);
			$this->_addResult('recommend', $extension['product'], 'Extension: ' . $extension['name'], $status, $extension['info']);
		}			
		foreach ($this->_recommended_classes as $class) {
			$status = class_exists($class['class']);
			$this->_addResult('recommend', $class['product'], 'Class: ' . $class['class'], $status, $class['info']);
		}
		
		// check safe mode
		$status = $this->checkSafeMode();
		$info 	= 'It is recommended to turn off PHP safe mode.';
		$this->_addResult('recommend', 'All', 'PHP Safe Mode', $status, $info);
		
		$status = $this->checkMemoryLimit();
		$info 	= 'It is recommended to set the php setting memory_limit to 64M or higher.';
		$this->_addResult('recommend', 'All', 'PHP Memory Limit', $status, $info);
		
		foreach ($this->_required_values as $value) {
			$server_var = is_numeric(ini_get($value['name'])) ? ini_get($value['name']) : substr (ini_get($value['name']), 0, -1);
			//echo $server_var;
			if($server_var == -1) {
				$status = 1;
			}
			else {
				$status = $server_var >= $value['value'];
			}
			
			$this->_addResult('recommend', $value['product'], 'Directive : ' . $value['name'], $status, "You need ".$value['value']. ", You have: ".$server_var."<br />".$value['info']);
		}				

		
		foreach ($this->_results as $return) {
			if (!$return['status']) {
				return false;
			}
		}
		
		return true;
	}
	
	function _addResult($type, $product, $name, $status, $info = '') {
		$this->_results[$type][] = compact('product', 'name', 'status', 'info');
		
	}
	
	function displayResults() {
		header("Content-Type: text/html; charset=utf-8");
		?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de-de" lang="de-de" dir="ltr" id="minwidth" >
<head>
	<title>PRIME Requirements Check</title>
	<style>
		body {font-family:arial;font-size:80%;}
		table {border:1px solid #ccc;margin:0;padding:0;width:80%;border-collapse:collapse;}
		td {margin:0;padding:6px 10px;}
		td.status {text-align:center;}
		tr.row0 {background-color: #eee;}
		table th {background-color: #ccc;padding:10px 0;}
	</style>
</head>
<body>		
	<h3>PRIME Requirements</h3>
	<p>If any of the items below are highlighted in red, you should try to correct them. Failure to do so could lead to your PRIME installation not functioning correctly.</p>
	
	
	<table>
		<thead>
			<tr>
				<th class="title" width="20%">Requirement</th><th width="10%">Status</th><th width="10%">PRIME Product</th><th width="50%">Info</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="4">&nbsp;</td>
			</tr>
		</tfoot>
		<tbody>
			<?php 
				foreach ($this->_results['required'] as $i => $req) : ?>
				<tr class="row<?php echo $i++ % 2; ?>">
					<td class="key"><?php echo $req['name']; ?></td>
					<td class="status">
						<?php $style = $req['status'] ? 'font-weight: bold; color: green;' : 'font-weight: bold; color: red;'; ?>
						<span style="<?php echo $style; ?>"><?php echo $req['status'] ?'OK' : 'Not OK'; ?></span>
					</td>
					<td><?php echo $req['product']; ?></td>
					<td>
						<span><?php echo $req['status'] ? '' : $req['info']; ?></span>
					</td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>	
	<br />
	<table>
		<thead>
			<tr>
				<th class="title" width="20%">Recommended</th><th width="10%">Status</th><th width="10%">PRIME Product</th><th width="50%">Info</th>
			</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="4">&nbsp;</td>
			</tr>
		</tfoot>
		<tbody>
			<?php 
				foreach ($this->_results['recommend'] as $i => $req) : ?>
				<tr class="row<?php echo $i++ % 2; ?>">
					<td class="key"><?php echo $req['name']; ?></td>
					<td class="status">
						<?php $style = $req['status'] ? 'font-weight: bold; color: green;' : 'font-weight: bold; color: red;'; ?>
						<span style="<?php echo $style; ?>"><?php echo $req['status'] ?'OK' : 'Not OK'; ?></span>
					</td>
					<td><?php echo $req['product']; ?></td>
					<td>
						<span><?php echo $req['status'] ? '' : $req['info']; ?></span>
					</td>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	
	
	
	
	
	
</body>		
		<?php
	}	
}

$requirements = new PRequirements();
$requirements->checkRequirements(); 
$requirements->displayResults();


