eZ Publish  [4.0]
eztemplatedigestoperator.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZTemplateDigestOperator class
00004 //
00005 // Created on: <18-Jul-2003 13:00:18 bh>
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 /*!
00032   \class eZTemplateDigestOperator eztemplatedigestoperator.php
00033   \ingroup eZTemplateOperators
00034 \code
00035 
00036 \endcode
00037 
00038 */
00039 
00040 class eZTemplateDigestOperator
00041 {
00042     /*!
00043      Constructor.
00044     */
00045     function eZTemplateDigestOperator()
00046     {
00047         $this->Operators = array( 'crc32', 'md5', 'rot13' );
00048         if ( function_exists( 'sha1' ) )
00049         {
00050             $this->Operators[] = 'sha1';
00051         }
00052         foreach ( $this->Operators as $operator )
00053         {
00054             $name = $operator . 'Name';
00055             $name[0] = $name[0] & "\xdf";
00056             $this->$name = $operator;
00057         }
00058     }
00059 
00060     /*!
00061      Returns the template operators.
00062     */
00063     function operatorList()
00064     {
00065         return $this->Operators;
00066     }
00067 
00068     function operatorTemplateHints()
00069     {
00070         return array( $this->Crc32Name => array( 'input' => true,
00071                                                  'output' => true,
00072                                                  'parameters' => false,
00073                                                  'element-transformation' => true,
00074                                                  'transform-parameters' => true,
00075                                                  'input-as-parameter' => 'always',
00076                                                  'element-transformation-func' => 'hashTransformation'),
00077                       $this->Md5Name => array( 'input' => true,
00078                                                'output' => true,
00079                                                'parameters' => true,
00080                                                'element-transformation' => true,
00081                                                'transform-parameters' => true,
00082                                                'input-as-parameter' => 'always',
00083                                                'element-transformation-func' => 'hashTransformation'),
00084                       $this->Sha1Name => array( 'input' => true,
00085                                                 'output' => true,
00086                                                 'parameters' => true,
00087                                                 'element-transformation' => true,
00088                                                 'transform-parameters' => true,
00089                                                 'input-as-parameter' => 'always',
00090                                                 'element-transformation-func' => 'hashTransformation'),
00091                       $this->Rot13Name => array( 'input' => true,
00092                                                  'output' => true,
00093                                                  'parameters' => true,
00094                                                  'element-transformation' => true,
00095                                                  'transform-parameters' => true,
00096                                                  'input-as-parameter' => 'always',
00097                                                  'element-transformation-func' => 'hashTransformation') );
00098     }
00099 
00100     /*!
00101      \return true to tell the template engine that the parameter list exists per operator type.
00102     */
00103     function namedParameterPerOperator()
00104     {
00105         return true;
00106     }
00107 
00108     /*!
00109      See eZTemplateOperator::namedParameterList()
00110     */
00111     function namedParameterList()
00112     {
00113         return false;
00114     }
00115 
00116     function hashTransformation( $operatorName, &$node, $tpl, &$resourceData,
00117                                  $element, $lastElement, $elementList, $elementTree, &$parameters )
00118     {
00119         if ( ( count( $parameters ) != 1) )
00120         {
00121             return false;
00122         }
00123 
00124         $code = '';
00125         $function = '';
00126         $newElements = array();
00127         $values = array( $parameters[0] );
00128 
00129         switch ( $operatorName )
00130         {
00131             case 'crc32':
00132                 {
00133                     $code = "//include_once( 'lib/ezutils/classes/ezsys.php' );\n";
00134                     $function = "eZSys::ezcrc32";
00135                 } break;
00136 
00137             case 'rot13':
00138                 {
00139                     $function = 'str_rot13';
00140                 } break;
00141 
00142             default:
00143                 {
00144                     $function = $operatorName;
00145                 } break;
00146         }
00147 
00148         $code .= "%output% = $function( %1% );\n";
00149 
00150         $newElements[] = eZTemplateNodeTool::createCodePieceElement( $code, $values );
00151         return $newElements;
00152     }
00153 
00154     /*!
00155      Display the variable.
00156     */
00157     function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters,
00158                      $placement )
00159     {
00160         $digestData = $operatorValue;
00161         switch ( $operatorName )
00162         {
00163             // Calculate and return crc32 polynomial.
00164             case $this->Crc32Name:
00165             {
00166                 //include_once( 'lib/ezutils/classes/ezsys.php' );
00167                 $operatorValue = eZSys::ezcrc32( $digestData );
00168             }break;
00169 
00170             // Calculate the MD5 hash.
00171             case $this->Md5Name:
00172             {
00173                 $operatorValue = md5( $digestData );
00174             }break;
00175 
00176             // Calculate the SHA1 hash.
00177             case $this->Sha1Name:
00178             {
00179                 $operatorValue = sha1( $digestData );
00180             }break;
00181 
00182             // Preform rot13 transform on the string.
00183             case $this->Rot13Name:
00184             {
00185                 $operatorValue = str_rot13( $digestData );
00186             }break;
00187 
00188             // Default case: something went wrong - unknown things...
00189             default:
00190             {
00191                 $tpl->warning( $operatorName, "Unknown input type '$type'", $placement );
00192             } break;
00193         }
00194     }
00195 
00196     /// The array of operators, used for registering operators
00197     public $Operators;
00198 }
00199 
00200 ?>