[FEATURE] Add hashing algorithms to stdWrap
authorFabrizio Branca <mail@fabrizio-branca.de>
Mon, 11 Jul 2011 19:02:04 +0000 (21:02 +0200)
committerXavier Perseguers <typo3@perseguers.ch>
Tue, 12 Jul 2011 07:36:42 +0000 (09:36 +0200)
Enable hashing the content through stdWrap using different hashing algorithms

Change-Id: I0673f33a1ca7bcb5784e5ab9f0def8db5ff7ea4b
Resolves: #28095
Releases: 4.6
Reviewed-on: http://review.typo3.org/3257
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
tests/typo3/sysext/cms/tslib/class.tslib_contentTest.php
typo3/sysext/cms/tslib/class.tslib_content.php

index 9715569..1543be3 100644 (file)
@@ -721,7 +721,72 @@ class tslib_contentTest extends tx_phpunit_testcase {
                $result = $this->cObj->stdWrap_round($float, $conf);
                $this->assertEquals($expected, $result);
        }
-       
+
+       /**
+        * Data provider for the hash test
+        *
+        * @return array multi-dimensional array with the second level like this:
+        *               0 => the plain text
+        *               1 => the conf array for the hash stdWrap function
+        *               2 => the expected result
+        *
+        * @see hash
+        */
+       public function hashDataProvider() {
+               $data = array(
+                       'testing md5' => array(
+                               'joh316',
+                               array(
+                                       'hash' => 'md5'
+                               ),
+                               'bacb98acf97e0b6112b1d1b650b84971'
+                       ),
+                       'testing sha1' => array(
+                               'joh316',
+                               array(
+                                       'hash' => 'sha1'
+                               ),
+                               '063b3d108bed9f88fa618c6046de0dccadcf3158'
+                       ),
+                       'testing non-existing hashing algorithm' => array(
+                               'joh316',
+                               array(
+                                       'hash' => 'non-existing'
+                               ),
+                               'joh316'
+                       ),
+                       'testing stdWrap capability' => array(
+                               'joh316',
+                               array(
+                                       'hash.' => array(
+                                               'cObject' => 'TEXT',
+                                               'cObject.' => array(
+                                                       'value' => 'md5'
+                                               )
+                                       )
+                               ),
+                               'bacb98acf97e0b6112b1d1b650b84971'
+                       )
+               );
+               return $data;
+       }
+
+       /**
+        * Test for the stdWrap function "hash"
+        *
+        * @param string $text
+        * @param array $conf
+        * @param string $expected
+        * @return void
+        *
+        * @dataProvider hashDataProvider
+        * @test
+        */
+       public function stdWrap_hash($text, array $conf, $expected) {
+               $result = $this->cObj->stdWrap_hash($text, $conf);
+               $this->assertEquals($expected, $result);
+       }
+
        /**
         * Data provider for the numberFormat test
         *
@@ -775,7 +840,7 @@ class tslib_contentTest extends tx_phpunit_testcase {
                );
                return $data;
        }
-       
+
        /**
         * Check if stdWrap.numberFormat and all of its properties work properly
         *
index 37d8c16..044d7a8 100644 (file)
@@ -119,6 +119,8 @@ class tslib_cObj {
                'char.' => 'array',
                'intval' => 'boolean',
                'intval.' => 'array',
+               'hash' => 'string',
+               'hash.' => 'array',
                'round' => 'boolean',
                'round.' => 'array',
                'numberFormat.' => 'array',
@@ -2326,6 +2328,24 @@ class tslib_cObj {
        }
 
        /**
+        * Will return a hashed value of the current content
+        *
+        * @param string Input value undergoing processing in this function.
+        * @param array stdWrap properties for hash.
+        * @return string The processed input value
+        * @link http://php.net/manual/de/function.hash-algos.php for a list of supported hash algorithms
+        */
+       public function stdWrap_hash($content = '', array $conf = array()) {
+               $algorithm = isset($conf['hash.'])
+                               ? $this->stdWrap($conf['hash'], $conf['hash.'])
+                               : $conf['hash'];
+               if (function_exists('hash') && in_array($algorithm, hash_algos())) {
+                       $content = hash($algorithm, $content);
+               }
+               return $content;
+       }
+
+       /**
         * stdWrap_round will return a rounded number with ceil(), floor() or round(), defaults to round()
         * Only the english number format is supported . (dot) as decimal point
         *