From 25d7cb258d59faa13dcc2cbaeda4712dbaeb225c Mon Sep 17 00:00:00 2001
From: CaMer0n <cameron@e107coders.org>
Date: Fri, 8 Jun 2012 06:03:07 +0000
Subject: [PATCH] Work on override class and multi-site support.

---
 e107_admin/update_routines.php       |  57 ++++++++++++++-
 e107_handlers/cron_class.php         |   2 +-
 e107_handlers/e107_class.php         |  67 +++++++++++++++---
 e107_handlers/override_class.php     | 100 +++++++++++++++++++++++----
 e107_handlers/secure_img_handler.php |  24 +++++++
 e107_media/icons/index.html          |   0
 thumb.php                            |   7 +-
 7 files changed, 230 insertions(+), 27 deletions(-)
 delete mode 100644 e107_media/icons/index.html

diff --git a/e107_admin/update_routines.php b/e107_admin/update_routines.php
index ad50682b3..b962ffa3a 100644
--- a/e107_admin/update_routines.php
+++ b/e107_admin/update_routines.php
@@ -1011,7 +1011,62 @@ function update_706_to_800($type='')
 	}
 	
 
-	//-- Media-manger import --------------------------------------------------
+	//-- Media-manger import --------------------------------------------------
+	
+	if(!is_dir(e_MEDIA))
+	{
+		mkdir(e_MEDIA,0755);	
+	}	
+	if(!is_dir(e_SYSTEM))
+	{
+		mkdir(e_SYSTEM,0755);
+	}
+	if(!is_dir(e_CACHE))
+	{
+		mkdir(e_CACHE,0755);
+	}
+	if(!is_dir(e_CACHE_CONTENT))
+	{
+		mkdir(e_CACHE_CONTENT,0755);
+	}	
+	if(!is_dir(e_CACHE_IMAGE))
+	{
+		mkdir(e_CACHE_IMAGE,0755);
+	}
+	if(!is_dir(e_CACHE_DB))
+	{
+		mkdir(e_CACHE_DB,0755);
+	}
+	if(!is_dir(e_LOG))
+	{
+		mkdir(e_LOG,0755);
+	}
+	if(!is_dir(e_BACKUP))
+	{
+		mkdir(e_BACKUP,0755);
+	}
+	
+	
+	$root_media = str_replace(basename(e_MEDIA)."/","",e_MEDIA);
+	$user_media_dirs = array("images","avatars","files","temp","videos","icons");
+	
+	// check for old paths and rename. 
+	if(is_dir($root_media."images") || is_dir($root_media."temp"))
+	{
+		foreach($user_media_dirs as $md)
+		{
+			@rename($root_media.$md,e_MEDIA.$md);	
+		}				
+	}
+	
+	// create sub-directories if they do not exist. 
+	if(!is_dir(e_MEDIA."images") || !is_dir(e_MEDIA."temp"))
+	{
+		foreach($user_media_dirs as $md)
+		{
+			mkdir(e_MEDIA.$md);	
+		}	
+	}
 	
 	$med = e107::getMedia();
 	
diff --git a/e107_handlers/cron_class.php b/e107_handlers/cron_class.php
index ccd1e3812..7d9c725a5 100644
--- a/e107_handlers/cron_class.php
+++ b/e107_handlers/cron_class.php
@@ -101,7 +101,7 @@ class _system_cron
 		$sql = e107::getDb();
 		$dbtable = $mySQLdefaultdb; // TODO - retrieve this in a better way. (without including e107_config) 
 	
-		$backupFile = e_SYSTEM."backups/".SITENAME."_".date("Y-m-d-H-i-s").".sql";
+		$backupFile = e_BACKUP.SITENAME."_".date("Y-m-d-H-i-s").".sql";
 		$result = mysql_list_tables($dbtable);
 		
 		while ($tab = mysql_fetch_array($result, MYSQL_NUM))
diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php
index f46a24e03..06d834ff7 100644
--- a/e107_handlers/e107_class.php
+++ b/e107_handlers/e107_class.php
@@ -45,6 +45,7 @@ class e107
 	public $https_path;
 	public $base_path;
 	public $file_path;
