PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/login/models/Challenge.php

http://digitalus-cms.googlecode.com/
PHP | 230 lines | 100 code | 18 blank | 112 comment | 7 complexity | d1bee3edc21c167e0ffe87a7cd0cafaa MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Digitalus CMS
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://digitalus-media.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@digitalus-media.com so we can send you a copy immediately.
  14. *
  15. * @author LowTower - lowtower@gmx.de
  16. * @category Digitalus CMS
  17. * @package Digitalus_CMS_Module_Login
  18. * @copyright Copyright (c) 2007 - 2010, Digitalus Media USA (digitalus-media.com)
  19. * @license http://digitalus-media.com/license/new-bsd New BSD License
  20. * @version $Id: Challenge.php Mon Dec 24 20:38:38 EST 2007 20:38:38 forrest lyman $
  21. * @link http://www.digitaluscms.com
  22. * @since Release 1.10
  23. */
  24. /**
  25. * Challenge model
  26. *
  27. * @author LowTower - lowtower@gmx.de
  28. * @copyright Copyright (c) 2007 - 2010, Digitalus Media USA (digitalus-media.com)
  29. * @license http://digitalus-media.com/license/new-bsd New BSD License
  30. * @version Release: @package_version@
  31. * @link http://www.digitaluscms.com
  32. * @since Release 1.10
  33. */
  34. class Login_Challenge extends Digitalus_Db_Table
  35. {
  36. const validTimePeriod = 172800; // the challenge is valid for 2 days: 2d = 2*24*3600 = 172800s
  37. const DB_NAME = 'challenge';
  38. /**
  39. * The table name.
  40. *
  41. * @var string
  42. */
  43. protected $_name = 'challenge';
  44. /**
  45. * The user name.
  46. *
  47. * @var string
  48. */
  49. protected $_userName;
  50. /**
  51. * The challenge id.
  52. *
  53. * @var string
  54. */
  55. protected $_challengeId;
  56. /**
  57. * The challenge url.
  58. *
  59. * @var string
  60. */
  61. protected $_challengeUrl;
  62. /**
  63. * Constructor
  64. *
  65. * @param mixed $config Array of user-specified config options, or just the Db Adapter.
  66. * @return void
  67. */
  68. public function __construct($config = array())
  69. {
  70. parent::__construct($config);
  71. $this->_createChallengeId();
  72. $this->_cleanUpDb();
  73. }
  74. public static function isDbInstalled()
  75. {
  76. if (Digitalus_Db_Table::tableExists(Digitalus_Db_Table::getTableName(self::DB_NAME))) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Returns a challenge Url
  83. *
  84. * @param bool $html Return a html or a plain text Challenge Url
  85. * @return string Challenge Url
  86. */
  87. public function getChallengeUrl($html = false, $action = null)
  88. {
  89. $challengeUrl = $this->_createChallengeUrl($action);
  90. if (true == $html) {
  91. $challengeUrl = '<a href="' . $this->_challengeUrl . '">' . $challengeUrl . '</a>';
  92. }
  93. return urldecode($challengeUrl);
  94. }
  95. /**
  96. * Creates a challenge Url
  97. *
  98. * @return string Challenge Url
  99. */
  100. protected function _createChallengeUrl($action = 'challenge')
  101. {
  102. if (empty($this->_challengeUrl) || '' == $this->_challengeUrl) {
  103. $this->_challengeUrl = urlencode(
  104. 'http://' . $_SERVER['HTTP_HOST'] . $this->view->getBaseUrl() . '/' . Digitalus_Toolbox_Page::getCurrentPageName() . '/p'
  105. . '/a/' . strtolower($action) // action
  106. . '/u/' . $this->_userName // username
  107. . '/c/' . $this->getChallengeId() // challenge
  108. );
  109. }
  110. return $this->_challengeUrl;
  111. }
  112. /**
  113. * Returns the challenge id
  114. *
  115. * @return string Challenge Id
  116. */
  117. public function getChallengeId()
  118. {
  119. return $this->_challengeId;
  120. }
  121. /**
  122. * Creates a new challenge Id
  123. *
  124. * @return void
  125. */
  126. protected function _createChallengeId()
  127. {
  128. // create challengeId (double mersenne twister)
  129. $this->_challengeId = md5(mt_rand() . mt_rand());
  130. }
  131. protected function _setUserName($userName)
  132. {
  133. $this->_userName = $userName;
  134. return $this->_userName;
  135. }
  136. /**
  137. * Inserts a new challenge into the database
  138. *
  139. * @param string $challengeId The challenge id
  140. * @param string $userName The corresponding username for the given challenge
  141. * @param id $valid Validity of new challenge
  142. * @return int The primary key of the row inserted.
  143. */
  144. public function insertChallenge($challengeId, $userName, $valid = 1)
  145. {
  146. $this->_setUserName($userName);
  147. $data = array(
  148. 'challenge_id' => $challengeId,
  149. 'user_name' => $userName,
  150. 'valid' => $valid,
  151. 'timestamp' => time()
  152. );
  153. return $this->insert($data);
  154. }
  155. /**
  156. * Makes a given challenge valid
  157. *
  158. * @param string $challengeId The challenge id to validate
  159. * @return int Number of rows updated
  160. */
  161. public function validate($challengeId)
  162. {
  163. $data = array(
  164. 'valid' => 1,
  165. 'timestamp' => time()
  166. );
  167. $where[] = $this->_db->quoteInto('challenge_id = ?', $challengeId);
  168. return $this->update($data, $where);
  169. }
  170. /**
  171. * Makes a given challenge invalid
  172. *
  173. * @param string $challengeId The challenge id to invalidate
  174. * @return int Number of rows updated
  175. */
  176. public function invalidate($challengeId)
  177. {
  178. $data['valid'] = 0;
  179. $where[] = $this->_db->quoteInto('challenge_id = ?', $challengeId);
  180. return $this->update($data, $where);
  181. }
  182. /**
  183. * Checks whether a given challenge is valid
  184. *
  185. * @param string $challengeId The challenge id to check against
  186. * @param string $userName The corresponding username for the given challenge
  187. * @return int Number of rows deleted
  188. */
  189. public function isValid($challengeId, $userName)
  190. {
  191. $select = $this->select();
  192. $select->where($this->_db->quoteInto('challenge_id = ?', $challengeId))
  193. ->where($this->_db->quoteInto('user_name = ?', $userName))
  194. ->where($this->_db->quoteInto('valid = ?', 1))
  195. ->where($this->_db->quoteInto('timestamp > ?', time() - self::validTimePeriod));
  196. $result = $this->_db->fetchRow($select);
  197. if (!empty($result)) {
  198. return true;
  199. }
  200. return false;
  201. }
  202. /**
  203. * Cleans up the database from old, unused challenges
  204. *
  205. * @return int Number of rows deleted
  206. */
  207. protected function _cleanUpDb()
  208. {
  209. $where[] = $this->_db->quoteInto('timestamp < ?', time() - (self::validTimePeriod + 1));
  210. return $this->delete($where);
  211. }
  212. }