Code Search for Developers
 
 
  

ModulesManager.php from ECP (EliteCore Project) at Krugle


Show ModulesManager.php syntax highlighted

<?php


/**
 * Verifies if object $module is a valid module (instance of Module class)
 */
function is_module($module) {
	if (isset ($module) && $module instanceof Module || $module instanceof module_class) {
		// $module instanceof module_class = DEPRECATED
		return true;
	} else {
		return false;
	}
}

require_once ('core/Module.php');

/**
 * ModulesManager provides static methods for creating new Modules.
 *
 * @package ModulesManager
 * @author Luke Satin <cyberluk@seznam.cz>
 * @version 0.9.6
 */
class ModulesManager {
	const e000 = "Module is disabled.";
	const e001 = "You are not allowed to load this module.";
	const e002 = "Fatal Error - Couldn't create new instance.";
	const e003 = "Bad parameters";

	private static $Modules = array ();
	private static $modulesEnabled = true;
	private static $perspectiveSettings = false;

	/**
	 * Always returns a base name of Module
	 *
	 * If $instance is Survey_1, it returns Survey for example.
	 */
	public static function getModuleBase($instance) {
		$instance = explode("_", $instance);
		if (sizeof($instance) <= 1)
			$offset = 0;
		else
			$offset = 1;
		$retval = "";
		for ($i = 0; $i < sizeof($instance) - $offset; $i++) {
			$retval .= $instance[$i];
			if ($offset && $i != sizeof($instance) - $offset -1)
				$retval .= "_";
		}
		return $retval;
	}

	/**
	 * Searches the array of active modules for specified name.
	 *
	 * @param string $search Name of active module to search for.
	 * @deprecated
	 */
	public static function getModule($search) {
		try {
			return self :: loadModule($search);
		} catch (NoModuleException $e) {
			Debug :: addReport($e->getMessage(), Debug :: ERROR);
		}
	}

	/**
	 * Returns an array of loaded modules
	 */
	public static function getModules() {
		if (!sizeof(self :: $Modules))
			self :: loadModules();
		return self :: $Modules;
	}

	public static function disableAll() {
		self :: $modulesEnabled = false;
	}
	public static function enableModule($module) {
		if (!is_array(self :: $modulesEnabled))
			self :: $modulesEnabled = array ();

		if (is_module($module))
			self :: $modulesEnabled[$module->getInstance()] = true;
		else
			self :: $modulesEnabled[$module] = true;
	}
	public static function isEnabled($module) {
		if (!is_array(self :: $modulesEnabled) || !sizeof(self :: $modulesEnabled))
			return true;
		else
			if (is_module($module) && isset (self :: $modulesEnabled[$module->getInstance()]) && self :: $modulesEnabled[$module->getInstance()] === true)
				return true;
			else
				if (isset (self :: $modulesEnabled[$module]) && self :: $modulesEnabled[$module] === true)
					return true;
				else
					return false;
	}

	/**
	 * Tries to load a module.
	 *
	 * If the module is loaded already, it's instance is returned.
	 * If the module can't be loaded, new NoModuleException is thrown.
	 *
	 * @param string $name
	 * @param string $instance You can also specify unique instance id, if you want a specific instance of Module (one of MenuModule instances for example)
	 * @return Module Returns new or existing instance of Module object.
	 */
	public static function loadModule($name, $instance = false) {
		if (empty ($name))
			throw new NoModuleException(self :: e003);

		// Check if module already exists
		foreach (self :: $Modules as $Module) {
			if ($Module->getInstance() == $name)
				return $Module;
		}

		// 	Load module
		$TA = new TA();
		$TA->addQuery(TA :: SELECT, "ecp_engine_modules");
		$TA->WHERE("`base`='" . $name . "'");
		$TA->Execute();
		$Module = $TA->Result()->FetchObject();
		if ($Module) {
			return self :: configureModule($Module);
		} else {
			throw new NoModuleException(self :: e002);
		}
	}

