process.class.php 4.7 KB

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