Skip to content

Commit 584f463

Browse files
committed
More cleanup
1 parent 832cd16 commit 584f463

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

action.php

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99
class action_plugin_imgpaste extends DokuWiki_Action_Plugin
1010
{
1111

12-
private $tempdir = '';
13-
private $tempfile = '';
12+
protected $tempdir = '';
13+
protected $tempfile = '';
14+
15+
/**
16+
* Clean up on destruction
17+
*/
18+
public function __destruct()
19+
{
20+
$this->clean();
21+
}
1422

1523
/** @inheritdoc */
1624
public function register(Doku_Event_Handler $controller)
@@ -45,37 +53,19 @@ public function handleAjaxUpload(Doku_Event $event)
4553

4654
// prepare file names
4755
$tempname = $this->storetemp($data);
48-
$filename = $this->getConf('filename');
49-
$filename = str_replace(
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];
66-
$filename = cleanID($filename);
56+
$filename = $this->createFileName($INPUT->post->str('id'), $mimetypes[$type], $_SERVER['REMOTE_USER']);
6757

6858
// check ACLs
6959
$auth = auth_quickaclcheck($filename);
7060
if ($auth < AUTH_UPLOAD) $this->fail(403, $lang['uploadfail']);
7161

7262
// do the actual saving
7363
$result = media_save(
74-
array(
64+
[
7565
'name' => $tempname,
7666
'mime' => $type,
7767
'ext' => $mimetypes[$type],
78-
),
68+
],
7969
$filename,
8070
false,
8171
$auth,
@@ -98,6 +88,37 @@ public function handleAjaxUpload(Doku_Event $event)
9888
$event->stopPropagation();
9989
}
10090

91+
/**
92+
* Create the filename for the new file
93+
*
94+
* @param string $pageid the original page the paste event happend on
95+
* @param string $ext the extension of the file
96+
* @param string $user the currently logged in user
97+
* @return string
98+
*/
99+
protected function createFileName($pageid, $ext, $user)
100+
{
101+
$filename = $this->getConf('filename');
102+
$filename = str_replace(
103+
[
104+
'@NS@',
105+
'@ID@',
106+
'@USER@',
107+
'@PAGE@',
108+
],
109+
[
110+
getNS($pageid),
111+
$pageid,
112+
$user,
113+
noNS($pageid),
114+
],
115+
$filename
116+
);
117+
$filename = strftime($filename);
118+
$filename .= '.' . $ext;
119+
return cleanID($filename);
120+
}
121+
101122
/**
102123
* Create a temporary file from the given data
103124
*
@@ -106,7 +127,7 @@ public function handleAjaxUpload(Doku_Event $event)
106127
* @param $data
107128
* @return string
108129
*/
109-
private function storetemp($data)
130+
protected function storetemp($data)
110131
{
111132
// store in temporary file
112133
$this->tempdir = io_mktmpdir();
@@ -119,7 +140,7 @@ private function storetemp($data)
119140
/**
120141
* remove temporary file and directory
121142
*/
122-
private function clean()
143+
protected function clean()
123144
{
124145
if ($this->tempfile && file_exists($this->tempfile)) @unlink($this->tempfile);
125146
if ($this->tempdir && is_dir($this->tempdir)) @rmdir($this->tempdir);
@@ -135,10 +156,11 @@ private function clean()
135156
* @param int $status HTTP status code
136157
* @param string $text
137158
*/
138-
private function fail($status, $text = '')
159+
protected function fail($status, $text = '')
139160
{
140161
$this->clean();
141162
http_status($status, $text);
142163
exit;
143164
}
165+
144166
}

0 commit comments

Comments
 (0)