[BUGFIX] Namespaces: Bring back OpenID library
[Packages/TYPO3.CMS.git] / typo3 / sysext / openid / Classes / OpenidStore.php
1 <?php
2 namespace TYPO3\CMS\Openid;
3
4 /**
5 * This class is a TYPO3-specific OpenID store.
6 *
7 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
8 * @package TYPO3
9 * @subpackage tx_openid
10 */
11 class OpenidStore extends \Auth_OpenID_OpenIDStore {
12
13 const ASSOCIATION_TABLE_NAME = 'tx_openid_assoc_store';
14 const ASSOCIATION_EXPIRATION_SAFETY_INTERVAL = 120;
15 /* 2 minutes */
16 const NONCE_TABLE_NAME = 'tx_openid_nonce_store';
17 const NONCE_STORAGE_TIME = 864000;
18 /* 10 days */
19 /**
20 * Sores the association for future use
21 *
22 * @param string $serverUrl Server URL
23 * @param \Auth_OpenID_Association $association OpenID association
24 * @return void
25 */
26 public function storeAssociation($serverUrl, $association) {
27 /* @var $association \Auth_OpenID_Association */
28 $GLOBALS['TYPO3_DB']->sql_query('START TRANSACTION');
29 if ($this->doesAssociationExist($serverUrl, $association->handle)) {
30 $this->updateExistingAssociation($serverUrl, $association);
31 } else {
32 $this->storeNewAssociation($serverUrl, $association);
33 }
34 $GLOBALS['TYPO3_DB']->sql_query('COMMIT');
35 }
36
37 /**
38 * Removes all expired associations.
39 *
40 * @return int A number of removed associations
41 */
42 public function cleanupAssociations() {
43 $where = sprintf('expires<=%d', time());
44 $GLOBALS['TYPO3_DB']->exec_DELETEquery(self::ASSOCIATION_TABLE_NAME, $where);
45 return $GLOBALS['TYPO3_DB']->sql_affected_rows();
46 }
47
48 /**
49 * Obtains the association to the server
50 *
51 * @param string $serverUrl Server URL
52 * @param string $handle Association handle (optional)
53 * @return \Auth_OpenID_Association
54 */
55 public function getAssociation($serverUrl, $handle = NULL) {
56 $this->cleanupAssociations();
57 $where = sprintf('server_url=%s AND expires>%d', $GLOBALS['TYPO3_DB']->fullQuoteStr($serverUrl, self::ASSOCIATION_TABLE_NAME), time());
58 if ($handle != NULL) {
59 $where .= sprintf(' AND assoc_handle=%s', $GLOBALS['TYPO3_DB']->fullQuoteStr($handle, self::ASSOCIATION_TABLE_NAME));
60 $sort = '';
61 } else {
62 $sort = 'tstamp DESC';
63 }
64 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid, content', self::ASSOCIATION_TABLE_NAME, $where, '', $sort);
65 $result = NULL;
66 if (is_array($row)) {
67 $result = @unserialize(base64_decode($row['content']));
68 if ($result === FALSE) {
69 $result = NULL;
70 } else {
71 $this->updateAssociationTimeStamp($row['tstamp']);
72 }
73 }
74 return $result;
75 }
76
77 /**
78 * Removes the association
79 *
80 * @param string $serverUrl Server URL
81 * @param string $handle Association handle (optional)
82 * @return boolean TRUE if the association existed
83 * @todo Define visibility
84 */
85 public function removeAssociation($serverUrl, $handle) {
86 $where = sprintf('server_url=%s AND assoc_handle=%s', $GLOBALS['TYPO3_DB']->fullQuoteStr($serverUrl, self::ASSOCIATION_TABLE_NAME), $GLOBALS['TYPO3_DB']->fullQuoteStr($handle, self::ASSOCIATION_TABLE_NAME));
87 $GLOBALS['TYPO3_DB']->exec_DELETEquery(self::ASSOCIATION_TABLE_NAME, $where);
88 $deletedCount = $GLOBALS['TYPO3_DB']->sql_affected_rows();
89 return $deletedCount > 0;
90 }
91
92 /**
93 * Removes old nonces
94 *
95 * @return void
96 */
97 public function cleanupNonces() {
98 $where = sprintf('crdate<%d', time() - self::NONCE_STORAGE_TIME);
99 $GLOBALS['TYPO3_DB']->exec_DELETEquery(self::NONCE_TABLE_NAME, $where);
100 }
101
102 /**
103 * Checks if this nonce was already used
104 *
105 * @param string $serverUrl Server URL
106 * @param integer $timestamp Time stamp
107 * @param string $salt Nonce value
108 * @return boolean TRUE if nonce was not used before anc can be used now
109 */
110 public function useNonce($serverUrl, $timestamp, $salt) {
111 $result = FALSE;
112 if (abs($timestamp - time()) < $GLOBALS['Auth_OpenID_SKEW']) {
113 $values = array(
114 'crdate' => time(),
115 'salt' => $salt,
116 'server_url' => $serverUrl,
117 'tstamp' => $timestamp
118 );
119 $GLOBALS['TYPO3_DB']->exec_INSERTquery(self::NONCE_TABLE_NAME, $values);
120 $affectedRows = $GLOBALS['TYPO3_DB']->sql_affected_rows();
121 $result = $affectedRows > 0;
122 }
123 return $result;
124 }
125
126 /**
127 * Resets the store by removing all data in it
128 *
129 * @return void
130 */
131 public function reset() {
132 $GLOBALS['TYPO3_DB']->exec_DELETEquery(self::ASSOCIATION_TABLE_NAME, '1=1');
133 $GLOBALS['TYPO3_DB']->exec_DELETEquery(self::NONCE_TABLE_NAME, '1=1');
134 }
135
136 /**
137 * Checks if such association exists.
138 *
139 * @param string $serverUrl Server URL
140 * @param \Auth_OpenID_Association $association OpenID association
141 * @return boolean
142 */
143 protected function doesAssociationExist($serverUrl, $association) {
144 $where = sprintf('server_url=%s AND assoc_handle=%s AND expires>%d', $GLOBALS['TYPO3_DB']->fullQuoteStr($serverUrl, self::ASSOCIATION_TABLE_NAME), $GLOBALS['TYPO3_DB']->fullQuoteStr($association->handle, self::ASSOCIATION_TABLE_NAME), time());
145 $row = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('COUNT(*) as assocCount', self::ASSOCIATION_TABLE_NAME, $where);
146 return $row['assocCount'] > 0;
147 }
148
149 /**
150 * Updates existing association.
151 *
152 * @param string $serverUrl Server URL
153 * @param \Auth_OpenID_Association $association OpenID association
154 * @return void
155 */
156 protected function updateExistingAssociation($serverUrl, \Auth_OpenID_Association $association) {
157 $where = sprintf('server_url=%s AND assoc_handle=%s AND expires>%d', $GLOBALS['TYPO3_DB']->fullQuoteStr($serverUrl, self::ASSOCIATION_TABLE_NAME), $GLOBALS['TYPO3_DB']->fullQuoteStr($association->handle, self::ASSOCIATION_TABLE_NAME), time());
158 $serializedAssociation = serialize($association);
159 $values = array(
160 'content' => base64_encode($serializedAssociation),
161 'tstamp' => time()
162 );
163 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(self::ASSOCIATION_TABLE_NAME, $where, $values);
164 }
165
166 /**
167 * Stores new association to the database.
168 *
169 * @param string $serverUrl Server URL
170 * @param \Auth_OpenID_Association $association OpenID association
171 * @return void
172 */
173 protected function storeNewAssociation($serverUrl, $association) {
174 $serializedAssociation = serialize($association);
175 $values = array(
176 'assoc_handle' => $association->handle,
177 'content' => base64_encode($serializedAssociation),
178 'crdate' => $association->issued,
179 'tstamp' => time(),
180 'expires' => ($association->issued + $association->lifetime) - self::ASSOCIATION_EXPIRATION_SAFETY_INTERVAL,
181 'server_url' => $serverUrl
182 );
183 // In the next query we can get race conditions. sha1_hash prevents many
184 // asociations from being stored for one server
185 $GLOBALS['TYPO3_DB']->exec_INSERTquery(self::ASSOCIATION_TABLE_NAME, $values);
186 }
187
188 /**
189 * Updates association time stamp.
190 *
191 * @param integer $recordId Association record id in the database
192 * @return void
193 */
194 protected function updateAssociationTimeStamp($recordId) {
195 $where = sprintf('uid=%d', $recordId);
196 $values = array(
197 'tstamp' => time()
198 );
199 $GLOBALS['TYPO3_DB']->exec_UPDATEquery(self::ASSOCIATION_TABLE_NAME, $where, $values);
200 }
201
202 }
203
204
205 ?>