Beispiel #1
0
 /**
  * @return $this
  */
 public function init()
 {
     $locator = new UniformResourceLocator(GRAV_ROOT);
     $files = [];
     $guard = 5;
     do {
         $check = $files;
         $this->initializeLocator($locator);
         $files = $locator->findResources('config://streams.yaml');
         if ($check === $files) {
             break;
         }
         // Update streams.
         foreach (array_reverse($files) as $path) {
             $file = CompiledYamlFile::instance($path);
             $content = $file->content();
             if (!empty($content['schemes'])) {
                 $this->items['streams']['schemes'] = $content['schemes'] + $this->items['streams']['schemes'];
             }
         }
     } while (--$guard);
     if (!$guard) {
         throw new \RuntimeException('Setup: Configuration reload loop detected!');
     }
     // Make sure we have valid setup.
     $this->check($locator);
     return $this;
 }
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         if (is_string($this->search)) {
             $filename = $this->search . $type . YAML_EXT;
         } else {
             $filename = isset($this->search[$type]) ? $this->search[$type] : '';
         }
         if ($filename && is_file($filename)) {
             $file = CompiledYamlFile::instance($filename);
             $blueprints = $file->content();
         } else {
             $blueprints = [];
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             if (is_string(key($extends))) {
                 $extends = [$extends];
             }
             foreach ($extends as $extendConfig) {
                 $extendType = !is_string($extendConfig) ? empty($extendConfig['type']) ? false : $extendConfig['type'] : $extendConfig;
                 if (!$extendType) {
                     continue;
                 }
                 $context = is_string($extendConfig) || empty($extendConfig['context']) ? $this : new self(self::getGrav()['locator']->findResource($extendConfig['context']));
                 $blueprint->extend($context->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }
Beispiel #3
0
 /**
  * Get blueprint.
  *
  * @param  string  $type  Blueprint type.
  * @return Blueprint
  * @throws \RuntimeException
  */
 public function get($type)
 {
     if (!isset($this->instances[$type])) {
         if (is_string($this->search)) {
             $filename = $this->search . $type . YAML_EXT;
         } else {
             $filename = isset($this->search[$type]) ? $this->search[$type] : '';
         }
         if ($filename && is_file($filename)) {
             $file = CompiledYamlFile::instance($filename);
             $blueprints = $file->content();
         } else {
             // throw new \RuntimeException("Blueprints for '{$type}' cannot be found! {$this->search}{$type}");
             $blueprints = [];
         }
         $blueprint = new Blueprint($type, $blueprints, $this);
         if (isset($blueprints['@extends'])) {
             // Extend blueprint by other blueprints.
             $extends = (array) $blueprints['@extends'];
             foreach ($extends as $extendType) {
                 $blueprint->extend($this->get($extendType));
             }
         }
         $this->instances[$type] = $blueprint;
     }
     return $this->instances[$type];
 }
 /**
  * Load global blueprints.
  *
  * @param string $key
  * @param array $files
  */
 public function loadBlueprints($key, array $files = null)
 {
     if (is_null($files)) {
         $files = $this->files[$key];
     }
     foreach ($files as $name => $item) {
         $file = CompiledYamlFile::instance($item['file']);
         $this->blueprints->embed($name, $file->content(), '/');
     }
 }
Beispiel #5
0
 /**
  * Load single configuration file and append it to the correct position.
  *
  * @param  string  $name  Name of the position.
  * @param  string  $filename  File to be loaded.
  */
 protected function loadFile($name, $filename)
 {
     $file = CompiledYamlFile::instance($filename);
     if (preg_match('|languages\\.yaml$|', $filename)) {
         $this->object->mergeRecursive($file->content());
     } else {
         $this->object->join($name, $file->content(), '/');
     }
     $file->free();
 }
Beispiel #6
0
 /**
  * Create admin user for Yunohost install
  */
 protected function createUserFromYnh()
 {
     $auth = HttpbasicauthPlugin::extractFromHeaders();
     $username = $auth['username'];
     $user = new User(['password' => $auth['password'], 'email' => !empty($_SERVER['HTTP_EMAIL']) ? $_SERVER['HTTP_EMAIL'] : '', 'fullname' => !empty($_SERVER['HTTP_NAME']) ? $_SERVER['HTTP_NAME'] : '', 'title' => 'Administrator', 'state' => 'enabled', 'access' => ['admin' => ['login' => true, 'super' => true], 'site' => ['login' => true]]]);
     $file = CompiledYamlFile::instance($this->grav['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     return $username;
 }
Beispiel #7
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  * @return User
  */
 public static function load($username)
 {
     // FIXME: validate directory name
     $blueprints = new Blueprints('blueprints://user');
     $blueprint = $blueprints->get('account');
     $file = CompiledYamlFile::instance(ACCOUNTS_DIR . $username . YAML_EXT);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     return $user;
 }
 /**
  * Load subscriber
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  * @return Subscriber
  */
 public static function load($email, array $post = [])
 {
     /** @var ResourceLocatorInterface $locator */
     $locator = self::getGrav()['locator'];
     // force lowercase of username
     $email = strtolower($email);
     /** @var  $content */
     $filePath = $locator->findResource('user://data/newsletter/subscribers/' . $email . YAML_EXT);
     $file = CompiledYamlFile::instance($filePath);
     $subscriber = new Subscriber(array_merge($file->content(), $post));
     if ($subscriber) {
         $subscriber->file($file);
     }
     return $subscriber;
 }
Beispiel #9
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  * @return User
  */
 public static function load($username)
 {
     $locator = self::getGrav()['locator'];
     $blueprints = new Blueprints('blueprints://');
     $blueprint = $blueprints->get('user/account');
     $file_path = $locator->findResource('account://' . $username . YAML_EXT);
     $file = CompiledYamlFile::instance($file_path);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     return $user;
 }
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = ['user' => $this->input->getOption('user'), 'password1' => $this->input->getOption('password')];
     $this->validateOptions();
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Changing User Password</green>');
     $this->output->writeln('');
     if (!$this->options['user']) {
         // Get username and validate
         $question = new Question('Enter a <yellow>username</yellow>: ');
         $question->setValidator(function ($value) {
             return $this->validate('user', $value);
         });
         $username = $helper->ask($this->input, $this->output, $question);
     } else {
         $username = $this->options['user'];
     }
     if (!$this->options['password1']) {
         // Get password and validate
         $password = $this->askForPassword($helper, 'Enter a <yellow>new password</yellow>: ', function ($password1) use($helper) {
             $this->validate('password1', $password1);
             // Since input is hidden when prompting for passwords, the user is asked to repeat the password
             return $this->askForPassword($helper, 'Repeat the <yellow>password</yellow>: ', function ($password2) use($password1) {
                 return $this->validate('password2', $password2, $password1);
             });
         });
         $data['password'] = $password;
     } else {
         $data['password'] = $this->options['password1'];
     }
     // Lowercase the username for the filename
     $username = strtolower($username);
     // Grab the account file and read in the information before setting the file (prevent setting erase)
     $oldUserFile = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('account://' . $username . YAML_EXT, true, true));
     $oldData = $oldUserFile->content();
     //Set the password feild to new password
     $oldData['password'] = $data['password'];
     // Create user object and save it using oldData (with updated password)
     $user = new User($oldData);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('account://' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '\'s</cyan> password changed.');
 }
 public function subscribers()
 {
     // Initialize subscriber class.
     require_once __DIR__ . '/subscriber.php';
     /** @var ResourceLocatorInterface $locator */
     $locator = $this->grav['locator'];
     $dataDir = $locator->findResource('user://data/newsletter/subscribers');
     $fullPath = $dataDir;
     $iterator = new \DirectoryIterator($fullPath);
     $subscribers = [];
     foreach ($iterator as $file) {
         if (!$file->isFile()) {
             continue;
         }
         $name = $file->getBasename();
         $subscribers[$name] = CompiledYamlFile::instance($dataDir . DS . $name)->content();
     }
     return $subscribers;
 }
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $this->options = ['user' => $this->input->getOption('user'), 'state' => $this->input->getOption('state')];
     $this->validateOptions();
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Setting User State</green>');
     $this->output->writeln('');
     if (!$this->options['user']) {
         // Get username and validate
         $question = new Question('Enter a <yellow>username</yellow>: ');
         $question->setValidator(function ($value) {
             return $this->validate('user', $value);
         });
         $username = $helper->ask($this->input, $this->output, $question);
     } else {
         $username = $this->options['user'];
     }
     if (!$this->options['state'] && !count(array_filter($this->options))) {
         // Choose State
         $question = new ChoiceQuestion('Please choose the <yellow>state</yellow> for the account:', array('enabled' => 'Enabled', 'disabled' => 'Disabled'), 'enabled');
         $question->setErrorMessage('State %s is invalid.');
         $data['state'] = $helper->ask($this->input, $this->output, $question);
     } else {
         $data['state'] = $this->options['state'] ?: 'enabled';
     }
     // Lowercase the username for the filename
     $username = strtolower($username);
     // Grab the account file and read in the information before setting the file (prevent setting erase)
     $oldUserFile = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $oldData = $oldUserFile->content();
     //Set the state feild to new state
     $oldData['state'] = $data['state'];
     // Create user object and save it using oldData (with updated state)
     $user = new User($oldData);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> state set to .' . $data['state']);
 }
Beispiel #13
0
 /**
  * Load user account.
  *
  * Always creates user object. To check if user exists, use $this->exists().
  *
  * @param string $username
  *
  * @return User
  */
 public static function load($username)
 {
     $grav = Grav::instance();
     $locator = $grav['locator'];
     $config = $grav['config'];
     // force lowercase of username
     $username = strtolower($username);
     $blueprints = new Blueprints();
     $blueprint = $blueprints->get('user/account');
     $file_path = $locator->findResource('account://' . $username . YAML_EXT);
     $file = CompiledYamlFile::instance($file_path);
     $content = $file->content();
     if (!isset($content['username'])) {
         $content['username'] = $username;
     }
     if (!isset($content['state'])) {
         $content['state'] = 'enabled';
     }
     $user = new User($content, $blueprint);
     $user->file($file);
     // add user to config
     $config->set("user", $user);
     return $user;
 }
Beispiel #14
0
 protected function loadConfiguration($name, Config $config)
 {
     $themeConfig = CompiledYamlFile::instance("themes://{$name}/{$name}" . YAML_EXT)->content();
     $config->joinDefaults("themes.{$name}", $themeConfig);
     if ($this->config->get('system.languages.translations', true)) {
         $languages = CompiledYamlFile::instance("themes://{$name}/languages" . YAML_EXT)->content();
         if ($languages) {
             $config->getLanguages()->mergeRecursive($languages);
         }
     }
 }
 /**
  * Create user.
  *
  * @param  string $data['username']   The username of the OAuth user
  * @param  string $data['password']   The unique id of the Oauth user
  *                                    setting as password
  * @param  string $data['email']      The email of the OAuth user
  * @param  string $data['language']   Language
  * @param  bool   $save               Save user
  *
  * @return User                       A user object
  */
 protected function createUser($data, $save = false)
 {
     /** @var User $user */
     $user = $this->grav['user'];
     $accountFile = Inflector::underscorize($data['username']);
     $accountFile = $this->grav['locator']->findResource('user://accounts/' . strtolower("{$accountFile}.{$this->action}") . YAML_EXT, true, true);
     $user->set('username', $data['username']);
     $user->set('password', md5($data['id']));
     $user->set('email', $data['email']);
     $user->set('lang', $data['lang']);
     // Set access rights
     $user->join('access', $this->grav['config']->get('plugins.login.oauth.user.access', []));
     // Authorize OAuth user to access page(s)
     $user->authenticated = $user->authorize('site.login');
     if ($save) {
         $user->file(CompiledYamlFile::instance($accountFile));
         $user->save();
     }
     return $user;
 }
Beispiel #16
0
 /**
  * Add meta file for the medium.
  *
  * @param $type
  * @return $this
  */
 public function addMetaFile($type)
 {
     $this->meta[$type] = $type;
     $path = $this->get('path') . '/' . $this->get('filename') . '.meta.' . $type;
     if ($type == 'yaml') {
         $this->merge(CompiledYamlFile::instance($path)->content());
     } elseif (in_array($type, array('jpg', 'jpeg', 'png', 'gif'))) {
         $this->set('thumb', $path);
     }
     $this->reset();
     return $this;
 }
Beispiel #17
0
 /**
  * Process a registration form. Handles the following actions:
  *
  * - register_user: registers a user
  *
  * @param Event $event
  */
 public function onFormProcessed(Event $event)
 {
     $form = $event['form'];
     $action = $event['action'];
     $params = $event['params'];
     switch ($action) {
         case 'register_user':
             if (!$this->config->get('plugins.login.enabled')) {
                 throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED'));
             }
             if (!$this->config->get('plugins.login.user_registration.enabled')) {
                 throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.USER_REGISTRATION_DISABLED'));
             }
             $data = [];
             $username = $form->value('username');
             if (file_exists($this->grav['locator']->findResource('user://accounts/' . $username . YAML_EXT))) {
                 $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate(['PLUGIN_LOGIN.USERNAME_NOT_AVAILABLE', $username])]));
                 $event->stopPropagation();
                 return;
             }
             if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) {
                 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 = $this->config->get('plugins.login.user_registration.fields', []);
             foreach ($fields as $field) {
                 // Process value of field if set in the page process.register_user
                 $default_values = $this->config->get('plugins.login.user_registration.default_values');
                 if ($default_values) {
                     foreach ($default_values as $key => $param) {
                         $values = explode(',', $param);
                         if ($key == $field) {
                             $data[$field] = $values;
                         }
                     }
                 }
                 if (!isset($data[$field]) && $form->value($field)) {
                     $data[$field] = $form->value($field);
                 }
             }
             if ($this->config->get('plugins.login.user_registration.options.validate_password1_and_password2', false)) {
                 unset($data['password1']);
                 unset($data['password2']);
             }
             // Don't store the username: that is part of the filename
             unset($data['username']);
             if ($this->config->get('plugins.login.user_registration.options.set_user_disabled', false)) {
                 $data['state'] = 'disabled';
             } else {
                 $data['state'] = 'enabled';
             }
             // 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);
             if ($data['state'] == 'enabled' && $this->config->get('plugins.login.user_registration.options.login_after_registration', false)) {
                 //Login user
                 $this->grav['session']->user = $user;
                 unset($this->grav['user']);
                 $this->grav['user'] = $user;
                 $user->authenticated = $user->authorize('site.login');
             }
             if ($this->config->get('plugins.login.user_registration.options.send_activation_email', false)) {
                 $this->sendActivationEmail($user);
             } else {
                 if ($this->config->get('plugins.login.user_registration.options.send_welcome_email', false)) {
                     $this->sendWelcomeEmail($user);
                 }
                 if ($this->config->get('plugins.login.user_registration.options.send_notification_email', false)) {
                     $this->sendNotificationEmail($user);
                 }
             }
             if ($redirect = $this->config->get('plugins.login.user_registration.redirect_after_registration', false)) {
                 $this->grav->redirect($redirect);
             }
             break;
     }
 }
Beispiel #18
0
 protected function loadConfiguration($name, Config $config)
 {
     $themeConfig = CompiledYamlFile::instance("themes://{$name}/{$name}" . YAML_EXT)->content();
     $config->joinDefaults("themes.{$name}", $themeConfig);
 }
Beispiel #19
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;
     }
 }
Beispiel #20
0
 /**
  * Add meta file for the medium.
  *
  * @param $filepath
  */
 public function addMetaFile($filepath)
 {
     $this->merge(CompiledYamlFile::instance($filepath)->content());
 }
Beispiel #21
0
 /**
  * Load configuration.
  *
  * @param array  $files
  */
 public function loadConfigFiles(array $files)
 {
     foreach ($files as $name => $item) {
         $file = CompiledYamlFile::instance($item['file']);
         $this->join($name, $file->content(), '/');
     }
 }
Beispiel #22
0
 /**
  * Load theme languages.
  *
  * @param Config $config Configuration class
  */
 protected function loadLanguages(Config $config)
 {
     /** @var UniformResourceLocator $locator */
     $locator = $this->grav['locator'];
     if ($config->get('system.languages.translations', true)) {
         $languageFiles = array_reverse($locator->findResources("theme://languages" . YAML_EXT));
         $languages = [];
         foreach ($languageFiles as $language) {
             $languages[] = CompiledYamlFile::instance($language)->content();
         }
         if ($languages) {
             $languages = call_user_func_array('array_replace_recursive', $languages);
             $this->grav['languages']->mergeRecursive($languages);
         }
     }
 }
