From 204f81c6f72af7930de6c3e25d1f89c8394be728 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Fri, 28 Dec 2018 11:15:40 +0100 Subject: [PATCH] [ticket/10824] Use json_sanitzer::decode and improve migrations from pre 3.2 PHPBB3-10824 --- phpBB/includes/acp/acp_styles.php | 3 ++- phpBB/phpbb/cache/service.php | 4 ++- .../migration/data/v310/style_update_p1.php | 26 ++++++++++++++----- .../db/migration/data/v31x/style_update.php | 9 ++++--- phpBB/phpbb/language/language_file_helper.php | 6 ++--- phpBB/phpbb/version_helper.php | 2 +- 6 files changed, 35 insertions(+), 15 deletions(-) diff --git a/phpBB/includes/acp/acp_styles.php b/phpBB/includes/acp/acp_styles.php index a157d283b0..7d7b5b6b9f 100644 --- a/phpBB/includes/acp/acp_styles.php +++ b/phpBB/includes/acp/acp_styles.php @@ -1191,6 +1191,7 @@ class acp_styles * Read style composer.json file * * @param string $dir style directory + * * @return array Style data * @throws \DomainException in case of error */ @@ -1203,7 +1204,7 @@ class acp_styles } $json = file_get_contents($this->styles_path . $dir . '/composer.json'); - $style_data = \phpbb\json_sanitizer::sanitize(json_decode($json, true)); + $style_data = \phpbb\json_sanitizer::decode($json); if (!is_array($style_data) || !isset($style_data['type']) || $style_data['type'] !== 'phpbb-style') { diff --git a/phpBB/phpbb/cache/service.php b/phpBB/phpbb/cache/service.php index 982ff84ee6..950080ef48 100644 --- a/phpBB/phpbb/cache/service.php +++ b/phpBB/phpbb/cache/service.php @@ -13,6 +13,8 @@ namespace phpbb\cache; +use phpbb\json_sanitizer; + /** * Class for grabbing/handling cached entries */ @@ -355,7 +357,7 @@ class service { // Re-parse cfg file $json = file_get_contents($filename); - $parsed_array = json_decode($json, true); + $parsed_array = json_sanitizer::decode($json); $parsed_array['filetime'] = @filemtime($filename); $this->driver->put('_cfg_' . $style['style_path'], $parsed_array); diff --git a/phpBB/phpbb/db/migration/data/v310/style_update_p1.php b/phpBB/phpbb/db/migration/data/v310/style_update_p1.php index 2b3fc0b670..0ba9b86a33 100644 --- a/phpBB/phpbb/db/migration/data/v310/style_update_p1.php +++ b/phpBB/phpbb/db/migration/data/v310/style_update_p1.php @@ -13,6 +13,8 @@ namespace phpbb\db\migration\data\v310; +use phpbb\json_sanitizer; + class style_update_p1 extends \phpbb\db\migration\migration { public function effectively_installed() @@ -69,14 +71,26 @@ class style_update_p1 extends \phpbb\db\migration\migration $skip_dirs = array('.', '..', 'prosilver'); foreach ($iterator as $fileinfo) { - if ($fileinfo->isDir() && !in_array($fileinfo->getFilename(), $skip_dirs) && file_exists($fileinfo->getPathname() . '/composer.json')) + if ($fileinfo->isDir() && !in_array($fileinfo->getFilename(), $skip_dirs)) { - $json = file_get_contents($fileinfo->getPathname() . '/composer.json'); - $style_data = json_decode($json, true); - if (isset($style_data['extra']['phpbb-version']) && version_compare($style_data['extra']['phpbb-version'], '3.1.0-dev', '>=')) + if (file_exists($fileinfo->getPathname() . '/style.cfg')) { - // 3.1 style - $available_styles[] = $fileinfo->getFilename(); + $style_cfg = parse_cfg_file($fileinfo->getPathname() . '/style.cfg'); + if (isset($style_cfg['phpbb_version']) && version_compare($style_cfg['phpbb_version'], '3.1.0-dev', '>=')) + { + // 3.1 & 3.2 style + $available_styles[] = $fileinfo->getFilename(); + } + } + else if (file_exists($fileinfo->getPathname() . '/composer.json')) + { + $json = file_get_contents($fileinfo->getPathname() . '/composer.json'); + $style_data = json_sanitizer::decode($json); + if (isset($style_data['extra']['phpbb-version']) && version_compare($style_data['extra']['phpbb-version'], '3.3.0-dev', '>=')) + { + // 3.3 style + $available_styles[] = $fileinfo->getFilename(); + } } } } diff --git a/phpBB/phpbb/db/migration/data/v31x/style_update.php b/phpBB/phpbb/db/migration/data/v31x/style_update.php index 36f1209c71..0c3f53c4a9 100644 --- a/phpBB/phpbb/db/migration/data/v31x/style_update.php +++ b/phpBB/phpbb/db/migration/data/v31x/style_update.php @@ -49,8 +49,11 @@ class style_update extends \phpbb\db\migration\migration // Install prosilver if no style is available and prosilver can be installed if (empty($style_paths) && in_array('prosilver', $styles)) { - // Stop running this if prosilver composer.json file can't be read - if (file_exists($this->phpbb_root_path . 'styles/prosilver/composer.json')) + // Try to parse config file + $cfg = parse_cfg_file($this->phpbb_root_path . 'styles/prosilver/style.cfg'); + + // Stop running this if both prosilver cfg file and composer.json file can't be read + if (empty($cfg) && !file_exists($this->phpbb_root_path . 'styles/prosilver/composer.json')) { throw new \RuntimeException('No styles available and could not fall back to prosilver.'); } @@ -120,7 +123,7 @@ class style_update extends \phpbb\db\migration\migration continue; } - if (file_exists("{$dir}/composer.json")) + if (file_exists("{$dir}/composer.json") || file_exists("{$dir}/style.cfg")) { $styles[] = $file; } diff --git a/phpBB/phpbb/language/language_file_helper.php b/phpBB/phpbb/language/language_file_helper.php index 14f64c701a..75e890ac06 100644 --- a/phpBB/phpbb/language/language_file_helper.php +++ b/phpBB/phpbb/language/language_file_helper.php @@ -13,6 +13,7 @@ namespace phpbb\language; +use phpbb\json_sanitizer; use Symfony\Component\Finder\Finder; /** @@ -54,7 +55,7 @@ class language_file_helper foreach ($finder as $file) { $json = $file->getContents(); - $data = \phpbb\json_sanitizer::sanitize(json_decode($json, true)); + $data = json_sanitizer::decode($json); $available_languages[] = $this->get_language_data_from_json($data); } @@ -71,7 +72,7 @@ class language_file_helper public function get_language_data_from_composer_file($path) { $json_data = file_get_contents($path); - return $this->get_language_data_from_json(json_decode($json_data, true)); + return $this->get_language_data_from_json(json_sanitizer::decode($json_data)); } /** @@ -101,7 +102,6 @@ class language_file_helper return array( 'iso' => $data['extra']['language-iso'], - 'name' => $data['extra']['english-name'], 'local_name' => $data['extra']['local-name'], 'author' => implode(', ', $authors), diff --git a/phpBB/phpbb/version_helper.php b/phpBB/phpbb/version_helper.php index c24d317097..5bcd311ffe 100644 --- a/phpBB/phpbb/version_helper.php +++ b/phpBB/phpbb/version_helper.php @@ -390,7 +390,7 @@ class version_helper } // Sanitize any data we retrieve from a server - $info = \phpbb\json_sanitizer::sanitize(json_decode($info, true)); + $info = json_sanitizer::decode($info); if (empty($info['stable']) && empty($info['unstable'])) {