コード例 #1
0
ファイル: Themes.php プロジェクト: miguelramos/grav
 /**
  * Get theme or throw exception if it cannot be found.
  *
  * @param string $name
  * @return Data
  * @throws \RuntimeException
  */
 public function get($name)
 {
     if (!$name) {
         throw new \RuntimeException('Theme name not provided.');
     }
     $blueprints = new Blueprints("theme:///{$name}");
     $blueprint = $blueprints->get('blueprints');
     $blueprint->name = $name;
     /** @var Config $config */
     $config = $this->grav['config'];
     // Find thumbnail.
     $thumb = "theme:///{$name}/thumbnail.jpg";
     if (file_exists($thumb)) {
         $blueprint->set('thumbnail', $config->get('system.base_url_relative') . "/user/themes/{$name}/thumbnail.jpg");
     }
     // Load default configuration.
     $file = Yaml::instance("theme:///{$name}/{$name}.yaml");
     $obj = new Data($file->content(), $blueprint);
     // Override with user configuration.
     $file = Yaml::instance("user://config/themes/{$name}.yaml");
     $obj->merge($file->content());
     // Save configuration always to user/config.
     $obj->file($file);
     return $obj;
 }
コード例 #2
0
ファイル: Themes.php プロジェクト: qbi/datenknoten.me
 /**
  * Get theme configuration or throw exception if it cannot be found.
  *
  * @param  string            $name
  * @return Data
  * @throws \RuntimeException
  */
 public function get($name)
 {
     if (!$name) {
         throw new \RuntimeException('Theme name not provided.');
     }
     $blueprints = new Blueprints("themes://{$name}");
     $blueprint = $blueprints->get('blueprints');
     $blueprint->name = $name;
     // Find thumbnail.
     $thumb = "themes://{$name}/thumbnail.jpg";
     if (file_exists($thumb)) {
         $blueprint->set('thumbnail', $this->grav['base_url'] . "/user/themes/{$name}/thumbnail.jpg");
     }
     // Load default configuration.
     $file = CompiledYamlFile::instance("themes://{$name}/{$name}" . YAML_EXT);
     $obj = new Data($file->content(), $blueprint);
     // Override with user configuration.
     $file = CompiledYamlFile::instance("user://config/themes/{$name}" . YAML_EXT);
     $obj->merge($file->content());
     // Save configuration always to user/config.
     $obj->file($file);
     return $obj;
 }
