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

## Source for file openid_file_store.php

Documentation is available at openid_file_store.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationOpenidFileStore 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 Authentication
25. * @version //autogen//
26. */
28. /**
29. * Class providing file storage for OpenID authentication.
30. *
31. * Example of use:
32. * <code>
33. * // create an OpenID options object
34. * $options = new ezcAuthenticationOpenidOptions();
35. * $options->mode = ezcAuthenticationOpenidFilter::MODE_SMART;
36. *
37. * // define a file store
38. * $options->store = new ezcAuthenticationOpenidFileStore( '/tmp/store' );
39. *
40. * // create an OpenID filter based on the options object
41. * $filter = new ezcAuthenticationOpenidFilter( $options );
42. * </code>
43. *
44. * @property string $path
45. * The path where the files will be kept. It must exist and it must
46. * be writable.
47. *
48. * @package Authentication
49. * @version //autogen//
50. */
51. class ezcAuthenticationOpenidFileStore extends ezcAuthenticationOpenidStore
52. {
53. /**
54. * Holds the properties of this class.
55. *
56. * @var array(string=>mixed)
57. */
58. private $properties = array();
60. /**
61. * Creates a new object of this class.
62. *
63. * @throws ezcBaseFileNotFoundException
64. * if $path does not exist
65. * @throws ezcBaseFilePermissionException
66. * if $path cannot be opened for reading and writing
67. * @param string $path The path where to save the nonces
68. * @param ezcAuthenticationOpenidFileStoreOptions $options Options for this class
69. */
70. public function __construct ( $path, ezcAuthenticationOpenidFileStoreOptions $options = null )
71. {
72. $this->path = $path;
73. $this-> options = ( $options === null ) ? new ezcAuthenticationOpenidFileStoreOptions () : $options;
74. }
76. /**
77. * Sets the property $name to $value.
78. *
79. * @throws ezcBasePropertyNotFoundException
80. * if the property $name does not exist
81. * @throws ezcBaseValueException
82. * if $value is not correct for the property $name
83. * @throws ezcBaseFileNotFoundException
84. * if the $value file does not exist
85. * @throws ezcBaseFilePermissionException
86. * if the $value file cannot be opened for reading and writing
87. * @param string $name The name of the property to set
88. * @param mixed $value The new value of the property
89. * @ignore
90. */
91. public function __set( $name, $value )
92. {
93. switch ( $name )
94. {
95. case 'path':
96. if ( ! is_string ( $value ) )
97. {
98. throw new ezcBaseValueException ( $name, $value, 'string' );
99. }
101. if ( ! is_dir ( $value ) )
102. {
103. throw new ezcBaseFileNotFoundException ( $value );
104. }
106. if ( ! is_readable ( $value ) )
107. {
108. throw new ezcBaseFilePermissionException ( $value, ezcBaseFileException ::READ );
109. }
111. if ( ! is_writable ( $value ) )
112. {
113. throw new ezcBaseFilePermissionException ( $value, ezcBaseFileException ::WRITE );
114. }
116. $this->properties[$name] = $value;
117. break;
119. default:
120. throw new ezcBasePropertyNotFoundException ( $name );
121. }
122. }
124. /**
125. * Returns the value of the property $name.
126. *
127. * @throws ezcBasePropertyNotFoundException
128. * if the property $name does not exist
129. * @param string $name The name of the property for which to return the value
130. * @return mixed
131. * @ignore
132. */
133. public function __get( $name )
134. {
135. switch ( $name )
136. {
137. case 'path':
138. return $this->properties[$name];
140. default:
141. throw new ezcBasePropertyNotFoundException ( $name );
142. }
143. }
145. /**
146. * Returns true if the property $name is set, otherwise false.
147. *
148. * @param string $name The name of the property to test if it is set
149. * @return bool
150. * @ignore
151. */
152. public function __isset( $name )
153. {
154. switch ( $name )
155. {
156. case 'path':
157. return isset( $this->properties[$name] );
159. default:
160. return false;
161. }
162. }
164. /**
165. * Stores the nonce in the store.
166. *
167. * Returns true if the nonce was stored successfully, and false otherwise.
168. *
169. * @throws ezcBaseFilePermissionException
170. * if the nonce cannot be written in the store
171. * @param string $nonce The nonce value to store
172. * @return bool
173. */
174. public function storeNonce ( $nonce )
175. {
176. $file = $this->path . DIRECTORY_SEPARATOR . $nonce;
178. // suppress warnings caused by fopen() if $file could not be opened
179. $fh = @ fopen ( $file, 'w' );
181. if ( $fh === false )
182. {
183. throw new ezcBaseFilePermissionException ( $file, ezcBaseFileException ::WRITE );
184. }
186. fclose ( $fh );
188. return true;
189. }
191. /**
192. * Checks if the nonce exists and afterwards deletes it.
193. *
194. * Returns the timestamp of the nonce if it exists, and false otherwise.
195. *
196. * @param string $nonce The nonce value to check and delete
197. * @return bool|int
198. */
199. public function useNonce ( $nonce )
200. {
201. $file = $this->path . DIRECTORY_SEPARATOR . $nonce;
203. if ( ! file_exists ( $file ) )
204. {
205. return false;
206. }
208. $lastModified = filemtime ( $file );
209. unlink ( $file );
211. return $lastModified;
212. }
214. /**
215. * Stores an association in the store linked to the OpenID provider URL.
216. *
217. * Returns true if the association was stored successfully, and false
218. * otherwise.
219. *
220. * @throws ezcBaseFilePermissionException
221. * if the nonce cannot be written in the store
222. * @param string $url The URL of the OpenID provider
223. * @param ezcAuthenticationOpenidAssociation $association The association value to store
224. * @return bool
225. */
226. public function storeAssociation ( $url, $association )
227. {
228. $file = $this->path . DIRECTORY_SEPARATOR . $this-> convertToFilename ( $url );
230. // suppress warnings caused by fopen() if $file could not be opened
231. $fh = @ fopen ( $file, 'w' );
233. if ( $fh === false )
234. {
235. throw new ezcBaseFilePermissionException ( $file, ezcBaseFileException ::WRITE );
236. }
238. $data = serialize ( $association );
239. fwrite ( $fh, $data );
240. fclose ( $fh );
242. return true;
243. }
245. /**
246. * Returns the unserialized association linked to the OpenID provider URL.
247. *
248. * Returns false if the association could not be retrieved or if it expired.
249. *
250. * @param string $url The URL of the OpenID provider
251. * @return ezcAuthenticationOpenidAssociation
252. */
253. public function getAssociation ( $url )
254. {
255. $file = $this->path . DIRECTORY_SEPARATOR . $this-> convertToFilename ( $url );
257. if ( ! file_exists ( $file ) )
258. {
259. return false;
260. }
262. $data = unserialize ( file_get_contents ( $file ) );
263. return $data;
264. }
266. /**
267. * Removes the association linked to the OpenID provider URL.
268. *
269. * Returns true if the association could be removed, and false otherwise.
270. *
271. * @param string $url The URL of the OpenID provider
272. * @return bool
273. */
274. public function removeAssociation ( $url )
275. {
276. $file = $this->path . DIRECTORY_SEPARATOR . $this-> convertToFilename ( $url );
278. if ( ! file_exists ( $file ) )
279. {
280. return false;
281. }
283. unlink ( $file );
284. return true;
285. }
287. /**
288. * Creates a valid filename from the provided string.
289. *
290. * @param string $value A string which needs to be used as a valid filename
291. * @return string
292. */
293. protected function convertToFilename ( $value )
294. {
295. $result = base64_encode ( $value );
296. $result = str_replace ( '/', '_', $result );
297. $result = str_replace ( '+', '-', $result );
298. return $result;
299. }
300. }
301. ?>

Documentation generated by phpDocumentor 1.4.3