-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.php
More file actions
194 lines (160 loc) · 6.3 KB
/
plugin.php
File metadata and controls
194 lines (160 loc) · 6.3 KB
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
<?php
/**
* Multiple attachment plugin files, contains hook callback and pretty much everything
*
* @package Wedgeward:MassAttach
* @author Shitiz "Dragooon" Garg <Email mail@dragooon.net> <Url http://smf-media.com> (and Nao)
* @copyright 2012, Shitiz "Dragooon" Garg <mail@dragooon.net>
* @license
* Licensed under "New BSD License (3-clause version)"
* http://www.opensource.org/licenses/BSD-3-Clause
*
* @version 1.0
*/
/**
* Callback for hook post_form_pre, loads the javascript for manipulating the upload via AJAX
*
* @return void
*/
function massattach_post_form_pre()
{
global $context, $board, $topic, $txt, $settings;
if (!allowedTo('post_attachment'))
return;
loadLanguage('Errors');
$current_attach_dir = get_attach_dir();
$total_size = 0;
if (!isset($_SESSION['temp_attachments']))
$_SESSION['temp_attachments'] = array();
foreach ($_SESSION['temp_attachments'] as $attach => $filename)
$total_size += filesize($current_attach_dir . '/' . $attach);
add_plugin_js_file('Wedgeward:MassAttach', 'attachui.js');
add_js('
attachOpts = {
sizeLimit: ', $settings['attachmentSizeLimit'], ',
totalSizeLimit: ', $settings['attachmentPostLimit'], ',
maxNum: ', $settings['attachmentNumPerPostLimit'], ',
currentNum: ', count($_SESSION['temp_attachments']), ',
checkExtension: ', !empty($settings['attachmentCheckExtensions']) ? 'true' : 'false', ',
validExtensions: ', JavaScriptEscape($settings['attachmentExtensions']), '.split(","),
totalSize: ', round($total_size / 1024), ',
ext_error: ', JavaScriptEscape(str_replace('{attach_exts}', strtr($settings['attachmentExtensions'], array(',' => ', ')), $txt['cannot_attach_ext'])), ',
filesize_error: ', sprintf(JavaScriptEscape($txt['file_too_big']), $settings['attachmentSizeLimit']), ',
maxNum_error: ', sprintf(JavaScriptEscape($txt['attachments_limit_per_post']), $settings['attachmentNumPerPostLimit']), ',
totalFilesize_error: ', sprintf(JavaScriptEscape($txt['file_too_big']), $settings['attachmentPostLimit']), '
};');
}
/**
* Action handler for massattach, handles uploading of files via AJAX
* A fair amount of this is borrowed from Post.php
*
* @return void
*/
function massattach()
{
global $settings, $topic, $board, $options, $language, $board, $context;
header('Content-type: text/plain; charset=utf-8');
// No board?
if (empty($board) && empty($context['allow_no_board']))
massattach_error('no_board');
// Not allowed to post attachments?
if (!allowedTo('post_attachment'))
massattach_error('permission_denied');
$current_attach_dir = get_attach_dir();
$stream = fopen('php://input', 'r');
$filename = isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : '';
if (empty($filename) || !is_writable($current_attach_dir))
massattach_error('invalid_filename');
// Check for extensions
if (!empty($settings['attachmentCheckExtensions']))
if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), explode(',', strtolower($settings['attachmentExtensions']))))
massattach_error($txt['cant_upload_type'] . ' ' . $settings['attachmentExtensions']);
if (!isset($_SESSION['temp_attachments']))
$_SESSION['temp_attachments'] = array();
$attachID = 'post_tmp_' . we::$id . '_' . (count($_SESSION['temp_attachments']) + 1);
$dest = $current_attach_dir . '/' . $attachID;
$target = fopen($dest, 'w');
stream_copy_to_stream($stream, $target);
fclose($target);
// Make sure the size declared by the browser is same as the one we received.
// This is mostly because on abort the request seems to be dumped into the
// script, if there's a difference of filesize there's a good chance it was
// an abort
if (filesize($dest) < $_SERVER['CONTENT_LENGTH'])
{
@unlink($dest);
exit;
}
// Do our basic attachment validation checks before counting this file in
if (!empty($settings['attachmentSizeLimit']) && filesize($dest) > $settings['attachmentSizeLimit'] * 1024)
massattach_error('file_too_big', $dest);
if (!empty($settings['attachmentNumPerPostLimit']) && (count($_SESSION['temp_attachments']) + 1) > $settings['attachmentNumPerPostLimit'])
massattach_error('attachments_limit_per_post', $dest);
$total_size = 0;
foreach ($_SESSION['temp_attachments'] as $attach => $dummy)
$total_size += filesize($current_attach_dir . '/' . $attach);
$total_size += filesize($dest);
if (!empty($settings['attachmentPostLimit']) && $total_size > $settings['attachmentPostLimit'] * 1024)
massattach_error('file_too_big', $dest);
if (!empty($settings['attachmentDirSizeLimit']))
{
// Make sure the directory isn't full.
$dirSize = 0;
$dir = @scandir($current_attach_dir) or massattach_error('cant_access_upload_path', $dest);
foreach ($dir as $file)
{
if ($file == '.' || $file == '..')
continue;
if (preg_match('~^post_tmp_\d+_\d+$~', $file) != 0)
{
// Temp file is more than 5 hours old!
if (filemtime($current_attach_dir . '/' . $file) < time() - 18000)
@unlink($current_attach_dir . '/' . $file);
continue;
}
$dirSize += filesize($current_attach_dir . '/' . $file);
}
// Too big! Maybe you could zip it or something...
if (filesize($dest) + $dirSize > $settings['attachmentDirSizeLimit'] * 1024)
massattach_error('ran_out_of_space', $dest);
}
$_SESSION['temp_attachments'][$attachID] = $filename;
@chmod($dest, 0644);
echo json_encode(array('valid' => true, 'id' => $attachID, 'name' => $filename));
exit;
}
/**
* Returns the current attachment directory
*
* @return string
*/
function get_attach_dir()
{
global $settings;
if (!empty($settings['currentAttachmentUploadDir']))
{
if (!is_array($settings['attachmentUploadDir']))
$settings['attachmentUploadDir'] = unserialize($settings['attachmentUploadDir']);
// Just use the current path for temp files.
$current_attach_dir = $settings['attachmentUploadDir'][$settings['currentAttachmentUploadDir']];
}
else
$current_attach_dir = $settings['attachmentUploadDir'];
return $current_attach_dir;
}
/**
* Throws an error on our behalf
*
* @param string $error_code
* @param string $filepath
* @return void
*/
function massattach_error($error_code, $filepath = '')
{
global $txt, $language;
if (!empty($filepath))
@unlink($filepath);
loadLanguage(array('Errors', 'Post'), $language);
echo json_encode(array('valid' => false, 'error' => isset($txt[$error_code]) ? $txt[$error_code] : $error_code));
exit;
}