Apache Zeta Components Manual :: File Source for htpasswd_filter.php
Source for file htpasswd_filter.php
Documentation is available at htpasswd_filter.php
* File containing the ezcAuthenticationHtpasswdFilter class.
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @package Authentication
* Filter to authenticate against an Unix htpasswd file.
* It supports files created with the htpasswd command options
* -m (MD5 encryption - different than the PHP md5() function)
* The encryption used for the password field in the file will be detected
* The password property can be specified as plain text or in encrypted form,
* depending on the option 'plain' in the ezcAuthenticationHtpasswdOptions object
* $credentials = new ezcAuthenticationPasswordCredentials( 'jan.modaal', 'b1b3773a05c0ed0176787a4f1574ff0075f7521e' );
* $authentication = new ezcAuthentication( $credentials );
* $authentication->session = new ezcAuthenticationSession();
* $authentication->addFilter( new ezcAuthenticationHtpasswdFilter( '/etc/htpasswd' ) );
* // add other filters if needed
* if ( !$authentication->run() )
* // authentication did not succeed, so inform the user
* $status = $authentication->getStatus();
* 'ezcAuthenticationHtpasswdFilter' => array(
* ezcAuthenticationHtpasswdFilter::STATUS_USERNAME_INCORRECT => 'Incorrect username',
* ezcAuthenticationHtpasswdFilter::STATUS_PASSWORD_INCORRECT => 'Incorrect password'
* foreach ( $status as $line )
* list( $key, $value ) = each( $line );
* echo $err[$key][$value] . "\n";
* // authentication succeeded, so allow the user to see his content
* The path and file name of the htpasswd file to use.
* @package Authentication
* Username is not found in the htpasswd file.
* Holds the properties of this class.
* @var array(string=>mixed)
private $properties =
array();
* Creates a new object of this class.
* @throws ezcBaseValueException
* if the value provided is not correct for the property $file
* @throws ezcBaseFileNotFoundException
* if $file does not exist
* @throws ezcBaseFilePermissionException
* if $file cannot be opened for reading
* @param string $file The path and file name of the htpasswd file to use
* @param ezcAuthenticationHtpasswdOptions $options Options for this class
public function __construct( $file, ezcAuthenticationHtpasswdOptions $options =
null )
* Sets the property $name to $value.
* @throws ezcBasePropertyNotFoundException
* if the property $name does not exist
* @throws ezcBaseValueException
* if $value is not correct for the property $name
* @throws ezcBaseFileNotFoundException
* if the $value file does not exist
* @throws ezcBaseFilePermissionException
* if the $value file cannot be opened for reading
* @param string $name The name of the property to set
* @param mixed $value The new value of the property
public function __set( $name, $value )
$this->properties[$name] =
$value;
* Returns the value of the property $name.
* @throws ezcBasePropertyNotFoundException
* if the property $name does not exist
* @param string $name The name of the property for which to return the value
public function __get( $name )
return $this->properties[$name];
* Returns true if the property $name is set, otherwise false.
* @param string $name The name of the property to test if it is set
public function __isset( $name )
return isset
( $this->properties[$name] );
* Runs the filter and returns a status code when finished.
* @param ezcAuthenticationPasswordCredentials $credentials Authentication credentials
public function run( $credentials )
$fh =
fopen( $this->file, 'r' );
while ( $line =
fgets( $fh ) )
if ( substr( $line, 0, strlen( $credentials->id ) +
1 ) ===
$credentials->id .
':' )
$hashFromFile =
trim( $parts[1] );
if ( substr( $hashFromFile, 0, 6 ) ===
'$apr1$' )
$password =
( $this->options->plain ) ?
ezcAuthenticationMath::apr1( $credentials->password, $hashFromFile ) :
'$apr1$' .
$credentials->password;
elseif ( substr( $hashFromFile, 0, 5 ) ===
'{SHA}' )
'{SHA}' .
$credentials->password;
$password =
( $this->options->plain ) ?
crypt( $credentials->password, $hashFromFile ) :
if ( $password ===
$hashFromFile )
return self::STATUS_PASSWORD_INCORRECT;
return self::STATUS_USERNAME_INCORRECT;