-
Notifications
You must be signed in to change notification settings - Fork 10
/
action.php
211 lines (188 loc) · 5.82 KB
/
action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
use dokuwiki\plugin\imgpaste\Exception as PasteException;
/**
* DokuWiki Plugin imgpaste (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
class action_plugin_imgpaste extends ActionPlugin
{
protected $tempdir = '';
protected $tempfile = '';
/**
* Clean up on destruction
*/
public function __destruct()
{
$this->clean();
}
/** @inheritdoc */
public function register(EventHandler $controller)
{
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxUpload');
}
/**
* Creates a new file from the given data URL
*
* @param Event $event AJAX_CALL_UNKNOWN
*/
public function handleAjaxUpload(Event $event)
{
if ($event->data != 'plugin_imgpaste') return;
$event->preventDefault();
$event->stopPropagation();
global $INPUT;
try {
if ($INPUT->has('url')) {
[$data, $type] = $this->externalUrlToData($INPUT->post->str('url'));
} else {
[$data, $type] = $this->dataUrlToData($INPUT->post->str('data'));
}
$result = $this->storeImage($data, $type);
} catch (PasteException $e) {
$this->clean();
http_status($e->getCode(), $e->getMessage());
exit;
}
header('Content-Type: application/json');
echo json_encode($result);
}
/**
* Get the binary data and mime type from a data URL
*
* @param string $dataUrl
* @return array [data, type]
* @throws PasteException
*/
protected function dataUrlToData($dataUrl)
{
list($type, $data) = explode(';', $dataUrl);
if (!$data) throw new PasteException($this->getLang('e_nodata'), 400);
// process data encoding
$type = strtolower(substr($type, 5)); // strip 'data:' prefix
$data = substr($data, 7); // strip 'base64,' prefix
$data = base64_decode($data);
return [$data, $type];
}
/**
* Download the file from an external URL
*
* @param string $externalUrl
* @return array [data, type]
* @throws PasteException
*/
protected function externalUrlToData($externalUrl)
{
global $lang;
// download the file
$http = new \dokuwiki\HTTP\DokuHTTPClient();
$data = $http->get($externalUrl);
if (!$data) throw new PasteException($lang['uploadfail'], 500);
[$type] = explode(';', $http->resp_headers['content-type']);
return [$data, $type];
}
/**
* @throws PasteException
*/
protected function storeImage($data, $type)
{
global $lang;
global $INPUT;
// check for supported mime type
$mimetypes = array_flip(getMimeTypes());
if (!isset($mimetypes[$type])) throw new PasteException($lang['uploadwrong'], 415);
// prepare file names
$tempname = $this->storetemp($data);
$filename = $this->createFileName($INPUT->post->str('id'), $mimetypes[$type], $_SERVER['REMOTE_USER']);
// check ACLs
$auth = auth_quickaclcheck($filename);
if ($auth < AUTH_UPLOAD) throw new PasteException($lang['uploadfail'], 403);
// do the actual saving
$result = media_save(
[
'name' => $tempname,
'mime' => $type,
'ext' => $mimetypes[$type],
],
$filename,
false,
$auth,
'copy'
);
if (is_array($result)) throw new PasteException($result[0], 500);
//Still here? We had a successful upload
$this->clean();
return [
'message' => $lang['uploadsucc'],
'id' => $result,
'mime' => $type,
'ext' => $mimetypes[$type],
'url' => ml($result),
];
}
/**
* Create the filename for the new file
*
* @param string $pageid the original page the paste event happend on
* @param string $ext the extension of the file
* @param string $user the currently logged in user
* @return string
*/
protected function createFileName($pageid, $ext, $user)
{
$unique = '';
$filename = $this->getConf('filename');
$filename = str_replace(
[
'@NS@',
'@ID@',
'@USER@',
'@PAGE@',
],
[
getNS($pageid),
$pageid,
$user,
noNS($pageid),
],
$filename
);
$filename = strftime($filename);
$filename = cleanID($filename);
while (media_exists($filename . $unique . '.' . $ext)) {
$unique = (int)$unique + 1;
}
return $filename . $unique . '.' . $ext;
}
/**
* Create a temporary file from the given data
*
* exits if an error occurs
*
* @param $data
* @return string
*/
protected function storetemp($data)
{
// store in temporary file
$this->tempdir = io_mktmpdir();
if (!$this->tempdir) throw new PasteException('', 500);
$this->tempfile = $this->tempdir . '/' . md5($data);
if (!io_saveFile($this->tempfile, $data)) throw new PasteException('', 500);
return $this->tempfile;
}
/**
* remove temporary file and directory
*/
protected function clean()
{
if ($this->tempfile && file_exists($this->tempfile)) @unlink($this->tempfile);
if ($this->tempdir && is_dir($this->tempdir)) @rmdir($this->tempdir);
$this->tempfile = '';
$this->tempdir = '';
}
}