|
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
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Helper |
|
28
|
|
|
* @package HaaseIT\HCSF |
|
29
|
|
|
*/ |
|
30
|
|
|
class Helper |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var ServiceManager |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $serviceManager; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var \HaaseIT\HCSF\HelperConfig |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $config; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $core = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $secrets = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $shop = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var \HaaseIT\HCSF\Shop\Helper |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $helperShop; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Helper constructor. |
|
64
|
|
|
* @param ServiceManager $serviceManager |
|
65
|
|
|
*/ |
|
66
|
|
View Code Duplication |
public function __construct(ServiceManager $serviceManager) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
$this->serviceManager = $serviceManager; |
|
69
|
|
|
$this->config = $serviceManager->get('config'); |
|
70
|
|
|
$this->secrets = $this->config->getSecret(); |
|
71
|
|
|
$this->core = $this->config->getCore(); |
|
72
|
|
|
$this->shop = $this->config->getShop(); |
|
73
|
|
|
$this->helperShop = $serviceManager->get('helpershop'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $target |
|
78
|
|
|
* @param bool $replace |
|
79
|
|
|
* @param int $http_response_header |
|
80
|
|
|
* @return void|false |
|
81
|
|
|
*/ |
|
82
|
|
|
public function redirectToPage($target = '', $replace = false, $http_response_header = 302) |
|
83
|
|
|
{ |
|
84
|
|
|
if (empty($target)) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
header('Location: '.$target, $replace, $http_response_header); |
|
89
|
|
|
$this->terminateScript(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $message |
|
94
|
|
|
*/ |
|
95
|
|
|
public function terminateScript($message = '') |
|
96
|
|
|
{ |
|
97
|
|
|
die($message); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param $file |
|
102
|
|
|
* @param int $width |
|
103
|
|
|
* @param int $height |
|
104
|
|
|
* @return bool|string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getSignedGlideURL($file, $width = 0, $height = 0) |
|
107
|
|
|
{ |
|
108
|
|
|
$urlBuilder = \League\Glide\Urls\UrlBuilderFactory::create('', $this->secrets['glide_signkey']); |
|
109
|
|
|
|
|
110
|
|
|
$param = []; |
|
111
|
|
|
if ($width == 0 && $height == 0) { |
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
if ($width != 0) { |
|
115
|
|
|
$param['w'] = $width; |
|
116
|
|
|
} |
|
117
|
|
|
if ($height != 0) { |
|
118
|
|
|
$param['h'] = $height; |
|
119
|
|
|
} |
|
120
|
|
|
if ($width != 0 && $height != 0) { |
|
121
|
|
|
$param['fit'] = 'stretch'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $urlBuilder->getUrl($file, $param); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param $to |
|
129
|
|
|
* @param string $subject |
|
130
|
|
|
* @param string $message |
|
131
|
|
|
* @param array $aImagesToEmbed |
|
132
|
|
|
* @param array $aFilesToAttach |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
|
|
public function mailWrapper($to, $subject = '(No subject)', $message = '', $aImagesToEmbed = [], $aFilesToAttach = []) { |
|
136
|
|
|
$mail = new \PHPMailer; |
|
137
|
|
|
$mail->CharSet = 'UTF-8'; |
|
138
|
|
|
|
|
139
|
|
|
$mail->isMail(); |
|
140
|
|
|
if ($this->core['mail_method'] === 'sendmail') { |
|
141
|
|
|
$mail->isSendmail(); |
|
142
|
|
|
} elseif ($this->core['mail_method'] === 'smtp') { |
|
143
|
|
|
$mail->isSMTP(); |
|
144
|
|
|
$mail->Host = $this->secrets['mail_smtp_server']; |
|
145
|
|
|
$mail->Port = $this->secrets['mail_smtp_port']; |
|
146
|
|
|
if ($this->secrets['mail_smtp_auth'] === true) { |
|
147
|
|
|
$mail->SMTPAuth = true; |
|
148
|
|
|
$mail->Username = $this->secrets['mail_smtp_auth_user']; |
|
149
|
|
|
$mail->Password = $this->secrets['mail_smtp_auth_pwd']; |
|
150
|
|
|
if ($this->secrets['mail_smtp_secure']) { |
|
151
|
|
|
$mail->SMTPSecure = 'tls'; |
|
152
|
|
|
if ($this->secrets['mail_smtp_secure_method'] === 'ssl') { |
|
153
|
|
|
$mail->SMTPSecure = 'ssl'; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$mail->From = $this->core['email_sender']; |
|
160
|
|
|
$mail->FromName = $this->core['email_sendername']; |
|
161
|
|
|
$mail->addAddress($to); |
|
162
|
|
|
$mail->isHTML(true); |
|
163
|
|
|
$mail->Subject = $subject; |
|
164
|
|
|
$mail->Body = $message; |
|
165
|
|
|
|
|
166
|
|
|
if (is_array($aImagesToEmbed) && count($aImagesToEmbed)) { |
|
167
|
|
|
foreach ($aImagesToEmbed as $sKey => $imgdata) { |
|
168
|
|
|
$imginfo = getimagesizefromstring($imgdata['binimg']); |
|
169
|
|
|
$mail->addStringEmbeddedImage($imgdata['binimg'], $sKey, $sKey, 'base64', $imginfo['mime']); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (is_array($aFilesToAttach) && count($aFilesToAttach)) { |
|
174
|
|
|
foreach ($aFilesToAttach as $sValue) { |
|
175
|
|
|
if (file_exists($sValue)) { |
|
176
|
|
|
$mail->addAttachment($sValue); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $mail->send(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// don't remove this, this is the fallback for unavailable twig functions |
|
185
|
|
|
/** |
|
186
|
|
|
* @param $string |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
public function reachThrough($string) { |
|
190
|
|
|
return $string; |
|
191
|
|
|
} |
|
192
|
|
|
// don't remove this, this is the fallback for unavailable twig functions |
|
193
|
|
|
/** |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function returnEmptyString() { |
|
197
|
|
|
return ''; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param array $aP |
|
202
|
|
|
* @param Page $P |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getDebug($aP, $P) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
if (!empty($_POST)) { |
|
207
|
|
|
Tools::debug($_POST, '$_POST'); |
|
208
|
|
|
} elseif (!empty($_REQUEST)) { |
|
209
|
|
|
Tools::debug($_REQUEST, '$_REQUEST'); |
|
210
|
|
|
} |
|
211
|
|
|
if (!empty($_SESSION)) { |
|
212
|
|
|
Tools::debug($_SESSION, '$_SESSION'); |
|
213
|
|
|
} |
|
214
|
|
|
Tools::debug($aP, '$aP'); |
|
215
|
|
|
Tools::debug($P, '$P'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $purpose |
|
220
|
|
|
* @return bool|\HTMLPurifier |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getPurifier($purpose) |
|
223
|
|
|
{ |
|
224
|
|
|
$purifier_config = \HTMLPurifier_Config::createDefault(); |
|
225
|
|
|
$purifier_config->set('Core.Encoding', 'UTF-8'); |
|
226
|
|
|
$purifier_config->set('Cache.SerializerPath', PATH_PURIFIERCACHE); |
|
227
|
|
|
$purifier_config->set('HTML.Doctype', $this->core['purifier_doctype']); |
|
228
|
|
|
|
|
229
|
|
|
if ($purpose === 'textcat') { |
|
230
|
|
|
$configkey = 'textcat'; |
|
231
|
|
|
$configsection = 'core'; |
|
232
|
|
|
} elseif ($purpose === 'page') { |
|
233
|
|
|
$configkey = 'pagetext'; |
|
234
|
|
|
$configsection = 'core'; |
|
235
|
|
|
} elseif ($purpose === 'item') { |
|
236
|
|
|
$configkey = 'itemtext'; |
|
237
|
|
|
$configsection = 'shop'; |
|
238
|
|
|
} elseif ($purpose === 'itemgroup') { |
|
239
|
|
|
$configkey = 'itemgrouptext'; |
|
240
|
|
|
$configsection = 'shop'; |
|
241
|
|
|
} else { |
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
if (!empty($this->{$configsection}[$configkey.'_unsafe_html_whitelist'])) { |
|
246
|
|
|
$purifier_config->set('HTML.Allowed', $this->{$configsection}[$configkey.'_unsafe_html_whitelist']); |
|
247
|
|
|
} |
|
248
|
|
|
if (!empty($this->{$configsection}[$configkey.'_loose_filtering'])) { |
|
249
|
|
|
$purifier_config->set('HTML.Trusted', true); |
|
250
|
|
|
$purifier_config->set('Attr.EnableID', true); |
|
251
|
|
|
$purifier_config->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return new \HTMLPurifier($purifier_config); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param $callback |
|
259
|
|
|
* @param $parameters |
|
260
|
|
|
* @return bool|mixed |
|
261
|
|
|
*/ |
|
262
|
|
|
public function twigCallback($callback, $parameters) |
|
263
|
|
|
{ |
|
264
|
|
|
$callbacks = [ |
|
265
|
|
|
'renderItemStatusIcon' => [$this->helperShop, 'renderItemStatusIcon'], |
|
266
|
|
|
'shopadminMakeCheckbox' => [$this->helperShop, 'shopadminMakeCheckbox'], |
|
267
|
|
|
]; |
|
268
|
|
|
|
|
269
|
|
|
if (!isset($callbacks[$callback])) { |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return call_user_func($callbacks[$callback], $parameters); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param \Doctrine\DBAL\Connection $dbal |
|
278
|
|
|
* @param string $table |
|
279
|
|
|
* @param array $data |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
public function autoInsert(\Doctrine\DBAL\Connection $dbal, $table, array $data) |
|
283
|
|
|
{ |
|
284
|
|
|
/** @var \Doctrine\DBAL\Query\QueryBuilder $querybuilder */ |
|
285
|
|
|
$querybuilder = $dbal->createQueryBuilder(); |
|
286
|
|
|
$querybuilder->insert($table); |
|
287
|
|
|
|
|
288
|
|
|
foreach ($data as $colname => $col) { |
|
289
|
|
|
$querybuilder |
|
290
|
|
|
->setValue($colname, ':'.$colname) |
|
291
|
|
|
->setParameter(':'.$colname, $col); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
$querybuilder->execute(); |
|
295
|
|
|
|
|
296
|
|
|
return $dbal->lastInsertId(); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
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.