home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Password.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  14.1 KB  |  537 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Class to create passwords
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to license@php.net so we can mail you a copy immediately.
  14.  *
  15.  * @category   Text
  16.  * @package    Text_Password
  17.  * @author     Martin Jansen <mj@php.net>
  18.  * @author     Olivier Vanhoucke <olivier@php.net>
  19.  * @copyright  2004-2005 Martin Jansen, Olivier Vanhoucke
  20.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  21.  * @version    CVS: $Id: Password.php,v 1.16 2005/08/21 19:47:53 mj Exp $
  22.  * @link       http://pear.php.net/package/Text_Password
  23.  */
  24.  
  25. /**
  26.  * Number of possible characters in the password
  27.  */
  28. $_Text_Password_NumberOfPossibleCharacters = 0;
  29.  
  30. /**
  31.  * Main class for the Text_Password package
  32.  *
  33.  * @category   Text
  34.  * @package    Text_Password
  35.  * @author     Martin Jansen <mj@php.net>
  36.  * @author     Olivier Vanhoucke <olivier@php.net>
  37.  * @copyright  2004-2005 Martin Jansen, Olivier Vanhoucke
  38.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  39.  * @version    Release: @package_version@
  40.  * @link       http://pear.php.net/package/Text_Password
  41.  */
  42. class Text_Password {
  43.  
  44.     /**
  45.      * Create a single password.
  46.      *
  47.      * @access public
  48.      * @param  integer Length of the password.
  49.      * @param  string  Type of password (pronounceable, unpronounceable)
  50.      * @param  string  Character which could be use in the
  51.      *                 unpronounceable password ex : 'A,B,C,D,E,F,G'
  52.      *                 or numeric, alphabetical or alphanumeric.
  53.      * @return string  Returns the generated password.
  54.      */
  55.     function create($length = 10, $type = 'pronounceable', $chars = '')
  56.     {
  57.         switch ($type) {
  58.         case 'unpronounceable' :
  59.             return Text_Password::_createUnpronounceable($length, $chars);
  60.  
  61.         case 'pronounceable' :
  62.         default :
  63.             return Text_Password::_createPronounceable($length);
  64.         }
  65.     }
  66.  
  67.     /**
  68.      * Create multiple, different passwords
  69.      *
  70.      * Method to create a list of different passwords which are
  71.      * all different.
  72.      *
  73.      * @access public
  74.      * @param  integer Number of different password
  75.      * @param  integer Length of the password
  76.      * @param  string  Type of password (pronounceable, unpronounceable)
  77.      * @param  string  Character which could be use in the
  78.      *                 unpronounceable password ex : 'A,B,C,D,E,F,G'
  79.      *                 or numeric, alphabetical or alphanumeric.
  80.      * @return array   Array containing the passwords
  81.      */
  82.     function createMultiple($number, $length = 10, $type = 'pronounceable', $chars = '')
  83.     {
  84.         $passwords = array();
  85.  
  86.         while ($number > 0) {
  87.             while (true) {
  88.                 $password = Text_Password::create($length, $type, $chars);
  89.                 if (!in_array($password, $passwords)) {
  90.                     $passwords[] = $password;
  91.                     break;
  92.                 }
  93.             }
  94.             $number--;
  95.         }
  96.         return $passwords;
  97.     }
  98.  
  99.     /**
  100.      * Create password from login
  101.      *
  102.      * Method to create password from login
  103.      *
  104.      * @access public
  105.      * @param  string  Login
  106.      * @param  string  Type
  107.      * @param  integer Key
  108.      * @return string
  109.      */
  110.     function createFromLogin($login, $type, $key = 0)
  111.     {
  112.         switch ($type) {
  113.         case 'reverse':
  114.             return strrev($login);
  115.  
  116.         case 'shuffle':
  117.             return Text_Password::_shuffle($login);
  118.  
  119.         case 'xor':
  120.             return Text_Password::_xor($login, $key);
  121.  
  122.         case 'rot13':
  123.             return str_rot13($login);
  124.  
  125.         case 'rotx':
  126.             return Text_Password::_rotx($login, $key);
  127.  
  128.         case 'rotx++':
  129.             return Text_Password::_rotxpp($login, $key);
  130.  
  131.         case 'rotx--':
  132.             return Text_Password::_rotxmm($login, $key);
  133.  
  134.         case 'ascii_rotx':
  135.             return Text_Password::_asciiRotx($login, $key);
  136.  
  137.         case 'ascii_rotx++':
  138.             return Text_Password::_asciiRotxpp($login, $key);
  139.  
  140.         case 'ascii_rotx--':
  141.             return Text_Password::_asciiRotxmm($login, $key);
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Create multiple, different passwords from an array of login
  147.      *
  148.      * Method to create a list of different password from login
  149.      *
  150.      * @access public
  151.      * @param  array   Login
  152.      * @param  string  Type
  153.      * @param  integer Key
  154.      * @return array   Array containing the passwords
  155.      */
  156.     function createMultipleFromLogin($login, $type, $key = 0)
  157.     {
  158.         $passwords = array();
  159.         $number    = count($login);
  160.         $save      = $number;
  161.  
  162.         while ($number > 0) {
  163.             while (true) {
  164.                 $password = Text_Password::createFromLogin($login[$save - $number], $type, $key);
  165.                 if (!in_array($password, $passwords)) {
  166.                     $passwords[] = $password;
  167.                     break;
  168.                 }
  169.             }
  170.             $number--;
  171.         }
  172.         return $passwords;
  173.     }
  174.  
  175.     /**
  176.      * Helper method to create password
  177.      *
  178.      * Method to create a password from a login
  179.      *
  180.      * @access private
  181.      * @param  string  Login
  182.      * @param  integer Key
  183.      * @return string
  184.      */
  185.     function _xor($login, $key)
  186.     {
  187.         $tmp = '';
  188.  
  189.         for ($i = 0; $i < strlen($login); $i++) {
  190.             $next = ord($login{$i}) ^ $key;
  191.             if ($next > 255) {
  192.                 $next -= 255;
  193.             } elseif ($next < 0) {
  194.                 $next += 255;
  195.             }
  196.             $tmp .= chr($next);
  197.         }
  198.  
  199.         return $tmp;
  200.     }
  201.  
  202.     /**
  203.      * Helper method to create password
  204.      *
  205.      * Method to create a password from a login
  206.      * lowercase only
  207.      *
  208.      * @access private
  209.      * @param  string  Login
  210.      * @param  integer Key
  211.      * @return string
  212.      */
  213.     function _rotx($login, $key)
  214.     {
  215.         $tmp = '';
  216.         $login = strtolower($login);
  217.  
  218.         for ($i = 0; $i < strlen($login); $i++) {
  219.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  220.                 $next = ord($login{$i}) + $key;
  221.                 if ($next > 122) {
  222.                     $next -= 26;
  223.                 } elseif ($next < 97) {
  224.                     $next += 26;
  225.                 }
  226.                 $tmp .= chr($next);
  227.             } else {
  228.                 $tmp .= $login{$i};
  229.             }
  230.         }
  231.  
  232.         return $tmp;
  233.     }
  234.  
  235.     /**
  236.      * Helper method to create password
  237.      *
  238.      * Method to create a password from a login
  239.      * lowercase only
  240.      *
  241.      * @access private
  242.      * @param  string  Login
  243.      * @param  integer Key
  244.      * @return string
  245.      */
  246.     function _rotxpp($login, $key)
  247.     {
  248.         $tmp = '';
  249.         $login = strtolower($login);
  250.  
  251.         for ($i = 0; $i < strlen($login); $i++, $key++) {
  252.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  253.                 $next = ord($login{$i}) + $key;
  254.                 if ($next > 122) {
  255.                     $next -= 26;
  256.                 } elseif ($next < 97) {
  257.                     $next += 26;
  258.                 }
  259.                 $tmp .= chr($next);
  260.             } else {
  261.                 $tmp .= $login{$i};
  262.             }
  263.         }
  264.  
  265.         return $tmp;
  266.     }
  267.  
  268.     /**
  269.      * Helper method to create password
  270.      *
  271.      * Method to create a password from a login
  272.      * lowercase only
  273.      *
  274.      * @access private
  275.      * @param  string  Login
  276.      * @param  integer Key
  277.      * @return string
  278.      */
  279.     function _rotxmm($login, $key)
  280.     {
  281.         $tmp = '';
  282.         $login = strtolower($login);
  283.  
  284.         for ($i = 0; $i < strlen($login); $i++, $key--) {
  285.             if ((ord($login{$i}) >= 97) && (ord($login{$i}) <= 122)) { // 65, 90 for uppercase
  286.                 $next = ord($login{$i}) + $key;
  287.                 if ($next > 122) {
  288.                     $next -= 26;
  289.                 } elseif ($next < 97) {
  290.                     $next += 26;
  291.                 }
  292.                 $tmp .= chr($next);
  293.             } else {
  294.                 $tmp .= $login{$i};
  295.             }
  296.         }
  297.  
  298.         return $tmp;
  299.     }
  300.  
  301.     /**
  302.      * Helper method to create password
  303.      *
  304.      * Method to create a password from a login
  305.      *
  306.      * @access private
  307.      * @param  string  Login
  308.      * @param  integer Key
  309.      * @return string
  310.      */
  311.     function _asciiRotx($login, $key)
  312.     {
  313.         $tmp = '';
  314.  
  315.         for ($i = 0; $i < strlen($login); $i++) {
  316.             $next = ord($login{$i}) + $key;
  317.             if ($next > 255) {
  318.                 $next -= 255;
  319.             } elseif ($next < 0) {
  320.                 $next += 255;
  321.             }
  322.             switch ($next) { // delete white space
  323.             case 0x09:
  324.             case 0x20:
  325.             case 0x0A:
  326.             case 0x0D:
  327.                 $next++;
  328.             }
  329.             $tmp .= chr($next);
  330.         }
  331.  
  332.         return $tmp;
  333.     }
  334.  
  335.     /**
  336.      * Helper method to create password
  337.      *
  338.      * Method to create a password from a login
  339.      *
  340.      * @access private
  341.      * @param  string  Login
  342.      * @param  integer Key
  343.      * @return string
  344.      */
  345.     function _asciiRotxpp($login, $key)
  346.     {
  347.         $tmp = '';
  348.  
  349.         for ($i = 0; $i < strlen($login); $i++, $key++) {
  350.             $next = ord($login{$i}) + $key;
  351.             if ($next > 255) {
  352.                 $next -= 255;
  353.             } elseif ($next < 0) {
  354.                 $next += 255;
  355.             }
  356.             switch ($next) { // delete white space
  357.             case 0x09:
  358.             case 0x20:
  359.             case 0x0A:
  360.             case 0x0D:
  361.                 $next++;
  362.             }
  363.             $tmp .= chr($next);
  364.         }
  365.  
  366.         return $tmp;
  367.     }
  368.  
  369.     /**
  370.      * Helper method to create password
  371.      *
  372.      * Method to create a password from a login
  373.      *
  374.      * @access private
  375.      * @param  string  Login
  376.      * @param  integer Key
  377.      * @return string
  378.      */
  379.     function _asciiRotxmm($login, $key)
  380.     {
  381.         $tmp = '';
  382.  
  383.         for ($i = 0; $i < strlen($login); $i++, $key--) {
  384.             $next = ord($login{$i}) + $key;
  385.             if ($next > 255) {
  386.                 $next -= 255;
  387.             } elseif ($next < 0) {
  388.                 $next += 255;
  389.             }
  390.             switch ($next) { // delete white space
  391.             case 0x09:
  392.             case 0x20:
  393.             case 0x0A:
  394.             case 0x0D:
  395.                 $next++;
  396.             }
  397.             $tmp .= chr($next);
  398.         }
  399.  
  400.         return $tmp;
  401.     }
  402.  
  403.     /**
  404.      * Helper method to create password
  405.      *
  406.      * Method to create a password from a login
  407.      *
  408.      * @access private
  409.      * @param  string  Login
  410.      * @return string
  411.      */
  412.     function _shuffle($login)
  413.     {
  414.         $tmp = array();
  415.  
  416.         for ($i = 0; $i < strlen($login); $i++) {
  417.             $tmp[] = $login{$i};
  418.         }
  419.  
  420.         shuffle($tmp);
  421.  
  422.         return implode($tmp, '');
  423.     }
  424.  
  425.     /**
  426.      * Create pronounceable password
  427.      *
  428.      * This method creates a string that consists of
  429.      * vowels and consonats.
  430.      *
  431.      * @access private
  432.      * @param  integer Length of the password
  433.      * @return string  Returns the password
  434.      */
  435.     function _createPronounceable($length)
  436.     {
  437.  
  438.         global $_Text_Password_NumberOfPossibleCharacters;
  439.         $retVal = '';
  440.  
  441.         /**
  442.          * List of vowels and vowel sounds
  443.          */
  444.         $v = array('a', 'e', 'i', 'o', 'u', 'ae', 'ou', 'io',
  445.                    'ea', 'ou', 'ia', 'ai'
  446.                    );
  447.  
  448.         /**
  449.          * List of consonants and consonant sounds
  450.          */
  451.         $c = array('b', 'c', 'd', 'g', 'h', 'j', 'k', 'l', 'm',
  452.                    'n', 'p', 'r', 's', 't', 'u', 'v', 'w',
  453.                    'tr', 'cr', 'fr', 'dr', 'wr', 'pr', 'th',
  454.                    'ch', 'ph', 'st', 'sl', 'cl'
  455.                    );
  456.  
  457.         $v_count = 12;
  458.         $c_count = 29;
  459.  
  460.         $_Text_Password_NumberOfPossibleCharacters = $v_count + $c_count;
  461.  
  462.         for ($i = 0; $i < $length; $i++) {
  463.             $retVal .= $c[mt_rand(0, $c_count-1)] . $v[mt_rand(0, $v_count-1)];
  464.         }
  465.  
  466.         return substr($retVal, 0, $length);
  467.     }
  468.  
  469.     /**
  470.      * Create unpronounceable password
  471.      *
  472.      * This method creates a random unpronounceable password
  473.      *
  474.      * @access private
  475.      * @param  integer Length of the password
  476.      * @param  string  Character which could be use in the
  477.      *                 unpronounceable password ex : 'ABCDEFG'
  478.      *                 or numeric, alphabetical or alphanumeric.
  479.      * @return string  Returns the password
  480.      */
  481.     function _createUnpronounceable($length, $chars)
  482.     {
  483.         global $_Text_Password_NumberOfPossibleCharacters;
  484.  
  485.         $password = '';
  486.  
  487.         /**
  488.          * List of character which could be use in the password
  489.          */
  490.          switch($chars) {
  491.  
  492.          case 'alphanumeric':
  493.              $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  494.              $_Text_Password_NumberOfPossibleCharacters = 62;
  495.              break;
  496.  
  497.          case 'alphabetical':
  498.              $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  499.              $_Text_Password_NumberOfPossibleCharacters = 52;
  500.              break;
  501.  
  502.          case 'numeric':
  503.              $chars = '0123456789';
  504.              $_Text_Password_NumberOfPossibleCharacters = 10;
  505.              break;
  506.  
  507.          case '':
  508.              $chars = '_#@%&ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  509.              $_Text_Password_NumberOfPossibleCharacters = 67;
  510.              break;
  511.  
  512.          default:
  513.              /**
  514.               * Some characters shouldn't be used
  515.               */
  516.              $chars = trim($chars);
  517.              $chars = str_replace(array('+', '|', '$', '^', '/', '\\', ','), '', $chars);
  518.  
  519.              $_Text_Password_NumberOfPossibleCharacters = strlen($chars);
  520.          }
  521.  
  522.          /**
  523.           * Generate password
  524.           */
  525.          for ($i = 0; $i < $length; $i++) {
  526.              $num = mt_rand(0, $_Text_Password_NumberOfPossibleCharacters - 1);
  527.              $password .= $chars{$num};
  528.          }
  529.  
  530.          /**
  531.           * Return password
  532.           */
  533.          return $password;
  534.     }
  535. }
  536. ?>
  537.