From be8b5a41c81853cd8f7ba9ee97b68aa512617366 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 1 Nov 2014 20:09:08 +0100 Subject: [ticket/13263] Use prosilver as default style if user's style doesn't exist If prosilver is not installed for whatever reason, it will be installed by the newly added migration. PHPBB3-13263 --- .../phpbb/db/migration/data/v31x/style_update.php | 175 +++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 phpBB/phpbb/db/migration/data/v31x/style_update.php (limited to 'phpBB/phpbb/db') diff --git a/phpBB/phpbb/db/migration/data/v31x/style_update.php b/phpBB/phpbb/db/migration/data/v31x/style_update.php new file mode 100644 index 0000000000..b0ac80245e --- /dev/null +++ b/phpBB/phpbb/db/migration/data/v31x/style_update.php @@ -0,0 +1,175 @@ + +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\db\migration\data\v31x; + +class style_update extends \phpbb\db\migration\migration +{ + static public function depends_on() + { + return array('\phpbb\db\migration\data\v310\gold'); + } + + public function update_data() + { + return array( + array('custom', array(array($this, 'update_installed_styles'))), + ); + } + + public function update_installed_styles() + { + // First check if prosilver is properly installed + $sql = 'SELECT style_id, style_active + FROM ' . $this->table_prefix . "styles + WHERE style_name = 'prosilver'"; + $result = $this->db->sql_query($sql); + $row = $this->db->sql_fetchrow($result); + $this->db->sql_freeresult($result); + + // Make sure prosilver is installed + if (empty($row) || !isset($row['style_id'])) + { + // Try to parse config file + $cfg = parse_cfg_file($this->phpbb_root_path . 'styles/prosilver/style.cfg'); + + // Stop running this if prosilver doesn't exist + if (empty($cfg)) + { + return; + } + + // Check data + if (!isset($cfg['template_bitfield'])) + { + $cfg['template_bitfield'] = $this->default_bitfield(); + } + + $style = array( + 'style_name' => 'prosilver', + 'style_copyright' => '© phpBB Limited', + 'style_active' => 1, + 'style_path' => 'prosilver', + 'bbcode_bitfield' => $cfg['template_bitfield'], + 'style_parent_id' => 0, + 'style_parent_tree' => '', + ); + + // Add to database + $this->db->sql_transaction('begin'); + + $sql = 'INSERT INTO ' . $this->table_prefix . 'styles + ' . $this->db->sql_build_array('INSERT', $style); + $this->db->sql_query($sql); + + $row = array('style_id' => $this->db->sql_nextid()); + + $this->db->sql_transaction('commit'); + } + // Make sure prosilver is activated + else if (!isset($row['style_active']) || !$row['style_active']) + { + $sql = 'UPDATE ' . STYLES_TABLE . ' SET style_active = 1 WHERE style_id = ' . $row['style_id']; + $this->db->sql_query($sql); + } + + // Get all currently available styles + $styles = $this->find_style_dirs(); + + // Get IDs of the available styles + $style_ids = array(); + $sql = 'SELECT DISTINCT(style_id) AS style_id + FROM ' . $this->table_prefix . 'styles + WHERE ' . $this->db->sql_in_set('style_name', $styles); + $result = $this->db->sql_query($sql); + while ($styles_row = $this->db->sql_fetchrow()) + { + $style_ids[] = $styles_row['style_id']; + } + $this->db->sql_freeresult($result); + + $sql = 'UPDATE ' . $this->table_prefix . "users + SET user_style = {$row['style_id']} + WHERE " . $this->db->sql_in_set('user_style', $style_ids, true); + $this->db->sql_query($sql); + } + + /** + * Generates default bitfield + * Copied from acp_styles + * + * This bitfield decides which bbcodes are defined in a template. + * + * @return string Bitfield + */ + protected function default_bitfield() + { + static $value; + if (isset($value)) + { + return $value; + } + + if (!class_exists('bitfield')) + { + include($this->phpbb_root_path . 'includes/functions_content.' . $this->php_ext); + } + + // Hardcoded template bitfield to add for new templates + $bitfield = new \bitfield(); + $bitfield->set(0); + $bitfield->set(1); + $bitfield->set(2); + $bitfield->set(3); + $bitfield->set(4); + $bitfield->set(8); + $bitfield->set(9); + $bitfield->set(11); + $bitfield->set(12); + $value = $bitfield->get_base64(); + return $value; + } + + /** + * Find all directories that have styles + * Copied from acp_styles + * + * @return array Directory names + */ + protected function find_style_dirs() + { + $styles = array(); + $styles_path = $this->phpbb_root_path . 'styles/'; + + $dp = @opendir($styles_path); + if ($dp) + { + while (($file = readdir($dp)) !== false) + { + $dir = $styles_path . $file; + if ($file[0] == '.' || !is_dir($dir)) + { + continue; + } + + if (file_exists("{$dir}/style.cfg")) + { + $styles[] = $file; + } + } + closedir($dp); + } + + return $styles; + } +} -- cgit v1.2.1 From d1f85f0de3dd958050df39ea79d2e7cd14147b07 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 1 Nov 2014 23:22:44 +0100 Subject: [ticket/13263] Only install/set prosilver if no style available Users that have a nonexistent style selectd will revert back to the default style. PHPBB3-13263 --- .../phpbb/db/migration/data/v31x/style_update.php | 52 ++++++++++------------ 1 file changed, 24 insertions(+), 28 deletions(-) (limited to 'phpBB/phpbb/db') diff --git a/phpBB/phpbb/db/migration/data/v31x/style_update.php b/phpBB/phpbb/db/migration/data/v31x/style_update.php index b0ac80245e..9f01514ff6 100644 --- a/phpBB/phpbb/db/migration/data/v31x/style_update.php +++ b/phpBB/phpbb/db/migration/data/v31x/style_update.php @@ -29,16 +29,25 @@ class style_update extends \phpbb\db\migration\migration public function update_installed_styles() { - // First check if prosilver is properly installed - $sql = 'SELECT style_id, style_active - FROM ' . $this->table_prefix . "styles - WHERE style_name = 'prosilver'"; + // Get all currently available styles + $styles = $this->find_style_dirs(); + $style_paths = $style_ids = array(); + + $sql = 'SELECT style_path, style_id + FROM ' . $this->table_prefix . 'styles'; $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); + while ($styles_row = $this->db->sql_fetchrow()) + { + if (in_array($styles_row['style_path'], $styles)) + { + $style_paths[] = $styles_row['style_path']; + $style_ids[] = $styles_row['style_id']; + } + } $this->db->sql_freeresult($result); - // Make sure prosilver is installed - if (empty($row) || !isset($row['style_id'])) + // Install prosilver if no style is available and prosilver can be installed + if (empty($style_paths) && in_array('prosilver', $styles)) { // Try to parse config file $cfg = parse_cfg_file($this->phpbb_root_path . 'styles/prosilver/style.cfg'); @@ -46,7 +55,7 @@ class style_update extends \phpbb\db\migration\migration // Stop running this if prosilver doesn't exist if (empty($cfg)) { - return; + throw new \RuntimeException('No styles available and could not fall back to prosilver.'); } // Check data @@ -75,31 +84,18 @@ class style_update extends \phpbb\db\migration\migration $row = array('style_id' => $this->db->sql_nextid()); $this->db->sql_transaction('commit'); - } - // Make sure prosilver is activated - else if (!isset($row['style_active']) || !$row['style_active']) - { - $sql = 'UPDATE ' . STYLES_TABLE . ' SET style_active = 1 WHERE style_id = ' . $row['style_id']; - $this->db->sql_query($sql); - } - - // Get all currently available styles - $styles = $this->find_style_dirs(); - // Get IDs of the available styles - $style_ids = array(); - $sql = 'SELECT DISTINCT(style_id) AS style_id - FROM ' . $this->table_prefix . 'styles - WHERE ' . $this->db->sql_in_set('style_name', $styles); - $result = $this->db->sql_query($sql); - while ($styles_row = $this->db->sql_fetchrow()) + // Set prosilver to default style + $this->config->set('default_style', $row['style_id']); + } + else if (empty($styles) && empty($available_styles)) { - $style_ids[] = $styles_row['style_id']; + throw new \RuntimeException('No valid styles available'); } - $this->db->sql_freeresult($result); + // Reset users to default style if their user_style is nonexistent $sql = 'UPDATE ' . $this->table_prefix . "users - SET user_style = {$row['style_id']} + SET user_style = {$this->config['default_style']} WHERE " . $this->db->sql_in_set('user_style', $style_ids, true); $this->db->sql_query($sql); } -- cgit v1.2.1 From 74615364598115a584d01503a5dfcc234d8a42b0 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 1 Nov 2014 23:40:21 +0100 Subject: [ticket/13263] Make sure default style exists and clean up code PHPBB3-13263 --- .../phpbb/db/migration/data/v31x/style_update.php | 59 +++++----------------- 1 file changed, 12 insertions(+), 47 deletions(-) (limited to 'phpBB/phpbb/db') diff --git a/phpBB/phpbb/db/migration/data/v31x/style_update.php b/phpBB/phpbb/db/migration/data/v31x/style_update.php index 9f01514ff6..bb030bbe6d 100644 --- a/phpBB/phpbb/db/migration/data/v31x/style_update.php +++ b/phpBB/phpbb/db/migration/data/v31x/style_update.php @@ -52,24 +52,18 @@ class style_update extends \phpbb\db\migration\migration // Try to parse config file $cfg = parse_cfg_file($this->phpbb_root_path . 'styles/prosilver/style.cfg'); - // Stop running this if prosilver doesn't exist + // Stop running this if prosilver cfg file can't be read if (empty($cfg)) { throw new \RuntimeException('No styles available and could not fall back to prosilver.'); } - // Check data - if (!isset($cfg['template_bitfield'])) - { - $cfg['template_bitfield'] = $this->default_bitfield(); - } - $style = array( 'style_name' => 'prosilver', 'style_copyright' => '© phpBB Limited', 'style_active' => 1, 'style_path' => 'prosilver', - 'bbcode_bitfield' => $cfg['template_bitfield'], + 'bbcode_bitfield' => 'kNg=', 'style_parent_id' => 0, 'style_parent_tree' => '', ); @@ -81,61 +75,32 @@ class style_update extends \phpbb\db\migration\migration ' . $this->db->sql_build_array('INSERT', $style); $this->db->sql_query($sql); - $row = array('style_id' => $this->db->sql_nextid()); + $style_id = $this->db->sql_nextid(); + $style_ids[] = $style_id; $this->db->sql_transaction('commit'); // Set prosilver to default style - $this->config->set('default_style', $row['style_id']); + $this->config->set('default_style', $style_id); } else if (empty($styles) && empty($available_styles)) { throw new \RuntimeException('No valid styles available'); } + // Make sure default style is available + if (!in_array($this->config['default_style'], $style_ids)) + { + $this->config->set('default_style', array_pop($style_ids)); + } + // Reset users to default style if their user_style is nonexistent $sql = 'UPDATE ' . $this->table_prefix . "users SET user_style = {$this->config['default_style']} - WHERE " . $this->db->sql_in_set('user_style', $style_ids, true); + WHERE " . $this->db->sql_in_set('user_style', $style_ids, true, true); $this->db->sql_query($sql); } - /** - * Generates default bitfield - * Copied from acp_styles - * - * This bitfield decides which bbcodes are defined in a template. - * - * @return string Bitfield - */ - protected function default_bitfield() - { - static $value; - if (isset($value)) - { - return $value; - } - - if (!class_exists('bitfield')) - { - include($this->phpbb_root_path . 'includes/functions_content.' . $this->php_ext); - } - - // Hardcoded template bitfield to add for new templates - $bitfield = new \bitfield(); - $bitfield->set(0); - $bitfield->set(1); - $bitfield->set(2); - $bitfield->set(3); - $bitfield->set(4); - $bitfield->set(8); - $bitfield->set(9); - $bitfield->set(11); - $bitfield->set(12); - $value = $bitfield->get_base64(); - return $value; - } - /** * Find all directories that have styles * Copied from acp_styles -- cgit v1.2.1