Completed
Pull Request — develop (#537)
by Agel_Nash
05:20
created

action_options.php ➔ getSnippets()   B

Complexity

Conditions 8
Paths 37

Size

Total Lines 21
Code Lines 17

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 37
nop 1
dl 21
loc 21
rs 7.1428
c 0
b 0
f 0
1
<?php
2
$installMode = isset($_POST['installmode']) ? (int)$_POST['installmode'] : 0;
3
4
if( ! function_exists('getTemplates')) {
5
    /**
6
     * @param array $presets
7
     * @return string
8
     */
9
    function getTemplates($presets = array())
10
    {
11
        if (empty($presets)) {
12
            return '';
13
        }
14
        $selectedTemplates = isset ($_POST['template']) ? $_POST['template'] : array();
15
        $tpl = '<label><input type="checkbox" name="template[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
16
        $_ = array();
17
        $i = 0;
18
        $ph = array();
19
        foreach ($presets as $preset) {
20
            $ph['i'] = $i;
21
            $ph['name'] = isset($preset[0]) ? $preset[0] : '';
22
            $ph['desc'] = isset($preset[1]) ? $preset[1] : '';
23
            $ph['class'] = !in_array('sample', $preset[6]) ? 'toggle' : 'toggle demo';
24
            $ph['checked'] = in_array($i, $selectedTemplates) || (!isset($_POST['options_selected'])) ? 'checked' : '';
25
            $_[] = parse($tpl, $ph);
26
            $i++;
27
        }
28
        return (0 < count($_)) ? '<h3>[%templates%]</h3>' . implode("\n", $_) : '';
29
    }
30
}
31
32
if( ! function_exists('getTVs')) {
33
    /**
34
     * @param array $presets
35
     * @return string
36
     */
37
    function getTVs($presets = array())
38
    {
39
        if (empty($presets)) {
40
            return '';
41
        }
42
        $selectedTvs = isset ($_POST['tv']) ? $_POST['tv'] : array();
43
        $tpl = '<label><input type="checkbox" name="tv[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+alterName+] <span class="description">([+desc+])</span></label><hr />';
44
        $_ = array();
45
        $i = 0;
46
        $ph = array();
47
        foreach ($presets as $preset) {
48
            $ph['i'] = $i;
49
            $ph['name'] = $preset[0];
50
            $ph['alterName'] = $preset[1];
51
            $ph['desc'] = $preset[2];
52
            $ph['class'] = !in_array('sample', $preset[12]) ? 'toggle' : 'toggle demo';
53
            $ph['checked'] = in_array($i, $selectedTvs) || (!isset($_POST['options_selected'])) ? 'checked' : '';
54
            $_[] = parse($tpl, $ph);
55
            $i++;
56
        }
57
        return (0 < count($_)) ? '<h3>[%tvs%]</h3>' . implode("\n", $_) : '';
58
    }
59
}
60
61 View Code Duplication
if( ! function_exists('getChunks')) {
62
    /**
63
     * display chunks
64
     *
65
     * @param array $presets
66
     * @return string
67
     */
68
    function getChunks($presets = array())
69
    {
70
        if (empty($presets)) {
71
            return '';
72
        }
73
        $selected = isset ($_POST['chunk']) ? $_POST['chunk'] : array();
74
        $tpl = '<label><input type="checkbox" name="chunk[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
75
        $_ = array();
76
        $i = 0;
77
        $ph = array();
78
        foreach ($presets as $preset) {
79
            $ph['i'] = $i;
80
            $ph['name'] = $preset[0];
81
            $ph['desc'] = $preset[1];
82
            $ph['class'] = !in_array('sample', $preset[5]) ? 'toggle' : 'toggle demo';
83
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
84
            $_[] = parse($tpl, $ph);
85
            $i++;
86
        }
87
        return (0 < count($_)) ? '<h3>[%chunks%]</h3>' . implode("\n", $_) : '';
88
    }
89
}
90
91 View Code Duplication
if( ! function_exists('getModules')) {
92
    /**
93
     * display modules
94
     *
95
     * @param array $presets
96
     * @return string
97
     */
98
    function getModules($presets = array())
99
    {
100
        if (empty($presets)) {
101
            return '';
102
        }
103
        $selected = isset ($_POST['module']) ? $_POST['module'] : array();
104
        $tpl = '<label><input type="checkbox" name="module[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
105
        $_ = array();
106
        $i = 0;
107
        $ph = array();
108
        foreach ($presets as $preset) {
109
            $ph['i'] = $i;
110
            $ph['name'] = $preset[0];
111
            $ph['desc'] = $preset[1];
112
            $ph['class'] = !in_array('sample', $preset[7]) ? 'toggle' : 'toggle demo';
113
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
114
            $_[] = parse($tpl, $ph);
115
            $i++;
116
        }
117
        return (0 < count($_)) ? '<h3>[%modules%]</h3>' . implode("\n", $_) : '';
118
    }
119
}
120
121
if( ! function_exists('getPlugins')) {
122
    /**
123
     * display plugins
124
     *
125
     * @param array $presets
126
     * @return string
127
     */
128
    function getPlugins($presets = array())
129
    {
130
        if (!count($presets)) {
131
            return '';
132
        }
133
        $selected = isset ($_POST['plugin']) ? $_POST['plugin'] : array();
134
        $tpl = '<label><input type="checkbox" name="plugin[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
135
        $_ = array();
136
        $i = 0;
137
        $ph = array();
138
        foreach ($presets as $preset) {
139
            $ph['i'] = $i;
140
            $ph['name'] = $preset[0];
141
            $ph['desc'] = $preset[1];
142
            if (is_array($preset[8])) {
143
                $ph['class'] = !in_array('sample', $preset[8]) ? 'toggle' : 'toggle demo';
144
            } else {
145
                $ph['class'] = 'toggle demo';
146
            }
147
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
148
            $_[] = parse($tpl, $ph);
149
            $i++;
150
        }
151
        return (0 < count($_)) ? '<h3>[%plugins%]</h3>' . implode("\n", $_) : '';
152
    }
153
}
154
155 View Code Duplication
if( ! function_exists('getSnippets')) {
156
    /**
157
     * display snippets
158
     *
159
     * @param array $presets
160
     * @return string
161
     */
162
    function getSnippets($presets = array())
163
    {
164
        if (!count($presets)) {
165
            return '';
166
        }
167
        $selected = isset ($_POST['snippet']) ? $_POST['snippet'] : array();
168
        $tpl = '<label><input type="checkbox" name="snippet[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
169
        $_ = array();
170
        $i = 0;
171
        $ph = array();
172
        foreach ($presets as $preset) {
173
            $ph['i'] = $i;
174
            $ph['name'] = $preset[0];
175
            $ph['desc'] = $preset[1];
176
            $ph['class'] = !in_array('sample', $preset[5]) ? 'toggle' : 'toggle demo';
177
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
178
            $_[] = parse($tpl, $ph);
179
            $i++;
180
        }
181
        return (0 < count($_)) ? '<h3>[%snippets%]</h3>' . implode("\n", $_) : '';
182
    }
183
}
184
185
switch($installMode){
186
    case 0:
187
    case 2:
188
        $database_collation = isset($_POST['database_collation']) ? $_POST['database_collation'] : 'utf8_general_ci';
189
        $database_charset = substr($database_collation, 0, strpos($database_collation, '_'));
190
        $_POST['database_connection_charset'] = $database_charset;
191
        if(empty($_SESSION['databaseloginpassword']))
192
            $_SESSION['databaseloginpassword'] = $_POST['databaseloginpassword'];
193
        if(empty($_SESSION['databaseloginname']))
194
            $_SESSION['databaseloginname'] = $_POST['databaseloginname'];
195
        break;
196
    case 1:
197
        include $base_path . MGR_DIR . '/includes/config.inc.php';
198
        if (@ $conn = mysqli_connect($database_server, $database_user, $database_password)) {
199
            if (@ mysqli_query($conn, "USE {$dbase}")) {
200
                if (!$rs = mysqli_query($conn, "show session variables like 'collation_database'")) {
201
                    $rs = mysqli_query($conn, "show session variables like 'collation_server'");
202
                }
203
                if ($rs && $collation = mysqli_fetch_row($rs)) {
204
                    $database_collation = trim($collation[1]);
205
                }
206
            }
207
        }
208
        if (empty ($database_collation)) $database_collation = 'utf8_general_ci';
209
210
        $database_charset = substr($database_collation, 0, strpos($database_collation, '_'));
211
        if (!isset ($database_connection_charset) || empty ($database_connection_charset)) {
212
            $database_connection_charset = $database_charset;
213
        }
214
215
        if (!isset ($database_connection_method) || empty ($database_connection_method)) {
216
            $database_connection_method = 'SET CHARACTER SET';
217
            if (function_exists('mysqli_set_charset')) mysqli_set_charset($conn, $database_connection_charset);
218
        }
219
        if ($database_connection_method != 'SET NAMES' && $database_connection_charset != $database_charset) {
220
            $database_connection_method = 'SET NAMES';
221
        }
222
223
        $_POST['database_name'] = $dbase;
224
        $_POST['tableprefix'] = $table_prefix;
225
        $_POST['database_connection_charset'] = $database_connection_charset;
226
        $_POST['database_connection_method'] = $database_connection_method;
227
        $_POST['databasehost'] = $database_server;
228
        $_SESSION['databaseloginname'] = $database_user;
229
        $_SESSION['databaseloginpassword'] = $database_password;
230
        break;
231
    default:
232
        throw new Exception('installmode is undefined');
233
}
234
235
$ph['install_language'] = $install_language;
236
$ph['manager_language'] = $manager_language;
237
$ph['installMode'] = $installMode;
238
$ph['database_name'] = trim($_POST['database_name'], '`');
239
$ph['tableprefix'] = $_POST['tableprefix'];
240
$ph['database_collation'] = $_POST['database_collation'];
241
$ph['database_connection_charset'] = $_POST['database_connection_charset'];
242
$ph['database_connection_method'] = $_POST['database_connection_method'];
243
$ph['databasehost'] = $_POST['databasehost'];
244
$ph['cmsadmin'] = trim($_POST['cmsadmin']);
245
$ph['cmsadminemail'] = trim($_POST['cmsadminemail']);
246
$ph['cmspassword'] = trim($_POST['cmspassword']);
247
$ph['cmspasswordconfirm'] = trim($_POST['cmspasswordconfirm']);
248
249
$ph['checked'] = isset ($_POST['installdata']) && $_POST['installdata'] == "1" ? 'checked' : '';
250
251
# load setup information file
252
include($base_path . 'install/setup.info.php');
253
$ph['templates'] = getTemplates($moduleTemplates);
254
$ph['tvs']       = getTVs($moduleTVs);
255
$ph['chunks']    = getChunks($moduleChunks);
256
$ph['modules']   = getModules($moduleModules);
257
$ph['plugins']   = getPlugins($modulePlugins);
258
$ph['snippets']  = getSnippets($moduleSnippets);
259
260
$ph['action'] = ($installMode == 1) ? 'mode' : 'connection';
261
262
$tpl = file_get_contents($base_path . 'install/actions/tpl_options.html');
263
$content = parse($tpl,$ph);
264
echo parse($content,$_lang,'[%','%]');
265