eZ Publish  [4.0]
ezimageshellhandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZImageShellHandler class
00004 //
00005 // Created on: <16-Oct-2003 14:22:43 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 ezimageshellhandler.php
00032 */
00033 
00034 /*!
00035   \class eZImageShellHandler ezimageshellhandler.php
00036   \ingroup eZImage
00037   \brief The class eZImageShellHandler does
00038 
00039 */
00040 
00041 //include_once( 'lib/ezimage/classes/ezimagehandler.php' );
00042 
00043 class eZImageShellHandler extends eZImageHandler
00044 {
00045     /*!
00046      Constructor
00047     */
00048     function eZImageShellHandler( $handlerName, $isEnabled = true, $outputRewriteType = self::REPLACE_SUFFIX,
00049                                   $supportedInputMIMETypes = false, $supportedOutputMIMETypes = false,
00050                                   $conversionRules = false, $filters = false, $mimeTagMap = false)
00051     {
00052         $this->eZImageHandler( $handlerName, $isEnabled, $outputRewriteType,
00053                                $supportedInputMIMETypes, $supportedOutputMIMETypes,
00054                                $conversionRules, $filters, $mimeTagMap );
00055         $this->Path = false;
00056         $this->Executable = false;
00057         $this->PreParameters = false;
00058         $this->PostParameters = false;
00059         $this->UseTypeTag = false;
00060         $this->QualityParameters = false;
00061         $this->FrameRangeParameters = false;
00062     }
00063 
00064     /*!
00065      Creates the shell string and runs the executable.
00066     */
00067     function convert( $manager, $sourceMimeData, &$destinationMimeData, $filters = false )
00068     {
00069         $argumentList = array();
00070         $executable = $this->Executable;
00071         if ( eZSys::osType() == 'win32' and $this->ExecutableWin32 )
00072             $executable = $this->ExecutableWin32;
00073         else if ( eZSys::osType() == 'mac' and $this->ExecutableMac )
00074             $executable = $this->ExecutableMac;
00075         else if ( eZSys::osType() == 'unix' and $this->ExecutableUnix )
00076             $executable = $this->ExecutableUnix;
00077         if ( $this->Path )
00078             $executable = $this->Path . eZSys::fileSeparator() . $executable;
00079         if ( eZSys::osType() == 'win32' )
00080             $executable = "\"$executable\"";
00081 
00082         $argumentList[] = $executable;
00083 
00084         if ( $this->PreParameters )
00085             $argumentList[] = $this->PreParameters;
00086 
00087         $qualityParameters = $this->QualityParameters;
00088         $frameRangeParameters = $this->FrameRangeParameters;
00089 
00090         if ( $qualityParameters and
00091              isset( $qualityParameters[$destinationMimeData['name']] ) )
00092         {
00093             $qualityParameter = $qualityParameters[$destinationMimeData['name']];
00094             $outputQuality = $manager->qualityValue( $destinationMimeData['name'] );
00095             if ( $outputQuality )
00096             {
00097                 $qualityArgument = eZSys::createShellArgument( $qualityParameter, array( '%1' => $outputQuality ) );
00098                 $argumentList[] = $qualityArgument;
00099             }
00100         }
00101 
00102         if ( $filters !== false )
00103         {
00104             foreach ( $filters as $filterData )
00105             {
00106                 $argumentList[] = $this->textForFilter( $filterData );
00107             }
00108         }
00109 
00110         if ( $frameRangeParameters && isset( $frameRangeParameters[$sourceMimeData['name']] ) )
00111         {
00112             $sourceMimeData['url'] .= $frameRangeParameters[$sourceMimeData['name']];
00113         }
00114 
00115         $argumentList[] = eZSys::escapeShellArgument( $sourceMimeData['url'] );
00116 
00117         $destinationURL = $destinationMimeData['url'];
00118         if ( $this->UseTypeTag )
00119             $destinationURL = $this->tagForMIMEType( $destinationMimeData ) . $this->UseTypeTag . $destinationURL;
00120         $argumentList[] = eZSys::escapeShellArgument( $destinationURL );
00121 
00122         if ( $this->PostParameters )
00123             $argumentList[] = $this->PostParameters;
00124 
00125         $systemString = implode( ' ', $argumentList );
00126         if ( eZSys::osType() == 'win32' )
00127             $systemString = "\"$systemString\"";
00128 
00129         system( $systemString, $returnCode );
00130 
00131         if ( $returnCode == 0 )
00132         {
00133             if ( !file_exists( $destinationMimeData['url'] ) )
00134             {
00135                 eZDebug::writeError( 'Unknown destination file: ' . $destinationMimeData['url'] . " when executing '$systemString'", 'eZImageShellHandler(' . $this->HandlerName . ')' );
00136                 return false;
00137             }
00138             $this->changeFilePermissions( $destinationMimeData['url'] );
00139             return true;
00140         }
00141         else
00142         {
00143             eZDebug::writeWarning( "Failed executing: $systemString, Error code: $returnCode", 'eZImageShellHandler::convert' );
00144             return false;
00145         }
00146 
00147     }
00148 
00149     /*!
00150      Creates a new image handler for shell executable from INI settings.
00151      The INI settings are read from ini file \a $iniFilename and group \a $iniGroup.
00152      If \a $iniFilename is not supplied \c image.ini is used.
00153     */
00154     static function createFromINI( $iniGroup, $iniFilename = false )
00155     {
00156         if ( !$iniFilename )
00157             $iniFilename = 'image.ini';
00158 
00159         $handler = false;
00160         //include_once( 'lib/ezutils/classes/ezini.php' );
00161         $ini = eZINI::instance( $iniFilename );
00162         if ( !$ini )
00163         {
00164             eZDebug::writeError( "Failed loading ini file $iniFilename",
00165                                  'eZImageShellHandler::createFromINI' );
00166             return $handler;
00167         }
00168 
00169         if ( $ini->hasGroup( $iniGroup ) )
00170         {
00171             $name = $iniGroup;
00172             if ( $ini->hasVariable( $iniGroup, 'Name' ) )
00173                 $name = $ini->variable( $iniGroup, 'Name' );
00174             $inputMimeList = false;
00175             $outputMimeList = false;
00176             if ( $ini->hasVariable( $iniGroup, 'InputMIMEList' ) )
00177                 $inputMimeList = $ini->variable( $iniGroup, 'InputMIMEList' );
00178             if ( $ini->hasVariable( $iniGroup, 'OutputMIMEList' ) )
00179                 $outputMimeList = $ini->variable( $iniGroup, 'OutputMIMEList' );
00180             $qualityParameters = false;
00181             if ( $ini->hasVariable( $iniGroup, 'QualityParameters' ) )
00182             {
00183                 $qualityParametersRaw = $ini->variable( $iniGroup, 'QualityParameters' );
00184                 foreach ( $qualityParametersRaw as $qualityParameterRaw )
00185                 {
00186                     $elements = explode( ';', $qualityParameterRaw );
00187                     $qualityParameters[$elements[0]] = $elements[1];
00188                 }
00189             }
00190 
00191             $frameRangeParameters = false;
00192             if ( $ini->hasVariable( $iniGroup, 'FrameRangeParameters' ) )
00193             {
00194                 foreach ( $ini->variable( $iniGroup, 'FrameRangeParameters' ) as $frameRangeParameter )
00195                 {
00196                     $elements = explode( ';', $frameRangeParameter );
00197                     $frameRangeParameters[$elements[0]] = $elements[1];
00198                 }
00199             }
00200 
00201             $conversionRules = false;
00202             if ( $ini->hasVariable( $iniGroup, 'ConversionRules' ) )
00203             {
00204                 $conversionRules = array();
00205                 $rules = $ini->variable( $iniGroup, 'ConversionRules' );
00206                 foreach ( $rules as $ruleString )
00207                 {
00208                     $ruleItems = explode( ';', $ruleString );
00209                     if ( count( $ruleItems ) >= 2 )
00210                     {
00211                         $conversionRules[] = array( 'from' => $ruleItems[0],
00212                                                     'to' => $ruleItems[1] );
00213                     }
00214                 }
00215             }
00216             $isEnabled = $ini->variable( $iniGroup, 'IsEnabled' ) == 'true';
00217             $path = false;
00218             $executable = false;
00219             $preParameters = false;
00220             $postParameters = false;
00221             if ( $ini->hasVariable( $iniGroup, 'ExecutablePath' ) )
00222                 $path = $ini->variable( $iniGroup, 'ExecutablePath' );
00223             if ( !$ini->hasVariable( $iniGroup, 'Executable' ) )
00224             {
00225                 eZDebug::writeError( "No Executable setting for group $iniGroup in ini file $iniFilename",
00226                                      'eZImageShellHandler::createFromINI' );
00227                 return $handler;
00228             }
00229             $executable = $ini->variable( $iniGroup, 'Executable' );
00230             $executableWin32 = false;
00231             $executableMac = false;
00232             $executableUnix = false;
00233             $ini->assign( $iniGroup, 'ExecutableWin32', $executableWin32 );
00234             $ini->assign( $iniGroup, 'ExecutableMac', $executableMac );
00235             $ini->assign( $iniGroup, 'ExecutableUnix', $executableUnix );
00236 
00237             if ( $ini->hasVariable( $iniGroup, 'ExecutablePath' ) )
00238                 $path = $ini->variable( $iniGroup, 'ExecutablePath' );
00239             if ( $ini->hasVariable( $iniGroup, 'ExecutablePath' ) )
00240                 $path = $ini->variable( $iniGroup, 'ExecutablePath' );
00241             if ( $ini->hasVariable( $iniGroup, 'PreParameters' ) )
00242                 $preParameters = $ini->variable( $iniGroup, 'PreParameters' );
00243             if ( $ini->hasVariable( $iniGroup, 'PostParameters' ) )
00244                 $postParameters = $ini->variable( $iniGroup, 'PostParameters' );
00245             $useTypeTag = false;
00246             if ( $ini->hasVariable( $iniGroup, 'UseTypeTag' ) )
00247             {
00248                 $useTypeTag = $ini->variable( $iniGroup, 'UseTypeTag' );
00249             }
00250             $outputRewriteType = self::REPLACE_SUFFIX;
00251             $filters = false;
00252             if ( $ini->hasVariable( $iniGroup, 'Filters' ) )
00253             {
00254                 $filterRawList = $ini->variable( $iniGroup, 'Filters' );
00255                 $filters = array();
00256                 foreach ( $filterRawList as $filterRawItem )
00257                 {
00258                     $filter = eZImageHandler::createFilterDefinitionFromINI( $filterRawItem );
00259                     $filters[] = $filter;
00260                 }
00261             }
00262             $mimeTagMap = false;
00263             if ( $ini->hasVariable( $iniGroup, 'MIMETagMap' ) )
00264             {
00265                 $mimeTagMapList = $ini->variable( $iniGroup, 'MIMETagMap' );
00266                 $mimeTagMap = array();
00267                 foreach ( $mimeTagMapList as $mimeTagMapItem )
00268                 {
00269                     $mimeTagMapArray = explode( ';', $mimeTagMapItem );
00270                     if ( count( $mimeTagMapArray ) >= 2 )
00271                         $mimeTagMap[$mimeTagMapArray[0]] = $mimeTagMapArray[1];
00272                 }
00273             }
00274             $handler = new eZImageShellHandler( $name, $isEnabled,
00275                                                 $outputRewriteType,
00276                                                 $inputMimeList, $outputMimeList,
00277                                                 $conversionRules, $filters, $mimeTagMap );
00278             $handler->Path = $path;
00279             $handler->Executable = $executable;
00280             $handler->ExecutableWin32 = $executableWin32;
00281             $handler->ExecutableMac = $executableMac;
00282             $handler->ExecutableUnix = $executableUnix;
00283             $handler->PreParameters = $preParameters;
00284             $handler->PostParameters = $postParameters;
00285             $handler->UseTypeTag = $useTypeTag;
00286             $handler->QualityParameters = $qualityParameters;
00287             $handler->FrameRangeParameters = $frameRangeParameters;
00288             return $handler;
00289         }
00290         return $handler;
00291     }
00292 
00293     /// \privatesection
00294     public $Path;
00295     public $Executable;
00296     public $PreParameters;
00297     public $PostParameters;
00298 }
00299 
00300 ?>