1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ContentsFile\Controller; |
4
|
|
|
|
5
|
|
|
use Cake\Core\Configure; |
6
|
|
|
use Cake\ORM\TableRegistry; |
7
|
|
|
use Cake\Utility\Inflector; |
8
|
|
|
use Cake\Http\Exception\NotFoundException; |
9
|
|
|
use ContentsFile\Controller\AppController; |
10
|
|
|
use ContentsFile\Controller\Traits\NormalContentsFileControllerTrait; |
11
|
|
|
use ContentsFile\Controller\Traits\S3ContentsFileControllerTrait; |
12
|
|
|
|
13
|
|
|
class ContentsFileController extends AppController |
14
|
|
|
{ |
15
|
|
|
use S3ContentsFileControllerTrait; |
16
|
|
|
use NormalContentsFileControllerTrait; |
17
|
|
|
private $baseModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* initialize |
21
|
|
|
* Configureの最後のスラッシュの設定 |
22
|
|
|
*/ |
23
|
|
|
public function initialize(): void |
24
|
|
|
{ |
25
|
|
|
parent::initialize(); |
26
|
|
|
// /が最後についていない場合はつける |
27
|
|
|
if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.Normal.tmpDir'))) { |
28
|
|
|
Configure::write('ContentsFile.Setting.Normal.tmpDir', Configure::read('ContentsFile.Setting.Normal.tmpDir') . '/'); |
29
|
|
|
} |
30
|
|
|
if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.Normal.fileDir'))) { |
31
|
|
|
Configure::write('ContentsFile.Setting.Normal.fileDir', Configure::read('ContentsFile.Setting.Normal.fileDir') . '/'); |
32
|
|
|
} |
33
|
|
|
if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.tmpDir'))) { |
34
|
|
|
Configure::write('ContentsFile.Setting.S3.tmpDir', Configure::read('ContentsFile.Setting.S3.tmpDir') . '/'); |
35
|
|
|
} |
36
|
|
|
if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.fileDir'))) { |
37
|
|
|
Configure::write('ContentsFile.Setting.S3.fileDir', Configure::read('ContentsFile.Setting.S3.fileDir') . '/'); |
38
|
|
|
} |
39
|
|
|
if (!preg_match('#/$#', Configure::read('ContentsFile.Setting.S3.workingDir'))) { |
40
|
|
|
Configure::write('ContentsFile.Setting.S3.workingDir', Configure::read('ContentsFile.Setting.S3.workingDir'). '/'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* loader |
46
|
|
|
* @author hagiwara |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function loader(): void |
50
|
|
|
{ |
51
|
|
|
$this->autoRender = false; |
52
|
|
|
|
53
|
|
|
// 必要なパラメータがない場合はエラー |
54
|
|
|
if ( |
55
|
|
|
empty($this->request->getQuery('model')) || |
56
|
|
|
empty($this->request->getQuery('field_name')) |
57
|
|
|
) { |
58
|
|
|
throw new NotFoundException('404 error'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//Entityに接続して設定値を取得 |
62
|
|
|
$this->baseModel = TableRegistry::getTableLocator()->get($this->request->getQuery('model')); |
|
|
|
|
63
|
|
|
|
64
|
|
|
// このレベルで切り出す |
65
|
|
|
$fieldName = $this->request->getQuery('field_name'); |
66
|
|
|
$filename = ''; |
67
|
|
|
$filepath = ''; |
68
|
|
|
if (!empty($this->request->getQuery('tmp_file_name'))) { |
69
|
|
|
$filename = $this->request->getQuery('tmp_file_name'); |
70
|
|
|
$filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'TmpFilePath'}($filename); |
71
|
|
|
Configure::read('ContentsFile.Setting.Normal.tmpDir') . $filename; |
72
|
|
|
} elseif (!empty($this->request->getQuery('model_id'))) { |
73
|
|
|
//表示条件をチェックする |
74
|
|
|
$checkMethodName = 'contentsFileCheck' . Inflector::camelize($fieldName); |
|
|
|
|
75
|
|
|
if (method_exists($this->baseModel, $checkMethodName)) { |
76
|
|
|
//エラーなどの処理はTableに任せる |
77
|
|
|
$this->baseModel->{$checkMethodName}($this->request->getQuery('model_id')); |
78
|
|
|
} |
79
|
|
|
//attachementからデータを取得 |
80
|
|
|
$attachmentModel = TableRegistry::getTableLocator()->get('Attachments'); |
81
|
|
|
$attachmentData = $attachmentModel->find('all') |
82
|
|
|
->where(['model' => $this->request->getQuery('model')]) |
83
|
|
|
->where(['model_id' => $this->request->getQuery('model_id')]) |
84
|
|
|
->where(['field_name' => $this->request->getQuery('field_name')]) |
85
|
|
|
->first() |
86
|
|
|
; |
87
|
|
|
if (empty($attachmentData)) { |
88
|
|
|
throw new NotFoundException('404 error'); |
89
|
|
|
} |
90
|
|
|
$filename = $attachmentData->file_name; |
91
|
|
|
$filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'FilePath'}($attachmentData); |
92
|
|
|
|
93
|
|
|
//通常のセットの時のみresize設定があれば見る |
94
|
|
|
if (!empty($this->request->getQuery('resize'))) { |
95
|
|
|
$filepath = $this->{Configure::read('ContentsFile.Setting.type') . 'ResizeSet'}($filepath, $this->request->getQuery('resize')); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->fileDownloadHeader($filename); |
100
|
|
|
$this->{Configure::read('ContentsFile.Setting.type') . 'Loader'}($filename, $filepath); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* getFileType |
105
|
|
|
* @author hagiwara |
106
|
|
|
* @param string $ext |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
private function getFileType($ext): string |
110
|
|
|
{ |
111
|
|
|
$aContentTypes = [ |
112
|
|
|
'txt'=>'text/plain', |
113
|
|
|
'htm'=>'text/html', |
114
|
|
|
'html'=>'text/html', |
115
|
|
|
'jpg'=>'image/jpeg', |
116
|
|
|
'jpeg'=>'image/jpeg', |
117
|
|
|
'gif'=>'image/gif', |
118
|
|
|
'png'=>'image/png', |
119
|
|
|
'bmp'=>'image/x-bmp', |
120
|
|
|
'ai'=>'application/postscript', |
121
|
|
|
'psd'=>'image/x-photoshop', |
122
|
|
|
'eps'=>'application/postscript', |
123
|
|
|
'pdf'=>'application/pdf', |
124
|
|
|
'swf'=>'application/x-shockwave-flash', |
125
|
|
|
'lzh'=>'application/x-lha-compressed', |
126
|
|
|
'zip'=>'application/x-zip-compressed', |
127
|
|
|
'sit'=>'application/x-stuffit', |
128
|
|
|
'mp3' => 'audio/mpeg', |
129
|
|
|
'wav' => 'audio/wav', |
130
|
|
|
'mp4' => 'video/mp4', |
131
|
|
|
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
132
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
133
|
|
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
134
|
|
|
]; |
135
|
|
|
$sContentType = 'application/octet-stream'; |
136
|
|
|
|
137
|
|
|
if (!empty($aContentTypes[$ext])) { |
138
|
|
|
$sContentType = $aContentTypes[$ext]; |
139
|
|
|
} |
140
|
|
|
return $sContentType; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* getMimeType |
145
|
|
|
* @author hagiwara |
146
|
|
|
* @param string $filename |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
private function getMimeType(string $filename): string |
150
|
|
|
{ |
151
|
|
|
$aContentTypes = [ |
152
|
|
|
'txt'=>'text/plain', |
153
|
|
|
'htm'=>'text/html', |
154
|
|
|
'html'=>'text/html', |
155
|
|
|
'jpg'=>'image/jpeg', |
156
|
|
|
'jpeg'=>'image/jpeg', |
157
|
|
|
'gif'=>'image/gif', |
158
|
|
|
'png'=>'image/png', |
159
|
|
|
'bmp'=>'image/x-bmp', |
160
|
|
|
'ai'=>'application/postscript', |
161
|
|
|
'psd'=>'image/x-photoshop', |
162
|
|
|
'eps'=>'application/postscript', |
163
|
|
|
'pdf'=>'application/pdf', |
164
|
|
|
'swf'=>'application/x-shockwave-flash', |
165
|
|
|
'lzh'=>'application/x-lha-compressed', |
166
|
|
|
'zip'=>'application/x-zip-compressed', |
167
|
|
|
'sit'=>'application/x-stuffit', |
168
|
|
|
'mp3' => 'audio/mpeg', |
169
|
|
|
'wav' => 'audio/wav', |
170
|
|
|
'mp4' => 'video/mp4', |
171
|
|
|
'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
172
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
173
|
|
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
174
|
|
|
]; |
175
|
|
|
$sContentType = 'application/octet-stream'; |
176
|
|
|
|
177
|
|
|
if (($pos = strrpos($filename, ".")) !== false) { |
178
|
|
|
// 拡張子がある場合 |
179
|
|
|
$ext = strtolower(substr($filename, $pos + 1)); |
180
|
|
|
if (strlen($ext)) { |
181
|
|
|
return array_key_exists($ext, $aContentTypes) ? $aContentTypes[$ext] : $sContentType; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
return $sContentType; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* fileDownloadHeader |
189
|
|
|
* @author hagiwara |
190
|
|
|
* @param string $filename |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
|
private function fileDownloadHeader(string $filename): void |
194
|
|
|
{ |
195
|
|
|
// loaderよりダウンロードするかどうか |
196
|
|
|
if (!empty($this->request->getQuery('download')) && $this->request->getQuery('download') == true) { |
197
|
|
|
// IE/Edge対応 |
198
|
|
|
if (strstr(env('HTTP_USER_AGENT'), 'MSIE') || strstr(env('HTTP_USER_AGENT'), 'Trident') || strstr(env('HTTP_USER_AGENT'), 'Edge')) { |
199
|
|
|
$filename = rawurlencode($filename); |
200
|
|
|
} |
201
|
|
|
$this->response->withDownload($filename); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.