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