Location: PHPKode > scripts > phpEngine > phpengine/phpEngine_run-up-1.1.2.php
<?php

/**	Copyright 2004 Live Web Institute Studio (lwis.net): Ingenious Web Solutions

	Licensed under "Non-Commercial License" for "Non-Commercial or Research Use": research, evaluation, personal and educational use,
	excluding use or distribution for direct or indirect commercial (including strategic) gain or advantage.	
	
	$Id: phpEngine-v1.1.2-stable.class.php, v1.1.2 2004/10/01 21:34:18 Tomas Bagdanavicius Exp $
	[ Main program ]

	**/

	error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);
	define("version", "phpEngine run-up v1.1.2");

class phpEngine {

	var $dtctimestamp = true;
	var $months = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var $dateformat = "%B %d, %Y";
	var $addons_dir_path = "./";
	var $addons = null;

	function addon($addons) {

		if(!is_string($addons)) $this->notice("Can't attach addons: supplied argument must be a string");
		$this->addons = preg_split("/[\s,]+/", trim($addons));

		$addon_dir = @opendir($this->addons_dir_path);
		if(!$addon_dir) $this->notice("Can't open directory $this->addons_dir_path");

		$file_names = array();
		while (false !== ($file = @readdir($addon_dir))) {
			if(eregi("^\.{1,2}$",$file)) continue;
			if(is_file($this->addons_dir_path.$file)) {
				if(ereg("^.*\.addon.php$", $this->addons_dir_path.$file))
					list($addon_name, $type, $ext) = split('[\..-]', $file);
					array_push($file_names, $addon_name);
			}
		}

		foreach($this->addons as $value) {
			if(!in_array($value, $file_names)) 
				$this->notice("Plugin name $value was not found");
				include($this->addons_dir_path.$value.".addon.php");
				$this->$value = new $value($this);
		}
	}

	function _array_combine($array1, $array2) {
		$array2 = array_values($array2);
		if(count($array1) <> count($array2))
			$this->notice("The number of keys and the number of elements in the array don't match!");

		if(function_exists("array_combine")) {
			return array_combine($array1, $array2);
		} else {
			for($re = 0; $re < count($array1); $re++) {
				$array[$array1[$re]] = $array2[$re];
			}
			return $array;
		}
	}

	function insert_keys($keys, $value) {
		if(is_string($keys)) {
			$keys = preg_split("/[\s,]+/", trim($keys));
		}
		if(!is_array($keys))
			$this->notice("Element keys is neither a string nor an array!");

		$keys = array_values($keys);
		$complect = array();
		foreach($value as $v) {
			 $combined = $this->_array_combine($keys, $v);
			 array_push($complect, $combined);
		}
		return $complect;
	}

	function global_ph($candidate, $tag, $lnum = null) {
		switch($candidate) {
			case "lnum": return $lnum; break;
			case "version": return ($tag == "var") ? version : false; break;
			default: return false; break;
		}
	}

	function verify_file($file) {
		if(!ereg("^.*\..{2,4}$", $file))
			$this->notice("Invalid file name supplied for inclusion");
		if(!file_exists($file))
			$this->notice("Failed to open stream (".$file."): no such file");
		return true;
	}

	function dynamic_ph($candidate, $placeholder) {
		if(is_numeric($candidate)) {
			if($this->dtctimestamp && ereg("[0-9]{10}", $candidate)) {
				$date = ereg_replace('%[bB]', $this->months[(int)strftime('%m', $candidate)-1], $this->dateformat);
				return strftime($date, $candidate);
			} else { return $candidate; }
		} elseif(ereg("^.*\:{2}(this|ph)$", $placeholder)) {
			$ph = explode("::", $placeholder);
			return $ph[0];
		} else {
			return $candidate;
		}
	}

	function assign($pat_var, $value, $keys = null) {
		if($keys <> null) {
			if(is_array($value)) {
				$this->assign($pat_var, $this->insert_keys($keys, $value));
			} else {
				$this->notice("Can't assign using keys if the value is not an array!");
			}
		} else {
			$this->pat_vars[$pat_var] = $value;
		}
	}

	function output($patfile, $return = true) {
		$output = null;
		$regexp = "/(<(var|loop|inc)[^>]*>)(.*?)(<\/\\2>)+/si";

		if($this->verify_file($patfile))
			$content = (!function_exists("file_get_contents")) ? implode('', file($patfile)) : file_get_contents($patfile);

		$parts = preg_split($regexp, $content, -1);
		preg_match_all($regexp, $content, $matches);

		for($i = 0; $i < count($parts); $i++) {
			$output .= $parts[$i];

		if($i < count($matches[0])) {
			switch($matches[2][$i]):
				case "var":
					$check_global = $this->global_ph($matches[3][$i], "var");
					if(!isset($this->pat_vars[$matches[3][$i]]) && !$check_global)
						$this->notice("Assigned variable ".$matches[3][$i]." is missing");
					$output .= (!$check_global) ? $this->pat_vars[$matches[3][$i]] : $check_global;
				break;

				case "loop":
					preg_match_all("/<loop[^>]*?name=[\"']?([^\"'>]+)[\"']?.*?>/si", $matches[1][$i], $match, PREG_SET_ORDER);
					preg_match_all("/\{(.*?)\}/si", trim($matches[3][$i]), $order);

					$lnum = "1";
					if(!isset($this->pat_vars[$match[0][1]]))
						$this->notice("Assigned loop name ".$match[0][1]." is missing");
					foreach($this->pat_vars[$match[0][1]] as $x => $xval) {
						foreach($order[1] as $y => $yval) {
							if(empty($order[1][$y]))
								$this->notice("The placeholder ".($y+1)." in loop ".$match[0][1]." is empty");
							$check_global = $this->global_ph($order[1][$y], "loop", $lnum);
							$patterns[] = "/\{".$order[1][$y]."\}/";
							$replacements[] = (!$check_global) ? $this->dynamic_ph($this->pat_vars[$match[0][1]][$x][$order[1][$y]], $order[1][$y]) : $check_global;
						}

					$output .= preg_replace($patterns, $replacements, $matches[3][$i]);
					unset($patterns, $replacements);
					$lnum++;
					}
				break;

				case "inc":
					$file = $matches[3][$i];
					if(!isset($this->pat_vars[$file]) && $this->verify_file($file)) {
						$output .= $this->output($file, false);
					} else {
						$filename = $this->pat_vars[$file];
						$output .= ($this->verify_file($filename)) ? $this->output($filename, false) : null;
					}
				break;
			endswitch;
			}
		}
	return ($return) ? print($output) : $output;
	}

	function notice($errstr) {
		print "NOTICE [".$errstr."]";
		error_log(date("Y/m/d H:i:s \| ").$errstr."\n", 3, "errors.log");
		exit;
	}

}
?>
Return current item: phpEngine