Fixed some issues found by phpstan, remove deprecated method calls (User)

This commit is contained in:
Matias Griese
2019-03-20 12:52:16 +02:00
parent 2847f8a10a
commit 6bc73a32c1
15 changed files with 182 additions and 165 deletions

View File

@@ -1,6 +1,8 @@
<?php <?php
namespace Grav\Plugin; namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Cache;
use Grav\Common\Debugger; use Grav\Common\Debugger;
use Grav\Common\File\CompiledYamlFile; use Grav\Common\File\CompiledYamlFile;
use Grav\Common\Grav; use Grav\Common\Grav;
@@ -13,8 +15,8 @@ use Grav\Common\Page\Pages;
use Grav\Common\Plugin; use Grav\Common\Plugin;
use Grav\Common\Session; use Grav\Common\Session;
use Grav\Common\Uri; use Grav\Common\Uri;
use Grav\Common\User\Interfaces\UserCollectionInterface;
use Grav\Common\Utils; use Grav\Common\Utils;
use Grav\Common\User\User;
use Grav\Framework\Session\Exceptions\SessionException; use Grav\Framework\Session\Exceptions\SessionException;
use Grav\Plugin\Admin\Admin; use Grav\Plugin\Admin\Admin;
use Grav\Plugin\Admin\Popularity; use Grav\Plugin\Admin\Popularity;
@@ -31,56 +33,37 @@ class AdminPlugin extends Plugin
'blueprints' => 1000, 'blueprints' => 1000,
]; ];
/** /** @var bool */
* @var bool
*/
protected $active = false; protected $active = false;
/** /** @var string */
* @var string
*/
protected $template; protected $template;
/** /** @var string */
* @var string
*/
protected $theme; protected $theme;
/** /** @var string */
* @var string
*/
protected $route; protected $route;
/** /** @var string */
* @var string
*/
protected $admin_route; protected $admin_route;
/** /** @var Uri */
* @var Uri
*/
protected $uri; protected $uri;
/** /** @var Admin */
* @var Admin
*/
protected $admin; protected $admin;
/** /** @var Session */
* @var Session
*/
protected $session; protected $session;
/** /** @var Popularity */
* @var Popularity
*/
protected $popularity; protected $popularity;
/** /** @var string */
* @var string
*/
protected $base; protected $base;
/** @var string */
protected $version; protected $version;
/** /**
@@ -90,6 +73,7 @@ class AdminPlugin extends Plugin
{ {
return [ return [
'onPluginsInitialized' => [ 'onPluginsInitialized' => [
['autoload', 100001],
['setup', 100000], ['setup', 100000],
['onPluginsInitialized', 1001] ['onPluginsInitialized', 1001]
], ],
@@ -130,6 +114,16 @@ class AdminPlugin extends Plugin
]; ];
} }
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
/** /**
* [onPluginsInitialized:100000] * [onPluginsInitialized:100000]
* *
@@ -138,13 +132,6 @@ class AdminPlugin extends Plugin
*/ */
public function setup() public function setup()
{ {
// Autoloader
spl_autoload_register(function ($class) {
if (Utils::startsWith($class, 'Grav\Plugin\Admin')) {
require_once __DIR__ .'/classes/' . strtolower(basename(str_replace("\\", '/', $class))) . '.php';
}
});
$route = $this->config->get('plugins.admin.route'); $route = $this->config->get('plugins.admin.route');
if (!$route) { if (!$route) {
return; return;
@@ -197,10 +184,11 @@ class AdminPlugin extends Plugin
// Only activate admin if we're inside the admin path. // Only activate admin if we're inside the admin path.
if ($this->active) { if ($this->active) {
// Store this version. // Store this version.
$this->version = $this->getBlueprint()->version; $this->version = $this->getBlueprint()->get('version');
// Have a unique Admin-only Cache key // Have a unique Admin-only Cache key
if (method_exists($this->grav['cache'], 'setKey')) { if (method_exists($this->grav['cache'], 'setKey')) {
/** @var Cache $cache */
$cache = $this->grav['cache']; $cache = $this->grav['cache'];
$cache_key = $cache->getKey(); $cache_key = $cache->getKey();
$cache->setKey($cache_key . '$'); $cache->setKey($cache_key . '$');
@@ -311,8 +299,11 @@ class AdminPlugin extends Plugin
$data['state'] = 'enabled'; $data['state'] = 'enabled';
$data['access'] = ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]]; $data['access'] = ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]];
/** @var UserCollectionInterface $users */
$users = $this->grav['users'];
// Create user object and save it // Create user object and save it
$user = User::load($username); $user = $users->load($username);
$user->update($data); $user->update($data);
$user->save(); $user->save();

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin\Twig; namespace Grav\Plugin\Admin\Twig;
use Grav\Common\Grav; use Grav\Common\Grav;

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use DateTime; use DateTime;
@@ -15,8 +16,10 @@ use Grav\Common\Page\Page;
use Grav\Common\Page\Pages; use Grav\Common\Page\Pages;
use Grav\Common\Plugins; use Grav\Common\Plugins;
use Grav\Common\Security; use Grav\Common\Security;
use Grav\Common\Session;
use Grav\Common\Themes; use Grav\Common\Themes;
use Grav\Common\Uri; use Grav\Common\Uri;
use Grav\Common\User\Interfaces\UserCollectionInterface;
use Grav\Common\User\User; use Grav\Common\User\User;
use Grav\Common\Utils; use Grav\Common\Utils;
use Grav\Framework\Collection\ArrayCollection; use Grav\Framework\Collection\ArrayCollection;
@@ -29,7 +32,6 @@ use RocketTheme\Toolbox\File\JsonFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceIterator; use RocketTheme\Toolbox\ResourceLocator\UniformResourceIterator;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator; use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
use RocketTheme\Toolbox\Session\Message; use RocketTheme\Toolbox\Session\Message;
use RocketTheme\Toolbox\Session\Session;
use Grav\Common\Yaml; use Grav\Common\Yaml;
use Composer\Semver\Semver; use Composer\Semver\Semver;
use PicoFeed\Reader\Reader; use PicoFeed\Reader\Reader;
@@ -41,82 +43,67 @@ class Admin
const MEDIA_PAGINATION_INTERVAL = 20; const MEDIA_PAGINATION_INTERVAL = 20;
const TMP_COOKIE_NAME = 'tmp-admin-message'; const TMP_COOKIE_NAME = 'tmp-admin-message';
/** /** @var Grav */
* @var Grav
*/
public $grav; public $grav;
/**
* @var string /** @var string */
*/
public $base; public $base;
/**
* @var string /** @var string */
*/
public $location; public $location;
/**
* @var string /** @var string */
*/
public $route; public $route;
/**
* @var User /** @var User */
*/
public $user; public $user;
/**
* @var array /** @var array */
*/
public $forgot; public $forgot;
/**
* @var string /** @var string */
*/
public $task; public $task;
/**
* @var array /** @var array */
*/
public $json_response; public $json_response;
/**
* @var Uri $uri /** @var Collection */
*/ public $collection;
/** @var bool */
public $multilang;
/** @var array */
public $languages_enabled;
/** @var Uri $uri */
protected $uri; protected $uri;
/**
* @var array /** @var array */
*/
protected $pages = []; protected $pages = [];
/**
* @var Session /** @var Session */
*/
protected $session; protected $session;
/**
* @var Data\Blueprints /** @var Data\Blueprints */
*/
protected $blueprints; protected $blueprints;
/**
* @var GPM /** @var GPM */
*/
protected $gpm; protected $gpm;
/** /** @var int */
* @var int
*/
protected $pages_count; protected $pages_count;
/** /** @var array */
* @var array
*/
protected $permissions; protected $permissions;
/** /** @var bool */
* @var bool
*/
protected $load_additional_files_in_background = false; protected $load_additional_files_in_background = false;
/** /** @var bool */
* @var bool
*/
protected $loading_additional_files_in_background = false; protected $loading_additional_files_in_background = false;
/** /** @var array */
* @var array
*/
protected $temp_messages = []; protected $temp_messages = [];
/** /**
@@ -145,7 +132,7 @@ class Admin
// Load utility class // Load utility class
if ($language->enabled()) { if ($language->enabled()) {
$this->multilang = true; $this->multilang = true;
$this->languages_enabled = $this->grav['config']->get('system.languages.supported', []); $this->languages_enabled = (array)$this->grav['config']->get('system.languages.supported', []);
//Set the currently active language for the admin //Set the currently active language for the admin
$language = $this->grav['uri']->param('lang'); $language = $this->grav['uri']->param('lang');
@@ -664,12 +651,18 @@ class Admin
$data[$type] = $obj; $data[$type] = $obj;
} elseif (preg_match('|users/|', $type)) { } elseif (preg_match('|users/|', $type)) {
$obj = User::load(preg_replace('|users/|', '', $type)); /** @var UserCollectionInterface $users */
$users = $this->grav['users'];
$obj = $users->load(preg_replace('|users/|', '', $type));
$obj->update($this->cleanUserPost($post)); $obj->update($this->cleanUserPost($post));
$data[$type] = $obj; $data[$type] = $obj;
} elseif (preg_match('|user/|', $type)) { } elseif (preg_match('|user/|', $type)) {
$obj = User::load(preg_replace('|user/|', '', $type)); /** @var UserCollectionInterface $users */
$users = $this->grav['users'];
$obj = $users->load(preg_replace('|user/|', '', $type));
$obj->update($this->cleanUserPost($post)); $obj->update($this->cleanUserPost($post));
$data[$type] = $obj; $data[$type] = $obj;
@@ -691,7 +684,7 @@ class Admin
$file = File::instance($filename); $file = File::instance($filename);
$obj = new \StdClass(); $obj = new \stdClass();
$obj->title = $file->basename(); $obj->title = $file->basename();
$obj->path = $file->filename(); $obj->path = $file->filename();
$obj->file = $file; $obj->file = $file;
@@ -718,7 +711,7 @@ class Admin
/** /**
* Clean user form post and remove extra stuff that may be passed along * Clean user form post and remove extra stuff that may be passed along
* *
* @param $post * @param array $post
* @return array * @return array
*/ */
protected function cleanUserPost($post) protected function cleanUserPost($post)
@@ -800,7 +793,7 @@ class Admin
/** /**
* Count the pages * Count the pages
* *
* @return array * @return int
*/ */
public function pagesCount() public function pagesCount()
{ {
@@ -996,7 +989,7 @@ class Admin
/** /**
* Check the passed packages list can be updated * Check the passed packages list can be updated
* *
* @param $packages * @param array $packages
* *
* @throws \Exception * @throws \Exception
* @return bool * @return bool
@@ -1168,7 +1161,7 @@ class Admin
/** /**
* Guest date format based on euro/US * Guest date format based on euro/US
* *
* @param $date * @param string $date
* *
* @return string * @return string
*/ */
@@ -1317,7 +1310,7 @@ class Admin
/** /**
* Sets the entire permissions array * Sets the entire permissions array
* *
* @param $permissions * @param array $permissions
*/ */
public function setPermissions($permissions) public function setPermissions($permissions)
{ {
@@ -1327,7 +1320,7 @@ class Admin
/** /**
* Adds a permission to the permissions array * Adds a permission to the permissions array
* *
* @param $permissions * @param array $permissions
*/ */
public function addPermissions($permissions) public function addPermissions($permissions)
{ {
@@ -1584,9 +1577,9 @@ class Admin
/** /**
* Returns the page creating it if it does not exist. * Returns the page creating it if it does not exist.
* *
* @param $path * @param string $path
* *
* @return PageInterface * @return PageInterface|null
*/ */
public function getPage($path) public function getPage($path)
{ {

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use Grav\Common\Config\Config; use Grav\Common\Config\Config;

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use Grav\Common\Backup\Backups; use Grav\Common\Backup\Backups;
@@ -12,11 +13,13 @@ use Grav\Common\Grav;
use Grav\Common\Data; use Grav\Common\Data;
use Grav\Common\Page\Interfaces\PageInterface; use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Page\Media; use Grav\Common\Page\Media;
use Grav\Common\Page\Medium\ImageMedium;
use Grav\Common\Page\Medium\Medium; use Grav\Common\Page\Medium\Medium;
use Grav\Common\Page\Page; use Grav\Common\Page\Page;
use Grav\Common\Page\Pages; use Grav\Common\Page\Pages;
use Grav\Common\Page\Collection; use Grav\Common\Page\Collection;
use Grav\Common\Security; use Grav\Common\Security;
use Grav\Common\User\Interfaces\UserCollectionInterface;
use Grav\Common\User\User; use Grav\Common\User\User;
use Grav\Common\Utils; use Grav\Common\Utils;
use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth; use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth;
@@ -95,7 +98,6 @@ class AdminController extends AdminBaseController
} }
/** /**
* @param null $secret
* @return bool * @return bool
*/ */
public function taskRegenerate2FASecret() public function taskRegenerate2FASecret()
@@ -116,14 +118,14 @@ class AdminController extends AdminBaseController
// Save secret into the user file. // Save secret into the user file.
$file = $user->file(); $file = $user->file();
if ($file->exists()) { if ($file->exists()) {
$content = $file->content(); $content = (array)$file->content();
$content['twofa_secret'] = $secret; $content['twofa_secret'] = $secret;
$file->save($content); $file->save($content);
$file->free(); $file->free();
} }
// Change secret in the session. // Change secret in the session.
$user->twofa_secret = $secret; $user->set('twofa_secret', $secret);
$this->admin->json_response = ['status' => 'success', 'image' => $image, 'secret' => preg_replace('|(\w{4})|', '\\1 ', $secret)]; $this->admin->json_response = ['status' => 'success', 'image' => $image, 'secret' => preg_replace('|(\w{4})|', '\\1 ', $secret)];
} catch (\Exception $e) { } catch (\Exception $e) {
@@ -144,13 +146,16 @@ class AdminController extends AdminBaseController
$data = $this->data; $data = $this->data;
if (isset($data['password'])) { if (isset($data['password'])) {
/** @var UserCollectionInterface $users */
$users = $this->grav['users'];
$username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : null; $username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : null;
$user = $username ? User::load($username) : null; $user = $username ? $users->load($username) : null;
$password = $data['password'] ?? null; $password = $data['password'] ?? null;
$token = $data['token'] ?? null; $token = $data['token'] ?? null;
if ($user && $user->exists() && !empty($user->reset)) { if ($user && $user->exists() && !empty($user->get('reset'))) {
list($good_token, $expire) = explode('::', $user->reset); list($good_token, $expire) = explode('::', $user->get('reset'));
if ($good_token === $token) { if ($good_token === $token) {
if (time() > $expire) { if (time() > $expire) {
@@ -160,8 +165,9 @@ class AdminController extends AdminBaseController
return true; return true;
} }
unset($user->hashed_password, $user->reset); $user->undef('hashed_password');
$user->password = $password; $user->undef('reset');
$user->set('password', $password);
$user->validate(); $user->validate();
$user->filter(); $user->filter();
@@ -210,8 +216,11 @@ class AdminController extends AdminBaseController
$data = $this->data; $data = $this->data;
$login = $this->grav['login']; $login = $this->grav['login'];
/** @var UserCollectionInterface $users */
$users = $this->grav['users'];
$username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : ''; $username = isset($data['username']) ? strip_tags(strtolower($data['username'])) : '';
$user = !empty($username) ? User::load($username) : null; $user = !empty($username) ? $users->load($username) : null;
if (!isset($this->grav['Email'])) { if (!isset($this->grav['Email'])) {
$this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error'); $this->admin->setMessage($this->admin::translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
@@ -249,7 +258,7 @@ class AdminController extends AdminBaseController
$token = md5(uniqid(mt_rand(), true)); $token = md5(uniqid(mt_rand(), true));
$expire = time() + 604800; // next week $expire = time() + 604800; // next week
$user->reset = $token . '::' . $expire; $user->set('reset', $token . '::' . $expire);
$user->save(); $user->save();
$author = $this->grav['config']->get('site.author.name', ''); $author = $this->grav['config']->get('site.author.name', '');
@@ -504,7 +513,7 @@ class AdminController extends AdminBaseController
/** /**
* Get the next available ordering number in a folder * Get the next available ordering number in a folder
* *
* @param $path * @param string $path
* *
* @return string the correct order string to prepend * @return string the correct order string to prepend
*/ */
@@ -675,9 +684,12 @@ class AdminController extends AdminBaseController
if ($this->view === 'user') { if ($this->view === 'user') {
if ($obj->username === $this->grav['user']->username) { if ($obj->username === $this->grav['user']->username) {
/** @var UserCollectionInterface $users */
$users = $this->grav['users'];
//Editing current user. Reload user object //Editing current user. Reload user object
unset($this->grav['user']->avatar); unset($this->grav['user']->avatar);
$this->grav['user']->merge(User::load($this->admin->route)->toArray()); $this->grav['user']->merge($users->load($this->admin->route)->toArray());
} }
} }
} }
@@ -830,19 +842,16 @@ class AdminController extends AdminBaseController
} }
// do we need to force a reload // do we need to force a reload
$refresh = $this->data['refresh'] === 'true' ? true : false; $refresh = $this->data['refresh'] === 'true';
$filter = $this->data['filter'] ?? ''; $filter = $this->data['filter'] ?? '';
$filter_types = !empty($filter) ? array_map('trim', explode(',', $filter)) : [];
if (!empty($filter)) {
$filter_types = array_map('trim', explode(',', $filter));
}
try { try {
$notifications = $this->admin->getNotifications($refresh); $notifications = $this->admin->getNotifications($refresh);
$notification_data = []; $notification_data = [];
foreach ($notifications as $type => $type_notifications) { foreach ($notifications as $type => $type_notifications) {
if (empty($filter) || in_array($type, $filter_types)) { if ($filter_types && in_array($type, $filter_types, true)) {
$twig_template = 'partials/notification-' . $type . '-block.html.twig'; $twig_template = 'partials/notification-' . $type . '-block.html.twig';
$notification_data[$type] = $this->grav['twig']->processTemplate($twig_template, ['notifications' => $type_notifications]); $notification_data[$type] = $this->grav['twig']->processTemplate($twig_template, ['notifications' => $type_notifications]);
} }
@@ -1347,7 +1356,7 @@ class AdminController extends AdminBaseController
if ($page) { if ($page) {
$child_type = $page->childType(); $child_type = $page->childType();
if (isset($child_type)) { if ($child_type !== '') {
$this->admin->json_response = [ $this->admin->json_response = [
'status' => 'success', 'status' => 'success',
'child_type' => $child_type 'child_type' => $child_type
@@ -1484,7 +1493,7 @@ class AdminController extends AdminBaseController
'message' => $this->admin::translate('PLUGIN_ADMIN.PAGES_FILTERED'), 'message' => $this->admin::translate('PLUGIN_ADMIN.PAGES_FILTERED'),
'results' => $results 'results' => $results
]; ];
$this->admin->collection = $collection; $this->admin->collection = $collection;
} }
/** /**
@@ -1511,7 +1520,7 @@ class AdminController extends AdminBaseController
$media_list = []; $media_list = [];
/** /**
* @var string $name * @var string $name
* @var Medium $medium * @var Medium|ImageMedium $medium
*/ */
foreach ($media->all() as $name => $medium) { foreach ($media->all() as $name => $medium) {
@@ -1522,13 +1531,14 @@ class AdminController extends AdminBaseController
} }
// Get original name // Get original name
$source = $medium->higherQualityAlternative(); /** @var ImageMedium $source */
$source = method_exists($medium, 'higherQualityAlternative') ? $medium->higherQualityAlternative() : null;
$media_list[$name] = [ $media_list[$name] = [
'url' => $medium->display($medium->get('extension') === 'svg' ? 'source' : 'thumbnail')->cropZoom(400, 300)->url(), 'url' => $medium->display($medium->get('extension') === 'svg' ? 'source' : 'thumbnail')->cropZoom(400, 300)->url(),
'size' => $medium->get('size'), 'size' => $medium->get('size'),
'metadata' => $metadata, 'metadata' => $metadata,
'original' => $source->get('filename') 'original' => $source ? $source->get('filename') : null
]; ];
} }
@@ -1577,7 +1587,7 @@ class AdminController extends AdminBaseController
/** @var Config $config */ /** @var Config $config */
$config = $this->grav['config']; $config = $this->grav['config'];
if (!isset($_FILES) || empty($_FILES)) { if (empty($_FILES)) {
$this->admin->json_response = [ $this->admin->json_response = [
'status' => 'error', 'status' => 'error',
'message' => $this->admin::translate('PLUGIN_ADMIN.EXCEEDED_POSTMAX_LIMIT') 'message' => $this->admin::translate('PLUGIN_ADMIN.EXCEEDED_POSTMAX_LIMIT')
@@ -1681,7 +1691,7 @@ class AdminController extends AdminBaseController
/** @var UniformResourceLocator $locator */ /** @var UniformResourceLocator $locator */
$locator = $this->grav['locator']; $locator = $this->grav['locator'];
$path = $media->path(); $path = $media->getPath();
if ($locator->isStream($path)) { if ($locator->isStream($path)) {
$path = $locator->findResource($path, true, true); $path = $locator->findResource($path, true, true);
} }
@@ -1763,7 +1773,7 @@ class AdminController extends AdminBaseController
/** @var UniformResourceLocator $locator */ /** @var UniformResourceLocator $locator */
$locator = $this->grav['locator']; $locator = $this->grav['locator'];
$targetPath = $media->path() . '/' . $filename; $targetPath = $media->getPath() . '/' . $filename;
if ($locator->isStream($targetPath)) { if ($locator->isStream($targetPath)) {
$targetPath = $locator->findResource($targetPath, true, true); $targetPath = $locator->findResource($targetPath, true, true);
} }
@@ -1786,10 +1796,10 @@ class AdminController extends AdminBaseController
} }
// Remove Extra Files // Remove Extra Files
foreach (scandir($media->path(), SCANDIR_SORT_NONE) as $file) { foreach (scandir($media->getPath(), SCANDIR_SORT_NONE) as $file) {
if (preg_match("/{$fileParts['filename']}@\d+x\.{$fileParts['extension']}(?:\.meta\.yaml)?$|{$filename}\.meta\.yaml$/", $file)) { if (preg_match("/{$fileParts['filename']}@\d+x\.{$fileParts['extension']}(?:\.meta\.yaml)?$|{$filename}\.meta\.yaml$/", $file)) {
$targetPath = $media->path() . '/' . $file; $targetPath = $media->getPath() . '/' . $file;
if ($locator->isStream($targetPath)) { if ($locator->isStream($targetPath)) {
$targetPath = $locator->findResource($targetPath, true, true); $targetPath = $locator->findResource($targetPath, true, true);
} }
@@ -1940,7 +1950,7 @@ class AdminController extends AdminBaseController
}); });
} }
$page->header((object)$header); $page->header((object)$header);
$page->frontmatter(Yaml::dump((array)$page->header()), 20); $page->frontmatter(Yaml::dump((array)$page->header(), 20));
} }
// Fill content last because it also renders the output. // Fill content last because it also renders the output.
if (isset($input['content'])) { if (isset($input['content'])) {

View File

@@ -27,9 +27,6 @@ class Gpm
{ {
if (!static::$GPM) { if (!static::$GPM) {
static::$GPM = new GravGPM(); static::$GPM = new GravGPM();
if (method_exists('GravGPM', 'loadRemoteGrav')) {
static::$GPM->loadRemoteGrav();
}
} }
return static::$GPM; return static::$GPM;
@@ -53,7 +50,7 @@ class Gpm
* @param Package[]|string[]|string $packages * @param Package[]|string[]|string $packages
* @param array $options * @param array $options
* *
* @return bool * @return string|bool
*/ */
public static function install($packages, array $options) public static function install($packages, array $options)
{ {
@@ -128,7 +125,7 @@ class Gpm
* @param Package[]|string[]|string $packages * @param Package[]|string[]|string $packages
* @param array $options * @param array $options
* *
* @return bool * @return string|bool
*/ */
public static function update($packages, array $options) public static function update($packages, array $options)
{ {
@@ -141,7 +138,7 @@ class Gpm
* @param Package[]|string[]|string $packages * @param Package[]|string[]|string $packages
* @param array $options * @param array $options
* *
* @return bool * @return string|bool
*/ */
public static function uninstall($packages, array $options) public static function uninstall($packages, array $options)
{ {
@@ -199,9 +196,9 @@ class Gpm
/** /**
* Direct install a file * Direct install a file
* *
* @param $package_file * @param string $package_file
* *
* @return bool * @return string|bool
*/ */
public static function directInstall($package_file) public static function directInstall($package_file)
{ {

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use Grav\Common\Config\Config; use Grav\Common\Config\Config;

View File

@@ -1,4 +1,5 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
/** /**

View File

@@ -1,8 +1,10 @@
<?php <?php
namespace Grav\Plugin\Admin; namespace Grav\Plugin\Admin;
use Grav\Common\Grav; use Grav\Common\Grav;
use Grav\Common\User\User; use Grav\Common\User\Interfaces\UserCollectionInterface;
use Grav\Common\User\Interfaces\UserInterface;
/** /**
* Admin utils class * Admin utils class
@@ -14,26 +16,18 @@ class Utils
/** /**
* Matches an email to a user * Matches an email to a user
* *
* @param $email * @param string $email
* *
* @return User * @return UserInterface
*/ */
public static function findUserByEmail($email) public static function findUserByEmail(string $email)
{ {
$account_dir = Grav::instance()['locator']->findResource('account://'); $grav = Grav::instance();
$files = array_diff(scandir($account_dir, SCANDIR_SORT_ASCENDING), ['.', '..']);
foreach ($files as $file) { /** @var UserCollectionInterface $users */
if (strpos($file, '.yaml') !== false) { $users = $grav['users'];
$user = User::load(trim(substr($file, 0, -5)));
if ($user['email'] === $email) {
return $user;
}
}
}
// If a User with the provided email cannot be found, then load user with that email as the username return $users->find($email, ['email']);
return User::load($email);
} }
/** /**
@@ -42,7 +36,7 @@ class Utils
* @param string $str * @param string $str
* @return string * @return string
*/ */
public static function slug($str) public static function slug(string $str)
{ {
if (function_exists('transliterator_transliterate')) { if (function_exists('transliterator_transliterate')) {
$str = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;', $str); $str = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;', $str);

View File

@@ -33,6 +33,12 @@
"symfony/finder": "~4.1", "symfony/finder": "~4.1",
"symfony/event-dispatcher": "~4.1" "symfony/event-dispatcher": "~4.1"
}, },
"autoload": {
"classmap": [
"classes/",
"admin.php"
]
},
"config": { "config": {
"platform": { "platform": {
"php": "7.1.3" "php": "7.1.3"

View File

@@ -1 +1 @@
<img src="{{ admin.user.avatarUrl() }}?s=47" /> <img src="{{ admin.user.getAvatarUrl() }}?s=47" />

View File

@@ -1,5 +1,5 @@
{% if data.avatar %} {% if data.avatar %}
<label><img src="{{ data.avatarUrl }}" /></label> <label><img src="{{ data.getAvatarUrl() }}" /></label>
{% else %} {% else %}
<label><img referrerpolicy="no-referrer" src="https://www.gravatar.com/avatar/{{ data.email|md5 }}?s=200" /></label> <label><img referrerpolicy="no-referrer" src="https://www.gravatar.com/avatar/{{ data.email|md5 }}?s=200" /></label>
{% endif %} {% endif %}

View File

@@ -279,7 +279,7 @@ class ClassLoader
*/ */
public function setApcuPrefix($apcuPrefix) public function setApcuPrefix($apcuPrefix)
{ {
$this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
} }
/** /**
@@ -377,7 +377,7 @@ class ClassLoader
$subPath = $class; $subPath = $class;
while (false !== $lastPos = strrpos($subPath, '\\')) { while (false !== $lastPos = strrpos($subPath, '\\')) {
$subPath = substr($subPath, 0, $lastPos); $subPath = substr($subPath, 0, $lastPos);
$search = $subPath . '\\'; $search = $subPath.'\\';
if (isset($this->prefixDirsPsr4[$search])) { if (isset($this->prefixDirsPsr4[$search])) {
$pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
foreach ($this->prefixDirsPsr4[$search] as $dir) { foreach ($this->prefixDirsPsr4[$search] as $dir) {

View File

@@ -6,4 +6,13 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir); $baseDir = dirname($vendorDir);
return array( return array(
'Grav\\Plugin\\AdminPlugin' => $baseDir . '/admin.php',
'Grav\\Plugin\\Admin\\Admin' => $baseDir . '/classes/admin.php',
'Grav\\Plugin\\Admin\\AdminBaseController' => $baseDir . '/classes/adminbasecontroller.php',
'Grav\\Plugin\\Admin\\AdminController' => $baseDir . '/classes/admincontroller.php',
'Grav\\Plugin\\Admin\\Gpm' => $baseDir . '/classes/gpm.php',
'Grav\\Plugin\\Admin\\Popularity' => $baseDir . '/classes/popularity.php',
'Grav\\Plugin\\Admin\\Themes' => $baseDir . '/classes/themes.php',
'Grav\\Plugin\\Admin\\Twig\\AdminTwigExtension' => $baseDir . '/classes/Twig/AdminTwigExtension.php',
'Grav\\Plugin\\Admin\\Utils' => $baseDir . '/classes/utils.php',
); );

View File

@@ -38,12 +38,25 @@ class ComposerStaticInitda370287ab6d5b8a28188afe08f659c5
), ),
); );
public static $classMap = array (
'Grav\\Plugin\\AdminPlugin' => __DIR__ . '/../..' . '/admin.php',
'Grav\\Plugin\\Admin\\Admin' => __DIR__ . '/../..' . '/classes/admin.php',
'Grav\\Plugin\\Admin\\AdminBaseController' => __DIR__ . '/../..' . '/classes/adminbasecontroller.php',
'Grav\\Plugin\\Admin\\AdminController' => __DIR__ . '/../..' . '/classes/admincontroller.php',
'Grav\\Plugin\\Admin\\Gpm' => __DIR__ . '/../..' . '/classes/gpm.php',
'Grav\\Plugin\\Admin\\Popularity' => __DIR__ . '/../..' . '/classes/popularity.php',
'Grav\\Plugin\\Admin\\Themes' => __DIR__ . '/../..' . '/classes/themes.php',
'Grav\\Plugin\\Admin\\Twig\\AdminTwigExtension' => __DIR__ . '/../..' . '/classes/Twig/AdminTwigExtension.php',
'Grav\\Plugin\\Admin\\Utils' => __DIR__ . '/../..' . '/classes/utils.php',
);
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixDirsPsr4;
$loader->prefixesPsr0 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixesPsr0; $loader->prefixesPsr0 = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$prefixesPsr0;
$loader->classMap = ComposerStaticInitda370287ab6d5b8a28188afe08f659c5::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }