Files
Grav-Admin-Plugin/admin.php

332 lines
9.8 KiB
PHP
Raw Normal View History

2014-08-05 13:06:38 -07:00
<?php
namespace Grav\Plugin;
use Grav\Common\GPM\GPM;
use Grav\Common\Grav;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Plugin;
use Grav\Common\Uri;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\Event\Event;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\Session\Session;
2014-08-05 13:06:38 -07:00
class AdminPlugin extends Plugin
{
/**
* @var bool
*/
protected $active = false;
/**
* @var string
*/
protected $template;
2014-09-03 22:22:03 -06:00
/**
* @var string
*/
protected $theme;
2014-08-05 13:06:38 -07:00
/**
* @var string
*/
protected $route;
/**
* @var Uri
*/
protected $uri;
/**
* @var Admin
*/
protected $admin;
2014-10-01 22:28:16 +03:00
/**
* @var Session
*/
protected $session;
/**
* @var Popularity
*/
protected $popularity;
2014-10-01 22:28:16 +03:00
/**
* @var string
*/
protected $base;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [['login', 100000], ['onPluginsInitialized', 1000]],
'onShutdown' => ['onShutdown', 1000]
];
}
2014-08-05 13:06:38 -07:00
/**
2015-07-30 12:20:25 +02:00
* If the admin path matches, initialize the Login plugin configuration and set the admin
* as active.
2014-08-05 13:06:38 -07:00
*/
public function login()
2014-08-05 13:06:38 -07:00
{
2014-12-29 11:31:10 -07:00
// Check for Pro version is enabled
if ($this->config->get('plugins.admin-pro.enabled')) {
$this->active = false;
return;
}
2014-08-05 13:06:38 -07:00
$route = $this->config->get('plugins.admin.route');
if (!$route) {
return;
}
2014-12-29 11:31:10 -07:00
$this->grav['debugger']->addMessage("Admin Basic");
$this->base = '/' . trim($route, '/');
$this->uri = $this->grav['uri'];
2014-08-05 13:06:38 -07:00
2014-08-05 13:06:38 -07:00
// Only activate admin if we're inside the admin path.
if (substr($this->uri->route(), 0, strlen($this->base)) == $this->base) {
// Change login behavior.
$this->config->set('plugins.login', $this->config->get('plugins.admin.login'));
2015-01-20 14:28:16 +02:00
$this->config->set('plugins.login.session.path', $this->uri->rootUrl(false) . $this->base);
2015-04-11 15:32:21 +02:00
$this->config->set('plugins.login.session.name', 'grav_admin');
$this->active = true;
}
}
/**
2015-07-30 12:20:25 +02:00
* If the admin plugin is set as active, initialize the admin
*/
public function onPluginsInitialized()
{
// Only activate admin if we're inside the admin path.
if ($this->active) {
$this->initializeAdmin();
2014-08-05 13:06:38 -07:00
}
2014-09-09 14:03:01 -06:00
// We need popularity no matter what
require_once __DIR__ . '/classes/popularity.php';
$this->popularity = new Popularity();
2014-08-05 13:06:38 -07:00
}
/**
* Sets longer path to the home page allowing us to have list of pages when we enter to pages section.
*/
public function onPagesInitialized()
2014-08-05 13:06:38 -07:00
{
$this->session = $this->grav['session'];
2014-08-05 13:06:38 -07:00
// Set original route for the home page.
$home = '/' . trim($this->config->get('system.home.alias'), '/');
// Disable Asset pipelining
$this->config->set('system.assets.css_pipeline', false);
$this->config->set('system.assets.js_pipeline', false);
2014-10-06 19:03:57 -06:00
// set the default if not set before
$this->session->expert = $this->session->expert ?: false;
2014-10-06 19:03:57 -06:00
// set session variable if it's passed via the url
if ($this->uri->param('mode') == 'expert') {
$this->session->expert = true;
} elseif ($this->uri->param('mode') == 'normal') {
$this->session->expert = false;
}
2014-08-05 13:06:38 -07:00
/** @var Pages $pages */
$pages = $this->grav['pages'];
$this->grav['admin']->routes = $pages->routes();
// Remove default route from routes.
if (isset($this->grav['admin']->routes['/'])) {
unset($this->grav['admin']->routes['/']);
}
$page = $pages->dispatch('/', true);
// If page is null, the default page does not exist, and we cannot route to it
if ($page) {
$page->route($home);
}
2014-08-05 13:06:38 -07:00
// Make local copy of POST.
$post = !empty($_POST) ? $_POST : array();
// Handle tasks.
2014-09-22 15:49:53 -06:00
$this->admin->task = $task = !empty($post['task']) ? $post['task'] : $this->uri->param('task');
2014-08-05 13:06:38 -07:00
if ($task) {
require_once __DIR__ . '/classes/controller.php';
$controller = new AdminController($this->grav, $this->template, $task, $this->route, $post);
2014-09-17 11:54:57 +03:00
$controller->execute();
2014-08-05 13:06:38 -07:00
$controller->redirect();
} elseif ($this->template == 'logs' && $this->route) {
// Display RAW error message.
echo $this->admin->logEntry();
exit();
}
$self = $this;
2014-08-05 13:06:38 -07:00
// Replace page service with admin.
$this->grav['page'] = function () use ($self) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
$page->slug(basename($self->template));
return $page;
};
2014-08-05 13:06:38 -07:00
}
/**
* Add twig paths to plugin templates.
*/
public function onTwigTemplatePaths()
2014-08-05 13:06:38 -07:00
{
$twig_paths = [];
$this->grav->fireEvent('onAdminTwigTemplatePaths', new Event(['paths' => &$twig_paths]));
$twig_paths[] = __DIR__ . '/themes/' . $this->theme . '/templates';
$this->grav['twig']->twig_paths = $twig_paths;
2014-08-05 13:06:38 -07:00
}
/**
* Set all twig variables for generating output.
*/
public function onTwigSiteVariables()
2014-08-05 13:06:38 -07:00
{
$twig = $this->grav['twig'];
2014-08-05 13:06:38 -07:00
2014-09-22 15:49:53 -06:00
// Dynamic type support
$format = $this->uri->extension();
$ext = '.' . ($format ? $format : 'html') . TWIG_EXT;
2014-08-05 13:06:38 -07:00
$twig->twig_vars['location'] = $this->template;
$twig->twig_vars['base_url_relative_frontend'] = $twig->twig_vars['base_url_relative'];
2014-08-05 13:06:38 -07:00
$twig->twig_vars['base_url_relative'] .=
($twig->twig_vars['base_url_relative'] != '/' ? '/' : '') . trim($this->config->get('plugins.admin.route'),
'/');
$twig->twig_vars['theme_url'] = '/user/plugins/admin/themes/' . $this->theme;
2014-08-29 11:59:43 +03:00
$twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative'];
2014-08-05 13:06:38 -07:00
$twig->twig_vars['admin'] = $this->admin;
switch ($this->template) {
case 'dashboard':
$twig->twig_vars['popularity'] = $this->popularity;
break;
2014-08-05 13:06:38 -07:00
case 'pages':
$page = $this->admin->page(true);
if ($page != null) {
$twig->twig_vars['file'] = File::instance($page->filePath());
$twig->twig_vars['media_types'] = str_replace('defaults,', '',
implode(',.', array_keys($this->config->get('media'))));
}
2014-08-05 13:06:38 -07:00
break;
}
}
2014-09-03 22:22:03 -06:00
public function onShutdown()
{
2014-09-09 14:03:01 -06:00
// Just so we know that we're in this debug mode
2014-09-06 18:13:04 -06:00
if ($this->config->get('plugins.admin.popularity.enabled')) {
// Only track non-admin
if (!$this->active) {
$this->popularity->trackHit();
}
}
}
2015-07-30 12:20:25 +02:00
/**
* Handles getting GPM updates
*/
public function onTaskGPM()
{
$action = $_POST['action']; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates
if (isset($this->grav['session'])) {
$this->grav['session']->close();
}
try {
$gpm = new GPM();
switch ($action) {
case 'getUpdates':
$resources_updates = $gpm->getUpdatable();
$grav_updates = [
"isUpdatable" => $gpm->grav->isUpdatable(),
"assets" => $gpm->grav->getAssets(),
"version" => GRAV_VERSION,
"available" => $gpm->grav->getVersion(),
"date" => $gpm->grav->getDate(),
"isSymlink" => $gpm->grav->isSymlink()
];
echo json_encode([
"status" => "success",
2014-12-19 16:25:36 -08:00
"payload" => ["resources" => $resources_updates, "grav" => $grav_updates, "installed" => $gpm->countInstalled()]
]);
break;
}
} catch (\Exception $e) {
echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}
exit;
}
2015-07-30 12:20:25 +02:00
/**
* Initialize the admin.
*
* @throws \RuntimeException
*/
2014-09-03 22:22:03 -06:00
protected function initializeAdmin()
{
$this->enable([
'onPagesInitialized' => ['onPagesInitialized', 1000],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000],
'onTask.GPM' => ['onTaskGPM', 0]
]);
2014-09-03 22:22:03 -06:00
2015-07-27 12:56:16 -06:00
// Check for required plugins
if (!$this->grav['config']->get('plugins.login.enabled') ||
!$this->grav['config']->get('plugins.form.enabled') ||
!$this->grav['config']->get('plugins.email.enabled')) {
throw new \RuntimeException('One of the required plugins is missing or not enabled');
}
// Decide admin template and route.
$path = trim(substr($this->uri->route(), strlen($this->base)), '/');
$this->template = 'dashboard';
2014-09-03 22:22:03 -06:00
if ($path) {
$array = explode('/', $path, 2);
$this->template = array_shift($array);
$this->route = array_shift($array);
}
2014-09-03 22:22:03 -06:00
// Initialize admin class.
require_once __DIR__ . '/classes/admin.php';
$this->admin = new Admin($this->grav, $this->base, $this->template, $this->route);
// And store the class into DI container.
$this->grav['admin'] = $this->admin;
// Get theme for admin
$this->theme = $this->config->get('plugins.admin.theme', 'grav');
2014-09-03 22:22:03 -06:00
}
2014-08-05 13:06:38 -07:00
}