TYPO3 API  SVNRelease
class.tx_saltedpasswords_salts_factory.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009-2011 Marcus Krause <marcus#exp2009@t3sec.info>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 /**
00028  * Contains class "tx_saltedpasswords_salts_factory"
00029  * that provides a salted hashing method factory.
00030  *
00031  * $Id: class.tx_saltedpasswords_salts_factory.php 10120 2011-01-18 20:03:36Z ohader $
00032  */
00033 
00034 
00035 /**
00036  * Class that implements Blowfish salted hashing based on PHP's
00037  * crypt() function.
00038  *
00039  * @author      Marcus Krause <marcus#exp2009@t3sec.info>
00040  *
00041  * @since       2009-09-06
00042  * @package     TYPO3
00043  * @subpackage  tx_saltedpasswords
00044  */
00045 class tx_saltedpasswords_salts_factory {
00046     /**
00047      * An instance of the salted hashing method.
00048      * This member is set in the getSaltingInstance() function.
00049      *
00050      * @var tx_saltedpasswords_abstract_salts
00051      */
00052     static protected $instance = NULL;
00053 
00054 
00055     /**
00056      * Obtains a salting hashing method instance.
00057      *
00058      * This function will return an instance of a class that implements
00059      * tx_saltedpasswords_abstract_salts.
00060      *
00061      * Use parameter NULL to reset the factory!
00062      *
00063      * @param   string      $saltedHash: (optional) salted hashed password to determine the type of used method from or NULL to reset the factory
00064      * @param   string      $mode: (optional) The TYPO3 mode (FE or BE) saltedpasswords shall be used for
00065      * @return  tx_saltedpasswords_abstract_salts   an instance of salting hashing method object
00066      */
00067     static public function getSaltingInstance($saltedHash = '', $mode = TYPO3_MODE) {
00068             // creating new instance when
00069             // * no instance existing
00070             // * a salted hash given to determine salted hashing method from
00071             // * a NULL parameter given to reset instance back to default method
00072         if (!is_object(self::$instance) || !empty($saltedHash) || is_NULL($saltedHash)) {
00073 
00074                 // determine method by checking the given hash
00075             if (!empty($saltedHash)) {
00076                 $result = self::determineSaltingHashingMethod($saltedHash);
00077                 if(!$result) {
00078                     self::$instance = NULL;
00079                 }
00080             } else {
00081                 $classNameToUse = tx_saltedpasswords_div::getDefaultSaltingHashingMethod($mode);
00082                 $availableClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
00083                 self::$instance = t3lib_div::getUserObj($availableClasses[$classNameToUse], 'tx_');
00084             }
00085         }
00086 
00087         return self::$instance;
00088     }
00089 
00090     /**
00091      * Method tries to determine the salting hashing method used for given salt.
00092      *
00093      * Method implicitly sets the instance of the found method object in the class property when found.
00094      *
00095      * @param   string      $saltedHash
00096      * @return  boolean     TRUE, if salting hashing method has been found, otherwise FALSE
00097      */
00098     static public function determineSaltingHashingMethod($saltedHash) {
00099         $methodFound = FALSE;
00100         $defaultMethods = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
00101         foreach($defaultMethods as $method) {
00102             $objectInstance = t3lib_div::getUserObj($method, 'tx_');
00103             if ($objectInstance instanceof tx_saltedpasswords_salts) {
00104                 $methodFound = $objectInstance->isValidSaltedPW($saltedHash);
00105                 if ($methodFound) {
00106                     self::$instance = $objectInstance;
00107                     break;
00108                 }
00109             }
00110         }
00111 
00112         return $methodFound;
00113     }
00114 
00115     /**
00116      * Method sets a custom salting hashing method class.
00117      *
00118      * @param   string      $resource: object resource to use (e.g. 'EXT:saltedpasswords/classes/salts/class.tx_saltedpasswords_salts_blowfish.php:tx_saltedpasswords_salts_blowfish')
00119      * @return  tx_saltedpasswords_abstract_salts   an instance of salting hashing method object
00120      */
00121     static public function setPreferredHashingMethod($resource) {
00122         self::$instance = NULL;
00123         $objectInstance = t3lib_div::getUserObj($resource);
00124         if (is_object($objectInstance)
00125             && is_subclass_of($objectInstance, 'tx_saltedpasswords_abstract_salts')) {
00126                 self::$instance = $objectInstance;
00127         }
00128 
00129         return self::$instance;
00130     }
00131 }
00132 
00133 ?>