PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Connection.php

https://gitlab.com/staging06/myproject
PHP | 205 lines | 136 code | 21 blank | 48 comment | 24 complexity | 47895c801c9a1f465ba25ef5eae478a9 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 PrestaShop
  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@prestashop.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 PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2015 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class ConnectionCore extends ObjectModel
  27. {
  28. /** @var int */
  29. public $id_guest;
  30. /** @var int */
  31. public $id_page;
  32. /** @var string */
  33. public $ip_address;
  34. /** @var string */
  35. public $http_referer;
  36. /** @var int */
  37. public $id_shop;
  38. /** @var int */
  39. public $id_shop_group;
  40. /** @var string */
  41. public $date_add;
  42. /**
  43. * @see ObjectModel::$definition
  44. */
  45. public static $definition = array(
  46. 'table' => 'connections',
  47. 'primary' => 'id_connections',
  48. 'fields' => array(
  49. 'id_guest' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  50. 'id_page' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  51. 'ip_address' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
  52. 'http_referer' => array('type' => self::TYPE_STRING, 'validate' => 'isAbsoluteUrl'),
  53. 'id_shop' => array('type' => self::TYPE_INT, 'required' => true),
  54. 'id_shop_group' => array('type' => self::TYPE_INT, 'required' => true),
  55. 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate')
  56. ),
  57. );
  58. /**
  59. * @see ObjectModel::getFields()
  60. * @return array
  61. */
  62. public function getFields()
  63. {
  64. if (!$this->id_shop_group) {
  65. $this->id_shop_group = Context::getContext()->shop->id_shop_group;
  66. }
  67. $fields = parent::getFields();
  68. return $fields;
  69. }
  70. public static function setPageConnection($cookie, $full = true)
  71. {
  72. $id_page = false;
  73. // The connection is created if it does not exist yet and we get the current page id
  74. if (!isset($cookie->id_connections) || !strstr(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', Tools::getHttpHost(false, false))) {
  75. $id_page = Connection::setNewConnection($cookie);
  76. }
  77. // If we do not track the pages, no need to get the page id
  78. if (!Configuration::get('PS_STATSDATA_PAGESVIEWS') && !Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) {
  79. return array();
  80. }
  81. if (!$id_page) {
  82. $id_page = Page::getCurrentId();
  83. }
  84. // If we do not track the page views by customer, the id_page is the only information needed
  85. if (!Configuration::get('PS_STATSDATA_CUSTOMER_PAGESVIEWS')) {
  86. return array('id_page' => $id_page);
  87. }
  88. // The ending time will be updated by an ajax request when the guest will close the page
  89. $time_start = date('Y-m-d H:i:s');
  90. Db::getInstance()->insert('connections_page', array(
  91. 'id_connections' => (int)$cookie->id_connections,
  92. 'id_page' => (int)$id_page,
  93. 'time_start' => $time_start
  94. ), false, true, Db::INSERT_IGNORE);
  95. // This array is serialized and used by the ajax request to identify the page
  96. return array(
  97. 'id_connections' => (int)$cookie->id_connections,
  98. 'id_page' => (int)$id_page,
  99. 'time_start' => $time_start);
  100. }
  101. public static function setNewConnection($cookie)
  102. {
  103. if (isset($_SERVER['HTTP_USER_AGENT'])
  104. && preg_match('/BotLink|ahoy|AlkalineBOT|anthill|appie|arale|araneo|AraybOt|ariadne|arks|ATN_Worldwide|Atomz|bbot|Bjaaland|Ukonline|borg\-bot\/0\.9|boxseabot|bspider|calif|christcrawler|CMC\/0\.01|combine|confuzzledbot|CoolBot|cosmos|Internet Cruiser Robot|cusco|cyberspyder|cydralspider|desertrealm, desert realm|digger|DIIbot|grabber|downloadexpress|DragonBot|dwcp|ecollector|ebiness|elfinbot|esculapio|esther|fastcrawler|FDSE|FELIX IDE|ESI|fido|H�m�h�kki|KIT\-Fireball|fouineur|Freecrawl|gammaSpider|gazz|gcreep|golem|googlebot|griffon|Gromit|gulliver|gulper|hambot|havIndex|hotwired|htdig|iajabot|INGRID\/0\.1|Informant|InfoSpiders|inspectorwww|irobot|Iron33|JBot|jcrawler|Teoma|Jeeves|jobo|image\.kapsi\.net|KDD\-Explorer|ko_yappo_robot|label\-grabber|larbin|legs|Linkidator|linkwalker|Lockon|logo_gif_crawler|marvin|mattie|mediafox|MerzScope|NEC\-MeshExplorer|MindCrawler|udmsearch|moget|Motor|msnbot|muncher|muninn|MuscatFerret|MwdSearch|sharp\-info\-agent|WebMechanic|NetScoop|newscan\-online|ObjectsSearch|Occam|Orbsearch\/1\.0|packrat|pageboy|ParaSite|patric|pegasus|perlcrawler|phpdig|piltdownman|Pimptrain|pjspider|PlumtreeWebAccessor|PortalBSpider|psbot|Getterrobo\-Plus|Raven|RHCS|RixBot|roadrunner|Robbie|robi|RoboCrawl|robofox|Scooter|Search\-AU|searchprocess|Senrigan|Shagseeker|sift|SimBot|Site Valet|skymob|SLCrawler\/2\.0|slurp|ESI|snooper|solbot|speedy|spider_monkey|SpiderBot\/1\.0|spiderline|nil|suke|http:\/\/www\.sygol\.com|tach_bw|TechBOT|templeton|titin|topiclink|UdmSearch|urlck|Valkyrie libwww\-perl|verticrawl|Victoria|void\-bot|Voyager|VWbot_K|crawlpaper|wapspider|WebBandit\/1\.0|webcatcher|T\-H\-U\-N\-D\-E\-R\-S\-T\-O\-N\-E|WebMoose|webquest|webreaper|webs|webspider|WebWalker|wget|winona|whowhere|wlm|WOLP|WWWC|none|XGET|Nederland\.zoek|AISearchBot|woriobot|NetSeer|Nutch|YandexBot/i', $_SERVER['HTTP_USER_AGENT'])) {
  105. // This is a bot and we have to retrieve its connection ID
  106. $sql = 'SELECT SQL_NO_CACHE `id_connections` FROM `'._DB_PREFIX_.'connections`
  107. WHERE ip_address = '.(int)ip2long(Tools::getRemoteAddr()).'
  108. AND `date_add` > \''.pSQL(date('Y-m-d H:i:00', time() - 1800)).'\'
  109. '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'
  110. ORDER BY `date_add` DESC';
  111. if ($id_connections = Db::getInstance()->getValue($sql, false)) {
  112. $cookie->id_connections = (int)$id_connections;
  113. return Page::getCurrentId();
  114. }
  115. }
  116. // A new connection is created if the guest made no actions during 30 minutes
  117. $sql = 'SELECT SQL_NO_CACHE `id_guest`
  118. FROM `'._DB_PREFIX_.'connections`
  119. WHERE `id_guest` = '.(int)$cookie->id_guest.'
  120. AND `date_add` > \''.pSQL(date('Y-m-d H:i:00', time() - 1800)).'\'
  121. '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'
  122. ORDER BY `date_add` DESC';
  123. $result = Db::getInstance()->getRow($sql, false);
  124. if (!$result['id_guest'] && (int)$cookie->id_guest) {
  125. // The old connections details are removed from the database in order to spare some memory
  126. Connection::cleanConnectionsPages();
  127. $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
  128. $array_url = parse_url($referer);
  129. if (!isset($array_url['host']) || preg_replace('/^www./', '', $array_url['host']) == preg_replace('/^www./', '', Tools::getHttpHost(false, false))) {
  130. $referer = '';
  131. }
  132. $connection = new Connection();
  133. $connection->id_guest = (int)$cookie->id_guest;
  134. $connection->id_page = Page::getCurrentId();
  135. $connection->ip_address = Tools::getRemoteAddr() ? (int)ip2long(Tools::getRemoteAddr()) : '';
  136. $connection->id_shop = Context::getContext()->shop->id;
  137. $connection->id_shop_group = Context::getContext()->shop->id_shop_group;
  138. $connection->date_add = $cookie->date_add;
  139. if (Validate::isAbsoluteUrl($referer)) {
  140. $connection->http_referer = substr($referer, 0, 254);
  141. }
  142. $connection->add();
  143. $cookie->id_connections = $connection->id;
  144. return $connection->id_page;
  145. }
  146. }
  147. public static function setPageTime($id_connections, $id_page, $time_start, $time)
  148. {
  149. if (!Validate::isUnsignedId($id_connections)
  150. || !Validate::isUnsignedId($id_page)
  151. || !Validate::isDate($time_start)) {
  152. return;
  153. }
  154. // Limited to 5 minutes because more than 5 minutes is considered as an error
  155. if ($time > 300000) {
  156. $time = 300000;
  157. }
  158. Db::getInstance()->execute('
  159. UPDATE `'._DB_PREFIX_.'connections_page`
  160. SET `time_end` = `time_start` + INTERVAL '.(int)($time / 1000).' SECOND
  161. WHERE `id_connections` = '.(int)$id_connections.'
  162. AND `id_page` = '.(int)$id_page.'
  163. AND `time_start` = \''.pSQL($time_start).'\'');
  164. }
  165. public static function cleanConnectionsPages()
  166. {
  167. $period = Configuration::get('PS_STATS_OLD_CONNECT_AUTO_CLEAN');
  168. if ($period === 'week') {
  169. $interval = '1 WEEK';
  170. } elseif ($period === 'month') {
  171. $interval = '1 MONTH';
  172. } elseif ($period === 'year') {
  173. $interval = '1 YEAR';
  174. } else {
  175. return;
  176. }
  177. if ($interval != null) {
  178. // Records of connections details older than the beginning of the specified interval are deleted
  179. Db::getInstance()->execute('
  180. DELETE FROM `'._DB_PREFIX_.'connections_page`
  181. WHERE time_start < LAST_DAY(DATE_SUB(NOW(), INTERVAL '.$interval.'))');
  182. }
  183. }
  184. }