Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
style_update
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 4
380
0.00% covered (danger)
0.00%
0 / 1
 depends_on
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 update_installed_styles
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
110
 find_style_dirs
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration\data\v31x;
15
16class style_update extends \phpbb\db\migration\migration
17{
18    public static function depends_on()
19    {
20        return array('\phpbb\db\migration\data\v310\gold');
21    }
22
23    public function update_data()
24    {
25        return array(
26            array('custom', array(array($this, 'update_installed_styles'))),
27        );
28    }
29
30    public function update_installed_styles()
31    {
32        // Get all currently available styles
33        $styles = $this->find_style_dirs();
34        $style_paths = $style_ids = array();
35
36        $sql = 'SELECT style_path, style_id
37                FROM ' . $this->table_prefix . 'styles';
38        $result = $this->db->sql_query($sql);
39        while ($styles_row = $this->db->sql_fetchrow())
40        {
41            if (in_array($styles_row['style_path'], $styles))
42            {
43                $style_paths[] = $styles_row['style_path'];
44                $style_ids[] = $styles_row['style_id'];
45            }
46        }
47        $this->db->sql_freeresult($result);
48
49        // Install prosilver if no style is available and prosilver can be installed
50        if (empty($style_paths) && in_array('prosilver', $styles))
51        {
52            // Try to parse config file
53            $cfg = parse_cfg_file($this->phpbb_root_path . 'styles/prosilver/style.cfg');
54
55            // Stop running this if both prosilver cfg file and composer.json file can't be read
56            if (empty($cfg) && !file_exists($this->phpbb_root_path . 'styles/prosilver/composer.json'))
57            {
58                throw new \RuntimeException('No styles available and could not fall back to prosilver.');
59            }
60
61            $style = array(
62                'style_name'        => 'prosilver',
63                'style_copyright'    => '&copy; phpBB Limited',
64                'style_active'        => 1,
65                'style_path'        => 'prosilver',
66                'bbcode_bitfield'    => 'kNg=',
67                'style_parent_id'    => 0,
68                'style_parent_tree'    => '',
69            );
70
71            // Add to database
72            $this->db->sql_transaction('begin');
73
74            $sql = 'INSERT INTO ' . $this->table_prefix . 'styles
75                    ' . $this->db->sql_build_array('INSERT', $style);
76            $this->db->sql_query($sql);
77
78            $style_id = $this->db->sql_nextid();
79            $style_ids[] = $style_id;
80
81            $this->db->sql_transaction('commit');
82
83            // Set prosilver to default style
84            $this->config->set('default_style', $style_id);
85        }
86        else if (empty($styles) && empty($style_paths))
87        {
88            throw new \RuntimeException('No valid styles available');
89        }
90
91        // Make sure default style is available
92        if (!in_array($this->config['default_style'], $style_ids))
93        {
94            $this->config->set('default_style', array_pop($style_ids));
95        }
96
97        // Reset users to default style if their user_style is nonexistent
98        $sql = 'UPDATE ' . $this->table_prefix . "users
99            SET user_style = {$this->config['default_style']}
100            WHERE " . $this->db->sql_in_set('user_style', $style_ids, true, true);
101        $this->db->sql_query($sql);
102    }
103
104    /**
105     * Find all directories that have styles
106     * Copied from acp_styles
107     *
108     * @return array Directory names
109     */
110    protected function find_style_dirs()
111    {
112        $styles = array();
113        $styles_path = $this->phpbb_root_path . 'styles/';
114
115        $dp = @opendir($styles_path);
116        if ($dp)
117        {
118            while (($file = readdir($dp)) !== false)
119            {
120                $dir = $styles_path . $file;
121                if ($file[0] == '.' || !is_dir($dir))
122                {
123                    continue;
124                }
125
126                if (file_exists("{$dir}/composer.json") || file_exists("{$dir}/style.cfg"))
127                {
128                    $styles[] = $file;
129                }
130            }
131            closedir($dp);
132        }
133
134        return $styles;
135    }
136}