code_key.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class CodeKey extends ToolsAppModel {
  3. var $name = 'CodeKey';
  4. var $displayField = 'key';
  5. var $order = array('CodeKey.created' => 'ASC');
  6. private $defaultLength = 22;
  7. var $validate = array(
  8. 'type' => array(
  9. 'notEmpty' => array(
  10. 'rule' => array('notEmpty'),
  11. 'message' => 'valErrMandatoryField',
  12. ),
  13. ),
  14. 'key' => array(
  15. 'isUnique' => array(
  16. 'rule' => array('isUnique'),
  17. 'message' => 'key already exists',
  18. ),
  19. 'notEmpty' => array(
  20. 'rule' => array('notEmpty'),
  21. 'message' => 'valErrMandatoryField',
  22. ),
  23. ),
  24. 'content' => array(
  25. 'maxLength' => array(
  26. 'rule' => array('maxLength', 255),
  27. 'message' => array('valErrMaxCharacters %s', 255),
  28. 'allowEmpty' => true
  29. ),
  30. ),
  31. 'used' => array('numeric')
  32. );
  33. //var $types = array('activate');
  34. /**
  35. * stores new key in DB
  36. * @param string type: neccessary
  37. * @param string key: optional key, otherwise a key will be generated
  38. * @param mixed user_id: optional (if used, only this user can use this key)
  39. * @param string content: up to 255 characters of content may be added (optional)
  40. * NOW: checks if this key is already used (should be unique in table)
  41. * @return string key on SUCCESS, boolean false otherwise
  42. * 2009-05-13 ms
  43. */
  44. function newKey($type, $key = null, $uid = null, $content = null) {
  45. if (empty($type)) { // || !in_array($type,$this->types)
  46. return false;
  47. }
  48. if (empty($key)) {
  49. $key = $this->generateKey($this->defaultLength);
  50. $keyLength = $this->defaultLength;
  51. } else {
  52. $keyLength = mb_strlen($key);
  53. }
  54. $data = array(
  55. 'type' => $type,
  56. 'user_id' => (string)$uid,
  57. 'content' => (string)$content,
  58. 'key' => $key,
  59. );
  60. $this->set($data);
  61. $max = 99;
  62. while (!$this->validates()) {
  63. $data['key'] = $this->generateKey($keyLength);
  64. $this->set($data);
  65. $max--;
  66. if ($max == 0) { //die('Exeption in CodeKey');
  67. return false;
  68. }
  69. }
  70. $this->create();
  71. if ($this->save($data)) {
  72. return $key;
  73. }
  74. return false;
  75. }
  76. /**
  77. * usesKey (only once!) - by KEY
  78. * @param string type: neccessary
  79. * @param string key: neccessary
  80. * @param mixed user_id: needs to be provided if this key has a user_id stored
  81. * @return ARRAY(content) if successfully used or if already used (used=1), FALSE else
  82. * 2009-05-13 ms
  83. */
  84. function useKey($type, $key, $uid = null) {
  85. if (empty($type) || empty($key)) {
  86. return false;
  87. }
  88. $conditions = array('conditions'=>array($this->alias.'.key'=>$key,$this->alias.'.type'=>$type));
  89. if (!empty($uid)) {
  90. $conditions['conditions'][$this->alias.'.user_id'] = $uid;
  91. }
  92. $res = $this->find('first', $conditions);
  93. if (empty($res)) {
  94. return false;
  95. } elseif(!empty($uid) && !empty($res[$this->alias]['user_id']) && $res[$this->alias]['user_id'] != $uid) {
  96. // return $res; # more secure to fail here if user_id is not provided, but was submitted prev.
  97. return false;
  98. } elseif ($res[$this->alias]['used'] == 1) {
  99. return $res;
  100. }
  101. # actually use key
  102. if ($this->spendKey($res[$this->alias]['id'])) {
  103. return $res;
  104. }
  105. $this->log('VIOLATION in CodeKey Model (method useKey)');
  106. return false;
  107. }
  108. /**
  109. * sets Key to "used" (only once!) - directly by ID
  110. * @param id of key to spend: neccessary
  111. * @return boolean true on success, false otherwise
  112. * 2009-05-13 ms
  113. */
  114. function spendKey($id = null) {
  115. if (empty($id)) {
  116. return false;
  117. }
  118. $this->id = $id;
  119. if ($this->saveField('used', 1)) {
  120. return true;
  121. }
  122. return false;
  123. }
  124. /**
  125. * remove old/invalid keys
  126. * does not remove recently used ones (for proper feedback)!
  127. * @return boolean success
  128. * 2010-06-17 ms
  129. */
  130. function garbigeCollector() {
  131. $conditions = array(
  132. $this->alias.'.created <'=>date(FORMAT_DB_DATETIME, time()-MONTH),
  133. );
  134. return $this->deleteAll($conditions, false);
  135. }
  136. /**
  137. * @param length (defaults to defaultLength)
  138. * @return string codekey
  139. * 2009-05-13 ms
  140. */
  141. function generateKey($length = null) {
  142. if (empty($length)) {
  143. $length = $defaultLength;
  144. } else {
  145. if ((class_exists('CommonComponent') || App::import('Component', 'Common')) && method_exists('CommonComponent', 'generatePassword')) {
  146. return CommonComponent::generatePassword($length);
  147. } else {
  148. return $this->_generateKey($length);
  149. }
  150. }
  151. }
  152. /**
  153. * backup method - only used if no custom function exists
  154. * 2010-06-17 ms
  155. */
  156. function _generateKey($length = null) {
  157. $chars = "234567890abcdefghijkmnopqrstuvwxyz"; // ABCDEFGHIJKLMNOPQRSTUVWXYZ
  158. $i = 0;
  159. $password = "";
  160. $max = strlen($chars) - 1;
  161. while ($i < $length) {
  162. $password .= $chars[mt_rand(0, $max)];
  163. $i++;
  164. }
  165. return $password;
  166. }
  167. }
  168. ?>