Advertisement
Guest User

Untitled

a guest
Feb 6th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. <?php
  2. class IValidator{
  3.  
  4. var $Themes;
  5. var $Fonts;
  6. var $Width = 100;
  7. var $Height = 40;
  8. var $BorderWidth = 1;
  9. var $ImageType = 'jpeg';
  10. var $FontsDir = DIR_ROOT;
  11. var $RndCodes = 'qwertyuiopasdfghjklzxcvbnm0123456789';
  12. var $RndLength = 6;
  13. var $SPrefix = 'SS_';
  14.  
  15. function IValidator(){
  16.  
  17. $this->Themes = array (
  18. array(
  19. 'background' => array(225, 60, 50),
  20. 'border' => array(0, 0, 0),
  21. 'font' => array(0, 0, 0),
  22. ),
  23. array(
  24. 'background' => array(205, 255, 204),
  25. 'border' => array(0, 0, 0),
  26. 'font' => array(0, 0, 0),
  27. ),
  28. array(
  29. 'background' => array(252,252,150),
  30. 'border' => array(0, 0, 0),
  31. 'font' => array(60,60,150),
  32. ),
  33. array(
  34. 'background' => array(160, 160, 227),
  35. 'border' => array(0, 0, 0),
  36. 'font' => array(32,69,38),
  37. ),
  38. );
  39. $this->Fonts = array(
  40. 'arial.ttf',
  41. 'verdana.ttf',
  42. );
  43. }
  44.  
  45. function generateImage($_Code = ''){
  46.  
  47. // $this->__generateImage($_Code);
  48. $this->__createCaptcha($_Code);
  49. }
  50.  
  51. function rndCode(){
  52.  
  53. $l_name='';
  54. $top = strlen($this->RndCodes)-1;
  55. srand((double) microtime()*1000000);
  56. for($j=0; $j<$this->RndLength; $j++)$l_name .= $this->RndCodes{rand(0,$top)};
  57. return $l_name;
  58. }
  59.  
  60. function storeCode($_Code){
  61.  
  62. if(!isset($_SESSION[$this->SPrefix.'IVAL'])){
  63. $_SESSION[$this->SPrefix.'IVAL'] = '';
  64. }
  65. $_SESSION[$this->SPrefix.'IVAL'] = $_Code;
  66. }
  67.  
  68. function checkCode($_Code){
  69.  
  70. if(!isset($_SESSION[$this->SPrefix.'IVAL'])) {
  71. return false;
  72. }
  73. if(!$_Code)
  74. return false;
  75. if(strtolower($_SESSION[$this->SPrefix.'IVAL']) == strtolower($_Code)){
  76. return true;
  77. }else{
  78.  
  79. $_SESSION[$this->SPrefix.'IVAL'] = '';
  80. return false;
  81. }
  82. }
  83.  
  84. function __generateImage($_Code = ''){
  85.  
  86.  
  87. if(!$_Code)$_Code = $this->rndCode();
  88.  
  89. $this->storeCode($_Code);
  90.  
  91. $Theme = mt_rand(0, count($this->Themes)-1);
  92. $Theme = $this->Themes[$Theme];
  93. $FontFile = mt_rand(0, count($this->Fonts)-1);
  94. $FontFile = $this->FontsDir.'/'.$this->Fonts[$FontFile];
  95.  
  96. if(function_exists('imagecreatetruecolor'))
  97. $Image = imagecreatetruecolor($this->Width, $this->Height);
  98. else
  99. $Image = imagecreate($this->Width, $this->Height);
  100.  
  101. $Fill = ImageColorAllocate($Image, $Theme['background'][0], $Theme['background'][1], $Theme['background'][2]);
  102. $Border = ImageColorAllocate($Image, $Theme['border'][0], $Theme['border'][1], $Theme['border'][2]);
  103.  
  104. ImageFilledRectangle($Image, $this->BorderWidth, $this->BorderWidth, $this->Width-$this->BorderWidth-1, $this->Height-$this->BorderWidth-1, $Fill);
  105. ImageRectangle($Image, 0, 0, $this->Width-1, $this->Height-1, $Border);
  106.  
  107. $Font = imagecolorallocate($Image, $Theme['font'][0], $Theme['font'][1], $Theme['font'][2]);
  108.  
  109. $TrFontSize = 14;
  110. $_TC = strlen($_Code)-1;
  111. $LettersStart = 5;
  112. $LetterOffset = ceil(($this->Width-$LettersStart*2)/($_TC+1));
  113. for(;$_TC>=0;$_TC--){
  114.  
  115. $RSize = mt_rand(3, 5);
  116. imagestring($Image,$RSize,$LettersStart+($_TC)*$LetterOffset, $RSize*2+$RSize, $_Code{$_TC}, $Font);
  117. // imagettftext($Image, $TrFontSize+$RSize, 0, $LettersStart+($_TC)*$LetterOffset, 25+$RSize*2, $Font, $FontFile, $_Code{$_TC});
  118. }
  119.  
  120. if(0 && function_exists('imagecreatetruecolor')){
  121.  
  122. $TrFont = imagecolorallocatealpha($Image, $Theme['font'][0], $Theme['font'][1], $Theme['font'][2], 100);
  123. $TrFontSize = 20;
  124. $_TC = strlen($_Code)-1;
  125. $LetterOffset = ceil($this->Width/($_TC+1));
  126. for(;$_TC>=0;$_TC--){
  127.  
  128. $RSize = mt_rand(1, 5);
  129. imagettftext($Image, $TrFontSize+$RSize, 0, ($_TC)*$LetterOffset, 25+$RSize, $TrFont, $FontFile, $_Code{$_TC});
  130. }
  131. }
  132.  
  133. if($this->ImageType == "jpeg"){
  134.  
  135. header("Content-type: image/jpeg");
  136. imagejpeg($Image, false, 95);
  137. }else{
  138.  
  139. header("Content-type: image/png");
  140. imagepng($Image);
  141. }
  142.  
  143. imagedestroy($Image);
  144. }
  145.  
  146. function __createCaptcha($_Code){
  147.  
  148. if(!$_Code)$_Code = $this->rndCode();
  149.  
  150. $this->storeCode($_Code);
  151.  
  152. $vals = array(
  153. 'word' => $_Code,
  154. 'img_path' => DIR_PRODUCTS_PICTURES,
  155. 'img_url' => DIR_PRODUCTS_PICTURES,
  156. 'font_path' => $this->FontsDir.'/ANTQUAB.TTF',
  157. 'img_width' => $this->Width,
  158. 'img_height' => $this->Height,
  159. 'expiration' => 7200
  160. );
  161.  
  162. $cap = $this->create_captcha($vals);
  163. }
  164.  
  165. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  166. {
  167. $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
  168.  
  169. foreach ($defaults as $key => $val)
  170. {
  171. if ( ! is_array($data))
  172. {
  173. if ( ! isset($$key) OR $$key == '')
  174. {
  175. $$key = $val;
  176. }
  177. }
  178. else
  179. {
  180. $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  181. }
  182. }
  183.  
  184. if ($img_path == '' OR $img_url == '')
  185. {
  186. return FALSE;
  187. }
  188.  
  189. if ( ! @is_dir($img_path))
  190. {
  191. return FALSE;
  192. }
  193.  
  194. if ( ! is_writable($img_path))
  195. {
  196. return FALSE;
  197. }
  198.  
  199. if ( ! extension_loaded('gd'))
  200. {
  201. return FALSE;
  202. }
  203.  
  204. // -----------------------------------
  205. // Remove old images
  206. // -----------------------------------
  207.  
  208. list($usec, $sec) = explode(" ", microtime());
  209. $now = ((float)$usec + (float)$sec);
  210. /*
  211. $current_dir = @opendir($img_path);
  212.  
  213. while($filename = @readdir($current_dir))
  214. {
  215. if ($filename != "." and $filename != ".." and $filename != "index.html")
  216. {
  217. $name = str_replace(".jpg", "", $filename);
  218.  
  219. if (($name + $expiration) < $now)
  220. {
  221. // @unlink($img_path.$filename);
  222. }
  223. }
  224. }
  225. @closedir($current_dir);
  226. */
  227. // -----------------------------------
  228. // Do we have a "word" yet?
  229. // -----------------------------------
  230.  
  231. if ($word == '')
  232. {
  233. $pool = '023456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  234.  
  235. $str = '';
  236. for ($i = 0; $i < 5; $i++)
  237. {
  238. $str .= strtolower(substr($pool, mt_rand(0, strlen($pool) -1), 1));
  239. }
  240.  
  241. $word = $str;
  242. }
  243.  
  244. // -----------------------------------
  245. // Determine angle and position
  246. // -----------------------------------
  247.  
  248. $length = strlen($word);
  249. $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  250. $x_axis = rand(6, (360/$length)-16);
  251. $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
  252.  
  253. // -----------------------------------
  254. // Create image
  255. // -----------------------------------
  256.  
  257. $im = ImageCreate($img_width, $img_height);
  258.  
  259. // -----------------------------------
  260. // Assign colors
  261. // -----------------------------------
  262.  
  263. $bg_color = ImageColorAllocate($im, 255, 255, 255);
  264. $border_color = ImageColorAllocate($im, 255, 255, 255);
  265. $text_color = ImageColorAllocate($im, 254, 53, 53);
  266. $grid_color = imagecolorallocate($im, 255, 182, 182);
  267. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  268.  
  269. // -----------------------------------
  270. // Create the rectangle
  271. // -----------------------------------
  272.  
  273. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  274.  
  275. // -----------------------------------
  276. // Create the spiral pattern
  277. // -----------------------------------
  278.  
  279. $theta = 1;
  280. $thetac = 7;
  281. $radius = 16;
  282. $circles = 20;
  283. $points = 32;
  284.  
  285. for ($i = 0; $i < ($circles * $points) - 1; $i++)
  286. {
  287. $theta = $theta + $thetac;
  288. $rad = $radius * ($i / $points );
  289. $x = ($rad * cos($theta)) + $x_axis;
  290. $y = ($rad * sin($theta)) + $y_axis;
  291. $theta = $theta + $thetac;
  292. $rad1 = $radius * (($i + 1) / $points);
  293. $x1 = ($rad1 * cos($theta)) + $x_axis;
  294. $y1 = ($rad1 * sin($theta )) + $y_axis;
  295. imageline($im, $x, $y, $x1, $y1, $grid_color);
  296. $theta = $theta - $thetac;
  297. }
  298.  
  299. // -----------------------------------
  300. // Write the text
  301. // -----------------------------------
  302.  
  303. $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  304.  
  305. if ($use_font == FALSE)
  306. {
  307. $font_size = 5;
  308. $x = rand(0, $img_width/($length/3));
  309. $y = 0;
  310. }
  311. else
  312. {
  313. $font_size = 16;
  314. $x = rand(0, $img_width/($length/1.5));
  315. $y = $font_size+2;
  316. }
  317.  
  318. for ($i = 0; $i < strlen($word); $i++)
  319. {
  320. if ($use_font == FALSE)
  321. {
  322. $y = rand(0 , $img_height/2);
  323. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  324. $x += ($font_size*2);
  325. }
  326. else
  327. {
  328. $y = rand($img_height/2, $img_height-3);
  329. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  330. $x += $font_size;
  331. }
  332. }
  333.  
  334.  
  335. // -----------------------------------
  336. // Create the border
  337. // -----------------------------------
  338.  
  339. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  340.  
  341. // -----------------------------------
  342. // Generate the image
  343. // -----------------------------------
  344.  
  345. $img_name = $now.'.jpg';
  346.  
  347. // ImageJPEG($im, $img_path.$img_name);
  348.  
  349. if($this->ImageType == "jpeg"){
  350.  
  351. header("Content-type: image/jpeg");
  352. imagejpeg($im, false, 95);
  353. }else{
  354.  
  355. header("Content-type: image/png");
  356. imagepng($im);
  357. }
  358.  
  359. ImageDestroy($im);
  360.  
  361. // $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" class=\"captcha\" style=\"border:0;\" alt=\" \" />";
  362.  
  363. return array('word' => $word, 'time' => $now, 'image' => $img);
  364. }
  365. }
  366. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement