PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/www/shop/engine/Shopware/Plugins/Default/Frontend/Statistics/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 313 lines | 198 code | 26 blank | 89 comment | 37 complexity | 44bd1168b08d79bfa62ae39d5720a8d8 MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright © 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage Statistics
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author Heiner Lohaus
  30. * @author $Author$
  31. */
  32. /**
  33. * Shopware Statistics Plugin
  34. *
  35. * todo@all: Documentation
  36. */
  37. class Shopware_Plugins_Frontend_Statistics_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  38. {
  39. /**
  40. * Install plugin method
  41. *
  42. * @return bool
  43. */
  44. public function install()
  45. {
  46. $this->subscribeEvent(
  47. 'Enlight_Controller_Front_DispatchLoopShutdown',
  48. 'onDispatchLoopShutdown'
  49. );
  50. $form = $this->Form();
  51. $parent = $this->Forms()->findOneBy(array('name' => 'Core'));
  52. $form->setParent($parent);
  53. $form->setElement('text', 'blockIp', array(
  54. 'label' => 'IP von Statistiken ausschließen', 'value' => null,
  55. 'scope' => \Shopware\Models\Config\Element::SCOPE_SHOP
  56. ));
  57. return true;
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function getInfo()
  63. {
  64. return array(
  65. 'label' => 'Statistiken'
  66. );
  67. }
  68. /**
  69. * Event listener method
  70. *
  71. * @param Enlight_Controller_EventArgs $args
  72. */
  73. public function onDispatchLoopShutdown(Enlight_Controller_EventArgs $args)
  74. {
  75. $request = $args->getRequest();
  76. $response = $args->getResponse();
  77. if ($response->isException()
  78. || $request->isXmlHttpRequest()
  79. || $request->getModuleName() != 'frontend'
  80. || $request->getControllerName() == 'captcha'
  81. ) {
  82. return;
  83. }
  84. if (!Shopware()->Shop()->get('esi')) {
  85. $this->updateLog($request, $response);
  86. }
  87. }
  88. /**
  89. * Check user is bot
  90. *
  91. * @param string $userAgent
  92. * @return bool
  93. */
  94. public function checkIsBot($userAgent)
  95. {
  96. $userAgent = preg_replace('/[^a-z]/', '', strtolower($userAgent));
  97. if (empty($userAgent)) {
  98. return false;
  99. }
  100. $result = false;
  101. $bots = preg_replace('/[^a-z;]/', '', strtolower(Shopware()->Config()->botBlackList));
  102. $bots = explode(';', $bots);
  103. if (!empty($userAgent) && str_replace($bots, '', $userAgent) != $userAgent) {
  104. $result = true;
  105. }
  106. return $result;
  107. }
  108. /**
  109. * @param Enlight_Controller_Request_RequestHttp $request
  110. * @param $response
  111. */
  112. public function updateLog($request, $response)
  113. {
  114. if (Shopware()->Session()->Bot === null) {
  115. Shopware()->Session()->Bot = $this->checkIsBot($request->getHeader('USER_AGENT'));
  116. }
  117. if ($this->shouldRefreshLog($request)) {
  118. $this->cleanupStatistic();
  119. $this->refreshBasket($request);
  120. $this->refreshLog($request);
  121. $this->refreshReferer($request);
  122. $this->refreshCurrentUsers($request);
  123. $this->refreshPartner($request, $response);
  124. }
  125. }
  126. /**
  127. * @param Enlight_Controller_Request_RequestHttp $request
  128. */
  129. public function refreshBasket($request)
  130. {
  131. $currentController = $request->getParam('requestController', $request->getControllerName());
  132. $sessionId = (string) Enlight_Components_Session::getId();
  133. if (!empty($currentController) && !empty($sessionId)) {
  134. $userId = (int) Shopware()->Session()->sUserId;
  135. $userAgent = (string) $request->getServer("HTTP_USER_AGENT");
  136. $sql = "
  137. UPDATE s_order_basket
  138. SET lastviewport = ?,
  139. useragent = ?,
  140. userID = ?
  141. WHERE sessionID=?
  142. ";
  143. Shopware()->Db()->query($sql, array(
  144. $currentController, $userAgent,
  145. $userId, $sessionId
  146. ));
  147. }
  148. }
  149. /**
  150. * @param $request
  151. * @return bool
  152. */
  153. public function shouldRefreshLog($request)
  154. {
  155. if ($request->getClientIp(false) === null
  156. || !empty(Shopware()->Session()->Bot)
  157. ) {
  158. return false;
  159. }
  160. if (!empty(Shopware()->Config()->blockIp)
  161. && strpos(Shopware()->Config()->blockIp, $request->getClientIp(false)) !== false
  162. ) {
  163. return false;
  164. }
  165. return true;
  166. }
  167. /**
  168. * Cleanup statistic
  169. */
  170. public function cleanupStatistic()
  171. {
  172. if ((rand() % 10) == 0) {
  173. $sql = 'DELETE FROM s_statistics_currentusers WHERE time < DATE_SUB(NOW(), INTERVAL 3 MINUTE)';
  174. Shopware()->Db()->query($sql);
  175. $sql = 'DELETE FROM s_statistics_pool WHERE datum!=CURDATE()';
  176. Shopware()->Db()->query($sql);
  177. }
  178. }
  179. /**
  180. * Refresh current users
  181. *
  182. * @param \Enlight_Controller_Request_RequestHttp $request
  183. */
  184. public function refreshCurrentUsers($request)
  185. {
  186. $sql = 'INSERT INTO s_statistics_currentusers (`remoteaddr`, `page`, `time`, `userID`) VALUES (?, ?, NOW(), ?)';
  187. Shopware()->Db()->query($sql, array(
  188. $request->getClientIp(false),
  189. $request->getParam('requestPage', $request->getRequestUri()),
  190. empty(Shopware()->Session()->sUserId) ? 0 : (int)Shopware()->Session()->sUserId
  191. ));
  192. }
  193. /**
  194. * Refresh visitor log
  195. *
  196. * @param Enlight_Controller_Request_RequestHttp $request
  197. */
  198. public function refreshLog($request)
  199. {
  200. $ip = $request->getClientIp(false);
  201. $shopId = Shopware()->Shop()->getId();
  202. $sql = 'SELECT id FROM s_statistics_visitors WHERE datum=CURDATE() AND shopID = ?';
  203. $result = Shopware()->Db()->fetchOne($sql, array($shopId));
  204. if (empty($result)) {
  205. $sql = 'INSERT INTO s_statistics_visitors (`datum`,`shopID`, `pageimpressions`, `uniquevisits`) VALUES(NOW(),?, 1, 1)';
  206. Shopware()->Db()->query($sql, array($shopId));
  207. return;
  208. }
  209. $sql = 'SELECT id FROM s_statistics_pool WHERE datum=CURDATE() AND remoteaddr=?';
  210. $result = Shopware()->Db()->fetchOne($sql, array($ip));
  211. if (empty($result)) {
  212. $sql = 'INSERT INTO s_statistics_pool (`remoteaddr`, `datum`) VALUES (?, NOW())';
  213. Shopware()->Db()->query($sql, array($ip));
  214. $sql = 'UPDATE s_statistics_visitors SET pageimpressions=pageimpressions+1, uniquevisits=uniquevisits+1 WHERE datum=CURDATE() AND shopID = ?';
  215. Shopware()->Db()->query($sql, array($shopId));
  216. } else {
  217. $sql = 'UPDATE s_statistics_visitors SET pageimpressions=pageimpressions+1 WHERE datum=CURDATE() AND shopID = ?';
  218. Shopware()->Db()->query($sql, array($shopId));
  219. }
  220. }
  221. /**
  222. * Refresh referrer log
  223. *
  224. * @param \Enlight_Controller_Request_RequestHttp $request
  225. */
  226. public function refreshReferer($request)
  227. {
  228. $referer = $request->getHeader('Referer', $request->getParam('referer'));
  229. $partner = $request->getParam('partner', $request->getParam('sPartner'));
  230. if (empty($referer)
  231. || strpos($referer, 'http') !== 0
  232. || strpos($referer, $request->getHttpHost()) !== false
  233. ) {
  234. return;
  235. }
  236. Shopware()->Session()->sReferer = $referer;
  237. if ($partner !== null) {
  238. $referer .= '$' . $partner;
  239. }
  240. $sql = 'INSERT INTO s_statistics_referer (datum, referer) VALUES (NOW(), ?)';
  241. Shopware()->Db()->query($sql, array($referer));
  242. }
  243. /**
  244. * Refresh partner log
  245. *
  246. * @param \Enlight_Controller_Request_RequestHttp $request
  247. * @param \Enlight_Controller_Response_ResponseHttp $response
  248. */
  249. public function refreshPartner($request, $response)
  250. {
  251. $partner = $request->getParam('partner', $request->getParam('sPartner'));
  252. if ($partner !== null) {
  253. if (strpos($partner, 'sCampaign') === 0) {
  254. $campaignID = (int) str_replace('sCampaign', '', $partner);
  255. if (!empty($campaignID)) {
  256. Shopware()->Session()->sPartner = 'sCampaign' . $campaignID;
  257. $sql = '
  258. UPDATE s_campaigns_mailings
  259. SET clicked = clicked + 1
  260. WHERE id = ?
  261. ';
  262. Shopware()->Db()->query($sql, array($campaignID));
  263. }
  264. } else {
  265. $sql = 'SELECT * FROM s_emarketing_partner WHERE active=1 AND idcode=?';
  266. $row = Shopware()->Db()->fetchRow($sql, array($partner));
  267. if (!empty($row)) {
  268. if ($row['cookielifetime']) {
  269. $valid = time() + $row['cookielifetime'];
  270. } else {
  271. $valid = 0;
  272. }
  273. $response->setCookie('partner', $row['idcode'], $valid, '/');
  274. }
  275. Shopware()->Session()->sPartner = $partner;
  276. }
  277. } elseif ($request->getCookie('partner') !== null) {
  278. $sql = 'SELECT idcode FROM s_emarketing_partner WHERE active=1 AND idcode=?';
  279. $partner = Shopware()->Db()->fetchOne($sql, array($request->getCookie('partner')));
  280. if (empty($partner)) {
  281. unset(Shopware()->Session()->sPartner);
  282. } else {
  283. Shopware()->Session()->sPartner = $partner;
  284. }
  285. }
  286. }
  287. }