PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/nova/lib/vcode.class.php

http://xklog.googlecode.com/
PHP | 119 lines | 88 code | 24 blank | 7 comment | 14 complexity | 97f91723e579f44eea8f92fb2e584c56 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. !defined('IN_NOVA') && exit('Access Denied!');
  3. class Vcode {
  4. private $sessionvar = 'vdcode'; //Session????
  5. public function show( $code_name = 'vdcode', $code_type = 1, $width = 50, $height = 20 ) {
  6. $this->sessionvar = $code_name;
  7. if (function_exists('imagecreate')) {
  8. //??+??????
  9. if ( $code_type == 1 ) {
  10. //??4?????????
  11. $str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  12. $len = strlen($str);
  13. $code = array();
  14. for ($i=0; $i<4; $i++) {
  15. $code[] = $str{mt_rand(0,$len-1)};
  16. }
  17. $_SESSION[$this->sessionvar] = strtolower(implode('',$code));
  18. $img = ImageCreate($width,$height); //????
  19. ImageColorAllocate($img,255,255,255); //????
  20. //????
  21. for ($i=0; $i<100; $i++) {
  22. $pxcolor = ImageColorAllocate($img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  23. ImagesetPixel($img,mt_rand(0,$width),mt_rand(0,$height),$pxcolor);
  24. }
  25. //????
  26. $bordercolor = ImageColorAllocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  27. ImageRectangle($img,0,0,$width-1,$height-1,$bordercolor);
  28. //???????
  29. $offset = round($width/4/2);
  30. $h1 = 0;
  31. $h2 = round($height/3);
  32. foreach ($code as $char) {
  33. $textcolor = ImageColorAllocate($img,mt_rand(0,250),mt_rand(0,150),mt_rand(0,250));
  34. ImageChar($img,5,$offset + mt_rand(-5,round($width/4/3)), mt_rand($h1, $h2),$char,$textcolor);
  35. $offset += round($width/4)-3;
  36. }
  37. //?????????
  38. } elseif ( $code_type == 2 ) {
  39. $operator = '+-*'; //???
  40. $code = array();
  41. $code[] = mt_rand(1,9);
  42. $code[] = $operator{mt_rand(0,2)};
  43. $code[] = mt_rand(1,9);
  44. $code[] = $operator{mt_rand(0,2)};
  45. $code[] = mt_rand(1,9);
  46. $codestr = implode('',$code);
  47. eval("\$result = ".implode('',$code).";");
  48. $code[] = '=';
  49. $_SESSION[$this->sessionvar] = $result;
  50. $img = ImageCreate($width,$height);
  51. ImageColorAllocate($img, mt_rand(230,250), mt_rand(230,250), mt_rand(230,250));
  52. $color = ImageColorAllocate($img, 0, 0, 0);
  53. $offset = round($width/6/2);
  54. $h1 = 0;
  55. $h2 = round($height/6);
  56. foreach ($code as $char) {
  57. $txtcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,150), mt_rand(0,255));
  58. ImageChar($img, mt_rand(3,5), $offset + mt_rand(-5,round($width/6/3)), mt_rand($h1, $h2), $char, $txtcolor);
  59. $offset += round($width/6)-3;
  60. }
  61. for ($i=0; $i<100; $i++) {
  62. $pxcolor = ImageColorAllocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
  63. ImagesetPixel($img, mt_rand(0,$width), mt_rand(0,$height), $pxcolor);
  64. }
  65. }
  66. header("pragma:no-cache\r\n");
  67. header("Cache-Control:no-cache\r\n");
  68. header("Expires:0\r\n");
  69. //????????????????PNG->JPEG->GIF
  70. if (Imagetypes() & IMG_PNG) {
  71. header('content-type:image/png');
  72. ImagePNG($img);
  73. } elseif (Imagetypes() & IMG_JPEG) {
  74. header('content-type:image/jpeg');
  75. ImageJPEG($img);
  76. } else {
  77. header('content-type:image/gif');
  78. ImageGif($img);
  79. }
  80. }
  81. }
  82. public function check( $code ) {
  83. if( !isset( $_SESSION[$this->sessionvar] ) ) {
  84. return false;
  85. }
  86. if( $code != $_SESSION[$this->sessionvar] ) {
  87. unset( $_SESSION[$this->sessionvar] );
  88. return false;
  89. } else {
  90. unset( $_SESSION[$this->sessionvar] );
  91. return true;
  92. }
  93. }
  94. }
  95. ?>