class_RandomNumberGenerator.php
Go to the documentation of this file.00001 <?php
00024 class RandomNumberGenerator extends BaseFrameworkSystem {
00028 private $prime = 0;
00029
00033 private $extraNumber = 0;
00034
00038 private $extraSalt = "";
00039
00043 private $fixedSalt = "";
00044
00048 private $rndStrLen = 0;
00049
00056 protected function __construct ($className = __CLASS__) {
00057
00058 parent::__construct($className);
00059
00060
00061 $this->removeNumberFormaters();
00062 $this->removeSystemArray();
00063 }
00064
00071 public final static function createRandomNumberGenerator (FrameworkInterface $extraInstance = null) {
00072
00073 $rngInstance = new RandomNumberGenerator();
00074
00075
00076 $rngInstance->initRng($extraInstance);
00077
00078
00079 return $rngInstance;
00080 }
00081
00089 protected function initRng ($extraInstance) {
00090
00091 $this->prime = $this->getConfigInstance()->readConfig('math_prime');
00092
00093
00094
00095 $this->extraNumber = ($this->prime * $this->prime / (pi() ^ 2));
00096
00097
00098 mt_srand((double) sqrt(microtime() * 100000000 * $this->extraNumber));
00099
00100
00101 $serverIp = "cluster";
00102
00103
00104 if ($this->getConfigInstance()->readConfig('is_single_server') === "Y") {
00105
00106 $serverIp = getenv('SERVER_ADDR');
00107 }
00108
00109
00110 if ($extraInstance instanceof FrameworkInterface) {
00111
00112 $this->fixedSalt = sha1($serverIp . ":" . $extraInstance->__toString() . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
00113 } else {
00114
00115 $this->fixedSalt = sha1($serverIp . ":" . serialize($this->getDatabaseInstance()->getConnectionData()));
00116 }
00117
00118
00119 $this->extraSalt = sha1($this->fixedSalt . ":" . getenv('SERVER_SOFTWARE') . ":" . $this->getConfigInstance()->readConfig('date_key') . $this->getConfigInstance()->readConfig('base_url'));
00120
00121
00122 $this->rndStrLen = $this->getConfigInstance()->readConfig('rnd_str_length');
00123 }
00124
00131 public function randomString ($length = -1) {
00132
00133 if ($length < 1) $length = $this->rndStrLen;
00134
00135
00136 $randomString = "";
00137
00138
00139 for ($idx = 0; $idx < $length; $idx++) {
00140
00141 $randomString .= chr($this->randomNumber(0, 255));
00142 }
00143
00144
00145 return str_shuffle($randomString);
00146 }
00147
00156 public function randomNumber ($min, $max) {
00157 return mt_rand($min, $max);
00158 }
00159
00165 public final function getExtraSalt () {
00166 return $this->extraSalt;
00167 }
00168
00174 public final function getFixedSalt () {
00175 return $this->fixedSalt;
00176 }
00177 }
00178
00179
00180 ?>