process.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. defined('InShopNC') or exit('Access Invalid!');
  3. /**
  4. * 连续操作验证
  5. * 默认如果两次操作间隔小于10分钟,系统会记录操作次数,超过N次将被锁定,10分钟以后才能提交表单
  6. * 超过10分钟提交表单,以前记录的数据将被清空,重新从1开始计数
  7. */
  8. class process{
  9. //zmr>v30
  10. const MAX_LOGIN = 10; //密码连续输入多少次被暂时锁定
  11. const MAX_COMMIT = 2; //连续评论多少次被暂时锁定
  12. const MAX_REG = 10; //连续注册多少个账号被暂时锁定
  13. const MAX_FORGET = 3; //找回密码输入多少次被暂时锁定
  14. const MAX_ADMIN = 10; //后台管理员输入多少次被暂时锁定
  15. /**
  16. * 是否启用验证
  17. *
  18. * @var unknown_type
  19. */
  20. private static $ifopen = true;
  21. /**
  22. * 锁表对象
  23. *
  24. * @var unknown_type
  25. */
  26. private static $lock;
  27. /**
  28. * 记录ID
  29. *
  30. * @var unknown_type
  31. */
  32. private static $processid = array();
  33. /**
  34. * 锁ID
  35. *
  36. * @var unknown_type
  37. */
  38. private static $lockid = array();
  39. /**
  40. * 初始化,未启用内存保存时默认使用lock表存储
  41. *
  42. * @param unknown_type $type
  43. */
  44. private static function init($type){
  45. if (C('cache_open')){
  46. self::$lock = Cache::getInstance('cacheredis');
  47. }else{
  48. self::$lock = new lock();
  49. }
  50. if (!isset(self::$processid[$type])) {
  51. $ip = sprintf('%u',ip2long(getIp()));
  52. self::$processid[$type] = str_pad($ip,10,'0').self::parsekey($type);;
  53. self::$lockid[$type] = str_pad($ip,11,'0').self::parsekey($type);;
  54. }
  55. }
  56. /**
  57. * 判断是否已锁
  58. *
  59. * @param unknown_type $type
  60. * @return unknown
  61. */
  62. public static function islock($type = null){
  63. if (!self::$ifopen) return;
  64. self::init($type);
  65. return self::$lock->get(self::$lockid[$type]);
  66. }
  67. /**
  68. * 添加锁
  69. *
  70. * @param unknown_type $type
  71. * @param unknown_type $ttl
  72. */
  73. private static function addlock($type = null,$ttl = 600){
  74. if (!self::$ifopen) return;
  75. self::init($type);
  76. self::$lock->set(self::$lockid[$type],1,'',$ttl);
  77. }
  78. /**
  79. * 删除锁
  80. *
  81. * @param unknown_type $type
  82. */
  83. public static function dellock($type = null){
  84. if (!self::$ifopen) return;
  85. self::$lock->rm(self::$lockid[$type]);
  86. }
  87. /**
  88. * 添加记录
  89. *
  90. * @param unknown_type $type
  91. * @param unknown_type $ttl
  92. */
  93. public static function addprocess($type = null,$ttl = 600){
  94. if (!self::$ifopen) return;
  95. self::init($type);
  96. $tims = self::parsetimes($type);
  97. $t = self::$lock->get(self::$processid[$type]);
  98. if ($t >= $tims-1) {
  99. self::addlock($type,$ttl);
  100. self::$lock->rm(self::$processid[$type]);
  101. }else{
  102. if (empty($t)) $t = 0;
  103. self::$lock->set(self::$processid[$type],$t+1,'',$ttl);
  104. }
  105. }
  106. /**
  107. * 删除记录
  108. *
  109. * @param unknown_type $type
  110. */
  111. public static function delprocess($type = null){
  112. if (!self::$ifopen) return;
  113. self::$lock->rm(self::$processid[$type]);
  114. }
  115. /**
  116. * 清空
  117. *
  118. */
  119. public static function clear($type = ''){
  120. if (!self::$ifopen) return;
  121. if (empty($type)) return ;
  122. self::dellock($type);
  123. self::delprocess($type);
  124. }
  125. public static function parsekey($type){
  126. return str_replace(array('login','commit','reg','forget','admin'),array(1,2,3,4,5),$type);
  127. }
  128. /**
  129. * 设置最多尝试次数
  130. *
  131. * @param unknown_type $type
  132. * @return unknown
  133. */
  134. public static function parsetimes($type){
  135. return str_replace(array('login', 'commit', 'reg', 'forget', 'admin'),array(self::MAX_LOGIN, self::MAX_COMMIT, self::MAX_REG, self::MAX_FORGET, self::MAX_ADMIN),$type);
  136. }
  137. }
  138. /**
  139. * lock表 操作
  140. *
  141. * @package
  142. */
  143. defined('InShopNC') or exit('Access Invalid!');
  144. class lock {
  145. private $model;
  146. public function __construct(){
  147. $this->model = Model('lock');
  148. }
  149. /**
  150. * 设置值
  151. *
  152. * @param mixed $key
  153. * @param mixed $value
  154. * @param string $type
  155. * @param int $ttl
  156. * @return bool
  157. */
  158. public function set($key, $value, $type='', $ttl = SESSION_EXPIRE){
  159. $info = $this->model->where(array('pid'=>$key))->find();
  160. if ($info){
  161. $this->model->where(array('pid'=>$key))->update(array('pvalue'=>$value,'expiretime'=>time()+$ttl));
  162. }else{
  163. $this->model->insert(array('pid'=>$key,'pvalue'=>$value,'expiretime'=>time()+$ttl));
  164. }
  165. }
  166. /**
  167. * 取得值
  168. *
  169. * @param mixed $key
  170. * @param mixed $type
  171. * @return bool
  172. */
  173. public function get($key, $type=''){
  174. $info = $this->model->where(array('pid'=>$key))->find();
  175. if ($info && ($info['expiretime'] < time())){
  176. $this->rm($key);return null;
  177. }else{
  178. return $info['pvalue'];
  179. }
  180. }
  181. /**
  182. * 删除值
  183. *
  184. * @param mixed $key
  185. * @param mixed $type
  186. * @return bool
  187. */
  188. public function rm($key, $type=''){
  189. return $this->model->where(array('pid'=>$key))->delete();
  190. }
  191. }
  192. ?>