+	public $site_path;
 	public $relative_base_path;
 	public $_ip_cache;
 	public $_host_name_cache;
@@ -310,13 +311,16 @@ class e107
 		{
 			// Do some security checks/cleanup, prepare the environment
 			$this->prepare_request();
-
-			// Set default folder (and override paths) if missing from e107_config.php
-			$this->setDirs($e107_paths, $e107_config_override);
-
+	
 			// mysql connection info
 			$this->e107_config_mysql_info = $e107_config_mysql_info;
-
+			
+			// unique folder for e_MEDIA - support for multiple websites from single-install. Must be set before setDirs() 
+			$this->site_path = substr(md5($e107_config_mysql_info['mySQLdefaultdb'].".".$e107_config_mysql_info['mySQLprefix']),0,10);
+		
+			// Set default folder (and override paths) if missing from e107_config.php
+			$this->setDirs($e107_paths, $e107_config_override);
+				
 			// various constants - MAGIC_QUOTES_GPC, MPREFIX, ...
 			$this->set_constants();
 
@@ -385,9 +389,12 @@ class e107
 			'CORE_DIRECTORY' 		=> 'e107_core/',
 			'WEB_DIRECTORY' 		=> 'e107_web/',
 		), (array) $override_root);
-
+		
+		$ret['MEDIA_DIRECTORY'] 	.= $this->site_path."/"; // multisite support. 
+		$ret['SYSTEM_DIRECTORY'] 	.= $this->site_path."/"; // multisite support. 
+				
 		if($return_root) return $ret;
-
+		
 		$ret['HELP_DIRECTORY'] 				= $ret['DOCS_DIRECTORY'].'help/';
 
 		$ret['MEDIA_IMAGES_DIRECTORY'] 		= $ret['MEDIA_DIRECTORY'].'images/';
@@ -398,6 +405,9 @@ class e107
 		$ret['MEDIA_UPLOAD_DIRECTORY'] 		= $ret['MEDIA_DIRECTORY'].'temp/';
 
 		$ret['WEB_JS_DIRECTORY'] 			= $ret['WEB_DIRECTORY'].'js/';
+		
+		
+		
 		$ret['WEB_CSS_DIRECTORY'] 			= $ret['WEB_DIRECTORY'].'css/';
 		$ret['WEB_IMAGES_DIRECTORY'] 		= $ret['WEB_DIRECTORY'].'images/';
 		$ret['WEB_PACKS_DIRECTORY'] 		= $ret['WEB_DIRECTORY'].'packages/';
@@ -411,6 +421,9 @@ class e107
 		$ret['CACHE_DB_DIRECTORY'] 			= $ret['CACHE_DIRECTORY'].'db/';
 
 		$ret['LOGS_DIRECTORY'] 				= $ret['SYSTEM_DIRECTORY'].'logs/';
+		$ret['BACKUP_DIRECTORY'] 			= $ret['SYSTEM_DIRECTORY'].'backup/';
+
+		//TODO create directories which don't exist. 
 
 		return $ret;
 	}
