PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Log/Model/Visitor.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 345 lines | 196 code | 33 blank | 116 comment | 27 complexity | 928d3f4c1949c88f8ef947e47affb03d MD5 | raw file
  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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Log
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Enter description here ...
  28. *
  29. * @method Mage_Log_Model_Resource_Visitor getResource()
  30. * @method string getSessionId()
  31. * @method Mage_Log_Model_Visitor setSessionId(string $value)
  32. * @method Mage_Log_Model_Visitor setFirstVisitAt(string $value)
  33. * @method Mage_Log_Model_Visitor setLastVisitAt(string $value)
  34. * @method int getLastUrlId()
  35. * @method Mage_Log_Model_Visitor setLastUrlId(int $value)
  36. * @method int getStoreId()
  37. * @method Mage_Log_Model_Visitor setStoreId(int $value)
  38. *
  39. * @category Mage
  40. * @package Mage_Log
  41. * @author Magento Core Team <core@magentocommerce.com>
  42. */
  43. class Mage_Log_Model_Visitor extends Mage_Core_Model_Abstract
  44. {
  45. const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
  46. const VISITOR_TYPE_CUSTOMER = 'c';
  47. const VISITOR_TYPE_VISITOR = 'v';
  48. protected $_skipRequestLogging = false;
  49. /**
  50. * @var Mage_Log_Helper_Data
  51. */
  52. protected $_logCondition;
  53. /**
  54. * @var Mage_Core_Helper_Http
  55. */
  56. protected $_httpHelper;
  57. /**
  58. * @var Mage_Core_Model_Config
  59. */
  60. protected $_config;
  61. /**
  62. * @var Mage_Core_Model_Session
  63. */
  64. protected $_session;
  65. public function __construct(array $data = array())
  66. {
  67. $this->_httpHelper = !empty($data['http_helper']) ? $data['http_helper'] : Mage::helper('core/http');
  68. $this->_config = !empty($data['config']) ? $data['config'] : Mage::getConfig();
  69. $this->_logCondition = !empty($data['log_condition']) ?
  70. $data['log_condition'] : Mage::helper('log');
  71. $this->_session = !empty($data['session']) ? $data['session'] : Mage::getSingleton('core/session');
  72. parent::__construct($data);
  73. }
  74. /**
  75. * Object initialization
  76. */
  77. protected function _construct()
  78. {
  79. $this->_init('log/visitor');
  80. $userAgent = $this->_httpHelper->getHttpUserAgent();
  81. $ignoreAgents = $this->_config->getNode('global/ignore_user_agents');
  82. if ($ignoreAgents) {
  83. $ignoreAgents = $ignoreAgents->asArray();
  84. if (in_array($userAgent, $ignoreAgents)) {
  85. $this->_skipRequestLogging = true;
  86. }
  87. }
  88. if ($this->_logCondition->isLogDisabled()) {
  89. $this->_skipRequestLogging = true;
  90. }
  91. }
  92. /**
  93. * Retrieve session object
  94. *
  95. * @return Mage_Core_Model_Session_Abstract
  96. */
  97. protected function _getSession()
  98. {
  99. return $this->_session;
  100. }
  101. /**
  102. * Initialize visitor information from server data
  103. *
  104. * @return Mage_Log_Model_Visitor
  105. */
  106. public function initServerData()
  107. {
  108. $this->addData(array(
  109. 'server_addr' => $this->_httpHelper->getServerAddr(true),
  110. 'remote_addr' => $this->_httpHelper->getRemoteAddr(true),
  111. 'http_secure' => Mage::app()->getStore()->isCurrentlySecure(),
  112. 'http_host' => $this->_httpHelper->getHttpHost(true),
  113. 'http_user_agent' => $this->_httpHelper->getHttpUserAgent(true),
  114. 'http_accept_language' => $this->_httpHelper->getHttpAcceptLanguage(true),
  115. 'http_accept_charset' => $this->_httpHelper->getHttpAcceptCharset(true),
  116. 'request_uri' => $this->_httpHelper->getRequestUri(true),
  117. 'session_id' => $this->_session->getSessionId(),
  118. 'http_referer' => $this->_httpHelper->getHttpReferer(true),
  119. ));
  120. return $this;
  121. }
  122. /**
  123. * Return Online Minutes Interval
  124. *
  125. * @return int Minutes Interval
  126. */
  127. public static function getOnlineMinutesInterval()
  128. {
  129. $configValue = Mage::getStoreConfig('customer/online_customers/online_minutes_interval');
  130. return intval($configValue) > 0
  131. ? intval($configValue)
  132. : self::DEFAULT_ONLINE_MINUTES_INTERVAL;
  133. }
  134. /**
  135. * Retrieve url from model data
  136. *
  137. * @return string
  138. */
  139. public function getUrl()
  140. {
  141. $url = 'http' . ($this->getHttpSecure() ? 's' : '') . '://';
  142. $url .= $this->getHttpHost().$this->getRequestUri();
  143. return $url;
  144. }
  145. public function getFirstVisitAt()
  146. {
  147. if (!$this->hasData('first_visit_at')) {
  148. $this->setData('first_visit_at', now());
  149. }
  150. return $this->getData('first_visit_at');
  151. }
  152. public function getLastVisitAt()
  153. {
  154. if (!$this->hasData('last_visit_at')) {
  155. $this->setData('last_visit_at', now());
  156. }
  157. return $this->getData('last_visit_at');
  158. }
  159. /**
  160. * Initialization visitor information by request
  161. *
  162. * Used in event "controller_action_predispatch"
  163. *
  164. * @param Varien_Event_Observer $observer
  165. * @return Mage_Log_Model_Visitor
  166. */
  167. public function initByRequest($observer)
  168. {
  169. if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
  170. return $this;
  171. }
  172. $this->setData($this->_session->getVisitorData());
  173. $visitorId = $this->getId();
  174. if (!$visitorId) {
  175. $this->initServerData();
  176. $this->setFirstVisitAt(now());
  177. $this->setIsNewVisitor(true);
  178. $this->save();
  179. }
  180. if (!$visitorId || $this->_isVisitorSessionNew()) {
  181. Mage::dispatchEvent('visitor_init', array('visitor' => $this));
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Check is session new
  187. *
  188. * @return bool
  189. */
  190. protected function _isVisitorSessionNew()
  191. {
  192. $visitorData = $this->_session->getVisitorData();
  193. $visitorSessionId = null;
  194. if (is_array($visitorData) && isset($visitorData['session_id'])) {
  195. $visitorSessionId = $visitorData['session_id'];
  196. }
  197. return $this->_session->getSessionId() != $visitorSessionId;
  198. }
  199. /**
  200. * Saving visitor information by request
  201. *
  202. * Used in event "controller_action_postdispatch"
  203. *
  204. * @param Varien_Event_Observer $observer
  205. * @return Mage_Log_Model_Visitor
  206. */
  207. public function saveByRequest($observer)
  208. {
  209. if ($this->_skipRequestLogging || $this->isModuleIgnored($observer)) {
  210. return $this;
  211. }
  212. try {
  213. $this->setLastVisitAt(now());
  214. $this->save();
  215. $this->_session->setVisitorData($this->getData());
  216. } catch (Exception $e) {
  217. Mage::logException($e);
  218. }
  219. return $this;
  220. }
  221. /**
  222. * Bind customer data when customer login
  223. *
  224. * Used in event "customer_login"
  225. *
  226. * @param Varien_Event_Observer $observer
  227. * @return Mage_Log_Model_Visitor
  228. */
  229. public function bindCustomerLogin($observer)
  230. {
  231. if (!$this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
  232. $this->setDoCustomerLogin(true);
  233. $this->setCustomerId($customer->getId());
  234. }
  235. return $this;
  236. }
  237. /**
  238. * Bind customer data when customer logout
  239. *
  240. * Used in event "customer_logout"
  241. *
  242. * @param Varien_Event_Observer $observer
  243. * @return Mage_Log_Model_Visitor
  244. */
  245. public function bindCustomerLogout($observer)
  246. {
  247. if ($this->getCustomerId() && $customer = $observer->getEvent()->getCustomer()) {
  248. $this->setDoCustomerLogout(true);
  249. }
  250. return $this;
  251. }
  252. public function bindQuoteCreate($observer)
  253. {
  254. if ($quote = $observer->getEvent()->getQuote()) {
  255. if ($quote->getIsCheckoutCart()) {
  256. $this->setQuoteId($quote->getId());
  257. $this->setDoQuoteCreate(true);
  258. }
  259. }
  260. return $this;
  261. }
  262. public function bindQuoteDestroy($observer)
  263. {
  264. if ($quote = $observer->getEvent()->getQuote()) {
  265. $this->setDoQuoteDestroy(true);
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Methods for research (depends from customer online admin section)
  271. */
  272. public function addIpData($data)
  273. {
  274. $ipData = array();
  275. $data->setIpData($ipData);
  276. return $this;
  277. }
  278. public function addCustomerData($data)
  279. {
  280. $customerId = $data->getCustomerId();
  281. if( intval($customerId) <= 0 ) {
  282. return $this;
  283. }
  284. $customerData = Mage::getModel('customer/customer')->load($customerId);
  285. $newCustomerData = array();
  286. foreach( $customerData->getData() as $propName => $propValue ) {
  287. $newCustomerData['customer_' . $propName] = $propValue;
  288. }
  289. $data->addData($newCustomerData);
  290. return $this;
  291. }
  292. public function addQuoteData($data)
  293. {
  294. $quoteId = $data->getQuoteId();
  295. if( intval($quoteId) <= 0 ) {
  296. return $this;
  297. }
  298. $data->setQuoteData(Mage::getModel('sales/quote')->load($quoteId));
  299. return $this;
  300. }
  301. public function isModuleIgnored($observer)
  302. {
  303. $ignores = $this->_config->getNode('global/ignoredModules/entities')->asArray();
  304. if( is_array($ignores) && $observer) {
  305. $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
  306. if (isset($ignores[$curModule])) {
  307. return true;
  308. }
  309. }
  310. return false;
  311. }
  312. }