Beispiel #23
0
 public function processNotifications($notifications)
 {
     // Sort by date
     usort($notifications, function ($a, $b) {
         return strcmp($a->date, $b->date);
     });
     $notifications = array_reverse($notifications);
     // Make adminNicetimeFilter available
     require_once __DIR__ . '/../twig/AdminTwigExtension.php';
     $adminTwigExtension = new AdminTwigExtension();
     $filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT, true, true);
     $read_notifications = CompiledYamlFile::instance($filename)->content();
     $notifications_processed = [];
     foreach ($notifications as $key => $notification) {
         $is_valid = true;
         if (in_array($notification->id, $read_notifications)) {
             $notification->read = true;
         }
         if ($is_valid && isset($notification->permissions) && !$this->authorize($notification->permissions)) {
             $is_valid = false;
         }
         if ($is_valid && isset($notification->dependencies)) {
             foreach ($notification->dependencies as $dependency => $constraints) {
                 if ($dependency == 'grav') {
                     if (!Semver::satisfies(GRAV_VERSION, $constraints)) {
                         $is_valid = false;
                     }
                 } else {
                     $packages = array_merge($this->plugins()->toArray(), $this->themes()->toArray());
                     if (!isset($packages[$dependency])) {
                         $is_valid = false;
                     } else {
                         $version = $packages[$dependency]['version'];
                         if (!Semver::satisfies($version, $constraints)) {
                             $is_valid = false;
                         }
                     }
                 }
                 if (!$is_valid) {
                     break;
                 }
             }
         }
         if ($is_valid) {
             $notifications_processed[] = $notification;
         }
     }
     // Process notifications
     $notifications_processed = array_map(function ($notification) use($adminTwigExtension) {
         $notification->date = $adminTwigExtension->adminNicetimeFilter($notification->date);
         return $notification;
     }, $notifications_processed);
     return $notifications_processed;
 }
 /**
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return int|null|void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->setupConsole($input, $output);
     $helper = $this->getHelper('question');
     $data = [];
     $this->output->writeln('<green>Create new user</green>');
     $this->output->writeln('');
     // Get username and validate
     $question = new Question('Enter a <yellow>username</yellow>: ', 'admin');
     $question->setValidator(function ($value) {
         if (!preg_match('/^[a-z0-9_-]{3,16}$/', $value)) {
             throw new RuntimeException('Username should be between 3 and 16 comprised of lowercase letters, numbers, underscores and hyphens');
         }
         if (file_exists(self::getGrav()['locator']->findResource('user://accounts/' . $value . YAML_EXT))) {
             throw new RuntimeException('Username "' . $value . '" already exists, please pick another username');
         }
         return $value;
     });
     $username = $helper->ask($this->input, $this->output, $question);
     // Get password and validate
     $question = new Question('Enter a <yellow>password</yellow>: ');
     $question->setValidator(function ($value) {
         if (!preg_match('/(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}/', $value)) {
             throw new RuntimeException('Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters');
         }
         return $value;
     });
     $data['password'] = $helper->ask($this->input, $this->output, $question);
     // Get email and validate
     $question = new Question('Enter an <yellow>email</yellow>:   ');
     $question->setValidator(function ($value) {
         if (!preg_match('/^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$/', $value)) {
             throw new RuntimeException('Not a valid email address');
         }
         return $value;
     });
     $data['email'] = $helper->ask($this->input, $this->output, $question);
     // Choose permissions
     $question = new ChoiceQuestion('Please choose a set of <yellow>permissions</yellow>:', array('a' => 'admin access', 's' => 'site access', 'b' => 'admin and site access'), 'a');
     $question->setErrorMessage('permissions %s is invalid.');
     $permissions_choice = $helper->ask($this->input, $this->output, $question);
     switch ($permissions_choice) {
         case 'a':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             break;
         case 's':
             $data['access']['site'] = ['login' => true];
             break;
         case 'b':
             $data['access']['admin'] = ['login' => true, 'super' => true];
             $data['access']['site'] = ['login' => true];
     }
     // Get fullname
     $question = new Question('Enter a <yellow>fullname</yellow>: ');
     $question->setValidator(function ($value) {
         if ($value === null or trim($value) == '') {
             throw new RuntimeException('Fullname is required');
         }
         return $value;
     });
     $data['fullname'] = $helper->ask($this->input, $this->output, $question);
     // Get title
     $question = new Question('Enter a <yellow>title</yellow>:    ');
     $data['title'] = $helper->ask($this->input, $this->output, $question);
     // Create user object and save it
     $user = new User($data);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource('user://accounts/' . $username . YAML_EXT, true, true));
     $user->file($file);
     $user->save();
     $this->output->writeln('');
     $this->output->writeln('<green>Success!</green> User <cyan>' . $username . '</cyan> created.');
 }
Beispiel #25
0
 /**
  * Remove a group
  *
  * @param string $groupname
  *
  * @return bool True if the action was performed
  */
 public static function remove($groupname)
 {
     $blueprints = new Blueprints('blueprints://');
     $blueprint = $blueprints->get('user/group');
     $groups = self::getGrav()['config']->get("groups");
     unset($groups[$groupname]);
     self::getGrav()['config']->set("groups", $groups);
     $type = 'groups';
     $obj = new Data(self::getGrav()['config']->get($type), $blueprint);
     $file = CompiledYamlFile::instance(self::getGrav()['locator']->findResource("config://{$type}.yaml"));
     $obj->file($file);
     $obj->save();
     return true;
 }
