Delete Simple Stats and create Visits stats
This commit is contained in:
parent
231a500a52
commit
3c97a31b1b
21 changed files with 205 additions and 489 deletions
1
bl-plugins/simple-stats/css/chartist.min.css
vendored
1
bl-plugins/simple-stats/css/chartist.min.css
vendored
File diff suppressed because one or more lines are too long
|
@ -1,36 +0,0 @@
|
|||
.simple-stats-plugin .ct-perfect-fourth {
|
||||
height: 250px;
|
||||
}
|
||||
.simple-stats-plugin .ct-series-a .ct-line {
|
||||
stroke: #4586d4;
|
||||
stroke-width: 2px;
|
||||
}
|
||||
|
||||
.simple-stats-plugin .ct-series-a .ct-point {
|
||||
stroke: #4586d4;
|
||||
stroke-width: 8px;
|
||||
}
|
||||
|
||||
.simple-stats-plugin .ct-series-b .ct-line {
|
||||
stroke: #777777;
|
||||
stroke-width: 2px;
|
||||
}
|
||||
|
||||
.simple-stats-plugin .ct-series-b .ct-point {
|
||||
stroke: #777777;
|
||||
stroke-width: 8px;
|
||||
}
|
||||
|
||||
.simple-stats-plugin p.legends {
|
||||
font-size: 0.8em;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.simple-stats-plugin p.visits-today {
|
||||
color: #4586d4;
|
||||
}
|
||||
|
||||
.simple-stats-plugin p.unique-today {
|
||||
color: #777777;
|
||||
}
|
9
bl-plugins/simple-stats/js/chartist.min.js
vendored
9
bl-plugins/simple-stats/js/chartist.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,14 +0,0 @@
|
|||
{
|
||||
"chart": "Diagram",
|
||||
"disk-usage": "Disk forbrug",
|
||||
"exclude-administrators-users": "Vis ikke brugere med rollen 'Administrator'",
|
||||
"plugin-data": {
|
||||
"description": "Vis antallet af besøg samt sidevisninger pr. dag på dit kontrolpanel, og se også andet indholdsstatistik.",
|
||||
"name": "Simple Stats"
|
||||
},
|
||||
"show-content-stats": "Vis indholdsstatistikker",
|
||||
"table": "Tabel",
|
||||
"unique-visitors-today": "Unikke besøgende i dag",
|
||||
"visits": "Besøg",
|
||||
"visits-today": "Besøg i dag"
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Simple Stats",
|
||||
"description": "Anzeige der täglichen Anzahl Besuche und Seitenaufrufe und Statistiken der Inhalte."
|
||||
},
|
||||
"visits": "Seitenaufrufe",
|
||||
"visits-today": "Seitenaufrufe heute",
|
||||
"unique-visitors-today": "Besuche heute",
|
||||
"chart": "Diagramm",
|
||||
"table": "Tabelle",
|
||||
"disk-usage" : "Belegter Speicherplatz",
|
||||
"show-content-stats" : "Zeige Statistiken der Inhalte.",
|
||||
"exclude-administrators-users" : "Benutzer mit der Rolle 'Administrator' ausschliessen."
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Simple Stats",
|
||||
"description": "Anzeige der täglichen Anzahl Besuche und Seitenaufrufe und Statistiken der Inhalte."
|
||||
},
|
||||
"visits": "Seitenaufrufe",
|
||||
"visits-today": "Seitenaufrufe heute",
|
||||
"unique-visitors-today": "Besuche heute",
|
||||
"chart": "Diagramm",
|
||||
"table": "Tabelle",
|
||||
"disk-usage" : "Belegter Speicherplatz",
|
||||
"show-content-stats" : "Zeige Statistiken der Inhalte.",
|
||||
"exclude-administrators-users" : "Benutzer mit der Rolle 'Administrator' ausschließen."
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Simple Stats",
|
||||
"description": "Show the number of visits and page views per day on your dashboard and content stats."
|
||||
},
|
||||
"visits": "Visits",
|
||||
"visits-today": "Visits today",
|
||||
"unique-visitors-today": "Unique visitors today",
|
||||
"chart": "Chart",
|
||||
"table": "Table",
|
||||
"disk-usage" : "Disk Usage",
|
||||
"show-content-stats" : "Show Content Stats",
|
||||
"exclude-administrators-users" : "Exclude users with role 'Administrator'"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Simple Stats",
|
||||
"description": "Muestra el número de visitantes por día en tu panel."
|
||||
},
|
||||
"visits": "Visitas",
|
||||
"visits-today": "Visitas de hoy",
|
||||
"unique-visitors-today": "Visitantes únicos de hoy",
|
||||
"chart": "Gráfico",
|
||||
"table": "Tabla",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "آمار ساده",
|
||||
"description": "نمایش روزانه بازدیدکنندگان در پیش خوان شما."
|
||||
},
|
||||
"visits": "بازدیدها",
|
||||
"visits-today": "بازدیدهای امروز",
|
||||
"unique-visitors-today": "بازدید کنندگان منحصر به فرد امروز",
|
||||
"chart": "چارت",
|
||||
"table": "جدول",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Statistiques",
|
||||
"description": "Afficher le nombre de visiteurs par jour sur votre tableau de bord."
|
||||
},
|
||||
"visits": "Visites",
|
||||
"visits-today": "Visites du jour",
|
||||
"unique-visitors-today": "Visiteurs uniques du jour",
|
||||
"chart": "Graphique",
|
||||
"table": "Tableau",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Statistiche semplici",
|
||||
"description": "Mostra il numero di vivitatori per giorno sulla tua dashboard."
|
||||
},
|
||||
"visits": "Visite",
|
||||
"visits-today": "Visite oggi",
|
||||
"unique-visitors-today": "Visitatori unici oggi",
|
||||
"chart": "Grafico",
|
||||
"table": "Tabella",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Simple Stats",
|
||||
"description": "Show the number of visitors per day on your dashboard."
|
||||
},
|
||||
"visits": "Visits",
|
||||
"visits-today": "Visits today",
|
||||
"unique-visitors-today": "Unique visitors today",
|
||||
"chart": "Chart",
|
||||
"table": "テーブル",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Statistieken",
|
||||
"description": "Toont het aantal bezoeken per dag op het dashboard."
|
||||
},
|
||||
"visits": "Aantal bezoeken",
|
||||
"visits-today": "Aantal bezoeken vandaag",
|
||||
"unique-visitors-today": "Unieke bezoekers vandaag",
|
||||
"chart": "Grafiek",
|
||||
"table": "Tabel",
|
||||
"disk-usage" : "Schijfgebruik",
|
||||
"show-content-stats" : "Statistieken over inhoud weergeven",
|
||||
"exclude-administrators-users" : "Gebruikers met de rol 'Beheerder' niet opnemen in statistieken"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Простая статистика",
|
||||
"description": "Показывает количество посетителей в панели управления."
|
||||
},
|
||||
"visits": "Посещения",
|
||||
"visits-today": "Посещений сегодня",
|
||||
"unique-visitors-today": "Уникальных посетителей сегодня",
|
||||
"chart": "Диаграмма",
|
||||
"table": "Таблица",
|
||||
"disk-usage" : "Использование диска"
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Basit İstatistikler",
|
||||
"description": "Yönetici panelinde günlük ziyaretçi sayısını gösterir."
|
||||
},
|
||||
"visits": "Ziyaretler",
|
||||
"visits-today": "Bugün yapılan ziyaretler",
|
||||
"unique-visitors-today": "Bugün yapılan benzersiz ziyaretler",
|
||||
"chart": "Grafik",
|
||||
"table": "Tablo",
|
||||
"disk-usage" : "Disk Usage"
|
||||
}
|
|
@ -1,275 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
This plugin uses the javascript library https://github.com/gionkunz/chartist-js
|
||||
*/
|
||||
class pluginSimpleStats extends Plugin {
|
||||
|
||||
private $loadOnController = array(
|
||||
'dashboard'
|
||||
);
|
||||
|
||||
public function init()
|
||||
{
|
||||
global $L;
|
||||
|
||||
// Fields and default values for the database of this plugin
|
||||
$this->dbFields = array(
|
||||
'label'=>$L->g('Visits'),
|
||||
'numberOfDays'=>7,
|
||||
'excludeAdmins'=>false,
|
||||
'showContentStats'=>false
|
||||
);
|
||||
}
|
||||
|
||||
public function form()
|
||||
{
|
||||
global $L;
|
||||
|
||||
$html = '<div class="alert alert-primary" role="alert">';
|
||||
$html .= $this->description();
|
||||
$html .= '</div>';
|
||||
|
||||
$html .= '<div>';
|
||||
$html .= '<label>'.$L->get('Label').'</label>';
|
||||
$html .= '<input id="jslabel" name="label" type="text" value="'.$this->getValue('label').'">';
|
||||
$html .= '<span class="tip">'.$L->get('This title is almost always used in the sidebar of the site').'</span>';
|
||||
$html .= '</div>';
|
||||
|
||||
$html .= '<div>';
|
||||
$html .= '<label>'.$L->get('Show Content Stats').'</label>';
|
||||
$html .= '<select name="showContentStats">';
|
||||
$html .= '<option value="true" '.($this->getValue('showContentStats')===true?'selected':'').'>'.$L->get('Enabled').'</option>';
|
||||
$html .= '<option value="false" '.($this->getValue('showContentStats')===false?'selected':'').'>'.$L->get('Disabled').'</option>';
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';
|
||||
|
||||
if (defined('BLUDIT_PRO')) {
|
||||
$html .= '<div>';
|
||||
$html .= '<label>'.$L->get('Exclude administrators users').'</label>';
|
||||
$html .= '<select name="excludeAdmins">';
|
||||
$html .= '<option value="true" '.($this->getValue('excludeAdmins')===true?'selected':'').'>'.$L->get('Enabled').'</option>';
|
||||
$html .= '<option value="false" '.($this->getValue('excludeAdmins')===false?'selected':'').'>'.$L->get('Disabled').'</option>';
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function adminHead()
|
||||
{
|
||||
if (!in_array($GLOBALS['ADMIN_CONTROLLER'], $this->loadOnController)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Include plugin's CSS files
|
||||
$html = $this->includeCSS('chartist.min.css');
|
||||
$html .= $this->includeCSS('style.css');
|
||||
|
||||
// Include plugin's Javascript files
|
||||
$html .= $this->includeJS('chartist.min.js');
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function dashboard()
|
||||
{
|
||||
global $L;
|
||||
$label = $this->getValue('label');
|
||||
$currentDate = Date::current('Y-m-d');
|
||||
$visitsToday = $this->visits($currentDate);
|
||||
$uniqueVisitors = $this->uniqueVisitors($currentDate);
|
||||
|
||||
$html = <<<EOF
|
||||
<div class="simple-stats-plugin">
|
||||
<div class="my-4 pt-4 border-top">
|
||||
<div class="ct-chart ct-perfect-fourth"></div>
|
||||
<p class="legends visits-today">{$L->g('Visits today')}: $visitsToday</p>
|
||||
<p class="legends unique-today">{$L->g('Unique visitors today')}: $uniqueVisitors</p>
|
||||
</div>
|
||||
</div>
|
||||
EOF;
|
||||
|
||||
$numberOfDays = $this->getValue('numberOfDays');
|
||||
$numberOfDays = $numberOfDays - 1;
|
||||
for ($i=$numberOfDays; $i >= 0 ; $i--) {
|
||||
$dateWithOffset = Date::currentOffset('Y-m-d', '-'.$i.' day');
|
||||
$visits[$i] = $this->visits($dateWithOffset);
|
||||
$unique[$i] = $this->uniqueVisitors($dateWithOffset);
|
||||
$days[$i] = Date::format($dateWithOffset, 'Y-m-d', 'D');
|
||||
}
|
||||
|
||||
$labels = "'" . implode("','", $days) . "'";
|
||||
$seriesVisits = implode(',', $visits);
|
||||
$seriesUnique = implode(',', $unique);
|
||||
|
||||
$script = <<<EOF
|
||||
<script>
|
||||
var data = {
|
||||
labels: [$labels],
|
||||
series: [
|
||||
[$seriesVisits],
|
||||
[$seriesUnique]
|
||||
]
|
||||
};
|
||||
|
||||
var options = {
|
||||
height: 250,
|
||||
axisY: {
|
||||
onlyInteger: true,
|
||||
}
|
||||
};
|
||||
|
||||
new Chartist.Line('.ct-chart', data, options);
|
||||
</script>
|
||||
EOF;
|
||||
|
||||
/**
|
||||
* Optional Content Stats Feature
|
||||
*/
|
||||
if ($this->getValue('showContentStats')) {
|
||||
global $pages, $categories, $tags;
|
||||
|
||||
$data['title'] = $L->get('Statistics');
|
||||
$data['tabTitleChart'] = $L->get('Chart');
|
||||
$data['tabTitleTable'] = $L->get('Table');
|
||||
$data['data'][$L->get('published')] = count($pages->getPublishedDB());
|
||||
$data['data'][$L->get('static')] = count($pages->getStaticDB());
|
||||
$data['data'][$L->get('drafts')] = count($pages->getDraftDB());
|
||||
$data['data'][$L->get('scheduled')] = count($pages->getScheduledDB());
|
||||
$data['data'][$L->get('sticky')] = count($pages->getStickyDB());
|
||||
$data['data'][$L->get('categories')] = count($categories->keys());
|
||||
$data['data'][$L->get('tags')] = count($tags->keys());
|
||||
$html .= $this->renderContentStatistics($data);
|
||||
}
|
||||
|
||||
$this->deleteOldLogs();
|
||||
|
||||
return $html.PHP_EOL.$script.PHP_EOL;
|
||||
}
|
||||
|
||||
public function siteBodyEnd()
|
||||
{
|
||||
$this->addVisitor();
|
||||
}
|
||||
|
||||
// Keep only 7 days of logs
|
||||
public function deleteOldLogs()
|
||||
{
|
||||
$logs = Filesystem::listFiles($this->workspace(), '*', 'log', true);
|
||||
$remove = array_slice($logs, 7);
|
||||
|
||||
foreach ($remove as $log) {
|
||||
Filesystem::rmfile($log);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the amount of visits by date
|
||||
public function visits($date)
|
||||
{
|
||||
$file = $this->workspace().$date.'.log';
|
||||
$handle = @fopen($file, 'rb');
|
||||
if ($handle===false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The amount of visits are the number of lines on the file
|
||||
$lines = 0;
|
||||
while (!feof($handle)) {
|
||||
$lines += substr_count(fread($handle, 8192), PHP_EOL);
|
||||
}
|
||||
@fclose($handle);
|
||||
return $lines;
|
||||
}
|
||||
|
||||
// Returns the amount of unique visitors by date
|
||||
public function uniqueVisitors($date)
|
||||
{
|
||||
$file = $this->workspace().$date.'.log';
|
||||
$lines = @file($file);
|
||||
if (empty($lines)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$tmp = array();
|
||||
foreach ($lines as $line) {
|
||||
$data = json_decode($line);
|
||||
$hashIP = $data[0];
|
||||
$tmp[$hashIP] = true;
|
||||
}
|
||||
return count($tmp);
|
||||
}
|
||||
|
||||
// Add a line to the current log
|
||||
// The line is a json array with the hash IP of the visitor and the time
|
||||
public function addVisitor()
|
||||
{
|
||||
if (Cookie::get('BLUDIT-KEY') && defined('BLUDIT_PRO') && $this->getValue('excludeAdmins')) {
|
||||
return false;
|
||||
}
|
||||
$currentTime = Date::current('Y-m-d H:i:s');
|
||||
$ip = TCP::getIP();
|
||||
$hashIP = md5($ip);
|
||||
|
||||
$line = json_encode(array($hashIP, $currentTime));
|
||||
$currentDate = Date::current('Y-m-d');
|
||||
$logFile = $this->workspace().$currentDate.'.log';
|
||||
|
||||
return file_put_contents($logFile, $line.PHP_EOL, FILE_APPEND | LOCK_EX)!==false;
|
||||
}
|
||||
|
||||
public function renderContentStatistics($data)
|
||||
{
|
||||
global $L;
|
||||
$diskUsage = Filesystem::bytesToHumanFileSize(
|
||||
Filesystem::getSize(PATH_ROOT)
|
||||
);
|
||||
|
||||
$html = '<div class="my-5 pt-4 border-top">';
|
||||
$html .= "<h4 class='pb-2'>{$data['title']}</h4>";
|
||||
$html .= '
|
||||
<nav>
|
||||
<div class="nav nav-tabs" id="nav-tab" role="tablist">
|
||||
<a class="nav-item nav-link active" id="nav-stats-chart-tab" data-toggle="tab" href="#nav-stats-chart" role="tab" aria-controls="nav-stats-chart" aria-selected="true">' . $data['tabTitleChart'] .'</a>
|
||||
<a class="nav-item nav-link" id="nav-stats-table-tab" data-toggle="tab" href="#nav-stats-table" role="tab" aria-controls="nav-stats-table" aria-selected="false">' . $data['tabTitleTable'] .'</a>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="tab-content my-2" id="nav-tabContent">
|
||||
<div class="tab-pane fade show active" id="nav-stats-chart" role="tabpanel" aria-labelledby="nav-stats-chart-tab">
|
||||
<div class="ct-chart-content pt-2"></div>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="nav-stats-table" role="tabpanel" aria-labelledby="nav-stats-table-tab">
|
||||
<table class="table table-borderless table-sm table-striped mt-3">
|
||||
<tbody>';
|
||||
|
||||
$html .= "<tr><th>{$L->get('disk-usage')}</th><td>$diskUsage</td></tr>";
|
||||
foreach ($data['data'] as $th => $td) {
|
||||
$html .= "
|
||||
<tr>
|
||||
<th>$th</th>
|
||||
<td>$td</td>
|
||||
</tr>
|
||||
";
|
||||
}
|
||||
|
||||
$html .= '
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script>
|
||||
new Chartist.Bar(".ct-chart-content", {
|
||||
labels: ' . json_encode(array_keys($data['data'])) . ',
|
||||
series: ' . json_encode(array_values($data['data'])) . '
|
||||
}, {
|
||||
distributeSeries: true
|
||||
});
|
||||
</script>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
}
|
1
bl-plugins/visits-stats/css/chart.min.css
vendored
Normal file
1
bl-plugins/visits-stats/css/chart.min.css
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
@keyframes chartjs-render-animation{from{opacity:.99}to{opacity:1}}.chartjs-render-monitor{animation:chartjs-render-animation 1ms}.chartjs-size-monitor,.chartjs-size-monitor-expand,.chartjs-size-monitor-shrink{position:absolute;direction:ltr;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1}.chartjs-size-monitor-expand>div{position:absolute;width:1000000px;height:1000000px;left:0;top:0}.chartjs-size-monitor-shrink>div{position:absolute;width:200%;height:200%;left:0;top:0}
|
7
bl-plugins/visits-stats/js/chart.bundle.min.js
vendored
Normal file
7
bl-plugins/visits-stats/js/chart.bundle.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
11
bl-plugins/visits-stats/languages/en.json
Normal file
11
bl-plugins/visits-stats/languages/en.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"plugin-data":
|
||||
{
|
||||
"name": "Visits Stats",
|
||||
"description": "Shows the number of visits and unique visitors."
|
||||
},
|
||||
"visits": "Visits",
|
||||
"visitors": "Visitors",
|
||||
"unique-visitors": "Unique visitors",
|
||||
"exclude-administrators-users" : "Exclude users with role 'Administrator'"
|
||||
}
|
|
@ -2,9 +2,10 @@
|
|||
"author": "Bludit",
|
||||
"email": "",
|
||||
"website": "https://plugins.bludit.com",
|
||||
"version": "3.13.1",
|
||||
"releaseDate": "2020-07-28",
|
||||
"version": "4.0.0",
|
||||
"releaseDate": "2021-03-02",
|
||||
"license": "MIT",
|
||||
"compatible": "3.13.1",
|
||||
"compatible": "4.0",
|
||||
"type": "dashboard",
|
||||
"notes": ""
|
||||
}
|
182
bl-plugins/visits-stats/plugin.php
Normal file
182
bl-plugins/visits-stats/plugin.php
Normal file
|
@ -0,0 +1,182 @@
|
|||
<?php
|
||||
|
||||
class pluginVisitsStats extends Plugin {
|
||||
|
||||
private $loadOnController = array(
|
||||
'dashboard' // Load this plugin only in the Dashboard
|
||||
);
|
||||
|
||||
public function init()
|
||||
{
|
||||
global $L;
|
||||
$this->dbFields = array(
|
||||
'label'=>$L->g('Visits'),
|
||||
'excludeAdmins'=>false
|
||||
);
|
||||
}
|
||||
|
||||
public function adminHead()
|
||||
{
|
||||
if (!in_array($GLOBALS['ADMIN_CONTROLLER'], $this->loadOnController)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$html = $this->includeCSS('chart.min.css');
|
||||
$html .= $this->includeJS('chart.bundle.min.js');
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function dashboard()
|
||||
{
|
||||
global $L;
|
||||
$label = $this->getValue('label');
|
||||
|
||||
$currentDate = Date::current('Y-m-d');
|
||||
$visitsToday = $this->visits($currentDate);
|
||||
$uniqueVisitors = $this->uniqueVisitors($currentDate);
|
||||
|
||||
$numberOfDays = 6;
|
||||
for ($i=$numberOfDays; $i >= 0 ; $i--) {
|
||||
$dateWithOffset = Date::currentOffset('Y-m-d', '-'.$i.' day');
|
||||
$visits[$i] = $this->visits($dateWithOffset);
|
||||
$unique[$i] = $this->uniqueVisitors($dateWithOffset);
|
||||
$days[$i] = Date::format($dateWithOffset, 'Y-m-d', 'D');
|
||||
}
|
||||
|
||||
$labels = "'" . implode("','", $days) . "'";
|
||||
$seriesVisits = implode(',', $visits);
|
||||
$seriesUnique = implode(',', $unique);
|
||||
|
||||
$labelVisits = $L->g('Visits');
|
||||
$labelUnique = $L->g('Unique');
|
||||
|
||||
return <<<EOF
|
||||
<div class="simple-stats-plugin">
|
||||
<h3>$label</h3>
|
||||
<canvas id="simple-stats-chart"></canvas>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var ctx = document.getElementById('simple-stats-chart');
|
||||
new Chart(ctx, {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: [$labels],
|
||||
datasets: [{
|
||||
label: '$labelUnique',
|
||||
data: [$seriesUnique]
|
||||
},
|
||||
{
|
||||
label: '$labelVisits',
|
||||
data: [$seriesVisits]
|
||||
}]
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
yAxes: [{
|
||||
ticks: {
|
||||
beginAtZero: true,
|
||||
stepSize: 1
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
EOF;
|
||||
}
|
||||
|
||||
// Plugin form for settings
|
||||
public function form()
|
||||
{
|
||||
global $L;
|
||||
|
||||
$html = '<div class="mb-3">';
|
||||
$html .= '<label class="form-label" for="label">'.$L->get('Label').'</label>';
|
||||
$html .= '<input class="form-control" id="label" name="label" type="text" value="'.$this->getValue('label').'">';
|
||||
$html .= '<div class="form-text">'.$L->get('This title is almost always used in the sidebar of the site').'</div>';
|
||||
$html .= '</div>';
|
||||
|
||||
if (defined('BLUDIT_PRO')) {
|
||||
$html .= '<div class="mb-3">';
|
||||
$html .= '<label class="form-label" for="excludeAdmins">'.$L->get('Exclude administrators users').'</label>';
|
||||
$html .= '<select class="form-select" id="excludeAdmins" name="excludeAdmins">';
|
||||
$html .= '<option value="true" '.($this->getValue('excludeAdmins')===true?'selected':'').'>'.$L->get('Enabled').'</option>';
|
||||
$html .= '<option value="false" '.($this->getValue('excludeAdmins')===false?'selected':'').'>'.$L->get('Disabled').'</option>';
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function siteBodyEnd()
|
||||
{
|
||||
$this->addVisitor();
|
||||
}
|
||||
|
||||
// Delete old logs
|
||||
public function deleteOldLogs()
|
||||
{
|
||||
$logs = Filesystem::listFiles($this->workspace(), '*', 'log', true);
|
||||
// Keep only 7 days of logs
|
||||
$remove = array_slice($logs, 7);
|
||||
foreach ($remove as $log) {
|
||||
Filesystem::rmfile($log);
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the number of visits by date
|
||||
public function visits($date)
|
||||
{
|
||||
$file = $this->workspace().$date.'.log';
|
||||
$handle = @fopen($file, 'rb');
|
||||
if ($handle===false) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The amount of visits are the number of lines on the file
|
||||
$lines = 0;
|
||||
while (!feof($handle)) {
|
||||
$lines += substr_count(fread($handle, 8192), PHP_EOL);
|
||||
}
|
||||
@fclose($handle);
|
||||
return $lines;
|
||||
}
|
||||
|
||||
// Returns the number of unique visitors by date
|
||||
public function uniqueVisitors($date)
|
||||
{
|
||||
$file = $this->workspace().$date.'.log';
|
||||
$lines = @file($file);
|
||||
if (empty($lines)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$tmp = array();
|
||||
foreach ($lines as $line) {
|
||||
$data = json_decode($line);
|
||||
$hashIP = $data[0];
|
||||
$tmp[$hashIP] = true;
|
||||
}
|
||||
return count($tmp);
|
||||
}
|
||||
|
||||
// Add a line to the current log
|
||||
// The line is a json array with the hash IP of the visitor and the time
|
||||
public function addVisitor()
|
||||
{
|
||||
if (Cookie::get('BLUDIT-KEY') && defined('BLUDIT_PRO') && $this->getValue('excludeAdmins')) {
|
||||
return false;
|
||||
}
|
||||
$currentTime = Date::current('Y-m-d H:i:s');
|
||||
$ip = TCP::getIP();
|
||||
$hashIP = md5($ip);
|
||||
|
||||
$line = json_encode(array($hashIP, $currentTime));
|
||||
$currentDate = Date::current('Y-m-d');
|
||||
$logFile = $this->workspace().$currentDate.'.log';
|
||||
|
||||
return file_put_contents($logFile, $line.PHP_EOL, FILE_APPEND | LOCK_EX)!==false;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue