liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file LibertySystem.php

Documentation is available at LibertySystem.php

  1. <?php
  2. /**
  3. * System class for handling the liberty package
  4. *
  5. @package  liberty
  6. @version  $Header$
  7. @author   spider <spider@steelsun.com>
  8. */
  9.  
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004, bitweaver.org
  12. // +----------------------------------------------------------------------+
  13. // | All Rights Reserved. See below for details and a complete list of authors.
  14. // | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  15. // |
  16. // | For comments, please use phpdocu.sourceforge.net documentation standards!!!
  17. // | -> see http://phpdocu.sourceforge.net/
  18. // +----------------------------------------------------------------------+
  19. // | Authors: spider <spider@steelsun.com>
  20. // +----------------------------------------------------------------------+
  21.  
  22. /**
  23.  * Local base defines
  24.  */
  25. // Plugin Definitions
  26. define'STORAGE_PLUGIN''storage' );
  27. define'FORMAT_PLUGIN''format' );
  28. define'DATA_PLUGIN''data' );
  29. define'MIME_PLUGIN''mime' );
  30. define'FILTER_PLUGIN''filter' );
  31.  
  32. if!defined'LIBERTY_DEFAULT_MIME_HANDLER' )) {
  33.     define'LIBERTY_DEFAULT_MIME_HANDLER''mimedefault' );
  34. }
  35.  
  36. // Service Definitions
  37. define'LIBERTY_SERVICE_ACCESS_CONTROL''access_control' );
  38. define'LIBERTY_SERVICE_CATEGORIZATION''categorization' );
  39. define'LIBERTY_SERVICE_COMMERCE''commerce' );
  40. define'LIBERTY_SERVICE_CONTENT_TEMPLATES''content_templates' );
  41. define'LIBERTY_SERVICE_DOCUMENT_GENERATION''document_generation' );
  42. define'LIBERTY_SERVICE_FORUMS''forums' );
  43. define'LIBERTY_SERVICE_GROUP''groups' );
  44. define'LIBERTY_SERVICE_MAPS''map_display' );
  45. define'LIBERTY_SERVICE_METADATA''metadata' );
  46. define'LIBERTY_SERVICE_MENU''menu' );
  47. define'LIBERTY_SERVICE_RATING''rating' );
  48. define'LIBERTY_SERVICE_REBLOG''reblogging_rss_feeds' );
  49. define'LIBERTY_SERVICE_SEARCH''search' );
  50. define'LIBERTY_SERVICE_THEMES''themes' );
  51. define'LIBERTY_SERVICE_TOPICA''topica' );
  52. define'LIBERTY_SERVICE_TRANSLATION''translation' );
  53. define'LIBERTY_SERVICE_TRANSLITERATION''transliteration' );
  54. define'LIBERTY_SERVICE_LIBERTYSECURE''security' );
  55. define'LIBERTY_SERVICE_MODCOMMENTS''comment_moderation' );
  56. define'LIBERTY_SERVICE_UPLOAD''upload' );
  57.  
  58. define'LIBERTY_TEXT_AREA''editliberty' );
  59. define'LIBERTY_UPLOAD''upload' );
  60.  
  61. /**
  62.  * Link to base class
  63.  */
  64. require_onceLIBERTY_PKG_PATH.'LibertyBase.php' );
  65.  
  66. /**
  67.  * System class for handling the liberty package
  68.  *
  69.  * @package liberty
  70.  */
  71. class LibertySystem extends BitSingleton {
  72.  
  73.  
  74.     // Hash of plugin data
  75.     var $mPlugins = array();
  76.  
  77.     // Liberty data tags
  78.     var $mDataTags;
  79.  
  80.     // Content Status
  81.     var $mContentStatus;
  82.  
  83.     // Content types
  84.     var $mContentTypes;
  85.  
  86.     // File name of last plug that registered
  87.     var $mPluginFileName;
  88.  
  89.     // Packages using LibertySystem
  90.     // this makes it possible to extend LibertySystem by another package
  91.     var $mSystem = LIBERTY_PKG_NAME;
  92.     var $mPluginPath;
  93.  
  94.  
  95.     /**
  96.      * Initiate Class
  97.      **/
  98.  
  99.     function __construct$pExtras TRUE {
  100.         parent::__construct();
  101.  
  102.         // if mPluginPath hasn't been set, we set it for liberty plugins
  103.         ifempty$this->mPluginPath )) {
  104.             $this->mPluginPath = LIBERTY_PKG_PATH.'plugins/';
  105.         }
  106.  
  107.         // extras - only needed by liberty
  108.         if$pExtras {
  109.             $this->mDataTags = array();
  110.             $this->loadContentTypes();
  111.         }
  112.     }
  113.  
  114.     // ****************************** Plugin Functions
  115.     /**
  116.      * Load only active plugins from disk
  117.      *
  118.      * @return none 
  119.      * @access public
  120.      ***/
  121.     function loadActivePlugins({
  122.         global $gBitSystem;
  123.         // all active plugins
  124.         $configs array_keys$gBitSystem->getConfigMatch"/^{$this->mSystem}_plugin_status_/i", 'y' ));
  125.  
  126.         // first we include the default one - this allows other plugins to make use of default functions
  127.         if( $this->mSystem == LIBERTY_PKG_NAME {
  128.             if( $key = array_search( 'liberty_plugin_status_'.LIBERTY_DEFAULT_MIME_HANDLER , $configs )) {
  129.                 unset( $configs[$key] );
  130.             }
  131.             array_unshift( $configs, 'liberty_plugin_status_'.LIBERTY_DEFAULT_MIME_HANDLER );
  132.         }
  133.  
  134.         foreach( $configs as $config ) {
  135.             $pluginGuid = preg_replace( "/^{$this->mSystem}_plugin_status_/", '', $config, 1 );
  136.             if( $pluginFile = $gBitSystem->getConfig"{$this->mSystem}_plugin_path_$pluginGuid" ) ) {
  137.                 if( is_file( BIT_ROOT_PATH.$pluginFile )) {
  138.                     $this->mPluginFilePath BIT_ROOT_PATH.$pluginFile;
  139.                     include_onceBIT_ROOT_PATH.$pluginFile );
  140.                 }
  141.             } elseif( $pluginFile = $gBitSystem->getConfig"{$this->mSystem}_plugin_file_$pluginGuid" ) ) {
  142.                 // TODO: all this is deprecated and doesn't really rock bitweavers boat anymore - we use the _plugin_path_ setting now.
  143.                 // this code here is only relevant if a user has updated bitweaver and scanAllPlugins() hasn't been called yet.
  144.                 // scanAllPlugins() is called during the upgrade in the installer so we really are only keeping this here for CVS users
  145.                 // and people who use nexus since it makes use of this plugin system as well.
  146.                 // - xing - Saturday Jul 05, 2008   20:47:29 CEST
  147.                 // check for the plugin in the default location - in case bitweaver root path changed.
  148.                 if( file_exists( $pluginFile )) {
  149.                     $this->mPluginFilePath $pluginFile;
  150.                     include_once$pluginFile );
  151.                 } else {
  152.                     $defaultFile = $this->mPluginPath.basename$pluginFile );
  153.                     iffile_exists$defaultFile )) {
  154.                         $this->mPluginFilePath $defaultFile;
  155.                         include_once$defaultFile );
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Load all plugins found in specified directory
  164.      * Use loadActivePlugins to load only the active plugins
  165.      *
  166.      * @param string $pPluginsPath Set the path where to scan for plugins
  167.      * @param string $pPrefixPattern Perl regex for filenames can start with to prevent inclusion of unwanted filenames (e.g. (data\.|storage\.)). Final regex: /^{$pPrefixPattern}.*\.php$/
  168.      * @access public
  169.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  170.      */
  171.     function scanAllPlugins( $pPluginsPath = NULL, $pPrefixPattern = NULL ) {
  172.         global $gBitSystem;
  173.         if( empty( $pPluginsPath )) {
  174.             $pPluginsPath = $this->mPluginPath;
  175.         }
  176.  
  177.         // check for plugins in plugins/ dir
  178.         if( $pluginHandle = opendir( $pPluginsPath )) {
  179.             while( FALSE !== ( $plugin = readdir( $pluginHandle ) ) ) {
  180.                 $pattern = "/^{$pPrefixPattern}.*\.php$/";
  181.                 if( preg_match( $pattern, $plugin ) ) {
  182.                     $this->mPluginFilePath $pPluginsPath.$plugin;
  183.                     include_once$pPluginsPath.$plugin );
  184.                 }
  185.             }
  186.         }
  187.  
  188.         // check for liberty plugins in other packages as well
  189.         if( $this->mSystem == LIBERTY_PKG_NAME && $pkgHandle opendirBIT_ROOT_PATH )) {
  190.             while( FALSE !== ( $dirName = readdir( $pkgHandle ))) {
  191.                 if( preg_match( '/^\w/', $dirName )  && $dirName != 'CVS' && is_dir( $pluginDir = BIT_ROOT_PATH.$dirName.'/liberty_plugins/' ) && ( $pluginHandle = opendir( $pluginDir ))) {
  192.                     while( FALSE !== ( $plugin = readdir( $pluginHandle ))) {
  193.                         if( preg_match( "/^{$pPrefixPattern}.*\.php$/", $plugin )) {
  194.                             $this->mPluginFilePath $pluginDir.$plugin;
  195.                             include_once$pluginDir.$plugin );
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.  
  202.         // keep plugin list in sorted order
  203.         if( !empty( $this->mPlugins and is_array$this->mPlugins ) ) {
  204.             asort( $this->mPlugins );
  205.         }
  206.  
  207.         // only execute the following if this class hasn't been extended
  208.         if( $this->mSystem == LIBERTY_PKG_NAME {
  209.             // There must be at least one format plugin active and set as the default format
  210.             $format_plugin_count = $default_format_found = 0;
  211.             $current_default_format_guid = $gBitSystem->getConfig'default_format' );
  212.             foreach$this->mPlugins as $guid => $plugin {
  213.                 // load all the requirements that we can display them on the plugin page
  214.                 if( $requirement_func = $this->getPluginFunction$guid'requirement_function'FALSETRUE )) {
  215.                     $this->mPlugins[$guid]['requirements'$requirement_func();
  216.                 }
  217.  
  218.                 if( $this->isPluginActive$guid )) {
  219.                     if( $plugin['plugin_type'] == FORMAT_PLUGIN ) {
  220.                         $format_plugin_count++;
  221.                     }
  222.                     if( $current_default_format_guid == $guid ) {
  223.                         $default_format_found++;
  224.                     }
  225.                 }
  226.             }
  227.  
  228.             // if no current default format or no format plugins active
  229.             // activate format.tikiwiki and make it the default format plugin
  230.             // This happens during installation and therefore requires that we include the plugin file for the constant definitions
  231.             $plugin_file = $this->mPluginPath.'format.tikiwiki.php';
  232.             if$format_plugin_count == || $default_format_found == && is_file$plugin_file ) ) {
  233.                 require_once( $plugin_file );
  234.                 $this->setActivePluginPLUGIN_GUID_TIKIWIKI );
  235.                 $gBitSystem->storeConfig'default_format'PLUGIN_GUID_TIKIWIKI$this->mSystem );
  236.                 //make memory match db
  237.                 $this->loadActivePlugins();
  238.             }
  239.         }
  240.  
  241.         // remove any config settings for plugin files that have been removed
  242.         $plugins = $gBitSystem->getConfigMatch"/^{$this->mSystem}_plugin_path_/" );
  243.         foreach( $plugins as $config => $path ) {
  244.             if( !is_file( BIT_ROOT_PATH.$path )) {
  245.                 $guid = str_replace( "{$this->mSystem}_plugin_path_", '', $config );
  246.                 $gBitSystem->storeConfigMatch"/^{$this->mSystem}_plugin_\w+_$guid/i", NULL );
  247.             }
  248.         }
  249.  
  250.         // TODO: we can remove this at some point since it's not really important - it just clears out stuff from the database that we don't use anymore
  251.         $gBitSystem->storeConfigMatch"/^{$this->mSystem}_plugin_file_/", NULL );
  252.     }
  253.  
  254.     /**
  255.      * Check to see if a given plugin is activ or not
  256.      *
  257.      * @param $pPluginGuid Plugin GUID of the plugin you want to check
  258.      * @return TRUE if the plugin is active, FALSE if it's not
  259.      **/
  260.     function isPluginActive( $pPluginGuid ) {
  261.         return( !empty( $this->mPlugins[$pPluginGuid]['is_active'&& $this->mPlugins[$pPluginGuid]['is_active'== 'y' ));
  262.     }
  263.  
  264.     /**
  265.      * Allow data plugins to register their tag
  266.      *
  267.      * @param string $pTag Tag of plugin, e.g.: TOC
  268.      * @param string $pPluginGuid GUID of plugin, e.g.: PLUGIN_GUID_TOC
  269.      * @access public
  270.      * @return void
  271.      */
  272.     function registerDataTag( $pTag, $pPluginGuid ) {
  273.         $this->mDataTags[strtolower$pTag )$pPluginGuid;
  274.     }
  275.  
  276.     /**
  277.      * Allow plugins to register themselves using this function. Data is added directly to the list of existing plugins
  278.      *
  279.      * @param $pGuid GUID of plugin
  280.      * @param $pPluginParams Set of plugin parameters (see treasury/plugins/mime.*.php for example)
  281.      * @return none
  282.      * @access public
  283.      **/
  284.     function registerPlugin( $pGuid, $pPluginParams ) {
  285.         global $gBitSystem;
  286.         // plugins can set their own file_name. this is not mandatory but makes sure we store the path to the correct file
  287.         // this is useful for files that are included by other plugins
  288.         if( !empty( $pPluginParams['file_name'] )) {
  289.             $pluginPath = dirname( $this->mPluginFilePath )."/".$pPluginParams['file_name'];
  290.         } else {
  291.             $pluginPath = $this->mPluginFilePath;
  292.         }
  293.  
  294.         if( !empty( $pGuid ) && !empty( $pluginPath ) && is_file( $pluginPath ) ) {
  295.             // store the relative path - we need to store the path to all plugins and not just active ones since we don't have access to this information when we use setActivePlugins()
  296.             $gBitSystem->storeConfig"{$this->mSystem}_plugin_path_".$pGuid, str_replace( BIT_ROOT_PATH, "", $pluginPath ), LIBERTY_PKG_NAME );
  297.             $settings['is_active'] = $gBitSystem->getConfig"{$this->mSystem}_plugin_status_".$pGuid );
  298.             if( empty( $settings['is_active'] ) && !empty( $pPluginParams['auto_activate'] )) {
  299.                 $this->setActivePlugin$pGuid );
  300.             }
  301.             $settings['plugin_guid'] = $pGuid;
  302.             $this->mPlugins[$pGuid]  array_merge$settings$pPluginParams );
  303.         }
  304.  
  305.     }
  306.  
  307.     /**
  308.      * setActivePlugins
  309.      *
  310.      * @param array $pPluginGuids an array of all the plugin guids that are active. Any left out are *inactive*!
  311.      * @access public
  312.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  313.      */
  314.     function setActivePlugins( $pPluginGuids ) {
  315.         global $gBitSystem;
  316.  
  317.         if( is_array( $pPluginGuids ) ) {
  318.             // zap list of plugins from DB
  319.             $gBitSystem->storeConfigMatch"/^{$this->mSystem}_plugin_status/i", NULL, 'n', LIBERTY_PKG_NAME );
  320.             foreach( array_keys( $this->mPlugins as $guid {
  321.                 $this->mPlugins[$guid]['is_active''n';
  322.             }
  323.  
  324.             // set active those specified
  325.             foreach( array_keys( $pPluginGuids ) as $guid ) {
  326.                 if( $pPluginGuids[$guid][0] == 'y' ) {
  327.                     $this->setActivePlugin$guid );
  328.                 }
  329.             }
  330.             // load any plugins made active, but not already loaded
  331.             $this->loadActivePlugins();
  332.  
  333.             // finally we need to remove all cache files since the content has been changed
  334.             LibertyContent::expungeCache();
  335.         }
  336.     }
  337.  
  338.     /**
  339.      * set a single plugin as active and store the appropriate information in the database
  340.      *
  341.      * @param array $pPluginGuid the plugin guid we want to set active
  342.      * @access public
  343.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  344.      */
  345.     function setActivePlugin( $pPluginGuid ) {
  346.         global $gBitSystem;
  347.         $gBitSystem->storeConfig"{$this->mSystem}_plugin_status_".$pPluginGuid, 'y', LIBERTY_PKG_NAME );
  348.         if( isset( $this->mPlugins[$pPluginGuid)) {
  349.             $this->mPlugins[$pPluginGuid]['is_active''y';
  350.         }
  351.  
  352.         // the requirement function can return a set of tables, indexes and sequences that need to be created for the plugin to work.
  353.         if( $requirement_func = $this->getPluginFunction$pPluginGuid'requirement_function' )) {
  354.             $reqs = $requirement_func( TRUE );
  355.             if( !empty( $reqs['schema']['tables'] )) {
  356.                 // fetch a list of tables in the database that we know if we need to insert any plugin ones
  357.                 if( strlen( BIT_DB_PREFIX ) > 0 ) {
  358.                     $lastQuote = strrpos( BIT_DB_PREFIX, '`' );
  359.                     if( $lastQuote != FALSE ) {
  360.                         $lastQuote++;
  361.                     }
  362.                     $prefix = substr( BIT_DB_PREFIX, $lastQuote );
  363.                 } else {
  364.                     $prefix = '';
  365.                 }
  366.  
  367.                 global $gBitDbType, $gBitDbHost, $gBitDbUser, $gBitDbPassword, $gBitDbName;
  368.                 $db = &ADONewConnection( $gBitDbType );
  369.                 if( $db->Connect$gBitDbHost$gBitDbUser$gBitDbPassword$gBitDbName )) {
  370.                     $dict = NewDataDictionary( $db );
  371.  
  372.                     if( !$gBitSystem->mDb->getCaseSensitivity() ) {
  373.                         $dict->connection->nameQuote '';
  374.                     }
  375.  
  376.                     if( $dbTables = $gBitSystem->mDb->MetaTables'TABLES'FALSE$prefix $prefix.'%' NULL ))) {
  377.                         // If we use MySql check which storage engine to use
  378.                         if( isset( $_SESSION['use_innodb'] )) {
  379.                             if( $_SESSION['use_innodb'] == TRUE ) {
  380.                                 $build = array( 'NEW', 'MYSQL' => 'ENGINE=INNODB' );
  381.                             } else {
  382.                                 $build = array( 'NEW', 'MYSQL' => 'ENGINE=MYISAM' );
  383.                             }
  384.                         } else {
  385.                             $build = 'NEW';
  386.                         }
  387.  
  388.                         // create tables
  389.                         foreach( $reqs['schema']['tables'] as $table => $tableDict ) {
  390.                             $fullTable = $prefix.$table;
  391.                             if( !in_array( $fullTable, $dbTables )) {
  392.                                 if( $sql = $dict->CreateTableSQL$fullTable$tableDict$build )) {
  393.                                     $ret = $dict->ExecuteSQLArray$sql );
  394.                                     if$ret === FALSE {
  395.                                         $errors[] = 'Failed to create table '.$completeTableName;
  396.                                         $tablesInstalled = TRUE;
  397.                                     }
  398.                                 }
  399.                             }
  400.                         }
  401.  
  402.                         // only continue if we installed at least one table
  403.                         if( !empty( $tablesInstalled )) {
  404.                             $schemaQuote = strrpos( BIT_DB_PREFIX, '`' );
  405.                             $sequencePrefix = ( $schemaQuote ? substr( BIT_DB_PREFIX,  $schemaQuote + 1 ) : BIT_DB_PREFIX );
  406.  
  407.                             // create indexes
  408.                             if( !empty( $reqs['schema']['indexes'] )) {
  409.                                 foreach( $reqs['schema']['indexes'] as $idx => $idxDict ) {
  410.                                     $completeTableName = $sequencePrefix.$reqs['schema']['indexes'][$idx]['table'];
  411.                                     if( $sql = $dict->CreateIndexSQL$idx$completeTableName$reqs['schema']['indexes'][$idx]['cols']$reqs['schema']['indexes'][$idx]['opts')) {
  412.                                         $ret = $dict->ExecuteSQLArray$sql );
  413.                                         if$ret === FALSE {
  414.                                             $errors[] = 'Failed to create index '.$completeTableName;
  415.                                         }
  416.                                     }
  417.                                 }
  418.                             }
  419.  
  420.                             // create sequences
  421.                             if( !empty( $reqs['schema']['sequences'] )) {
  422.                                 // If we use InnoDB for MySql we need this to get sequence tables created correctly.
  423.                                 if( isset( $_SESSION['use_innodb'] ) ) {
  424.                                     if( $_SESSION['use_innodb'] == TRUE ) {
  425.                                         $gBitInstallDb->_genSeqSQL "create table %s (id int not null) ENGINE=INNODB";
  426.                                     } else {
  427.                                         $gBitInstallDb->_genSeqSQL "create table %s (id int not null) ENGINE=MYISAM";
  428.                                     }
  429.                                 }
  430.  
  431.                                 foreach( array_keys( $reqs['schema']['sequences'] ) as $sequenceIdx ) {
  432.                                     if( !$gBitInstallDb->CreateSequence$sequencePrefix.$sequenceIdx$reqs['schema']['sequences'][$sequenceIdx]['start')) {
  433.                                         $errors[] = 'Failed to create sequence '.$sequencePrefix.$sequenceIdx;
  434.                                     }
  435.                                 }
  436.                             }
  437.                         }
  438.                     }
  439.                 }
  440.             }
  441.         }
  442.  
  443.         return( !empty( $errors ) ? $errors : NULL );
  444.     }
  445.  
  446.     /**
  447.      * getPluginInfo
  448.      *
  449.      * @param array $pGuid
  450.      * @access public
  451.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  452.      */
  453.     function getPluginInfo( $pGuid ) {
  454.         $ret = NULL;
  455.         if( !empty( $pGuid ) && !empty( $this->mPlugins[$pGuid)) {
  456.             $ret = $this->mPlugins[$pGuid];
  457.         }
  458.         return $ret;
  459.     }
  460.  
  461.     /**
  462.      * getPluginFunction
  463.      *
  464.      * @param string $pGuid GUID of plugin used - if empty, we get all available functions of that type in all active plugins
  465.      * @param string $pFunctionName Function type we want to use
  466.      * @param string $pGetDefault Get default function for a given plugin type such as 'mime'
  467.      * @param string $pGetInactive don't worry if plugin is active or not
  468.      * @access public
  469.      * @return function name on success, NULL on failure
  470.      */
  471.     function getPluginFunction( $pGuid, $pFunctionName, $pGetDefault = FALSE, $pGetInactive = FALSE ) {
  472.         if(( $this->isPluginActive$pGuid || $pGetInactive && !empty$this->mPlugins[$pGuid][$pFunctionName&& function_exists$this->mPlugins[$pGuid][$pFunctionName)) {
  473.             $ret = $this->mPlugins[$pGuid][$pFunctionName];
  474.         }
  475.  
  476.         // if we can't get a function on the first round, we fetch the default
  477.         if( empty( $ret ) && $pGetDefault == 'mime' && $pGuid != LIBERTY_DEFAULT_MIME_HANDLER ) {
  478.             $ret = $this->getPluginFunctionLIBERTY_DEFAULT_MIME_HANDLER$pFunctionName );
  479.         }
  480.  
  481.         return( !empty( $ret ) ? $ret : NULL );
  482.     }
  483.  
  484.     /**
  485.      * getPluginFunctions Get a list of functions of a given type
  486.      *
  487.      * @param string $pFunctionName Function type we want to get
  488.      * @access public
  489.      * @return array of functions with the GUID as key
  490.      */
  491.     function getPluginFunctions( $pFunctionName ) {
  492.         foreach( $this->mPlugins as $guid => $plugin {
  493.             if( $this->isPluginActive$guid && !empty$plugin[$pFunctionName&& function_exists$plugin[$pFunctionName)) {
  494.                 $ret[$guid] = $plugin[$pFunctionName];
  495.             }
  496.         }
  497.  
  498.         return( !empty( $ret ) ? $ret : array() );
  499.     }
  500.  
  501.     /**
  502.      * getMimeTemplate will fetch an appropriate template to display a given filetype
  503.      *
  504.      * @param string $pTemplate Basename of the template
  505.      * @param string $pGuid GUID of plugin
  506.      * @access public
  507.      * @return resource path to template
  508.      */
  509.     function getMimeTemplate( $pTemplate, $pGuid = LIBERTY_DEFAULT_MIME_HANDLER ) {
  510.         $ret = NULL;
  511.         if( $this->isPluginActive$pGuid && $plugin $this->getPluginInfo$pGuid )) && !empty$plugin[$pTemplate.'_tpl')) {
  512.             $ret = $plugin[$pTemplate.'_tpl'];
  513.         } elseif( $pGuid != LIBERTY_DEFAULT_MIME_HANDLER ) {
  514.             $ret = $this->getMimeTemplate$pTemplate );
  515.         }
  516.         return $ret;
  517.     }
  518.  
  519.     /**
  520.      * getAllMimeTemplates will fetch templates of a given type from all active plugins
  521.      *
  522.      * @param array $pTemplate Name of the template
  523.      * @access public
  524.      * @return array of resource paths to templates
  525.      */
  526.     function getAllMimeTemplates( $pTemplate ) {
  527.         $ret = array();
  528.         foreach( $this->getPluginsOfTypeMIME_PLUGIN as $guid => $plugin {
  529.             if( $this->isPluginActive$guid && !empty$plugin[$pTemplate.'_tpl')) {
  530.                 $ret[] = $plugin[$pTemplate.'_tpl'];
  531.             }
  532.         }
  533.         return $ret;
  534.     }
  535.  
  536.     /**
  537.      * getPluginsOfType will fetch all plugins of a given type
  538.      *
  539.      * @param string $pPluginType
  540.      * @access public
  541.      * @return an array of plugins of a given type
  542.      */
  543.     function getPluginsOfType( $pPluginType ) {
  544.         $ret = array();
  545.         if( !empty( $pPluginType )) {
  546.             foreach( $this->mPlugins as $guid => $plugin {
  547.                 if( !empty( $plugin['plugin_type'] ) && $plugin['plugin_type'] == $pPluginType ) {
  548.                     $ret[$guid] = $plugin;
  549.                 }
  550.             }
  551.         }
  552.         return $ret;
  553.     }
  554.  
  555.     /**
  556.      * This function will purge all plugin settings set in kernel_config. useful when the path to plugins changes
  557.      * or plugins don't seem to be working
  558.      *
  559.      * @access public
  560.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  561.      */
  562.     function resetAllPluginSettings() {
  563.         global $gBitSystem;
  564.         $gBitSystem->storeConfigMatch"/^{$this->mSystem}_plugin_/", NULL );
  565.         if( $this->mSystem == LIBERTY_PKG_NAME {
  566.             // also remove the default format
  567.             $gBitSystem->storeConfig'default_format'NULL$this->mSystem );
  568.         }
  569.         $this->scanAllPlugins();
  570.     }
  571.  
  572.     /**
  573.      * Load all available content types into $this->mContentTypes
  574.      *
  575.      * @return none
  576.      **/
  577.     function loadContentTypes( $pCacheTime=BIT_QUERY_CACHE_TIME ) {
  578.         if( $rs = $this->mDb->query"SELECT * FROM `".BIT_DB_PREFIX."liberty_content_types`"NULLBIT_QUERY_DEFAULTBIT_QUERY_DEFAULT ) ) {
  579.             while( $row = $rs->fetchRow() ) {
  580.                 // translate name
  581.                 // content_description backward compatibility for now
  582.                 $row['content_description'] = $row['content_name'] = tra( $row['content_name'] );
  583.                 if( !empty( $row['content_name_plural'] ) ){
  584.                     $row['content_name_plural'] = tra( $row['content_name_plural'] );
  585.                 }
  586.                 $this->mContentTypes[$row['content_type_guid']] $row;
  587.             }
  588.         }
  589.     }
  590.  
  591.     /**
  592.      * Register new content type
  593.      *
  594.      * @return none
  595.      * @access public
  596.      **/
  597.     function registerContentType( $pGuid, $pTypeParams ) {
  598.         global $gBitSystem;
  599.         if ( !$this->mDb->isValid() ) return;
  600.         if!isset$this->mContentTypes ) ) {
  601.             $this->loadContentTypes();
  602.         }
  603.         $pTypeParams['content_type_guid'] = $pGuid;
  604.         // automagically populate plural name value if none is set using most comment english of appending 's'
  605.         if( empty( $pTypeParams['content_name_plural'] ) ){
  606.             $pTypeParams['content_name_plural'] = $pTypeParams['content_name'].'s';
  607.         }
  608.         $this->mDb->StartTrans();
  609.         ifempty$this->mContentTypes[$pGuid&& !empty$pTypeParams ) ) {
  610.             $result = $this->mDb->associateInsertBIT_DB_PREFIX."liberty_content_types"$pTypeParams );
  611.             // we just ran some SQL - let's flush the loadContentTypes query cache
  612.             $this->loadContentTypes);
  613.         } else {
  614.             if( $pTypeParams['handler_package'] != $this->mContentTypes[$pGuid]['handler_package'||
  615.                 $pTypeParams['handler_file'!= $this->mContentTypes[$pGuid]['handler_file'||
  616.                 $pTypeParams['handler_class'!= $this->mContentTypes[$pGuid]['handler_class'||
  617.                 empty$this->mContentTypes[$pGuid]['content_name_plural'&& version_compare$gBitSystem->getVersionLIBERTY_PKG_NAME )'2.1.4''>=' ) ) // temporary update condition during migration of content_description to content_name remove after april 20 2011
  618.                 {
  619.                 $result = $this->mDb->associateUpdateBIT_DB_PREFIX."liberty_content_types"$pTypeParamsarray'content_type_guid'=>$pGuid ) );
  620.                 // we just ran some SQL - let's flush the loadContentTypes query cache
  621.                 $this->loadContentTypes);
  622.             }
  623.         }
  624.         $this->mDb->CompleteTrans();
  625.     }
  626.  
  627.     /**
  628.      * requireHandlerFile will require_once() the handler file if given the hash found in $gLibertySystem->mContentTypes[content_type_guid]
  629.      *
  630.      * @param array $pContentTypeHash the hash found in $gLibertySystem->mContentTypes[content_type_guid]
  631.      * @access public
  632.      * @return TRUE on success, FALSE on failure
  633.      */
  634.     function requireHandlerFile( $pContentTypeHash ) {
  635.         $ret = FALSE;
  636.         if( defined( strtoupper( $pContentTypeHash['handler_package'] ).'_PKG_PATH' )) {
  637.             require_once( constant( strtoupper( $pContentTypeHash['handler_package'] ).'_PKG_PATH' ).$pContentTypeHash['handler_file'] );
  638.             $ret = TRUE;
  639.         }
  640.         return $ret;
  641.     }
  642.  
  643.     /**
  644.      * Get the display name of the content type
  645.      * @param boolean $pPlural true will return the plural form of the content type display name
  646.      * @return string the display name of the content type
  647.       */
  648.     function getContentType( $pContentTypeGuid ){
  649.         $ret = NULL;
  650.         if( !isset( $this->mContentTypes ) ) {
  651.             $this->loadContentTypes();
  652.         }
  653.         if( !empty( $this->mContentTypes[$pContentTypeGuid) ) {
  654.              $ret = $this->mContentTypes[$pContentTypeGuid];
  655.         }
  656.         return $ret;
  657.     }
  658.  
  659.     /**
  660.      * Get the display name of the content type
  661.      * @param boolean $pPlural true will return the plural form of the content type display name
  662.      * @return string the display name of the content type
  663.       */
  664.     function getContentClassName( $pContentTypeGuid ) {
  665.         $ret = NULL;
  666.         if( !isset( $this->mContentTypes ) ) {
  667.             $this->loadContentTypes();
  668.         }
  669.         if( !empty( $this->mContentTypes[$pContentTypeGuid&& $this->requireHandlerFile$this->mContentTypes[$pContentTypeGuid) ) {
  670.              $ret = $this->mContentTypes[$pContentTypeGuid]['handler_class'];
  671.         }
  672.         return $ret;
  673.     }
  674.  
  675.     /**
  676.      * Get the display name of the content type
  677.      * @param boolean $pPlural true will return the plural form of the content type display name
  678.      * @return string the display name of the content type
  679.       */
  680.     function getContentTypeName( $pContentTypeGuid, $pPlural=FALSE ){
  681.         $ret = NULL;
  682.         if( $pPlural && isset( $this->mContentTypes[$pContentTypeGuid]['content_name_plural') ) {
  683.             $ret = tra( $this->mContentTypes[$pContentTypeGuid]['content_name_plural');
  684.         } elseif( !empty( $this->mContentTypes[$pContentTypeGuid]['content_name') ) {
  685.              $ret = tra( $this->mContentTypes[$pContentTypeGuid]['content_name');
  686.         }
  687.         return $ret;
  688.     }
  689.  
  690.     /**
  691.      * Get the description of a given content type
  692.      *
  693.      * @param $pContentType Content type GUID you want the description for
  694.      * @return Content type description
  695.      * @access public
  696.      **/
  697.     function getContentTypeDescription( $pContentType ) {
  698.         deprecated( 'You are calling the deprecated method getContentTypeDescription, use getContentTypeName( $pPlural )' );
  699.         return $this->getContentTypeName$pContentType );
  700.     }
  701.  
  702.  
  703.  
  704.  
  705.     // ****************************** Service Functions
  706.     /**
  707.      * Get the service details of a given package
  708.      *
  709.      * @param $pPackageName Package name of you want the service details for
  710.      * @return Service details if the package has them - FALSE if the package is not a service
  711.      * @access public
  712.      **/
  713.     function getService( $pPackageName ) {
  714.         global $gBitSystem;
  715.         return( !empty( $gBitSystem->mPackages[$pPackageName]['service'$gBitSystem->mPackages[$pPackageName]['service'NULL );
  716.     }
  717.  
  718.     /**
  719.      * Register package as service - hash added to $this->mServices
  720.      *
  721.      * $pServiceHash Service hash details. see existing service hashes found in <package>/bit_setup_inc.php for examples and details
  722.      * @return none
  723.      * @access public
  724.      **/
  725.     function registerService( $pServiceName, $pPackageName, $pServiceHash, $pOptions = array()  ) {
  726.         $this->mServices[$pServiceNamearray(
  727.                                                    'package' => $pPackageName,
  728.                                                    'services'    => $pServiceHash,
  729.                                                 'description' => !empty$pOptions['description'$pOptions['description'NULL,
  730.                                                 'required' => !empty$pOptions['required'$pOptions['required'FALSE,
  731.                                              );
  732.     }
  733.  
  734.     /**
  735.      * Check to see if a package has any service capabilities
  736.      *
  737.      * @return TRUE on success, FALSE on failure
  738.      * @access public
  739.      **/
  740.     function hasService( $pServiceName ) {
  741.         return( !empty( $this->mServices[$pServiceName) );
  742.     }
  743.  
  744.     /**
  745.      * Get contents of a given service value
  746.      *
  747.      * @param $pServiceValue Service value you want to work to get
  748.      * @return Value of a given service value
  749.      * @access private
  750.      **/
  751.     function getServiceValues( $pServiceValue ) {
  752.         global $gBitSystem;
  753.         $ret = NULL;
  754.         if( !empty( $this->mServices ) ) {
  755.             foreach( array_keys( $this->mServices as $service {
  756.                 if( $this->hasService$service ) ) {
  757.                     // DEPRECATED - this is mostly circular logic - getting the package name from itself to look itself up
  758.                     // Service names are key values - regardless of package
  759.                     // Accessing services directly by name infact allows multiple packages to provide the same kind of service
  760.                     /*
  761.                     if( !($package = $gBitSystem->getConfig( 'liberty_service_'.$service )) ) {
  762.                         $package = key( $this->mServices[$service] );
  763.                     }
  764.                     if( !empty( $this->mServices[$service][$package][$pServiceValue] ) ) {
  765.                         $ret[$service] = $this->mServices[$service][$package][$pServiceValue];
  766.                     }
  767.                     */
  768.                     if( !empty( $this->mServices[$service]['services'][$pServiceValue) ) {
  769.                         $ret[$service] = $this->mServices[$service]['services'][$pServiceValue];
  770.                     }
  771.                 }
  772.             }
  773.         }
  774.         return $ret;
  775.     }
  776.  
  777.  
  778.  
  779.  
  780.     // ****************************** Miscellaneous Functions
  781.     /**
  782.      * Get the URL to the icon for the mime type passed in. This should probably check for files of multiple image types instead of just jpg
  783.      *
  784.      * @param string $pMimeType Mime type of the file
  785.      * @param string $pExt Extension of the file - used to get backup mime icon
  786.      * @access public
  787.      * @return Full image HTML tag to mime icon
  788.      */
  789.     public static function getMimeThumbnailURL($pMimeType, $pExt=NULL) {
  790.         $ret = NULL;
  791.         $parts = explode( '/',$pMimeType );
  792.         if( count( $parts ) > 1 ) {
  793.             global $gBitSmarty;
  794.             $gBitSmarty->loadPlugin'smarty_function_biticon' );
  795.  
  796.             $ext strtolower$parts[1);
  797.             $biticon array(
  798.                 'ipackage' => 'liberty',
  799.                 'ipath' => 'mime/',
  800.                 'iname' => $ext,
  801.                 'iexplain' => $ext,
  802.                 'url' => 'only',
  803.             );
  804.  
  805.             if!$ret smarty_function_biticon$biticon,$gBitSmarty ) ) {
  806.                 $biticon['iname'] = strtolower( $pExt );
  807.                 if( !$ret = smarty_function_biticon( $biticon,$gBitSmarty ) ) {
  808.                     $biticon['iname'] = 'generic';
  809.                     $ret = smarty_function_biticon( $biticon,$gBitSmarty );
  810.                 }
  811.             }
  812.         }
  813.         return $ret;
  814.     }
  815.  
  816.     /**
  817.      * Will return the plugin that is responsible for the given mime type
  818.      *
  819.      * @param string $pFileHash['mimetype'] (required if no tmp_name) Mime type of file that needs to be dealt with
  820.      * @param string $pFileHash['tmp_name'] (required if no mimetype) Full path to file that needs to be dealt with
  821.      * @access public
  822.      * @return handler plugin guid
  823.      * TODO: Currently this will return the first found handler - might want to have a sort order?
  824.      **/
  825.     function lookupMimeHandler( &$pFileHash ) {
  826.         global $gBitSystem;
  827.  
  828.         if( empty( $this->mPlugins )) {
  829.             $this->scanAllPluginsNULL"mime\." );
  830.         }
  831.  
  832.         // we will do our best to work out what this file is.
  833.         // both these methods use a different method for fetching the filetype
  834.         // this can be particularly important when fetching the mime-type of video files.
  835.         // ! Windows looses the file extension when creating the tmp file
  836.         // need a better way of handling this
  837.         if( !is_windows() ) {
  838.             $pFileHash['type'] = $gBitSystem->verifyMimeType$pFileHash['tmp_name');
  839.         }
  840.  
  841.         if( $pFileHash['type'] == 'application/binary' || $pFileHash['type'] == 'application/octet-stream' || $pFileHash['type'] == 'application/octetstream' ) {
  842.             $pFileHash['type'] = $gBitSystem->lookupMimeType$pFileHash['name');
  843.         }
  844.  
  845.         foreach( $this->getPluginsOfTypeMIME_PLUGIN as $handler => $plugin {
  846.             if( $this->isPluginActive$handler && !empty$plugin['mimetypes'&& is_array$plugin['mimetypes')) {
  847.                 foreach( $plugin['mimetypes'] as $pattern ) {
  848.                     if( preg_match( $pattern, $pFileHash['type'] )) {
  849.                         return $handler;
  850.                     }
  851.                 }
  852.             }
  853.         }
  854.  
  855.         return LIBERTY_DEFAULT_MIME_HANDLER;
  856.     }
  857. }

Documentation generated on Wed, 29 Jul 2015 13:56:59 +0000 by phpDocumentor 1.5.0-lsces