Completed
Push — master ( 7ac2b2...53f768 )
by Marcus
02:57
created

Helper::reachThrough()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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 HaaseIT\Toolbox\Tools;
24
25
/**
26
 * Class Helper
27
 * @package HaaseIT\HCSF
28
 */
29
class Helper
30
{
31
    public static function redirectToPage($target = '', $replace = false, $http_response_header = 302)
32
    {
33
        if (empty($target)) {
34
            return false;
35
        }
36
37
        header('Location: '.$target, $replace, $http_response_header);
38
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method redirectToPage() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
39
    }
40
41
    public static function terminateScript($message = '')
42
    {
43
        die($message);
0 ignored issues
show
Coding Style Compatibility introduced by
The method terminateScript() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
44
    }
45
46
    /**
47
     * @param $file
48
     * @param int $width
49
     * @param int $height
50
     * @return bool|string
51
     */
52
    public static function getSignedGlideURL($file, $width = 0, $height = 0)
53
    {
54
        $urlBuilder = \League\Glide\Urls\UrlBuilderFactory::create('', HelperConfig::$secrets['glide_signkey']);
55
56
        $param = [];
57
        if ($width == 0 && $height == 0) {
58
            return false;
59
        }
60
        if ($width != 0) {
61
            $param['w'] = $width;
62
        }
63
        if ($height != 0) {
64
            $param['h'] = $height;
65
        }
66
        if ($width != 0 && $height != 0) {
67
            $param['fit'] = 'stretch';
68
        }
69
70
        return $urlBuilder->getUrl($file, $param);
71
    }
72
73
    /**
74
     * @param $to
75
     * @param string $subject
76
     * @param string $message
77
     * @param array $aImagesToEmbed
78
     * @param array $aFilesToAttach
79
     * @return bool
80
     */
81
    public static function mailWrapper($to, $subject = '(No subject)', $message = '', $aImagesToEmbed = [], $aFilesToAttach = []) {
82
        $mail = new \PHPMailer;
83
        $mail->CharSet = 'UTF-8';
84
85
        $mail->isMail();
86
        if (HelperConfig::$core['mail_method'] === 'sendmail') {
87
            $mail->isSendmail();
88
        } elseif (HelperConfig::$core['mail_method'] === 'smtp') {
89
            $mail->isSMTP();
90
            $mail->Host = HelperConfig::$secrets['mail_smtp_server'];
91
            $mail->Port = HelperConfig::$secrets['mail_smtp_port'];
92
            if (HelperConfig::$secrets['mail_smtp_auth'] === true) {
93
                $mail->SMTPAuth = true;
94
                $mail->Username = HelperConfig::$secrets['mail_smtp_auth_user'];
95
                $mail->Password = HelperConfig::$secrets['mail_smtp_auth_pwd'];
96
                if (HelperConfig::$secrets['mail_smtp_secure']) {
97
                    $mail->SMTPSecure = 'tls';
98
                    if (HelperConfig::$secrets['mail_smtp_secure_method'] === 'ssl') {
99
                        $mail->SMTPSecure = 'ssl';
100
                    }
101
                }
102
            }
103
        }
104
105
        $mail->From = HelperConfig::$core['email_sender'];
106
        $mail->FromName = HelperConfig::$core['email_sendername'];
107
        $mail->addAddress($to);
108
        $mail->isHTML(true);
109
        $mail->Subject = $subject;
110
        $mail->Body = $message;
111
112
        if (is_array($aImagesToEmbed) && count($aImagesToEmbed)) {
113
            foreach ($aImagesToEmbed as $sKey => $imgdata) {
114
                $imginfo = getimagesizefromstring($imgdata['binimg']);
115
                $mail->addStringEmbeddedImage($imgdata['binimg'], $sKey, $sKey, 'base64', $imginfo['mime']);
116
            }
117
        }
118
119
        if (is_array($aFilesToAttach) && count($aFilesToAttach)) {
120
            foreach ($aFilesToAttach as $sValue) {
121
                if (file_exists($sValue)) {
122
                    $mail->addAttachment($sValue);
123
                }
124
            }
125
        }
126
127
        //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
        return $mail->send();
129
    }
130
131
    // don't remove this, this is the fallback for unavailable twig functions
132
    /**
133
     * @param $string
134
     * @return mixed
135
     */
136
    public static function reachThrough($string) {
137
        return $string;
138
    }
139
    // don't remove this, this is the fallback for unavailable twig functions
140
    /**
141
     * @return string
142
     */
143
    public static function returnEmptyString() {
144
        return '';
145
    }
146
147
    /**
148
     * @param array $aP
149
     * @param Page $P
150
     */
151
    public static function getDebug($aP, $P)
0 ignored issues
show
Unused Code introduced by
The parameter $P is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
getDebug uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getDebug uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getDebug uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
152
    {
153
        if (!empty($_POST)) {
154
            Tools::debug($_POST, '$_POST');
155
        } elseif (!empty($_REQUEST)) {
156
            Tools::debug($_REQUEST, '$_REQUEST');
157
        }
158
        if (!empty($_SESSION)) {
159
            Tools::debug($_SESSION, '$_SESSION');
160
        }
161
        Tools::debug($aP, '$aP');
162
        //Tools::debug($P, '$P');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
163
    }
164
165
    /**
166
     * @return int|mixed|string
167
     */
168
    public static function getLanguage()
0 ignored issues
show
Coding Style introduced by
getLanguage uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getLanguage uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
getLanguage uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
169
    {
170
        $langavailable = HelperConfig::$core['lang_available'];
171
        if (
172
            HelperConfig::$core['lang_detection_method'] === 'domain'
173
            && isset(HelperConfig::$core['lang_by_domain'])
174
            && is_array(HelperConfig::$core['lang_by_domain'])
175
        ) { // domain based language detection
176
            foreach (HelperConfig::$core['lang_by_domain'] as $sKey => $sValue) {
177
                if ($_SERVER['SERVER_NAME'] == $sValue || $_SERVER['SERVER_NAME'] == 'www.'.$sValue) {
178
                    $sLang = $sKey;
179
                    break;
180
                }
181
            }
182
        } elseif (HelperConfig::$core['lang_detection_method'] === 'legacy') { // legacy language detection
183
            $sLang = key($langavailable);
184
            if (isset($_GET['language']) && array_key_exists($_GET['language'], $langavailable)) {
185
                $sLang = strtolower($_GET['language']);
186
                setcookie('language', strtolower($_GET['language']), 0, '/');
187
            } elseif (isset($_COOKIE['language']) && array_key_exists($_COOKIE['language'], $langavailable)) {
188
                $sLang = strtolower($_COOKIE['language']);
189
            } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && array_key_exists(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), $langavailable)) {
190
                $sLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
191
            }
192
        }
