eZ Publish  [4.0]
ezfilepackagehandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZFilePackageHandler class
00004 //
00005 // Created on: <23-Jul-2003 16:11:42 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 ezfilepackagehandler.php
00032 */
00033 
00034 /*!
00035   \class eZFilePackageHandler ezfilepackagehandler.php
00036   \brief Handles content classes in the package system
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/ezpackagehandler.php' );
00041 
00042 class eZFilePackageHandler extends eZPackageHandler
00043 {
00044     /*!
00045      Constructor
00046     */
00047     function eZFilePackageHandler()
00048     {
00049         $this->eZPackageHandler( 'ezfile' );
00050     }
00051 
00052     function install( $package, $installType, $parameters,
00053                       $name, $os, $filename, $subdirectory,
00054                       $content, &$installParameters,
00055                       &$installData )
00056     {
00057         $collectionName = $parameters['collection'];
00058         $installVariables = array();
00059         if ( isset( $installParameters['variables'] ) )
00060             $installVariables = $installParameters['variables'];
00061         $iniFileVariables = false;
00062         if ( isset( $installParameters['ini'] ) )
00063             $iniFileVariables = $installParameters['ini'];
00064         $fileList = $package->fileList( $collectionName );
00065         if ( $fileList )
00066         {
00067             foreach ( $fileList as $fileItem )
00068             {
00069                 $newFilePath = false;
00070                 if ( $fileItem['type'] == 'thumbnail' )
00071                 {
00072                 }
00073                 else
00074                 {
00075                     $filePath = $package->fileItemPath( $fileItem, $collectionName );
00076                     if ( is_dir( $filePath ) )
00077                     {
00078                         $newFilePath = $package->fileStorePath( $fileItem, $collectionName, $installParameters['path'], $installVariables );
00079                         eZDir::mkdir( $newFilePath, false, true );
00080                     }
00081                     else
00082                     {
00083                         $newFilePath = $package->fileStorePath( $fileItem, $collectionName, $installParameters['path'], $installVariables );
00084                         if ( preg_match( "#^(.+)/[^/]+$#", $newFilePath, $matches ) )
00085                         {
00086                             eZDir::mkdir( $matches[1], false, true );
00087                         }
00088                         eZFileHandler::copy( $filePath, $newFilePath );
00089                     }
00090                 }
00091                 if ( $fileItem['type'] == 'ini' and $iniFileVariables and $newFilePath )
00092                 {
00093                     $fileRole = $fileItem['role'];
00094                     $fileRoleValue = $fileItem['role-value'];
00095                     $fileVariableName = $fileItem['variable-name'];
00096                     $fileName = $fileItem['name'];
00097                     if ( $fileVariableName and
00098                          isset( $installParameters['variables'][$fileVariableName] ) )
00099                         $fileRoleValue = $installParameters['variables'][$fileVariableName];
00100                     if ( isset( $iniFileVariables[$fileRole][$fileRoleValue][$fileName] ) )
00101                     {
00102                         $variables = $iniFileVariables[$fileRole][$fileRoleValue][$fileName];
00103                         $ini =& eZINI::fetchFromFile( $newFilePath );
00104                         $ini->setVariables( $variables );
00105                         $ini->save( false, false, false, false, false );
00106                     }
00107                 }
00108             }
00109         }
00110         return true;
00111     }
00112 
00113     /*!
00114      \reimp
00115     */
00116     function add( $packageType, $package, $cli, $parameters )
00117     {
00118         $collections = array();
00119         foreach ( $parameters['file-list'] as $fileItem )
00120         {
00121             $package->appendFile( $fileItem['file'], $fileItem['type'], $fileItem['role'],
00122                                   $fileItem['design'], $fileItem['path'], $fileItem['collection'],
00123                                   null, null, true, null,
00124                                   $fileItem['file-type'], $fileItem['role-value'], $fileItem['variable-name'],
00125                                   $fileItem['package-path'] );
00126             if ( !in_array( $fileItem['collection'], $collections ) )
00127                 $collections[] = $fileItem['collection'];
00128             $addString = "Adding " . $cli->stylize( 'mark', $fileItem['type'] );
00129             if ( $fileItem['type'] != 'design' )
00130                 $addString .= " " . $cli->stylize( 'file', $fileItem['file'] );
00131             if ( $fileItem['type'] == 'design' )
00132                 $addString .= " " . $cli->stylize( 'dir', $fileItem['design'] );
00133             if ( ( $fileItem['type'] == 'design' or $fileItem['type'] == 'ini' ) and
00134                  $fileItem['role'] )
00135                 $addString .= " using role " . $cli->stylize( 'italic', $fileItem['role'] );
00136             if ( $fileItem['variable-name'] )
00137                 $addString .= " bound to variable " . $cli->stylize( 'variable', $fileItem['variable-name'] );
00138 //            . " (" . $fileItem['design'] . ", " . $fileItem['role'] . ")";
00139 //            if ( $fileItem['variable-name'] )
00140 //                $addString .= '[' . $fileItem['variable-name'] . ']';
00141             $cli->notice( $addString );
00142         }
00143         foreach ( $collections as $collection )
00144         {
00145             $installItems = $package->installItemsList( 'ezfile', false, $collection, true );
00146             if ( count( $installItems ) == 0 )
00147                 $package->appendInstall( 'ezfile', false, false, true,
00148                                          false, false,
00149                                          array( 'collection' => $collection ) );
00150             $dependencyItems = $package->dependencyItems( 'provides',
00151                                                           array( 'type'  => 'ezfile',
00152                                                                  'name'  => 'collection',
00153                                                                  'value' =>  $collection ) );
00154             if ( count( $dependencyItems ) == 0 )
00155                 $package->appendDependency( 'provides',
00156                                             array( 'type'  => 'ezfile',
00157                                                    'name'  => 'collection',
00158                                                    'value' => $collection ) );
00159             $installItems = $package->installItemsList( 'ezfile', false, $collection, false );
00160             if ( count( $installItems ) == 0 )
00161                 $package->appendInstall( 'ezfile', false, false, false,
00162                                          false, false,
00163                                          array( 'collection' => $collection ) );
00164         }
00165     }
00166 
00167     function handleAddParameters( $packageType, $package, $cli, $arguments )
00168     {
00169         return $this->handleParameters( $packageType, $package, $cli, 'add', $arguments );
00170     }
00171 
00172     function handleParameters( $packageType, $package, $cli, $type, $arguments )
00173     {
00174         $fileList = array();
00175         $currentType = 'file';
00176         $currentVariableName = false;
00177         $currentRole = false;
00178         $currentRoleValue = false;
00179         $currentDesign = false;
00180         $currentCollection = 'default';
00181         $packagePath = false;
00182         if ( $packageType == 'design' )
00183         {
00184             $currentType = 'design';
00185         }
00186         else if ( $packageType == 'dir' )
00187         {
00188             $currentType = 'dir';
00189         }
00190         else if ( $packageType == 'ini' )
00191         {
00192             $currentType = 'ini';
00193             $currentRole = 'standard';
00194         }
00195         else if ( $packageType == 'template' )
00196         {
00197             $currentType = 'design';
00198             $currentRole = 'template';
00199         }
00200         else if ( $packageType == 'thumbnail' )
00201         {
00202             $currentType = 'thumbnail';
00203             $currentRole = false;
00204         }
00205         for ( $i = 0; $i < count( $arguments ); ++$i )
00206         {
00207             $argument = $arguments[$i];
00208             if ( $argument[0] == '-' )
00209             {
00210                 if ( strlen( $argument ) > 1 and
00211                      $argument[1] == '-' )
00212                 {
00213                 }
00214                 else
00215                 {
00216                     $flag = substr( $argument, 1, 1 );
00217                     if ( $flag == 't' or
00218                          $flag == 'r' or
00219                          $flag == 'n' or
00220                          $flag == 'v' or
00221                          $flag == 'd' or
00222                          $flag == 'p' or
00223                          $flag == 'c' )
00224                     {
00225                         if ( strlen( $argument ) > 2 )
00226                         {
00227                             $data = substr( $argument, 2 );
00228                         }
00229                         else
00230                         {
00231                             $data = $arguments[$i+1];
00232                             ++$i;
00233                         }
00234                         if ( $flag == 't' )
00235                         {
00236                             if ( !in_array( $data, array( 'design', 'ini', 'file', 'thumbnail' ) ) )
00237                             {
00238                                 $cli->error( "Unknown file type $data, allowed values are design, ini, thumbnail and file" );
00239                                 return false;
00240                             }
00241                             $currentType = $data;
00242                             $currentRole = false;
00243                             $currentDesign = false;
00244                             if ( $currentType == 'design' )
00245                             {
00246                                 $currentRole = 'template';
00247                                 $currentDesign = 'standard';
00248                             }
00249                         }
00250                         else if ( $flag == 'r' )
00251                         {
00252                             if ( $currentType != 'design' and
00253                                  $currentType != 'ini' )
00254                             {
00255                                 $cli->error( "The current file type is not 'design' or 'ini' ($currentType), cannot set specific roles for files" );
00256                                 return false;
00257                             }
00258                             if ( !$this->roleExists( $currentType, $data ) )
00259                             {
00260                                 $cli->error( "Unknown file role $data for file type $currentType" );
00261                                 return false;
00262                             }
00263                             $currentRole = $data;
00264                         }
00265                         else if ( $flag == 'v' )
00266                         {
00267                             $currentRoleValue = $data;
00268                         }
00269                         else if ( $flag == 'n' )
00270                         {
00271                             $currentVariableName = $data;
00272                         }
00273                         else if ( $flag == 'p' )
00274                         {
00275                             $packagePath = $data;
00276                         }
00277                         else if ( $flag == 'd' )
00278                         {
00279                             if ( $currentType != 'design' )
00280                             {
00281                                 $cli->error( "The current file type is not 'design' ($currentType), cannot set specific designs for files" );
00282                                 return false;
00283                             }
00284                             if ( !$this->designExists( $data ) )
00285                             {
00286                                 $cli->error( "The design $data does not exist" );
00287                                 return false;
00288                             }
00289                             $currentDesign = $data;
00290                         }
00291                         else if ( $flag == 'c' )
00292                         {
00293                             $currentCollection = $data;
00294                         }
00295                     }
00296                 }
00297             }
00298             else
00299             {
00300                 $file = $argument;
00301                 $type = $currentType;
00302                 $role = $currentRole;
00303                 $roleValue = $currentRoleValue;
00304                 $design = $currentDesign;
00305                 $realFilePath = $this->fileExists( $file, $type, $role, $roleValue, $design,
00306                                                    $triedFiles );
00307                 if ( !$realFilePath )
00308                 {
00309                     $error = ( "File " . $cli->stylize( 'file', $file ) . " does not exist\n" .
00310                                "The following files were searched for:\n" );
00311                     $files = array();
00312                     foreach ( $triedFiles as $triedFile )
00313                     {
00314                         $files[] = $cli->stylize( 'file', $triedFile );
00315                     }
00316                     $cli->output( $error . implode( "\n", $files ) );
00317                     return false;
00318                 }
00319                 $fileFileType = false;
00320                 if ( is_dir( $realFilePath ) )
00321                     $fileFileType = 'dir';
00322                 if ( $currentType == 'ini' and
00323                      $fileFileType == 'dir' )
00324                 {
00325                     $iniFiles = eZDir::recursiveFind( $realFilePath, "" );
00326                     $fileFileType = 'file';
00327                     foreach ( $iniFiles as $iniFile )
00328                     {
00329                         $iniFile = $this->iniMatch( $iniFile, $role, $roleValue, $file, $triedFiles );
00330                         if ( !$iniFile )
00331                             continue;
00332                         $fileList[] = array( 'file' => $file,
00333                                              'package-path' => $packagePath,
00334                                              'type' => $type,
00335                                              'role' => $role,
00336                                              'role-value' => $roleValue,
00337                                              'variable-name' => $currentVariableName,
00338                                              'file-type' => $fileFileType,
00339                                              'design' => $design,
00340                                              'collection' => $currentCollection,
00341                                              'path' => $iniFile );
00342                     }
00343                 }
00344                 else
00345                 {
00346                     $fileList[] = array( 'file' => $file,
00347                                          'package-path' => $packagePath,
00348                                          'type' => $type,
00349                                          'role' => $role,
00350                                          'role-value' => $roleValue,
00351                                          'variable-name' => $currentVariableName,
00352                                          'file-type' => $fileFileType,
00353                                          'design' => $design,
00354                                          'collection' => $currentCollection,
00355                                          'path' => $realFilePath );
00356                 }
00357                 $realPath = false;
00358             }
00359         }
00360         if ( count( $fileList ) == 0 )
00361         {
00362             $cli->error( "No files were added" );
00363             return false;
00364         }
00365         return array( 'file-list' => $fileList );
00366     }
00367 
00368     function roleExists( $type, $role )
00369     {
00370         if ( $type == 'design' )
00371             return in_array( $role,
00372                              array( 'template', 'image', 'stylesheet', 'font' ) );
00373         if ( $type == 'ini' )
00374             return in_array( $role,
00375                              array( 'standard', 'siteaccess', 'override' ) );
00376         return false;
00377     }
00378 
00379     function designExists( $design )
00380     {
00381         return file_exists( 'design/' . $design );
00382     }
00383 
00384     function fileExists( &$file, &$type, &$role, &$roleValue, &$design,
00385                          &$triedFiles )
00386     {
00387         $triedFiles = array();
00388         switch ( $type )
00389         {
00390             case 'file':
00391             {
00392                 if ( file_exists( $file ) )
00393                     return $file;
00394                 $triedFiles[] = $file;
00395             } break;
00396             case 'dir':
00397             {
00398                 if ( file_exists( $file ) and is_dir( $file ) )
00399                     return $file;
00400                 $triedFiles[] = $file;
00401             } break;
00402             case 'ini':
00403             {
00404                 $filePath = $file;
00405                 if ( file_exists( $filePath ) )
00406                 {
00407                     $filePath = $this->iniMatch( $filePath, $role, $roleValue, $file, $triedFiles );
00408                     if ( $filePath )
00409                         return $filePath;
00410                 }
00411             } break;
00412             case 'thumbnail':
00413             {
00414                 if ( file_exists( $file ) )
00415                 {
00416                     $filePath = $file;
00417 //                     if ( preg_match( "#^(.+)/([^/]+)$#", $file, $matches ) )
00418 //                     {
00419 //                         $file = $matches[2];
00420 //                     }
00421                     if ( preg_match( "#^(.+)\.([^.]+)$#", $file, $matches ) )
00422                     {
00423                         $file = 'thumbnail.' . $matches[2];
00424                     }
00425                     return $filePath;
00426                 }
00427                 $triedFiles[] = $file;
00428             } break;
00429             case 'design':
00430             {
00431                 $roleFileName = false;
00432                 switch ( $role )
00433                 {
00434                     case 'template':
00435                     {
00436                         $roleFileName = 'templates';
00437                     } break;
00438                     case 'image':
00439                     {
00440                         $roleFileName = 'images';
00441                     } break;
00442                     case 'stylesheet':
00443                     {
00444                         $roleFileName = 'stylesheets';
00445                     } break;
00446                     case 'font':
00447                     {
00448                         $roleFileName = 'fonts';
00449                     } break;
00450                 }
00451                 $designDirectories = array( 'design' );
00452                 $extensionBaseDirectory = eZExtension::baseDirectory();
00453                 $ini = eZINI::instance( 'design.ini' );
00454                 $extensionDesigns = $ini->variable( 'ExtensionSettings', 'DesignExtensions' );
00455                 foreach ( $extensionDesigns as $extensionDesign )
00456                 {
00457                     $designDirectories[] = $extensionBaseDirectory . '/' . $extensionDesign . '/design';
00458                 }
00459                 if ( file_exists( $file ) )
00460                 {
00461                     $preg = '#^';
00462                     $i = 0;
00463                     foreach ( $designDirectories as $designDirectory )
00464                     {
00465                         if ( $i > 0 )
00466                             $preg .= '|';
00467                         $preg .= '(?:' . $designDirectory . ')';
00468                         ++$i;
00469                     }
00470                     $preg .= '/([^/]+)/(.+)$#';
00471                     $realFile = $file;
00472                     if ( preg_match( $preg, $file, $matches ) )
00473                     {
00474                         $design = $matches[1];
00475                         if ( preg_match( '#^(template(?:s)|image(?:s)|stylesheet(?:s)|font(?:s))/(.+)$#', $matches[2], $matches ) )
00476                         {
00477                             $role = $matches[1];
00478                             $file = $matches[2];
00479                         }
00480                         else
00481                         {
00482                             $file = $matches[2];
00483                             $role = false;
00484                         }
00485                     }
00486                     else
00487                     {
00488                         $type = 'file';
00489                         $role = false;
00490                         $design = false;
00491                     }
00492                     return $realFile;
00493                 }
00494                 $triedFiles[] = $file;
00495                 if ( !$design )
00496                 {
00497                     foreach ( $designDirectories as $designDirectory )
00498                     {
00499                         $filePath = $designDirectory . '/standard/' . $roleFileName . '/' . $file;
00500                         if ( file_exists( $filePath ) )
00501                         {
00502                             $design = 'standard';
00503                             return $filePath;
00504                         }
00505                         $triedFiles[] = $filePath;
00506                         if ( !file_exists( $designDirectory ) or
00507                              !is_dir( $designDirectory ) )
00508                             continue;
00509                         $dirHandler = @opendir( $designDirectory );
00510                         if ( !$dirHandler )
00511                             continue;
00512                         while ( ( $designSubDirectory = @readdir( $dirHandler ) ) !== false )
00513                         {
00514                             if ( $designSubDirectory == '.' or $designSubDirectory == '..' )
00515                                 continue;
00516                             $filePath = $designDirectory . '/' . $designSubDirectory . '/' . $roleFileName . '/' . $file;
00517                             if ( file_exists( $filePath ) )
00518                             {
00519                                 @closedir( $dirHandler );
00520                                 $design = $designSubDirectory;
00521                                 return $filePath;
00522                             }
00523                             $triedFiles[] = $filePath;
00524                         }
00525                         @closedir( $dirHandler );
00526                     }
00527                 }
00528                 foreach ( $designDirectories as $designDirectory )
00529                 {
00530                     if ( !file_exists( $designDirectory ) or
00531                          !is_dir( $designDirectory ) )
00532                         continue;
00533 
00534                     $filePath = $designDirectory . '/' . $file;
00535                     if ( file_exists( $filePath ) )
00536                     {
00537                         if ( preg_match( "#^([^/]+)/(.+)$#", $file, $matches ) )
00538                         {
00539                             $design = $matches[1];
00540                             $file = $matches[2];
00541                         }
00542                         else
00543                         {
00544                             $design = $file;
00545                             $file = false;
00546                         }
00547                         return $filePath;
00548                     }
00549 
00550                     $filePath = $designDirectory . '/' . $design . '/' . $roleFileName . '/' . $file;
00551                     if ( file_exists( $filePath ) )
00552                         return $filePath;
00553                     $triedFiles[] = $filePath;
00554                 }
00555             } break;
00556         }
00557         return false;
00558     }
00559 
00560     function iniMatch( $filePath, &$role, &$roleValue, &$file )
00561     {
00562         if ( preg_match( "#^settings/siteaccess/([^/]+)/([^/]+)$#", $filePath, $matches ) )
00563         {
00564             $role = 'siteaccess';
00565             $roleValue = $matches[1];
00566             $file = $matches[2];
00567             return $filePath;
00568         }
00569         else if ( preg_match( "#^settings/override/([^/]+)$#", $filePath, $matches ) )
00570         {
00571             $role = 'override';
00572             $roleValue = false;
00573             $file = $matches[1];
00574             return $filePath;
00575         }
00576         else if ( preg_match( "#^settings/([^/]+)$#", $filePath, $matches ) )
00577         {
00578             $role = 'standard';
00579             $roleValue = false;
00580             $file = $matches[1];
00581             return $filePath;
00582         }
00583         $triedFiles[] = $filePath;
00584         $filePath = 'settings';
00585         if ( $role == 'siteaccess' )
00586         {
00587             $filePath = 'settings/siteaccess';
00588             if ( $roleValue )
00589                 $filePath .= '/' . $roleValue;
00590         }
00591         else if ( $role == 'override' )
00592             $filePath = 'settings/override';
00593         $filePath .= '/' . $file;
00594         if ( file_exists( $filePath ) )
00595         {
00596             return $filePath;
00597         }
00598         $triedFiles[] = $filePath;
00599         $filePath = $file;
00600         if ( file_exists( $filePath ) )
00601         {
00602             if ( preg_match( "#^.+/([^/]+)$#", $filePath, $matches ) )
00603                 $file = $matches[1];
00604             return $filePath;
00605         }
00606         $triedFiles[] = $filePath;
00607         return false;
00608     }
00609 
00610     /*!
00611      \reimp
00612     */
00613     function createInstallNode( $package, $installNode, $installItem, $installType )
00614     {
00615         $installNode->setAttribute( 'collection', $installItem['collection'] );
00616     }
00617 
00618     /*!
00619      \reimp
00620     */
00621     function parseInstallNode( $package, $installNode, &$installParameters, $isInstall )
00622     {
00623         $collection = $installNode->getAttribute( 'collection' );
00624         $installParameters['collection'] = $collection;
00625     }
00626 }
00627 
00628 ?>