1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Captcha_Model_Resource_Log extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39:
40: const TYPE_REMOTE_ADDRESS = 1;
41:
42: 43: 44:
45: const TYPE_LOGIN = 2;
46:
47: 48: 49: 50:
51: protected function _construct()
52: {
53: $this->_setMainTable('captcha/log');
54: }
55:
56: 57: 58: 59: 60: 61:
62: public function logAttempt($login)
63: {
64: if ($login != null){
65: $this->_getWriteAdapter()->insertOnDuplicate(
66: $this->getMainTable(),
67: array(
68: 'type' => self::TYPE_LOGIN, 'value' => $login, 'count' => 1,
69: 'updated_at' => Mage::getSingleton('core/date')->gmtDate()
70: ),
71: array('count' => new Zend_Db_Expr('count+1'), 'updated_at')
72: );
73: }
74: $ip = Mage::helper('core/http')->getRemoteAddr();
75: if ($ip != null) {
76: $this->_getWriteAdapter()->insertOnDuplicate(
77: $this->getMainTable(),
78: array(
79: 'type' => self::TYPE_REMOTE_ADDRESS, 'value' => $ip, 'count' => 1,
80: 'updated_at' => Mage::getSingleton('core/date')->gmtDate()
81: ),
82: array('count' => new Zend_Db_Expr('count+1'), 'updated_at')
83: );
84: }
85: return $this;
86: }
87:
88: 89: 90: 91: 92: 93:
94: public function deleteUserAttempts($login)
95: {
96: if ($login != null) {
97: $this->_getWriteAdapter()->delete(
98: $this->getMainTable(),
99: array('type = ?' => self::TYPE_LOGIN, 'value = ?' => $login)
100: );
101: }
102: $ip = Mage::helper('core/http')->getRemoteAddr();
103: if ($ip != null) {
104: $this->_getWriteAdapter()->delete(
105: $this->getMainTable(), array('type = ?' => self::TYPE_REMOTE_ADDRESS, 'value = ?' => $ip)
106: );
107: }
108:
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: public function countAttemptsByRemoteAddress()
118: {
119: $ip = Mage::helper('core/http')->getRemoteAddr();
120: if (!$ip) {
121: return 0;
122: }
123: $read = $this->_getReadAdapter();
124: $select = $read->select()->from($this->getMainTable(), 'count')->where('type = ?', self::TYPE_REMOTE_ADDRESS)
125: ->where('value = ?', $ip);
126: return $read->fetchOne($select);
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function countAttemptsByUserLogin($login)
136: {
137: if (!$login) {
138: return 0;
139: }
140: $read = $this->_getReadAdapter();
141: $select = $read->select()->from($this->getMainTable(), 'count')->where('type = ?', self::TYPE_LOGIN)
142: ->where('value = ?', $login);
143: return $read->fetchOne($select);
144: }
145:
146: 147: 148: 149: 150:
151: public function deleteOldAttempts()
152: {
153: $this->_getWriteAdapter()->delete(
154: $this->getMainTable(),
155: array('updated_at < ?' => Mage::getSingleton('core/date')->gmtDate(null, time() - 60*30))
156: );
157: }
158: }
159: