PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/utility/verifycode/PwBaseCode.php

https://github.com/cuijinquan/nextwind
PHP | 159 lines | 91 code | 23 blank | 45 comment | 6 complexity | 99630e2cade9c4a77ef75137c20c4bf8 MD5 | raw file
  1. <?php
  2. /**
  3. * the last known user to change this file in the repository <$LastChangedBy: gao.wanggao $>
  4. * @author $Author: gao.wanggao $ foxsee@aliyun.com
  5. * @version $Id: PwBaseCode.php 20486 2012-10-30 07:50:14Z gao.wanggao $
  6. * @package
  7. */
  8. class PwBaseCode {
  9. /**
  10. * 验证码长度
  11. *
  12. * @var int
  13. */
  14. public static $verifyLength = 4;
  15. /**
  16. * 1.数字 2.字母 3.数字+字母 4,随机加减,5.汉字6.自定义问题 7语音
  17. *
  18. * @var int
  19. */
  20. public static $verifyType = 3;
  21. public static $verifyWidth = 240;
  22. public static $verifyHeight = 60;
  23. public static $isRandBackground = false;
  24. public static $isRandGraph = false;
  25. public static $isRandFont = false;
  26. public static $isRandSize = false;
  27. public static $isRandAngle = false;
  28. public static $isRandColor = false;
  29. public static $isRandGif = false;
  30. public static $isRandDistortion = false;
  31. public static $askCode = '';
  32. public static $answerCode = '';
  33. protected static $verifyCode = '';
  34. protected static $path = 'REP:';
  35. public static function getCode() {
  36. if (in_array(self::$verifyType, array(4,6))) return self::$answerCode;
  37. return strtolower(self::$verifyCode);
  38. }
  39. /**
  40. * 设置验证码
  41. *
  42. * @return void
  43. */
  44. protected static function setRandCode() {
  45. switch (self::$verifyType) {
  46. case '1':
  47. $str = '1234567890';
  48. break;
  49. case '2':
  50. $str = 'abcdefghjkmnpqrstuvwxyABCDEFGHJKLMNPQRSTUVWXY';
  51. break;
  52. case '3':
  53. default:
  54. $str = '3456789bcefghjkmpqrtvwxyzBCEFGHJKMPQRTVWXYZ';
  55. break;
  56. case '5':
  57. $str = '人之初性本善性相近习相远苟不教性乃迁教之道贵以专昔孟母择邻处子不学断机杼窦燕山有义方教五子名俱扬养段算适讲按值美态黄易彪服早班麦削信排台声该击素张密害侯草何树肥继右属市严径螺检左页抗苏显苦英快称坏移约巴材省黑武培著河帝仅针怎植京助升王眼她抓含苗副杂普谈围食射源例致酸旧却充足短划剂宣环落首尺波承粉践府鱼随考刻靠够满夫失包住促枝局菌杆周护岩师举曲春元超负砂封换太模贫减阳扬江析亩木言球朝医校古呢稻宋听唯输滑站另卫字鼓刚写刘微略范供阿块某功套友限项余倒卷创律雨让骨远帮初皮播优占死毒圈伟季训控激找叫云互跟裂粮粒母练塞钢顶策双留误础吸阻故寸盾晚丝女散焊功株亲院冷彻弹错散商视艺灭版烈零室轻血倍缺厘泵察绝富城冲喷壤简否柱李望盘磁雄似困巩益洲脱投送奴侧润盖挥距触星松送获兴独官混纪依未突架宽冬章湿偏纹吃执阀矿寨责熟稳夺硬价努翻奇甲预职评读背协损棉侵灰虽矛厚罗泥辟告卵箱掌氧恩爱停曾溶营终纲孟钱待尽俄缩沙退陈讨奋械载胞幼哪剥迫旋征槽倒握担仍呀鲜吧卡粗介钻逐弱脚怕盐末阴丰编印蜂急拿扩伤飞露核缘游振操央伍域甚迅辉异序免纸夜乡久隶缸夹念兰映沟乙吗儒杀汽磷艰晶插埃燃欢铁补咱芽永瓦倾阵碳演威附牙芽永瓦斜灌欧献顺猪洋腐请透司危括脉宜笑若尾束壮暴企菜穗楚汉愈绿拖牛份染既秋遍锻玉夏疗尖殖井费州访吹荣铜沿替滚客召旱悟刺脑';
  58. break;
  59. case '4':
  60. case '6':
  61. self::$verifyCode = self::_convert(self::$askCode);
  62. return true;
  63. case '7': //目前只有这些声音文件
  64. $str = '123456789BCEFGHJKMPQRTVWXYZ';
  65. break;
  66. }
  67. $_tmp = Pw::strlen($str)-1;
  68. $_num=0;
  69. for($i = 0;$i < self::$verifyLength;$i++){
  70. $_num = mt_rand(0, $_tmp);
  71. $_code = Pw::substrs($str, 1,$_num, false);
  72. self::$verifyCode .= self::_convert($_code);
  73. }
  74. }
  75. private static function _convert($text='') {
  76. return Pw::convert($text, 'UTF-8');
  77. /*if ($text !== utf8_decode(utf8_encode($text))) {
  78. $text = WindConvert::convert($text, 'UTF-8', 'GBK');
  79. }
  80. return $text;*/
  81. }
  82. /**
  83. * 获取验证码背景文件
  84. *
  85. * @return array
  86. */
  87. protected static function getVerifyBackground() {
  88. $_files = array();
  89. $_path = Wind::getRealDir(self::$path.'.bg.');
  90. $files = WindFolder::read($_path);
  91. foreach ($files AS $file) {
  92. if (is_file($_path .$file)) $_files[] = $_path .$file;
  93. }
  94. return $_files;
  95. }
  96. /**
  97. * 获取字体列表
  98. *
  99. * @return array
  100. */
  101. protected static function getFontList() {
  102. $_path=Wind::getRealDir(self::$path.'.font');
  103. return WindFolder::read($_path, WindFolder::READ_FILE);
  104. }
  105. /**
  106. * 获取英文字体列表
  107. *
  108. * @return array
  109. */
  110. protected static function getEnFontList() {
  111. $_fontList = array();
  112. $fontList = self::getFontList();
  113. foreach ($fontList AS $key=>$font) {
  114. if (strpos($font, 'en_')===0) {
  115. $_fontList[] = $font;
  116. }
  117. }
  118. return $_fontList ? $_fontList : array('en_arial.ttf');
  119. }
  120. /**
  121. * 获取中文字体列表
  122. *
  123. * @return array
  124. */
  125. protected static function getCnFontList() {
  126. $_fontList =array();
  127. $fontList = self::getFontList();
  128. foreach ($fontList AS $key=>$font) {
  129. if (strpos($font, 'cn_')===0) {
  130. $_fontList[] = $font;
  131. }
  132. }
  133. return $_fontList;
  134. }
  135. }
  136. ?>