Qurl.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. App::uses('ToolsAppModel', 'Tools.Model');
  3. /**
  4. * Manage Quick Urls
  5. *
  6. * @author Mark Scherer
  7. * @cakephp 2.x
  8. * @license MIT
  9. */
  10. class Qurl extends ToolsAppModel {
  11. public $displayField = 'key';
  12. public $scaffoldSkipFields = array('note', 'key', 'content');
  13. public $order = array('Qurl.created' => 'DESC');
  14. protected $defaultLength = 22;
  15. protected $validity = YEAR;
  16. public $validate = array(
  17. 'key' => array(
  18. 'notEmpty' => array(
  19. 'rule' => array('notEmpty'),
  20. 'message' => 'valErrMandatoryField',
  21. 'last' => true,
  22. ),
  23. 'isUnique' => array(
  24. 'rule' => array('isUnique'),
  25. 'message' => 'valErrQurlKeyExists',
  26. ),
  27. ),
  28. 'url' => array(
  29. 'notEmpty' => array(
  30. 'rule' => array('notEmpty'),
  31. 'message' => 'valErrMandatoryField',
  32. 'last' => true
  33. ),
  34. 'validateUrl' => array(
  35. 'rule' => array('validateUrl', array('deep'=>false, 'sameDomain'=>true, 'autoComplete'=>true)),
  36. 'message' => 'valErrInvalidQurlUrl',
  37. 'last' => true
  38. )
  39. ),
  40. 'note' => array(
  41. ),
  42. 'comment' => array(
  43. ),
  44. 'used' => array(
  45. 'numeric' => array(
  46. 'rule' => array('numeric'),
  47. 'message' => 'valErrMandatoryField',
  48. ),
  49. ),
  50. );
  51. public function beforeValidate($options = array()) {
  52. parent::beforeValidate($options);
  53. if (isset($this->data[$this->alias]['key']) && empty($this->data[$this->alias]['key'])) {
  54. $length = null;
  55. if (!empty($this->data[$this->alias]['key_length'])) {
  56. $length = $this->data[$this->alias]['key_length'];
  57. }
  58. $this->data[$this->alias]['key'] = $this->generateKey($length);
  59. }
  60. return true;
  61. }
  62. public function beforeSave($options = array()) {
  63. parent::beforeSave($options);
  64. if (isset($this->data[$this->alias]['content'])) {
  65. $this->data[$this->alias]['content'] = serialize($this->data[$this->alias]['content']);
  66. }
  67. return true;
  68. }
  69. /**
  70. * Qurl::translate()
  71. *
  72. * @param mixed $key
  73. * @return array
  74. */
  75. public function translate($key) {
  76. $res = $this->find('first', array('conditions'=>array($this->alias.'.key'=>$key, $this->alias.'.active'=>1)));
  77. if (!$res) {
  78. return false;
  79. }
  80. //$res['CodeKey']['url'] = Router::url($content['url'], true);
  81. if ($res[$this->alias]['content']) {
  82. $res[$this->alias]['content'] = unserialize($res[$this->alias]['content']);
  83. } else {
  84. $res[$this->alias]['content'] = array();
  85. }
  86. return $res;
  87. }
  88. /**
  89. * Form the access url by key
  90. *
  91. * @param string $key
  92. * @return string Url (absolute)
  93. */
  94. public static function urlByKey($key, $title = null, $slugTitle = true) {
  95. if ($title && $slugTitle) {
  96. $title = Inflector::slug($title, '-');
  97. }
  98. return Router::url(array('admin' => false, 'plugin'=>'tools', 'controller'=>'qurls', 'action'=>'go', $key, $title), true);
  99. }
  100. /**
  101. * Makes an absolute url string ready to input anywhere.
  102. * Uses generate() internally to get the key.
  103. *
  104. * @param mixed $url
  105. * @return string Url (absolute)
  106. */
  107. public function url($url, $data = array()) {
  108. $key = $this->generate($url, $data);
  109. if (!$key) {
  110. return false;
  111. }
  112. return $this->urlByKey($key);
  113. }
  114. /**
  115. * Generates a Qurl key
  116. *
  117. * @param mixed $url
  118. * @param string $uid
  119. * @return string Key
  120. */
  121. public function generate($url, $data = array()) {
  122. $url = Router::url($url, true);
  123. $content = array_merge($data, array('url' => $url));
  124. if (!isset($content['key'])) {
  125. $content['key'] = '';
  126. }
  127. $res = $this->save($content);
  128. if (!$res) {
  129. return false;
  130. }
  131. return $res[$this->alias]['key'];
  132. }
  133. /**
  134. * Sets Key to "used" (only once!) - directly by ID
  135. *
  136. * @param id of key to spend: necessary
  137. * @return boolean Success
  138. */
  139. public function markAsUsed($id = null) {
  140. if (empty($id)) {
  141. return false;
  142. }
  143. //$this->id = $id;
  144. if ($this->updateAll(array($this->alias.'.used' => $this->alias.'.used + 1', $this->alias.'.last_used'=>'"'.date(FORMAT_DB_DATETIME).'"'), array($this->alias.'.id'=>$id))) {
  145. return true;
  146. }
  147. return false;
  148. }
  149. /**
  150. * Remove old/invalid keys
  151. * does not remove recently used ones (for proper feedback)!
  152. *
  153. * @return boolean success
  154. */
  155. public function garbigeCollector() {
  156. $conditions = array(
  157. $this->alias.'.created <'=>date(FORMAT_DB_DATETIME, time()-$this->validity),
  158. );
  159. return $this->deleteAll($conditions, false);
  160. }
  161. /**
  162. * Get admin stats
  163. *
  164. * @return array
  165. */
  166. public function stats() {
  167. $keys = array();
  168. $keys['unused_valid'] = $this->find('count', array('conditions'=>array($this->alias.'.used'=>0, $this->alias.'.created >='=>date(FORMAT_DB_DATETIME, time()-$this->validity))));
  169. $keys['used_valid'] = $this->find('count', array('conditions'=>array($this->alias.'.used'=>1, $this->alias.'.created >='=>date(FORMAT_DB_DATETIME, time()-$this->validity))));
  170. $keys['unused_invalid'] = $this->find('count', array('conditions'=>array($this->alias.'.used'=>0, $this->alias.'.created <'=>date(FORMAT_DB_DATETIME, time()-$this->validity))));
  171. $keys['used_invalid'] = $this->find('count', array('conditions'=>array($this->alias.'.used'=>1, $this->alias.'.created <'=>date(FORMAT_DB_DATETIME, time()-$this->validity))));
  172. $urls = $this->find('all', array('conditions'=>array(), 'fields'=>array('DISTINCT url')));
  173. $keys['urls'] = !empty($urls) ? Set::extract('/'.$this->alias.'/url', $urls) : array();
  174. return $keys;
  175. }
  176. /**
  177. * Generate key
  178. *
  179. * @param length (defaults to defaultLength)
  180. * @return string codekey
  181. */
  182. public function generateKey($length = null) {
  183. if (empty($length)) {
  184. $length = $this->defaultLength;
  185. }
  186. App::uses('RandomLib', 'Tools.Lib');
  187. return RandomLib::generatePassword($length);
  188. }
  189. }