コード例 #3
0
ファイル: admin.php プロジェクト: symac/grav-plugin-admin
 /**
  * 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];
 }
コード例 #4
0
ファイル: Plugins.php プロジェクト: clee03/metal
 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;
 }
コード例 #5
0
ファイル: Plugins.php プロジェクト: miguelramos/grav
 public static function get($type)
 {
     $blueprints = new Data\Blueprints('plugin://' . $type);
     $blueprint = $blueprints->get('blueprints');
     $blueprint->name = $type;
     // Load default configuration.
     $file = File\Yaml::instance('plugin://' . "{$type}/{$type}" . YAML_EXT);
     $obj = new Data\Data($file->content(), $blueprint);
     // Override with user configuration.
     $file = File\Yaml::instance('plugin://' . "config/plugins/{$type}" . YAML_EXT);
     $obj->merge($file->content());
     // Save configuration always to user/config.
     $obj->file($file);
     return $obj;
 }
コード例 #6
0
ファイル: admin.php プロジェクト: getgrav/grav-plugin-admin
 /**
  * Gets configuration data.
  *
  * @param string $type
  * @param array  $post
  *
  * @return mixed
  * @throws \RuntimeException
  */
 public function data($type, array $post = [])
 {
     static $data = [];
     if (isset($data[$type])) {
         return $data[$type];
     }
     if (!$post) {
         $post = isset($_POST['data']) ? $_POST['data'] : [];
     }
     // Check to see if a data type is plugin-provided, before looking into core ones
     $event = $this->grav->fireEvent('onAdminData', new Event(['type' => &$type]));
     if ($event && isset($event['data_type'])) {
         return $event['data_type'];
     }
     /** @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));
         if (!$obj) {
             return [];
         }
         $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));
         if (!$obj) {
             return [];
         }
         $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;
     } elseif (preg_match('|user/|', $type)) {
         $obj = User::load(preg_replace('|user/|', '', $type));
         $obj->merge($post);
         $data[$type] = $obj;
     } elseif (preg_match('|config/|', $type)) {
         $type = preg_replace('|config/|', '', $type);
         $blueprints = $this->blueprints("config/{$type}");
         $config = $this->grav['config'];
         $obj = new Data\Data($config->get($type, []), $blueprints);
         $obj->merge($post);
         // FIXME: We shouldn't allow user to change configuration files in system folder!
         $filename = $this->grav['locator']->findResource("config://{$type}.yaml") ?: $this->grav['locator']->findResource("config://{$type}.yaml", true, true);
         $file = CompiledYamlFile::instance($filename);
         $obj->file($file);
         $data[$type] = $obj;
     } else {
         throw new \RuntimeException("Data type '{$type}' doesn't exist!");
     }
     return $data[$type];
 }
コード例 #7
0
ファイル: Config.php プロジェクト: miguelramos/grav
 /**
  * Initialize object by loading all the configuration files.
  *
  * @param array $files
  */
 protected function init(array $files)
 {
     $this->updated = true;
     // Combine all configuration files into one larger lookup table (only keys matter).
     $allFiles = $files['user'] + $files['plugins'] + $files['system'];
     // Then sort the files to have all parent nodes first.
     // This is to make sure that child nodes override parents content.
     uksort($allFiles, function ($a, $b) {
         $diff = substr_count($a, '/') - substr_count($b, '/');
         return $diff ? $diff : strcmp($a, $b);
     });
     $systemBlueprints = new Blueprints(SYSTEM_DIR . 'blueprints');
     $pluginBlueprints = new Blueprints(USER_DIR);
     $items = array();
     foreach ($allFiles as $name => $dummy) {
         $lookup = array('system' => SYSTEM_DIR . 'config/' . $name . YAML_EXT, 'plugins' => USER_DIR . $name . '/' . basename($name) . YAML_EXT, 'user' => USER_DIR . 'config/' . $name . YAML_EXT);
         if (strpos($name, 'plugins/') === 0) {
             $blueprint = $pluginBlueprints->get("{$name}/blueprints");
         } else {
             $blueprint = $systemBlueprints->get($name);
         }
         $data = new Data(array(), $blueprint);
         foreach ($lookup as $key => $path) {
             if (is_file($path)) {
                 $data->merge(File\Yaml::instance($path)->content());
             }
         }
         //            $data->validate();
         //            $data->filter();
         // Find the current sub-tree location.
         $current =& $items;
         $parts = explode('/', $name);
         foreach ($parts as $part) {
             if (!isset($current[$part])) {
                 $current[$part] = array();
             }
             $current =& $current[$part];
         }
         // Handle both updated and deleted configuration files.
         $current = $data->toArray();
     }
     $this->items = $items;
     $this->files = $files;
 }
コード例 #8
0
ファイル: Themes.php プロジェクト: jeremycherfas/grav-blog
 /**
  * Get theme configuration or throw exception if it cannot be found.
  *
  * @param  string $name
  *
  * @return Data
  * @throws \RuntimeException
  */
 public function get($name)
 {
     if (!$name) {
         throw new \RuntimeException('Theme name not provided.');
     }
     $blueprints = new Blueprints('themes://');
     $blueprint = $blueprints->get("{$name}/blueprints");
     $blueprint->name = $name;
     // Load default configuration.
     $file = CompiledYamlFile::instance("themes://{$name}/{$name}" . YAML_EXT);
     // ensure this is a valid theme
     if (!$file->exists()) {
         return null;
     }
     // Find thumbnail.
     $thumb = "themes://{$name}/thumbnail.jpg";
     $path = $this->grav['locator']->findResource($thumb, false);
     if ($path) {
         $blueprint->set('thumbnail', $this->grav['base_url'] . '/' . $path);
     }
     $obj = new Data($file->content(), $blueprint);
     // Override with user configuration.
     $obj->merge($this->grav['config']->get('themes.' . $name) ?: []);
     // Save configuration always to user/config.
     $file = CompiledYamlFile::instance("config://themes/{$name}" . YAML_EXT);
     $obj->file($file);
     return $obj;
 }
コード例 #9
0
ファイル: Plugins.php プロジェクト: qbi/datenknoten.me
 public static function get($name)
 {
     $blueprints = new Blueprints("plugins://{$name}");
     $blueprint = $blueprints->get('blueprints');
     $blueprint->name = $name;
     // Load default configuration.
     $file = CompiledYamlFile::instance("plugins://{$name}/{$name}.yaml");
     $obj = new Data($file->content(), $blueprint);
     // Override with user configuration.
     $file = CompiledYamlFile::instance("user://config/plugins/{$name}.yaml");
     $obj->merge($file->content());
     // Save configuration always to user/config.
     $obj->file($file);
     return $obj;
 }
コード例 #10
0
ファイル: form.php プロジェクト: getgrav/grav-plugin-form
 /**
  * Handle form processing on POST action.
  */
 public function post()
 {
     $grav = Grav::instance();
     $uri = $grav['uri']->url;
     $session = $grav['session'];
     if (isset($_POST)) {
         $this->values = new Data(isset($_POST) ? (array) $_POST : []);
         $data = $this->values->get('data');
         // Add post data to form dataset
         if (!$data) {
             $data = $this->values->toArray();
         }
         if (method_exists('Grav\\Common\\Utils', 'getNonce')) {
             if (!$this->values->get('form-nonce') || !Utils::verifyNonce($this->values->get('form-nonce'), 'form')) {
                 $event = new Event(['form' => $this, 'message' => $grav['language']->translate('PLUGIN_FORM.NONCE_NOT_VALIDATED')]);
                 $grav->fireEvent('onFormValidationError', $event);
                 return;
             }
         }
         $i = 0;
         foreach ($this->items['fields'] as $key => $field) {
             $name = isset($field['name']) ? $field['name'] : $key;
             if (!isset($field['name'])) {
                 if (isset($data[$i])) {
                     //Handle input@ false fields
                     $data[$name] = $data[$i];
                     unset($data[$i]);
                 }
             }
             if ($field['type'] == 'checkbox') {
                 $data[$name] = isset($data[$name]) ? true : false;
             }
             $i++;
         }
         $this->data->merge($data);
     }
     // Validate and filter data
     try {
         $this->data->validate();
         $this->data->filter();
         $grav->fireEvent('onFormValidationProcessed', new Event(['form' => $this]));
     } catch (\RuntimeException $e) {
         $event = new Event(['form' => $this, 'message' => $e->getMessage(), 'messages' => $e->getMessages()]);
         $grav->fireEvent('onFormValidationError', $event);
         if ($event->isPropagationStopped()) {
             return;
         }
     }
     // Process previously uploaded files for the current URI
     // and finally store them. Everything else will get discarded
     $queue = $session->getFlashObject('files-upload');
     $queue = $queue[base64_encode($uri)];
     if (is_array($queue)) {
         foreach ($queue as $key => $files) {
             foreach ($files as $destination => $file) {
                 if (!rename($file['tmp_name'], $destination)) {
                     throw new \RuntimeException(sprintf($grav['language']->translate('PLUGIN_FORM.FILEUPLOAD_UNABLE_TO_MOVE', null, true), '"' . $file['tmp_name'] . '"', $destination));
                 }
                 unset($files[$destination]['tmp_name']);
             }
             $this->data->merge([$key => $files]);
         }
     }
     $process = isset($this->items['process']) ? $this->items['process'] : [];
     if (is_array($process)) {
         $event = null;
         foreach ($process as $action => $data) {
             if (is_numeric($action)) {
                 $action = \key($data);
                 $data = $data[$action];
             }
             $previousEvent = $event;
             $event = new Event(['form' => $this, 'action' => $action, 'params' => $data]);
             if ($previousEvent) {
                 if (!$previousEvent->isPropagationStopped()) {
                     $grav->fireEvent('onFormProcessed', $event);
                 } else {
                     break;
                 }
             } else {
                 $grav->fireEvent('onFormProcessed', $event);
             }
         }
     } else {
         // Default action.
     }
 }