@@ -543,7 +556,23 @@ class e107
 	function getMySQLConfig($for)
 	{
 		$key = 'mySQL'.$for;
-		return (isset($this->e107_config_mysql_info[$key]) ? $this->e107_config_mysql_info[$key] : '');
+		$self = self::getInstance();
+		return (isset($self->e107_config_mysql_info[$key]) ? $self->e107_config_mysql_info[$key] : '');
+		
+	//	return (isset($this->e107_config_mysql_info[$key]) ? $this->e107_config_mysql_info[$key] : '');
+	}
+	
+
+	/**
+	 * Return a unique path based on database used. ie. multi-site support from single install. 
+	 *
+	 * @return string
+	 * @author  
+	 */
+	function getSitePath()
+	{
+		$self = self::getInstance();
+		return $self->site_path;
 	}
 
 	/**
@@ -1178,6 +1207,19 @@ class e107
 		return self::getSingleton('notify', true);
 	}
 
+
+	/**
+	 * Retrieve override handler singleton object
+	 *
+	 * @return notify
+	 */
+	public static function getOverride()
+	{
+		return self::getSingleton('override', true);
+	}
+
+
+
 	/**
 	 * Retrieve Language handler singleton object
 	 *
@@ -2327,7 +2369,10 @@ class e107
 		{
 			return $this->e107_dirs[$dir.'_SERVER'];
 		}
-		return e_BASE.$this->e107_dirs[$dir.'_DIRECTORY'];
+		$ret = e_BASE.$this->e107_dirs[$dir.'_DIRECTORY'];
+
+		
+		return $ret;
 	}
 
 	/**
@@ -2471,6 +2516,7 @@ class e107
 			define('e_CACHE_DB', $this->get_override_rel('CACHE_DB'));
 
 			define('e_LOG', $this->get_override_rel('LOGS'));
+			define('e_BACKUP', $this->get_override_rel('BACKUP'));
 
 			//
 			// HTTP absolute paths
@@ -2502,6 +2548,9 @@ class e107
 			define('e_CSS_ABS', $this->get_override_http('WEB_CSS'));
 			define('e_PACK_ABS', $this->get_override_http('WEB_PACKS'));
 			define('e_WEB_IMAGE_ABS', $this->get_override_http('WEB_IMAGES'));
+			
+			define('e_JS', $this->get_override_http('WEB_JS')); // ABS Alias 
+			define('e_CSS', $this->get_override_http('WEB_CSS')); // ABS Alias 
 
 		}
 		return $this;
diff --git a/e107_handlers/override_class.php b/e107_handlers/override_class.php
index 50bbd16a3..4340c578a 100644
--- a/e107_handlers/override_class.php
+++ b/e107_handlers/override_class.php
@@ -26,36 +26,106 @@ if (!defined('e107_INIT')) { exit; }
  * if ($over_func_name = $override->override_check('original_func_name')) {
  *	$result=call_user_func($over_func_name, params...);
  * }
+ * 
  *
  */
  
 class override {
-	var $functions = array();
-	var $includes = array();
+	
+	protected $functions = array();
+	protected $includes = array();
+	
+	
+	
+	/**
+	 * Replace an existing function or class method
+	 * @param string|array $override - function name or class::method
+	 * @param string|array $function - new function name or class::method 
+	 * @param $include (optional) - file to include from root dir. 
+	 * @example e107::getOverride()->replace('secure_image::create_code', 'myclass::mymethod');
+	 */
+	public function replace($override,$function,$include) // Alias with class functionality. 
+	{
+		if(is_array($override))
+		{
+			$arr = $override[0]."::".$override[1];		
+		}
+		else
+		{
+			$arr = $override;	
+		}
+		
+		$this->override_function($arr, $function, $include);
+	}
+	/**
+	 * check if an override exists
+	 * @param $override : function name or class object
+	 * @param $method : method name when 'class' is used for $override
+	 * @return mixed
+	 * @example if ($user_func = e107::getOverride()->check($this,'secure_image'))
+				{
+	 				return call_user_func($user_func);
+				}  
+	 */
+	public function check($override,$method='') // alias with check for class object
+	{
+		
+		if(vartrue($method))
+		{			
+			$class = get_class($override);
+			$override = $class."::".$method;	
+		}
+		return $this->override_check($override);	
+	}
+	
+
 	 
-	function override_function($override, $function, $include) {
-		if ($include) {
+	function override_function($override, $function, $include) 
+	{
+		if ($include)
+		{
 			$this->includes[$override] = $include;
 		}
-		else if (isset($this->includes[$override])) {
+		else if (isset($this->includes[$override]))
+		{
 			unset($this->includes[$override]);
 		}
+		
 		$this->functions[$override] = $function;
 	}
 	 
-	function override_check($override) {
-		if (isset($this->includes[$override])) {
-			if (file_exists($this->includes[$override])) {
+	 
+	function override_check($override)
+	{		
+		if (isset($this->includes[$override])) 
+		{			
+			if (file_exists($this->includes[$override]))
+			{
 				include_once($this->includes[$override]);
-			}
-			if (function_exists($this->functions[$override])) {
-				return $this->functions[$override];
-			} else {
-				return false;
-			}
-		} else {
+			}	
+		} 
+		
+		
+		$tmp =  strpos($this->functions[$override],"::") ?  explode("::",$this->functions[$override]) : $this->functions[$override];
+		if(is_array($tmp) && class_exists($tmp[0]))
+		{
+			$cl = new $tmp[0];
+			 if(method_exists($cl,$tmp[1]))
+			 {
+			 	return $this->functions[$override];
+			 }	
+		}	
+	
+		if (function_exists($this->functions[$override]))
+		{
+			
+			return $this->functions[$override];
+		}
+		else
+		{
 			return false;
 		}
+
 	}
 }
 	
diff --git a/e107_handlers/secure_img_handler.php b/e107_handlers/secure_img_handler.php
index 42278ccaa..ee43e925a 100644
--- a/e107_handlers/secure_img_handler.php
+++ b/e107_handlers/secure_img_handler.php
@@ -24,6 +24,11 @@ class secure_image
 
 	function secure_image()
 	{
+		if ($user_func = e107::getOverride()->check($this,'secure_image'))
+		{
+	 		return call_user_func($user_func);
+		}
+		
 		list($usec, $sec) = explode(" ", microtime());
 		$this->random_number = str_replace(".", "", $sec.$usec);
 
@@ -49,6 +54,10 @@ class secure_image
 
 	function create_code()
 	{
+		if ($user_func = e107::getOverride()->check($this,'create_code'))
+		{
+	 		return call_user_func($user_func);
+		}
 
 		$pref = e107::getPref();
 		$sql = e107::getDb();
@@ -68,6 +77,11 @@ class secure_image
 
 	function verify_code($rec_num, $checkstr)
 	{
+		if ($user_func = e107::getOverride()->check($this,'verify_code'))
+		{
+	 		return call_user_func($user_func,$rec_num,$checkstr);
+		}
+		
 		$sql = e107::getDb();
 		$tp = e107::getParser();
 
@@ -83,6 +97,11 @@ class secure_image
 
 	function r_image()
 	{
+		if ($user_func = e107::getOverride()->check($this,'r_image'))
+		{
+	 		return call_user_func($user_func);
+		}
+
 		$code = $this->create_code();
 		return "<img src='".e_HTTP.$this->HANDLERS_DIRECTORY."secure_img_render.php?{$code}' class='icon secure-image' alt='' />";
 	}
@@ -94,6 +113,11 @@ class secure_image
 	 */
 	function render($qcode)
 	{
+		if ($user_func = e107::getOverride()->check($this,'render'))
+		{
+	 		return call_user_func($user_func,$qcode);
+		}
+		
 		if(!is_numeric($qcode)){ exit; }
 		$recnum = preg_replace('#\D#',"",$qcode);
 
diff --git a/e107_media/icons/index.html b/e107_media/icons/index.html
deleted file mode 100644
index e69de29bb..000000000
diff --git a/thumb.php b/thumb.php
index f4bf7333e..b42a39800 100644
--- a/thumb.php
+++ b/thumb.php
@@ -101,6 +101,9 @@ class e_thumbpage
 		$sql_info = array(); //compact('mySQLserver', 'mySQLuser', 'mySQLpassword', 'mySQLdefaultdb', 'mySQLprefix', 'mySQLcharset');
 		//e107::getInstance()->initCore($e107_paths, $self, $sql_info, varset($e107_CONFIG, array()));
 		$e107 = e107::getInstance();
+		
+		$e107->site_path = substr(md5($mySQLdefaultdb.".".$mySQLprefix),0,10);
+		
 		$e107->prepare_request();
 		$e107->setDirs($e107_paths, varset($E107_CONFIG, array()));
 		$e107->set_constants();
@@ -110,7 +113,7 @@ class e_thumbpage
 		$e107->set_request(false);
 		$e107->set_urls(false);
 		unset($tmp, $self);
-
+	
 		// basic Admin area detection - required for proper path parsing
 		define('ADMIN', strpos(e_SELF, ($e107->getFolder('admin')) !== false || strpos(e_PAGE, 'admin') !== false));
 		
@@ -159,6 +162,8 @@ class e_thumbpage
 			$this->_src_path = $path;
 			return true;
 		}
+		
+		// echo "path=".$path."<br />";
 		return false;
 	}