Completed
Push — master ( 152809...934151 )
by Marcus
02:26
created

Helper::redirectToPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
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 = '')
32
    {
33
        if (empty($target)) {
34
            return false;
35
        }
36
37
        header('Location: '.$target);
38
        die();
39
    }
40
41
    /**
42
     * @param $file
43
     * @param int $width
44
     * @param int $height
45
     * @return bool|string
46
     */
47
    public static function getSignedGlideURL($file, $width = 0, $height = 0)
48
    {
49
        $urlBuilder = \League\Glide\Urls\UrlBuilderFactory::create('', HelperConfig::$secrets['glide_signkey']);
50
51
        $param = [];
52
        if ($width == 0 && $height == 0) {
53
            return false;
54
        }
55
        if ($width != 0) {
56
            $param['w'] = $width;
57
        }
58
        if ($height != 0) {
59
            $param['h'] = $height;
60
        }
61
        if ($width != 0 && $height != 0) {
62
            $param['fit'] = 'stretch';
63
        }
64
65
        return $urlBuilder->getUrl($file, $param);
66
    }
67
68
    /**
69
     * @param $to
70
     * @param string $subject
71
     * @param string $message
72
     * @param array $aImagesToEmbed
73
     * @param array $aFilesToAttach
74
     * @return bool
75
     */
76
    public static function mailWrapper($to, $subject = '(No subject)', $message = '', $aImagesToEmbed = [], $aFilesToAttach = []) {
77
        $mail = new \PHPMailer;
78
        $mail->CharSet = 'UTF-8';
79
80
        $mail->isMail();
81
        if (HelperConfig::$core['mail_method'] === 'sendmail') {
82
            $mail->isSendmail();
83
        } elseif (HelperConfig::$core['mail_method'] === 'smtp') {
84
            $mail->isSMTP();
85
            $mail->Host = HelperConfig::$secrets['mail_smtp_server'];
86
            $mail->Port = HelperConfig::$secrets['mail_smtp_port'];
87
            if (HelperConfig::$secrets['mail_smtp_auth'] === true) {
88
                $mail->SMTPAuth = true;
89
                $mail->Username = HelperConfig::$secrets['mail_smtp_auth_user'];
90
                $mail->Password = HelperConfig::$secrets['mail_smtp_auth_pwd'];
91
                if (HelperConfig::$secrets['mail_smtp_secure']) {
92
                    $mail->SMTPSecure = 'tls';
93
                    if (HelperConfig::$secrets['mail_smtp_secure_method'] === 'ssl') {
94
                        $mail->SMTPSecure = 'ssl';
95
                    }
96
                }
97
            }
98
        }
99
100
        $mail->From = HelperConfig::$core['email_sender'];
101
        $mail->FromName = HelperConfig::$core['email_sendername'];
102
        $mail->addAddress($to);
103
        $mail->isHTML(true);
104
        $mail->Subject = $subject;
105
        $mail->Body = $message;
106
107
        if (is_array($aImagesToEmbed) && count($aImagesToEmbed)) {
108
            foreach ($aImagesToEmbed as $sKey => $imgdata) {
109
                $imginfo = getimagesizefromstring($imgdata['binimg']);
110
                $mail->addStringEmbeddedImage($imgdata['binimg'], $sKey, $sKey, 'base64', $imginfo['mime']);
111
            }
112
        }
113
114
        if (is_array($aFilesToAttach) && count($aFilesToAttach)) {
115
            foreach ($aFilesToAttach as $sValue) {
116
                if (file_exists($sValue)) {
117
                    $mail->addAttachment($sValue);
118
                }
119
            }
120
        }
121
122
        //$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...
123
        return $mail->send();
124
    }
125
126
    // don't remove this, this is the fallback for unavailable twig functions
127
    /**
128
     * @param $string
129
     * @return mixed
130
     */
131
    public static function reachThrough($string) {
132
        return $string;
133
    }
134
    // don't remove this, this is the fallback for unavailable twig functions
135
    /**
136
     * @return string
137
     */
138
    public static function returnEmptyString() {
139
        return '';
140
    }
141
142
    /**
143
     * @param array $aP
144
     * @param Page $P
145
     */
146
    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...
147
    {
148
        if (!empty($_POST)) {
149
            Tools::debug($_POST, '$_POST');
150
        } elseif (!empty($_REQUEST)) {
151
            Tools::debug($_REQUEST, '$_REQUEST');
152
        }
153
        if (!empty($_SESSION)) {
154
            Tools::debug($_SESSION, '$_SESSION');
155
        }
156
        Tools::debug($aP, '$aP');
157
        //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...
158
    }
159
160
    /**
161
     * @return int|mixed|string
162
     */
163
    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...
164
    {
165
        $langavailable = HelperConfig::$core['lang_available'];
166
        if (
167
            HelperConfig::$core['lang_detection_method'] === 'domain'
168
            && isset(HelperConfig::$core['lang_by_domain'])
169
            && is_array(HelperConfig::$core['lang_by_domain'])
170
        ) { // domain based language detection
171
            foreach (HelperConfig::$core['lang_by_domain'] as $sKey => $sValue) {
172
                if ($_SERVER['SERVER_NAME'] == $sValue || $_SERVER['SERVER_NAME'] == 'www.'.$sValue) {
173
                    $sLang = $sKey;
174
                    break;
175
                }
176
            }
177
        } elseif (HelperConfig::$core['lang_detection_method'] === 'legacy') { // legacy language detection
178
            $sLang = key($langavailable);
179
            if (isset($_GET['language']) && array_key_exists($_GET['language'], $langavailable)) {
180
                $sLang = strtolower($_GET['language']);
181
                setcookie('language', strtolower($_GET['language']), 0, '/');
182
            } elseif (isset($_COOKIE['language']) && array_key_exists($_COOKIE['language'], $langavailable)) {
183
                $sLang = strtolower($_COOKIE['language']);
184
            } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && array_key_exists(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), $langavailable)) {
185
                $sLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
186
            }
187
        }
188
        if (!isset($sLang)) {
189
            $sLang = key($langavailable);
190
        }
191
192
        return $sLang;
193
    }
194
195
    /**
196
     * @param string $purpose
197
     * @return bool|\HTMLPurifier
198
     */
199
    public static function getPurifier($purpose)
200
    {
201
        $purifier_config = \HTMLPurifier_Config::createDefault();
202
        $purifier_config->set('Core.Encoding', 'UTF-8');
203
        $purifier_config->set('Cache.SerializerPath', PATH_PURIFIERCACHE);
204
        $purifier_config->set('HTML.Doctype', HelperConfig::$core['purifier_doctype']);
205
206
        if ($purpose === 'textcat') {
207
            $configkey = 'textcat';
208
            $configsection = 'core';
209
        } elseif ($purpose === 'page') {
210
            $configkey = 'pagetext';
211
            $configsection = 'core';
212
        } elseif ($purpose === 'item') {
213
            $configkey = 'itemtext';
214
            $configsection = 'shop';
215
        } elseif ($purpose === 'itemgroup') {
216
            $configkey = 'itemgrouptext';
217
            $configsection = 'shop';
218
        } else {
219
            return false;
220
        }
221
222
        if (!empty(HelperConfig::${$configsection}[$configkey.'_unsafe_html_whitelist'])) {
223
            $purifier_config->set('HTML.Allowed', HelperConfig::${$configsection}[$configkey.'_unsafe_html_whitelist']);
224
        }
225
        if (!empty(HelperConfig::${$configsection}[$configkey.'_loose_filtering'])) {
226
            $purifier_config->set('HTML.Trusted', true);
227
            $purifier_config->set('Attr.EnableID', true);
228
            $purifier_config->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']);
229
        }
230
231
        return new \HTMLPurifier($purifier_config);
232
    }
233
234
    /**
235
     * @param $callback
236
     * @param $parameters
237
     * @return bool|mixed
238
     */
239
    public static function twigCallback($callback, $parameters)
240
    {
241
        $callbacks = [
242
            'renderItemStatusIcon' => '\HaaseIT\HCSF\Shop\Helper::renderItemStatusIcon',
243
            'shopadminMakeCheckbox' => '\HaaseIT\HCSF\Shop\Helper::shopadminMakeCheckbox',
244
        ];
245
246
        if (!isset($callbacks[$callback])) {
247
            return false;
248
        }
249
        
250
        return call_user_func($callbacks[$callback], $parameters);
251
    }
252
}