|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Event\Event; |
|
5
|
|
|
use Cake\Filesystem\File; |
|
6
|
|
|
use Cake\Network\Exception\ForbiddenException; |
|
7
|
|
|
use Cake\Network\Exception\NotFoundException; |
|
8
|
|
|
use Cake\Network\Response; |
|
9
|
|
|
|
|
10
|
|
|
class AttachmentsController extends AppController |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* BeforeFilter handle. |
|
15
|
|
|
* |
|
16
|
|
|
* @param Event $event The beforeFilter event that was fired. |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
public function beforeFilter(Event $event) |
|
21
|
|
|
{ |
|
22
|
|
|
parent::beforeFilter($event); |
|
23
|
|
|
|
|
24
|
|
|
$this->Auth->deny(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Download an attachment realated to an article. |
|
29
|
|
|
* |
|
30
|
|
|
* @throws \Cake\Network\Exception\NotFoundException When it missing an arguments or when the file doesn't exist. |
|
31
|
|
|
* @throws \Cake\Network\Exception\ForbiddenException When the user is not premium. |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Cake\Network\Exception\ForbiddenException |
|
34
|
|
|
* \Cake\Network\Exception\NotFoundException |
|
35
|
|
|
* \Cake\Network\Response |
|
36
|
|
|
*/ |
|
37
|
|
|
public function download() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->loadModel('Users'); |
|
40
|
|
|
|
|
41
|
|
|
$user = $this->Users |
|
|
|
|
|
|
42
|
|
|
->find() |
|
43
|
|
|
->where([ |
|
44
|
|
|
'Users.id' => $this->request->session()->read('Auth.User.id') |
|
45
|
|
|
]) |
|
46
|
|
|
->contain([ |
|
47
|
|
|
'Groups' => function ($q) { |
|
48
|
|
|
return $q->select(['id', 'is_staff']); |
|
49
|
|
|
} |
|
50
|
|
|
]) |
|
51
|
|
|
->first(); |
|
52
|
|
|
|
|
53
|
|
|
if (is_null($user)) { |
|
54
|
|
|
throw new ForbiddenException(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!isset($this->request->type)) { |
|
58
|
|
|
throw new NotFoundException(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
switch ($this->request->type) { |
|
62
|
|
|
case "blog": |
|
63
|
|
|
if (!$user->premium && !$user->group->is_staff) { |
|
64
|
|
|
throw new ForbiddenException(); |
|
65
|
|
|
} |
|
66
|
|
|
$this->loadModel('BlogAttachments'); |
|
67
|
|
|
|
|
68
|
|
|
$attachment = $this->BlogAttachments->get($this->request->id); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
if (!$attachment) { |
|
71
|
|
|
throw new NotFoundException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$file = new File($attachment->url); |
|
75
|
|
|
|
|
76
|
|
|
if (!$file->exists()) { |
|
77
|
|
|
throw new NotFoundException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->response->file( |
|
81
|
|
|
$file->path, |
|
82
|
|
|
['download' => true, 'name' => $attachment->name] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
$this->BlogAttachments->patchEntity($attachment, ['download' => $attachment->download + 1]); |
|
|
|
|
|
|
86
|
|
|
$this->BlogAttachments->save($attachment); |
|
|
|
|
|
|
87
|
|
|
break; |
|
88
|
|
|
|
|
89
|
|
|
default: |
|
90
|
|
|
throw new NotFoundException(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->response; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.