	/**
	 * Does several checks in Module before it's created.
	 *
	 * If there's any error, a new NoModuleException is thrown
	 *
	 * @access private
	 */
	private static function configureModule($dbModule) {
		$dbModule = self :: checkPerspective($dbModule);
		$dbModule = self :: checkPermissions($dbModule);
		$dbModule = self :: templateModule($dbModule);
		$Module = $dbModule;
		if (is_module($Module)) {
			self :: $Modules[] = $Module;
			return $Module;
		} else {
			throw new NoModuleException(self :: e002);
		}
	}

	/**
	 * Loads all possible modules
	 */
	public static function loadModules() {
		if (sizeof(self :: $Modules)) {
			// Modules are already loaded
			return false;
		}
		// Load active modules
		$TA = new TA();
		$TA->addQuery(TA :: SELECT, "ecp_engine_modules");
		$TA->Execute();
		while ($Module = $TA->Result()->FetchObject()) {
			try {
				self :: configureModule($Module);
			} catch (NoModuleException $e) {
				Debug :: addReport($e->getMessage(), Debug :: WARNING);
			}
		}
	}

	/**
	 * Returns module's nick
	 *
	 * @param string $instance
	 * @deprecated
	 */
	public static function getModuleNick($instance) {
		$TA = new TA();
		$TA->addQuery(TA :: SELECT, "ecp_engine_modules");
		$TA->addParam("name");
		$TA->WHERE("`base`='" . $instance . "' OR `instance_name`='" . $instance . "'");
		$TA->LIMIT(1);
		$TA->Execute();
		return $TA->Result()->FetchObject()->name;
	}

	/**
	 * configureModule() subroutine
	 *
	 * @access private
	 */
	private static function checkPerspective($dbModule) {
		// Get module's properties for current perspective
		if (self :: $perspectiveSettings === false) {
			$TA = new TA();
			self :: $perspectiveSettings = array ();
			if (Perspective :: getCurrent() != "none") {
				// If any perspective has been activated, then proceed
				$TA->addQuery(TA :: SELECT, "ecp_engine_perspective_" . Perspective :: getCurrent(), "perspective");
				$TA->Execute();
				while ($object = $TA->Result("perspective")->FetchObject()) {
					// Get all modules with custom properties in current perspective
					self :: $perspectiveSettings[$object->id] = $object;
				}
			}
		}
		if (array_key_exists($dbModule->id, self :: $perspectiveSettings)) {
			// If current module has any custom properties
			$dbModule->status = self :: $perspectiveSettings[$dbModule->id]->status;
			$dbModule->template = self :: $perspectiveSettings[$dbModule->id]->template;
		}
		return $dbModule;
	}

	/**
	 * configureModule() subroutine
	 *
	 * @access private
	 */
	private static function checkPermissions($dbModule) {
		if (!$dbModule->status && !User :: inAdmin() && !User :: $profile->admin) {
			// Module is not enabled or user is not in admin and user is not super-admin
			throw new NoModuleException(self :: e000);
		}

		if ($dbModule->instance_name != "none") {
			// Module is another's Module instance
			$Table = $dbModule->instance_name;
		} else {
			// Module is not instance of any other Module
			$Table = $dbModule->base;
		}
		if ($_GET['module'] == $Table && isset ($_GET['site'])) {
			// We want to load module from another site in current database
			// DISABLED
			//DB :: SetPrefix($_GET['site']);
		}

		$TA = new TA();
		$TA->addQuery(TA :: SELECT, "ecp_modules_access", "accessTable");
		$TA->WHERE("module='" . $Table . "'");
		$TA->Execute("accessTable");
		$accessRights = array ();
		$Allow = false; // this module can't be loaded now (we must check the access rights first)
		while ($access = $TA->Result("accessTable")->FetchObject()) {
			// fill the array with data from accessTable
			$accessRights[] = $access;
		}
		$TA->RemoveQuery();
		foreach ($accessRights as $Rights) {
			// Check if group 0(all) or User's group has access to this module
			if ($Rights->read) {
				if ($Rights->group == 0 || $Rights->group == User :: $profile->group) {
					$Allow = true; // user has rights to load this module
					$dbModule->accessRights = $accessRights;
					break;
				}
			}
		}

		if ($_GET['module'] == $Table && isset ($_GET['site'])) {
			// DISABLED
			//DB :: RestorePrefix();
		}

		if ($Allow) {
			return $dbModule;
		} else {
			throw new NoModuleException(self :: e001);
		}
	}

