Files
Grav-Admin-Plugin/classes/admin.php

617 lines
15 KiB
PHP
Raw Normal View History

2014-08-05 13:06:38 -07:00
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
2014-10-07 12:12:21 +03:00
use Grav\Common\File\CompiledYamlFile;
use Grav\Common\GPM\GPM;
2014-08-05 13:06:38 -07:00
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
2014-08-05 13:06:38 -07:00
use Grav\Common\Plugins;
use Grav\Common\Themes;
use Grav\Common\Uri;
use Grav\Common\User\User;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\File\File;
2015-04-27 13:37:22 +02:00
use RocketTheme\Toolbox\File\JsonFile;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\File\LogFile;
2014-10-07 12:12:21 +03:00
use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\Session\Message;
use RocketTheme\Toolbox\Session\Session;
use Symfony\Component\Yaml\Yaml;
2014-08-05 13:06:38 -07:00
define('LOGIN_REDIRECT_COOKIE', 'grav-login-redirect');
2014-08-05 13:06:38 -07:00
class Admin
{
/**
* @var Grav
*/
public $grav;
2014-08-05 13:06:38 -07:00
/**
* @var Uri $uri
*/
protected $uri;
/**
* @var array
*/
protected $pages = array();
/**
2014-10-01 22:28:16 +03:00
* @var Session
2014-08-05 13:06:38 -07:00
*/
protected $session;
/**
* @var Data\Blueprints
*/
protected $blueprints;
/**
* @var string
*/
public $base;
/**
* @var string
*/
public $location;
/**
* @var string
*/
public $route;
/**
* @var User
2014-08-05 13:06:38 -07:00
*/
public $user;
2014-10-02 12:14:13 -07:00
/**
* @var Grav\Common\GPM\GPM
2014-10-02 12:14:13 -07:00
*/
protected $gpm;
2014-10-02 12:14:13 -07:00
2014-08-05 13:06:38 -07:00
/**
* Constructor.
*
* @param Grav $grav
2014-08-05 13:06:38 -07:00
* @param string $base
* @param string $location
* @param string $route
*/
public function __construct(Grav $grav, $base, $location, $route)
2014-08-05 13:06:38 -07:00
{
$this->grav = $grav;
2014-08-05 13:06:38 -07:00
$this->base = $base;
$this->location = $location;
$this->route = $route;
$this->uri = $this->grav['uri'];
$this->session = $this->grav['session'];
$this->user = $this->grav['user'];
2014-08-05 13:06:38 -07:00
}
/**
* Get current session.
*
2014-10-01 22:28:16 +03:00
* @return Session
2014-08-05 13:06:38 -07:00
*/
public function session()
{
return $this->session;
}
/**
* Add message into the session queue.
*
* @param string $msg
* @param string $type
*/
public function setMessage($msg, $type = 'info')
{
2014-10-01 22:28:16 +03:00
/** @var Message $messages */
$messages = $this->grav['messages'];
2014-08-05 13:06:38 -07:00
$messages->add($msg, $type);
}
/**
* Fetch and delete messages from the session queue.
*
* @param string $type
*
* @return array
2014-08-05 13:06:38 -07:00
*/
public function messages($type = null)
{
2014-10-01 22:28:16 +03:00
/** @var Message $messages */
$messages = $this->grav['messages'];
return $messages->fetch($type);
2014-08-05 13:06:38 -07:00
}
/**
* Authenticate user.
*
* @param array $form Form fields.
*
2014-08-05 13:06:38 -07:00
* @return bool
*/
public function authenticate($form)
{
if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
$user = User::load($form['username']);
if ($user->exists()) {
$user->authenticated = true;
2014-08-05 13:06:38 -07:00
// Authenticate user.
$result = $user->authenticate($form['password']);
if ($result) {
$this->user = $this->session->user = $user;
/** @var Grav $grav */
$grav = $this->grav;
$l = $this->grav['language'];
$this->setMessage($l->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info');
// $redirect_route =$this->getLoginRedirect() ?: $this->uri->route();
$redirect_route = $this->uri->route();
$grav->redirect($redirect_route);
2014-08-05 13:06:38 -07:00
}
}
}
return $this->authorise();
}
/**
* Checks user authorisation to the action.
*
* @param string $action
*
2014-08-05 13:06:38 -07:00
* @return bool
*/
public function authorise($action = 'admin.login')
{
$action = (array)$action;
foreach ($action as $a) {
if ($this->user->authorise($a)) {
return true;
}
}
return false;
2014-08-05 13:06:38 -07:00
}
/**
* Returns edited page.
*
* @param bool $route
*
2014-08-05 13:06:38 -07:00
* @return Page
*/
public function page($route = false)
{
$path = $this->route;
if ($route && !$path) {
$path = '/';
}
if (!isset($this->pages[$path])) {
$this->pages[$path] = $this->getPage($path);
}
return $this->pages[$path];
}
/**
* Returns blueprints for the given type.
*
* @param string $type
*
2014-08-05 13:06:38 -07:00
* @return Data\Blueprint
*/
public function blueprints($type)
{
if ($this->blueprints === null) {
$this->blueprints = new Data\Blueprints($this->grav['locator']->findResource('blueprints://'));
2014-08-05 13:06:38 -07:00
}
2014-08-05 13:06:38 -07:00
return $this->blueprints->get($type);
}
/**
* Gets configuration data.
*
* @param string $type
* @param array $post
*
2014-08-05 13:06:38 -07:00
* @return Data\Data|null
* @throws \RuntimeException
*/
public function data($type, $post = array())
{
2014-10-10 13:26:39 +03:00
static $data = [];
2014-08-05 13:06:38 -07:00
if (isset($data[$type])) {
return $data[$type];
}
2014-10-10 13:26:39 +03:00
if (!$post) {
$post = isset($_POST) ? $_POST : [];
}
2014-08-05 13:06:38 -07:00
switch ($type) {
case 'configuration':
case 'system':
$type = 'system';
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get('system'), $blueprints);
2014-08-05 13:06:38 -07:00
$obj->merge($post);
2015-05-29 14:30:58 +02:00
$file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
2014-08-05 13:06:38 -07:00
$obj->file($file);
$data[$type] = $obj;
break;
case 'settings':
case 'site':
$type = 'site';
$blueprints = $this->blueprints("config/{$type}");
$config = $this->grav['config'];
$obj = new Data\Data($config->get('site'), $blueprints);
2014-08-05 13:06:38 -07:00
$obj->merge($post);
2015-05-29 14:30:58 +02:00
$file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
2014-08-05 13:06:38 -07:00
$obj->file($file);
$data[$type] = $obj;
break;
case 'login':
$data[$type] = null;
break;
default:
2014-10-07 12:12:21 +03:00
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$filename = $locator->findResource("config://{$type}.yaml", true, true);
$file = CompiledYamlFile::instance($filename);
2014-08-05 13:06:38 -07:00
if (preg_match('|plugins/|', $type)) {
2014-10-01 22:28:16 +03:00
/** @var Plugins $plugins */
$plugins = $this->grav['plugins'];
$obj = $plugins->get(preg_replace('|plugins/|', '', $type));
2014-08-05 13:06:38 -07:00
$obj->merge($post);
2014-10-07 12:12:21 +03:00
$obj->file($file);
2014-08-05 13:06:38 -07:00
$data[$type] = $obj;
} elseif (preg_match('|themes/|', $type)) {
2014-10-01 22:28:16 +03:00
/** @var Themes $themes */
$themes = $this->grav['themes'];
$obj = $themes->get(preg_replace('|themes/|', '', $type));
2014-08-05 13:06:38 -07:00
$obj->merge($post);
2014-10-07 12:12:21 +03:00
$obj->file($file);
2014-08-05 13:06:38 -07:00
$data[$type] = $obj;
} elseif (preg_match('|users/|', $type)) {
$obj = User::load(preg_replace('|users/|', '', $type));
$obj->merge($post);
2014-08-05 13:06:38 -07:00
$data[$type] = $obj;
} else {
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
}
}
return $data[$type];
}
2015-07-30 11:22:24 +02:00
/**
* Get the GPM instance
*
* @return GPM The GPM instance
*/
public function gpm()
{
if (!$this->gpm) {
$this->gpm = new GPM();
}
return $this->gpm;
}
2014-08-05 13:06:38 -07:00
/**
* Converts dot notation to array notation.
*
* @param string $name
*
2014-08-05 13:06:38 -07:00
* @return string
*/
public function field($name)
{
$path = explode('.', $name);
return array_shift($path) . ($path ? '[' . implode('][', $path) . ']' : '');
}
/**
* Get all routes.
*
* @return array
*/
public function routes()
{
2014-10-01 22:28:16 +03:00
/** @var Pages $pages */
$pages = $this->grav['pages'];
2014-10-01 22:28:16 +03:00
return $pages->routes();
}
/**
* Get all plugins.
*
* @return array
*/
public function plugins($local = true)
{
$gpm = $this->gpm();
return $local ? $gpm->getInstalledPlugins() : $gpm->getRepositoryPlugins()->filter(function (
$package,
$slug
) use ($gpm) {
return !$gpm->isPluginInstalled($slug);
});
2014-10-03 15:40:58 -06:00
}
/**
* Get all themes.
*
* @return array
*/
public function themes($local = true)
2014-10-03 15:40:58 -06:00
{
$gpm = $this->gpm();
return $local ? $gpm->getInstalledThemes() : $gpm->getRepositoryThemes()->filter(function ($package, $slug) use
(
$gpm
) {
return !$gpm->isThemeInstalled($slug);
});
}
2014-08-05 13:06:38 -07:00
/**
* Get log file for fatal errors.
*
* @return string
*/
public function logs()
{
if (!isset($this->logs)) {
$file = LogFile::instance($this->grav['locator']->findResource('log://exception.log'));
2014-08-05 13:06:38 -07:00
$content = $file->content();
2014-08-05 13:06:38 -07:00
$this->logs = array_reverse($content);
}
return $this->logs;
}
/**
* Used by the Dashboard in the admin to display the X latest pages
* that have been modified
*
* @param integer $count number of pages to pull back
*
2014-10-01 22:28:16 +03:00
* @return array
*/
2014-10-02 12:14:13 -07:00
public function latestPages($count = 10)
{
2014-10-01 22:28:16 +03:00
/** @var Pages $pages */
$pages = $this->grav['pages'];
$latest = array();
2014-10-01 22:28:16 +03:00
foreach ($pages->routes() as $url => $path) {
$page = $pages->dispatch($url);
2014-12-10 10:23:22 -07:00
if ($page && $page->routable()) {
$latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page];
2014-12-10 10:23:22 -07:00
}
}
// sort based on modified
2014-10-02 12:14:13 -07:00
uasort($latest, function ($a, $b) {
if ($a['modified'] == $b['modified']) {
return 0;
}
return ($a['modified'] > $b['modified']) ? -1 : 1;
});
// build new array with just pages in it
// TODO: Optimized this
2014-10-01 22:28:16 +03:00
$list = array();
foreach ($latest as $item) {
2014-10-01 22:28:16 +03:00
$list[] = $item['page'];
}
2014-10-01 22:28:16 +03:00
return array_slice($list, 0, $count);
2014-08-05 13:06:38 -07:00
}
/**
* Get log file for fatal errors.
*
* @return string
*/
public function logEntry()
{
$file = File::instance($this->grav['locator']->findResource("log://{$this->route}.html"));
2014-08-05 13:06:38 -07:00
$content = $file->content();
return $content;
}
2015-07-30 11:22:24 +02:00
/**
* Search in the logs when was the latest backup made
*
* @return array Array containing the latest backup information
*/
2015-04-27 13:37:22 +02:00
public function lastBackup()
{
$file = JsonFile::instance($this->grav['locator']->findResource("log://backup.log"));
$content = $file->content();
if (empty($content)) {
return [
'days' => '&infin;',
'chart_fill' => 100,
2015-04-27 13:37:22 +02:00
'chart_empty' => 0
];
}
$backup = new \DateTime();
$backup->setTimestamp($content['time']);
$diff = $backup->diff(new \DateTime());
$days = $diff->days;
$chart_fill = $days > 30 ? 100 : round($days / 30 * 100);
return [
'days' => $days,
'chart_fill' => $chart_fill,
2015-04-27 13:37:22 +02:00
'chart_empty' => 100 - $chart_fill
];
}
2014-08-05 13:06:38 -07:00
/**
* Returns the page creating it if it does not exist.
*
* @param $path
*
2014-08-05 13:06:38 -07:00
* @return Page
*/
protected function getPage($path)
{
/** @var Pages $pages */
$pages = $this->grav['pages'];
2014-08-05 13:06:38 -07:00
if ($path && $path[0] != '/') {
$path = "/{$path}";
}
$page = $path ? $pages->dispatch($path, true) : $pages->root();
if (!$page) {
$slug = basename($path);
if ($slug == '') {
return null;
}
2014-08-05 13:06:38 -07:00
$ppath = dirname($path);
// Find or create parent(s).
$parent = $this->getPage($ppath != '/' ? $ppath : '');
// Create page.
$page = new Page;
$page->parent($parent);
$page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
2014-08-05 13:06:38 -07:00
// Add routing information.
2014-08-05 13:06:38 -07:00
$pages->addPage($page, $path);
2015-08-05 18:07:55 -06:00
// Set if Modular
$page->modularTwig($slug[0] == '_');
2014-08-05 13:06:38 -07:00
// Determine page type.
if (isset($this->session->{$page->route()})) {
2014-09-17 11:54:57 +03:00
// Found the type and header from the session.
$data = $this->session->{$page->route()};
$visible = isset($data['visible']) && $data['visible'] != '' ? (bool)$data['visible'] : $this->guessVisibility($page);
$header = ['title' => $data['title'], 'visible' => $visible];
if ($data['type'] == 'modular') {
$header['body_classes'] = 'modular';
}
2015-08-05 18:07:55 -06:00
$name = $page->modular() ? str_replace('modular/', '', $data['type']) : $data['type'];
$page->name($name . '.md');
$page->header($header);
$page->frontmatter(Yaml::dump((array)$page->header()));
2014-08-05 13:06:38 -07:00
} else {
// Find out the type by looking at the parent.
$type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type',
'default');
$page->name($type . CONTENT_EXT);
2014-09-17 11:54:57 +03:00
$page->header();
2014-08-05 13:06:38 -07:00
}
2014-10-10 11:57:57 +03:00
$page->modularTwig($slug[0] == '_');
2014-08-05 13:06:38 -07:00
}
return $page;
}
/**
* Guess the intended visibility status based on other sibling folders
*
* @param \Grav\Common\Page\Page $page
*
* @return bool
*/
public function guessVisibility(Page $page)
{
$children = $page->parent()->children();
foreach ($children as $child) {
if ($child->order()) {
return true;
}
}
return false;
}
2014-08-05 13:06:38 -07:00
/**
* Static helper method to return current route.
*
* @return string
*/
public static function route()
{
return dirname('/' . Grav::instance()['admin']->route);
2014-08-05 13:06:38 -07:00
}
2015-07-27 19:33:52 -06:00
2015-07-30 11:16:05 +02:00
/**
* Determine if the plugin or theme info passed is from Team Grav
*
* @param object $info Plugin or Theme info object
*
2015-07-30 11:16:05 +02:00
* @return bool
*/
2015-07-27 19:33:52 -06:00
public function isTeamGrav($info)
{
if (isset($info['author']['name']) && $info['author']['name'] == 'Team Grav') {
return true;
} else {
return false;
}
}
function phpinfo() {
ob_start();
phpinfo();
$pinfo = ob_get_contents();
ob_end_clean();
$pinfo = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);
return $pinfo;
}
2014-08-05 13:06:38 -07:00
}