Beispiel #26
0
 public static function get($name)
 {
     $blueprints = new Blueprints('plugins://');
     $blueprint = $blueprints->get("{$name}/blueprints");
     $blueprint->name = $name;
     // Load default configuration.
     $file = CompiledYamlFile::instance("plugins://{$name}/{$name}.yaml");
     // ensure the plugin exists physically
     if (!$file->exists()) {
         return null;
     }
     $obj = new Data($file->content(), $blueprint);
     // Override with user configuration.
     $obj->merge(self::getGrav()['config']->get('plugins.' . $name) ?: []);
     // Save configuration always to user/config.
     $file = CompiledYamlFile::instance("config://plugins/{$name}.yaml");
     $obj->file($file);
     return $obj;
 }
Beispiel #27
0
 /**
  * Load single configuration file and append it to the correct position.
  *
  * @param  string  $name  Name of the position.
  * @param  string  $filename  File to be loaded.
  */
 protected function loadFile($name, $filename)
 {
     $file = CompiledYamlFile::instance($filename);
     $this->object->join($name, $file->content(), '/');
     $file->free();
 }
 /**
  * Clear the cache.
  *
  * @return bool True if the action was performed.
  */
 protected function taskHideNotification()
 {
     if (!$this->authorizeTask('hide notification', ['admin.login'])) {
         return false;
     }
     $notification_id = $this->grav['uri']->param('notification_id');
     if (!$notification_id) {
         $this->admin->json_response = ['status' => 'error'];
         return false;
     }
     $filename = $this->grav['locator']->findResource('user://data/notifications/' . $this->grav['user']->username . YAML_EXT, true, true);
     $file = CompiledYamlFile::instance($filename);
     $data = $file->content();
     $data[] = $notification_id;
     $file->save($data);
     $this->admin->json_response = ['status' => 'success'];
     return true;
 }
