PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/class_card.php

https://github.com/kuaileshike/upload
PHP | 125 lines | 104 code | 13 blank | 8 comment | 16 complexity | b2b681a884aeb532394d6e06a150bafe MD5 | raw file
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_card.php 27449 2012-02-01 05:32:35Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class card{
  12. var $set = array();
  13. var $rulekey = array("str"=>"\@", "num"=>"\#", "full"=>"\*");
  14. var $sysrule = '';
  15. var $rule = '';
  16. var $rulemap_str = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
  17. var $rulemap_num = "123456789";
  18. var $rulereturn = array();
  19. var $cardlist = array();
  20. var $succeed = 0;
  21. var $fail = 0;
  22. var $failmin = 1;
  23. var $failrate = '0.1';
  24. function card() {
  25. $this->init();
  26. }
  27. function init() {
  28. global $_G;
  29. $this->set = &$_G['setting']['card'];
  30. $this->sysrule = "^[A-Z0-9".implode('|', $this->rulekey)."]+$";
  31. }
  32. function make($rule = '', $num = 1, $cardval = array()) {
  33. global $_G;
  34. $this->rule = empty($rule) ? $this->set['rule'] : trim($rule) ;
  35. if(empty($this->rule)) {
  36. return -1;
  37. }
  38. $this->fail($num);
  39. $cardval['makeruid'] = $_G['uid'];
  40. $cardval['dateline'] = $_G['timestamp'];
  41. for($i = 0; $i < $num ; $i++) {
  42. if($this->checkrule($this->rule)) {
  43. $card = $this->rule;
  44. foreach($this->rulereturn AS $key => $val) {
  45. $search = array();
  46. foreach($val AS $skey => $sval) {
  47. $search[] = '/'.$this->rulekey[$key].'/';
  48. }
  49. $card = preg_replace($search, $val, $card, 1);
  50. }
  51. } else {
  52. return 0;
  53. }
  54. $cardval['id'] = $card;
  55. C::t('common_card')->insert($cardval, false, false, 'SILENT');
  56. if(($sqlerror = DB::error())) {
  57. if($sqlerror == 1062) {
  58. $this->fail++;
  59. if($this->failmin > $this->fail) {
  60. $num++;
  61. } else {
  62. $num = $i - 1;
  63. }
  64. }/* else {
  65. DB::halt($sqlerror, $sql);
  66. }*/
  67. } else {
  68. $this->succeed += intval(DB::affected_rows());
  69. $this->cardlist[] = $card;
  70. }
  71. }
  72. return true;
  73. }
  74. function checkrule($rule, $type = '0') {
  75. if(!preg_match("/($this->sysrule)/i", $rule)){
  76. return -2;
  77. }
  78. if($type == 0) {
  79. foreach($this->rulekey AS $key => $val) {
  80. $match = array();
  81. preg_match_all("/($val){1}/i", $rule, $match);
  82. $number[$key] = count($match[0]);
  83. if($number[$key] > 0) {
  84. for($i = 0; $i < $number[$key]; $i++) {
  85. switch($key) {
  86. case 'str':
  87. $rand = mt_rand(0, (strlen($this->rulemap_str) - 1));
  88. $this->rulereturn[$key][$i] = $this->rulemap_str[$rand];
  89. break;
  90. case 'num':
  91. $rand = mt_rand(0, (strlen($this->rulemap_num) - 1));
  92. $this->rulereturn[$key][$i] = $this->rulemap_num[$rand];
  93. break;
  94. case 'full':
  95. $fullstr = $this->rulemap_str.$this->rulemap_num;
  96. $rand = mt_rand(0,(strlen($fullstr) - 1));
  97. $this->rulereturn[$key][$i] = $fullstr[$rand];
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. function fail($num = 1) {
  107. $failrate = $this->failrate ? (float)$this->failrate : '0.1';
  108. $this->failmin = ceil($num * $failrate);
  109. $this->failmin = $this->failmin > 100 ? 100 : $this->failmin;
  110. }
  111. };
  112. ?>