国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
6.0KB

  1. <?php
  2. /**
  3. * 验证图片
  4. *
  5. * @version $Id: vdimgck.php$
  6. * @package DedeBIZ.Libraries
  7. * @copyright Copyright (c) 2022, DedeBIZ.COM
  8. * @license https://www.dedebiz.com/license
  9. * @link https://www.dedebiz.com
  10. */
  11. require_once(dirname(__FILE__).'/../system/common.inc.php');
  12. require_once(DEDEDATA.'/config.cache.inc.php');
  13. //安全提示
  14. $config = array(
  15. 'font_size' => 20,
  16. 'img_height' => '36',
  17. 'word_type' => 3,
  18. 'img_width' => '80',
  19. 'use_boder' => TRUE,
  20. 'font_file' => DEDEINC.'/data/fonts/'.mt_rand(1, 6).'.ttf',
  21. 'wordlist_file' => DEDEINC.'/data/words/words.txt',
  22. 'filter_type' => 5
  23. );
  24. $enkey = substr(md5(substr($cfg_cookie_encode, 0, 5)), 0, 10);
  25. $sessSavePath = DEDEDATA."/sessions_{$enkey}";
  26. if (!is_dir($sessSavePath)) mkdir($sessSavePath);
  27. //Session保存路径
  28. if (is_writeable($sessSavePath) && is_readable($sessSavePath)) {
  29. session_save_path($sessSavePath);
  30. }
  31. if (!empty($cfg_domain_cookie)) session_set_cookie_params(0, '/', $cfg_domain_cookie);
  32. if (!echo_validate_image($config)) {
  33. //如果不成功则初始化一个默认验证码
  34. @session_start();
  35. $_SESSION['securimage_code_value'] = strtolower('abcd');
  36. if (function_exists('imagecreatefromjpeg')) {
  37. $im = @imagecreatefromjpeg(DEDEINC.'/data/vdcode.jpg');
  38. header("Pragma:no-cache\r\n");
  39. header("Cache-Control:no-cache\r\n");
  40. header("Expires:0\r\n");
  41. imagejpeg($im);
  42. imagedestroy($im);
  43. } else {
  44. header("Pragma:no-cache\r\n");
  45. header("Cache-Control:no-cache\r\n");
  46. header("Expires:0\r\n");
  47. $c = file_get_contents(DEDEINC.'/data/vdcode.jpg', true);
  48. $size = filesize(DEDEINC.'/data/vdcode.jpg');
  49. header('Content-Type: image/x-icon');
  50. header("Content-length: $size");
  51. echo $c;
  52. }
  53. }
  54. function echo_validate_image($config = array())
  55. {
  56. @session_start();
  57. if (!function_exists('imagettftext')) {
  58. return false;
  59. }
  60. //主要参数
  61. $font_size = isset($config['font_size']) ? $config['font_size'] : 14;
  62. $img_height = isset($config['img_height']) ? $config['img_height'] : 38;
  63. $img_width = isset($config['img_width']) ? $config['img_width'] : 68;
  64. $font_file = isset($config['font_file']) ? $config['font_file'] : DEDEINC.'/data/font/'.mt_rand(1, 6).'.ttf';
  65. $use_boder = isset($config['use_boder']) ? $config['use_boder'] : TRUE;
  66. $filter_type = isset($config['filter_type']) ? $config['filter_type'] : 0;
  67. //创建图片,并设置背景色
  68. $im = @imagecreate($img_width, $img_height);
  69. imagecolorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  70. //文字随机颜色
  71. $fontColor[] = imagecolorallocate($im, 0x15, 0x15, 0x15);
  72. $fontColor[] = imagecolorallocate($im, 0x95, 0x1e, 0x04);
  73. $fontColor[] = imagecolorallocate($im, 0x93, 0x14, 0xa9);
  74. $fontColor[] = imagecolorallocate($im, 0x12, 0x81, 0x0a);
  75. $fontColor[] = imagecolorallocate($im, 0x06, 0x3a, 0xd5);
  76. //获取随机字符
  77. $rndstring = '';
  78. if ($config['word_type'] != 3) {
  79. for ($i = 0; $i < 4; $i++) {
  80. if ($config['word_type'] == 1) {
  81. $c = chr(mt_rand(48, 57));
  82. } else if ($config['word_type'] == 2) {
  83. $c = chr(mt_rand(65, 90));
  84. if ($c == 'I') $c = 'P';
  85. if ($c == 'O') $c = 'N';
  86. }
  87. $rndstring .= $c;
  88. }
  89. } else {
  90. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
  91. $rndstring = '';
  92. $length = rand(4, 4);
  93. $max = strlen($chars) - 1;
  94. for ($i = 0; $i < $length; $i++) {
  95. $rndstring .= $chars[mt_rand(0, $max)];
  96. }
  97. }
  98. $_SESSION['securimage_code_value'] = strtolower($rndstring);
  99. $rndcodelen = strlen($rndstring);
  100. //增加一些噪线
  101. for ($i = 0; $i < 5; $i++) {
  102. $red = mt_rand(50, 255);
  103. $green = mt_rand(50, 255);
  104. $blue = mt_rand(50, 255);
  105. $tcol = imagecolorallocate($im, $red, $green, $blue);
  106. if (mt_rand(0, 1)) { //Horizontal
  107. $Xa = mt_rand(0, $img_width / 2);
  108. $Ya = mt_rand(0, $img_height);
  109. $Xb = mt_rand($img_width / 2, $img_width);
  110. $Yb = mt_rand(0, $img_height);
  111. } else { //Vertical
  112. $Xa = mt_rand(0, $img_width);
  113. $Ya = mt_rand(0, $img_height / 2);
  114. $Xb = mt_rand(0, $img_width);
  115. $Yb = mt_rand($img_height / 2, $img_height);
  116. }
  117. imagesetthickness($im, mt_rand(1, 3));
  118. imageline($im, $Xa, $Ya, $Xb, $Yb, $tcol);
  119. }
  120. //画边框
  121. if ($use_boder && $filter_type == 0) {
  122. $bordercolor = imagecolorallocate($im, 0x9d, 0x9e, 0x96);
  123. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $bordercolor);
  124. }
  125. //输出文字
  126. $lastc = '';
  127. for ($i = 0; $i < $rndcodelen; $i++) {
  128. $rndstring[$i] = strtoupper($rndstring[$i]);
  129. $c_fontColor = $fontColor[mt_rand(0, 4)];
  130. $y_pos = $i == 0 ? 4 : $i * ($font_size + 2);
  131. $c = mt_rand(10, 30);
  132. @imagettftext($im, $font_size, $c, $y_pos, 28, $c_fontColor, $font_file, $rndstring[$i]);
  133. $lastc = $rndstring[$i];
  134. }
  135. //图象效果
  136. switch ($filter_type) {
  137. case 1:
  138. imagefilter($im, IMG_FILTER_NEGATE);
  139. break;
  140. case 2:
  141. imagefilter($im, IMG_FILTER_EMBOSS);
  142. break;
  143. case 3:
  144. imagefilter($im, IMG_FILTER_EDGEDETECT);
  145. break;
  146. default:
  147. break;
  148. }
  149. header("Pragma:no-cache\r\n");
  150. header("Cache-Control:no-cache\r\n");
  151. header("Expires:0\r\n");
  152. if (function_exists("imagejpeg")) {
  153. header("content-type:image/jpeg\r\n");
  154. imagejpeg($im);
  155. } else {
  156. header("content-type:image/png\r\n");
  157. imagepng($im);
  158. }
  159. imagedestroy($im);
  160. exit();
  161. }