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