functions.php ➔ isJson()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 6
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
if (! function_exists('getLangOptions')) {
3
    /**
4
     * @param string $install_language
5
     * @return string
6
     */
7
    function getLangOptions($install_language = 'english')
8
    {
9
        $langs = array();
10
        if ($handle = opendir(__DIR__ . '/lang/')) {
11
            while (false !== ($file = readdir($handle))) {
12
                if (strpos($file, '.')) {
13
                    $langs[] = str_replace('.inc.php', '', $file);
14
                }
15
            }
16
            closedir($handle);
17
        }
18
        sort($langs);
19
        $_ = array();
20
        foreach ($langs as $language) {
21
            $abrv_language = explode('-', $language);
22
            $selected = ($language === $install_language) ? 'selected' : '';
23
            $_[] = sprintf('<option value="%s" %s>%s</option>', $language, $selected,
24
                    ucwords($abrv_language[0])) . "\n";
25
        }
26
27
        return implode("\n", $_);
28
    }
29
}
30
31
if (! function_exists('install_sessionCheck')) {
32
    function install_sessionCheck()
33
    {
34
        global $_lang;
35
36
        // session loop-back tester
37
        if (!isset($_GET['action']) || $_GET['action'] !== 'mode') {
38
            if (!isset($_SESSION['test']) || $_SESSION['test'] != 1) {
39
                echo '
40
<html>
41
<head>
42
	<title>Install Problem</title>
43
	<style type="text/css">
44
		*{margin:0;padding:0}
45
		body{margin:150px;background:#eee;}
46
		.install{padding:10px;border:3px solid #ffc565;background:#ffddb4;margin:0 auto;text-align:center;}
47
		p{ margin:20px 0; }
48
		a{margin-top:30px;padding:5px;}
49
	</style>
50
</head>
51
<body>
52
	<div class="install">
53
		<p>' . $_lang["session_problem"] . '</p>
54
		<p><a href="./">' . $_lang["session_problem_try_again"] . '</a></p>
55
	</div>
56
</body>
57
</html>';
58
                exit;
59
            }
60
        }
61
    }
62
}
63
64 View Code Duplication
if (!function_exists('parse')) {
65
    /**
66
     * @param string $src
67
     * @param array $ph
68
     * @param string $left
69
     * @param string $right
70
     * @return string
71
     */
72
    function parse($src, $ph, $left = '[+', $right = '+]')
73
    {
74
        foreach ($ph as $k => $v) {
75
            $k = $left . $k . $right;
76
            $src = str_replace($k, $v, $src);
77
        }
78
79
        return $src;
80
    }
81
}
82
83
if (!function_exists('ph')) {
84
    /**
85
     * @return array
86
     */
87
    function ph()
88
    {
89
        global $_lang, $moduleName, $moduleVersion, $modx_textdir, $modx_release_date;
90
        $ph = array();
91
92
        if (isset($_SESSION['installmode'])) {
93
            $installmode = $_SESSION['installmode'];
94
        } else {
95
            $installmode = get_installmode();
96
        }
97
98
        $ph['pagetitle'] = $_lang['modx_install'];
99
        $ph['textdir'] = $modx_textdir ? ' id="rtl"' : '';
100
        $ph['help_link'] = $installmode == 0 ? $_lang['help_link_new'] : $_lang['help_link_upd'];
101
        $ph['version'] = $moduleVersion;
102
        $ph['release_date'] = ($modx_textdir ? '&rlm;' : '') . $modx_release_date;
103
        $ph['footer1'] = $_lang['modx_footer1'];
104
        $ph['footer2'] = $_lang['modx_footer2'];
105
        $ph['current_year'] = date('Y');
106
107
        return $ph;
108
    }
109
}
110
111
if (!function_exists('get_installmode')) {
112
    /**
113
     * @return int
114
     */
115
    function get_installmode()
116
    {
117
        global $base_path, $database_server, $database_user, $database_password, $dbase, $table_prefix;
118
119
        $conf_path = "{$base_path}manager/includes/config.inc.php";
120
        if (!is_file($conf_path)) {
121
            $installmode = 0;
122
        } elseif (isset($_POST['installmode'])) {
123
            $installmode = $_POST['installmode'];
124
        } else {
125
            include_once("{$base_path}manager/includes/config.inc.php");
126
127
            if (!isset($dbase) || empty($dbase)) {
128
                $installmode = 0;
129
            } else {
130
                $host = explode(':', $database_server, 2);
131
                $conn = mysqli_connect($host[0], $database_user, $database_password,'', isset($host[1]) ? $host[1] : null);
132
                if ($conn) {
133
                    $_SESSION['database_server'] = $database_server;
134
                    $_SESSION['database_user'] = $database_user;
135
                    $_SESSION['database_password'] = $database_password;
136
137
                    $dbase = trim($dbase, '`');
138
                    $rs = mysqli_select_db($conn, $dbase);
139
                } else {
140
                    $rs = false;
141
                }
142
143
                if ($rs) {
144
                    $_SESSION['dbase'] = $dbase;
145
                    $_SESSION['table_prefix'] = $table_prefix;
146
                    $_SESSION['database_collation'] = 'utf8mb4_general_ci';
147
                    $_SESSION['database_connection_method'] = 'SET CHARACTER SET';
148
149
                    $tbl_system_settings = "`{$dbase}`.`{$table_prefix}system_settings`";
150
                    $rs = mysqli_query($conn,
151
                        "SELECT setting_value FROM {$tbl_system_settings} WHERE setting_name='settings_version'");
152
                    if ($rs) {
153
                        $row = mysqli_fetch_assoc($rs);
154
                        $settings_version = $row['setting_value'];
155
                    } else {
156
                        $settings_version = '';
157
                    }
158
159
                    if (empty($settings_version)) {
160
                        $installmode = 0;
161
                    } else {
162
                        $installmode = 1;
163
                    }
164
                } else {
165
                    $installmode = 1;
166
                }
167
            }
168
        }
169
170
        return $installmode;
171
    }
172
}
173
174
if (!function_exists('getLangs')) {
175
    /**
176
     * @param string $install_language
177
     * @return string
178
     */
179
    function getLangs($install_language)
180
    {
181
        if ($install_language !== "english" &&
182
            is_file(sprintf("../%s/includes/lang/%s.inc.php", MGR_DIR, $install_language))
183
        ) {
184
            $manager_language = $install_language;
185
        } else {
186
            $manager_language = "english";
187
        }
188
        $langs = array();
189
        if ($handle = opendir(MODX_MANAGER_PATH . 'includes/lang')) {
190
            while (false !== ($file = readdir($handle))) {
191
                if (strpos($file, '.inc.') !== false) {
192
                    $langs[] = $file;
193
                }
194
            }
195
            closedir($handle);
196
        }
197
        sort($langs);
198
199
        $_ = array();
200
        foreach ($langs as $language) {
201
            $abrv_language = explode('.', $language);
202
            $selected = (strtolower($abrv_language[0]) == strtolower($manager_language)) ? ' selected' : '';
203
            $_[] = sprintf('<option value="%s" %s>%s</option>', $abrv_language[0], $selected,
204
                ucwords($abrv_language[0]));
205
        }
206
207
        return implode("\n", $_);
208
    }
209
}
210
211
if (!function_exists('sortItem')) {
212
    function sortItem($array = array(), $order = 'utf8mb4,utf8')
213
    {
214
        $rs = array('recommend' => '');
215
        $order = explode(',', $order);
216
        foreach ($order as $v) {
217
            foreach ($array as $name => $sel) {
218
                if (strpos($name, $v) !== false) {
219
                    $rs[$name] = $array[$name];
220
                    unset($array[$name]);
221
                }
222
            }
223
        }
224
        $rs['unrecommend'] = '';
225
226
        return $rs + $array;
227
    }
228
}
229
230
if (!function_exists('getTemplates')) {
231
    /**
232
     * @param array $presets
233
     * @return string
234
     */
235
    function getTemplates($presets = array())
236
    {
237
        if (empty($presets)) {
238
            return '';
239
        }
240
        $selectedTemplates = isset ($_POST['template']) ? $_POST['template'] : array();
241
        $tpl = '<label><input type="checkbox" name="template[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
242
        $_ = array();
243
        $i = 0;
244
        $ph = array();
245
        foreach ($presets as $preset) {
246
            $ph['i'] = $i;
247
            $ph['name'] = isset($preset[0]) ? $preset[0] : '';
248
            $ph['desc'] = isset($preset[1]) ? $preset[1] : '';
249
            $ph['class'] = !in_array('sample', $preset[6]) ? 'toggle' : 'toggle demo';
250
            $ph['checked'] = in_array($i, $selectedTemplates) || (!isset($_POST['options_selected'])) ? 'checked' : '';
251
            $_[] = parse($tpl, $ph);
252
            $i++;
253
        }
254
255
        return (0 < count($_)) ? '<h3>[%templates%]</h3>' . implode("\n", $_) : '';
256
    }
257
}
258
259
if (!function_exists('getTVs')) {
260
    /**
261
     * @param array $presets
262
     * @return string
263
     */
264
    function getTVs($presets = array())
265
    {
266
        if (empty($presets)) {
267
            return '';
268
        }
269
        $selectedTvs = isset ($_POST['tv']) ? $_POST['tv'] : array();
270
        $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 />';
271
        $_ = array();
272
        $i = 0;
273
        $ph = array();
274 View Code Duplication
        foreach ($presets as $preset) {
275
            $ph['i'] = $i;
276
            $ph['name'] = $preset[0];
277
            $ph['alterName'] = $preset[1];
278
            $ph['desc'] = $preset[2];
279
            $ph['class'] = !in_array('sample', $preset[12]) ? 'toggle' : 'toggle demo';
280
            $ph['checked'] = in_array($i, $selectedTvs) || (!isset($_POST['options_selected'])) ? 'checked' : '';
281
            $_[] = parse($tpl, $ph);
282
            $i++;
283
        }
284
285
        return (0 < count($_)) ? '<h3>[%tvs%]</h3>' . implode("\n", $_) : '';
286
    }
287
}
288
289 View Code Duplication
if (!function_exists('getChunks')) {
290
    /**
291
     * display chunks
292
     *
293
     * @param array $presets
294
     * @return string
295
     */
296
    function getChunks($presets = array())
297
    {
298
        if (empty($presets)) {
299
            return '';
300
        }
301
        $selected = isset ($_POST['chunk']) ? $_POST['chunk'] : array();
302
        $tpl = '<label><input type="checkbox" name="chunk[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
303
        $_ = array();
304
        $i = 0;
305
        $ph = array();
306
        foreach ($presets as $preset) {
307
            $ph['i'] = $i;
308
            $ph['name'] = $preset[0];
309
            $ph['desc'] = $preset[1];
310
            $ph['class'] = !in_array('sample', $preset[5]) ? 'toggle' : 'toggle demo';
311
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
312
            $_[] = parse($tpl, $ph);
313
            $i++;
314
        }
315
316
        return (0 < count($_)) ? '<h3>[%chunks%]</h3>' . implode("\n", $_) : '';
317
    }
318
}
319
320 View Code Duplication
if (!function_exists('getModules')) {
321
    /**
322
     * display modules
323
     *
324
     * @param array $presets
325
     * @return string
326
     */
327
    function getModules($presets = array())
328
    {
329
        if (empty($presets)) {
330
            return '';
331
        }
332
        $selected = isset ($_POST['module']) ? $_POST['module'] : array();
333
        $tpl = '<label><input type="checkbox" name="module[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
334
        $_ = array();
335
        $i = 0;
336
        $ph = array();
337
        foreach ($presets as $preset) {
338
            $ph['i'] = $i;
339
            $ph['name'] = $preset[0];
340
            $ph['desc'] = $preset[1];
341
            $ph['class'] = !in_array('sample', $preset[7]) ? 'toggle' : 'toggle demo';
342
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
343
            $_[] = parse($tpl, $ph);
344
            $i++;
345
        }
346
347
        return (0 < count($_)) ? '<h3>[%modules%]</h3>' . implode("\n", $_) : '';
348
    }
349
}
350
351
if (!function_exists('getPlugins')) {
352
    /**
353
     * display plugins
354
     *
355
     * @param array $presets
356
     * @return string
357
     */
358
    function getPlugins($presets = array())
359
    {
360
        if (!count($presets)) {
361
            return '';
362
        }
363
        $selected = isset ($_POST['plugin']) ? $_POST['plugin'] : array();
364
        $tpl = '<label><input type="checkbox" name="plugin[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
365
        $_ = array();
366
        $i = 0;
367
        $ph = array();
368 View Code Duplication
        foreach ($presets as $preset) {
369
            $ph['i'] = $i;
370
            $ph['name'] = $preset[0];
371
            $ph['desc'] = $preset[1];
372
            if (is_array($preset[8])) {
373
                $ph['class'] = !in_array('sample', $preset[8]) ? 'toggle' : 'toggle demo';
374
            } else {
375
                $ph['class'] = 'toggle demo';
376
            }
377
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
378
            $_[] = parse($tpl, $ph);
379
            $i++;
380
        }
381
382
        return (0 < count($_)) ? '<h3>[%plugins%]</h3>' . implode("\n", $_) : '';
383
    }
384
}
385
386 View Code Duplication
if (!function_exists('getSnippets')) {
387
    /**
388
     * display snippets
389
     *
390
     * @param array $presets
391
     * @return string
392
     */
393
    function getSnippets($presets = array())
394
    {
395
        if (!count($presets)) {
396
            return '';
397
        }
398
        $selected = isset ($_POST['snippet']) ? $_POST['snippet'] : array();
399
        $tpl = '<label><input type="checkbox" name="snippet[]" value="[+i+]" class="[+class+]" [+checked+] />[%install_update%] <span class="comname">[+name+]</span> - [+desc+]</label><hr />';
400
        $_ = array();
401
        $i = 0;
402
        $ph = array();
403
        foreach ($presets as $preset) {
404
            $ph['i'] = $i;
405
            $ph['name'] = $preset[0];
406
            $ph['desc'] = $preset[1];
407
            $ph['class'] = !in_array('sample', $preset[5]) ? 'toggle' : 'toggle demo';
408
            $ph['checked'] = in_array($i, $selected) || (!isset($_POST['options_selected'])) ? 'checked' : '';
409
            $_[] = parse($tpl, $ph);
410
            $i++;
411
        }
412
413
        return (0 < count($_)) ? '<h3>[%snippets%]</h3>' . implode("\n", $_) : '';
414
    }
415
}
416
417
if (!function_exists('clean_up')) {
418
    function clean_up($sqlParser)
419
    {
420
        $ids = array();
421
422
        // secure web documents - privateweb
423
        mysqli_query($sqlParser->conn,
424
            "UPDATE `" . $sqlParser->prefix . "site_content` SET privateweb = 0 WHERE privateweb = 1");
425
        $sql = "SELECT DISTINCT sc.id
426
             FROM `" . $sqlParser->prefix . "site_content` sc
427
             LEFT JOIN `" . $sqlParser->prefix . "document_groups` dg ON dg.document = sc.id
428
             LEFT JOIN `" . $sqlParser->prefix . "webgroup_access` wga ON wga.documentgroup = dg.document_group
429
             WHERE wga.id>0";
430
        $ds = mysqli_query($sqlParser->conn, $sql);
431 View Code Duplication
        if (!$ds) {
432
            echo "An error occurred while executing a query: " . mysqli_error($sqlParser->conn);
433
        } else {
434
            while ($r = mysqli_fetch_assoc($ds)) {
435
                $ids[] = $r["id"];
436
            }
437
            if (count($ids) > 0) {
438
                mysqli_query($sqlParser->conn,
439
                    "UPDATE `" . $sqlParser->prefix . "site_content` SET privateweb = 1 WHERE id IN (" . implode(", ",
440
                        $ids) . ")");
441
                unset($ids);
442
            }
443
        }
444
445
        // secure manager documents privatemgr
446
        mysqli_query($sqlParser->conn,
447
            "UPDATE `" . $sqlParser->prefix . "site_content` SET privatemgr = 0 WHERE privatemgr = 1");
448
        $sql = "SELECT DISTINCT sc.id
449
             FROM `" . $sqlParser->prefix . "site_content` sc
450
             LEFT JOIN `" . $sqlParser->prefix . "document_groups` dg ON dg.document = sc.id
451
             LEFT JOIN `" . $sqlParser->prefix . "membergroup_access` mga ON mga.documentgroup = dg.document_group
452
             WHERE mga.id>0";
453
        $ds = mysqli_query($sqlParser->conn, $sql);
454 View Code Duplication
        if (!$ds) {
455
            echo "An error occurred while executing a query: " . mysqli_error($sqlParser->conn);
456
        } else {
457
            while ($r = mysqli_fetch_assoc($ds)) {
458
                $ids[] = $r["id"];
459
            }
460
            if (count($ids) > 0) {
461
                mysqli_query($sqlParser->conn,
462
                    "UPDATE `" . $sqlParser->prefix . "site_content` SET privatemgr = 1 WHERE id IN (" . implode(", ",
463
                        $ids) . ")");
464
                unset($ids);
465
            }
466
        }
467
    }
468
}
469
470
if (!function_exists('parse_docblock')) {
471
    function parse_docblock($element_dir, $filename)
472
    {
473
        $params = array();
474
        $fullpath = $element_dir . '/' . $filename;
475
        if (is_readable($fullpath)) {
476
            $tpl = @fopen($fullpath, "r");
477
            if ($tpl) {
478
                $params['filename'] = $filename;
479
                $docblock_start_found = false;
480
                $name_found = false;
481
                $description_found = false;
482
483
                while (!feof($tpl)) {
484
                    $line = fgets($tpl);
485
                    if (!$docblock_start_found) {
486
                        // find docblock start
487
                        if (strpos($line, '/**') !== false) {
488
                            $docblock_start_found = true;
489
                        }
490
                        continue;
491 View Code Duplication
                    } elseif (!$name_found) {
492
                        // find name
493
                        $ma = null;
494
                        if (preg_match("/^\s+\*\s+(.+)/", $line, $ma)) {
495
                            $params['name'] = trim($ma[1]);
496
                            $name_found = !empty($params['name']);
497
                        }
498
                        continue;
499
                    } elseif (!$description_found) {
500
                        // find description
501
                        $ma = null;
502
                        if (preg_match("/^\s+\*\s+(.+)/", $line, $ma)) {
503
                            $params['description'] = trim($ma[1]);
504
                            $description_found = !empty($params['description']);
505
                        }
506
                        continue;
507
                    } else {
508
                        $ma = null;
509
                        if (preg_match("/^\s+\*\s+\@([^\s]+)\s+(.+)/", $line, $ma)) {
510
                            $param = trim($ma[1]);
511
                            $val = trim($ma[2]);
512
                            if (!empty($param) && !empty($val)) {
513 View Code Duplication
                                if ($param == 'internal') {
514
                                    $ma = null;
515
                                    if (preg_match("/\@([^\s]+)\s+(.+)/", $val, $ma)) {
516
                                        $param = trim($ma[1]);
517
                                        $val = trim($ma[2]);
518
                                    }
519
                                    //if($val !== '0' && (empty($param) || empty($val))) {
520
                                    if (empty($param)) {
521
                                        continue;
522
                                    }
523
                                }
524
                                $params[$param] = $val;
525
                            }
526
                        } elseif (preg_match("/^\s*\*\/\s*$/", $line)) {
527
                            break;
528
                        }
529
                    }
530
                }
531
                @fclose($tpl);
532
            }
533
        }
534
535
        return $params;
536
    }
537
}
538
539 View Code Duplication
if (!function_exists('propertiesNameValue')) {
540
    /**
541
     * parses a resource property string and returns the result as an array
542
     * duplicate of method in documentParser class
543
     *
544
     * @param string $propertyString
545
     * @return array
546
     */
547
    function propertiesNameValue($propertyString)
0 ignored issues
show
Best Practice introduced by
The function propertiesNameValue() has been defined more than once; this definition is ignored, only the first definition in install/cli-install.php (L544-567) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
548
    {
549
        $parameter = array();
550
        if (!empty ($propertyString)) {
551
            $tmpParams = explode("&", $propertyString);
552
            $countParams = count($tmpParams);
553
            for ($x = 0; $x < $countParams; $x++) {
554
                if (strpos($tmpParams[$x], '=', 0)) {
555
                    $pTmp = explode("=", $tmpParams[$x]);
556
                    $pvTmp = explode(";", trim($pTmp[1]));
557
                    if ($pvTmp[1] == 'list' && $pvTmp[3] != "") {
558
                        $parameter[trim($pTmp[0])] = $pvTmp[3];
559
                    } //list default
560
                    else {
561
                        if ($pvTmp[1] != 'list' && $pvTmp[2] != "") {
562
                            $parameter[trim($pTmp[0])] = $pvTmp[2];
563
                        }
564
                    }
565
                }
566
            }
567
        }
568
569
        return $parameter;
570
    }
571
}
572
573
if (!function_exists('propUpdate')) {
574
    /**
575
     * Property Update function
576
     *
577
     * @param string $new
578
     * @param string $old
579
     * @return string
580
     */
581
    function propUpdate($new, $old)
582
    {
583
        $newArr = parseProperties($new);
584
        $oldArr = parseProperties($old);
585
        foreach ($oldArr as $k => $v) {
0 ignored issues
show
Bug introduced by
The expression $oldArr of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
586
            if (isset($v['0']['options'])) {
587
                $oldArr[$k]['0']['options'] = $newArr[$k]['0']['options'];
588
            }
589
        }
590
        $return = $oldArr + $newArr;
591
        $return = json_encode($return, JSON_UNESCAPED_UNICODE);
592
        $return = ($return !== '[]') ? $return : '';
593
594
        return $return;
595
    }
596
}
597
598
if (!function_exists('parseProperties')) {
599
    /**
600
     * @param string $propertyString
601
     * @param bool|mixed $json
602
     * @return string|array
603
     */
604
    function parseProperties($propertyString, $json = false)
605
    {
606
        $propertyString = str_replace('{}', '', $propertyString);
607
        $propertyString = str_replace('} {', ',', $propertyString);
608
609
        if (empty($propertyString) || $propertyString == '{}' || $propertyString == '[]') {
610
            $propertyString = '';
611
        }
612
613
        $jsonFormat = isJson($propertyString, true);
614
        $property = array();
615
        // old format
616
        if ($jsonFormat === false) {
617
            $props = explode('&', $propertyString);
618
            foreach ($props as $prop) {
619
                $prop = trim($prop);
620
                if ($prop === '') {
621
                    continue;
622
                }
623
624
                $arr = explode(';', $prop);
625
                if (!is_array($arr)) {
626
                    $arr = array();
627
                }
628
                $key = explode('=', isset($arr[0]) ? $arr[0] : '');
629
                if (!is_array($key) || empty($key[0])) {
630
                    continue;
631
                }
632
633
                $property[$key[0]]['0']['label'] = isset($key[1]) ? trim($key[1]) : '';
634
                $property[$key[0]]['0']['type'] = isset($arr[1]) ? trim($arr[1]) : '';
635
                switch ($property[$key[0]]['0']['type']) {
636
                    case 'list':
637
                    case 'list-multi':
638
                    case 'checkbox':
639
                    case 'radio':
640
                    case 'menu':
641
                        $property[$key[0]]['0']['value'] = isset($arr[3]) ? trim($arr[3]) : '';
642
                        $property[$key[0]]['0']['options'] = isset($arr[2]) ? trim($arr[2]) : '';
643
                        $property[$key[0]]['0']['default'] = isset($arr[3]) ? trim($arr[3]) : '';
644
                        break;
645
                    default:
646
                        $property[$key[0]]['0']['value'] = isset($arr[2]) ? trim($arr[2]) : '';
647
                        $property[$key[0]]['0']['default'] = isset($arr[2]) ? trim($arr[2]) : '';
648
                }
649
                $property[$key[0]]['0']['desc'] = '';
650
651
            }
652
            // new json-format
653
        } else {
654
            if (!empty($jsonFormat)) {
655
                $property = $jsonFormat;
656
            }
657
        }
658
659
        if ($json) {
660
            $property = json_encode($property, JSON_UNESCAPED_UNICODE);
661
        }
662
        $property = ($property !== '[]') ? $property : '';
663
664
        return $property;
665
    }
666
}
667
668 View Code Duplication
if (!function_exists('isJson')) {
669
    /**
670
     * @param string $string
671
     * @param bool $returnData
672
     * @return bool|mixed
673
     */
674
    function isJson($string, $returnData = false)
675
    {
676
        $data = json_decode($string, true);
677
678
        return (json_last_error() == JSON_ERROR_NONE) ? ($returnData ? $data : true) : false;
679
    }
680
}
681
682
if (!function_exists('getCreateDbCategory')) {
683
    /**
684
     * @param string|int $category
685
     * @param SqlParser $sqlParser
686
     * @return int
687
     */
688
    function getCreateDbCategory($category, $sqlParser)
689
    {
690
        $dbase = $sqlParser->dbname;
691
        $dbase = '`' . trim($dbase, '`') . '`';
692
        $table_prefix = $sqlParser->prefix;
693
        $category_id = 0;
694
        if (!empty($category)) {
695
            $category = mysqli_real_escape_string($sqlParser->conn, $category);
696
            $rs = mysqli_query($sqlParser->conn,
697
                "SELECT id FROM $dbase.`" . $table_prefix . "categories` WHERE category = '" . $category . "'");
698 View Code Duplication
            if (mysqli_num_rows($rs) && ($row = mysqli_fetch_assoc($rs))) {
699
                $category_id = $row['id'];
700
            } else {
701
                $q = "INSERT INTO $dbase.`" . $table_prefix . "categories` (`category`) VALUES ('{$category}');";
702
                $rs = mysqli_query($sqlParser->conn, $q);
703
                if ($rs) {
704
                    $category_id = mysqli_insert_id($sqlParser->conn);
705
                }
706
            }
707
        }
708
709
        return $category_id;
710
    }
711
}
712
713
if (!function_exists('removeDocblock')) {
714
    /**
715
     * Remove installer Docblock only from components using plugin FileSource / fileBinding
716
     *
717
     * @param string $code
718
     * @param string $type
719
     * @return string
720
     */
721
    function removeDocblock($code, $type)
722
    {
723
724
        $cleaned = preg_replace("/^.*?\/\*\*.*?\*\/\s+/s", '', $code, 1);
725
726
        // Procedure taken from plugin.filesource.php
727
        switch ($type) {
728
            case 'snippet':
729
                $elm_name = 'snippets';
730
                $include = 'return require';
731
                $count = 47;
732
                break;
733
734
            case 'plugin':
735
                $elm_name = 'plugins';
736
                $include = 'require';
737
                $count = 39;
738
                break;
739
740
            default:
741
                return $cleaned;
742
        };
743
        if (substr(trim($cleaned), 0, $count) == $include . ' MODX_BASE_PATH.\'assets/' . $elm_name . '/') {
744
            return $cleaned;
745
        }
746
747
        // fileBinding not found - return code incl docblock
748
        return $code;
749
    }
750
}
751
752
if (!function_exists('removeFolder')) {
753
    /**
754
     * RemoveFolder
755
     *
756
     * @param string $path
757
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
758
     */
759
    function removeFolder($path)
760
    {
761
        $dir = realpath($path);
762
        if (!is_dir($dir)) {
763
            return;
764
        }
765
766
        $it = new RecursiveDirectoryIterator($dir);
767
        $files = new RecursiveIteratorIterator($it,
768
            RecursiveIteratorIterator::CHILD_FIRST);
769
        foreach ($files as $file) {
770
            if ($file->getFilename() === "." || $file->getFilename() === "..") {
771
                continue;
772
            }
773
            if ($file->isDir()) {
774
                rmdir($file->getRealPath());
775
            } else {
776
                unlink($file->getRealPath());
777
            }
778
        }
779
        rmdir($dir);
780
    }
781
}
782