Completed
Push — master ( 60b053...61ce86 )
by Marcus
02:08
created

HelperConfig::getLang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
HCSF - A multilingual CMS and Shopsystem
5
Copyright (C) 2014  Marcus Haase - [email protected]
6
7
This program is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
21
namespace HaaseIT\HCSF;
22
23
use Symfony\Component\Yaml\Yaml;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class HelperConfig
28
 * @package HaaseIT\HCSF
29
 */
30
class HelperConfig
31
{
32
    /**
33
     * @var array
34
     */
35
    private $core = [];
36
37
    /**
38
     * @var array
39
     */
40
    private $secrets = [];
41
42
    /**
43
     * @var array
44
     */
45
    private $countries = [];
46
47
    /**
48
     * @var array
49
     */
50
    private $shop = [];
51
52
    /**
53
     * @var array
54
     */
55
    private $customer = [];
56
57
    /**
58
     * @var array
59
     */
60
    private $navigation = [];
61
62
    /**
63
     * @var string
64
     */
65
    private $lang;
66
67
    /**
68
     *
69
     */
70
    public function __construct()
71
    {
72
        $this->loadCore();
73
        $this->loadCountries();
74
75
        $this->lang = $this->getLanguage();
76
77
        $this->loadSecrets();
78
79
        if ($this->core['enable_module_customer']) {
80
            $this->loadCustomer();
81
        }
82
83
        if ($this->core['enable_module_shop']) {
84
            $this->loadShop();
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getLang()
92
    {
93
        return $this->lang;
94
    }
95
96
    /**
97
     *
98
     */
99
    private function loadCore()
100
    {
101
        $core = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/core.yml'));
102
        if (is_file(PATH_BASEDIR.'config/core.yml')) {
103
            $core = array_merge($core, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/core.yml')));
104
        }
105
106
        $core['directory_images'] = trim($core['directory_images'], " \t\n\r\0\x0B/"); // trim this
107
108
        if (!empty($core['maintenancemode']) && $core['maintenancemode']) {
109
            $core['enable_module_customer'] = false;
110
            $core['enable_module_shop'] = false;
111
            $core['templatecache_enable'] = false;
112
            $core['debug'] = false;
113
            $core['textcatsverbose'] = false;
114
        } else {
115
            $core['maintenancemode'] = false;
116
        }
117
118
        if ($core['enable_module_shop']) {
119
            $core['enable_module_customer'] = true;
120
        }
121
122
        $this->core = $core;
123
    }
124
125
    /**
126
     * @param string|false $setting
127
     * @return mixed
128
     */
129 View Code Duplication
    public function getCore($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
132
            return $this->core;
133
        }
134
135
        return !empty($this->core[$setting]) ? $this->core[$setting] : false;
136
    }
137
138
    /**
139
     *
140
     */
141 View Code Duplication
    private function loadCountries()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        $countries = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/countries.yml'));
144
        if (is_file(PATH_BASEDIR.'config/countries.yml')) {
145
            $countries = array_merge($countries, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/countries.yml')));
146
        }
147
148
        $this->countries = $countries;
149
    }
150
151
    /**
152
     * @param string|false $setting
153
     * @return mixed
154
     */
155 View Code Duplication
    public function getCountries($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
158
            return $this->countries;
159
        }
160
161
        return !empty($this->countries[$setting]) ? $this->countries[$setting] : false;
162
    }
163
164
    /**
165
     *
166
     */
167 View Code Duplication
    private function loadSecrets()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
    {
169
        $secrets = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/secrets.yml'));
170
        if (is_file(PATH_BASEDIR.'config/secrets.yml')) {
171
            $secrets = array_merge($secrets, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/secrets.yml')));
172
        }
173
174
        $this->secrets = $secrets;
175
    }
176
177
    /**
178
     * @param string|false $setting
179
     * @return mixed
180
     */
181 View Code Duplication
    public function getSecret($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
184
            return $this->secrets;
185
        }
186
187
        return !empty($this->secrets[$setting]) ? $this->secrets[$setting] : false;
188
    }
189
190
    /**
191
     *
192
     */
193 View Code Duplication
    private function loadCustomer()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195
        $customer = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/customer.yml'));
196
        if (is_file(PATH_BASEDIR.'/config/customer.yml')) {
197
            $customer = array_merge($customer, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/customer.yml')));
198
        }
199
200
        $this->customer = $customer;
201
    }
202
203
    /**
204
     * @param string|bool $setting
205
     * @return mixed
206
     */
207 View Code Duplication
    public function getCustomer($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209
        if (!$setting) {
210
            return $this->customer;
211
        }
212
213
        return !empty($this->customer[$setting]) ? $this->customer[$setting] : false;
214
    }
215
216
    /**
217
     *
218
     */
219
    private function loadShop()
220
    {
221
        $shop = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/shop.yml'));
222
        if (is_file(PATH_BASEDIR.'config/shop.yml')) {
223
            $shop = array_merge($shop, Yaml::parse(file_get_contents(PATH_BASEDIR.'config/shop.yml')));
224
        }
225
        if (isset($shop['vat_disable']) && $shop['vat_disable']) {
226
            $shop['vat'] = ['full' => 0, 'reduced' => 0];
227
        }
228
229
        $this->shop = $shop;
230
    }
231
232
    /**
233
     * @param string|false $setting
234
     * @return mixed
235
     */
236 View Code Duplication
    public function getShop($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
239
            return $this->shop;
240
        }
241
242
        return !empty($this->shop[$setting]) ? $this->shop[$setting] : false;
243
    }
244
245
    /**
246
     * @param ServiceManager $serviceManager
247
     */
248
    public function loadNavigation(ServiceManager $serviceManager)
249
    {
250
        if (is_file(PATH_BASEDIR.'config/navigation.yml')) {
251
            $navstruct = Yaml::parse(file_get_contents(PATH_BASEDIR.'config/navigation.yml'));
252
        } else {
253
            $navstruct = Yaml::parse(file_get_contents(HCSF_BASEDIR.'config/navigation.yml'));
254
        }
255
256
        if (!empty($navstruct) && $this->core['navigation_fetch_text_from_textcats']) {
257
            $textcats = $serviceManager->get('textcats');
258
            $TMP = [];
259
260
            foreach ($navstruct as $key => $item) {
0 ignored issues
show
Bug introduced by
The expression $navstruct of type object<Symfony\Component...|array|object<stdClass> 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...
261
                foreach ($item as $subkey => $subitem) {
262
                    if (!empty($textcats->T($subkey))) {
263
                        $TMP[$key][$textcats->T($subkey)] = $subitem;
264
                    } else {
265
                        $TMP[$key][$subkey] = $subitem;
266
                    }
267
                }
268
            }
269
            $navstruct = $TMP;
270
            unset($TMP);
271
        }
272
273
        if (isset($navstruct['admin'])) {
274
            unset($navstruct['admin']);
275
        }
276
277
        $hardcodedtextcats = $serviceManager->get('hardcodedtextcats');
278
279
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_home')] = '/_admin/index.html';
280
281
        if ($this->core['enable_module_shop']) {
282
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_orders')] = '/_admin/shopadmin.html';
283
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_items')] = '/_admin/itemadmin.html';
284
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_itemgroups')] = '/_admin/itemgroupadmin.html';
285
        }
286
287
        if ($this->core['enable_module_customer']) {
288
            $navstruct['admin'][$hardcodedtextcats->get('admin_nav_customers')] = '/_admin/customeradmin.html';
289
        }
290
291
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_pages')] = '/_admin/pageadmin.html';
292
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_textcats')] = '/_admin/textcatadmin.html';
293
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_cleartemplatecache')] = '/_admin/cleartemplatecache.html';
294
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_clearimagecache')] = '/_admin/clearimagecache.html';
295
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_phpinfo')] = '/_admin/phpinfo.html';
296
        $navstruct['admin'][$hardcodedtextcats->get('admin_nav_dbstatus')] = '/_admin/dbstatus.html';
297
298
        $this->navigation = $navstruct;
299
    }
300
301
    /**
302
     * @param string|false $setting
303
     * @return mixed
304
     */
305 View Code Duplication
    public function getNavigation($setting = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306
    {
307
        if (!$setting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $setting of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
308
            return $this->navigation;
309
        }
310
311
        return !empty($this->navigation[$setting]) ? $this->navigation[$setting] : false;
312
    }
313
314
    /**
315
     * @return int|mixed|string
316
     */
317
    public function getLanguage()
318
    {
319
        $langavailable = $this->core['lang_available'];
320
        if (
321
            $this->core['lang_detection_method'] === 'domain'
322
            && isset($this->core['lang_by_domain'])
323
            && is_array($this->core['lang_by_domain'])
324
        ) { // domain based language detection
325
            $serverservername = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL);
326
            foreach ($this->core['lang_by_domain'] as $sKey => $sValue) {
327
                if ($serverservername == $sValue || $serverservername == 'www.'.$sValue) {
328
                    $sLang = $sKey;
329
                    break;
330
                }
331
            }
332
        } elseif ($this->core['lang_detection_method'] === 'legacy') { // legacy language detection
333
            $sLang = key($langavailable);
334
            $getlanguage = filter_input(INPUT_GET, 'language', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
335
            $cookielanguage = filter_input(INPUT_COOKIE, 'language', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
336
            $serverhttpaccepptlanguage = filter_input(INPUT_SERVER, '', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
337
            if ($getlanguage !== null && array_key_exists($getlanguage, $langavailable)) {
338
                $sLang = strtolower($getlanguage);
339
                setcookie('language', strtolower($getlanguage), 0, '/');
340
            } elseif ($cookielanguage !== null && array_key_exists($cookielanguage, $langavailable)) {
341
                $sLang = strtolower($cookielanguage);
342
            } elseif ($serverhttpaccepptlanguage !== null && array_key_exists(substr($serverhttpaccepptlanguage, 0, 2), $langavailable)) {
343
                $sLang = substr($serverhttpaccepptlanguage, 0, 2);
344
            }
345
        }
346
        if (!isset($sLang)) {
347
            $sLang = key($langavailable);
348
        }
349
350
        return $sLang;
351
    }
352
}
353