PageRenderTime 89ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/aoliz/core/model/utility/mdl.verifyCode.php

http://phpfor.googlecode.com/
PHP | 231 lines | 161 code | 41 blank | 29 comment | 33 complexity | ba4922b926256aeebc2fdf51abe0cb10 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ??????
  5. *
  6. * @package ShopEx??????
  7. * @version 4.6
  8. * @author ShopEx.cn <develop@shopex.cn>
  9. * @url http://www.shopex.cn/
  10. * @since PHP 4.3
  11. * @copyright ShopEx.cn
  12. *
  13. **/
  14. class mdl_verifyCode {
  15. var $Noisy = 2; // ????????
  16. var $Count = 4; // ????
  17. var $Width = 60; // ????
  18. var $Height = 16; // ????
  19. var $Angle = 2; // ???????
  20. var $Offset = 10; // ???????
  21. var $Border = 1; // ????
  22. var $imgData = "";
  23. var $Graph=array();
  24. var $code = "";
  25. function mdl_verifycode() {
  26. }
  27. function initverfycode(){
  28. $system = &$GLOBALS['system'];
  29. mt_srand((double)microtime() * 1000000);
  30. $code = mt_rand(1000, 9999);
  31. // $system->setCookie("RANDOM_CODE",md5($code.$system->getConf('certificate.token')));
  32. if (!empty($this->type))
  33. $cookieName = strtoupper($this->type).'_RANDOM_CODE';
  34. else
  35. $cookieName = 'RANDOM_CODE';
  36. $system->setCookie($cookieName,md5($code));
  37. $this->code = $code;
  38. for($i=0;$i<strlen($code);$i++)
  39. $this->SetDraw(substr($code,$i,1), $i);
  40. }
  41. function init() {
  42. mt_srand((double)microtime() * 1000000);
  43. $code = mt_rand(1000, 9999);
  44. $this->code = $code;
  45. for($i=0;$i<strlen($code);$i++)
  46. $this->SetDraw(substr($code,$i,1), $i);
  47. return $this->code;
  48. }
  49. function SetDot($pX, $pY) {
  50. if($pX * ($this->Width-$pX-1) >= 0 && $pY * ($this->Height-$pY-1) >= 0)
  51. {
  52. $this->Graph[$pX][$pY] = 1;
  53. }
  54. }
  55. function Rnd() {
  56. mt_srand((double)microtime() * 1000000);
  57. return mt_rand(1, 999)/1000;
  58. }
  59. function Sgn($v) {
  60. if($v>0) return 1;
  61. if($v==0) return 0;
  62. if($v<0) return -1;
  63. }
  64. function SetDraw($pIndex, $pNumber) {
  65. // ????
  66. $DotData[0] = array(1, 80, 30, 100, 80, 100, 100, 70, 100, 20, 70, 1, 30, 1, 1, 20, 1, 40, 1, 80);
  67. $DotData[1] = array(30, 15, 50, 1, 50, 100);
  68. $DotData[2] = array(1 ,34 ,30 ,1 ,71, 1, 100, 34, 1, 100, 93, 100, 100, 86);
  69. $DotData[3] = array(1, 1, 100, 1, 42, 42, 100, 70, 50, 100, 1, 70);
  70. $DotData[4] = array(100, 73, 6, 73, 75, 6, 75, 100);
  71. $DotData[5] = array(100, 1, 1, 1, 1, 50, 50, 35, 100, 55, 100, 80, 50, 100, 1, 95);
  72. $DotData[6] = array(100, 20, 70, 1, 20, 1, 1, 30, 1, 80, 30, 100, 70, 100, 100, 80, 100, 60, 70, 50, 30, 50, 1, 60);
  73. $DotData[7] = array(6, 26, 6, 6, 100, 6, 53, 100);
  74. $DotData[8] = array(100, 30, 100, 20, 70, 1, 30, 1, 1, 20, 1, 30, 100, 70, 100, 80, 70, 100, 30, 100, 1, 80, 1, 70, 100, 30);
  75. $DotData[9] = array(1, 80, 30, 100, 80, 100, 100, 70, 100, 20, 70, 1, 30, 1, 1, 20, 1, 40, 30, 50, 70, 50, 100, 40);
  76. $vExtent = $this->Width / strlen($this->code);
  77. $Margin[0] = $this->Border +$vExtent *$pNumber+ $vExtent * ($this->Rnd() * $this->Offset) / 100;
  78. $Margin[1] = $vExtent * ($pNumber + 1) - $this->Border - $vExtent * ($this->Rnd() * $this->Offset) / 100;
  79. $Margin[2] = $this->Border + $this->Height * ($this->Rnd() * $this->Offset) / 100;
  80. $Margin[3] = $this->Height - $this->Border - $this->Height * ($this->Rnd * $this->Offset) / 100;
  81. $vWidth = intval($Margin[1] - $Margin[0]);
  82. $vHeight = intval($Margin[3] - $Margin[2]);
  83. //????
  84. $vStartX = intval(($DotData[$pIndex][0]-1) * $vWidth / 100);
  85. $vStartY = intval(($DotData[$pIndex][1]-1) * $vHeight / 100);
  86. for ($i = 1 ;$i<=count($DotData[$pIndex])/2;$i++)
  87. {
  88. If($DotData[$pIndex][2*$i-2] <> 0 && $DotData[$pIndex][2*$i] <> 0)
  89. {
  90. // ????
  91. $vEndX = ($DotData[$pIndex][2*$i]-1) * $vWidth / 100;
  92. $vEndY = ($DotData[$pIndex][2*$i+1]-1) * $vHeight / 100;
  93. // ????
  94. $vDX = $vEndX - $vStartX;
  95. // ????
  96. $vDY = $vEndY - $vStartY;
  97. // ????
  98. if($vDX == 0)
  99. $vAngle = $this->Sgn($vDY) * 3.14/2;
  100. else
  101. $vAngle = atan($vDY /$vDX);
  102. //?????
  103. if(sin($vAngle) == 0)
  104. $vLength = $vDX;
  105. else
  106. $vLength = $vDY / sin($vAngle);
  107. // ??????
  108. $vAngle = $vAngle + ($this->Rnd() - 0.5) * 2 * $this->Angle * 3.14 * 2 / 100;
  109. $vDX = intval(cos($vAngle) * $vLength);
  110. $vDY = intval(sin($vAngle) * $vLength);
  111. if(abs($vDX) > abs($vDY))
  112. $vDeltaT = abs($vDX) ;
  113. else
  114. $vDeltaT = abs($vDY);
  115. for($j = 1;$j<=$vDeltaT;$j++)
  116. $this->SetDot($Margin[0] + $vStartX + $j * $vDX / $vDeltaT, $Margin[2] + $vStartY + $j * $vDY / $vDeltaT);
  117. $vStartX = $vStartX + $vDX;
  118. $vStartY = $vStartY + $vDY;
  119. }
  120. }
  121. }
  122. function Output($width=60,$height=16,$type=false) {
  123. if($type){
  124. $this->initverfycode();
  125. }
  126. $this->Width = $width;
  127. $this->Height = $height;
  128. $out = "";
  129. $i = 0;
  130. // ????
  131. $out .= "GIF";
  132. // ????
  133. $out .= "89a";
  134. // ??????
  135. $out .= chr($this->Width % 256).chr(($this->Width/256) % 256);
  136. // ??????
  137. $out .= chr($this->Height % 256).chr(($this->Height / 256) % 256);
  138. $out .= chr(128) . chr(0) . chr(0);
  139. // ??????
  140. $out .= chr(0xEE).chr(0xEE).chr(0xEE);
  141. $out .= chr(0x00).chr(0x00).chr(0x00);
  142. // ?????
  143. $out .= ",";
  144. $out .= chr(0).chr(0).chr(0).chr(0);
  145. // ????
  146. $out .= chr($this->Width % 256).chr(($this->Width/256) % 256);
  147. // ????
  148. $out .= chr($this->Height % 256).chr(($this->Height / 256) % 256);
  149. $out .= chr(0).chr(7).chr(255);
  150. for($y = 0;$y<$this->Height;$y++)
  151. {
  152. for($x = 0;$x<$this->Width;$x++)
  153. {
  154. if($this->Rnd() < $this->Noisy / 100)
  155. $out .= chr(1-$this->Graph[$x][$y]);
  156. else
  157. {
  158. if($x * ($x-$this->Width) == 0 || $y * ($y-$this->Height) == 0)
  159. $out .= chr($this->Graph[$x][$y]);
  160. else
  161. {
  162. if($this->Graph[$x-1][$y]== 1 || $this->Graph[$x][$y] || $this->Graph[$x][$y-1] == 1)
  163. $out .= chr(1);
  164. else
  165. $out .= chr(0);
  166. }
  167. }
  168. if(($y * $this->Width + $x + 1) % 126 == 0)
  169. {
  170. $out .= chr(128);
  171. $i++;
  172. }
  173. if(($y * $this->Width + $x + $i + 1) % 255 == 0)
  174. {
  175. if(($this->Width*$this->Height - $y * $this->Width - $x - 1) > 255)
  176. $out .= chr(255);
  177. else
  178. $out .= chr($this->Width * $this->Height %255);
  179. }
  180. }
  181. }
  182. $out .= chr(128).chr(0).chr(129).chr(0).chr(59);
  183. header("Expires: -9999");
  184. header("Pragma: no-cache");
  185. header("Cache-Control: no-cache, no-store");
  186. header("Content-Type: image/gif");
  187. echo $out;
  188. }
  189. }
  190. ?>