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

## Source for file htpasswd_filter.php

Documentation is available at htpasswd_filter.php

1. <?php
2. /**
3. * File containing the ezcAuthenticationHtpasswdFilter 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. * Filter to authenticate against an Unix htpasswd file.
30. *
31. * It supports files created with the htpasswd command options
32. * -m (MD5 encryption - different than the PHP md5() function)
33. * -d (CRYPT encryption)
34. * -s (SHA encryption)
35. * -p (plain text)
36. *
37. * The encryption used for the password field in the file will be detected
38. * automatically.
39. *
40. * The password property can be specified as plain text or in encrypted form,
41. * depending on the option 'plain' in the ezcAuthenticationHtpasswdOptions object
42. * used as options.
43. *
44. * Example:
45. * <code>
46. * $credentials = new ezcAuthenticationPasswordCredentials( 'jan.modaal', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e' );
47. * $authentication = new ezcAuthentication( $credentials );
48. * $authentication->session = new ezcAuthenticationSession();
49. * $authentication->addFilter( new ezcAuthenticationHtpasswdFilter( '/etc/htpasswd' ) );
50. * // add other filters if needed
51. * if ( !$authentication->run() )
52. * {
53. * // authentication did not succeed, so inform the user
54. * $status = $authentication->getStatus();
55. * $err = array(
56. * 'ezcAuthenticationHtpasswdFilter' => array(
57. * ezcAuthenticationHtpasswdFilter::STATUS_USERNAME_INCORRECT => 'Incorrect username',
58. * ezcAuthenticationHtpasswdFilter::STATUS_PASSWORD_INCORRECT => 'Incorrect password'
59. * )
60. * );
61. * foreach ( $status as $line )
62. * {
63. * list( $key, $value ) = each( $line );
64. * echo $err[$key][$value] . "\n";
65. * }
66. * }
67. * else
68. * {
69. * // authentication succeeded, so allow the user to see his content
70. * }
71. * </code>
72. *
73. * @property string $file
74. * The path and file name of the htpasswd file to use.
75. *
76. * @package Authentication
77. * @version //autogen//
78. * @mainclass
79. */
80. class ezcAuthenticationHtpasswdFilter extends ezcAuthenticationFilter
81. {
82. /**
83. * Username is not found in the htpasswd file.
84. */
85. const STATUS_USERNAME_INCORRECT = 1;
87. /**
88. * Password is incorrect.
89. */
90. const STATUS_PASSWORD_INCORRECT = 2;
92. /**
93. * Holds the properties of this class.
94. *
95. * @var array(string=>mixed)
96. */
97. private $properties = array();
99. /**
100. * Creates a new object of this class.
101. *
102. * @throws ezcBaseValueException
103. * if the value provided is not correct for the property $file
104. * @throws ezcBaseFileNotFoundException
105. * if $file does not exist
106. * @throws ezcBaseFilePermissionException
107. * if $file cannot be opened for reading
108. * @param string $file The path and file name of the htpasswd file to use
109. * @param ezcAuthenticationHtpasswdOptions $options Options for this class
110. */
111. public function __construct ( $file, ezcAuthenticationHtpasswdOptions $options = null )
112. {
113. $this->file = $file;
114. $this-> options = ( $options === null ) ? new ezcAuthenticationHtpasswdOptions () : $options;
115. }
117. /**
118. * Sets the property $name to $value.
119. *
120. * @throws ezcBasePropertyNotFoundException
121. * if the property $name does not exist
122. * @throws ezcBaseValueException
123. * if $value is not correct for the property $name
124. * @throws ezcBaseFileNotFoundException
125. * if the $value file does not exist
126. * @throws ezcBaseFilePermissionException
127. * if the $value file cannot be opened for reading
128. * @param string $name The name of the property to set
129. * @param mixed $value The new value of the property
130. * @ignore
131. */
132. public function __set( $name, $value )
133. {
134. switch ( $name )
135. {
136. case 'file':
137. if ( ! is_string ( $value ) )
138. {
139. throw new ezcBaseValueException ( $name, $value, 'string' );
140. }
142. if ( ! file_exists ( $value ) )
143. {
144. throw new ezcBaseFileNotFoundException ( $value );
145. }
147. if ( ! is_readable ( $value ) )
148. {
149. throw new ezcBaseFilePermissionException ( $value, ezcBaseFileException ::READ );
150. }
152. $this->properties[$name] = $value;
153. break;
155. default:
156. throw new ezcBasePropertyNotFoundException ( $name );
157. }
158. }
160. /**
161. * Returns the value of the property $name.
162. *
163. * @throws ezcBasePropertyNotFoundException
164. * if the property $name does not exist
165. * @param string $name The name of the property for which to return the value
166. * @return mixed
167. * @ignore
168. */
169. public function __get( $name )
170. {
171. switch ( $name )
172. {
173. case 'file':
174. return $this->properties[$name];
176. default:
177. throw new ezcBasePropertyNotFoundException ( $name );
178. }
179. }
181. /**
182. * Returns true if the property $name is set, otherwise false.
183. *
184. * @param string $name The name of the property to test if it is set
185. * @return bool
186. * @ignore
187. */
188. public function __isset( $name )
189. {
190. switch ( $name )
191. {
192. case 'file':
193. return isset( $this->properties[$name] );
195. default:
196. return false;
197. }
198. }
200. /**
201. * Runs the filter and returns a status code when finished.
202. *
203. * @param ezcAuthenticationPasswordCredentials $credentials Authentication credentials
204. * @return int
205. */
206. public function run ( $credentials )
207. {
208. $fh = fopen ( $this->file, 'r' );
209. $found = false;
210. while ( $line = fgets ( $fh ) )
211. {
212. if ( substr ( $line, 0, strlen ( $credentials->id ) + 1 ) === $credentials->id . ':' )
213. {
214. $found = true;
215. break;
216. }
217. }
218. fclose ( $fh );
219. if ( $found )
220. {
221. $parts = explode ( ':', $line );
222. $hashFromFile = trim ( $parts[1] );
223. if ( substr ( $hashFromFile, 0, 6 ) === '$apr1$' )
224. {
225. $password = ( $this-> options ->plain ) ? ezcAuthenticationMath::apr1( $credentials->password, $hashFromFile ) :
226. '$apr1$' . $credentials->password;
227. }
228. elseif ( substr ( $hashFromFile, 0, 5 ) === '{SHA}' )
229. {
230. $password = ( $this-> options ->plain ) ? '{SHA}' . base64_encode ( pack ( 'H40', sha1 ( $credentials->password ) ) ) :
231. '{SHA}' . $credentials->password;
232. }
233. else
234. {
235. $password = ( $this-> options ->plain ) ? crypt ( $credentials->password, $hashFromFile ) :
236. $credentials->password;
237. }
238. if ( $password === $hashFromFile )
239. {
240. return self::STATUS_OK;
241. }
242. else
243. {
244. return self::STATUS_PASSWORD_INCORRECT;
245. }
246. }
247. return self::STATUS_USERNAME_INCORRECT;
248. }
249. }
250. ?>

Documentation generated by phpDocumentor 1.4.3