plugin.class.php
上传用户:stephen_wu
上传日期:2008-07-05
资源大小:1757k
文件大小:91k
- <?php
- /**
- * Joomla/Mambo Community Builder : Plugin Handler
- * @version $Id: plugin.class.php 610 2006-12-13 17:33:44Z beat $
- * @package Community Builder
- * @subpackage plugin.class.php
- * @author various, JoomlaJoe and Beat
- * @copyright (C) JoomlaJoe and Beat, www.joomlapolis.com and various
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
- */
- // ensure this file is being included by a parent file
- if ( ! ( defined( '_VALID_CB' ) || defined( '_JEXEC' ) || defined( '_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }
- /**
- * CB framework
- * @global CBframework $_CB_framework
- */
- global $_CB_framework;
- global $mainframe;
- if ( defined( 'JPATH_ADMINISTRATOR' ) ) {
- $foundation = JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php';
- } else {
- $foundation = $mainframe->getCfg( 'absolute_path' ) . '/administrator/components/com_comprofiler/plugin.foundation.php';
- }
- include_once( $foundation );
- cbimport( 'cb.database' );
- /** utility: adds all vars of object $src to object $obj except the variable named in array exclude Array */
- function addVarsToClass( &$obj, $src, $excludeArray ) {
- foreach( get_object_vars( $src ) as $key => $val ) {
- if ( ! in_array( $key, $excludeArray ) ) {
- $obj->$key = $val;
- }
- }
- }
- class cbPluginHandler {
- /** @var array An array of functions in event groups */
- var $_events = null;
- /** @var array An array of classes and pluginids for field-types */
- var $_fieldTypes = array();
- /** @var array An array of classes for additional field-parameters */
- var $_fieldParams = array();
- /** @var array An array of menu and status items (array) */
- var $_menus = null;
- /** @var array An array of lists */
- var $_lists = null;
- /** @var array An array of loaded plugins objects, index=pluginId */
- var $_plugins = array();
- /** @var array An array indexed by the group-name of arrays of plugin ids of the plugins already loaded containing stdClass objects of the plugin table entry */
- var $_pluginGroups = array();
- /** @var int Index of the plugin being loaded */
- var $_loading = null;
- /** @var array collection of debug data */
- var $debugMSG = array();
- /** @var string Error Message*/
- var $errorMSG = array();
- var $_iserror = false;
- var $params = null;
-
- /**
- * Constructor
- */
- function cbPluginHandler() {
- $this->_events = array();
- }
- /**
- * Loads all the bot files for a particular group (if group not already loaded)
- * @param string The group name, relates to the sub-directory in the plugins directory
- * @param mixed array of int : ids of plugins to load. OR: string : name of class
- * @param int if 1 (DEFAULT): load only published plugins, if 0: load all plugins including unpublished ones
- * @return boolean TRUE: load done, FALSE: no plugin loaded
- */
- function loadPluginGroup( $group, $ids = null, $publishedStatus = 1 ) {
- global $_CB_framework, $_CB_database;
- static $dbCache = null;
- $this->_iserror = false;
- $group = trim( $group );
- if ( ( $group && ! isset( $this->_pluginGroups[$group] ) ) || ( ! $this->all_in_array_key( $ids, $this->_plugins ) ) ) {
- $cmsAccess = $_CB_framework->myCmsGid();
- if ( ! isset( $dbCache[$publishedStatus][$cmsAccess][$group] ) ) {
- $where = array();
- if ( $publishedStatus == 1 ) {
- $where[] = 'published = 1';
- } else {
- $where[] = 'published >= ' . (int) $publishedStatus;
- }
- $where[] = 'access <= '. (int) $cmsAccess;
- if ( $group ) {
- $where[] = 'type = ' . $_CB_database->Quote( trim ( $group ) );
- }
- /*
- if ( ( $ids !== null ) && ( count( $ids ) > 0 ) ) {
- cbArrayToInts( $ids );
- if ( count( $ids ) == 1 ) {
- $where[] = 'id = ' . implode( '', $ids );
- } else {
- $where[] = 'id IN (' . implode( ',', $ids ) . ')';
- }
- }
- */
- $_CB_database->setQuery( "SELECT id, folder, element, published, type, params, CONCAT_WS('/',folder,element) AS lookup, name"
- . "n FROM #__comprofiler_plugin"
- . "n WHERE " . implode( ' AND ', $where )
- . "n ORDER BY ordering"
- );
- $dbCache[$publishedStatus][$cmsAccess][$group] = $_CB_database->loadObjectList();
- if ( $dbCache[$publishedStatus][$cmsAccess][$group] === null ) {
- return false;
- }
- }
- if ( count( $ids ) == 0 ) {
- $ids = null;
- }
- foreach ( $dbCache[$publishedStatus][$cmsAccess][$group] AS $plugin ) {
- if ( ( $ids === null ) || in_array( $plugin->id, $ids ) ) {
- if ( ( ! isset( $this->_plugins[$plugin->id] ) ) && $this->_loadPluginFile( $plugin ) ) {
- $this->_plugins[$plugin->id] = $plugin;
- if ( ! isset( $this->_pluginGroups[$plugin->type][$plugin->id] ) ) {
- $this->_pluginGroups[$plugin->type][$plugin->id] =& $this->_plugins[$plugin->id];
- }
- }
- }
- }
- }
- return true;
- }
- /**
- * Returns array of plugins which got loaded through loadPluginGroup method for that $group
- * Returns empty array() if none is loaded.
- *
- * @param string $group
- * @return array keyed array ( pluginid => plugin row ( + lookup = path to plugin files
- */
- function & getLoadedPluginGroup( $group ) {
- if ( isset( $this->_pluginGroups[$group] ) ) {
- return $this->_pluginGroups[$group];
- } else {
- $array = array();
- return $array;
- }
- }
- /** utility: checks if all elements of array needles are in array haystack */
- function all_in_array($needles,$haystack) {
- if (is_array($needles)) {
- foreach ($needles as $needle) {
- if (!in_array($needle,$haystack)) return false;
- }
- } else {
- if (!in_array($needles,$haystack)) return false;
- }
- return true;
- }
- /** utility: checks if all elements of array needles are in array haystack */
- function all_in_array_key($needles,$haystack) {
- if (is_array($needles)) {
- foreach ($needles as $needle) {
- if (!array_key_exists($needle,$haystack)) return false;
- }
- } else {
- if (!array_key_exists($needles,$haystack)) return false;
- }
- return true;
- }
- function _setLoading( $plugin, $loading = true ) {
- $savePreviousPluginId = $this->_loading;
- if ( $loading === true ) {
- $this->_loading = $plugin->id;
- } elseif ( $loading === false) {
- $this->_loading = null;
- } else {
- $this->_loading = $loading;
- }
- return $savePreviousPluginId;
- }
- function _loadPluginFile($plugin) {
- global $_CB_framework, $_PLUGINS;
-
- $path = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/plugin/' . $plugin->type . '/'. $plugin->folder . '/' . $plugin->element . '.php';
- if ( file_exists( $path ) && is_readable( $path ) ) {
- $savePreviousPluginId = $this->_setLoading( $plugin, true );
- require_once( $path );
- $this->_setLoading( $plugin, $savePreviousPluginId );
- return true;
- } else {
- return false;
- }
- }
- function getPluginId( ) {
- global $_PLUGINS;
- return $_PLUGINS->_loading;
- }
- function & getPluginObject( $pluginId = null ) {
- global $_PLUGINS;
- if ( $pluginId === null ) {
- $pluginId = $_PLUGINS->_loading;
- }
- return $_PLUGINS->_plugins[$pluginId];
- }
- function & getInstanceOfPluginClass( $class, $pluginId = null ) {
- global $_PLUGINS;
- if ( $pluginId === null ) {
- $pluginId = $_PLUGINS->_loading;
- }
- if ( ! isset( $this->_plugins[$pluginId]->classInstance[$class] ) ) {
- if ( ! isset( $this->_plugins[$pluginId]->classInstance ) ) {
- $this->_plugins[$pluginId]->classInstance = array();
- }
- $this->_plugins[$pluginId]->classInstance[$class] = new $class();
- $this->_plugins[$pluginId]->classInstance[$class]->_cbpluginid = $pluginId;
- }
- return $this->_plugins[$pluginId]->classInstance[$class];
- }
- /**
- * Gets a variable from the plugin class
- * @param int id of plugin
- * @param string name of plugin class
- * @param string name of class variable
- * @return mixed : variable's content
- */
- function getVar($pluginId, $class, $variable) {
- if ($class!=null && class_exists($class) && isset( $this->_plugins[$pluginId] ) ) {
- if ($this->_plugins[$pluginId]->published) {
- if (isset( $this->_plugins[$pluginId]->classInstance[$class]->$variable )) {
- return $this->_plugins[$pluginId]->classInstance[$class]->$variable;
- }
- }
- }
- return false;
- }
- function getPluginPath() {
- global $_CB_framework, $_PLUGINS;
- // $plugin = $_PLUGINS->_pluginGroups[$this->type][$this->_cbpluginid]; //TBD: check for multiple classes per plugin ??? + getPluginCLass/vs. getTabCLass
- $plugin =& $_PLUGINS->_plugins[$this->_cbpluginid]; //TBD: remove those vars from here and make this function available to API
- $path = $_CB_framework->getCfg('absolute_path') . '/components/com_comprofiler/plugin/' . $plugin->type . '/'. $plugin->folder;
- return $path;
- }
- function getPluginLIvePath() {
- global $_CB_framework, $_PLUGINS;
- // $plugin = $_PLUGINS->_pluginGroups[$this->type][$this->_cbpluginid]; //TBD: check for multiple classes per plugin ??? + getPluginCLass/vs. getTabCLass
- $plugin =& $_PLUGINS->_plugins[$this->_cbpluginid]; //TBD: remove those vars from here and make this function available to API
- $path = $_CB_framework->getCfg('live_site') . '/components/com_comprofiler/plugin/' . $plugin->type . '/'. $plugin->folder;
- return $path;
- }
- function _loadParams($pluginId, $extraParams=null) {
- global $_PLUGINS;
- $this->params = new cbParamsBase($_PLUGINS->_plugins[$pluginId]->params . "n" . $extraParams);
- }
- function & getParams() {
- return $this->params;
- }
- function getXml( $type = null, $typeValue = null ) {
- return null; // override if needed
- }
- /**
- * FIELDS PLUGINS MANAGEMENT: through $_PLUGINS only:
- */
- /**
- * Registers a field type which can be used by users
- *
- * @param array $typesArray array of string of the names of types of fields
- * @param int $pluginId for CB internal plugin installer use ONLY: id of plugin to associate with field type
- */
- function registerUserFieldTypes( $typesArray, $pluginId = null ) {
- if ( $pluginId === null ) {
- $pluginId = $this->_loading;
- }
- foreach ( $typesArray as $type => $class ) {
- $this->_fieldTypes[$type] = array( $class, $pluginId );
- }
- }
- /**
- * Returns array of field types
- *
- * @return array of string Names of types registered
- */
- function getUserFieldTypes( ) {
- return array_keys( $this->_fieldTypes );
- }
- /**
- * Returns array of field types
- *
- * @param string $fieldType Type of field
- * @return array of string Names of types registered
- */
- function getUserFieldPluginId( $fieldType ) {
- if (isset( $this->_fieldTypes[$fieldType] ) ) {
- return $this->_fieldTypes[$fieldType][1];
- }
- return null;
- }
- /**
- * Calls a function of a field type
- *
- * @param string $fieldType Type of field
- * @param string $method Method to call
- * @param array $args An array of arguments
- * @return mixed result of function call or NULL if non-existant
- */
- function callField( $fieldType, $method, $args = null, &$field ) {
- $result = null;
- if ($args === null) {
- $args = array();
- }
- if ( isset( $this->_fieldTypes[$fieldType] ) ) {
- $result = $this->call( $this->_fieldTypes[$fieldType][1], $method, $this->_fieldTypes[$fieldType][0], $args );
- }
- return $result;
- }
- /**
- * Registers field params for fields
- *
- * @param $class name of class if overriding core class cbFieldParamsHandler which then needs to be extended.
- */
- function registerUserFieldParams( $class = null ) {
- $pluginId = $this->_loading;
- if ( $class === null ) {
- $class = 'cbFieldParamsHandler';
- }
- $this->_fieldParams[$pluginId] = $class;
- }
- /**
- * Returns array of field types
- *
- * @return array of string pluginid => Names of class
- */
- function getUserFieldParamsPluginIds( ) {
- return $this->_fieldParams;
- }
- /**
- * MENU MANAGEMENT:
- */
- /**
- * Adds a menu
- * @access private
- *
- * @param unknown_type $menuItem
- */
- function _internalPLUGINSaddMenu( $menuItem ) {
- $this->_menus[] = $menuItem;
- }
- /**
- * Registers a menu or status item to a particular menu position
- *
- * @param array a menu item like:
- // Test example:
- $mi = array(); $mi["_UE_MENU_CONNECTIONS"]["duplique"]=null;
- $this->addMenu( array( "position" => "menuBar" , // "menuBar", "menuList"
- "arrayPos" => $mi ,
- "caption" => _UE_MENU_MANAGEMYCONNECTIONS ,
- "url" => cbSef($ue_manageConnection_url) , // can also be "<a ....>" or "javascript:void(0)" or ""
- "target" => "" , // e.g. "_blank"
- "img" => null , // e.g. "<img src='plugins/user/myplugin/images/icon.gif' width='16' height='16' alt='' />"
- "alt" => null , // e.g. "text"
- "tooltip" => _UE_MENU_MANAGEMYCONNECTIONS_DESC ,
- "keystroke" => null ) ); // e.g. "P"
- // Test example: Member Since:
- $mi = array(); $mi["_UE_MENU_STATUS"]["_UE_MEMBERSINCE"]["dupl"]=null;
- $dat = cbFormatDate($user->registerDate);
- if (!$dat) $dat="?";
- $this->addMenu( array( "position" => "menuList" , // "menuBar", "menuList"
- "arrayPos" => $mi ,
- "caption" => $dat ,
- "url" => "" , // can also be "<a ....>" or "javascript:void(0)" or ""
- "target" => "" , // e.g. "_blank"
- "img" => null , // e.g. "<img src='plugins/user/myplugin/images/icon.gif' width='16' height='16' alt='' />"
- "alt" => null , // e.g. "text"
- "tooltip" => _UE_MEMBERSINCE_DESC ,
- "keystroke" => null ) ); // e.g. "P"
- */
- function addMenu( $menuItem ) {
- global $_PLUGINS;
- $_PLUGINS->_internalPLUGINSaddMenu($menuItem);
- }
- /**
- * Returns all menu items registered with addMenu
- * @param string The event name
- * @param string The function name
- */
- function getMenus() {
- return $this->_menus;
- }
- /**
- * EVENTS AND TRIGGERS METHODS:
- */
- /**
- * Registers a function to a particular event group
- * @param string The event name
- * @param string The function name
- */
- function registerFunction( $event, $method, $class=null ) {
- $this->_events[$event][] = array( $class,$method, $this->_loading );
- }
- /**
- * Calls all functions associated with an event group
- * @param string The event name
- * @param array An array of arguments
- * @return array An array of results from each function call
- */
- function trigger( $event, $args=null ) {
- $result = array();
- if ($args === null) {
- $args = array();
- }
- if (isset( $this->_events[$event] ) ) {
- foreach ($this->_events[$event] as $func) {
- $result[] = $this->call($func[2],$func[1],$func[0],$args);
- }
- }
- return $result;
- }
- function is_errors() {
- return $this->_iserror;
- }
- /**
- * Execute the plugin class/method pair
- * @param int id of plugin
- * @param string name of plugin variable
- * @param mixed value to assign (if any)
- * @return mixed : previous value
- */
- function plugVarValue($pluginid, $var, $value=null) {
- $preValue = $this->_plugins[$pluginid]->$var;
- if ( $value !== null ) {
- $this->_plugins[$pluginid]->$var = $value;
- }
- return $preValue;
- }
- /**
- * Execute the plugin class/method pair
- * @param $pluginid int id of plugin
- * @param $method string name of plugin method
- * @param $class string name of plugin class
- * @param $args array set of variables to path to class/method
- * @param $extraParams string additional parameters external to plugin params (e.g. tab params)
- * @return mixed : either string HTML for tab content, or false if ErrorMSG generated
- */
- function call( $pluginid, $method, $class, &$args, $extraParams = null, $ignorePublishedStatus = false ) {
- if ( $class != null && class_exists( $class ) ) {
- if ( $this->_plugins[$pluginid]->published || $ignorePublishedStatus ) {
- $pluginClassInstance =& $this->getInstanceOfPluginClass( $class, $pluginid );
- if (method_exists( $pluginClassInstance, $method )) {
- $pluginClassInstance->_loadParams( $pluginid, $extraParams );
- //BB1.2b7: really needed to have plugin row in tab and field classes called ??? element below should be enough: addVarsToClass($pluginClassInstance, $this->_plugins[$pluginid], array( 'params', 'classinstance' ));
- $pluginClassInstance->element = $this->_plugins[$pluginid]->element; // needed for _getPrefix for _getReqParam & co
- $savePreviousPluginId = $this->_loading;
- $this->_loading = $pluginid;
- $ret = call_user_func_array( array( &$pluginClassInstance, $method ), $args );
- $this->_loading = $savePreviousPluginId;
- return $ret;
- }
- }
- } elseif (function_exists( $method )) {
- if ( $this->_plugins[$pluginid]->published || $ignorePublishedStatus ) {
- $this->_loadParams($pluginid, $extraParams);
- $savePreviousPluginId = $this->_loading;
- $this->_loading = $pluginid;
- $ret = call_user_func_array( $method, $args );
- $this->_loading = $savePreviousPluginId;
- return $ret;
- }
- }
- return false;
- }
- /**
- * PRIVATE method: sets the text of the last error
- * @access private
- *
- * @param string $msg error message
- * @return boolean true
- */
- function _setErrorMSG( $msg ) {
- $this->errorMSG[] = $msg;
- return true;
- }
- /**
- * Gets the text of the last error:
- * $separator == FALSE: always returns array
- * $separator is string: returns null or string of errors
- *
- * @param string|boolean $separator FALSE: return array, STRING: Separator between the errors which are imploded from array
- * @return string|array Text for error message or array of texts of error messages.
- */
- function getErrorMSG( $separator = "n" ) {
- if ( $separator === false ) {
- return $this->errorMSG;
- } else {
- $error = null;
- if ( count( $this->errorMSG ) > 0 ) {
- $error = implode( $separator, $this->errorMSG );
- }
- return $error;
- }
- }
- /**
- * PRIVATE method: sets the error condition and priority (for now 0)
- * @param error priority
- * @return boolean true
- */
- function raiseError($priority) {
- $this->_iserror=true;
- return true;
- }
-
- /**
- * Gets the debug text
- * @returns string text for debug
- */
- function getDebugMSG() {
- return $this->debugMSG;
- }
- /**
- * PRIVATE method: sets the text of the last error
- * @returns void
- */
- function _setDebugMSG($method,$msg) {
- $debugARRAY=array();
- $debugARRAY['class']=get_class($this);
- $debugARRAY['method']=$method;
- $debugARRAY['msg']=$msg;
- $this->debugMSG[]=$debugARRAY;
- return true;
- }
- /**
- * XML LOAD AND ACCESS METHOD:
- */
- /**
- * xml file for plugin
- *
- * @param string $actionType
- * @param string $action
- * @param int $pluginId
- * @return CBSimpleXMLElement
- */
- function & loadPluginXML( $actionType, $action, $pluginId = null ) {
- global $_CB_framework;
- static $cache = array();
- cbimport('cb.xml.simplexml');
- $row =& $this->getPluginObject( $pluginId );
- $xmlString = null;
- if ( $row ) {
- // security sanitization to disable use of `/`, `\` and `:` in $action variable
- $unsecureChars = array( '/', '\', ':', ';', '{', '}', '(', ')', """, "'", '.', ',', "