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.  */
  27.  
  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. {
  81.     /**
  82.      * Username is not found in the htpasswd file.
  83.      */
  84.     const STATUS_USERNAME_INCORRECT = 1;
  85.  
  86.     /**
  87.      * Password is incorrect.
  88.      */
  89.     const STATUS_PASSWORD_INCORRECT = 2;
  90.  
  91.     /**
  92.      * Holds the properties of this class.
  93.      *
  94.      * @var array(string=>mixed) 
  95.      */
  96.     private $properties array();
  97.  
  98.     /**
  99.      * Creates a new object of this class.
  100.      *
  101.      * @throws ezcBaseValueException
  102.      *          if the value provided is not correct for the property $file
  103.      * @throws ezcBaseFileNotFoundException
  104.      *          if $file does not exist
  105.      * @throws ezcBaseFilePermissionException
  106.      *          if $file cannot be opened for reading
  107.      * @param string $file The path and file name of the htpasswd file to use
  108.      * @param ezcAuthenticationHtpasswdOptions $options Options for this class
  109.      */
  110.     public function __construct$fileezcAuthenticationHtpasswdOptions $options null )
  111.     {
  112.         $this->file $file;
  113.         $this->options = $options === null new ezcAuthenticationHtpasswdOptions($options;
  114.     }
  115.  
  116.     /**
  117.      * Sets the property $name to $value.
  118.      *
  119.      * @throws ezcBasePropertyNotFoundException
  120.      *          if the property $name does not exist
  121.      * @throws ezcBaseValueException
  122.      *          if $value is not correct for the property $name
  123.      * @throws ezcBaseFileNotFoundException
  124.      *          if the $value file does not exist
  125.      * @throws ezcBaseFilePermissionException
  126.      *          if the $value file cannot be opened for reading
  127.      * @param string $name The name of the property to set
  128.      * @param mixed $value The new value of the property
  129.      * @ignore
  130.      */
  131.     public function __set$name$value )
  132.     {
  133.         switch $name )
  134.         {
  135.             case 'file':
  136.                 if !is_string$value ) )
  137.                 {
  138.                     throw new ezcBaseValueException$name$value'string' );
  139.                 }
  140.  
  141.                 if !file_exists$value ) )
  142.                 {
  143.                     throw new ezcBaseFileNotFoundException$value );
  144.                 }
  145.  
  146.                 if !is_readable$value ) )
  147.                 {
  148.                     throw new ezcBaseFilePermissionException$valueezcBaseFileException::READ );
  149.                 }
  150.  
  151.                 $this->properties[$name$value;
  152.                 break;
  153.  
  154.             default:
  155.                 throw new ezcBasePropertyNotFoundException$name );
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Returns the value of the property $name.
  161.      *
  162.      * @throws ezcBasePropertyNotFoundException
  163.      *          if the property $name does not exist
  164.      * @param string $name The name of the property for which to return the value
  165.      * @return mixed 
  166.      * @ignore
  167.      */
  168.     public function __get$name )
  169.     {
  170.         switch $name )
  171.         {
  172.             case 'file':
  173.                 return $this->properties[$name];
  174.  
  175.             default:
  176.                 throw new ezcBasePropertyNotFoundException$name );
  177.         }
  178.     }
  179.  
  180.     /**
  181.      * Returns true if the property $name is set, otherwise false.
  182.      *
  183.      * @param string $name The name of the property to test if it is set
  184.      * @return bool 
  185.      * @ignore
  186.      */
  187.     public function __isset$name )
  188.     {
  189.         switch $name )
  190.         {
  191.             case 'file':
  192.                 return isset$this->properties[$name);
  193.  
  194.             default:
  195.                 return false;
  196.         }
  197.     }
  198.  
  199.     /**
  200.      * Runs the filter and returns a status code when finished.
  201.      *
  202.      * @param ezcAuthenticationPasswordCredentials $credentials Authentication credentials
  203.      * @return int 
  204.      */
  205.     public function run$credentials )
  206.     {
  207.         $fh fopen$this->file'r' );
  208.         $found false;
  209.         while $line fgets$fh ) )
  210.         {
  211.             if substr$line0strlen$credentials->id === $credentials->id ':' )
  212.             {
  213.                 $found true;
  214.                 break;
  215.             }
  216.         }
  217.         fclose$fh );
  218.         if $found )
  219.         {
  220.             $parts explode':'$line );
  221.             $hashFromFile trim$parts[1);
  222.             if substr$hashFromFile0=== '$apr1$' )
  223.             {
  224.                 $password $this->options->plain ezcAuthenticationMath::apr1$credentials->password$hashFromFile :
  225.                                                         '$apr1$' $credentials->password;
  226.             }
  227.             elseif substr$hashFromFile0=== '{SHA}' )
  228.             {
  229.                 $password $this->options->plain '{SHA}' base64_encodepack'H40'sha1$credentials->password ) ) ) :
  230.                                                         '{SHA}' $credentials->password;
  231.             }
  232.             else
  233.             {
  234.                 $password $this->options->plain crypt$credentials->password$hashFromFile :
  235.                                                         $credentials->password;
  236.             }
  237.             if $password === $hashFromFile )
  238.             {
  239.                 return self::STATUS_OK;
  240.             }
  241.             else
  242.             {
  243.                 return self::STATUS_PASSWORD_INCORRECT;
  244.             }
  245.         }
  246.         return self::STATUS_USERNAME_INCORRECT;
  247.     }
  248. }
  249. ?>
Documentation generated by phpDocumentor 1.4.3