mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-04 20:36:03 +01:00
Use PSR-4 for plugin classes
This commit is contained in:
1968
classes/plugin/Admin.php
Normal file
1968
classes/plugin/Admin.php
Normal file
File diff suppressed because it is too large
Load Diff
1122
classes/plugin/AdminBaseController.php
Normal file
1122
classes/plugin/AdminBaseController.php
Normal file
File diff suppressed because it is too large
Load Diff
2495
classes/plugin/AdminController.php
Normal file
2495
classes/plugin/AdminController.php
Normal file
File diff suppressed because it is too large
Load Diff
430
classes/plugin/Gpm.php
Normal file
430
classes/plugin/Gpm.php
Normal file
@@ -0,0 +1,430 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Admin;
|
||||
|
||||
use Grav\Common\Cache;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\GPM\GPM as GravGPM;
|
||||
use Grav\Common\GPM\Licenses;
|
||||
use Grav\Common\GPM\Installer;
|
||||
use Grav\Common\GPM\Response;
|
||||
use Grav\Common\GPM\Upgrader;
|
||||
use Grav\Common\Filesystem\Folder;
|
||||
use Grav\Common\GPM\Common\Package;
|
||||
|
||||
/**
|
||||
* Class Gpm
|
||||
*
|
||||
* @package Grav\Plugin\Admin
|
||||
*/
|
||||
class Gpm
|
||||
{
|
||||
// Probably should move this to Grav DI container?
|
||||
/** @var GravGPM */
|
||||
protected static $GPM;
|
||||
|
||||
public static function GPM()
|
||||
{
|
||||
if (!static::$GPM) {
|
||||
static::$GPM = new GravGPM();
|
||||
}
|
||||
|
||||
return static::$GPM;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default options for the install
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $options = [
|
||||
'destination' => GRAV_ROOT,
|
||||
'overwrite' => true,
|
||||
'ignore_symlinks' => true,
|
||||
'skip_invalid' => true,
|
||||
'install_deps' => true,
|
||||
'theme' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* @param Package[]|string[]|string $packages
|
||||
* @param array $options
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function install($packages, array $options)
|
||||
{
|
||||
$options = array_merge(self::$options, $options);
|
||||
|
||||
if (!Installer::isGravInstance($options['destination']) || !Installer::isValidDestination($options['destination'],
|
||||
[Installer::EXISTS, Installer::IS_LINK])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$packages = is_array($packages) ? $packages : [$packages];
|
||||
$count = count($packages);
|
||||
|
||||
$packages = array_filter(array_map(function ($p) {
|
||||
return !is_string($p) ? $p instanceof Package ? $p : false : self::GPM()->findPackage($p);
|
||||
}, $packages));
|
||||
|
||||
if (!$options['skip_invalid'] && $count !== count($packages)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$messages = '';
|
||||
|
||||
foreach ($packages as $package) {
|
||||
if (isset($package->dependencies) && $options['install_deps']) {
|
||||
$result = static::install($package->dependencies, $options);
|
||||
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Check destination
|
||||
Installer::isValidDestination($options['destination'] . DS . $package->install_path);
|
||||
|
||||
if (!$options['overwrite'] && Installer::lastErrorCode() === Installer::EXISTS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$license = Licenses::get($package->slug);
|
||||
$local = static::download($package, $license);
|
||||
|
||||
Installer::install($local, $options['destination'],
|
||||
['install_path' => $package->install_path, 'theme' => $options['theme']]);
|
||||
Folder::delete(dirname($local));
|
||||
|
||||
$errorCode = Installer::lastErrorCode();
|
||||
if ($errorCode) {
|
||||
$msg = Installer::lastErrorMsg();
|
||||
throw new \RuntimeException($msg);
|
||||
}
|
||||
|
||||
if (count($packages) === 1) {
|
||||
$message = Installer::getMessage();
|
||||
if ($message) {
|
||||
return $message;
|
||||
}
|
||||
|
||||
$messages .= $message;
|
||||
}
|
||||
}
|
||||
|
||||
return $messages ?: true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Package[]|string[]|string $packages
|
||||
* @param array $options
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function update($packages, array $options)
|
||||
{
|
||||
$options['overwrite'] = true;
|
||||
|
||||
return static::install($packages, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Package[]|string[]|string $packages
|
||||
* @param array $options
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function uninstall($packages, array $options)
|
||||
{
|
||||
$options = array_merge(self::$options, $options);
|
||||
|
||||
$packages = (array)$packages;
|
||||
$count = count($packages);
|
||||
|
||||
$packages = array_filter(array_map(function ($p) {
|
||||
|
||||
if (is_string($p)) {
|
||||
$p = strtolower($p);
|
||||
$plugin = static::GPM()->getInstalledPlugin($p);
|
||||
$p = $plugin ?: static::GPM()->getInstalledTheme($p);
|
||||
}
|
||||
|
||||
return $p instanceof Package ? $p : false;
|
||||
|
||||
}, $packages));
|
||||
|
||||
if (!$options['skip_invalid'] && $count !== count($packages)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($packages as $package) {
|
||||
|
||||
$location = Grav::instance()['locator']->findResource($package->package_type . '://' . $package->slug);
|
||||
|
||||
// Check destination
|
||||
Installer::isValidDestination($location);
|
||||
|
||||
if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Installer::uninstall($location);
|
||||
|
||||
$errorCode = Installer::lastErrorCode();
|
||||
if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) {
|
||||
$msg = Installer::lastErrorMsg();
|
||||
throw new \RuntimeException($msg);
|
||||
}
|
||||
|
||||
if (count($packages) === 1) {
|
||||
$message = Installer::getMessage();
|
||||
if ($message) {
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct install a file
|
||||
*
|
||||
* @param string $package_file
|
||||
*
|
||||
* @return string|bool
|
||||
*/
|
||||
public static function directInstall($package_file)
|
||||
{
|
||||
if (!$package_file) {
|
||||
return Admin::translate('PLUGIN_ADMIN.NO_PACKAGE_NAME');
|
||||
}
|
||||
|
||||
$tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
|
||||
$tmp_zip = $tmp_dir . '/Grav-' . uniqid('', false);
|
||||
|
||||
if (Response::isRemote($package_file)) {
|
||||
$zip = GravGPM::downloadPackage($package_file, $tmp_zip);
|
||||
} else {
|
||||
$zip = GravGPM::copyPackage($package_file, $tmp_zip);
|
||||
}
|
||||
|
||||
if (file_exists($zip)) {
|
||||
$tmp_source = $tmp_dir . '/Grav-' . uniqid('', false);
|
||||
$extracted = Installer::unZip($zip, $tmp_source);
|
||||
|
||||
if (!$extracted) {
|
||||
Folder::delete($tmp_source);
|
||||
Folder::delete($tmp_zip);
|
||||
return Admin::translate('PLUGIN_ADMIN.PACKAGE_EXTRACTION_FAILED');
|
||||
}
|
||||
|
||||
$type = GravGPM::getPackageType($extracted);
|
||||
|
||||
if (!$type) {
|
||||
Folder::delete($tmp_source);
|
||||
Folder::delete($tmp_zip);
|
||||
return Admin::translate('PLUGIN_ADMIN.NOT_VALID_GRAV_PACKAGE');
|
||||
}
|
||||
|
||||
if ($type === 'grav') {
|
||||
Installer::isValidDestination(GRAV_ROOT . '/system');
|
||||
if (Installer::IS_LINK === Installer::lastErrorCode()) {
|
||||
Folder::delete($tmp_source);
|
||||
Folder::delete($tmp_zip);
|
||||
return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
|
||||
}
|
||||
|
||||
static::upgradeGrav($zip, $extracted);
|
||||
} else {
|
||||
$name = GravGPM::getPackageName($extracted);
|
||||
|
||||
if (!$name) {
|
||||
Folder::delete($tmp_source);
|
||||
Folder::delete($tmp_zip);
|
||||
return Admin::translate('PLUGIN_ADMIN.NAME_COULD_NOT_BE_DETERMINED');
|
||||
}
|
||||
|
||||
$install_path = GravGPM::getInstallPath($type, $name);
|
||||
$is_update = file_exists($install_path);
|
||||
|
||||
Installer::isValidDestination(GRAV_ROOT . DS . $install_path);
|
||||
if (Installer::lastErrorCode() === Installer::IS_LINK) {
|
||||
Folder::delete($tmp_source);
|
||||
Folder::delete($tmp_zip);
|
||||
return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
|
||||
}
|
||||
|
||||
Installer::install($zip, GRAV_ROOT,
|
||||
['install_path' => $install_path, 'theme' => $type === 'theme', 'is_update' => $is_update],
|
||||
$extracted);
|
||||
}
|
||||
|
||||
Folder::delete($tmp_source);
|
||||
|
||||
if (Installer::lastErrorCode()) {
|
||||
return Installer::lastErrorMsg();
|
||||
}
|
||||
|
||||
} else {
|
||||
return Admin::translate('PLUGIN_ADMIN.ZIP_PACKAGE_NOT_FOUND');
|
||||
}
|
||||
|
||||
Folder::delete($tmp_zip);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Package $package
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function download(Package $package, $license = null)
|
||||
{
|
||||
$query = '';
|
||||
|
||||
if ($package->premium) {
|
||||
$query = \json_encode(array_merge($package->premium, [
|
||||
'slug' => $package->slug,
|
||||
'filename' => $package->premium['filename'],
|
||||
'license_key' => $license
|
||||
]));
|
||||
|
||||
$query = '?d=' . base64_encode($query);
|
||||
}
|
||||
|
||||
try {
|
||||
$contents = Response::get($package->zipball_url . $query, []);
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
$tmp_dir = Admin::getTempDir() . '/Grav-' . uniqid('', false);
|
||||
Folder::mkdir($tmp_dir);
|
||||
|
||||
$bad_chars = array_merge(array_map('chr', range(0, 31)), ['<', '>', ':', '"', '/', '\\', '|', '?', '*']);
|
||||
|
||||
$filename = $package->slug . str_replace($bad_chars, '', basename($package->zipball_url));
|
||||
$filename = preg_replace('/[\\\\\/:"*?&<>|]+/m', '-', $filename);
|
||||
|
||||
file_put_contents($tmp_dir . DS . $filename . '.zip', $contents);
|
||||
|
||||
return $tmp_dir . DS . $filename . '.zip';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $package
|
||||
* @param string $tmp
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function _downloadSelfupgrade(array $package, $tmp)
|
||||
{
|
||||
$output = Response::get($package['download'], []);
|
||||
Folder::mkdir($tmp);
|
||||
file_put_contents($tmp . DS . $package['name'], $output);
|
||||
|
||||
return $tmp . DS . $package['name'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public static function selfupgrade()
|
||||
{
|
||||
$upgrader = new Upgrader();
|
||||
|
||||
if (!Installer::isGravInstance(GRAV_ROOT)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_link(GRAV_ROOT . DS . 'index.php')) {
|
||||
Installer::setError(Installer::IS_LINK);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (method_exists($upgrader, 'meetsRequirements') &&
|
||||
method_exists($upgrader, 'minPHPVersion') &&
|
||||
!$upgrader->meetsRequirements()) {
|
||||
$error = [];
|
||||
$error[] = '<p>Grav has increased the minimum PHP requirement.<br />';
|
||||
$error[] = 'You are currently running PHP <strong>' . phpversion() . '</strong>';
|
||||
$error[] = ', but PHP <strong>' . $upgrader->minPHPVersion() . '</strong> is required.</p>';
|
||||
$error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>';
|
||||
|
||||
Installer::setError(implode("\n", $error));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$update = $upgrader->getAssets()['grav-update'];
|
||||
$tmp = Admin::getTempDir() . '/Grav-' . uniqid('', false);
|
||||
if ($tmp) {
|
||||
$file = self::_downloadSelfupgrade($update, $tmp);
|
||||
$folder = Installer::unZip($file, $tmp . '/zip');
|
||||
$keepFolder = false;
|
||||
} else {
|
||||
// If you make $tmp empty, you can install your local copy of Grav (for testing purposes only).
|
||||
$file = 'grav.zip';
|
||||
$folder = '~/phpstorm/grav-clones/grav';
|
||||
//$folder = '/home/matias/phpstorm/rockettheme/grav-devtools/grav-clones/grav';
|
||||
$keepFolder = true;
|
||||
}
|
||||
|
||||
static::upgradeGrav($file, $folder, $keepFolder);
|
||||
|
||||
$errorCode = Installer::lastErrorCode();
|
||||
|
||||
if ($tmp) {
|
||||
Folder::delete($tmp);
|
||||
}
|
||||
|
||||
return !(is_string($errorCode) || ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)));
|
||||
}
|
||||
|
||||
private static function upgradeGrav($zip, $folder, $keepFolder = false)
|
||||
{
|
||||
static $ignores = [
|
||||
'backup',
|
||||
'cache',
|
||||
'images',
|
||||
'logs',
|
||||
'tmp',
|
||||
'user',
|
||||
'.htaccess',
|
||||
'robots.txt'
|
||||
];
|
||||
|
||||
if (!is_dir($folder)) {
|
||||
Installer::setError('Invalid source folder');
|
||||
}
|
||||
|
||||
try {
|
||||
$script = $folder . '/system/install.php';
|
||||
/** Install $installer */
|
||||
if ((file_exists($script) && $install = include $script) && is_callable($install)) {
|
||||
$install($zip);
|
||||
} else {
|
||||
Installer::install(
|
||||
$zip,
|
||||
GRAV_ROOT,
|
||||
['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => $ignores],
|
||||
$folder,
|
||||
$keepFolder
|
||||
);
|
||||
|
||||
Cache::clearCache();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Installer::setError($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
303
classes/plugin/Popularity.php
Normal file
303
classes/plugin/Popularity.php
Normal file
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Admin;
|
||||
|
||||
use Grav\Common\Config\Config;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
|
||||
/**
|
||||
* Class Popularity
|
||||
* @package Grav\Plugin
|
||||
*/
|
||||
class Popularity
|
||||
{
|
||||
/** @var Config */
|
||||
protected $config;
|
||||
protected $data_path;
|
||||
|
||||
protected $daily_file;
|
||||
protected $monthly_file;
|
||||
protected $totals_file;
|
||||
protected $visitors_file;
|
||||
|
||||
protected $daily_data;
|
||||
protected $monthly_data;
|
||||
protected $totals_data;
|
||||
protected $visitors_data;
|
||||
|
||||
const DAILY_FORMAT = 'd-m-Y';
|
||||
const MONTHLY_FORMAT = 'm-Y';
|
||||
const DAILY_FILE = 'daily.json';
|
||||
const MONTHLY_FILE = 'monthly.json';
|
||||
const TOTALS_FILE = 'totals.json';
|
||||
const VISITORS_FILE = 'visitors.json';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = Grav::instance()['config'];
|
||||
|
||||
$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;
|
||||
$this->visitors_file = $this->data_path . '/' . self::VISITORS_FILE;
|
||||
|
||||
}
|
||||
|
||||
public function trackHit()
|
||||
{
|
||||
// Don't track bot or crawler requests
|
||||
if (!Grav::instance()['browser']->isHuman()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Respect visitors "do not track" setting
|
||||
if (!Grav::instance()['browser']->isTrackable()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var PageInterface $page */
|
||||
$page = Grav::instance()['page'];
|
||||
$relative_url = str_replace(Grav::instance()['base_url_relative'], '', $page->url());
|
||||
|
||||
// Don't track error pages or pages that have no route
|
||||
if ($page->template() === 'error' || !$page->route()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure no 'widcard-style' ignore matches this url
|
||||
foreach ((array)$this->config->get('plugins.admin.popularity.ignore') as $ignore) {
|
||||
if (fnmatch($ignore, $relative_url)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// initial creation if it doesn't exist
|
||||
if (!file_exists($this->data_path)) {
|
||||
mkdir($this->data_path);
|
||||
$this->flushPopularity();
|
||||
}
|
||||
|
||||
// Update the data we want to track
|
||||
$this->updateDaily();
|
||||
$this->updateMonthly();
|
||||
$this->updateTotals($page->route());
|
||||
$this->updateVisitors(Grav::instance()['uri']->ip());
|
||||
|
||||
}
|
||||
|
||||
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)) {
|
||||
$this->daily_data[$day_month_year] = (int)$this->daily_data[$day_month_year] + 1;
|
||||
} else {
|
||||
$this->daily_data[$day_month_year] = 1;
|
||||
}
|
||||
|
||||
// keep correct number as set by history
|
||||
$count = (int)$this->config->get('plugins.admin.popularity.history.daily', 30);
|
||||
$total = count($this->daily_data);
|
||||
|
||||
if ($total > $count) {
|
||||
$this->daily_data = array_slice($this->daily_data, -$count, $count, true);
|
||||
}
|
||||
|
||||
file_put_contents($this->daily_file, json_encode($this->daily_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getDailyChartData()
|
||||
{
|
||||
if (!$this->daily_data) {
|
||||
$this->daily_data = $this->getData($this->daily_file);
|
||||
}
|
||||
|
||||
$limit = (int)$this->config->get('plugins.admin.popularity.dashboard.days_of_stats', 7);
|
||||
$chart_data = array_slice($this->daily_data, -$limit, $limit);
|
||||
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
/** @var Admin $admin */
|
||||
$admin = Grav::instance()['admin'];
|
||||
foreach ($chart_data as $date => $count) {
|
||||
$labels[] = $admin::translate([
|
||||
'PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]) .
|
||||
'<br>' . date('M d', strtotime($date));
|
||||
$data[] = $count;
|
||||
}
|
||||
|
||||
return ['labels' => $labels, 'data' => $data];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
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)];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getWeeklyTotal()
|
||||
{
|
||||
if (!$this->daily_data) {
|
||||
$this->daily_data = $this->getData($this->daily_file);
|
||||
}
|
||||
|
||||
$day = 0;
|
||||
$total = 0;
|
||||
foreach (array_reverse($this->daily_data) as $daily) {
|
||||
$total += $daily;
|
||||
$day++;
|
||||
if ($day === 7) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
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)];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected function updateMonthly()
|
||||
{
|
||||
|
||||
if (!$this->monthly_data) {
|
||||
$this->monthly_data = $this->getData($this->monthly_file);
|
||||
}
|
||||
|
||||
$month_year = date(self::MONTHLY_FORMAT);
|
||||
|
||||
// get the monthly access count
|
||||
if (array_key_exists($month_year, $this->monthly_data)) {
|
||||
$this->monthly_data[$month_year] = (int)$this->monthly_data[$month_year] + 1;
|
||||
} else {
|
||||
$this->monthly_data[$month_year] = 1;
|
||||
}
|
||||
|
||||
// keep correct number as set by history
|
||||
$count = (int)$this->config->get('plugins.admin.popularity.history.monthly', 12);
|
||||
$total = count($this->monthly_data);
|
||||
$this->monthly_data = array_slice($this->monthly_data, $total - $count, $count);
|
||||
|
||||
|
||||
file_put_contents($this->monthly_file, json_encode($this->monthly_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getMonthyChartData()
|
||||
{
|
||||
if (!$this->monthly_data) {
|
||||
$this->monthly_data = $this->getData($this->monthly_file);
|
||||
}
|
||||
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
foreach ($this->monthly_data as $date => $count) {
|
||||
$labels[] = date('M', strtotime($date));
|
||||
$data[] = $count;
|
||||
}
|
||||
|
||||
return ['labels' => $labels, 'data' => $data];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
*/
|
||||
protected function updateTotals($url)
|
||||
{
|
||||
if (!$this->totals_data) {
|
||||
$this->totals_data = $this->getData($this->totals_file);
|
||||
}
|
||||
|
||||
// get the totals for this url
|
||||
if (array_key_exists($url, $this->totals_data)) {
|
||||
$this->totals_data[$url] = (int)$this->totals_data[$url] + 1;
|
||||
} else {
|
||||
$this->totals_data[$url] = 1;
|
||||
}
|
||||
|
||||
file_put_contents($this->totals_file, json_encode($this->totals_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $ip
|
||||
*/
|
||||
protected function updateVisitors($ip)
|
||||
{
|
||||
if (!$this->visitors_data) {
|
||||
$this->visitors_data = $this->getData($this->visitors_file);
|
||||
}
|
||||
|
||||
// update with current timestamp
|
||||
$this->visitors_data[hash('sha1', $ip)] = time();
|
||||
$visitors = $this->visitors_data;
|
||||
arsort($visitors);
|
||||
|
||||
$count = (int)$this->config->get('plugins.admin.popularity.history.visitors', 20);
|
||||
$this->visitors_data = array_slice($visitors, 0, $count, true);
|
||||
|
||||
file_put_contents($this->visitors_file, json_encode($this->visitors_data));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getData($path)
|
||||
{
|
||||
if (file_exists($path)) {
|
||||
return (array)json_decode(file_get_contents($path), true);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
public function flushPopularity()
|
||||
{
|
||||
file_put_contents($this->daily_file, []);
|
||||
file_put_contents($this->monthly_file, []);
|
||||
file_put_contents($this->totals_file, []);
|
||||
file_put_contents($this->visitors_file, []);
|
||||
}
|
||||
}
|
||||
22
classes/plugin/Themes.php
Normal file
22
classes/plugin/Themes.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Admin;
|
||||
|
||||
/**
|
||||
* Admin theme object
|
||||
*
|
||||
* @author RocketTheme
|
||||
* @license MIT
|
||||
*/
|
||||
class Themes extends \Grav\Common\Themes
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
/** @var Themes $themes */
|
||||
$themes = $this->grav['themes'];
|
||||
$themes->configure();
|
||||
$themes->initTheme();
|
||||
|
||||
$this->grav->fireEvent('onAdminThemeInitialized');
|
||||
}
|
||||
}
|
||||
118
classes/plugin/Twig/AdminTwigExtension.php
Normal file
118
classes/plugin/Twig/AdminTwigExtension.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Admin\Twig;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
use Grav\Common\Yaml;
|
||||
use Grav\Common\Language\Language;
|
||||
|
||||
class AdminTwigExtension extends \Twig_Extension
|
||||
{
|
||||
/**
|
||||
* @var Grav
|
||||
*/
|
||||
protected $grav;
|
||||
|
||||
/**
|
||||
* @var Language $lang
|
||||
*/
|
||||
protected $lang;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->grav = Grav::instance();
|
||||
$this->lang = $this->grav['user']->language;
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFilter('tu', [$this, 'tuFilter']),
|
||||
new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']),
|
||||
new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']),
|
||||
new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
|
||||
new \Twig_SimpleFilter('nested', [$this, 'nestedFilter']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new \Twig_SimpleFunction('getPageUrl', [$this, 'getPageUrl'], ['needs_context' => true]),
|
||||
new \Twig_SimpleFunction('clone', [$this, 'cloneFunc']),
|
||||
];
|
||||
}
|
||||
|
||||
public function nestedFilter($current, $name)
|
||||
{
|
||||
$path = explode('.', trim($name, '.'));
|
||||
|
||||
foreach ($path as $field) {
|
||||
if (is_object($current) && isset($current->{$field})) {
|
||||
$current = $current->{$field};
|
||||
} elseif (is_array($current) && isset($current[$field])) {
|
||||
$current = $current[$field];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
public function cloneFunc($obj)
|
||||
{
|
||||
return clone $obj;
|
||||
}
|
||||
|
||||
public function getPageUrl($context, PageInterface $page)
|
||||
{
|
||||
$page_route = trim($page->rawRoute(), '/');
|
||||
$page_lang = $page->language();
|
||||
$base_url = $context['base_url'];
|
||||
$base_url_simple = $context['base_url_simple'];
|
||||
$admin_lang = Grav::instance()['session']->admin_lang ?: 'en';
|
||||
|
||||
if ($page_lang && $page_lang !== $admin_lang) {
|
||||
$page_url = $base_url_simple . '/' . $page_lang . '/' . $context['admin_route'] . '/pages/' . $page_route;
|
||||
} else {
|
||||
$page_url = $base_url . '/pages/' . $page_route;
|
||||
}
|
||||
|
||||
return $page_url;
|
||||
}
|
||||
|
||||
public static function tuFilter()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$numargs = count($args);
|
||||
$lang = null;
|
||||
|
||||
if (($numargs === 3 && is_array($args[1])) || ($numargs === 2 && !is_array($args[1]))) {
|
||||
$lang = array_pop($args);
|
||||
} elseif ($numargs === 2 && is_array($args[1])) {
|
||||
$subs = array_pop($args);
|
||||
$args = array_merge($args, $subs);
|
||||
}
|
||||
|
||||
return Grav::instance()['admin']->translate($args, $lang);
|
||||
}
|
||||
|
||||
public function toYamlFilter($value, $inline = null)
|
||||
{
|
||||
return Yaml::dump($value, $inline);
|
||||
|
||||
}
|
||||
|
||||
public function fromYamlFilter($value)
|
||||
{
|
||||
return Yaml::parse($value);
|
||||
}
|
||||
|
||||
public function adminNicetimeFilter($date, $long_strings = true)
|
||||
{
|
||||
return Grav::instance()['admin']->adminNiceTime($date, $long_strings);
|
||||
}
|
||||
|
||||
}
|
||||
54
classes/plugin/Utils.php
Normal file
54
classes/plugin/Utils.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Admin;
|
||||
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\User\Interfaces\UserCollectionInterface;
|
||||
use Grav\Common\User\Interfaces\UserInterface;
|
||||
|
||||
/**
|
||||
* Admin utils class
|
||||
*
|
||||
* @license MIT
|
||||
*/
|
||||
class Utils
|
||||
{
|
||||
/**
|
||||
* Matches an email to a user
|
||||
*
|
||||
* @param string $email
|
||||
*
|
||||
* @return UserInterface
|
||||
*/
|
||||
public static function findUserByEmail(string $email)
|
||||
{
|
||||
$grav = Grav::instance();
|
||||
|
||||
/** @var UserCollectionInterface $users */
|
||||
$users = $grav['accounts'];
|
||||
|
||||
return $users->find($email, ['email']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a slug of the given string
|
||||
*
|
||||
* @param string $str
|
||||
* @return string
|
||||
*/
|
||||
public static function slug(string $str)
|
||||
{
|
||||
if (function_exists('transliterator_transliterate')) {
|
||||
$str = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;', $str);
|
||||
} else {
|
||||
$str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
|
||||
}
|
||||
|
||||
$str = strtolower($str);
|
||||
$str = preg_replace('/[-\s]+/', '-', $str);
|
||||
$str = preg_replace('/[^a-z0-9-]/i', '', $str);
|
||||
$str = trim($str, '-');
|
||||
|
||||
return $str;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user