## Apache Zeta Components Manual :: File Source for openid_db_store.php

## Source for file openid_db_store.php

Documentation is available at openid_db_store.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationOpenidDbStore class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
23. * @filesource
24. * @package AuthenticationDatabaseTiein
25. * @version //autogentag//
26. */
28. /**
29. * Class providing database storage for OpenID authentication.
30. *
31. * This class requires that the database used contains two special tables. See
32. * the tutorial for information on how to create those tables.
33. *
34. * Example of use:
35. * <code>
36. * // create an OpenID options object
37. * $options = new ezcAuthenticationOpenidOptions();
38. * $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
39. *
40. * // define a database store
41. * $options->store = new ezcAuthenticationOpenidDbStore( ezcDbInstance::get() );
42. *
43. * // create an OpenID filter based on the options object
44. * $filter = new ezcAuthenticationOpenidFilter( $options );
45. * </code>
46. *
47. * @property ezcDbHandler $instance
48. * The database instance to use for database storage.
49. *
50. * @package AuthenticationDatabaseTiein
51. * @version //autogentag//
52. */
53. class ezcAuthenticationOpenidDbStore extends ezcAuthenticationOpenidStore
54. {
55. /**
56. * Holds the properties of this class.
57. *
58. * @var array(string=>mixed)
59. */
60. private $properties = array();
62. /**
63. * Creates a new object of this class.
64. *
65. * @param ezcDbHandler $instance The database instance used for this store
66. * @param ezcAuthenticationOpenidDbStoreOptions $options Options for this class
67. */
68. public function __construct ( ezcDbHandler $instance, ezcAuthenticationOpenidDbStoreOptions $options = null )
69. {
70. $this->instance = $instance;
71. $this-> options = ( $options === null ) ? new ezcAuthenticationOpenidDbStoreOptions () : $options;
72. }
74. /**
75. * Sets the property $name to $value.
76. *
77. * @throws ezcBasePropertyNotFoundException
78. * if the property $name does not exist
79. * @throws ezcBaseValueException
80. * if $value is not correct for the property $name
81. * @param string $name The name of the property to set
82. * @param mixed $value The new value of the property
83. * @ignore
84. */
85. public function __set( $name, $value )
86. {
87. switch ( $name )
88. {
89. case 'instance':
90. if ( !( $value instanceof ezcDbHandler ) )
91. {
92. throw new ezcBaseValueException ( $name, $value, 'ezcDbHandler' );
93. }
95. $this->properties[$name] = $value;
96. break;
98. default:
99. throw new ezcBasePropertyNotFoundException ( $name );
100. }
101. }
103. /**
104. * Returns the value of the property $name.
105. *
106. * @throws ezcBasePropertyNotFoundException
107. * if the property $name does not exist
108. * @param string $name The name of the property for which to return the value
109. * @return mixed
110. * @ignore
111. */
112. public function __get( $name )
113. {
114. switch ( $name )
115. {
116. case 'instance':
117. return $this->properties[$name];
119. default:
120. throw new ezcBasePropertyNotFoundException ( $name );
121. }
122. }
124. /**
125. * Returns true if the property $name is set, otherwise false.
126. *
127. * @param string $name The name of the property to test if it is set
128. * @return bool
129. * @ignore
130. */
131. public function __isset( $name )
132. {
133. switch ( $name )
134. {
135. case 'instance':
136. return isset( $this->properties[$name] );
138. default:
139. return false;
140. }
141. }
143. /**
144. * Stores the nonce in the store.
145. *
146. * Returns true if the nonce was stored successfully, and false otherwise.
147. *
148. * @throws ezcBaseFilePermissionException
149. * if the nonce cannot be written in the store
150. * @param string $nonce The nonce value to store
151. * @return bool
152. */
153. public function storeNonce ( $nonce )
154. {
155. $table = $this-> options ->tableNonces;
157. $query = new ezcQueryInsert ( $this->instance );
159. $query->insertInto( $this->instance->quoteIdentifier( $table['name'] ) )
160. ->set( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
161. ->set( $this->instance->quoteIdentifier( $table['fields']['timestamp'] ), $query->bindValue( time () ) );
163. $stmt = $query->prepare();
164. $stmt->execute();
166. return true;
167. }
169. /**
170. * Checks if the nonce exists and afterwards deletes it.
171. *
172. * Returns the timestamp of the nonce if it exists, and false otherwise.
173. *
174. * @param string $nonce The nonce value to check and delete
175. * @return bool|int
176. */
177. public function useNonce ( $nonce )
178. {
179. $table = $this-> options ->tableNonces;
181. $query = new ezcQuerySelect ( $this->instance );
182. $e = $query->expr;
183. $query->select( '*' )
184. ->from( $this->instance->quoteIdentifier( $table['name'] ) )
185. ->where(
186. $e->eq( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
187. );
188. $query = $query->prepare();
189. $query->execute();
190. $rows = $query->fetchAll();
191. if ( count ( $rows ) > 0 )
192. {
193. $rows = $rows[0];
194. $lastModified = (int) $rows[$table['fields']['timestamp']];
196. $this-> removeNonce ( $nonce );
198. return $lastModified;
199. }
201. // $nonce was not found in the database
202. return false;
203. }
205. /**
206. * Removes the nonce from the nonces table.
207. *
208. * @param string $nonce
209. */
210. protected function removeNonce ( $nonce )
211. {
212. $table = $this-> options ->tableNonces;
214. $query = new ezcQueryDelete ( $this->instance );
215. $e = $query->expr;
216. $query->deleteFrom( $this->instance->quoteIdentifier( $table['name'] ) )
217. ->where(
218. $e->eq( $this->instance->quoteIdentifier( $table['fields']['nonce'] ), $query->bindValue( $nonce ) )
219. );
220. $query = $query->prepare();
221. $query->execute();
222. }
224. /**
225. * Stores an association in the store linked to the OpenID provider URL.
226. *
227. * Returns true always.
228. *
229. * @param string $url The URL of the OpenID provider
230. * @param ezcAuthenticationOpenidAssociation $association The association value to store
231. * @return bool
232. */
233. public function storeAssociation ( $url, $association )
234. {
235. $table = $this-> options ->tableAssociations;
236. $data = serialize ( $association );
238. $query = new ezcQueryInsert ( $this->instance );
240. $query->insertInto( $this->instance->quoteIdentifier( $table['name'] ) )
241. ->set( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
242. ->set( $this->instance->quoteIdentifier( $table['fields']['association'] ), $query->bindValue( $data ) );
244. $stmt = $query->prepare();
245. $stmt->execute();
247. return true;
248. }
250. /**
251. * Returns the unserialized association linked to the OpenID provider URL.
252. *
253. * Returns false if the association could not be retrieved or if it expired.
254. *
255. * @param string $url The URL of the OpenID provider
256. * @return ezcAuthenticationOpenidAssociation
257. */
258. public function getAssociation ( $url )
259. {
260. $table = $this-> options ->tableAssociations;
262. $query = new ezcQuerySelect ( $this->instance );
263. $e = $query->expr;
264. $query->select( '*' )
265. ->from( $this->instance->quoteIdentifier( $table['name'] ) )
266. ->where(
267. $e->eq( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
268. );
270. $query = $query->prepare();
271. $query->execute();
272. $rows = $query->fetchAll();
274. if ( count ( $rows ) > 0 )
275. {
276. $rows = $rows[0];
277. $data = unserialize ( $rows[$table['fields']['association']] );
279. return $data;
280. }
282. // no association was found for $url
283. return false;
284. }
286. /**
287. * Removes the association linked to the OpenID provider URL.
288. *
289. * Returns true always.
290. *
291. * @param string $url The URL of the OpenID provider
292. * @return bool
293. */
294. public function removeAssociation ( $url )
295. {
296. $table = $this-> options ->tableAssociations;
298. $query = new ezcQueryDelete ( $this->instance );
299. $e = $query->expr;
300. $query->deleteFrom( $this->instance->quoteIdentifier( $table['name'] ) )
301. ->where(
302. $e->eq( $this->instance->quoteIdentifier( $table['fields']['url'] ), $query->bindValue( $url ) )
303. );
304. $query = $query->prepare();
305. $query->execute();
307. return true;
308. }
309. }
310. ?>

Documentation generated by phpDocumentor 1.4.3