	private static function detectMainFile($name) {
		$subroutinesFile = "modules/" . $name . "/subroutines.php";
		if (!Engine :: file_exists($subroutinesFile)) {
			$subroutinesFile = "modules/" . $name . "/".$name.".php";
		}
		if (Engine :: file_exists($subroutinesFile)) {
			return $subroutinesFile;
		} else {
			return false;
		}
	}

	/**
	 * configureModule() subroutine
	 *
	 * @access private
	 */
	private static function templateModule($dbModule) {
		//
		// ECP MODULE 2.0 SUPPORT
		//
		$subroutinesFile = self::detectMainFile($dbModule->base);
		if ($subroutinesFile) {
			require_once ($subroutinesFile);
			$baseClass = $dbModule->base;
			$className = $baseClass . 'Module';
			if (class_exists($className)) {
				$class = new ReflectionClass($className);
			}
			if (isset ($class)) {
				$Module = $class->newInstance($dbModule);
				// REWRITE ALL TEMPLATES WITH AJAX SUPPORT
				Engine :: setFlag('currentModule', $Module);
				$ajaxActions = Cache :: getMetadata($subroutinesFile);
				if ($ajaxActions === false) {
					$ajaxActions = Ajax :: getActionIdsFromFile($subroutinesFile);
					Cache :: FileMetadata($subroutinesFile, $ajaxActions);
				}
				if ($ajaxActions !== false) {
					foreach ($ajaxActions as $action) {
						Template :: ReplaceAll('<?=' . $action . '?>', '<div class="ajECP" id="' . Ajax :: generateID($action, $Module->getName()) . '"><?php echo ' . $action . '?></div>', $Module);
						Template :: ReplaceAll('<?=', '<?php echo ', $Module);
					}
					Template :: Write();
				}

				// AJAX EXPORT ALL METHODS
				foreach ($class->getMethods() as $method) {
					// Method starts with 'on' and it's not onDefault() method
					if ($method->getName() != 'onDefault' && strpos($method->getName(), 'on') === 0) {
						$parameters = false;
						foreach ($method->getParameters() as $parameter) {
							try {
								$defaultValue = $parameter->getDefaultValue();
								$parameters[] = $defaultValue;
							} catch (ReflectionException $e) {
								$parameters[] = $parameter->getName();
							}
						}
						if ($parameters === false) {
							Ajax :: Export($Module->getName() . '->' . $method->getName());
						} else {
							if (sizeof($parameters) == 1) {
								$parameters = $parameters[0];
							}
							Ajax :: Export($Module->getName() . '->' . $method->getName(), $parameters);
						}
					}
				}
				return $Module;
			} else {
				return false;
			}
			//
			//
			//
		} else
			if (Engine :: file_exists("modules/" . $dbModule->base . "/class.php")) {
				//
				// ECP MODULE 1.0
				//
				require_once ("modules/" . $dbModule->base . "/class.php");
				if (User :: inAdmin() && class_exists("module_" . $dbModule->base . "_admin"))
					$class = new ReflectionClass("module_" . $dbModule->base . "_admin");
				else
					if (class_exists("module_" . $dbModule->base))
						$class = new ReflectionClass("module_" . $dbModule->base);
				$Module = new Module($dbModule);
				if (isset ($class)) {
					$Module->object = $class->newInstance();
				}
				return $Module;
				//
				//
				//
			} else {
				return false;
			}
	}
	public static function launchState($Module = false, $State = false) {
		global $ECP;

		$Returns=false;
		if ($Module === false) {
			$Module = Engine :: getFlag('currentModule');
		}
		if (empty($Module)) {
			return false;
		}
		if (!is_module($Module)) {
			$Module=self::loadModule($Module);
		}

		if ($State === false) {
			if ($_GET['module'] == $Module->getInstance()) {
				$State = $_GET['op'];
			} else {
				$State = 'Default';
			}
		}
		if (empty ($State))
			$State = 'Default';

		$launchParam = Engine :: getFlag('launchParam');

		if (($Module->getStatus() == 1 || User :: inAdmin()) && (User :: $profile->admin || $Module->getName() != "modules")) {

			$rights = $Module->getRights(User :: $profile->group);

			// ECP MODULE 2.0 SUPPORT
			$subroutinesFile = self::detectMainFile($Module->getName());

			if ($subroutinesFile) {
				try {

					if (method_exists($Module, 'on' . $State)) {
						if (strpos($State, 'admin') === 0 && !$rights->write) {
							// You can't load admin method when you don't have required rights
						} else {
							$Returns=$Module-> {
								'on' . $State }
							($launchParam);
						}
					} else {
						// No default handler, proceed to next module
					}

				} catch (UndefinedDataException $e) {
					// input data (from $_POST etc.) doesn't exist, skip this module
				} catch (AjaxFlushException $e) {
					echo $e->getMessage();
					throw new AjaxFlushException($e->getMessage());
				}
			} else
				if ($Module->display_admin) {
					if ($rights->write) {
						require ("modules/" . $Module->getName() . "/admin.php"); // display administration of current module
					} else {
						Engine :: CallError(401);
					}
				} else
					if ($Module->getStatus() == 1) {
						if ($Module->getBlock() == "none" || $Module->block == "") {
							if (Engine :: file_exists("modules/" . $Module->getName() . "/module.php")) {
								require ("modules/" . $Module->getName() . "/module.php"); // display normal output
							}
						} else {
							if (Engine :: file_exists("modules/" . $Module->getName() . "/blocks/" . $Module->getBlock() . ".php"))
								require ("modules/" . $Module->getName() . "/blocks/" . $Module->getBlock() . ".php"); // display block's output
						}
					}

		}
		return $Returns;

	}
	public static function switchModule($Module = false) {
		if ($Module === false) {
			Engine :: setFlag('currentModule', Engine :: getFlag('parentModule'));
		} else {
			if (!Engine :: getFlag('parentModule'))
				Engine :: setFlag('parentModule', Engine :: getFlag('currentModule'));
			Engine :: setFlag('currentModule', $Module);
		}
	}
	public static function switchBack() {
		self::switchModule();
	}
}
?>



