Source for file passwords.php

Documentation is available at passwords.php

  1. <?php
  2. /**
  3. * functions for handling authentication
  4. *
  5. * @author    Stuart Prescott
  6. * @copyright  Copyright Stuart Prescott
  7. * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. * @version    $Id$
  9. * @package    Bumblebee
  10. * @subpackage Misc
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. require_once 'inc/bb/configreader.php';
  17.  
  18. /**
  19. * tests if string is a valid username as per the config setting for usernames
  20. *
  21. * @param string $v string to test if it is a valid username
  22. * @return boolean username is valid
  23. */
  24. function is_valid_username($v) {
  25.   $conf = ConfigReader::getInstance();
  26.   $validUserRegexp = $conf->value('auth', 'validUserRegexp');
  27.   return (empty($validUserRegexp) || preg_match($validUserRegexp, $v));
  28. }
  29.  
  30. /**
  31. * check that the supplied password matches the encoded password in the database
  32. *
  33. * @param string $test    unencoded string supplied by the user as a password attempt
  34. * @param string $real    encoded string from the database
  35. * @returns boolean       password matches
  36. */
  37. function check_password($test, $real) {
  38.   //return (makePasswordHash($test, $real) == $real);
  39.   $hash = makePasswordHash($test, $real);
  40.   #print $hash;
  41.   return $hash == $real;
  42. }
  43.  
  44. /**
  45. * encode a password according to the specified hash method (or using the requested
  46. *
  47. * @param string $passwd     unencoded password string
  48. * @param string $salt       salt used to hash the password
  49. * @returns string           hashed password
  50. *
  51. */
  52. function makePasswordHash($passwd, $salt=NULL, $method=NULL) {
  53.   $conf = ConfigReader::getInstance();
  54.   if ($salt === NULL) $salt = makeHashSalt(
  55.             $conf->value('auth', 'LocalPassToken', ($method === NULL) ? 'md5_compat' : $method));
  56.  
  57.   $tmp = explode('$', $salt);
  58.   if (count($tmp) > 3) $saltpart = '$'.$tmp[1].'$'.$tmp[2].'$';
  59.  
  60.   # echo "salt=$salt";
  61.   switch(passwordHashType($salt)) {
  62.     case 'des':
  63.     case 'md5':
  64.       # echo "using crypt()";
  65.       return crypt($passwd, $salt);
  66.     case 'md5_emulated':
  67.       # echo "emulating salted md5";
  68.       return $saltpart.md5($tmp[2].$passwd);  // custom bogo md5
  69.     case 'md5_compat':
  70.       # echo "using bogo MD5";
  71.       return md5($passwd);
  72.     case 'sha1':
  73.       # echo "using simple sha1";
  74.       return $saltpart.base64_encode(pack('H*',sha1($tmp[2].$passwd)));  // custom bogo sha1
  75.     case 'sha1_binary':
  76.       # echo "using binary sha1";
  77.       return $saltpart.base64_encode(sha1($tmp[2].$passwd, true));  // custom bogo sha1
  78.   }
  79. }
  80.  
  81. function makeHashSalt($type='md5') {
  82.   switch($type) {
  83.     case 'md5':
  84.       $len=9;
  85.       $prefix='$1$';
  86.       $suffix='$';
  87.       break;
  88.     case 'md5_emulated':
  89.       $len=9;
  90.       $prefix='$md5$';
  91.       $suffix='$';
  92.       break;
  93.     case 'md5_compat':
  94.       return '';
  95.     case 'des':
  96.       $len=2;
  97.       $prefix='';
  98.       $suffix='';
  99.       break;
  100.     case 'sha1':
  101.       $len=10;
  102.       $prefix='$sha1$';
  103.       $suffix='$';
  104.       break;
  105.     case 'sha1_binary':
  106.       $len=10;
  107.       $prefix='$sha1$';
  108.       $suffix='$';
  109.       break;
  110.   }
  111.   $salt='';
  112.   while (strlen($salt) < $len) $salt .= chr(rand(64,126));
  113.   return $prefix.$salt.$suffix;
  114. }
  115.  
  116. function passwordHashType($pass) {
  117.   $tmp = explode('$', $pass);
  118.   if (count($tmp) < 3) {
  119.     if (strlen($pass) == 32 || strlen($pass) == 0) return 'md5_compat';
  120.     return 'des';
  121.   }
  122.  
  123.   if ($tmp[1] == 1)             return 'md5';
  124.   if ($tmp[1] == 'md5')         return 'md5_emulated';
  125.   if ($tmp[1] == 'sha1')        return 'sha1';
  126.   if ($tmp[1] == 'sha1_binary') return 'sha1_binary';
  127. }
  128.  
  129. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:48 +0000 by phpDocumentor 1.3.0