eZ Publish  [4.0]
ezextension.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZExtension class
00004 //
00005 // Created on: <16-Mar-2002 14:23:45 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezextension.php
00032 */
00033 
00034 /*!
00035   \class eZExtension ezextension.php
00036   \brief The class eZExtension does
00037 
00038 */
00039 
00040 //include_once( 'lib/ezutils/classes/ezini.php' );
00041 
00042 class eZExtension
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZExtension()
00048     {
00049     }
00050 
00051     /*!
00052      \static
00053      \return the base directory for extensions
00054     */
00055     static function baseDirectory()
00056     {
00057         $ini = eZINI::instance();
00058         $extensionDirectory = $ini->variable( 'ExtensionSettings', 'ExtensionDirectory' );
00059         return $extensionDirectory;
00060     }
00061 
00062     /*!
00063      \static
00064      \return an array with extensions that has been activated.
00065      \param $extensionType Decides which extension to include in the list, the follow values are possible.
00066             - \c false - Means add both default and access extensions
00067             - 'default' - Add only default extensions
00068             - 'access' - Add only access extensions
00069 
00070      Default extensions are those who are loaded before a siteaccess are determined while access extensions
00071      are loaded after siteaccess is set.
00072     */
00073     static function activeExtensions( $extensionType = false )
00074     {
00075         $ini = eZINI::instance();
00076         $activeExtensions = array();
00077         if ( !$extensionType or
00078              $extensionType == 'default' )
00079             $activeExtensions = array_merge( $activeExtensions,
00080                                              $ini->variable( 'ExtensionSettings', 'ActiveExtensions' ) );
00081         if ( !$extensionType or
00082              $extensionType == 'access' )
00083             $activeExtensions = array_merge( $activeExtensions,
00084                                              $ini->variable( 'ExtensionSettings', 'ActiveAccessExtensions' ) );
00085         $globalActiveExtensions =& $GLOBALS['eZActiveExtensions'];
00086         if ( isset( $globalActiveExtensions ) )
00087         {
00088             $activeExtensions = array_merge( $activeExtensions,
00089                                              $globalActiveExtensions );
00090         }
00091         $activeExtensions = array_unique( $activeExtensions );
00092         return $activeExtensions;
00093     }
00094 
00095     /*!
00096      \static
00097      Will make sure that all extensions that has settings directories
00098      are added to the eZINI override list.
00099     */
00100     static function activateExtensions( $extensionType = false )
00101     {
00102         $extensionDirectory = eZExtension::baseDirectory();
00103         $activeExtensions = eZExtension::activeExtensions( $extensionType );
00104         $hasExtensions = false;
00105         $ini = eZINI::instance();
00106         foreach ( $activeExtensions as $activeExtension )
00107         {
00108             if ( !file_exists( $extensionDirectory . '/' . $activeExtension ) )
00109             {
00110                 eZDebug::writeWarning( "Extension '$activeExtension' does not exist, looked for directory '" . $extensionDirectory . '/' . $activeExtension . "'" );
00111             }
00112             $extensionSettingsPath = $extensionDirectory . '/' . $activeExtension . '/settings';
00113             if ( file_exists( $extensionSettingsPath ) )
00114             {
00115                 $ini->prependOverrideDir( $extensionSettingsPath, true );
00116 
00117                 if ( isset( $GLOBALS['eZCurrentAccess'] ) )
00118                 {
00119                     eZExtension::prependSiteAccess( $activeExtension );
00120                 }
00121                 $hasExtensions = true;
00122             }
00123         }
00124         if ( $hasExtensions )
00125             $ini->loadCache();
00126     }
00127 
00128     /*!
00129      \static
00130 
00131      Prepend extension siteaccesses
00132 
00133      \param siteaccess name ( default false )
00134     */
00135     static function prependExtensionSiteAccesses( $accessName = false, $ini = false, $globalDir = true, $identifier = false, $order = true )
00136     {
00137         $extensionList = eZExtension::activeExtensions();
00138 
00139         if ( !$order )
00140         {
00141             $extensionList = array_reverse( $extensionList );
00142         }
00143 
00144         foreach( $extensionList as $extension )
00145         {
00146             eZExtension::prependSiteAccess( $extension, $accessName, $ini, $globalDir, $identifier );
00147         }
00148     }
00149 
00150     /*!
00151      \static
00152 
00153      Prepend siteaccess for specified extension.
00154 
00155      \param $extension name
00156     */
00157     static function prependSiteAccess( $extension, $accessName = false, $ini = false, $globalDir = true, $identifier = false )
00158     {
00159         if ( !$accessName )
00160         {
00161             $accessName = $GLOBALS['eZCurrentAccess']['name'];
00162         }
00163 
00164         $extensionSettingsPath = eZExtension::baseDirectory() . '/' . $extension;
00165 
00166         if ( file_exists ( $extensionSettingsPath . '/settings/siteaccess/' . $accessName ) )
00167         {
00168             if ( !$ini )
00169             {
00170                 $ini = eZINI::instance();
00171             }
00172             $ini->prependOverrideDir( $extensionSettingsPath . '/settings/siteaccess/' . $accessName, $globalDir );
00173         }
00174     }
00175 
00176     /*!
00177      \static
00178      Generates a list with expanded paths and returns it.
00179      The paths are expanded to where the extensions are placed.
00180      Optionally a subdirectory of the extension may be set using \a $subdirectory.
00181     */
00182     static function expandedPathList( $extensionList, $subdirectory = false )
00183     {
00184         $pathList = array();
00185         $extensionBase = eZExtension::baseDirectory();
00186         foreach ( $extensionList as $extensionName )
00187         {
00188             $path = $extensionBase . '/' . $extensionName;
00189             if ( $subdirectory )
00190                 $path .= '/' . $subdirectory;
00191             $pathList[] = $path;
00192         }
00193         return $pathList;
00194     }
00195 
00196     /*!
00197      \static
00198      This is help function for searching for extension code. It will read ini variables
00199      defined in \a $parameters, search trough the specified directories for specific files
00200      and set the result in \a $out.
00201 
00202      The \a $parameters parameter must contain the following entries.
00203      - ini-name - The name of the ini file which has the settings, must include the .ini suffix.
00204      - repository-group - The INI group which has the basic repository settings.
00205      - repository-variable - The INI variable which has the basic repository settings.
00206      - extension-group - The INI group which has the extension settings.
00207      - extension-variable - The INI variable which has the extension settings.
00208      - subdir - A subdir which will be appended to all repositories searched for, can be left out.
00209      - extension-subdir - A subdir which will be appended to all extension repositories searched for, can be left out.
00210      - suffix-name - A suffix which will be appended after the file searched for.
00211      - type-directory - Whether the type has a directory for it's file or not. Default is true.
00212      - type - The type to look for, it will try to find a file named repository/subdir/type/type-suffix or
00213               if type-directory is false repository/subdir/type-suffix.
00214               If type is not specified the type-group and typ-variable may be used for fetching the current type.
00215      - type-group - The INI group which has the type setting.
00216      - type-variable - The INI variable which has the type setting.
00217      - alias-group - The INI group which defines type aliases, see below.
00218      - alias-variable - The INI variable which defines type aliases.
00219 
00220      Type aliases allows overriding a specific type to use another type handler,
00221      this is useful when extensions want to take control of some specific types
00222      or you want multiple names (aliases) for one type.
00223 
00224      On success the \a $out parameter will contain:
00225      - type - The current type used.
00226      - original-type - The original type, if aliasing was used it may differ from type.
00227      - found-file-dir - The directory where the type was found.
00228      - found-file-path - The full path to the type.
00229      - found-file-name - The filename of the type.
00230 
00231      \return true if the extension type was found.
00232     */
00233     static function findExtensionType( $parameters, &$out )
00234     {
00235         $iniName = $parameters['ini-name'];
00236         $repositoryGroup = $parameters['repository-group'];
00237         $repositoryVariable = $parameters['repository-variable'];
00238         $extensionGroup = $parameters['extension-group'];
00239         $extensionVariable = $parameters['extension-variable'];
00240         $subdir = false;
00241         if ( isset( $parameters['subdir'] ) )
00242             $subdir = $parameters['subdir'];
00243         $extensionSubdir = false;
00244         if ( isset( $parameters['extension-subdir'] ) )
00245             $extensionSubdir = $parameters['extension-subdir'];
00246         $typeDirectory = true;
00247         if ( isset( $parameters['type-directory'] ) )
00248             $typeDirectory = $parameters['type-directory'];
00249         $suffixName = $parameters['suffix-name'];
00250         $ini = eZINI::instance( $iniName );
00251         if ( isset( $parameters['type'] ) )
00252             $originalType = $parameters['type'];
00253         else if ( isset( $parameters['type-group'] ) and
00254                   isset( $parameters['type-variable'] ) )
00255             $originalType = $ini->variable( $parameters['type-group'], $parameters['type-variable'] );
00256         else
00257             return false;
00258         $type = $originalType;
00259         if ( isset( $parameters['alias-group'] ) and
00260              isset( $parameters['alias-variable'] ) )
00261         {
00262             if ( $ini->hasVariable( $parameters['alias-group'], $parameters['alias-variable'] ) )
00263             {
00264                 $aliasMap = $ini->variable( $parameters['alias-group'], $parameters['alias-variable'] );
00265                 if ( isset( $aliasMap[$type] ) )
00266                     $type = $aliasMap[$type];
00267             }
00268         }
00269 
00270         $baseDirectory = eZExtension::baseDirectory();
00271         $repositoryDirectoryList = array();
00272         $repositoryList = $ini->variable( $repositoryGroup, $repositoryVariable );
00273         $extensionDirectories = $ini->variable( $extensionGroup, $extensionVariable );
00274         foreach ( $repositoryList as $repository )
00275         {
00276             $repositoryDirectory = $repository;
00277             if ( $subdir != '' )
00278                 $repositoryDirectory .= '/' . $subdir;
00279             $repositoryDirectoryList[] = $repositoryDirectory;
00280         }
00281         foreach ( $extensionDirectories as $extensionDirectory )
00282         {
00283             $extensionPath = $baseDirectory . '/' . $extensionDirectory;
00284             if ( $extensionSubdir != '' )
00285                 $extensionPath .= '/' . $extensionSubdir;
00286             if ( file_exists( $extensionPath ) )
00287             {
00288                 $repositoryDirectoryList[] = $extensionPath;
00289             }
00290             else if ( $extensionSubdir )
00291             {
00292                 eZDebug::writeWarning( "Extension '$extensionDirectory' does not have the subdirectory $extensionSubdir, looked for directory '" . $extensionPath . "'" );
00293             }
00294         }
00295         $foundType = false;
00296         foreach ( $repositoryDirectoryList as $repositoryDirectory )
00297         {
00298             $fileDir = $repositoryDirectory;
00299             if ( $typeDirectory )
00300                 $fileDir .= "/$type";
00301             $fileName = $type . $suffixName;
00302             $filePath = $fileDir . '/' . $fileName;
00303             if ( file_exists( $filePath ) )
00304             {
00305                 $foundType = true;
00306                 break;
00307             }
00308         }
00309         $out['repository-directory-list'] = $repositoryDirectoryList;
00310         if ( $foundType )
00311         {
00312             $out['type'] = $type;
00313             $out['original-type'] = $originalType;
00314             $out['found-file-dir'] = $fileDir;
00315             $out['found-file-path'] = $filePath;
00316             $out['found-file-name'] = $fileName;
00317         }
00318         $out['found-type'] = $foundType;
00319         return $foundType;
00320     }
00321 
00322     /*!
00323      \static
00324      Read extension information. Returns extension information array
00325      specified in feature request 9371. ( http://issues.ez.no/9371 )
00326 
00327      \param extension name
00328 
00329      \return Extension information array. null if extension is not found,
00330              or does not contain extension information.
00331     */
00332     static function extensionInfo( $extension )
00333     {
00334         //include_once( 'lib/ezfile/classes/ezdir.php' );
00335         $infoFileName = eZDir::path( array( eZExtension::baseDirectory(), $extension, 'ezinfo.php' ) );
00336         if ( file_exists( $infoFileName ) )
00337         {
00338             include_once( $infoFileName );
00339             $className = $extension . 'Info';
00340             if ( is_callable( array( $className, 'info' ) ) )
00341             {
00342                 $result = call_user_func_array( array( $className, 'info' ), array() );
00343                 if ( is_array( $result ) )
00344                 {
00345                     return $result;
00346                 }
00347             }
00348         }
00349 
00350         return null;
00351     }
00352 
00353     /*!
00354      \static
00355      eZExtension::nameFromPath( __FILE__ ) executed in any file of an extension
00356      can help you to find the path to additional resources
00357      \return Name of the extension a path belongs to.
00358      \param $path Path to check.
00359     */
00360     static function nameFromPath( $path )
00361     {
00362         //include_once( 'lib/ezfile/classes/ezdir.php' );
00363         $path = eZDir::cleanPath( $path );
00364         $base = eZExtension::baseDirectory() . '/';
00365         $base = preg_quote( $base, '/' );
00366         $pattern = '/'.$base.'([^\/]+)/';
00367         if ( preg_match( $pattern, $path, $matches ) )
00368             return $matches[1];
00369         else
00370             false;
00371     }
00372 
00373     /*!
00374      \static
00375      \return true if this path is related to some extension.
00376      \param $path Path to check.
00377      \note The root of an extension is considered to be in this path too.
00378     */
00379     static function isExtension( $path )
00380     {
00381         if ( eZExtension::nameFromPath( $path ) )
00382             return true;
00383         else
00384             return false;
00385     }
00386 
00387 }
00388 
00389 ?>