00001 <?php 00002 /*************************************************************************** 00003 * Copyright (C) 2004-2007 by Dmitry E. Demidov * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU Lesser General Public License as * 00007 * published by the Free Software Foundation; either version 3 of the * 00008 * License, or (at your option) any later version. * 00009 * * 00010 ***************************************************************************/ 00011 /* $Id: CodeGenerator.class.php 4687 2007-12-09 18:57:18Z voxus $ */ 00012 00016 final class CodeGenerator 00017 { 00018 private $length = null; 00019 00020 private $lowerAllowed = true; 00021 private $upperAllowed = true; 00022 private $numbersAllowed = true; 00023 private $similarAllowed = true; 00024 00025 static private $similarSymbols = array('0', 'o', '1', 'l'); 00026 00027 public function generate() 00028 { 00029 mt_srand(microtime(true) * 1000000); 00030 00031 $code = null; 00032 00033 for ($i = 0; $i < $this->length; ++$i) 00034 $code .= $this->generateOneSymbol(); 00035 00036 return $code; 00037 } 00038 00042 public function setLength($length) 00043 { 00044 $this->length = $length; 00045 00046 return $this; 00047 } 00048 00052 public function setLowerAllowed($lowerAllowed = true) 00053 { 00054 $this->lowerAllowed = $lowerAllowed; 00055 00056 return $this; 00057 } 00058 00062 public function setUpperAllowed($upperAllowed = true) 00063 { 00064 $this->upperAllowed = $upperAllowed; 00065 00066 return $this; 00067 } 00068 00072 public function setSimilarAllowed($similarAllowed = true) 00073 { 00074 $this->similarAllowed = $similarAllowed; 00075 00076 return $this; 00077 } 00078 00082 public function setNumbersAllowed($numbersAllowed = true) 00083 { 00084 $this->numbersAllowed = $numbersAllowed; 00085 00086 return $this; 00087 } 00088 00092 public function setCharactersAllowed($charactersAllowed = true) 00093 { 00094 $this->setLowerAllowed($charactersAllowed); 00095 $this->setUpperAllowed($charactersAllowed); 00096 00097 return $this; 00098 } 00099 00100 private function generateOneSymbol() 00101 { 00102 $variants = array(); 00103 00104 Assert::isTrue( 00105 $this->lowerAllowed 00106 || $this->upperAllowed 00107 || $this->numbersAllowed, 00108 00109 'what exactly should i generate?' 00110 ); 00111 00112 do { 00113 if ($this->lowerAllowed) 00114 $variants[] = $this->randomChar(); 00115 00116 if ($this->upperAllowed) 00117 $variants[] = strtoupper($this->randomChar()); 00118 00119 if ($this->numbersAllowed) 00120 $variants[] = $this->randomNumber(); 00121 00122 shuffle($variants); 00123 00124 $symbol = $variants[0]; 00125 00126 } while ( 00127 (!$this->similarAllowed) 00128 && (in_array($symbol, self::$similarSymbols)) 00129 ); 00130 00131 return $symbol; 00132 } 00133 00134 private function randomNumber() 00135 { 00136 return mt_rand(0,9); 00137 } 00138 00139 private function randomChar() 00140 { 00141 return chr(mt_rand(ord('a'), ord('z'))); 00142 } 00143 } 00144 ?>