2014-08-05 13:06:38 -07:00
|
|
|
<?php
|
|
|
|
|
namespace Grav\Plugin;
|
|
|
|
|
|
2015-08-11 18:30:27 -06:00
|
|
|
use Grav\Common\Data;
|
2014-10-07 12:12:21 +03:00
|
|
|
use Grav\Common\File\CompiledYamlFile;
|
2015-08-11 18:30:27 -06:00
|
|
|
use Grav\Common\GPM\GPM;
|
2014-08-05 13:06:38 -07:00
|
|
|
use Grav\Common\Grav;
|
2015-08-17 18:22:15 -06:00
|
|
|
use Grav\Common\Language\LanguageCodes;
|
2015-08-11 18:30:27 -06:00
|
|
|
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;
|
2015-08-11 18:30:27 -06:00
|
|
|
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;
|
2014-09-26 14:10:32 +03:00
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2014-08-05 13:06:38 -07:00
|
|
|
|
2015-07-28 11:11:43 -06:00
|
|
|
define('LOGIN_REDIRECT_COOKIE', 'grav-login-redirect');
|
|
|
|
|
|
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;
|
|
|
|
|
|
2015-08-14 15:05:33 +02:00
|
|
|
/**
|
|
|
|
|
* @var Lang
|
|
|
|
|
*/
|
|
|
|
|
protected $lang;
|
|
|
|
|
|
2014-10-02 12:14:13 -07:00
|
|
|
/**
|
2015-04-04 14:49:40 +02:00
|
|
|
* @var Grav\Common\GPM\GPM
|
2014-10-02 12:14:13 -07:00
|
|
|
*/
|
2015-04-04 14:49:40 +02:00
|
|
|
protected $gpm;
|
2014-10-02 12:14:13 -07: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'];
|
2015-08-14 19:12:04 +02:00
|
|
|
$language = $this->grav['language'];
|
2015-08-18 21:07:11 +02:00
|
|
|
|
2015-08-14 19:12:04 +02:00
|
|
|
if ($language->enabled()) {
|
|
|
|
|
$this->multilang = true;
|
|
|
|
|
$this->languages_enabled = $this->grav['config']->get('system.languages.supported', []);
|
|
|
|
|
|
|
|
|
|
//Set the currently active language for the admin
|
|
|
|
|
$language = $this->grav['uri']->param('lang');
|
|
|
|
|
if (!$language) {
|
2015-08-18 21:07:11 +02:00
|
|
|
if (!$this->session->admin_lang) $this->session->admin_lang = 'en';
|
2015-08-14 19:12:04 +02:00
|
|
|
$language = $this->session->admin_lang;
|
|
|
|
|
}
|
|
|
|
|
$this->grav['language']->setActive($language ?: 'en');
|
|
|
|
|
} else {
|
|
|
|
|
$this->grav['language']->setActive('en');
|
|
|
|
|
$this->multilang = false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
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'];
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2014-09-09 06:10:31 +03:00
|
|
|
return $messages->fetch($type);
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authenticate user.
|
|
|
|
|
*
|
|
|
|
|
* @param array $form Form fields.
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
2014-08-05 13:06:38 -07:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authenticate($form)
|
|
|
|
|
{
|
2014-09-09 06:10:31 +03:00
|
|
|
if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
|
2015-04-11 17:47:37 +02:00
|
|
|
$user = User::load($form['username']);
|
2015-08-20 11:29:37 -06:00
|
|
|
|
|
|
|
|
//default to english if language not set
|
|
|
|
|
if (empty($user->language)) {
|
|
|
|
|
$user->set('language', 'en');
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-11 17:47:37 +02:00
|
|
|
if ($user->exists()) {
|
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;
|
2015-07-28 11:11:43 -06:00
|
|
|
|
2015-08-18 21:13:04 +02:00
|
|
|
$this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN', [$this->user->language]), 'info');
|
2015-07-28 16:10:48 -06:00
|
|
|
|
2015-07-28 12:09:53 -06:00
|
|
|
$redirect_route = $this->uri->route();
|
2015-07-28 11:11:43 -06:00
|
|
|
$grav->redirect($redirect_route);
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->authorise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks user authorisation to the action.
|
|
|
|
|
*
|
2015-08-11 18:30:27 -06:00
|
|
|
* @param string $action
|
|
|
|
|
*
|
2014-08-05 13:06:38 -07:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function authorise($action = 'admin.login')
|
|
|
|
|
{
|
2015-08-11 18:30:27 -06:00
|
|
|
$action = (array)$action;
|
2015-05-11 16:35:23 +02:00
|
|
|
|
|
|
|
|
foreach ($action as $a) {
|
2015-08-11 18:30:27 -06:00
|
|
|
if ($this->user->authorise($a)) {
|
2015-05-11 16:35:23 +02:00
|
|
|
return true;
|
2015-08-11 18:30:27 -06:00
|
|
|
}
|
2015-05-11 16:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns edited page.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $route
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
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
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
2014-08-05 13:06:38 -07:00
|
|
|
* @return Data\Blueprint
|
|
|
|
|
*/
|
|
|
|
|
public function blueprints($type)
|
|
|
|
|
{
|
|
|
|
|
if ($this->blueprints === null) {
|
2015-08-20 11:58:14 +03:00
|
|
|
$this->blueprints = new Data\Blueprints('blueprints://');
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2014-08-05 13:06:38 -07:00
|
|
|
return $this->blueprints->get($type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets configuration data.
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
2015-08-11 18:30:27 -06:00
|
|
|
* @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';
|
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);
|
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';
|
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);
|
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
|
|
|
|
2014-10-10 15:04:12 +03: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
|
|
|
|
|
*/
|
2015-04-04 14:49:40 +02:00
|
|
|
public function gpm()
|
|
|
|
|
{
|
|
|
|
|
if (!$this->gpm) {
|
2015-09-01 18:14:54 +02:00
|
|
|
try {
|
|
|
|
|
$this->gpm = new GPM();
|
|
|
|
|
} catch (\Exception $e) {}
|
2015-04-04 14:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->gpm;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 13:06:38 -07:00
|
|
|
/**
|
|
|
|
|
* Converts dot notation to array notation.
|
|
|
|
|
*
|
|
|
|
|
* @param string $name
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
2014-08-05 13:06:38 -07:00
|
|
|
* @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
|
|
|
|
|
*/
|
2015-08-20 14:55:24 -06:00
|
|
|
public function routes($unique = false)
|
2014-09-05 22:27:30 -06:00
|
|
|
{
|
2014-10-01 22:28:16 +03:00
|
|
|
/** @var Pages $pages */
|
|
|
|
|
$pages = $this->grav['pages'];
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2015-08-20 14:55:24 -06:00
|
|
|
if ($unique) {
|
|
|
|
|
$routes = array_unique($pages->routes());
|
|
|
|
|
} else {
|
|
|
|
|
$routes = $pages->routes();
|
|
|
|
|
}
|
|
|
|
|
return $routes;
|
2014-09-05 22:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
2015-09-08 11:13:52 -06:00
|
|
|
/**
|
|
|
|
|
* Get All modular types
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function modularTypes()
|
|
|
|
|
{
|
|
|
|
|
return Pages::modularTypes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
/**
|
|
|
|
|
* Get all plugins.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-04-04 14:49:40 +02:00
|
|
|
public function plugins($local = true)
|
2014-09-05 22:27:30 -06:00
|
|
|
{
|
2015-04-04 14:49:40 +02:00
|
|
|
$gpm = $this->gpm();
|
2015-08-11 18:30:27 -06:00
|
|
|
|
|
|
|
|
return $local ? $gpm->getInstalledPlugins() : $gpm->getRepositoryPlugins()->filter(function (
|
|
|
|
|
$package,
|
|
|
|
|
$slug
|
|
|
|
|
) use ($gpm) {
|
2015-04-04 14:49:40 +02:00
|
|
|
return !$gpm->isPluginInstalled($slug);
|
|
|
|
|
});
|
2014-10-03 15:40:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all themes.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-04-04 14:49:40 +02:00
|
|
|
public function themes($local = true)
|
2014-10-03 15:40:58 -06:00
|
|
|
{
|
2015-04-04 14:49:40 +02:00
|
|
|
$gpm = $this->gpm();
|
2015-08-11 18:30:27 -06:00
|
|
|
|
|
|
|
|
return $local ? $gpm->getInstalledThemes() : $gpm->getRepositoryThemes()->filter(function ($package, $slug) use
|
|
|
|
|
(
|
|
|
|
|
$gpm
|
|
|
|
|
) {
|
2015-04-04 14:49:40 +02:00
|
|
|
return !$gpm->isThemeInstalled($slug);
|
|
|
|
|
});
|
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)) {
|
2015-01-11 00:47:27 +01:00
|
|
|
$file = LogFile::instance($this->grav['locator']->findResource('log://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);
|
|
|
|
|
}
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
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
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
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-12-10 10:23:22 -07:00
|
|
|
if ($page && $page->routable()) {
|
2015-08-11 18:30:27 -06:00
|
|
|
$latest[$page->route()] = ['modified' => $page->modified(), 'page' => $page];
|
2014-12-10 10:23:22 -07:00
|
|
|
}
|
2014-09-05 22:27:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2014-09-05 22:27:30 -06:00
|
|
|
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()
|
|
|
|
|
{
|
2015-01-11 00:47:27 +01:00
|
|
|
$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 [
|
2015-08-11 18:30:27 -06:00
|
|
|
'days' => '∞',
|
|
|
|
|
'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 [
|
2015-08-11 18:30:27 -06:00
|
|
|
'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
|
2015-08-11 18:30:27 -06:00
|
|
|
*
|
2014-08-05 13:06:38 -07:00
|
|
|
* @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);
|
2015-08-07 12:05:34 +02:00
|
|
|
|
|
|
|
|
if ($slug == '') {
|
2015-08-11 18:30:27 -06:00
|
|
|
return null;
|
2015-08-07 12:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-19 12:51:55 -06:00
|
|
|
$ppath = str_replace('\\', '/' , dirname($path));
|
2014-08-05 13:06:38 -07:00
|
|
|
|
|
|
|
|
// Find or create parent(s).
|
|
|
|
|
$parent = $this->getPage($ppath != '/' ? $ppath : '');
|
|
|
|
|
|
|
|
|
|
// Create page.
|
|
|
|
|
$page = new Page;
|
|
|
|
|
$page->parent($parent);
|
2015-08-11 18:30:27 -06:00
|
|
|
$page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
|
2014-08-05 13:06:38 -07:00
|
|
|
|
2014-09-09 06:10:31 +03: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()};
|
2015-08-05 19:04:55 -06:00
|
|
|
|
2015-08-14 06:23:16 -06:00
|
|
|
$header = ['title' => $data['title']];
|
|
|
|
|
|
|
|
|
|
if (isset($data['visible'])) {
|
2015-08-14 14:28:24 -06:00
|
|
|
if ($data['visible'] == '' || $data['visible']) {
|
2015-08-14 06:23:16 -06:00
|
|
|
// if auto (ie '')
|
|
|
|
|
$children = $page->parent()->children();
|
|
|
|
|
foreach ($children as $child) {
|
|
|
|
|
if ($child->order()) {
|
|
|
|
|
// set page order
|
|
|
|
|
$page->order(1000);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2015-08-05 19:04:55 -06:00
|
|
|
|
2015-08-29 10:43:58 -06:00
|
|
|
if ($data['name'] == 'modular') {
|
2015-08-05 19:04:55 -06:00
|
|
|
$header['body_classes'] = 'modular';
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 10:43:58 -06:00
|
|
|
$name = $page->modular() ? str_replace('modular/', '', $data['name']) : $data['name'];
|
2015-08-05 18:07:55 -06:00
|
|
|
$page->name($name . '.md');
|
2015-08-05 19:04:55 -06:00
|
|
|
$page->header($header);
|
2015-08-20 18:11:54 -06:00
|
|
|
$page->frontmatter(Yaml::dump((array)$page->header(), 10, 2, false));
|
2014-08-05 13:06:38 -07:00
|
|
|
} else {
|
|
|
|
|
// Find out the type by looking at the parent.
|
2015-08-11 18:30:27 -06:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 19:44:07 +02:00
|
|
|
/**
|
|
|
|
|
* Return the languages available in the admin
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2015-08-17 18:22:15 -06:00
|
|
|
public static function adminLanguages()
|
|
|
|
|
{
|
|
|
|
|
$languages = [];
|
|
|
|
|
$lang_data = Yaml::parse(file_get_contents(__DIR__ . '/../languages.yaml'));
|
|
|
|
|
foreach ($lang_data as $lang => $values) {
|
|
|
|
|
$languages[$lang] = LanguageCodes::getNativeName($lang);
|
|
|
|
|
}
|
|
|
|
|
return $languages;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-18 19:44:07 +02:00
|
|
|
/**
|
|
|
|
|
* Return the languages available in the site
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function siteLanguages()
|
|
|
|
|
{
|
|
|
|
|
$languages = [];
|
|
|
|
|
$lang_data = Grav::instance()['config']->get('system.languages.supported', []);
|
|
|
|
|
|
|
|
|
|
foreach ($lang_data as $index => $lang) {
|
|
|
|
|
$languages[$lang] = LanguageCodes::getNativeName($lang);
|
|
|
|
|
}
|
|
|
|
|
return $languages;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 13:06:38 -07:00
|
|
|
/**
|
|
|
|
|
* Static helper method to return current route.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public static function route()
|
|
|
|
|
{
|
2015-08-20 18:11:54 -06:00
|
|
|
$pages = Grav::instance()['pages'];
|
|
|
|
|
$route = '/' . ltrim(Grav::instance()['admin']->route, '/');
|
|
|
|
|
|
|
|
|
|
$page = $pages->dispatch($route);
|
2015-08-28 16:02:32 +02:00
|
|
|
$parent_route = null;
|
|
|
|
|
if ($page) {
|
|
|
|
|
$parent = $page->parent();
|
|
|
|
|
$parent_route = $parent->rawRoute();
|
|
|
|
|
}
|
2015-08-20 18:11:54 -06:00
|
|
|
|
|
|
|
|
return $parent_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-08-11 18:30:27 -06:00
|
|
|
*
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-11 18:30:27 -06:00
|
|
|
|
2015-08-14 15:05:33 +02:00
|
|
|
/**
|
|
|
|
|
* Renders phpinfo
|
|
|
|
|
*
|
|
|
|
|
* @return string The phpinfo() output
|
|
|
|
|
*/
|
2015-08-11 18:30:27 -06:00
|
|
|
function phpinfo() {
|
|
|
|
|
ob_start();
|
|
|
|
|
phpinfo();
|
|
|
|
|
$pinfo = ob_get_contents();
|
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
|
|
$pinfo = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);
|
|
|
|
|
return $pinfo;
|
|
|
|
|
}
|
2015-08-14 15:05:33 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Translate a string to the user-defined language
|
|
|
|
|
*
|
|
|
|
|
* @param $string the string to translate
|
|
|
|
|
*/
|
|
|
|
|
public function translate($string) {
|
2015-08-18 18:48:15 +02:00
|
|
|
return $this->_translate($string, [$this->grav['user']->authenticated ? $this->grav['user']->language : 'en']);
|
2015-08-14 15:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-18 18:48:15 +02:00
|
|
|
public function _translate($args, Array $languages = null, $array_support = false, $html_out = false)
|
|
|
|
|
{
|
|
|
|
|
if (is_array($args)) {
|
|
|
|
|
$lookup = array_shift($args);
|
|
|
|
|
} else {
|
|
|
|
|
$lookup = $args;
|
|
|
|
|
$args = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($lookup) {
|
2015-08-19 10:17:50 +02:00
|
|
|
if (empty($languages) || reset($languages) == null) {
|
2015-08-18 18:48:15 +02:00
|
|
|
if ($this->grav['config']->get('system.languages.translations_fallback', true)) {
|
|
|
|
|
$languages = $this->grav['language']->getFallbackLanguages();
|
|
|
|
|
} else {
|
|
|
|
|
$languages = (array)$this->grav['language']->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$languages = ['en'];
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-29 14:39:37 +02:00
|
|
|
|
2015-08-18 18:48:15 +02:00
|
|
|
foreach ((array)$languages as $lang) {
|
|
|
|
|
$translation = $this->grav['language']->getTranslation($lang, $lookup, $array_support);
|
|
|
|
|
|
2015-08-24 19:09:07 +02:00
|
|
|
if (!$translation) {
|
2015-08-29 14:39:37 +02:00
|
|
|
$language = $this->grav['language']->getDefault() ?: 'en';
|
|
|
|
|
$translation = $this->grav['language']->getTranslation($language, $lookup, $array_support);
|
2015-08-24 19:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-18 18:48:15 +02:00
|
|
|
if ($translation) {
|
|
|
|
|
if (count($args) >= 1) {
|
|
|
|
|
return vsprintf($translation, $args);
|
|
|
|
|
} else {
|
|
|
|
|
return $translation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $lookup;
|
|
|
|
|
}
|
2014-08-05 13:06:38 -07:00
|
|
|
}
|