/** * Initialize the admin. * * @throws \RuntimeException */ protected function initializeAdmin() { $this->enable(['onTwigExtensions' => ['onTwigExtensions', 1000], 'onPagesInitialized' => ['onPagesInitialized', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000], 'onAssetsInitialized' => ['onAssetsInitialized', 1000], 'onTask.GPM' => ['onTaskGPM', 0], 'onAdminRegisterPermissions' => ['onAdminRegisterPermissions', 0]]); // Initialize admin class. require_once __DIR__ . '/classes/admin.php'; // Autoload classes $autoload = __DIR__ . '/vendor/autoload.php'; if (!is_file($autoload)) { throw new \Exception('Admin Plugin failed to load. Composer dependencies not met.'); } require_once $autoload; // 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'); } // 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(); } // Decide admin template and route. $path = trim(substr($this->uri->route(), strlen($this->base)), '/'); if (empty($this->template)) { $this->template = 'dashboard'; } // Can't access path directly... if ($path && $path != 'register') { $array = explode('/', $path, 2); $this->template = array_shift($array); $this->route = array_shift($array); } $this->admin = new Admin($this->grav, $this->admin_route, $this->template, $this->route); // And store the class into DI container. $this->grav['admin'] = $this->admin; // Double check we have system.yaml, 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); } } // Get theme for admin $this->theme = $this->config->get('plugins.admin.theme', 'grav'); $assets = $this->grav['assets']; $translations = 'this.GravAdmin = this.GravAdmin || {}; if (!this.GravAdmin.translations) this.GravAdmin.translations = {}; ' . PHP_EOL . 'this.GravAdmin.translations.PLUGIN_ADMIN = {'; // Enable language translations $translations_actual_state = $this->config->get('system.languages.translations'); $this->config->set('system.languages.translations', true); $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', 'UPDATES_AVAILABLE', 'FULLY_UPDATED', 'DAYS', 'PAGE_MODES', 'PAGE_TYPES', 'ACCESS_LEVELS', 'NOTHING_TO_SAVE', 'FILE_UNSUPPORTED', 'FILE_ERROR_ADD', 'FILE_ERROR_UPLOAD', 'DROP_FILES_HERE_TO_UPLOAD', 'DELETE', 'INSERT', 'UNDO', 'UNDO', 'REDO', 'HEADERS', 'BOLD', 'ITALIC', 'STRIKETHROUGH', 'SUMMARY_DELIMITER', 'LINK', 'IMAGE', 'BLOCKQUOTE', 'UNORDERED_LIST', 'ORDERED_LIST', 'EDITOR', 'PREVIEW', 'FULLSCREEN', 'MODULAR', 'NON_MODULAR', 'VISIBLE', 'NON_VISIBLE', 'ROUTABLE', 'NON_ROUTABLE', 'PUBLISHED', 'NON_PUBLISHED', 'PLUGINS', 'THEMES', 'ALL', 'FROM', 'TO', 'DROPZONE_CANCEL_UPLOAD', 'DROPZONE_CANCEL_UPLOAD_CONFIRMATION', 'DROPZONE_DEFAULT_MESSAGE', 'DROPZONE_FALLBACK_MESSAGE', 'DROPZONE_FALLBACK_TEXT', 'DROPZONE_FILE_TOO_BIG', 'DROPZONE_INVALID_FILE_TYPE', 'DROPZONE_MAX_FILES_EXCEEDED', 'DROPZONE_REMOVE_FILE', 'DROPZONE_RESPONSE_ERROR']; foreach ($strings as $string) { $separator = end($strings) === $string ? '' : ','; $translations .= '"' . $string . '": "' . $this->admin->translate('PLUGIN_ADMIN.' . $string) . '"' . $separator; } $translations .= '};'; // set the actual translations state back $this->config->set('system.languages.translations', $translations_actual_state); $assets->addInlineJs($translations); }
/** * Set all twig variables for generating output. */ public function onTwigSiteVariables() { $twig = $this->grav['twig']; // Dynamic type support $format = $this->uri->extension(); $ext = '.' . ($format ? $format : 'html') . TWIG_EXT; $twig->twig_vars['location'] = $this->template; $twig->twig_vars['base_url_relative_frontend'] = $twig->twig_vars['base_url_relative']; $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; $twig->twig_vars['base_url'] = $twig->twig_vars['base_url_relative']; $twig->twig_vars['admin'] = $this->admin; switch ($this->template) { case 'dashboard': $twig->twig_vars['popularity'] = $this->popularity; break; 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')))); } break; } }
/** * Checks if the user is allowed to perform the given task with its associated permissions * * @param string $task The task to execute * @param array $permissions The permissions given * @return bool True if authorized. False if not. */ protected function authorizeTask($task = '', $permissions = []) { if (!$this->admin->authorize($permissions)) { if ($this->grav['uri']->extension() === 'json') { $this->admin->json_response = ['status' => 'unauthorized', 'message' => $this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.']; } else { $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.', 'error'); } return false; } return true; }
/** * Checks if the user is allowed to perform the given task with its associated permissions * * @param string $task The task to execute * @param array $permissions The permissions given * @return bool True if authorized. False if not. */ protected function authoriseTask($task = '', $permissions = []) { if (!$this->admin->authorise($permissions)) { if ($this->grav['uri']->extension() === 'json') { $this->admin->json_response = ['status' => 'unauthorized', 'message' => 'You have insufficient permissions for task ' . $task . '.']; } else { $this->admin->setMessage('You have insufficient permissions for task ' . $task . '.', 'error'); } return false; } return true; }
/** * Initialize the admin. * * @throws \RuntimeException */ protected function initializeAdmin() { $this->enable(['onTwigExtensions' => ['onTwigExtensions', 1000], 'onPagesInitialized' => ['onPagesInitialized', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000], 'onAssetsInitialized' => ['onAssetsInitialized', 1000], 'onTask.GPM' => ['onTaskGPM', 0]]); // Initialize admin class. require_once __DIR__ . '/classes/admin.php'; // 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'); } // Double check we have system.yaml and site.yaml $config_files[] = $this->grav['locator']->findResource('user://config') . '/system.yaml'; $config_files[] = $this->grav['locator']->findResource('user://config') . '/site.yaml'; foreach ($config_files as $config_file) { if (!file_exists($config_file)) { touch($config_file); } } // 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(); } // Decide admin template and route. $path = trim(substr($this->uri->route(), strlen($this->base)), '/'); if (empty($this->template)) { $this->template = 'dashboard'; } // Can't access path directly... if ($path && $path != 'register') { $array = explode('/', $path, 2); $this->template = array_shift($array); $this->route = array_shift($array); } $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'); $assets = $this->grav['assets']; $translations = 'if (!window.translations) window.translations = {}; ' . PHP_EOL . 'window.translations.PLUGIN_ADMIN = {};' . PHP_EOL; // Enable language translations $translations_actual_state = $this->config->get('system.languages.translations'); $this->config->set('system.languages.translations', true); $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', 'UPDATES_AVAILABLE', 'FULLY_UPDATED', 'DAYS', 'PAGE_MODES', 'PAGE_TYPES', 'ACCESS_LEVELS']; foreach ($strings as $string) { $translations .= 'translations.PLUGIN_ADMIN.' . $string . ' = "' . $this->admin->translate('PLUGIN_ADMIN.' . $string) . '"; ' . PHP_EOL; } // set the actual translations state back $this->config->set('system.languages.translations', $translations_actual_state); $assets->addInlineJs($translations); }
/** * @return bool */ public static function selfupgrade() { $upgrader = new Upgrader(); if (!Installer::isGravInstance(GRAV_ROOT)) { return false; } if (is_link(GRAV_ROOT . DS . 'index.php')) { Installer::setError(Installer::IS_LINK); return false; } if (method_exists($upgrader, 'meetsRequirements') && !$upgrader->meetsRequirements()) { $error = []; $error[] = '<p>Grav has increased the minimum PHP requirement.<br />'; $error[] = 'You are currently running PHP <strong>' . PHP_VERSION . '</strong>'; $error[] = ', but PHP <strong>' . GRAV_PHP_MIN . '</strong> is required.</p>'; $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>'; Installer::setError(implode("\n", $error)); return false; } $update = $upgrader->getAssets()['grav-update']; $tmp = Admin::getTempDir() . '/Grav-' . uniqid(); $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { return false; } return true; }