2014-08-05 13:06:38 -07:00
|
|
|
<?php
|
|
|
|
|
namespace Grav\Plugin;
|
|
|
|
|
|
2014-10-07 12:12:21 +03:00
|
|
|
use Grav\Common\File\CompiledYamlFile;
|
2014-08-05 13:06:38 -07:00
|
|
|
use Grav\Common\User\User;
|
|
|
|
|
use Grav\Common\Grav;
|
|
|
|
|
use Grav\Common\Plugins;
|
|
|
|
|
use Grav\Common\Themes;
|
|
|
|
|
use Grav\Common\Uri;
|
|
|
|
|
use Grav\Common\Page\Pages;
|
|
|
|
|
use Grav\Common\Page\Page;
|
|
|
|
|
use Grav\Common\Data;
|
2014-10-02 12:14:13 -07:00
|
|
|
use Grav\Common\GPM\Local\Packages as LocalPackages;
|
2014-10-01 22:28:16 +03:00
|
|
|
use RocketTheme\Toolbox\File\File;
|
|
|
|
|
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;
|
2014-09-26 14:10:32 +03:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2014-08-05 13:06:38 -07:00
|
|
|
|
|
|
|
|
class Admin
|
|
|
|
|
{
|
2014-08-29 11:27:53 +03:00
|
|
|
/**
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-09-09 06:10:31 +03:00
|
|
|
* @var User
|
2014-08-05 13:06:38 -07:00
|
|
|
*/
|
|
|
|
|
public $user;
|
|
|
|
|
|
2014-10-02 12:14:13 -07:00
|
|
|
/**
|
|
|
|
|
* @var Packages
|
|
|
|
|
*/
|
|
|
|
|
public $localPackages;
|
|
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
|
2014-08-05 13:06:38 -07:00
|
|
|
/**
|
|
|
|
|
* Constructor.
|
|
|
|
|
*
|
2014-08-29 11:27:53 +03:00
|
|
|
* @param Grav $grav
|
2014-08-05 13:06:38 -07:00
|
|
|
* @param string $base
|
|
|
|
|
* @param string $location
|
|
|
|
|
* @param string $route
|
|
|
|
|
*/
|
2014-08-29 11:27:53 +03:00
|
|
|
public function __construct(Grav $grav, $base, $location, $route)
|
2014-08-05 13:06:38 -07:00
|
|
|
{
|
2014-08-29 11:27:53 +03:00
|
|
|
$this->grav = $grav;
|
2014-08-05 13:06:38 -07:00
|
|
|
$this->base = $base;
|
|
|
|
|
$this->location = $location;
|
|
|
|
|
$this->route = $route;
|
|
|
|
|
|
2014-08-29 11:27:53 +03:00
|
|
|
$this->uri = $this->grav['uri'];
|
2014-09-09 06:10:31 +03:00
|
|
|
$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 */
|
2014-09-09 06:10:31 +03:00
|
|
|
$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
|
2014-09-09 06:10:31 +03:00
|
|
|
* @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 */
|
2014-09-09 06:10:31 +03:00
|
|
|
$messages = $this->grav['messages'];
|
|
|
|
|
return $messages->fetch($type);
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authenticate user.
|
|
|
|
|
*
|
|
|
|
|
* @param array $form Form fields.
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authenticate($form)
|
|
|
|
|
{
|
2014-09-09 06:10:31 +03:00
|
|
|
if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
|
2014-10-07 12:12:21 +03:00
|
|
|
$file = CompiledYamlFile::instance(ACCOUNTS_DIR . $form['username'] . YAML_EXT);
|
2014-08-05 13:06:38 -07:00
|
|
|
if ($file->exists()) {
|
|
|
|
|
$user = new User($file->content());
|
2014-09-09 06:10:31 +03:00
|
|
|
$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 */
|
2014-08-29 11:27:53 +03:00
|
|
|
$grav = $this->grav;
|
2014-08-05 13:06:38 -07:00
|
|
|
$grav->redirect($this->uri->route());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->authorise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks user authorisation to the action.
|
|
|
|
|
*
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authorise($action = 'admin.login')
|
|
|
|
|
{
|
2014-09-09 06:10:31 +03:00
|
|
|
return $this->user->authorise($action);
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns edited page.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $route
|
|
|
|
|
* @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
|
|
|
|
|
* @return Data\Blueprint
|
|
|
|
|
*/
|
|
|
|
|
public function blueprints($type)
|
|
|
|
|
{
|
|
|
|
|
if ($this->blueprints === null) {
|
|
|
|
|
$this->blueprints = new Data\Blueprints(SYSTEM_DIR . '/blueprints/');
|
|
|
|
|
}
|
|
|
|
|
return $this->blueprints->get($type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets configuration data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param array $post
|
|
|
|
|
* @return Data\Data|null
|
|
|
|
|
* @throws \RuntimeException
|
|
|
|
|
*/
|
|
|
|
|
public function data($type, $post = array())
|
|
|
|
|
{
|
|
|
|
|
static $data = array();
|
|
|
|
|
|
|
|
|
|
if (isset($data[$type])) {
|
|
|
|
|
return $data[$type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'configuration':
|
|
|
|
|
case 'system':
|
|
|
|
|
$type = 'system';
|
2014-10-06 14:38:14 +03:00
|
|
|
$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);
|
2014-10-07 12:12:21 +03:00
|
|
|
$file = CompiledYamlFile::instance(USER_DIR . "config/{$type}.yaml");
|
2014-08-05 13:06:38 -07:00
|
|
|
$obj->file($file);
|
|
|
|
|
$data[$type] = $obj;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'settings':
|
|
|
|
|
case 'site':
|
|
|
|
|
$type = 'site';
|
2014-10-06 14:38:14 +03:00
|
|
|
$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);
|
2014-10-07 12:12:21 +03:00
|
|
|
$file = CompiledYamlFile::instance(USER_DIR . "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;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \RuntimeException("Data type '{$type}' doesn't exist!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data[$type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts dot notation to array notation.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function field($name)
|
|
|
|
|
{
|
|
|
|
|
$path = explode('.', $name);
|
|
|
|
|
|
|
|
|
|
return array_shift($path) . ($path ? '[' . implode('][', $path) . ']' : '');
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
/**
|
|
|
|
|
* Get all routes.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function routes()
|
|
|
|
|
{
|
2014-10-01 22:28:16 +03:00
|
|
|
/** @var Pages $pages */
|
|
|
|
|
$pages = $this->grav['pages'];
|
|
|
|
|
return $pages->routes();
|
2014-09-05 22:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all plugins.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function plugins()
|
|
|
|
|
{
|
2014-10-02 12:14:13 -07:00
|
|
|
if (!$this->localPackages) {
|
|
|
|
|
$this->localPackages = new LocalPackages();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->localPackages['plugins'];
|
2014-10-03 15:40:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all themes.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function themes()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!$this->localPackages) {
|
|
|
|
|
$this->localPackages = new LocalPackages();
|
|
|
|
|
}
|
2014-10-02 12:14:13 -07:00
|
|
|
|
2014-10-03 15:40:58 -06:00
|
|
|
return $this->localPackages['themes'];
|
2014-09-05 22:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
2014-08-05 13:06:38 -07:00
|
|
|
/**
|
|
|
|
|
* Get log file for fatal errors.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function logs()
|
|
|
|
|
{
|
2014-09-05 22:27:30 -06:00
|
|
|
if (!isset($this->logs)) {
|
2014-10-01 22:28:16 +03:00
|
|
|
$file = LogFile::instance(LOG_DIR . 'exception.log');
|
2014-08-05 13:06:38 -07:00
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
$content = $file->content();
|
2014-08-05 13:06:38 -07:00
|
|
|
|
2014-09-05 22:27:30 -06: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-09-05 22:27:30 -06:00
|
|
|
*/
|
2014-10-02 12:14:13 -07:00
|
|
|
public function latestPages($count = 10)
|
2014-09-05 22:27:30 -06:00
|
|
|
{
|
2014-10-01 22:28:16 +03:00
|
|
|
/** @var Pages $pages */
|
|
|
|
|
$pages = $this->grav['pages'];
|
|
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
$latest = array();
|
|
|
|
|
|
2014-10-01 22:28:16 +03:00
|
|
|
foreach ($pages->routes() as $url => $path) {
|
|
|
|
|
$page = $pages->dispatch($url);
|
2014-09-05 22:27:30 -06:00
|
|
|
$latest[$page->route()] = ['modified'=>$page->modified(),'page'=>$page];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sort based on modified
|
2014-10-02 12:14:13 -07:00
|
|
|
uasort($latest, function ($a, $b) {
|
2014-09-05 22:27:30 -06:00
|
|
|
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();
|
2014-09-05 22:27:30 -06:00
|
|
|
foreach ($latest as $item) {
|
2014-10-01 22:28:16 +03:00
|
|
|
$list[] = $item['page'];
|
2014-09-05 22:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2014-10-01 22:28:16 +03:00
|
|
|
$file = File::instance(LOG_DIR . $this->route . '.html');
|
2014-08-05 13:06:38 -07:00
|
|
|
$content = $file->content();
|
|
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the page creating it if it does not exist.
|
|
|
|
|
*
|
|
|
|
|
* @param $path
|
|
|
|
|
* @return Page
|
|
|
|
|
*/
|
|
|
|
|
protected function getPage($path)
|
|
|
|
|
{
|
|
|
|
|
/** @var Pages $pages */
|
2014-08-29 11:27:53 +03:00
|
|
|
$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);
|
|
|
|
|
$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-09-09 06:10:31 +03:00
|
|
|
// Add routing information.
|
2014-08-05 13:06:38 -07:00
|
|
|
$pages->addPage($page, $path);
|
|
|
|
|
|
|
|
|
|
// 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()};
|
|
|
|
|
$page->name($data['type'] . '.md');
|
|
|
|
|
$page->header(['title' => $data['title']]);
|
2014-09-26 14:10:32 +03:00
|
|
|
$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->child_type() ? $parent->child_type() : $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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static helper method to return current route.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function route()
|
|
|
|
|
{
|
2014-08-29 11:27:53 +03:00
|
|
|
return dirname('/' . Grav::instance()['admin']->route);
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
}
|