Issues (2407)

administration/model/setting/setting.php (3 issues)

1
<?php
2
3
/* 	Divine CMS - Open source CMS for widespread use.
4
    Copyright (c) 2019 Mykola Burakov ([email protected])
5
6
    See SOURCE.txt for other and additional information.
7
8
    This file is part of Divine CMS.
9
10
    This program is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
15
    This program is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
    GNU General Public License for more details.
19
20
    You should have received a copy of the GNU General Public License
21
    along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23
class ModelSettingSetting extends \Divine\Engine\Core\Model
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    public function getSetting($code)
0 ignored issues
show
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        $setting_data = array();
28
29
        $query = $this->db->query("
30
            SELECT * 
31
            FROM setting 
32
            WHERE `code` = '" . $this->db->escape($code) . "'
33
        ");
34
35
        foreach ($query->rows as $result) {
36
            if (!$result['serialized']) {
37
                $setting_data[$result['key']] = $result['value'];
38
            } else {
39
                $setting_data[$result['key']] = json_decode($result['value'], true);
40
            }
41
        }
42
43
        return $setting_data;
44
    }
45
46
    public function editSetting($code, $data)
47
    {
48
        $this->db->query("
49
            DELETE 
50
            FROM `setting` 
51
            WHERE `code` = '" . $this->db->escape($code) . "'
52
        ");
53
54
        foreach ($data as $key => $value) {
55
            if (substr($key, 0, strlen($code)) == $code) {
56
                if (!is_array($value)) {
57
                    $this->db->query("
58
                        INSERT INTO setting 
59
                        SET `code` = '" . $this->db->escape($code) . "', 
60
                            `key` = '" . $this->db->escape($key) . "', 
61
                            `value` = '" . $this->db->escape($value) . "'
62
                    ");
63
                } else {
64
                    $this->db->query("
65
                        INSERT INTO setting 
66
                        SET `code` = '" . $this->db->escape($code) . "', 
67
                            `key` = '" . $this->db->escape($key) . "', 
68
                            `value` = '" . $this->db->escape(json_encode($value, true)) . "', 
0 ignored issues
show
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
                            `value` = '" . $this->db->escape(json_encode($value, /** @scrutinizer ignore-type */ true)) . "', 
Loading history...
69
                            serialized = '1'
70
                    ");
71
                }
72
            }
73
        }
74
    }
75
76
    public function deleteSetting($code)
77
    {
78
        $this->db->query("
79
            DELETE 
80
            FROM setting 
81
            WHERE `code` = '" . $this->db->escape($code) . "'
82
        ");
83
    }
84
85
    public function getSettingValue($key)
86
    {
87
        $query = $this->db->query("
88
            SELECT value 
89
            FROM setting 
90
            WHERE `key` = '" . $this->db->escape($key) . "'
91
        ");
92
93
        if ($query->num_rows) {
94
            return $query->row['value'];
95
        } else {
96
            return null;
97
        }
98
    }
99
100
    public function editSettingValue($code = '', $key = '', $value = '')
101
    {
102
        if (!is_array($value)) {
103
            $this->db->query("
104
                UPDATE setting 
105
                SET `value` = '" . $this->db->escape($value) . "', 
106
                    serialized = '0' 
107
                WHERE `code` = '" . $this->db->escape($code) . "' 
108
                    AND `key` = '" . $this->db->escape($key) . "' 
109
            ");
110
        } else {
111
            $this->db->query("
112
                UPDATE setting 
113
                SET `value` = '" . $this->db->escape(json_encode($value)) . "', 
114
                    serialized = '1' 
115
                WHERE `code` = '" . $this->db->escape($code) . "' 
116
                    AND `key` = '" . $this->db->escape($key) . "'
117
            ");
118
        }
119
    }
120
}
121