|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016-2021 |
|
6
|
|
|
* @package Client |
|
7
|
|
|
* @subpackage Html |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Client\Html\Account\Download; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default implementation of account download HTML client. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Client |
|
18
|
|
|
* @subpackage Html |
|
19
|
|
|
*/ |
|
20
|
|
|
class Standard |
|
21
|
|
|
extends \Aimeos\Client\Html\Common\Client\Factory\Base |
|
22
|
|
|
implements \Aimeos\Client\Html\Common\Client\Factory\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** client/html/account/download/subparts |
|
25
|
|
|
* List of HTML sub-clients rendered within the account download section |
|
26
|
|
|
* |
|
27
|
|
|
* The output of the frontend is composed of the code generated by the HTML |
|
28
|
|
|
* clients. Each HTML client can consist of serveral (or none) sub-clients |
|
29
|
|
|
* that are responsible for rendering certain sub-parts of the output. The |
|
30
|
|
|
* sub-clients can contain HTML clients themselves and therefore a |
|
31
|
|
|
* hierarchical tree of HTML clients is composed. Each HTML client creates |
|
32
|
|
|
* the output that is placed inside the container of its parent. |
|
33
|
|
|
* |
|
34
|
|
|
* At first, always the HTML code generated by the parent is printed, then |
|
35
|
|
|
* the HTML code of its sub-clients. The order of the HTML sub-clients |
|
36
|
|
|
* determines the order of the output of these sub-clients inside the parent |
|
37
|
|
|
* container. If the configured list of clients is |
|
38
|
|
|
* |
|
39
|
|
|
* array( "subclient1", "subclient2" ) |
|
40
|
|
|
* |
|
41
|
|
|
* you can easily change the order of the output by reordering the subparts: |
|
42
|
|
|
* |
|
43
|
|
|
* client/html/<clients>/subparts = array( "subclient1", "subclient2" ) |
|
44
|
|
|
* |
|
45
|
|
|
* You can also remove one or more parts if they shouldn't be rendered: |
|
46
|
|
|
* |
|
47
|
|
|
* client/html/<clients>/subparts = array( "subclient1" ) |
|
48
|
|
|
* |
|
49
|
|
|
* As the clients only generates structural HTML, the layout defined via CSS |
|
50
|
|
|
* should support adding, removing or reordering content by a fluid like |
|
51
|
|
|
* design. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array List of sub-client names |
|
54
|
|
|
* @since 2016.02 |
|
55
|
|
|
* @category Developer |
|
56
|
|
|
*/ |
|
57
|
|
|
private $subPartPath = 'client/html/account/download/subparts'; |
|
58
|
|
|
private $subPartNames = []; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the HTML code for insertion into the body. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
|
65
|
|
|
* @return string HTML code |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getBody( string $uid = '' ) : string |
|
68
|
|
|
{ |
|
69
|
|
|
return ''; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns the HTML string for insertion into the header. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
|
77
|
|
|
* @return string|null String including HTML tags for the header on error |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getHeader( string $uid = '' ) : ?string |
|
80
|
|
|
{ |
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the sub-client given by its name. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $type Name of the client type |
|
89
|
|
|
* @param string|null $name Name of the sub-client (Default if null) |
|
90
|
|
|
* @return \Aimeos\Client\Html\Iface Sub-client object |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getSubClient( string $type, string $name = null ) : \Aimeos\Client\Html\Iface |
|
93
|
|
|
{ |
|
94
|
|
|
/** client/html/account/download/decorators/excludes |
|
95
|
|
|
* Excludes decorators added by the "common" option from the account download html client |
|
96
|
|
|
* |
|
97
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
98
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
99
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
100
|
|
|
* modify what is returned to the caller. |
|
101
|
|
|
* |
|
102
|
|
|
* This option allows you to remove a decorator added via |
|
103
|
|
|
* "client/html/common/decorators/default" before they are wrapped |
|
104
|
|
|
* around the html client. |
|
105
|
|
|
* |
|
106
|
|
|
* client/html/account/download/decorators/excludes = array( 'decorator1' ) |
|
107
|
|
|
* |
|
108
|
|
|
* This would remove the decorator named "decorator1" from the list of |
|
109
|
|
|
* common decorators ("\Aimeos\Client\Html\Common\Decorator\*") added via |
|
110
|
|
|
* "client/html/common/decorators/default" to the html client. |
|
111
|
|
|
* |
|
112
|
|
|
* @param array List of decorator names |
|
113
|
|
|
* @since 2016.02 |
|
114
|
|
|
* @category Developer |
|
115
|
|
|
* @see client/html/common/decorators/default |
|
116
|
|
|
* @see client/html/account/download/decorators/global |
|
117
|
|
|
* @see client/html/account/download/decorators/local |
|
118
|
|
|
*/ |
|
119
|
|
|
|
|
120
|
|
|
/** client/html/account/download/decorators/global |
|
121
|
|
|
* Adds a list of globally available decorators only to the account download html client |
|
122
|
|
|
* |
|
123
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
124
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
125
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
126
|
|
|
* modify what is returned to the caller. |
|
127
|
|
|
* |
|
128
|
|
|
* This option allows you to wrap global decorators |
|
129
|
|
|
* ("\Aimeos\Client\Html\Common\Decorator\*") around the html client. |
|
130
|
|
|
* |
|
131
|
|
|
* client/html/account/download/decorators/global = array( 'decorator1' ) |
|
132
|
|
|
* |
|
133
|
|
|
* This would add the decorator named "decorator1" defined by |
|
134
|
|
|
* "\Aimeos\Client\Html\Common\Decorator\Decorator1" only to the html client. |
|
135
|
|
|
* |
|
136
|
|
|
* @param array List of decorator names |
|
137
|
|
|
* @since 2016.02 |
|
138
|
|
|
* @category Developer |
|
139
|
|
|
* @see client/html/common/decorators/default |
|
140
|
|
|
* @see client/html/account/download/decorators/excludes |
|
141
|
|
|
* @see client/html/account/download/decorators/local |
|
142
|
|
|
*/ |
|
143
|
|
|
|
|
144
|
|
|
/** client/html/account/download/decorators/local |
|
145
|
|
|
* Adds a list of local decorators only to the account download html client |
|
146
|
|
|
* |
|
147
|
|
|
* Decorators extend the functionality of a class by adding new aspects |
|
148
|
|
|
* (e.g. log what is currently done), executing the methods of the underlying |
|
149
|
|
|
* class only in certain conditions (e.g. only for logged in users) or |
|
150
|
|
|
* modify what is returned to the caller. |
|
151
|
|
|
* |
|
152
|
|
|
* This option allows you to wrap local decorators |
|
153
|
|
|
* ("\Aimeos\Client\Html\Account\Decorator\*") around the html client. |
|
154
|
|
|
* |
|
155
|
|
|
* client/html/account/download/decorators/local = array( 'decorator2' ) |
|
156
|
|
|
* |
|
157
|
|
|
* This would add the decorator named "decorator2" defined by |
|
158
|
|
|
* "\Aimeos\Client\Html\Account\Decorator\Decorator2" only to the html client. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array List of decorator names |
|
161
|
|
|
* @since 2016.02 |
|
162
|
|
|
* @category Developer |
|
163
|
|
|
* @see client/html/common/decorators/default |
|
164
|
|
|
* @see client/html/account/download/decorators/excludes |
|
165
|
|
|
* @see client/html/account/download/decorators/global |
|
166
|
|
|
*/ |
|
167
|
|
|
return $this->createSubClient( 'account/download/' . $type, $name ); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Processes the input, e.g. store given values. |
|
173
|
|
|
* |
|
174
|
|
|
* A view must be available and this method doesn't generate any output |
|
175
|
|
|
* besides setting view variables if necessary. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function process() |
|
178
|
|
|
{ |
|
179
|
|
|
$context = $this->getContext(); |
|
180
|
|
|
|
|
181
|
|
|
try |
|
182
|
|
|
{ |
|
183
|
|
|
$view = $this->getView(); |
|
184
|
|
|
$id = $view->param( 'dl_id' ); |
|
185
|
|
|
|
|
186
|
|
|
/** client/html/account/download/error/url/target |
|
187
|
|
|
* Destination of the URL to redirect the customer if the file download isn't allowed |
|
188
|
|
|
* |
|
189
|
|
|
* The destination can be a page ID like in a content management system or the |
|
190
|
|
|
* module of a software development framework. This "target" must contain or know |
|
191
|
|
|
* the controller that should be called by the generated URL. |
|
192
|
|
|
* |
|
193
|
|
|
* @param string Destination of the URL |
|
194
|
|
|
* @since 2019.04 |
|
195
|
|
|
* @category Developer |
|
196
|
|
|
*/ |
|
197
|
|
|
$target = $context->getConfig()->get( 'client/html/account/download/error/url/target' ); |
|
198
|
|
|
|
|
199
|
|
|
if( $this->checkAccess( $id ) === false ) { |
|
200
|
|
|
return $view->response()->withStatus( 401 )->withHeader( 'Location', $view->url( $target ) ); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$manager = \Aimeos\MShop::create( $context, 'order/base/product/attribute' ); |
|
204
|
|
|
$item = $manager->get( $id ); |
|
205
|
|
|
|
|
206
|
|
|
if( $this->checkDownload( $id ) === false ) { |
|
207
|
|
|
return $view->response()->withStatus( 403 )->withHeader( 'Location', $view->url( $target ) ); |
|
208
|
|
|
} else { |
|
209
|
|
|
$this->addDownload( $item ); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
parent::process(); |
|
213
|
|
|
} |
|
214
|
|
|
catch( \Exception $e ) |
|
215
|
|
|
{ |
|
216
|
|
|
$this->logException( $e ); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Returns the list of sub-client names configured for the client. |
|
223
|
|
|
* |
|
224
|
|
|
* @return array List of HTML client names |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function getSubClientNames() : array |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->getContext()->getConfig()->get( $this->subPartPath, $this->subPartNames ); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Adds the necessary headers and the download content to the reponse object |
|
234
|
|
|
* |
|
235
|
|
|
* @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item Order product attribute item with file reference |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function addDownload( \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item ) |
|
238
|
|
|
{ |
|
239
|
|
|
$fs = $this->getContext()->getFilesystemManager()->get( 'fs-secure' ); |
|
240
|
|
|
$response = $this->getView()->response(); |
|
241
|
|
|
$value = (string) $item->getValue(); |
|
242
|
|
|
|
|
243
|
|
|
if( $fs->has( $value ) ) |
|
244
|
|
|
{ |
|
245
|
|
|
$name = $item->getName(); |
|
246
|
|
|
|
|
247
|
|
|
if( pathinfo( $name, PATHINFO_EXTENSION ) == null |
|
248
|
|
|
&& ( $ext = pathinfo( $value, PATHINFO_EXTENSION ) ) != null |
|
249
|
|
|
) { |
|
250
|
|
|
$name .= '.' . $ext; |
|
|
|
|
|
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$response->withHeader( 'Content-Description', 'File Transfer' ); |
|
254
|
|
|
$response->withHeader( 'Content-Type', 'application/octet-stream' ); |
|
255
|
|
|
$response->withHeader( 'Content-Disposition', 'attachment; filename="' . $name . '"' ); |
|
256
|
|
|
$response->withHeader( 'Content-Length', (string) $fs->size( $value ) ); |
|
257
|
|
|
$response->withHeader( 'Cache-Control', 'must-revalidate' ); |
|
258
|
|
|
$response->withHeader( 'Pragma', 'private' ); |
|
259
|
|
|
$response->withHeader( 'Expires', '0' ); |
|
260
|
|
|
|
|
261
|
|
|
$response->withBody( $response->createStream( $fs->reads( $value ) ) ); |
|
262
|
|
|
} |
|
263
|
|
|
elseif( filter_var( $value, FILTER_VALIDATE_URL ) !== false ) |
|
264
|
|
|
{ |
|
265
|
|
|
$response->withHeader( 'Location', $value ); |
|
266
|
|
|
$response->withStatus( 303 ); |
|
267
|
|
|
} |
|
268
|
|
|
else |
|
269
|
|
|
{ |
|
270
|
|
|
$response->withStatus( 404 ); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Checks if the customer is allowed to download the file |
|
277
|
|
|
* |
|
278
|
|
|
* @param string|null $id Unique order base product attribute ID referencing the download file |
|
279
|
|
|
* @return bool True if download is allowed, false if not |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function checkAccess( string $id = null ) : bool |
|
282
|
|
|
{ |
|
283
|
|
|
$context = $this->getContext(); |
|
284
|
|
|
|
|
285
|
|
|
if( ( $customerId = $context->getUserId() ) !== null && $id !== null ) |
|
286
|
|
|
{ |
|
287
|
|
|
$manager = \Aimeos\MShop::create( $context, 'order/base' ); |
|
288
|
|
|
|
|
289
|
|
|
$search = $manager->filter(); |
|
290
|
|
|
$expr = array( |
|
291
|
|
|
$search->compare( '==', 'order.base.customerid', $customerId ), |
|
292
|
|
|
$search->compare( '==', 'order.base.product.attribute.id', $id ), |
|
293
|
|
|
); |
|
294
|
|
|
$search->setConditions( $search->and( $expr ) ); |
|
295
|
|
|
$search->slice( 0, 1 ); |
|
296
|
|
|
|
|
297
|
|
|
if( !$manager->search( $search )->isEmpty() ) { |
|
298
|
|
|
return true; |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return false; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Updates the download counter for the downloaded file |
|
308
|
|
|
* |
|
309
|
|
|
* @param string|null $id Unique order base product attribute ID referencing the download file |
|
310
|
|
|
* @return bool True if download is allowed, false if not |
|
311
|
|
|
*/ |
|
312
|
|
|
protected function checkDownload( string $id = null ) : bool |
|
313
|
|
|
{ |
|
314
|
|
|
$context = $this->getContext(); |
|
315
|
|
|
|
|
316
|
|
|
/** client/html/account/download/maxcount |
|
317
|
|
|
* Maximum number of file downloads allowed for an ordered product |
|
318
|
|
|
* |
|
319
|
|
|
* This configuration setting enables you to limit the number of downloads |
|
320
|
|
|
* of a product download file. The count is the maximum number for each |
|
321
|
|
|
* bought product and customer, i.e. setting the count to "3" allows |
|
322
|
|
|
* a customer to download the bought product file up to three times. |
|
323
|
|
|
* |
|
324
|
|
|
* The default value of null enforces no limit. |
|
325
|
|
|
* |
|
326
|
|
|
* @param integer Maximum number of downloads |
|
327
|
|
|
* @since 2016.02 |
|
328
|
|
|
* @category Developer |
|
329
|
|
|
* @category User |
|
330
|
|
|
*/ |
|
331
|
|
|
$maxcnt = $context->getConfig()->get( 'client/html/account/download/maxcount' ); |
|
332
|
|
|
|
|
333
|
|
|
$cntl = \Aimeos\Controller\Frontend::create( $context, 'customer' ); |
|
334
|
|
|
$item = $cntl->uses( ['order' => ['download']] )->get(); |
|
335
|
|
|
|
|
336
|
|
|
if( ( $listItem = $item->getListItem( 'order', 'download', $id ) ) === null ) { |
|
337
|
|
|
$listItem = $cntl->createListItem()->setType( 'download' )->setRefId( $id ); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
$config = $listItem->getConfig(); |
|
341
|
|
|
$count = (int) $listItem->getConfigValue( 'count', 0 ); |
|
342
|
|
|
|
|
343
|
|
|
if( $maxcnt === null || $count < $maxcnt ) |
|
344
|
|
|
{ |
|
345
|
|
|
$config['count'] = $count++; |
|
346
|
|
|
$cntl->addListItem( 'order', $listItem->setConfig( $config ) )->store(); |
|
347
|
|
|
|
|
348
|
|
|
return true; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
return false; |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
|