-
Notifications
You must be signed in to change notification settings - Fork 0
/
Visitor.php
345 lines (312 loc) · 10.1 KB
/
Visitor.php
1
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Log
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Enter description here ...
*
* @method Mage_Log_Model_Resource_Visitor getResource()
* @method string getSessionId()
* @method Mage_Log_Model_Visitor setSessionId(string $value)
* @method Mage_Log_Model_Visitor setFirstVisitAt(string $value)
* @method Mage_Log_Model_Visitor setLastVisitAt(string $value)
* @method int getLastUrlId()
* @method Mage_Log_Model_Visitor setLastUrlId(int $value)
* @method int getStoreId()
* @method Mage_Log_Model_Visitor setStoreId(int $value)
*
* @category Mage
* @package Mage_Log
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Log_Model_Visitor extends Mage_Core_Model_Abstract
{
const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
const VISITOR_TYPE_CUSTOMER = 'c';
const VISITOR_TYPE_VISITOR = 'v';
protected $_skipRequestLogging = false;
/**
* @var Mage_Log_Helper_Data
*/
protected $_logCondition;
/**
* @var Mage_Core_Helper_Http
*/
protected $_httpHelper;
/**
* @var Mage_Core_Model_Config
*/
protected $_config;
/**
* @var Mage_Core_Model_Session
*/
protected $_session;
public function __construct(array $data = array())
{
$this->_httpHelper = !empty($data['http_helper']) ? $data['http_helper'] : Mage::helper('core/http');
$this->_config = !empty($data['config']) ? $data['config'] : Mage::getConfig();
$this->_logCondition = !empty($data['log_condition']) ?
$data['log_condition'] : Mage::helper('log');
$this->_session = !empty($data['session']) ? $data['session'] : Mage::getSingleton('core/session');
parent::__construct($data);
}
/**
* Object initialization
*/
protected function _construct()
{
$this->_init('log/visitor');
$userAgent = $this->_httpHelper->getHttpUserAgent();
$ignoreAgents = $this->_config->getNode('global/ignore_user_agents');
if ($ignoreAgents) {
$ignoreAgents = $ignoreAgents->asArray();
if (in_array($userAgent, $ignoreAgents)) {
$this->_skipRequestLogging = true;
}
}
if ($this->_logCondition->isLogDisabled()) {
$this->_skipRequestLogging = true;
}
}
/**
* Retrieve session object
*
* @return Mage_Core_Model_Session_Abstract
*/
protected function _getSession()
{
return $this->_session;
}
/**
* Initialize visitor information from server data
*
* @return Mage_Log_Model_Visitor
*/
public function initServerData()
{
$this->addData(array(
'server_addr' => $this->_httpHelper->getServerAddr(true),
'remote_addr' => $this->_httpHelper->getRemoteAddr(true),
'http_secure' => Mage::app()->getStore()->isCurrentlySecure(),
'http_host' => $this->_httpHelper->getHttpHost(true),
'http_user_agent' => $this->_httpHelper->getHttpUserAgent(true),
'http_accept_language' => $this->_httpHelper->getHttpAcceptLanguage(true),
'http_accept_charset' => $this->_httpHelper->getHttpAcceptCharset(true),
'request_uri' => $this->_httpHelper->getRequestUri(true),
'session_id' => $this->_session->getSessionId(),
'http_referer' => $this->_httpHelper->getHttpReferer(true),
));
return $this;
}
/**
* Return Online Minutes Interval
*
* @return int Minutes Interval
*/
public static function getOnlineMinutesInterval()
{
$configValue = Mage::getStoreConfig('customer/online_customers/online_minutes_interval');
return intval($configValue) > 0
? intval($configValue)
: self::DEFAULT_ONLINE_MINUTES_INTERVAL;
}
/**
* Retrieve url from model data
*
* @return string
*/
public function getUrl()
{
$url = 'http' . ($this->getHttpSecure() ? 's' : '') . '://';
$url .= $this->getHttpHost().$this->getRequestUri();
return $url;
}
public function getFirstVisitAt()
{
if (!$this->hasData('first_visit_at')) {
$this->setData('first_visit_at', now());
}
return $this->getData('first_visit_at');
}
public function getLastVisitAt()
{
if (!$this->hasData('last_visit_at')) {
$this->setData('last_visit_at', now());
}
return $this->getData('last_visit_at');
}
/**
* Initialization visitor information by request
*
* Used in event "controller_action_predispatch"
*
* @param Varien_Event_Observer $observer
* @return Mage_Log_Model_Visitor
*/
public function initByRequest($observer)
{
if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
return $this;
}
$this->setData($this->_session->getVisitorData());
$visitorId = $this->getId();
if (!$visitorId) {
$this->initServerData();
$this->setFirstVisitAt(now());
$this->setIsNewVisitor(true);
$this->save();
}
if (!$visitorId || $this->_isVisitorSessionNew()) {
Mage::dispatchEvent('visitor_init', array('visitor' => $this));
}
return $this;
}
/**
* Check is session new
*
* @return bool
*/
protected function _isVisitorSessionNew()
{
$visitorData = $this->_session->getVisitorData();
$visitorSessionId = null;
if (is_array($visitorData) && isset($visitorData['session_id'])) {
$visitorSessionId = $visitorData['session_id'];
}
return $this->_session->getSessionId() != $visitorSessionId;
}
/**
* Saving visitor information by request
*
* Used in event "controller_action_postdispatch"
*
* @param Varien_Event_Observer $observer
* @return Mage_Log_Model_Visitor
*/
public function saveByRequest($observer)
{
if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
return $this;
}
try {
$this->setLastVisitAt(now());
$this->save();
$this->_session->setVisitorData($this->getData());
} catch (Exception $e) {
Mage::logException($e);
}
return $this;
}
/**
* Bind customer data when customer login
*
* Used in event "customer_login"
*
* @param Varien_Event_Observer $observer
* @return Mage_Log_Model_Visitor
*/
public function bindCustomerLogin($observer)
{
if (!$this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
$this->setDoCustomerLogin(true);
$this->setCustomerId($customer->getId());
}
return $this;
}
/**
* Bind customer data when customer logout
*
* Used in event "customer_logout"
*
* @param Varien_Event_Observer $observer
* @return Mage_Log_Model_Visitor
*/
public function bindCustomerLogout($observer)
{
if ($this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
$this->setDoCustomerLogout(true);
}
return $this;
}
public function bindQuoteCreate($observer)
{
if ($quote = $observer->getEvent()->getQuote()) {
if ($quote->getIsCheckoutCart()) {
$this->setQuoteId($quote->getId());
$this->setDoQuoteCreate(true);
}
}
return $this;
}
public function bindQuoteDestroy($observer)
{
if ($quote = $observer->getEvent()->getQuote()) {
$this->setDoQuoteDestroy(true);
}
return $this;
}
/**
* Methods for research (depends from customer online admin section)
*/
public function addIpData($data)
{
$ipData = array();
$data->setIpData($ipData);
return $this;
}
public function addCustomerData($data)
{
$customerId = $data->getCustomerId();
if( intval($customerId) <= 0 ) {
return $this;
}
$customerData = Mage::getModel('customer/customer')->load($customerId);
$newCustomerData = array();
foreach( $customerData->getData() as $propName => $propValue ) {
$newCustomerData['customer_' . $propName] = $propValue;
}
$data->addData($newCustomerData);
return $this;
}
public function addQuoteData($data)
{
$quoteId = $data->getQuoteId();
if( intval($quoteId) <= 0 ) {
return $this;
}
$data->setQuoteData(Mage::getModel('sales/quote')->load($quoteId));
return $this;
}
public function isModuleIgnored($observer)
{
$ignores = $this->_config->getNode('global/ignoredModules/entities')->asArray();
if( is_array($ignores) && $observer) {
$curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
if (isset($ignores[$curModule])) {
return true;
}
}
return false;
}
}