public function getGlobals() { /** @var \Bolt\Config $config */ $config = $this->app['config']; $configVal = $this->safe ? null : $config; /** @var \Bolt\Users $users */ $users = $this->app['users']; /** @var \Bolt\Configuration\ResourceManager $resources */ $resources = $this->app['resources']; $zone = null; /** @var RequestStack $requestStack */ $requestStack = $this->app['request_stack']; if ($request = $requestStack->getCurrentRequest()) { $zone = Zone::get($request); } // User calls can cause exceptions that block the exception handler try { /** @deprecated Deprecated since 3.0, to be removed in 4.0. */ $usersVal = $this->safe ? null : $users->getUsers(); $usersCur = $users->getCurrentUser(); } catch (\Exception $e) { $usersVal = null; $usersCur = null; } // Structured to allow PHPStorm's SymfonyPlugin to provide code completion return ['bolt_name' => Bolt\Version::name(), 'bolt_version' => Bolt\Version::VERSION, 'bolt_stable' => Bolt\Version::isStable(), 'frontend' => $zone === Zone::FRONTEND, 'backend' => $zone === Zone::BACKEND, 'async' => $zone === Zone::ASYNC, 'paths' => $resources->getPaths(), 'theme' => $config->get('theme'), 'user' => $usersCur, 'users' => $usersVal, 'config' => $configVal]; }
/** * @param array $values */ public function __construct(array $values = []) { /** @deprecated since 3.0, to be removed in 4.0. */ $values['bolt_version'] = Version::VERSION; /** @deprecated since 3.0, to be removed in 4.0. */ $values['bolt_name'] = Version::name(); /** @deprecated since 3.0, to be removed in 4.0. */ $values['bolt_released'] = Version::isStable(); /** @deprecated since 3.0, to be removed in 4.0. */ $values['bolt_long_version'] = Version::VERSION; /** @internal Parameter to track a deprecated PHP version */ $values['deprecated.php'] = version_compare(PHP_VERSION, '5.5.9', '<'); parent::__construct($values); $this->register(new PathServiceProvider()); // Initialize the config. Note that we do this here, on 'construct'. // All other initialisation is triggered from bootstrap.php // Warning! // One of a valid ResourceManager ['resources'] or ClassLoader ['classloader'] // must be defined for working properly if (!isset($this['resources'])) { $this['resources'] = new Configuration\ResourceManager($this); } else { $this['classloader'] = $this['resources']->getClassLoader(); } // Register a PHP shutdown function to catch fatal errors with the application object register_shutdown_function(['\\Bolt\\Exception\\LowlevelException', 'catchFatalErrors'], $this); $this['resources']->setApp($this); $this->initConfig(); $this->initLogger(); $this['resources']->initialize(); $this['debug'] = $this['config']->get('general/debug', false); $locales = (array) $this['config']->get('general/locale'); $this['locale'] = reset($locales); // Initialize the 'editlink' and 'edittitle'. $this['editlink'] = ''; $this['edittitle'] = ''; // Initialize the JavaScript data gateway. $this['jsdata'] = []; }
/** * @param Request $request * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function installInfo(Request $request) { $package = $request->get('package'); $versions = ['dev' => [], 'stable' => []]; try { $info = $this->app['extend.info']->info($package, Bolt\Version::forComposer()); } catch (\Exception $e) { return $this->getJsonException($e); } if (isset($info->version) && is_array($info->version)) { foreach ($info->version as $version) { $versions[$version->stability][] = $version; } } return $this->json($versions); }
/** * Get the news from either cache or Bolt HQ. * * @param string $hostname * * @return array|string */ private function getNews($hostname) { /** @var \Bolt\Cache $cache */ $cache = $this->app['cache']; // Cached for two hours. $news = $cache->fetch('dashboardnews'); // If not cached, get fresh news. if ($news !== false) { $this->app['logger.system']->info('Using cached data', ['event' => 'news']); return $news; } else { $source = $this->getOption('general/branding/news_source', 'http://news.bolt.cm/'); $curl = $this->getDashboardCurlOptions($hostname, $source); $this->app['logger.system']->info('Fetching from remote server: ' . $source, ['event' => 'news']); try { $fetchedNewsData = $this->app['guzzle.client']->get($curl['url'], ['config' => ['curl' => $curl['options']]])->getBody(true); if ($this->getOption('general/branding/news_variable')) { $newsVariable = $this->getOption('general/branding/news_variable'); $fetchedNewsItems = json_decode($fetchedNewsData)->{$newsVariable}; } else { $fetchedNewsItems = json_decode($fetchedNewsData); } if ($fetchedNewsItems) { $news = []; // Iterate over the items, pick the first news-item that // applies and the first alert we need to show foreach ($fetchedNewsItems as $item) { $type = $item->type === 'alert' ? 'alert' : 'information'; if (!isset($news[$type]) && (empty($item->target_version) || Bolt\Version::compare($item->target_version, '>'))) { $news[$type] = $item; } } $cache->save('dashboardnews', $news, 7200); } else { $this->app['logger.system']->error('Invalid JSON feed returned', ['event' => 'news']); } return $news; } catch (RequestException $e) { $this->app['logger.system']->critical('Error occurred during newsfeed fetch', ['event' => 'exception', 'exception' => $e]); return ['error' => ['type' => 'error', 'title' => 'Unable to fetch news!', 'teaser' => "<p>Unable to connect to {$source}</p>"]]; } } }
/** * Attempt to load cached configuration files. * * @throws LowlevelException * * @return bool */ protected function loadCache() { $dir = $this->app['resources']->getPath('config'); /* Get the timestamps for the config files. `config_local.yml` and `extensions.yml` default to '0', because if they aren't present, it shouldn't trigger an update for the cache, while the others should. */ $timestamps = [file_exists($dir . '/config.yml') ? filemtime($dir . '/config.yml') : 10000000000, file_exists($dir . '/taxonomy.yml') ? filemtime($dir . '/taxonomy.yml') : 10000000000, file_exists($dir . '/contenttypes.yml') ? filemtime($dir . '/contenttypes.yml') : 10000000000, file_exists($dir . '/menu.yml') ? filemtime($dir . '/menu.yml') : 10000000000, file_exists($dir . '/routing.yml') ? filemtime($dir . '/routing.yml') : 10000000000, file_exists($dir . '/permissions.yml') ? filemtime($dir . '/permissions.yml') : 10000000000, file_exists($dir . '/extensions.yml') ? filemtime($dir . '/extensions.yml') : 0, file_exists($dir . '/config_local.yml') ? filemtime($dir . '/config_local.yml') : 0]; $configCache = $this->app['resources']->getPath('cache/config-cache.json'); $this->cachetimestamp = file_exists($configCache) ? filemtime($configCache) : 0; if ($this->cachetimestamp > max($timestamps)) { $finder = new Finder(); $finder->files()->in($this->app['resources']->getPath('cache'))->name('config-cache.json')->depth('== 0'); /** @var SplFileInfo $file */ foreach ($finder as $file) { try { $this->data = json_decode($file->getContents(), true); } catch (\RuntimeException $e) { $part = Translator::__('Try logging in with your ftp-client and make the file readable. ' . 'Else try to go <a>back</a> to the last page.'); $message = '<p>' . Translator::__('general.phrase.file-not-readable-following-colon') . '</p>' . '<pre>' . htmlspecialchars($configCache) . '</pre>' . '<p>' . str_replace('<a>', '<a href="javascript:history.go(-1)">', $part) . '</p>'; throw new LowlevelException(Translator::__('page.file-management.message.file-not-readable' . $message)); } } // Check if we loaded actual data. if (count($this->data) < 4 || empty($this->data['general'])) { return false; } // Check to make sure the version is still the same. If not, effectively invalidate the // cached config to force a reload. if (!isset($this->data['version']) || Bolt\Version::compare($this->data['version'], '!=')) { // The logger and the flashbags aren't available yet, so we set a flag to notify the user later. $this->notify_update = true; return false; } // Trigger the config loaded event on the resource manager $this->app['resources']->initializeConfig($this->data); // Yup, all seems to be right. return true; } return false; }
/** * Get the parameters that will be used to update Bolt's registered Twig * globals. * * This is here as a transitory measure. * * @param bool $safe * * @return array */ private function setGlobals($safe) { /** @var \Twig_Environment $twig */ $twig = $safe ? $this->app['safe_twig'] : $this->app['twig']; /** @var \Bolt\Config $config */ $config = $this->app['config']; $configVal = $safe ? null : $config; /** @var \Bolt\Users $users */ $users = $this->app['users']; /** @var \Bolt\Configuration\ResourceManager $resources */ $resources = $this->app['resources']; $zone = null; /** @var RequestStack $requestStack */ $requestStack = $this->app['request_stack']; if ($request = $requestStack->getCurrentRequest()) { $zone = Zone::get($request); } // User calls can cause exceptions that block the exception handler try { /** @deprecated Deprecated since 3.0, to be removed in 4.0. */ $usersVal = $safe ? null : $users->getUsers(); $usersCur = $users->getCurrentUser(); } catch (\Exception $e) { $usersVal = null; $usersCur = null; } $twig->addGlobal('bolt_name', Bolt\Version::name()); $twig->addGlobal('bolt_version', Bolt\Version::VERSION); $twig->addGlobal('bolt_stable', Bolt\Version::isStable()); $twig->addGlobal('frontend', $zone === Zone::FRONTEND); $twig->addGlobal('backend', $zone === Zone::BACKEND); $twig->addGlobal('async', $zone === Zone::ASYNC); $twig->addGlobal('paths', $resources->getPaths()); $twig->addGlobal('theme', $config->get('theme')); $twig->addGlobal('user', $usersCur); $twig->addGlobal('users', $usersVal); $twig->addGlobal('config', $configVal); }
/** * Enforce the default JSON settings. * * @param array $json * * @return array */ private function setJsonDefaults(array $json) { $rootPath = $this->app['resources']->getPath('root'); $extensionsPath = $this->app['resources']->getPath('extensions'); $srcPath = $this->app['resources']->getPath('src'); $webPath = $this->app['resources']->getPath('web'); $pathToRoot = Path::makeRelative($rootPath, $extensionsPath); $pathToWeb = Path::makeRelative($webPath, $extensionsPath); $eventPath = Path::makeRelative($srcPath . '/Composer/EventListener', $extensionsPath); // Enforce standard settings $defaults = ['name' => 'bolt/extensions', 'description' => 'Bolt extension installation interface', 'license' => 'MIT', 'repositories' => ['packagist' => false, 'bolt' => ['type' => 'composer', 'url' => $this->app['extend.site'] . 'satis/']], 'minimum-stability' => $this->app['config']->get('general/extensions/stability', 'stable'), 'prefer-stable' => true, 'config' => ['discard-changes' => true, 'preferred-install' => 'dist'], 'provide' => ['bolt/bolt' => Bolt\Version::forComposer()], 'extra' => ['bolt-web-path' => $pathToWeb, 'bolt-root-path' => $pathToRoot], 'autoload' => ['psr-4' => ['Bolt\\Composer\\EventListener\\' => $eventPath]], 'scripts' => ['post-autoload-dump' => 'Bolt\\Composer\\EventListener\\PackageEventListener::dump', 'post-package-install' => 'Bolt\\Composer\\EventListener\\PackageEventListener::handle', 'post-package-update' => 'Bolt\\Composer\\EventListener\\PackageEventListener::handle']]; $json = Arr::mergeRecursiveDistinct($json, $defaults); ksort($json); return $json; }
/** * Get the news from Bolt HQ. * * @param string $hostname * * @return array */ private function fetchNews($hostname) { $source = $this->getOption('general/branding/news_source', 'http://news.bolt.cm/'); $options = $this->fetchNewsOptions($hostname); $this->app['logger.system']->info('Fetching from remote server: ' . $source, ['event' => 'news']); try { $fetchedNewsData = (string) $this->app['guzzle.client']->get($source, $options)->getBody(true); } catch (RequestException $e) { $this->app['logger.system']->error('Error occurred during newsfeed fetch', ['event' => 'exception', 'exception' => $e]); return ['error' => ['type' => 'error', 'title' => 'Unable to fetch news!', 'teaser' => "<p>Unable to connect to {$source}</p>"]]; } $fetchedNewsItems = json_decode($fetchedNewsData); if ($newsVariable = $this->getOption('general/branding/news_variable')) { $fetchedNewsItems = $fetchedNewsItems->{$newsVariable}; } $news = []; if (!$fetchedNewsItems) { $this->app['logger.system']->error('Invalid JSON feed returned', ['event' => 'news']); return $news; } // Iterate over the items, pick the first news-item that // applies and the first alert we need to show foreach ($fetchedNewsItems as $item) { $type = $item->type === 'alert' ? 'alert' : 'information'; if (!isset($news[$type]) && (empty($item->target_version) || Bolt\Version::compare($item->target_version, '>'))) { $news[$type] = $item; } } return $news; }