PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Captcha/Model/Resource/Log.php

https://bitbucket.org/jit_bec/shopifine
PHP | 158 lines | 79 code | 10 blank | 69 comment | 10 complexity | 0d6e10ef4744a0ee50d284914a3ac92c MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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 license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Captcha
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Log Attempts resource
  28. *
  29. * @category Mage
  30. * @package Mage_Captcha
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Captcha_Model_Resource_Log extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. /**
  36. * Type Remote Address
  37. */
  38. const TYPE_REMOTE_ADDRESS = 1;
  39. /**
  40. * Type User Login Name
  41. */
  42. const TYPE_LOGIN = 2;
  43. /**
  44. * Define main table
  45. *
  46. */
  47. protected function _construct()
  48. {
  49. $this->_setMainTable('captcha/log');
  50. }
  51. /**
  52. * Save or Update count Attempts
  53. *
  54. * @param string|null $login
  55. * @return Mage_Captcha_Model_Resource_Log
  56. */
  57. public function logAttempt($login)
  58. {
  59. if ($login != null){
  60. $this->_getWriteAdapter()->insertOnDuplicate(
  61. $this->getMainTable(),
  62. array(
  63. 'type' => self::TYPE_LOGIN, 'value' => $login, 'count' => 1,
  64. 'updated_at' => Mage::getSingleton('core/date')->gmtDate()
  65. ),
  66. array('count' => new Zend_Db_Expr('count+1'), 'updated_at')
  67. );
  68. }
  69. $ip = Mage::helper('core/http')->getRemoteAddr();
  70. if ($ip != null) {
  71. $this->_getWriteAdapter()->insertOnDuplicate(
  72. $this->getMainTable(),
  73. array(
  74. 'type' => self::TYPE_REMOTE_ADDRESS, 'value' => $ip, 'count' => 1,
  75. 'updated_at' => Mage::getSingleton('core/date')->gmtDate()
  76. ),
  77. array('count' => new Zend_Db_Expr('count+1'), 'updated_at')
  78. );
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Delete User attempts by login
  84. *
  85. * @param string $login
  86. * @return Mage_Captcha_Model_Resource_Log
  87. */
  88. public function deleteUserAttempts($login)
  89. {
  90. if ($login != null) {
  91. $this->_getWriteAdapter()->delete(
  92. $this->getMainTable(),
  93. array('type = ?' => self::TYPE_LOGIN, 'value = ?' => $login)
  94. );
  95. }
  96. $ip = Mage::helper('core/http')->getRemoteAddr();
  97. if ($ip != null) {
  98. $this->_getWriteAdapter()->delete(
  99. $this->getMainTable(), array('type = ?' => self::TYPE_REMOTE_ADDRESS, 'value = ?' => $ip)
  100. );
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Get count attempts by ip
  106. *
  107. * @return null|int
  108. */
  109. public function countAttemptsByRemoteAddress()
  110. {
  111. $ip = Mage::helper('core/http')->getRemoteAddr();
  112. if (!$ip) {
  113. return 0;
  114. }
  115. $read = $this->_getReadAdapter();
  116. $select = $read->select()->from($this->getMainTable(), 'count')->where('type = ?', self::TYPE_REMOTE_ADDRESS)
  117. ->where('value = ?', $ip);
  118. return $read->fetchOne($select);
  119. }
  120. /**
  121. * Get count attempts by user login
  122. *
  123. * @param string $login
  124. * @return null|int
  125. */
  126. public function countAttemptsByUserLogin($login)
  127. {
  128. if (!$login) {
  129. return 0;
  130. }
  131. $read = $this->_getReadAdapter();
  132. $select = $read->select()->from($this->getMainTable(), 'count')->where('type = ?', self::TYPE_LOGIN)
  133. ->where('value = ?', $login);
  134. return $read->fetchOne($select);
  135. }
  136. /**
  137. * Delete attempts with expired in update_at time
  138. *
  139. * @return void
  140. */
  141. public function deleteOldAttempts()
  142. {
  143. $this->_getWriteAdapter()->delete(
  144. $this->getMainTable(),
  145. array('updated_at < ?' => Mage::getSingleton('core/date')->gmtDate(null, time() - 60*30))
  146. );
  147. }
  148. }