[TASk] Remove dummy files from ext:saltedpasswords
[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 /**
31 * Class that implements Blowfish salted hashing based on PHP's
32 * crypt() function.
33 *
34 * @author Marcus Krause <marcus#exp2009@t3sec.info>
35 */
36 class SaltFactory {
37
38 /**
39 * An instance of the salted hashing method.
40 * This member is set in the getSaltingInstance() function.
41 *
42 * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
43 */
44 static protected $instance = NULL;
45
46 /**
47 * Obtains a salting hashing method instance.
48 *
49 * This function will return an instance of a class that implements
50 * \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
51 *
52 * Use parameter NULL to reset the factory!
53 *
54 * @param string $saltedHash (optional) Salted hashed password to determine the type of used method from or NULL to reset the factory
55 * @param string $mode (optional) The TYPO3 mode (FE or BE) saltedpasswords shall be used for
56 * @return \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt An instance of salting hashing method object
57 */
58 static public function getSaltingInstance($saltedHash = '', $mode = TYPO3_MODE) {
59 // Creating new instance when
60 // * no instance existing
61 // * a salted hash given to determine salted hashing method from
62 // * a NULL parameter given to reset instance back to default method
63 if (!is_object(self::$instance) || !empty($saltedHash) || is_NULL($saltedHash)) {
64 // Determine method by checking the given hash
65 if (!empty($saltedHash)) {
66 $result = self::determineSaltingHashingMethod($saltedHash);
67 if (!$result) {
68 self::$instance = NULL;
69 }
70 } else {
71 $classNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod($mode);
72 $availableClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
73 self::$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($availableClasses[$classNameToUse], 'tx_');
74 }
75 }
76 return self::$instance;
77 }
78
79 /**
80 * Method tries to determine the salting hashing method used for given salt.
81 *
82 * Method implicitly sets the instance of the found method object in the class property when found.
83 *
84 * @param string $saltedHash
85 * @return boolean TRUE, if salting hashing method has been found, otherwise FALSE
86 */
87 static public function determineSaltingHashingMethod($saltedHash) {
88 $methodFound = FALSE;
89 $defaultMethods = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
90 foreach ($defaultMethods as $method) {
91 $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($method, 'tx_');
92 if ($objectInstance instanceof \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface) {
93 $methodFound = $objectInstance->isValidSaltedPW($saltedHash);
94 if ($methodFound) {
95 self::$instance = $objectInstance;
96 break;
97 }
98 }
99 }
100 return $methodFound;
101 }
102
103 /**
104 * Method sets a custom salting hashing method class.
105 *
106 * @param string $resource Object resource to use (e.g. 'TYPO3\\CMS\\Saltedpasswords\\Salt\\BlowfishSalt')
107 * @return \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt An instance of salting hashing method object
108 */
109 static public function setPreferredHashingMethod($resource) {
110 self::$instance = NULL;
111 $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($resource);
112 if (is_object($objectInstance) && is_subclass_of($objectInstance, 'TYPO3\\CMS\\Saltedpasswords\\Salt\\AbstractSalt')) {
113 self::$instance = $objectInstance;
114 }
115 return self::$instance;
116 }
117
118 }
119 ?>