/** * 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 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>"]]; } } }
/** * 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; }