Skip to content

Commit 832cd16

Browse files
committed
code cleanup
1 parent cc81bb5 commit 832cd16

File tree

1 file changed

+64
-65
lines changed

1 file changed

+64
-65
lines changed

action.php

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
11
<?php
2+
23
/**
34
* DokuWiki Plugin imgpaste (Action Component)
45
*
56
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
67
* @author Andreas Gohr <[email protected]>
78
*/
9+
class action_plugin_imgpaste extends DokuWiki_Action_Plugin
10+
{
811

9-
// must be run within Dokuwiki
10-
if(!defined('DOKU_INC')) die();
11-
12-
if(!defined('DOKU_LF')) define('DOKU_LF', "\n");
13-
if(!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14-
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15-
16-
require_once DOKU_PLUGIN . 'action.php';
17-
18-
class action_plugin_imgpaste extends DokuWiki_Action_Plugin {
19-
20-
private $tempdir = '';
12+
private $tempdir = '';
2113
private $tempfile = '';
2214

23-
public function register(Doku_Event_Handler $controller) {
24-
25-
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
26-
15+
/** @inheritdoc */
16+
public function register(Doku_Event_Handler $controller)
17+
{
18+
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handleAjaxUpload');
2719
}
2820

29-
public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
30-
if($event->data != 'plugin_imgpaste') return;
21+
/**
22+
* Creates a new file from the given data URL
23+
*
24+
* @param Doku_Event $event AJAX_CALL_UNKNOWN
25+
*/
26+
public function handleAjaxUpload(Doku_Event $event)
27+
{
28+
if ($event->data != 'plugin_imgpaste') return;
3129
global $lang;
3230

3331
// get data
3432
global $INPUT;
3533
$data = $INPUT->post->str('data');
3634
list($type, $data) = explode(';', $data);
37-
if(!$data) $this->fail(400, $this->getLang('e_nodata'));
35+
if (!$data) $this->fail(400, $this->getLang('e_nodata'));
3836

3937
// process data encoding
4038
$type = strtolower(substr($type, 5)); // strip 'data:' prefix
@@ -43,58 +41,58 @@ public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
4341

4442
// check for supported mime type
4543
$mimetypes = array_flip(getMimeTypes());
46-
if(!isset($mimetypes[$type])) $this->fail(415, $lang['uploadwrong']);
44+
if (!isset($mimetypes[$type])) $this->fail(415, $lang['uploadwrong']);
4745

4846
// prepare file names
4947
$tempname = $this->storetemp($data);
5048
$filename = $this->getConf('filename');
5149
$filename = str_replace(
52-
array(
53-
'@NS@',
54-
'@ID@',
55-
'@USER@',
56-
'@PAGE@'
57-
),
58-
array(
59-
getNS($INPUT->post->str('id')),
60-
$INPUT->post->str('id'),
61-
$_SERVER['REMOTE_USER'],
62-
noNS($INPUT->post->str('id'))
63-
),
64-
$filename
65-
);
66-
$filename = strftime($filename);
67-
$filename .= '.'.$mimetypes[$type];
50+
[
51+
'@NS@',
52+
'@ID@',
53+
'@USER@',
54+
'@PAGE@',
55+
],
56+
[
57+
getNS($INPUT->post->str('id')),
58+
$INPUT->post->str('id'),
59+
$_SERVER['REMOTE_USER'],
60+
noNS($INPUT->post->str('id')),
61+
],
62+
$filename
63+
);
64+
$filename = strftime($filename);
65+
$filename .= '.' . $mimetypes[$type];
6866
$filename = cleanID($filename);
6967

7068
// check ACLs
7169
$auth = auth_quickaclcheck($filename);
72-
if($auth < AUTH_UPLOAD) $this->fail(403, $lang['uploadfail']);
70+
if ($auth < AUTH_UPLOAD) $this->fail(403, $lang['uploadfail']);
7371

7472
// do the actual saving
7573
$result = media_save(
76-
array(
77-
'name' => $tempname,
78-
'mime' => $type,
79-
'ext' => $mimetypes[$type]
80-
),
81-
$filename,
82-
false,
83-
$auth,
84-
'copy'
74+
array(
75+
'name' => $tempname,
76+
'mime' => $type,
77+
'ext' => $mimetypes[$type],
78+
),
79+
$filename,
80+
false,
81+
$auth,
82+
'copy'
8583
);
86-
if(is_array($result)) $this->fail(500, $result[0]);
84+
if (is_array($result)) $this->fail(500, $result[0]);
8785

8886
//Still here? We had a successful upload
8987
$this->clean();
9088
header('Content-Type: application/json');
91-
$json = new JSON();
92-
echo $json->encode(
93-
array(
94-
'message' => $lang['uploadsucc'],
95-
'id' => $result
96-
)
97-
);
89+
echo json_encode([
90+
'message' => $lang['uploadsucc'],
91+
'id' => $result,
92+
'mime' => $type,
93+
'ext' => $mimetypes[$type],
94+
'url' => ml($result),
95+
]);
9896

9997
$event->preventDefault();
10098
$event->stopPropagation();
@@ -108,21 +106,23 @@ public function handle_ajax_call_unknown(Doku_Event &$event, $param) {
108106
* @param $data
109107
* @return string
110108
*/
111-
private function storetemp($data){
109+
private function storetemp($data)
110+
{
112111
// store in temporary file
113-
$this->tempdir = io_mktmpdir();
114-
if(!$this->tempdir) $this->fail(500);
115-
$this->tempfile = $this->tempdir.'/'.md5($data);
116-
if(!io_saveFile($this->tempfile, $data)) $this->fail(500);
112+
$this->tempdir = io_mktmpdir();
113+
if (!$this->tempdir) $this->fail(500);
114+
$this->tempfile = $this->tempdir . '/' . md5($data);
115+
if (!io_saveFile($this->tempfile, $data)) $this->fail(500);
117116
return $this->tempfile;
118117
}
119118

120119
/**
121120
* remove temporary file and directory
122121
*/
123-
private function clean(){
124-
if($this->tempfile && file_exists($this->tempfile)) @unlink($this->tempfile);
125-
if($this->tempdir && is_dir($this->tempdir)) @rmdir($this->tempdir);
122+
private function clean()
123+
{
124+
if ($this->tempfile && file_exists($this->tempfile)) @unlink($this->tempfile);
125+
if ($this->tempdir && is_dir($this->tempdir)) @rmdir($this->tempdir);
126126
$this->tempfile = '';
127127
$this->tempdir = '';
128128
}
@@ -135,11 +135,10 @@ private function clean(){
135135
* @param int $status HTTP status code
136136
* @param string $text
137137
*/
138-
private function fail($status, $text=''){
138+
private function fail($status, $text = '')
139+
{
139140
$this->clean();
140141
http_status($status, $text);
141142
exit;
142143
}
143144
}
144-
145-
// vim:ts=4:sw=4:et:

0 commit comments

Comments
 (0)