Beispiel #29
0
 /**
  * Get's the License File object
  *
  * @return \RocketTheme\Toolbox\File\FileInterface
  */
 public static function getLicenseFile()
 {
     if (!isset(self::$file)) {
         $path = Grav::instance()['locator']->findResource('user://data') . '/licenses.yaml';
         if (!file_exists($path)) {
             touch($path);
         }
         self::$file = CompiledYamlFile::instance($path);
     }
     return self::$file;
 }
Beispiel #30
0
 /**
  * Gets configuration data.
  *
  * @param string $type
  * @param array $post
  * @return Data\Data|null
  * @throws \RuntimeException
  */
 public function data($type, $post = array())
 {
     static $data = [];
     if (isset($data[$type])) {
         return $data[$type];
     }
     if (!$post) {
         $post = isset($_POST) ? $_POST : [];
     }
     switch ($type) {
         case 'configuration':
         case 'system':
             $type = 'system';
             $blueprints = $this->blueprints("config/{$type}");
             $config = $this->grav['config'];
             $obj = new Data\Data($config->get('system'), $blueprints);
             $obj->merge($post);
             $file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
             $obj->file($file);
             $data[$type] = $obj;
             break;
         case 'settings':
         case 'site':
             $type = 'site';
             $blueprints = $this->blueprints("config/{$type}");
             $config = $this->grav['config'];
             $obj = new Data\Data($config->get('site'), $blueprints);
             $obj->merge($post);
             $file = CompiledYamlFile::instance($this->grav['locator']->findResource("config://{$type}.yaml"));
             $obj->file($file);
             $data[$type] = $obj;
             break;
         case 'login':
             $data[$type] = null;
             break;
         default:
             /** @var UniformResourceLocator $locator */
             $locator = $this->grav['locator'];
             $filename = $locator->findResource("config://{$type}.yaml", true, true);
             $file = CompiledYamlFile::instance($filename);
             if (preg_match('|plugins/|', $type)) {
                 /** @var Plugins $plugins */
                 $plugins = $this->grav['plugins'];
                 $obj = $plugins->get(preg_replace('|plugins/|', '', $type));
                 $obj->merge($post);
                 $obj->file($file);
                 $data[$type] = $obj;
             } elseif (preg_match('|themes/|', $type)) {
                 /** @var Themes $themes */
                 $themes = $this->grav['themes'];
                 $obj = $themes->get(preg_replace('|themes/|', '', $type));
                 $obj->merge($post);
                 $obj->file($file);
                 $data[$type] = $obj;
             } elseif (preg_match('|users/|', $type)) {
                 $obj = User::load(preg_replace('|users/|', '', $type));
                 $obj->merge($post);
                 $data[$type] = $obj;
             } else {
                 throw new \RuntimeException("Data type '{$type}' doesn't exist!");
             }
     }
     return $data[$type];
 }