forked from cosmocode/dokuwiki-plugin-imgpaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
145 lines (124 loc) · 4.28 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
<?php
/**
* DokuWiki Plugin imgpaste (Action Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_LF')) define('DOKU_LF', "\n");
if(!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
require_once DOKU_PLUGIN . 'action.php';
class action_plugin_imgpaste extends DokuWiki_Action_Plugin {
private $tempdir = '';
private $tempfile = '';
public function register(Doku_Event_Handler $controller) {
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
}
public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
if($event->data != 'plugin_imgpaste') return;
global $lang;
// get data
global $INPUT;
$data = $INPUT->post->str('data');
list($type, $data) = explode(';', $data);
if(!$data) $this->fail(400, $this->getLang('e_nodata'));
// process data encoding
$type = strtolower(substr($type, 5)); // strip 'data:' prefix
$data = substr($data, 7); // strip 'base64,' prefix
$data = base64_decode($data);
// check for supported mime type
$mimetypes = array_flip(getMimeTypes());
if(!isset($mimetypes[$type])) $this->fail(415, $lang['uploadwrong']);
// prepare file names
$tempname = $this->storetemp($data);
$filename = $this->getConf('filename');
$filename = str_replace(
array(
'@NS@',
'@ID@',
'@USER@',
'@PAGE@'
),
array(
getNS($INPUT->post->str('id')),
$INPUT->post->str('id'),
$_SERVER['REMOTE_USER'],
noNS($INPUT->post->str('id'))
),
$filename
);
$filename = strftime($filename);
$filename .= '.'.$mimetypes[$type];
$filename = cleanID($filename);
// check ACLs
$auth = auth_quickaclcheck($filename);
if($auth < AUTH_UPLOAD) $this->fail(403, $lang['uploadfail']);
// do the actual saving
$result = media_save(
array(
'name' => $tempname,
'mime' => $type,
'ext' => $mimetypes[$type]
),
$filename,
false,
$auth,
'copy'
);
if(is_array($result)) $this->fail(500, $result[0]);
//Still here? We had a successful upload
$this->clean();
header('Content-Type: application/json');
$json = new JSON();
echo $json->encode(
array(
'message' => $lang['uploadsucc'],
'id' => $result
)
);
$event->preventDefault();
$event->stopPropagation();
}
/**
* Create a temporary file from the given data
*
* exits if an error occurs
*
* @param $data
* @return string
*/
private function storetemp($data){
// store in temporary file
$this->tempdir = io_mktmpdir();
if(!$this->tempdir) $this->fail(500);
$this->tempfile = $this->tempdir.'/'.md5($data);
if(!io_saveFile($this->tempfile, $data)) $this->fail(500);
return $this->tempfile;
}
/**
* remove temporary file and directory
*/
private 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 = '';
}
/**
* End the execution with a HTTP error code
*
* Calls clean
*
* @param int $status HTTP status code
* @param string $text
*/
private function fail($status, $text=''){
$this->clean();
http_status($status, $text);
exit;
}
}
// vim:ts=4:sw=4:et: