Completed
Push — master ( 1d1f79...85914a )
by Harald
06:13 queued 02:42
created

OSCOM::linkImage()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 15

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 8
nop 0
dl 28
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright (c) 2016 osCommerce; https://www.oscommerce.com
6
  * @license GPL; https://www.oscommerce.com/gpllicense.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\DateTime;
12
use OSC\OM\ErrorHandler;
13
use OSC\OM\FileSystem;
14
use OSC\OM\HTML;
15
use OSC\OM\HTTP;
16
use OSC\OM\Registry;
17
18
class OSCOM
19
{
20
    const BASE_DIR = OSCOM_BASE_DIR;
21
22
    protected static $version;
23
    protected static $site = 'Shop';
24
    protected static $cfg = [];
25
26
    public static function initialize()
27
    {
28
        static::loadConfig();
29
30
        DateTime::setTimeZone();
31
32
        ErrorHandler::initialize();
33
34
        HTTP::setRequestType();
35
    }
36
37
    public static function getVersion()
38
    {
39
        if (!isset(static::$version)) {
40
            $file = static::BASE_DIR . 'version.txt';
41
42
            $v = trim(file_get_contents($file));
43
44
            if (preg_match('/^(\d+\.)?(\d+\.)?(\d+)$/', $v)) {
45
                static::$version = $v;
46
            } else {
47
                trigger_error('Version number is not numeric. Please verify: ' . $file);
48
            }
49
        }
50
51
        return static::$version;
52
    }
53
54
    public static function siteExists($site, $strict = true) {
55
        $class = 'OSC\Sites\\' . $site . '\\' . $site;
56
57
        if (class_exists($class)) {
58 View Code Duplication
            if (is_subclass_of($class, 'OSC\OM\SitesInterface')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
59
                return true;
60
            } else {
61
                trigger_error('OSC\OM\OSCOM::siteExists() - ' . $site . ': Site does not implement OSC\OM\SitesInterface and cannot be loaded.');
62
            }
63
        } elseif ($strict === true) {
64
            trigger_error('OSC\OM\OSCOM::siteExists() - ' . $site . ': Site does not exist.');
65
        }
66
67
        return false;
68
    }
69
70
    public static function loadSite($site = null)
71
    {
72
        if (!isset($site)) {
73
            $site = static::$site;
74
        }
75
76
        static::setSite($site);
77
    }
78
79
    public static function setSite($site)
80
    {
81
        if (!static::siteExists($site)) {
82
            $site = static::$site;
83
        }
84
85
        static::$site = $site;
86
87
        $class = 'OSC\Sites\\' . $site . '\\' . $site;
88
89
        $OSCOM_Site = new $class();
90
        Registry::set('Site', $OSCOM_Site);
91
92
        $OSCOM_Site->setPage();
93
    }
94
95
    public static function getSite()
96
    {
97
        return static::$site;
98
    }
99
100
    public static function hasSitePage()
101
    {
102
        return Registry::get('Site')->hasPage();
103
    }
104
105
    public static function getSitePageFile()
106
    {
107
        return Registry::get('Site')->getPage()->getFile();
108
    }
109
110
    public static function useSiteTemplateWithPageFile()
111
    {
112
        return Registry::get('Site')->getPage()->useSiteTemplate();
113
    }
114
115
    public static function isRPC()
116
    {
117
        $OSCOM_Site = Registry::get('Site');
118
119
        return $OSCOM_Site->hasPage() && $OSCOM_Site->getPage()->isRPC();
120
    }
121
122
    public static function link($page, $parameters = null, $add_session_id = true, $search_engine_safe = true)
123
    {
124
        $OSCOM_Session = Registry::get('Session');
125
126
        $page = HTML::sanitize($page);
127
128
        $site = $req_site = static::$site;
129
130
        if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && OSCOM::siteExists($matches[1], false)) {
131
            $req_site = $matches[1];
132
            $page = $matches[2];
133
        }
134
135
        if (!is_bool($add_session_id)) {
136
            $add_session_id = true;
137
        }
138
139
        if (!is_bool($search_engine_safe)) {
140
            $search_engine_safe = true;
141
        }
142
143
        if (($add_session_id === true) && ($site !== $req_site)) {
144
            $add_session_id = false;
145
        }
146
147
        $link = static::getConfig('http_server', $req_site) . static::getConfig('http_path', $req_site) . $page;
148
149
        if (!empty($parameters)) {
150
            $link .= '?' . HTML::sanitize($parameters);
151
            $separator = '&';
152
        } else {
153
            $separator = '?';
154
        }
155
156
        while ((substr($link, -1) == '&') || (substr($link, -1) == '?')) {
157
            $link = substr($link, 0, -1);
158
        }
159
160
// Add the session ID when moving from different HTTP and HTTPS servers, or when SID is defined
161
        if (($add_session_id == true) && $OSCOM_Session->hasStarted() && ($OSCOM_Session->isForceCookies() === false)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
162
            if ((strlen(SID) > 0) || (((HTTP::getRequestType() == 'NONSSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'https')) || ((HTTP::getRequestType() == 'SSL') && (parse_url(static::getConfig('http_server', $req_site), PHP_URL_SCHEME) == 'http')))) {
163
                $link .= $separator . HTML::sanitize(session_name() . '=' . session_id());
164
            }
165
        }
166
167
        while (strpos($link, '&&') !== false) {
168
            $link = str_replace('&&', '&', $link);
169
        }
170
171
        if ((SEARCH_ENGINE_FRIENDLY_URLS == 'true') && ($search_engine_safe == true)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
172
            $link = str_replace(['?', '&', '='], '/', $link);
173
        }
174
175
        return $link;
176
    }
177
178 View Code Duplication
    public static function linkImage()
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...
179
    {
180
        $args = func_get_args();
181
182
        if (!isset($args[0])) {
183
            $args[0] = null;
184
        }
185
186
        if (!isset($args[1])) {
187
            $args[1] = null;
188
        }
189
190
        $args[2] = false;
191
192
        $page = $args[0];
193
        $req_site = static::$site;
194
195
        if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && OSCOM::siteExists($matches[1], false)) {
196
            $req_site = $matches[1];
197
            $page = $matches[2];
198
        }
199
200
        $args[0] = $req_site . '/' . static::getConfig('http_images_path', $req_site) . $page;
201
202
        $url = forward_static_call_array('static::link', $args);
203
204
        return $url;
205
    }
206
207 View Code Duplication
    public static function linkPublic()
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
        $args = func_get_args();
210
211
        if (!isset($args[0])) {
212
            $args[0] = null;
213
        }
214
215
        if (!isset($args[1])) {
216
            $args[1] = null;
217
        }
218
219
        $args[2] = false;
220
221
        $page = $args[0];
222
        $req_site = static::$site;
223
224
        if ((strpos($page, '/') !== false) && (preg_match('/^([A-Z][A-Za-z0-9-_]*)\/(.*)$/', $page, $matches) === 1) && OSCOM::siteExists($matches[1], false)) {
225
            $req_site = $matches[1];
226
            $page = $matches[2];
227
        }
228
229
        $args[0] = 'Shop/public/Sites/' . $req_site . '/' . $page;
230
231
        $url = forward_static_call_array('static::link', $args);
232
233
        return $url;
234
    }
235
236
    public static function redirect()
237
    {
238
        $args = func_get_args();
239
240
        $url = forward_static_call_array('static::link', $args);
241
242
        if ((strstr($url, "\n") !== false) || (strstr($url, "\r") !== false)) {
243
            $url = static::link('index.php', '', false);
244
        }
245
246
        HTTP::redirect($url);
247
    }
248
249
    public static function getDef()
250
    {
251
        $OSCOM_Language = Registry::get('Language');
252
253
        return call_user_func_array([$OSCOM_Language, 'getDef'], func_get_args());
254
    }
255
256
    public static function hasRoute(array $path)
257
    {
258
        return array_slice(array_keys($_GET), 0, count($path)) == $path;
259
    }
260
261
    public static function loadConfig()
262
    {
263
        static::loadConfigFile(static::BASE_DIR . 'Conf/global.php', 'global');
264
265
        if (is_file(static::BASE_DIR . 'Custom/Conf/global.php')) {
266
            static::loadConfigFile(static::BASE_DIR . 'Custom/Conf/global.php', 'global');
267
        }
268
269
        foreach (glob(static::BASE_DIR . 'Sites/*', GLOB_ONLYDIR) as $s) {
270
            $s = basename($s);
271
272
            if (static::siteExists($s, false) && is_file(static::BASE_DIR . 'Sites/' . $s . '/site_conf.php')) {
273
                static::loadConfigFile(static::BASE_DIR . 'Sites/' . $s . '/site_conf.php', $s);
274
275
                if (is_file(static::BASE_DIR . 'Custom/Sites/' . $s . '/site_conf.php')) {
276
                    static::loadConfigFile(static::BASE_DIR . 'Custom/Sites/' . $s . '/site_conf.php', $s);
277
                }
278
            }
279
        }
280
    }
281
282
    public static function loadConfigFile($file, $group)
283
    {
284
        $cfg = [];
285
286
        if (is_file($file)) {
287
            include($file);
288
289
            if (isset($ini)) {
0 ignored issues
show
Bug introduced by
The variable $ini seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
290
                $cfg = parse_ini_string($ini);
291
            }
292
        }
293
294
        if (!empty($cfg)) {
295
            static::$cfg[$group] = (isset(static::$cfg[$group])) ? array_merge(static::$cfg[$group], $cfg) : $cfg;
296
        }
297
    }
298
299
    public static function getConfig($key, $group = null)
300
    {
301
        if (!isset($group)) {
302
            $group = static::getSite();
303
        }
304
305
        if (isset(static::$cfg[$group][$key])) {
306
            return static::$cfg[$group][$key];
307
        }
308
309
        return static::$cfg['global'][$key];
310
    }
311
312
    public static function configExists($key, $group = null)
313
    {
314
        if (!isset($group)) {
315
            $group = static::getSite();
316
        }
317
318
        if (isset(static::$cfg[$group][$key])) {
319
            return true;
320
        }
321
322
        return isset(static::$cfg['global'][$key]);
323
    }
324
325
    public static function setConfig($key, $value, $group = null)
326
    {
327
        if (!isset($group)) {
328
            $group = 'global';
329
        }
330
331
        static::$cfg[$group][$key] = $value;
332
    }
333
334
    public static function autoload($class)
335
    {
336
        $prefix = 'OSC\\';
337
338
        if (strncmp($prefix, $class, strlen($prefix)) !== 0) {
339
            return false;
340
        }
341
342
        if (strncmp($prefix . 'OM\Module\\', $class, strlen($prefix . 'OM\Module\\')) === 0) { // TODO remove and fix namespace
343
          $file = dirname(OSCOM_BASE_DIR) . '/' . str_replace(['OSC\OM\\', '\\'], ['', '/'], $class) . '.php';
344
          $custom = dirname(OSCOM_BASE_DIR) . '/' . str_replace(['OSC\OM\\', '\\'], ['OSC\Custom\OM\\', '/'], $class) . '.php';
345
        } else {
346
          $file = dirname(OSCOM_BASE_DIR) . '/' . str_replace('\\', '/', $class) . '.php';
347
          $custom = str_replace('OSC/OM/', 'OSC/Custom/OM/', $file);
348
        }
349
350
        if (is_file($custom)) {
351
            require($custom);
352
        } elseif (is_file($file)) {
353
            require($file);
354
        }
355
    }
356
}
357