예제 #1
0
 /**
  * Save the current page in a different language. Automatically switches to that language.
  *
  * @return bool True if the action was performed.
  */
 protected function taskSaveas()
 {
     if (!$this->authorizeTask('save', $this->dataPermissions())) {
         return false;
     }
     $data = (array) $this->data;
     $language = $data['lang'];
     if ($language) {
         $this->grav['session']->admin_lang = $language ?: 'en';
     }
     $uri = $this->grav['uri'];
     $obj = $this->admin->page($uri->route());
     $this->preparePage($obj, false, $language);
     $file = $obj->file();
     if ($file) {
         $filename = $this->determineFilenameIncludingLanguage($obj->name(), $language);
         $path = $obj->path() . DS . $filename;
         $aFile = File::instance($path);
         $aFile->save();
         $aPage = new Page();
         $aPage->init(new \SplFileInfo($path), $language . '.md');
         $aPage->header($obj->header());
         $aPage->rawMarkdown($obj->rawMarkdown());
         $aPage->validate();
         $aPage->filter();
         $aPage->save();
         $this->grav->fireEvent('onAdminAfterSave', new Event(['page' => $obj]));
     }
     $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_SWITCHED_LANGUAGE'), 'info');
     $this->setRedirect('/' . $language . $uri->route());
     return true;
 }
 /**
  * Handles removing a media file
  *
  * @return bool True if the action was performed
  */
 public function taskRemoveMedia()
 {
     if (!$this->canEditMedia()) {
         return false;
     }
     $filename = base64_decode($this->grav['uri']->param('route'));
     $file = File::instance($filename);
     $resultRemoveMedia = false;
     $resultRemoveMediaMeta = true;
     if ($file->exists()) {
         $resultRemoveMedia = $file->delete();
         $metaFilePath = $filename . '.meta.yaml';
         $metaFilePath = str_replace('@3x', '', $metaFilePath);
         $metaFilePath = str_replace('@2x', '', $metaFilePath);
         if (is_file($metaFilePath)) {
             $metaFile = File::instance($metaFilePath);
             $resultRemoveMediaMeta = $metaFile->delete();
         }
     }
     if ($resultRemoveMedia && $resultRemoveMediaMeta) {
         $this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.REMOVE_SUCCESSFUL')];
         return true;
     } else {
         $this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.REMOVE_FAILED')];
         return false;
     }
 }
예제 #3
0
 /**
  * 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], 'onOutputGenerated' => ['onOutputGenerated', 0]]);
     // Autoload classes
     require_once __DIR__ . '/vendor/autoload.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');
     }
     // 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);
     }
     // Initialize admin class.
     $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);
 }
예제 #4
0
 /**
  * @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;
 }