193
        if (!isset($sLang)) {
194
            $sLang = key($langavailable);
195
        }
196
197
        return $sLang;
198
    }
199
200
    /**
201
     * @param string $purpose
202
     * @return bool|\HTMLPurifier
203
     */
204
    public static function getPurifier($purpose)
205
    {
206
        $purifier_config = \HTMLPurifier_Config::createDefault();
207
        $purifier_config->set('Core.Encoding', 'UTF-8');
208
        $purifier_config->set('Cache.SerializerPath', PATH_PURIFIERCACHE);
209
        $purifier_config->set('HTML.Doctype', HelperConfig::$core['purifier_doctype']);
210
211
        if ($purpose === 'textcat') {
212
            $configkey = 'textcat';
213
            $configsection = 'core';
214
        } elseif ($purpose === 'page') {
215
            $configkey = 'pagetext';
216
            $configsection = 'core';
217
        } elseif ($purpose === 'item') {
218
            $configkey = 'itemtext';
219
            $configsection = 'shop';
220
        } elseif ($purpose === 'itemgroup') {
221
            $configkey = 'itemgrouptext';
222
            $configsection = 'shop';
223
        } else {
224
            return false;
225
        }
226
227
        if (!empty(HelperConfig::${$configsection}[$configkey.'_unsafe_html_whitelist'])) {
228
            $purifier_config->set('HTML.Allowed', HelperConfig::${$configsection}[$configkey.'_unsafe_html_whitelist']);
229
        }
230
        if (!empty(HelperConfig::${$configsection}[$configkey.'_loose_filtering'])) {
231
            $purifier_config->set('HTML.Trusted', true);
232
            $purifier_config->set('Attr.EnableID', true);
233
            $purifier_config->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']);
234
        }
235
236
        return new \HTMLPurifier($purifier_config);
237
    }
238
239
    /**
240
     * @param $callback
241
     * @param $parameters
242
     * @return bool|mixed
243
     */
244
    public static function twigCallback($callback, $parameters)
245
    {
246
        $callbacks = [
247
            'renderItemStatusIcon' => '\HaaseIT\HCSF\Shop\Helper::renderItemStatusIcon',
248
            'shopadminMakeCheckbox' => '\HaaseIT\HCSF\Shop\Helper::shopadminMakeCheckbox',
249
        ];
250
251
        if (!isset($callbacks[$callback])) {
252
            return false;
253
        }
254
        
255
        return call_user_func($callbacks[$callback], $parameters);
256
    }
257
}