diff --git a/system/config/streams.yaml b/system/config/streams.yaml index eddcfce7a..cab7a866d 100644 --- a/system/config/streams.yaml +++ b/system/config/streams.yaml @@ -40,8 +40,7 @@ schemes: paths: - user/data - theme: + themes: type: ReadOnlyStream - prefixes: - '/': - - user/themes + paths: + - user/themes diff --git a/system/config/system.yaml b/system/config/system.yaml index 7529f9b01..bd18a408c 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -47,3 +47,5 @@ debugger: log: enabled: true # Enable logging timing: false # Enable timing logging + shutdown: + close_connection: true # Close the connection before calling onShutdown(). disable for debugging diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 476da4ac1..2a848fb7d 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -54,12 +54,17 @@ class Grav extends Container $container['grav'] = $container; - $container['events'] = function ($c) { - return new EventDispatcher; - }; $container['uri'] = function ($c) { return new Uri($c); }; + + $container['task'] = function ($c) { + return !empty($_POST['task']) ? $_POST['task'] : $c['uri']->param('task'); + }; + + $container['events'] = function ($c) { + return new EventDispatcher; + }; $container['config'] = function ($c) { return Config::instance($c); }; @@ -122,6 +127,15 @@ class Grav extends Container $this->fireEvent('onPluginsInitialized'); + $this['themes']->init(); + + $this->fireEvent('onThemeInitialized'); + + $task = $this['task']; + if ($task) { + $this->fireEvent('onTask.' . $task); + } + $this['assets']->init(); $this->fireEvent('onAssetsInitialized'); @@ -215,17 +229,18 @@ class Grav extends Container */ public function shutdown() { - set_time_limit(0); - ignore_user_abort(true); + if($this['config']->get('system.debugger.shutdown.close_connection')) { + set_time_limit(0); + ignore_user_abort(true); + session_write_close(); - header('Content-length: ' . ob_get_length()); - header("Connection: close\r\n"); + header('Content-length: ' . ob_get_length()); + header("Connection: close\r\n"); - ob_end_flush(); - ob_flush(); - flush(); - - session_write_close(); + ob_end_flush(); + ob_flush(); + flush(); + } $this->fireEvent('onShutdown'); } diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 9f70d8b7c..ec2c3bffe 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -60,14 +60,6 @@ class Plugins extends Iterator } } - /** @var Themes $themes */ - $themes = $this->grav['themes']; - $themes->configure(); - $instance = $themes->load(); - if ($instance instanceof EventSubscriberInterface) { - $events->addSubscriber($instance); - } - return $this->items; } diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index 215e50dbe..ca5854ecb 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -1,7 +1,11 @@ grav = $grav; $this->config = $grav['config']; } + public function init() + { + /** @var EventDispatcher $events */ + $events = $this->grav['events']; + + /** @var Themes $themes */ + $themes = $this->grav['themes']; + $themes->configure(); + $instance = $themes->load(); + + if ($instance instanceof EventSubscriberInterface) { + $events->addSubscriber($instance); + } + + $this->grav['theme'] = $instance; + } + /** * Return list of all theme data with their blueprints. * - * @return array|Data[] + * @return array */ public function all() { $list = array(); - $iterator = new \DirectoryIterator('theme:///'); + $iterator = new \DirectoryIterator('themes://'); /** @var \DirectoryIterator $directory */ foreach ($iterator as $directory) { @@ -46,7 +71,7 @@ class Themes extends Iterator } /** - * Get theme or throw exception if it cannot be found. + * Get theme configuration or throw exception if it cannot be found. * * @param string $name * @return Data @@ -58,20 +83,20 @@ class Themes extends Iterator throw new \RuntimeException('Theme name not provided.'); } - $blueprints = new Data\Blueprints("theme:///{$name}"); + $blueprints = new Blueprints("themes://{$name}"); $blueprint = $blueprints->get('blueprints'); $blueprint->name = $name; // Find thumbnail. - $thumb = "theme:///{$name}/thumbnail.jpg"; + $thumb = "themes://{$name}/thumbnail.jpg"; if (file_exists($thumb)) { $blueprint->set('thumbnail', $this->config->get('system.base_url_relative') . "/user/themes/{$name}/thumbnail.jpg"); } // Load default configuration. - $file = File\Yaml::instance("theme:///{$name}/{$name}" . YAML_EXT); - $obj = new Data\Data($file->content(), $blueprint); + $file = File\Yaml::instance("themes://{$name}/{$name}" . YAML_EXT); + $obj = new Data($file->content(), $blueprint); // Override with user configuration. $file = File\Yaml::instance("user://config/themes/{$name}" . YAML_EXT); @@ -83,52 +108,64 @@ class Themes extends Iterator return $obj; } - public function current($name = null) + /** + * Return name of the current theme. + * + * @return string + */ + public function current() { - - if (!$name) { - $name = $this->config->get('system.pages.theme'); - } - - return $name; + return (string) $this->config->get('system.pages.theme'); } - function load($name = null) + /** + * Load current theme. + * + * @return Theme|object + */ + public function load() { - $name = $this->current($name); + // NOTE: ALL THE LOCAL VARIABLES ARE USED INSIDE INCLUDED FILE, DO NOT REMOVE THEM! $grav = $this->grav; + $config = $this->config; + $name = $this->current(); /** @var ResourceLocator $locator */ $locator = $grav['locator']; - $file = $locator("theme://theme.php") ?: $locator("theme://{$name}.php"); + if ($file) { - // Local variables available in the file: $grav, $config, $name, $path, $file + // Local variables available in the file: $grav, $config, $name, $file $class = include $file; if (!is_object($class)) { $className = '\\Grav\\Theme\\' . ucfirst($name); if (class_exists($className)) { - $class = new $className($grav, $this->config, $name); + $class = new $className($grav, $config, $name); } } } if (empty($class)) { - $class = new Theme($grav, $this->config, $name); + $class = new Theme($grav, $config, $name); } return $class; } - public function configure($name = null) { - $name = $this->current($name); + /** + * Configure and prepare streams for current template. + * + * @throws \InvalidArgumentException + */ + public function configure() { + $name = $this->current(); /** @var Config $config */ $config = $this->config; - $themeConfig = File\Yaml::instance("theme://{$name}/{$name}" . YAML_EXT)->content(); + $themeConfig = File\Yaml::instance("themes://{$name}/{$name}" . YAML_EXT)->content(); $config->merge(['themes' => [$name => $themeConfig]]); @@ -163,7 +200,6 @@ class Themes extends Iterator if (!stream_wrapper_register($scheme, $type)) { throw new \InvalidArgumentException("Stream '{$type}' could not be initialized."); } - } } } diff --git a/system/src/Grav/Common/Twig.php b/system/src/Grav/Common/Twig.php index 0177ba029..2e8d57c7e 100644 --- a/system/src/Grav/Common/Twig.php +++ b/system/src/Grav/Common/Twig.php @@ -103,6 +103,7 @@ class Twig // Set some standard variables for twig $this->twig_vars = array( + 'grav' => $this->grav, 'grav_version' => GRAV_VERSION, 'config' => $config, 'uri' => $this->grav['uri'], diff --git a/system/src/Grav/Common/TwigExtension.php b/system/src/Grav/Common/TwigExtension.php index 82b0cdad3..7cb241bb9 100644 --- a/system/src/Grav/Common/TwigExtension.php +++ b/system/src/Grav/Common/TwigExtension.php @@ -213,6 +213,6 @@ class TwigExtension extends \Twig_Extension /** @var Uri $uri */ $uri = $grav['uri']; - return $uri->rootUrl($domain) . $locator->findResource($input, false); + return $uri->rootUrl($domain) .'/'. $locator->findResource($input, false); } } diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index cd9ddb29d..f196a50cd 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -301,4 +301,30 @@ class Uri // Return relative path. return substr($referrer, strlen($root)); } + + /** + * Retrun the IP address of the current user + * + * @return string ip address + */ + public function ip() + { + $ipaddress = ''; + if (getenv('HTTP_CLIENT_IP')) + $ipaddress = getenv('HTTP_CLIENT_IP'); + else if(getenv('HTTP_X_FORWARDED_FOR')) + $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); + else if(getenv('HTTP_X_FORWARDED')) + $ipaddress = getenv('HTTP_X_FORWARDED'); + else if(getenv('HTTP_FORWARDED_FOR')) + $ipaddress = getenv('HTTP_FORWARDED_FOR'); + else if(getenv('HTTP_FORWARDED')) + $ipaddress = getenv('HTTP_FORWARDED'); + else if(getenv('REMOTE_ADDR')) + $ipaddress = getenv('REMOTE_ADDR'); + else + $ipaddress = 'UNKNOWN'; + return $ipaddress; + + } }