Ejemplo n.º 1
0
 /**
  * Twig process that renders the site layout. This is the main twig process that renders the overall
  * page and handles all the layout for the site display.
  *
  * @param string $format Output format (defaults to HTML).
  * @return string the rendered output
  * @throws \RuntimeException
  */
 public function processSite($format = null)
 {
     // set the page now its been processed
     $this->grav->fireEvent('onTwigSiteVariables');
     $pages = $this->grav['pages'];
     $page = $this->grav['page'];
     $content = $page->content();
     $config = $this->grav['config'];
     $twig_vars = $this->twig_vars;
     $twig_vars['pages'] = $pages->root();
     $twig_vars['page'] = $page;
     $twig_vars['header'] = $page->header();
     $twig_vars['content'] = $content;
     $ext = '.' . ($format ? $format : 'html') . TWIG_EXT;
     // determine if params are set, if so disable twig cache
     $params = $this->grav['uri']->params(null, true);
     if (!empty($params)) {
         $this->twig->setCache(false);
     }
     // Get Twig template layout
     $template = $this->template($page->template() . $ext);
     try {
         $output = $this->twig->render($template, $twig_vars);
     } catch (\Twig_Error_Loader $e) {
         // If loader error, and not .html.twig, try it as fallback
         if (Utils::contains($e->getMessage(), $template)) {
             $inflector = new Inflector();
             $error_msg = 'The template file for this page: "' . $template . '" is not provided by the theme: "' . $inflector->titleize($config->get('system.pages.theme')) . '"';
         } else {
             $error_msg = $e->getMessage();
         }
         // Try html version of this template if initial template was NOT html
         if ($ext != '.html' . TWIG_EXT) {
             try {
                 $output = $this->twig->render($page->template() . '.html' . TWIG_EXT, $twig_vars);
             } catch (\Twig_Error_Loader $e) {
                 throw new \RuntimeException($error_msg, 400, $e);
             }
         } else {
             throw new \RuntimeException($error_msg, 400, $e);
         }
     }
     return $output;
 }
Ejemplo n.º 2
0
 /**
  * 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');
             if ($form->value('password1') != $form->value('password2')) {
                 $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate('PLUGIN_LOGIN.PASSWORDS_DO_NOT_MATCH')]));
                 $event->stopPropagation();
                 return;
             }
             $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']);
             // Extra lowercase to ensure file is saved lowercase
             $username = strtolower($username);
             $inflector = new Inflector();
             $data['fullname'] = isset($data['fullname']) ? $data['fullname'] : $inflector->titleize($username);
             $data['title'] = isset($data['title']) ? $data['title'] : 'Administrator';
             $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');
             $this->grav->redirect($this->admin_route);
             break;
     }
 }