Issues (2407)

application/controller/startup/startup.php (4 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 ControllerStartupStartup extends \Divine\Engine\Core\Controller
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 index()
0 ignored issues
show
Expected 2 blank lines before function; 0 found
Loading history...
26
    {
27
        // Store
28
        $this->config->set(
29
            'config_url',
30
            'https://' . $_SERVER['HTTP_HOST'] . '/'
31
        );
32
33
        // Settings
34
        $query = $this->db->query("
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal \n SELECT * \...ROM `setting`\n does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
35
            SELECT * 
36
            FROM `setting`
37
        ");
38
39
        foreach ($query->rows as $result) {
40
            if (!$result['serialized']) {
41
                $this->config->set(
42
                    $result['key'],
43
                    $result['value']
44
                );
45
            } else {
46
                $this->config->set(
47
                    $result['key'],
48
                    json_decode($result['value'], true)
49
                );
50
            }
51
        }
52
53
        // Url
54
        $this->registry->set(
55
            'url',
56
            new \Divine\Engine\Library\Url(
57
                $this->config->get('config_url')
58
            )
59
        );
60
61
        // Language
62
        $code = '';
63
64
        $this->load->model('localisation/language');
65
66
        $languages = $this->model_localisation_language->getLanguages();
67
68
        if (isset($this->session->data['language'])) {
69
            $code = $this->session->data['language'];
70
        }
71
72
        if (isset($this->request->cookie['language']) && !array_key_exists($code, $languages)) {
73
            $code = $this->request->cookie['language'];
74
        }
75
76
        // Language Detection
77
        if (!empty($this->request->server['HTTP_ACCEPT_LANGUAGE']) && !array_key_exists($code, $languages)) {
78
            $detect = '';
79
80
            $browser_languages = explode(',', $this->request->server['HTTP_ACCEPT_LANGUAGE']);
81
82
            // Try using local to detect the language
83
            foreach ($browser_languages as $browser_language) {
84
                foreach ($languages as $key => $value) {
85
                    if ($value['status']) {
86
                        $locale = explode(',', $value['locale']);
87
88
                        if (in_array($browser_language, $locale)) {
89
                            $detect = $key;
90
                            break 2;
91
                        }
92
                    }
93
                }
94
            }
95
96
            if (!$detect) {
97
                // Try using language folder to detect the language
98
                foreach ($browser_languages as $browser_language) {
99
                    if (array_key_exists(strtolower($browser_language), $languages)) {
100
                        $detect = strtolower($browser_language);
101
102
                        break;
103
                    }
104
                }
105
            }
106
107
            $code = $detect ? $detect : '';
108
        }
109
110
        if (!array_key_exists($code, $languages)) {
111
            $code = $this->config->get('config_language');
112
        }
113
114
        if (!isset($this->session->data['language']) || $this->session->data['language'] != $code) {
115
            $this->session->data['language'] = $code;
116
        }
117
118
        if (!isset($this->request->cookie['language']) || $this->request->cookie['language'] != $code) {
119
            setcookie(
120
                'language',
121
                $code,
122
                time() + 60 * 60 * 24 * 30,
123
                '/',
124
                $this->request->server['HTTP_HOST']
125
            );
126
        }
127
128
        // Overwrite the default language object
129
        $language = new \Divine\Engine\Library\Language($code);
130
        $language->load($code);
131
132
        $this->registry->set(
133
            'language',
134
            $language
135
        );
136
137
        // Set the config language_id
138
        $this->config->set(
139
            'config_language_id',
140
            $languages[$code]['language_id']
141
        );
142
143
        // Customer
144
        $customer = new \Divine\Engine\Library\Customer(
145
            $this->registry
146
        );
147
        $this->registry->set(
148
            'customer',
149
            $customer
150
        );
151
152
        // Customer Group
153
        if ($this->customer->isLogged()) {
154
            $this->config->set(
155
                'config_customer_group_id',
156
                $this->customer->getGroupId()
157
            );
158
        } elseif (isset($this->session->data['customer']) && isset($this->session->data['customer']['customer_group_id'])) {
159
            // For API calls
160
            $this->config->set(
161
                'config_customer_group_id',
162
                $this->session->data['customer']['customer_group_id']
163
            );
164
        } elseif (isset($this->session->data['guest']) && isset($this->session->data['guest']['customer_group_id'])) {
165
            $this->config->set(
166
                'config_customer_group_id',
167
                $this->session->data['guest']['customer_group_id']
168
            );
169
        }
170
171
        // Tracking Code
172
        if (isset($this->request->get['tracking'])) {
173
            setcookie('tracking', $this->request->get['tracking'], time() + 3600 * 24 * 1000, '/');
174
175
            $this->db->query("
176
                UPDATE `marketing` 
177
                SET clicks = (clicks + 1) 
178
                WHERE code = '" . $this->db->escape($this->request->get['tracking']) . "'
179
            ");
180
        }
181
182
        // Currency
183
        $code = '';
184
185
        $this->load->model('localisation/currency');
186
187
        $currencies = $this->model_localisation_currency->getCurrencies();
188
189
        if (isset($this->session->data['currency'])) {
190
            $code = $this->session->data['currency'];
191
        }
192
193
        if (isset($this->request->cookie['currency']) && !array_key_exists($code, $currencies)) {
194
            $code = $this->request->cookie['currency'];
195
        }
196
197
        if (!array_key_exists($code, $currencies)) {
198
            $code = $this->config->get('config_currency');
199
        }
200
201
        if (!isset($this->session->data['currency']) || $this->session->data['currency'] != $code) {
202
            $this->session->data['currency'] = $code;
203
        }
204
205
        if (!isset($this->request->cookie['currency']) || $this->request->cookie['currency'] != $code) {
206
            setcookie('currency', $code, time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);
207
        }
208
209
        $this->registry->set(
210
            'currency',
211
            new \Divine\Engine\Library\Currency(
212
                $this->registry
213
            )
214
        );
215
216
        // Cart
217
        $this->registry->set(
218
            'cart',
219
            new \Divine\Engine\Library\Cart(
220
                $this->registry
221
            )
222
        );
223
224
        // Encryption
225
        $this->registry->set(
226
            'encryption',
227
            new \Divine\Engine\Library\Encryption(
228
                $this->config->get('config_encryption')
0 ignored issues
show
The call to Divine\Engine\Library\Encryption::__construct() has too many arguments starting with $this->config->get('config_encryption'). ( Ignorable by Annotation )

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

228
            /** @scrutinizer ignore-call */ 
229
            new \Divine\Engine\Library\Encryption(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
229
            )
230
        );
231
    }
232
}
233