See more files for this project here

ECP (EliteCore Project)

EliteCore Project is a PHP5.1/Javascript/AJAX/XHTML/CSS framework for creating WEB 2.0 applications and services.The basic open-source instalation can be also used as an interactive personal page or BLOG.This project uses the latest features available.

Project homepage: http://sourceforge.net/projects/elitecore
Programming language(s): JavaScript,PHP,XML
License: cpl

  debug/
    content.php
  exceptions/
    ajaxflush.php
    nomodule.php
    undefineddata.php
  interface/
    encryption.php
    form.php
    module_class.php
    session_interface.php
  renderers/
    default.php
  sql/
    mysql.php
    mysqli.php
  themes/
    ECP/
      accept.png
      add.png
      alt_star.gif
      anchor.png
      arrow_refresh.png
      asterisk_orange.png
      asterisk_yellow.png
      attach.png
      back.png
      cog_error.png
      cog_go.png
      comment.png
      comment_add.png
      comment_delete.png
      comment_edit.png
      comments.png
      comments_add.png
      comments_delete.png
      control_play_blue.png
      drive.png
      gnome-fs-directory.png
      gnome-mime-audio.png
      layers.png
      layout.png
      layout_add.png
      layout_content.png
      layout_delete.png
      layout_edit.png
      layout_error.png
      layout_header.png
      layout_link.png
      layout_sidebar.png
      lightbulb.png
      lightbulb_add.png
      lightbulb_delete.png
      lightbulb_off.png
      lightning.png
      lightning_add.png
      lightning_delete.png
      lightning_go.png
      link.png
      link_add.png
      link_break.png
      link_delete.png
      link_edit.png
      link_error.png
      link_go.png
      lock.png
      lock_add.png
      lock_break.png
      lock_delete.png
      lock_edit.png
      lock_go.png
      lock_open.png
      newspaper.png
      newspaper_add.png
      newspaper_delete.png
      newspaper_go.png
      newspaper_link.png
      note.gif
      note.png
      note_add.png
      note_delete.gif
      note_delete.png
      note_edit.png
      note_error.png
      note_go.png
      note_new.gif
      overlays.png
      package.png
      package_add.png
      package_delete.png
      package_go.png
      package_green.png
      package_link.png
      page.gif
      page.png
      page_add.png
      page_attach.png
      page_code.png
      page_copy.png
      page_delete.png
      page_edit.png
      page_error.png
      page_excel.png
      page_find.png
      page_gear.png
      page_go.png
      page_green.png
      page_key.png
      page_lightning.png
      page_link.png
      page_paintbrush.png
      page_paste.png
      page_red.png
      page_refresh.png
      page_save.png
      page_white.png
      pencil.png
      pencil_add.png
      pencil_delete.png
      pencil_go.png
      photo.png
      photo_add.png
      photo_delete.png
      photo_link.png
      photos.png
      picture.png
      picture_add.png
      picture_delete.png
      picture_edit.png
      picture_empty.png
      picture_error.png
      picture_go.png
      picture_key.png
      picture_link.png
      picture_save.png
      pictures.png
      plugin.png
      plugin_add.png
      plugin_delete.png
      plugin_disabled.png
      plugin_edit.png
      plugin_error.png
      plugin_go.png
      plugin_link.png
      report.png
      report_add.png
      report_delete.png
      report_disk.png
      report_edit.png
      report_go.png
      report_key.png
      report_link.png
      report_magnify.png
      report_picture.png
      report_user.png
      report_word.png
      script.png
      script_add.png
      script_code.png
      script_code_red.png
      script_delete.png
      script_edit.png
      script_error.png
      script_gear.png
      script_go.png
      script_key.png
      script_lightning.png
      script_link.png
      script_palette.png
      script_save.png
      star.png
      star_rating.gif
      stop.png
      style.png
      text_align_center.png
      text_align_justify.png
      text_align_left.png
      text_align_right.png
      text_allcaps.png
      text_bold.png
      text_columns.png
      text_dropcaps.png
      text_heading_1.png
      text_heading_2.png
      text_heading_3.png
      text_heading_4.png
      text_heading_5.png
      text_heading_6.png
      text_horizontalrule.png
      text_indent.png
      text_indent_remove.png
      text_italic.png
      text_kerning.png
      text_letter_omega.png
      text_letterspacing.png
      text_linespacing.png
      text_list_bullets.png
      text_list_numbers.png
      text_lowercase.png
      text_padding_bottom.png
      text_padding_left.png
      text_padding_right.png
      text_padding_top.png
      text_replace.png
      text_signature.png
      text_smallcaps.png
      text_strikethrough.png
      text_subscript.png
      text_superscript.png
      text_underline.png
      text_uppercase.png
      textfield.png
      textfield_add.png
      textfield_delete.png
      textfield_key.png
      textfield_rename.png
      tux.png
      vert_star.gif
    ECP.xml
  Icon.php
  Location.php
  Module.php
  ModulesManager.php
  MusicTags.php
  Page.php
  XHTMLParser.php
  XMLForms.php
  ajax.php
  author.html
  cache.php
  config.php
  date.php
  db.php
  debug.php
  ecp-full.php
  ecp-mini.php
  engine.php
  events.php
  filesystem.php
  footer.html
  i18n.php
  mailer.php
  main.css
  mcrypt.php
  mime.php
  mod_rewrite.php
  perspective.php
  rc4.php
  reflection.php
  session_passport.php
  storage.php
  string.php
  template.php
  texy.php
  user.php
  user_cache.php
  wysiwyg_texy.php
  xhtml_form.php
  xtea.php