41830e25511a679642715efea87af0a4b25f7403
[Packages/TYPO3.CMS.git] / typo3 / sysext / saltedpasswords / Classes / Salt / SaltFactory.php
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords\Salt;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Marcus Krause <marcus#exp2009@t3sec.info>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Contains class "tx_saltedpasswords_salts_factory"
31 * that provides a salted hashing method factory.
32 */
33 /**
34 * Class that implements Blowfish salted hashing based on PHP's
35 * crypt() function.
36 *
37 * @author Marcus Krause <marcus#exp2009@t3sec.info>
38 * @since 2009-09-06
39 */
40 class SaltFactory {
41
42 /**
43 * An instance of the salted hashing method.
44 * This member is set in the getSaltingInstance() function.
45 *
46 * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
47 */
48 static protected $instance = NULL;
49
50 /**
51 * Obtains a salting hashing method instance.
52 *
53 * This function will return an instance of a class that implements
54 * tx_saltedpasswords_abstract_salts.
55 *
56 * Use parameter NULL to reset the factory!
57 *
58 * @param string $saltedHash (optional) Salted hashed password to determine the type of used method from or NULL to reset the factory
59 * @param string $mode (optional) The TYPO3 mode (FE or BE) saltedpasswords shall be used for
60 * @return tx_saltedpasswords_abstract_salts an instance of salting hashing method object
61 */
62 static public function getSaltingInstance($saltedHash = '', $mode = TYPO3_MODE) {
63 // Creating new instance when
64 // * no instance existing
65 // * a salted hash given to determine salted hashing method from
66 // * a NULL parameter given to reset instance back to default method
67 if (!is_object(self::$instance) || !empty($saltedHash) || is_NULL($saltedHash)) {
68 // Determine method by checking the given hash
69 if (!empty($saltedHash)) {
70 $result = self::determineSaltingHashingMethod($saltedHash);
71 if (!$result) {
72 self::$instance = NULL;
73 }
74 } else {
75 $classNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod($mode);
76 $availableClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
77 self::$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($availableClasses[$classNameToUse], 'tx_');
78 }
79 }
80 return self::$instance;
81 }
82
83 /**
84 * Method tries to determine the salting hashing method used for given salt.
85 *
86 * Method implicitly sets the instance of the found method object in the class property when found.
87 *
88 * @param string $saltedHash
89 * @return boolean TRUE, if salting hashing method has been found, otherwise FALSE
90 */
91 static public function determineSaltingHashingMethod($saltedHash) {
92 $methodFound = FALSE;
93 $defaultMethods = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
94 foreach ($defaultMethods as $method) {
95 $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($method, 'tx_');
96 if ($objectInstance instanceof \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface) {
97 $methodFound = $objectInstance->isValidSaltedPW($saltedHash);
98 if ($methodFound) {
99 self::$instance = $objectInstance;
100 break;
101 }
102 }
103 }
104 return $methodFound;
105 }
106
107 /**
108 * Method sets a custom salting hashing method class.
109 *
110 * @param string $resource Object resource to use (e.g. 'EXT:saltedpasswords/classes/salts/class.tx_saltedpasswords_salts_blowfish.php:tx_saltedpasswords_salts_blowfish')
111 * @return tx_saltedpasswords_abstract_salts an instance of salting hashing method object
112 */
113 static public function setPreferredHashingMethod($resource) {
114 self::$instance = NULL;
115 $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($resource);
116 if (is_object($objectInstance) && is_subclass_of($objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\AbstractSalt')) {
117 self::$instance = $objectInstance;
118 }
119 return self::$instance;
120 }
121
122 }
123 ?>