| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-03-20 12:52:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-07 09:54:10 -07:00
										 |  |  | namespace Grav\Plugin\Admin; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  | use Grav\Common\Config\Config; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  | use Grav\Common\Page\Interfaces\PageInterface; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class Popularity | 
					
						
							|  |  |  |  * @package Grav\Plugin | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | class Popularity | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-10-01 22:28:16 +03:00
										 |  |  |     /** @var Config */ | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected $config; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     protected $data_path; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |     protected $daily_file; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected $monthly_file; | 
					
						
							|  |  |  |     protected $totals_file; | 
					
						
							|  |  |  |     protected $visitors_file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |     protected $daily_data; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected $monthly_data; | 
					
						
							|  |  |  |     protected $totals_data; | 
					
						
							|  |  |  |     protected $visitors_data; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |     const DAILY_FORMAT = 'd-m-Y'; | 
					
						
							|  |  |  |     const MONTHLY_FORMAT = 'm-Y'; | 
					
						
							|  |  |  |     const DAILY_FILE = 'daily.json'; | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |     const MONTHLY_FILE = 'monthly.json'; | 
					
						
							|  |  |  |     const TOTALS_FILE = 'totals.json'; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     const VISITORS_FILE = 'visitors.json'; | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  |         $this->config = Grav::instance()['config']; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $this->data_path     = Grav::instance()['locator']->findResource('log://popularity', true, true); | 
					
						
							|  |  |  |         $this->daily_file    = $this->data_path . '/' . self::DAILY_FILE; | 
					
						
							|  |  |  |         $this->monthly_file  = $this->data_path . '/' . self::MONTHLY_FILE; | 
					
						
							|  |  |  |         $this->totals_file   = $this->data_path . '/' . self::TOTALS_FILE; | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $this->visitors_file = $this->data_path . '/' . self::VISITORS_FILE; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function trackHit() | 
					
						
							| 
									
										
										
										
											2015-10-06 14:56:26 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-09-30 18:46:53 +02:00
										 |  |  |         // Don't track bot or crawler requests
 | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  |         if (!Grav::instance()['browser']->isHuman()) { | 
					
						
							| 
									
										
										
										
											2015-09-30 18:46:53 +02:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 01:50:15 +01:00
										 |  |  |         // Respect visitors "do not track" setting
 | 
					
						
							|  |  |  |         if (!Grav::instance()['browser']->isTrackable()) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-05 15:46:55 +02:00
										 |  |  |         /** @var PageInterface $page */ | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $page         = Grav::instance()['page']; | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  |         $relative_url = str_replace(Grav::instance()['base_url_relative'], '', $page->url()); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |         // Don't track error pages or pages that have no route
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         if ($page->template() === 'error' || !$page->route()) { | 
					
						
							| 
									
										
										
										
											2014-09-06 18:13:04 -06:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |         // Make sure no 'widcard-style' ignore matches this url
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         foreach ((array)$this->config->get('plugins.admin.popularity.ignore') as $ignore) { | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |             if (fnmatch($ignore, $relative_url)) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |         // initial creation if it doesn't exist
 | 
					
						
							|  |  |  |         if (!file_exists($this->data_path)) { | 
					
						
							|  |  |  |             mkdir($this->data_path); | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |             $this->flushPopularity(); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |         // Update the data we want to track
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         $this->updateDaily(); | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         $this->updateMonthly(); | 
					
						
							|  |  |  |         $this->updateTotals($page->route()); | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  |         $this->updateVisitors(Grav::instance()['uri']->ip()); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |     protected function updateDaily() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$this->daily_data) { | 
					
						
							|  |  |  |             $this->daily_data = $this->getData($this->daily_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $day_month_year = date(self::DAILY_FORMAT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // get the daily access count
 | 
					
						
							|  |  |  |         if (array_key_exists($day_month_year, $this->daily_data)) { | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             $this->daily_data[$day_month_year] = (int)$this->daily_data[$day_month_year] + 1; | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $this->daily_data[$day_month_year] = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // keep correct number as set by history
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         $count = (int)$this->config->get('plugins.admin.popularity.history.daily', 30); | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         $total = count($this->daily_data); | 
					
						
							| 
									
										
										
										
											2015-07-24 17:31:29 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if ($total > $count) { | 
					
						
							|  |  |  |             $this->daily_data = array_slice($this->daily_data, -$count, $count, true); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         file_put_contents($this->daily_file, json_encode($this->daily_data)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     public function getDailyChartData() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->daily_data) { | 
					
						
							|  |  |  |             $this->daily_data = $this->getData($this->daily_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         $limit      = (int)$this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7); | 
					
						
							| 
									
										
										
										
											2015-07-24 17:31:29 -06:00
										 |  |  |         $chart_data = array_slice($this->daily_data, -$limit, $limit); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $labels = []; | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $data   = []; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |         /** @var Admin $admin */ | 
					
						
							|  |  |  |         $admin = Grav::instance()['admin']; | 
					
						
							| 
									
										
										
										
											2015-07-24 17:31:29 -06:00
										 |  |  |         foreach ($chart_data as $date => $count) { | 
					
						
							| 
									
										
										
										
											2018-12-05 08:20:38 +02:00
										 |  |  |             $labels[] = $admin::translate([ | 
					
						
							| 
									
										
										
										
											2017-04-29 17:21:57 +02:00
										 |  |  |                 'PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]) . | 
					
						
							|  |  |  |                 '<br>' . date('M d', strtotime($date)); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |             $data[]   = $count; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         return ['labels' => $labels, 'data' => $data]; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     public function getDailyTotal() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->daily_data) { | 
					
						
							|  |  |  |             $this->daily_data = $this->getData($this->daily_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (isset($this->daily_data[date(self::DAILY_FORMAT)])) { | 
					
						
							|  |  |  |             return $this->daily_data[date(self::DAILY_FORMAT)]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     public function getWeeklyTotal() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->daily_data) { | 
					
						
							|  |  |  |             $this->daily_data = $this->getData($this->daily_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $day   = 0; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |         $total = 0; | 
					
						
							| 
									
										
										
										
											2015-10-06 14:56:26 +02:00
										 |  |  |         foreach (array_reverse($this->daily_data) as $daily) { | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |             $total += $daily; | 
					
						
							| 
									
										
										
										
											2015-10-06 14:56:26 +02:00
										 |  |  |             $day++; | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             if ($day === 7) { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $total; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return int | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     public function getMonthlyTotal() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->monthly_data) { | 
					
						
							|  |  |  |             $this->monthly_data = $this->getData($this->monthly_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (isset($this->monthly_data[date(self::MONTHLY_FORMAT)])) { | 
					
						
							|  |  |  |             return $this->monthly_data[date(self::MONTHLY_FORMAT)]; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected function updateMonthly() | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |     { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         if (!$this->monthly_data) { | 
					
						
							|  |  |  |             $this->monthly_data = $this->getData($this->monthly_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         $month_year = date(self::MONTHLY_FORMAT); | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         // get the monthly access count
 | 
					
						
							|  |  |  |         if (array_key_exists($month_year, $this->monthly_data)) { | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             $this->monthly_data[$month_year] = (int)$this->monthly_data[$month_year] + 1; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             $this->monthly_data[$month_year] = 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         // keep correct number as set by history
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         $count              = (int)$this->config->get('plugins.admin.popularity.history.monthly', 12); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $total              = count($this->monthly_data); | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |         $this->monthly_data = array_slice($this->monthly_data, $total - $count, $count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         file_put_contents($this->monthly_file, json_encode($this->monthly_data)); | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     protected function getMonthyChartData() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!$this->monthly_data) { | 
					
						
							|  |  |  |             $this->monthly_data = $this->getData($this->monthly_file); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $labels = []; | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $data   = []; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($this->monthly_data as $date => $count) { | 
					
						
							| 
									
										
										
										
											2015-07-24 17:31:29 -06:00
										 |  |  |             $labels[] = date('M', strtotime($date)); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |             $data[]   = $count; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return ['labels' => $labels, 'data' => $data]; | 
					
						
							| 
									
										
										
										
											2014-09-08 18:32:13 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $url | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected function updateTotals($url) | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         if (!$this->totals_data) { | 
					
						
							|  |  |  |             $this->totals_data = $this->getData($this->totals_file); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-07 09:02:39 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         // get the totals for this url
 | 
					
						
							|  |  |  |         if (array_key_exists($url, $this->totals_data)) { | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             $this->totals_data[$url] = (int)$this->totals_data[$url] + 1; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |             $this->totals_data[$url] = 1; | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         file_put_contents($this->totals_file, json_encode($this->totals_data)); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $ip | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected function updateVisitors($ip) | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         if (!$this->visitors_data) { | 
					
						
							|  |  |  |             $this->visitors_data = $this->getData($this->visitors_file); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         // update with current timestamp
 | 
					
						
							| 
									
										
										
										
											2018-05-15 11:47:46 -06:00
										 |  |  |         $this->visitors_data[hash('sha1', $ip)] = time(); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $visitors                 = $this->visitors_data; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |         arsort($visitors); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         $count               = (int)$this->config->get('plugins.admin.popularity.history.visitors', 20); | 
					
						
							| 
									
										
										
										
											2014-12-12 16:08:07 -07:00
										 |  |  |         $this->visitors_data = array_slice($visitors, 0, $count, true); | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         file_put_contents($this->visitors_file, json_encode($this->visitors_data)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param string $path | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     protected function getData($path) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-12-11 21:49:06 -07:00
										 |  |  |         if (file_exists($path)) { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             return (array)json_decode(file_get_contents($path), true); | 
					
						
							| 
									
										
										
										
											2015-12-11 21:49:06 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return []; | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-08 12:07:35 -06:00
										 |  |  |     public function flushPopularity() | 
					
						
							| 
									
										
										
										
											2014-09-07 19:58:04 -06:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         file_put_contents($this->daily_file, []); | 
					
						
							|  |  |  |         file_put_contents($this->monthly_file, []); | 
					
						
							|  |  |  |         file_put_contents($this->totals_file, []); | 
					
						
							|  |  |  |         file_put_contents($this->visitors_file, []); | 
					
						
							| 
									
										
										
										
											2014-09-06 16:59:58 -06:00
										 |  |  |     } | 
					
						
							|  |  |  | } |