2014-08-05 13:06:38 -07:00
< ? php
namespace Grav\Plugin ;
2015-12-10 12:07:36 +01:00
use Grav\Common\File\CompiledYamlFile ;
2014-10-07 17:01:40 -07:00
use Grav\Common\GPM\GPM ;
use Grav\Common\Grav ;
2015-12-10 13:22:23 -07:00
use Grav\Common\Inflector ;
2015-10-22 15:53:42 -06:00
use Grav\Common\Language\Language ;
2014-08-29 11:27:53 +03:00
use Grav\Common\Page\Page ;
use Grav\Common\Page\Pages ;
2014-10-07 17:01:40 -07:00
use Grav\Common\Plugin ;
2014-08-29 11:27:53 +03:00
use Grav\Common\Uri ;
2015-12-10 12:07:36 +01:00
use Grav\Common\User\User ;
2014-10-01 22:28:16 +03:00
use RocketTheme\Toolbox\File\File ;
2015-05-20 17:19:12 +02:00
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
{
2016-01-22 10:42:38 +02:00
public $features = [
'blueprints' => 1000 ,
];
2014-08-05 13:06:38 -07:00
/**
* @ 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
*/
2014-09-08 18:32:13 -06:00
protected $popularity ;
2014-10-01 22:28:16 +03:00
/**
* @ var string
*/
protected $base ;
2016-04-26 12:57:29 -06:00
protected $version ;
2014-08-29 11:27:53 +03:00
/**
* @ return array
*/
2014-09-23 12:45:26 -07:00
public static function getSubscribedEvents ()
{
2015-10-16 21:16:34 +02:00
if ( ! Grav :: instance ()[ 'config' ] -> get ( 'plugins.admin-pro.enabled' )) {
return [
2016-04-14 15:28:48 -06:00
'onPluginsInitialized' => [[ 'setup' , 100000 ], [ 'onPluginsInitialized' , 1001 ]],
2015-12-10 12:07:36 +01:00
'onShutdown' => [ 'onShutdown' , 1000 ],
2016-01-10 17:39:54 +01:00
'onFormProcessed' => [ 'onFormProcessed' , 0 ],
'onAdminDashboard' => [ 'onAdminDashboard' , 0 ],
2015-10-16 21:16:34 +02:00
];
2015-12-10 12:07:36 +01:00
}
2015-12-10 22:28:30 +01:00
return [];
2015-12-10 12:07:36 +01:00
}
2015-12-10 11:02:19 -07:00
/**
* If the admin path matches , initialize the Login plugin configuration and set the admin
* as active .
*/
public function setup ()
{
$route = $this -> config -> get ( 'plugins.admin.route' );
if ( ! $route ) {
return ;
}
2015-12-10 13:22:23 -07:00
$this -> base = '/' . trim ( $route , '/' );
$this -> uri = $this -> grav [ 'uri' ];
2015-12-10 11:02:19 -07:00
2015-12-10 16:14:13 -07:00
// check for existence of a user account
$account_dir = $file_path = $this -> grav [ 'locator' ] -> findResource ( 'account://' );
2016-01-17 23:08:19 +01:00
$user_check = glob ( $account_dir . '/*.yaml' );
2015-12-10 11:02:19 -07:00
2015-12-10 16:14:13 -07:00
// If no users found, go to register
2016-01-17 23:08:19 +01:00
if ( $user_check == false || count (( array ) $user_check ) == 0 ) {
2015-12-10 16:14:13 -07:00
if ( ! $this -> isAdminPath ()) {
$this -> grav -> redirect ( $this -> base );
2015-12-10 11:02:19 -07:00
}
2015-12-10 16:14:13 -07:00
$this -> template = 'register' ;
2015-12-10 11:02:19 -07:00
}
// Only activate admin if we're inside the admin path.
2015-12-10 13:22:23 -07:00
if ( $this -> isAdminPath ()) {
2015-12-10 11:02:19 -07:00
$this -> active = true ;
}
}
2015-12-10 12:07:36 +01:00
/**
* Validate a value . Currently validates
*
* - 'user' for username format and username availability .
* - 'password1' for password format
* - 'password2' for equality to password1
*
* @ param string $type The field type
* @ param string $value The field value
* @ param string $extra Any extra value required
*
2016-01-21 09:46:38 +02:00
* @ return bool
2015-12-10 12:07:36 +01:00
*/
protected function validate ( $type , $value , $extra = '' )
{
switch ( $type ) {
case 'username_format' :
if ( ! preg_match ( '/^[a-z0-9_-]{3,16}$/' , $value )) {
return false ;
}
return true ;
case 'password1' :
if ( ! preg_match ( '/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/' , $value )) {
return false ;
}
return true ;
case 'password2' :
if ( strcmp ( $value , $extra )) {
return false ;
}
return true ;
}
2016-01-21 09:46:38 +02:00
return false ;
2015-12-10 12:07:36 +01:00
}
/**
* Process the admin registration form .
*
* @ param Event $event
*/
public function onFormProcessed ( Event $event )
{
$form = $event [ 'form' ];
$action = $event [ 'action' ];
switch ( $action ) {
case 'register_admin_user' :
if ( ! $this -> config -> get ( 'plugins.login.enabled' )) {
throw new \RuntimeException ( $this -> grav [ 'language' ] -> translate ( 'PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED' ));
}
$data = [];
$username = $form -> value ( 'username' );
2015-12-10 19:34:02 +01:00
if ( $form -> value ( 'password1' ) != $form -> value ( 'password2' )) {
2015-12-10 12:07:36 +01:00
$this -> grav -> fireEvent ( 'onFormValidationError' ,
new Event ([
'form' => $form ,
'message' => $this -> grav [ 'language' ] -> translate ( 'PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH' )
]));
$event -> stopPropagation ();
return ;
}
2015-12-10 19:34:02 +01:00
2015-12-10 12:07:36 +01:00
$data [ 'password' ] = $form -> value ( 'password1' );
$fields = [
'email' ,
'fullname' ,
'title'
];
foreach ( $fields as $field ) {
// Process value of field if set in the page process.register_user
if ( ! isset ( $data [ $field ]) && $form -> value ( $field )) {
$data [ $field ] = $form -> value ( $field );
}
}
unset ( $data [ 'password1' ]);
unset ( $data [ 'password2' ]);
// Don't store the username: that is part of the filename
unset ( $data [ 'username' ]);
2015-12-11 10:34:40 -07:00
// Extra lowercase to ensure file is saved lowercase
$username = strtolower ( $username );
2015-12-10 13:22:23 -07:00
$inflector = new Inflector ();
$data [ 'fullname' ] = isset ( $data [ 'fullname' ]) ? $data [ 'fullname' ] : $inflector -> titleize ( $username );
$data [ 'title' ] = isset ( $data [ 'title' ]) ? $data [ 'title' ] : 'Administrator' ;
2015-12-10 12:07:36 +01:00
$data [ 'state' ] = 'enabled' ;
$data [ 'access' ] = [ 'admin' => [ 'login' => true , 'super' => true ], 'site' => [ 'login' => true ]];
// Create user object and save it
$user = new User ( $data );
$file = CompiledYamlFile :: instance ( $this -> grav [ 'locator' ] -> findResource ( 'user://accounts/' . $username . YAML_EXT , true , true ));
$user -> file ( $file );
$user -> save ();
$user = User :: load ( $username );
//Login user
$this -> grav [ 'session' ] -> user = $user ;
unset ( $this -> grav [ 'user' ]);
$this -> grav [ 'user' ] = $user ;
$user -> authenticated = $user -> authorize ( 'site.login' );
$messages = $this -> grav [ 'messages' ];
$messages -> add ( $this -> grav [ 'language' ] -> translate ( 'PLUGIN_ADMIN.LOGIN_LOGGED_IN' ), 'info' );
2015-12-10 13:22:23 -07:00
$this -> grav -> redirect ( $this -> base );
2015-12-10 12:07:36 +01:00
break ;
2015-10-16 21:16:34 +02:00
}
2014-08-29 11:27:53 +03:00
}
2014-10-07 17:01:40 -07:00
/**
2015-07-30 12:20:25 +02:00
* If the admin plugin is set as active , initialize the admin
2014-09-09 06:10:31 +03:00
*/
public function onPluginsInitialized ()
{
// Only activate admin if we're inside the admin path.
if ( $this -> active ) {
2016-04-07 21:01:38 +02:00
2016-04-26 12:57:29 -06:00
// Store this version and prefer newer method
if ( method_exists ( $this , 'getBlueprint' )) {
$this -> version = $this -> getBlueprint () -> version ;
} else {
$this -> version = $this -> grav [ 'plugins' ] -> get ( 'admin' ) -> blueprints () -> version ;
}
2016-04-26 12:42:16 -06:00
// Test for correct Grav 1.1 version
2016-04-26 18:53:13 +02:00
if ( version_compare ( GRAV_VERSION , '1.1.0-beta.1' , '<' )) {
$messages = $this -> grav [ 'messages' ];
2016-04-26 12:10:56 -06:00
$messages -> add ( $this -> grav [ 'language' ] -> translate ([ 'PLUGIN_ADMIN.NEEDS_GRAV_1_1' , GRAV_VERSION ]), 'error' );
2016-04-26 18:53:13 +02:00
}
2016-04-14 15:28:48 -06:00
// Have a unique Admin-only Cache key
if ( method_exists ( $this -> grav [ 'cache' ], 'setKey' )){
$cache = $this -> grav [ 'cache' ];
$cache_key = $cache -> getKey ();
$cache -> setKey ( $cache_key . '$' );
}
2016-04-07 21:01:38 +02:00
// Turn on Twig autoescaping
2016-04-07 16:17:19 -06:00
if ( method_exists ( $this -> grav [ 'twig' ], 'setAutoescape' )) {
$this -> grav [ 'twig' ] -> setAutoescape ( true );
}
2016-04-07 21:01:38 +02:00
2015-12-03 17:29:46 +01:00
if ( php_sapi_name () == 'cli-server' ) {
throw new \RuntimeException ( 'The Admin Plugin cannot run on the PHP built-in webserver. It needs Apache, Nginx or another full-featured web server.' , 500 );
}
2015-10-20 10:36:48 +02:00
$this -> grav [ 'debugger' ] -> addMessage ( " Admin Basic " );
2014-09-09 07:57:53 +03:00
$this -> initializeAdmin ();
2014-09-09 14:03:01 -06:00
2015-12-03 14:47:34 -07:00
// Disable Asset pipelining (old method - remove this after Grav is updated)
if ( ! method_exists ( $this -> grav [ 'assets' ], 'setJsPipeline' )) {
$this -> config -> set ( 'system.assets.css_pipeline' , false );
$this -> config -> set ( 'system.assets.js_pipeline' , false );
}
2015-11-25 18:31:25 +01:00
// Replace themes service with admin.
2016-01-28 14:38:01 +01:00
$this -> grav [ 'themes' ] = function () {
2015-11-25 18:31:25 +01:00
require_once __DIR__ . '/classes/themes.php' ;
return new Themes ( $this -> grav );
};
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 ();
2016-05-04 14:26:27 -06:00
// Fire even to register permissions from other plugins
$this -> grav -> fireEvent ( 'onAdminRegisterPermissions' , new Event ([ 'admin' => $this -> admin ]));
2014-08-05 13:06:38 -07:00
}
2015-10-19 11:46:51 +02:00
protected function initializeController ( $task , $post ) {
require_once __DIR__ . '/classes/controller.php' ;
$controller = new AdminController ( $this -> grav , $this -> template , $task , $this -> route , $post );
$controller -> execute ();
$controller -> redirect ();
}
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 .
*/
2014-08-29 11:27:53 +03:00
public function onPagesInitialized ()
2014-08-05 13:06:38 -07:00
{
2014-09-21 17:17:08 -06: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' ), '/' );
2014-10-06 19:03:57 -06:00
// set the default if not set before
2015-01-11 00:47:27 +01:00
$this -> session -> expert = $this -> session -> expert ? : false ;
2014-10-06 19:03:57 -06:00
2014-09-21 17:17:08 -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 */
2014-08-29 11:27:53 +03:00
$pages = $this -> grav [ 'pages' ];
2014-09-05 22:27:30 -06:00
$this -> grav [ 'admin' ] -> routes = $pages -> routes ();
2015-01-11 00:47:27 +01:00
// Remove default route from routes.
2015-01-11 18:33:45 +01:00
if ( isset ( $this -> grav [ 'admin' ] -> routes [ '/' ])) {
unset ( $this -> grav [ 'admin' ] -> routes [ '/' ]);
2015-01-11 00:47:27 +01:00
}
2015-08-07 12:05:34 +02:00
$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 ) {
2015-10-19 11:46:51 +02:00
$this -> initializeController ( $task , $post );
2014-08-05 13:06:38 -07:00
} elseif ( $this -> template == 'logs' && $this -> route ) {
// Display RAW error message.
echo $this -> admin -> logEntry ();
exit ();
}
2014-09-09 06:10:31 +03:00
$self = $this ;
2014-08-05 13:06:38 -07:00
2015-12-09 21:37:34 -07:00
// make sure page is not frozen!
unset ( $this -> grav [ 'page' ]);
2016-01-28 14:03:53 +01:00
$this -> admin -> pagesCount ();
2014-09-09 06:10:31 +03:00
// Replace page service with admin.
2014-09-23 12:45:26 -07:00
$this -> grav [ 'page' ] = function () use ( $self ) {
2014-09-09 06:10:31 +03:00
$page = new Page ;
2015-09-07 11:49:41 +02:00
2016-03-11 16:51:06 -07:00
// If the page cannot be found in other plugins, try looking in admin plugin itself.
if ( file_exists ( __DIR__ . " /pages/admin/ { $self -> template } .md " )) {
$page -> init ( new \SplFileInfo ( __DIR__ . " /pages/admin/ { $self -> template } .md " ));
$page -> slug ( basename ( $self -> template ));
return $page ;
}
2015-09-07 11:49:41 +02:00
// Allows pages added by plugins in admin
2016-03-11 16:02:45 -07:00
$plugins = $this -> grav [ 'plugins' ];
$locator = $this -> grav [ 'locator' ];
2015-09-07 11:49:41 +02:00
2016-03-11 16:02:45 -07:00
foreach ( $plugins as $plugin ) {
$path = $locator -> findResource (
" user://plugins/ { $plugin -> name } /admin/pages/ { $self -> template } .md " );
2015-10-26 21:30:33 +01:00
2016-03-11 16:02:45 -07:00
if ( $path ) {
2015-10-26 21:30:33 +01:00
$page -> init ( new \SplFileInfo ( $path ));
$page -> slug ( basename ( $self -> template ));
return $page ;
2015-09-07 11:49:41 +02:00
}
}
2016-01-21 09:46:38 +02:00
return null ;
2014-09-09 06:10:31 +03:00
};
2015-11-25 15:43:26 -07:00
if ( empty ( $this -> grav [ 'page' ])) {
2016-04-21 15:10:02 +02:00
if ( $this -> grav [ 'user' ] -> authenticated ) {
2016-04-21 15:08:37 +02:00
$event = $this -> grav -> fireEvent ( 'onPageNotFound' );
if ( isset ( $event -> page )) {
unset ( $this -> grav [ 'page' ]);
$this -> grav [ 'page' ] = $event -> page ;
} else {
throw new \RuntimeException ( 'Page Not Found' , 404 );
}
2016-04-21 15:10:02 +02:00
} else {
2016-04-21 15:08:37 +02:00
$this -> grav -> redirect ( $this -> base );
2015-11-25 15:43:26 -07:00
}
}
2016-03-03 14:58:11 -07:00
// Explicitly set a timestamp on assets
$this -> grav [ 'assets' ] -> setTimestamp ( substr ( md5 ( GRAV_VERSION . $this -> grav [ 'config' ] -> checksum ()), 0 , 10 ));
2014-08-05 13:06:38 -07:00
}
2016-01-10 17:17:04 +01:00
/**
* Handles initializing the assets
*/
2015-12-03 14:47:34 -07:00
public function onAssetsInitialized ()
{
// Disable Asset pipelining
$assets = $this -> grav [ 'assets' ];
2016-03-03 14:58:11 -07:00
$assets -> setJsPipeline ( false );
$assets -> setCssPipeline ( false );
2015-12-03 14:47:34 -07:00
}
2014-08-05 13:06:38 -07:00
/**
* Add twig paths to plugin templates .
*/
2014-08-29 11:27:53 +03:00
public function onTwigTemplatePaths ()
2014-08-05 13:06:38 -07:00
{
2015-05-20 17:19:12 +02: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 ;
2015-02-28 13:41:47 -07:00
2014-08-05 13:06:38 -07:00
}
/**
* Set all twig variables for generating output .
*/
2014-08-29 11:27:53 +03:00
public function onTwigSiteVariables ()
2014-08-05 13:06:38 -07:00
{
2014-08-29 11:27:53 +03:00
$twig = $this -> grav [ 'twig' ];
2014-08-05 13:06:38 -07:00
$twig -> twig_vars [ 'location' ] = $this -> template ;
2015-12-10 18:17:09 +01:00
$twig -> twig_vars [ 'base_url_relative_frontend' ] = $twig -> twig_vars [ 'base_url_relative' ] ? : '/' ;
2015-08-19 14:58:18 -06:00
$twig -> twig_vars [ 'admin_route' ] = trim ( $this -> config -> get ( 'plugins.admin.route' ), '/' );
2015-10-24 13:14:00 -06:00
$twig -> twig_vars [ 'base_url_relative' ] =
$twig -> twig_vars [ 'base_url_simple' ] . '/' . $twig -> twig_vars [ 'admin_route' ];
2016-01-24 19:27:53 -07:00
$twig -> twig_vars [ 'theme_url' ] = $this -> grav [ 'locator' ] -> findResource ( 'plugin://admin/themes/' . $this -> theme , false );
2014-08-29 11:59:43 +03:00
$twig -> twig_vars [ 'base_url' ] = $twig -> twig_vars [ 'base_url_relative' ];
2015-08-21 15:33:04 -06:00
$twig -> twig_vars [ 'base_path' ] = GRAV_ROOT ;
2014-08-05 13:06:38 -07:00
$twig -> twig_vars [ 'admin' ] = $this -> admin ;
2016-04-26 12:57:29 -06:00
$twig -> twig_vars [ 'admin_version' ] = $this -> version ;
2014-08-05 13:06:38 -07:00
2015-09-07 11:48:42 +02:00
// Gather Plugin-hooked nav items
2015-10-26 21:30:33 +01:00
$this -> grav -> fireEvent ( 'onAdminMenu' );
2014-08-05 13:06:38 -07:00
switch ( $this -> template ) {
2014-09-08 18:32:13 -06:00
case 'dashboard' :
$twig -> twig_vars [ 'popularity' ] = $this -> popularity ;
2016-01-10 17:39:54 +01:00
// Gather Plugin-hooked dashboard items
$this -> grav -> fireEvent ( 'onAdminDashboard' );
2014-08-05 13:06:38 -07:00
break ;
}
}
2014-09-03 22:22:03 -06:00
2016-01-10 17:17:04 +01:00
/**
* Handles the shutdown
*/
2014-09-06 16:59:58 -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' )) {
2014-09-08 18:32:13 -06:00
// Only track non-admin
if ( ! $this -> active ) {
$this -> popularity -> trackHit ();
2014-09-06 16:59:58 -06:00
}
}
}
2015-07-30 12:20:25 +02:00
/**
* Handles getting GPM updates
*/
2014-10-07 17:01:40 -07:00
public function onTaskGPM ()
{
2016-04-08 11:21:11 +02:00
$task = 'GPM' ;
if ( ! $this -> admin -> authorize ([ 'admin.maintenance' , 'admin.super' ])) {
$this -> admin -> json_response = [
'status' => 'unauthorized' ,
'message' => $this -> admin -> translate ( 'PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK' ) . ' ' . $task . '.'
];
return false ;
}
2014-10-07 17:01:40 -07:00
$action = $_POST [ 'action' ]; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates
2015-08-07 14:08:12 -07:00
$flush = isset ( $_POST [ 'flush' ]) && $_POST [ 'flush' ] == true ? true : false ;
2014-10-07 17:01:40 -07:00
if ( isset ( $this -> grav [ 'session' ])) {
$this -> grav [ 'session' ] -> close ();
}
try {
2015-08-07 14:08:12 -07:00
$gpm = new GPM ( $flush );
2014-10-07 17:01:40 -07:00
switch ( $action ) {
case 'getUpdates' :
$resources_updates = $gpm -> getUpdatable ();
2015-11-01 12:05:07 +01:00
if ( $gpm -> grav != null ) {
$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 " ,
" payload " => [ " resources " => $resources_updates , " grav " => $grav_updates , " installed " => $gpm -> countInstalled (), 'flushed' => $flush ]
]);
} else {
echo json_encode ([ " status " => " error " , " message " => " Cannot connect to the GPM " ]);
}
2014-10-07 17:01:40 -07:00
break ;
}
} catch ( \Exception $e ) {
2015-05-11 16:37:43 +02:00
echo json_encode ([ " status " => " error " , " message " => $e -> getMessage ()]);
2014-10-07 17:01:40 -07:00
}
exit ;
}
2016-02-29 14:21:59 +02:00
/**
* Get list of form field types specified in this plugin . Only special types needs to be listed .
*
* @ return array
*/
public function getFormFieldTypes ()
{
return [
'column' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-02-29 14:21:59 +02:00
],
'columns' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-02-29 14:21:59 +02:00
],
'fieldset' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-02-29 14:21:59 +02:00
],
'section' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-02-29 14:21:59 +02:00
],
'tab' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-02-29 14:21:59 +02:00
],
'tabs' => [
2016-03-03 20:59:59 +02:00
'input@' => false
2016-03-10 19:56:38 +02:00
],
2016-03-15 14:38:17 +02:00
'key' => [
'input@' => false
],
2016-03-10 19:56:38 +02:00
'list' => [
'array' => true
2016-02-29 14:21:59 +02:00
]
];
}
2015-07-30 12:20:25 +02:00
/**
* Initialize the admin .
*
* @ throws \RuntimeException
*/
2014-09-03 22:22:03 -06:00
protected function initializeAdmin ()
{
2014-09-09 07:57:53 +03:00
$this -> enable ([
2015-08-13 20:26:50 +02:00
'onTwigExtensions' => [ 'onTwigExtensions' , 1000 ],
2014-10-07 17:01:40 -07:00
'onPagesInitialized' => [ 'onPagesInitialized' , 1000 ],
2014-09-09 07:57:53 +03:00
'onTwigTemplatePaths' => [ 'onTwigTemplatePaths' , 1000 ],
2014-10-07 17:01:40 -07:00
'onTwigSiteVariables' => [ 'onTwigSiteVariables' , 1000 ],
2015-12-03 14:47:34 -07:00
'onAssetsInitialized' => [ 'onAssetsInitialized' , 1000 ],
2016-05-04 14:26:27 -06:00
'onTask.GPM' => [ 'onTaskGPM' , 0 ],
'onAdminRegisterPermissions' => [ 'onAdminRegisterPermissions' , 0 ],
2014-09-09 07:57:53 +03:00
]);
2014-09-03 22:22:03 -06:00
2015-12-10 12:08:06 -07:00
// Initialize admin class.
require_once __DIR__ . '/classes/admin.php' ;
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' );
}
2015-11-20 11:02:52 -07:00
// Initialize Admin Language if needed
/** @var Language $language */
$language = $this -> grav [ 'language' ];
if ( $language -> enabled () && empty ( $this -> grav [ 'session' ] -> admin_lang )) {
$this -> grav [ 'session' ] -> admin_lang = $language -> getLanguage ();
}
2014-09-09 07:57:53 +03:00
// Decide admin template and route.
$path = trim ( substr ( $this -> uri -> route (), strlen ( $this -> base )), '/' );
2015-12-10 11:02:19 -07:00
if ( empty ( $this -> template )) {
$this -> template = 'dashboard' ;
2015-12-04 11:58:28 -07:00
}
2015-12-10 13:22:23 -07:00
// Can't access path directly...
if ( $path && $path != 'register' ) {
2014-09-09 07:57:53 +03:00
$array = explode ( '/' , $path , 2 );
$this -> template = array_shift ( $array );
$this -> route = array_shift ( $array );
}
2014-09-03 22:22:03 -06:00
2014-09-09 07:57:53 +03:00
$this -> admin = new Admin ( $this -> grav , $this -> base , $this -> template , $this -> route );
2014-09-06 16:59:58 -06:00
2016-02-04 20:59:08 +01:00
2014-09-09 07:57:53 +03:00
// And store the class into DI container.
$this -> grav [ 'admin' ] = $this -> admin ;
2015-02-28 13:41:47 -07:00
2016-02-11 16:14:43 +01:00
// Double check we have system.yam, site.yaml etc
$config_path = $this -> grav [ 'locator' ] -> findResource ( 'user://config' );
foreach ( $this -> admin -> configurations () as $config_file ) {
$config_file = " { $config_path } / { $config_file } .yaml " ;
if ( ! file_exists ( $config_file )) {
touch ( $config_file );
}
}
2015-02-28 13:41:47 -07:00
// Get theme for admin
$this -> theme = $this -> config -> get ( 'plugins.admin.theme' , 'grav' );
2015-08-24 18:49:11 +02:00
$assets = $this -> grav [ 'assets' ];
2016-02-01 11:27:34 -08:00
$translations = 'this.GravAdmin = this.GravAdmin || {}; if (!this.GravAdmin.translations) this.GravAdmin.translations = {}; ' . PHP_EOL . 'this.GravAdmin.translations.PLUGIN_ADMIN = {' ;
2015-08-24 18:49:11 +02:00
2015-11-29 21:51:07 -07:00
// Enable language translations
$translations_actual_state = $this -> config -> get ( 'system.languages.translations' );
$this -> config -> set ( 'system.languages.translations' , true );
2015-08-24 18:49:11 +02:00
$strings = [ 'EVERYTHING_UP_TO_DATE' ,
'UPDATES_ARE_AVAILABLE' ,
'IS_AVAILABLE_FOR_UPDATE' ,
'AND' ,
'IS_NOW_AVAILABLE' ,
'CURRENT' ,
'UPDATE_GRAV_NOW' ,
'TASK_COMPLETED' ,
'UPDATE' ,
'UPDATING_PLEASE_WAIT' ,
'GRAV_SYMBOLICALLY_LINKED' ,
'OF_YOUR' ,
'OF_THIS' ,
'HAVE_AN_UPDATE_AVAILABLE' ,
'UPDATE_AVAILABLE' ,
2015-08-28 17:31:45 +02:00
'UPDATES_AVAILABLE' ,
'FULLY_UPDATED' ,
2015-10-21 19:54:10 +02:00
'DAYS' ,
'PAGE_MODES' ,
'PAGE_TYPES' ,
2016-02-01 11:27:34 -08:00
'ACCESS_LEVELS' ,
2016-02-01 16:34:50 -08:00
'NOTHING_TO_SAVE' ,
'FILE_UNSUPPORTED' ,
'FILE_ERROR_ADD' ,
2016-05-03 11:10:54 +02:00
'FILE_ERROR_UPLOAD' ,
'DROP_FILES_HERE_TO_UPLOAD' ,
'DELETE' ,
2016-05-04 14:57:40 +02:00
'INSERT' ,
'UNDO' ,
'UNDO' ,
'REDO' ,
'HEADERS' ,
'BOLD' ,
'ITALIC' ,
'STRIKETHROUGH' ,
'SUMMARY_DELIMITER' ,
'LINK' ,
'IMAGE' ,
'BLOCKQUOTE' ,
'UNORDERED_LIST' ,
'ORDERED_LIST' ,
'EDITOR' ,
'PREVIEW' ,
2016-05-06 18:17:26 +02:00
'FULLSCREEN' ,
'MODULAR' ,
'NON_MODULAR' ,
'VISIBLE' ,
'NON_VISIBLE' ,
'ROUTABLE' ,
'NON_ROUTABLE' ,
'PUBLISHED' ,
2016-06-10 15:10:24 +02:00
'NON_PUBLISHED' ,
'PLUGINS' ,
'THEMES' ,
'ALL' ,
'FROM' ,
'TO'
2015-10-21 19:54:10 +02:00
];
2015-08-24 18:49:11 +02:00
foreach ( $strings as $string ) {
2016-02-01 11:27:34 -08:00
$separator = ( end ( $strings ) === $string ) ? '' : ',' ;
$translations .= '"' . $string . '": "' . $this -> admin -> translate ( 'PLUGIN_ADMIN.' . $string ) . '"' . $separator ;
2015-08-24 18:49:11 +02:00
}
2016-02-01 11:27:34 -08:00
$translations .= '};' ;
2015-11-29 21:51:07 -07:00
// set the actual translations state back
$this -> config -> set ( 'system.languages.translations' , $translations_actual_state );
2015-08-24 18:49:11 +02:00
$assets -> addInlineJs ( $translations );
2014-09-03 22:22:03 -06:00
}
2015-08-13 20:26:50 +02:00
/**
2016-01-10 17:17:04 +01:00
* Add the Admin Twig Extensions
2015-08-13 20:26:50 +02:00
*/
public function onTwigExtensions ()
{
require_once ( __DIR__ . '/twig/AdminTwigExtension.php' );
$this -> grav [ 'twig' ] -> twig -> addExtension ( new AdminTwigExtension ());
}
2015-12-10 12:08:06 -07:00
2016-01-10 17:17:04 +01:00
/**
* Check if the current route is under the admin path
*
* @ return bool
*/
2015-12-10 13:22:23 -07:00
public function isAdminPath ()
{
if ( $this -> uri -> route () == $this -> base ||
substr ( $this -> uri -> route (), 0 , strlen ( $this -> base ) + 1 ) == $this -> base . '/' ) {
return true ;
}
return false ;
}
2016-01-10 17:39:54 +01:00
public function onAdminDashboard ()
{
2016-01-11 11:34:15 +01:00
$this -> grav [ 'twig' ] -> plugins_hooked_dashboard_widgets_top [] = [ 'template' => 'dashboard-maintenance' ];
$this -> grav [ 'twig' ] -> plugins_hooked_dashboard_widgets_top [] = [ 'template' => 'dashboard-statistics' ];
$this -> grav [ 'twig' ] -> plugins_hooked_dashboard_widgets_main [] = [ 'template' => 'dashboard-pages' ];
2016-01-10 17:39:54 +01:00
}
2016-05-04 14:26:27 -06:00
/**
* Initial stab at registering permissions ( WIP )
*
* @ param Event $e
*/
public function onAdminRegisterPermissions ( Event $e )
{
$admin = $e [ 'admin' ];
$permissions = [
'admin.super' => 'boolean' ,
'admin.login' => 'boolean' ,
'admin.cache' => 'boolean' ,
'admin.configuration' => 'boolean' ,
'admin.settings' => 'boolean' ,
'admin.pages' => 'boolean' ,
'admin.maintenance' => 'boolean' ,
'admin.statistics' => 'boolean' ,
'admin.plugins' => 'boolean' ,
'admin.themes' => 'boolean' ,
'admin.users' => 'boolean' ,
];
$admin -> addPermissions ( $permissions );
}
2014-08-05 13:06:38 -07:00
}