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