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: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45:
46: class Mage_Log_Model_Visitor extends Mage_Core_Model_Abstract
47: {
48: const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
49: const VISITOR_TYPE_CUSTOMER = 'c';
50: const VISITOR_TYPE_VISITOR = 'v';
51:
52: protected $_skipRequestLogging = false;
53:
54: 55: 56:
57: protected function _construct()
58: {
59: $this->_init('log/visitor');
60: $userAgent = Mage::helper('core/http')->getHttpUserAgent();
61: $ignoreAgents = Mage::getConfig()->getNode('global/ignore_user_agents');
62: if ($ignoreAgents) {
63: $ignoreAgents = $ignoreAgents->asArray();
64: if (in_array($userAgent, $ignoreAgents)) {
65: $this->_skipRequestLogging = true;
66: }
67: }
68: }
69:
70: 71: 72: 73: 74:
75: protected function _getSession()
76: {
77: return Mage::getSingleton('core/session');
78: }
79:
80: 81: 82: 83: 84:
85: public function initServerData()
86: {
87:
88: $helper = Mage::helper('core/http');
89:
90: $this->addData(array(
91: 'server_addr' => $helper->getServerAddr(true),
92: 'remote_addr' => $helper->getRemoteAddr(true),
93: 'http_secure' => Mage::app()->getStore()->isCurrentlySecure(),
94: 'http_host' => $helper->getHttpHost(true),
95: 'http_user_agent' => $helper->getHttpUserAgent(true),
96: 'http_accept_language' => $helper->getHttpAcceptLanguage(true),
97: 'http_accept_charset' => $helper->getHttpAcceptCharset(true),
98: 'request_uri' => $helper->getRequestUri(true),
99: 'session_id' => $this->_getSession()->getSessionId(),
100: 'http_referer' => $helper->getHttpReferer(true),
101: ));
102:
103: return $this;
104: }
105:
106: 107: 108: 109: 110:
111: public static function getOnlineMinutesInterval()
112: {
113: $configValue = Mage::getStoreConfig('customer/online_customers/online_minutes_interval');
114: return intval($configValue) > 0
115: ? intval($configValue)
116: : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
117: }
118:
119: 120: 121: 122: 123:
124: public function getUrl()
125: {
126: $url = 'http' . ($this->getHttpSecure() ? 's' : '') . '://';
127: $url .= $this->getHttpHost().$this->getRequestUri();
128: return $url;
129: }
130:
131: public function getFirstVisitAt()
132: {
133: if (!$this->hasData('first_visit_at')) {
134: $this->setData('first_visit_at', now());
135: }
136: return $this->getData('first_visit_at');
137: }
138:
139: public function getLastVisitAt()
140: {
141: if (!$this->hasData('last_visit_at')) {
142: $this->setData('last_visit_at', now());
143: }
144: return $this->getData('last_visit_at');
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154:
155: public function initByRequest($observer)
156: {
157: if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
158: return $this;
159: }
160:
161: $this->setData($this->_getSession()->getVisitorData());
162: $this->initServerData();
163:
164: if (!$this->getId()) {
165: $this->setFirstVisitAt(now());
166: $this->setIsNewVisitor(true);
167: $this->save();
168: Mage::dispatchEvent('visitor_init', array('visitor' => $this));
169: }
170: return $this;
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180:
181: public function saveByRequest($observer)
182: {
183: if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
184: return $this;
185: }
186:
187: try {
188: $this->setLastVisitAt(now());
189: $this->save();
190: $this->_getSession()->setVisitorData($this->getData());
191: } catch (Exception $e) {
192: Mage::logException($e);
193: }
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202: 203: 204:
205: public function bindCustomerLogin($observer)
206: {
207: if (!$this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
208: $this->setDoCustomerLogin(true);
209: $this->setCustomerId($customer->getId());
210: }
211: return $this;
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221:
222: public function bindCustomerLogout($observer)
223: {
224: if ($this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
225: $this->setDoCustomerLogout(true);
226: }
227: return $this;
228: }
229:
230: public function bindQuoteCreate($observer)
231: {
232: if ($quote = $observer->getEvent()->getQuote()) {
233: if ($quote->getIsCheckoutCart()) {
234: $this->setQuoteId($quote->getId());
235: $this->setDoQuoteCreate(true);
236: }
237: }
238: return $this;
239: }
240:
241: public function bindQuoteDestroy($observer)
242: {
243: if ($quote = $observer->getEvent()->getQuote()) {
244: $this->setDoQuoteDestroy(true);
245: }
246: return $this;
247: }
248:
249: 250: 251:
252: public function addIpData($data)
253: {
254: $ipData = array();
255: $data->setIpData($ipData);
256: return $this;
257: }
258:
259: public function addCustomerData($data)
260: {
261: $customerId = $data->getCustomerId();
262: if( intval($customerId) <= 0 ) {
263: return $this;
264: }
265: $customerData = Mage::getModel('customer/customer')->load($customerId);
266: $newCustomerData = array();
267: foreach( $customerData->getData() as $propName => $propValue ) {
268: $newCustomerData['customer_' . $propName] = $propValue;
269: }
270:
271: $data->addData($newCustomerData);
272: return $this;
273: }
274:
275: public function addQuoteData($data)
276: {
277: $quoteId = $data->getQuoteId();
278: if( intval($quoteId) <= 0 ) {
279: return $this;
280: }
281: $data->setQuoteData(Mage::getModel('sales/quote')->load($quoteId));
282: return $this;
283: }
284:
285: public function isModuleIgnored($observer)
286: {
287: $ignores = Mage::getConfig()->getNode('global/ignoredModules/entities')->asArray();
288:
289: if( is_array($ignores) && $observer) {
290: $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
291: if (isset($ignores[$curModule])) {
292: return true;
293: }
294: }
295: return false;
296: }
297: }
298: