All pastes #168767 Raw Edit

Someone

public text v1 · immutable
·published 2006-09-13 08:49 UTC
rendered paste body
<?php

// ------------- ~ Tango ~ ------------- \\
// File: kernel/core/libmodule.php
// Author: Alex Cartwright
// Created: 5th September 2006
// Recoded for TangoCMS 085
// ------------- ~ Tango ~ ------------- \\

	class libmodule {
		
		var $siteType; /* Stores which type of site to load, AdminCP ( admin ) or Main Site ( main ) */
		
		var $tcmTemplate; /* Stores the data fetched from tcm_template in modGetModules() */
		
		var $SCinfo; /* Stores all information about the module loaded into SC */	
		
		// ---------------------------------------------------------------------------------------
		// modGetModules will get pr)epare the rest of the script on what modules will be needed
		// to load, and inwhich sector they should be plugged into
		// ---------------------------------------------------------------------------------------
		function modGetModules( $siteType = 'main' ) {
		
			global $libmysql;
			
			// Store site type
			$this->siteType = $siteType;
			
			// Find out what Modules need to be loaded and inwhich Sector to be plugged into
			$templateQuery = $libmysql->Query( " SELECT * FROM `tcm_template` WHERE `type` = '$this->siteType' " );
			while( $row = mysql_fetch_array( $templateQuery ) ) {
			
				// As {SC} is always unknown untill the page is loaded, we must replace the Section and Module parameters
				if ( $row['sector'] == 'SC' ) {
					$row['modname'] = _URLMOD;
					$row['section'] = _URLSEC;
				}
				
				// Store all results incase we need them later
				$tmpTemplate[] = $row;			
			
			}
			
			// Include the MDR
			if ( $this->siteType == 'main' ) {
				require_once( 'admin/kernel/core/libmdr.php' );
			} else {
				require_once( 'kernel/core/libmdr.php' );
			}
			$libmdr = new libmdr();
			
			foreach( $tmpTemplate as $key ) {			
				
				// Display rules - Damm that's gonna be hard, I think I'll delay that till 086				
				$row = $libmdr->displayRules( $key, $tmpTemplate );
				
				// We now need to store the Modules into an array and add single quotes to them
				// so that they can be used in a query to find the requested modules
				$templateModules[] = "'".$row['modname']."'";
				
				// Save results so it can be used later on
				$this->tcmTemplate[] = $row;
			
			}
			
			// Make the array a string to be used in the query
			$templateModules = implode( ', ', $templateModules );
			
			$this->findModules( $templateModules );							
		
		}		
		
		// Find all requested modules that are needed and see if SC needs the theme
		// Note: This does not check if the physical files/classes exist - this is purely to see if the module is in the database
		// findModules will output an array in this kind of structure
		/* array (
						0 => array(
										'NAME' => 'module',
										'SECTION' => 2
										'SECTOR' => 'SC',
										'PERMISSION' => 1,
										'TITLE' => 'Module Title',
										'THEME' => 1,
										)
					)
		*/
		function findModules( $modules ) {
		
			global $libmysql;

			$modulesQuery = $libmysql->Query( " SELECT * FROM `tcm_modules` WHERE `type` = '$this->siteType' AND `name` IN ( $modules ) ");
			
			// Loop the results to find out if SC needs the theme
			while ( $row = mysql_fetch_array( $modulesQuery ) ) {
				$tcmModules[] = $row;
			}
			
			/* Debug 
			echo '<pre>';
			echo print_r( $tcmModules );
			echo '</pre>';
			die();
			/* End Debug */
			
			// Loop the results from tcm_tcm
			foreach( $this->tcmTemplate as $key ) {
			
				// tmp vars to see what module/section is needed
				$tmpModule = strtolower( $key['modname'] );
				$tmpSection = strtolower( $key['section'] );
				
				/* Debug				
				echo 'Module: '.$tmpModule.' Section: '.$tmpSection.'<br />';							
				/* End Debug */ 
				
				// To see if the module section exists in the database, we need to see if either
				// the Permission, Theme or Title ( Doesn't matter which ) for that section exists
				foreach( $tcmModules as $modKey ) {					
					
					$foundModule = NULL;
					
					// As Array Index's start at 0, we need to alter the section for it to be correct
					$arraySection = $tmpSection - 1;
					
					// Get the correct values for Permission,Title and Theme based on the required Section
					$tmpPermission = explode( '-', $modKey['permission'] );
						$modPermission = $tmpPermission[ $arraySection ];
						// We don't check if we have a value for Permission, as this is to see if the Section Exists
					$tmpTitle = explode( '-', $modKey['title'] );
						$modTitle = $tmpTitle[ $arraySection ];
						// Check we have a value
						if ( empty( $modTitle ) ) {
							$modTitle = NULL;
						}						
					$tmpTheme = explode( '-', $modKey['theme'] );
						$modTheme = $tmpTheme[ $arraySection ];
						// Check we have a value
						if ( empty( $modTheme ) ) {
							$modTheme = 1;
						}					
					
					if ( strtolower( $modKey['name'] ) == $tmpModule && $modPermission != NULL ) {
						// This is the module we want, and the section exists in the database
						$requestedModules[] = array(
															'name'       => $tmpModule,
															'section'    => $tmpSection,
															'sector'     => $key['sector'],
															'permission' => $modPermission,
															'title'      => $modTitle,
															'theme'      => $modTheme,
															);
																			
						// See if the current module is in Sector SC - if so check if it needs the theme
						if ( $key['sector'] == 'SC' ) {
							$this->SCinfo[] = array(
														'name'       => $tmpModule,
														'section'    => $tmpSection,
														'sector'     => $key['sector'],
														'permission' => $modPermission,
														'title'      => $modTitle,
														'theme'      => $modTheme,
														);
						}
						
						// Boolean value to see if we have found a module for that sector
						$foundModule = 1;
						
						break;
					
					}					
				
				}
				
				// Check if a module was found for that sector
				if ( $foundModule != 1  ) {

						// Create a dummy array 
						$tmpArray =  array(
												'theme'   => 1,
												'missing' => 1,
												'sector'  => $key['sector'],
												);	
						$requestedModules[] = $tmpArray;
						
						// Copy the array over to the SC Info too									
						if ( $key['sector'] == 'SC' ) {
							$this->SCinfo[] = $tmpArray;
						}
						
						// Resert the dummy array
						$tmpArray = NULL;					
											
				}				
			
			}
			
			/* Debug
			echo '<pre>';
			echo print_r( $requestedModules );
			echo '</pre>';
			die();
			/* End Debug */
			
			// Load module with or without theme?
			if ( $this->SCinfo[0]['theme'] == 1 ) {		
				$this->loadTheme( $requestedModules );
			} else {		
				$this->loadNoTheme( $requestedModules );
			} 				
		
		}
		
		// Load the SC module with no theme - this is normaly used for like Updates in modules
		function loadNoTheme( $modules ) {
		
			foreach( $modules as $key ) {
			
				// Find the SC
				if ( $key['sector'] == 'SC' ) {
					
					// Check Permission
					if ( libPermission( $key['permission'] ) == 1 ) {
					
						// Check if Class name and File Exists
						$tmpFile = 'modules/'.$key['name'].'/'.$key['name'].'_mod.php';	
						
						if ( file_exists( $tmpFile ) ) {
							// Ok file Exists - lets see if the class does
							require_once( $tmpFile );
							$className = 'mod'.ucfirst( strtolower( $key['name'] ) ).'_sec'._URLSEC;
							
							if ( class_exists( $className ) ) {
								// Load the module!
								new $className();								
							} else {
								// Load the theme to show a Module doesn't exist error
								$this->loadTheme( $modules );
							}
							
						} else {
							// Load the theme to show a Module doesn't exist error
							$this->loadTheme( $modules );							
						}						
					
					} else {
						// Load the theme to show a Permission error
						$this->loadTheme( $modules );
						
					}				
				
				}
				
			}		
		
		}
		
		function loadTheme( $modules ) {
		
			global $libsession;
			
			// Choose what theme to load
			if ( $_GET['usetheme'] != NULL && is_dir( 'themes/'.$_GET['usetheme'] ) ) {
				$templateTheme = $_GET['usetheme'];
			} else if ( $this->siteType == 'admin' ) {
			
				if ( $libsession->LoggedIn == 1 ) {
					$templateTheme = "themes/"._CONFIG_THEME."/main_template.html";
				} else {
					$templateTheme = "themes/"._CONFIG_THEME."/login_template.html";
				}
				
			} else { 
				$templateTheme = "themes/"._CONFIG_THEME_MAIN."/main_template.html";
			}
			
			// Load the modules into the theme
			$this->loadModules( $modules, $templateTheme );
			
		}
		
		function loadModules( $modules, $templateTheme ) {
		
			// Loop every module to be replace into the correct Sectors of the template file
			// While doing this, we need to check for Permission, Module File Existing and Class exisitng
			foreach( $modules as $key ) {				
				
				// First we check if the user has permission to load this module
				if ( libPermission( $key['permission'] ) == 1 ) {
				
					// Ok so user has permission, next thing we do is see if the module file exists
					$tmpFile = 'modules/'.$key['name'].'/'.$key['name'].'_mod.php';					
					
					if ( file_exists( $tmpFile ) ) {
					
						require_once( $tmpFile );
						
						// Ok so the file exists - now we need to see if the class exists
						if ( $key['sector'] == 'SC' ) {
							$className = 'mod'.ucfirst( strtolower( $key['name'] ) ).'_sec'._URLSEC;								
						} else {						
							$className = 'mod'.ucfirst( strtolower( $key['name'] ) ).'_sec'.$key['section'];
						}
						
						if ( class_exists( $className ) ) {						
							
							// Start output buffering
							ob_start();
								new $className();
							$buffer = ob_get_clean();
							
							$templateTags[ $key['sector'] ] .= $buffer;
							
							// Gather CSS files ( if any ) and store them in an array
							$tempCSSDir = 'modules/'.$key['name'].'/template/'.$key['name'].'_css.css';					
						
							if ( file_exists( $tempCSSDir ) ) {
								// Put the CSS files into an array	
								$templateTags['CSS'] .= '<link rel="StyleSheet" href="'.$tempCSSDir.'" type="text/css" media="screen" />';
							}
						
						} else {
							global $libehc;
							$templateTags[ $key['sector'] ] .= $libehc->ErReport( 15 );
						}						
					
					} else {
						global $libehc;
						$templateTags[ $key['sector'] ] .= $libehc->ErReport( 15 );
					}
				
				} else if ( $key['missing'] == 1 ) {
					global $libehc;
					$templateTags[ $key['sector'] ] = $libehc->ErReport( 15 );
				} else {
				
					// If the module should be running in SC, we shall display a No Permission error
					if ( $key['sector'] == 'SC' ) {
						global $libehc;
						$templateTags[ $key['sector'] ] = $libehc->ErReport( 14 );
					}
					
				}							
			
			}
			
			/* Debug
			echo '<pre>';
			echo print_r( $templateTags );
			echo '</pre>';
			die();
			/* End Debug */
			
			// ** If your looking through the source code - Here is where you can add more Tag replacement tags ** \\
			// These are some Generic Hard Coded Tag's for people to use
			$templateTags['TITLE'] = _CONFIG_TITLE;			
					
			// Check for errors
			if ( $_SESSION['error'] != NULL ) {			
				global $libehc;				
				$error = $libehc->ErReport( $_SESSION{'error'] );
				$templateTags['ERROR'] = '<div class="main-error">'.$error.'</div>';
				$_SESSION['error'] = NULL;							
			} else {
				$templateTags['ERROR'] = NULL;
			}			
			
		
			// Check we have CSS's to load
			if ( $templateTags['CSS'] == NULL ) {
				$templateTags['CSS'] = '';
			}
			
			$template = new libtemplate( $templateTheme );
			$template->replace_tags( $templateTags );
			echo $template->page; 
		    
		} 			
						
	}	

?>