1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file handles the uploading and creation of attachments |
||
5 | * as well as the auto management of the attachment directories. |
||
6 | * |
||
7 | * Simple Machines Forum (SMF) |
||
8 | * |
||
9 | * @package SMF |
||
10 | * @author Simple Machines http://www.simplemachines.org |
||
11 | * @copyright 2019 Simple Machines and individual contributors |
||
12 | * @license http://www.simplemachines.org/about/smf/license.php BSD |
||
13 | * |
||
14 | * @version 2.1 RC2 |
||
15 | */ |
||
16 | |||
17 | if (!defined('SMF')) |
||
18 | die('No direct access...'); |
||
19 | |||
20 | /** |
||
21 | * Check if the current directory is still valid or not. |
||
22 | * If not creates the new directory |
||
23 | * |
||
24 | * @return void|bool False if any error occurred |
||
25 | */ |
||
26 | function automanage_attachments_check_directory() |
||
27 | { |
||
28 | global $smcFunc, $boarddir, $modSettings, $context; |
||
29 | |||
30 | // Not pretty, but since we don't want folders created for every post. It'll do unless a better solution can be found. |
||
31 | if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'admin') |
||
32 | $doit = true; |
||
33 | elseif (empty($modSettings['automanage_attachments'])) |
||
34 | return; |
||
35 | elseif (!isset($_FILES)) |
||
36 | return; |
||
37 | elseif (isset($_FILES['attachment'])) |
||
38 | foreach ($_FILES['attachment']['tmp_name'] as $dummy) |
||
39 | if (!empty($dummy)) |
||
40 | { |
||
41 | $doit = true; |
||
42 | break; |
||
43 | } |
||
44 | |||
45 | if (!isset($doit)) |
||
46 | return; |
||
47 | |||
48 | $year = date('Y'); |
||
49 | $month = date('m'); |
||
50 | |||
51 | $rand = md5(mt_rand()); |
||
52 | $rand1 = $rand[1]; |
||
53 | $rand = $rand[0]; |
||
54 | |||
55 | if (!empty($modSettings['attachment_basedirectories']) && !empty($modSettings['use_subdirectories_for_attachments'])) |
||
56 | { |
||
57 | if (!is_array($modSettings['attachment_basedirectories'])) |
||
58 | $modSettings['attachment_basedirectories'] = $smcFunc['json_decode']($modSettings['attachment_basedirectories'], true); |
||
59 | $base_dir = array_search($modSettings['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']); |
||
60 | } |
||
61 | else |
||
62 | $base_dir = 0; |
||
63 | |||
64 | if ($modSettings['automanage_attachments'] == 1) |
||
65 | { |
||
66 | if (!isset($modSettings['last_attachments_directory'])) |
||
67 | $modSettings['last_attachments_directory'] = array(); |
||
68 | if (!is_array($modSettings['last_attachments_directory'])) |
||
69 | $modSettings['last_attachments_directory'] = $smcFunc['json_decode']($modSettings['last_attachments_directory'], true); |
||
70 | if (!isset($modSettings['last_attachments_directory'][$base_dir])) |
||
71 | $modSettings['last_attachments_directory'][$base_dir] = 0; |
||
72 | } |
||
73 | |||
74 | $basedirectory = (!empty($modSettings['use_subdirectories_for_attachments']) ? ($modSettings['basedirectory_for_attachments']) : $boarddir); |
||
75 | //Just to be sure: I don't want directory separators at the end |
||
76 | $sep = (DIRECTORY_SEPARATOR === '\\') ? '\/' : DIRECTORY_SEPARATOR; |
||
77 | $basedirectory = rtrim($basedirectory, $sep); |
||
78 | |||
79 | switch ($modSettings['automanage_attachments']) |
||
80 | { |
||
81 | case 1: |
||
82 | $updir = $basedirectory . DIRECTORY_SEPARATOR . 'attachments_' . (isset($modSettings['last_attachments_directory'][$base_dir]) ? $modSettings['last_attachments_directory'][$base_dir] : 0); |
||
83 | break; |
||
84 | case 2: |
||
85 | $updir = $basedirectory . DIRECTORY_SEPARATOR . $year; |
||
86 | break; |
||
87 | case 3: |
||
88 | $updir = $basedirectory . DIRECTORY_SEPARATOR . $year . DIRECTORY_SEPARATOR . $month; |
||
89 | break; |
||
90 | case 4: |
||
91 | $updir = $basedirectory . DIRECTORY_SEPARATOR . (empty($modSettings['use_subdirectories_for_attachments']) ? 'attachments-' : 'random_') . $rand; |
||
92 | break; |
||
93 | case 5: |
||
94 | $updir = $basedirectory . DIRECTORY_SEPARATOR . (empty($modSettings['use_subdirectories_for_attachments']) ? 'attachments-' : 'random_') . $rand . DIRECTORY_SEPARATOR . $rand1; |
||
95 | break; |
||
96 | default : |
||
97 | $updir = ''; |
||
98 | } |
||
99 | |||
100 | if (!is_array($modSettings['attachmentUploadDir'])) |
||
101 | $modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
102 | if (!in_array($updir, $modSettings['attachmentUploadDir']) && !empty($updir)) |
||
103 | $outputCreation = automanage_attachments_create_directory($updir); |
||
104 | elseif (in_array($updir, $modSettings['attachmentUploadDir'])) |
||
105 | $outputCreation = true; |
||
106 | |||
107 | if ($outputCreation) |
||
108 | { |
||
109 | $modSettings['currentAttachmentUploadDir'] = array_search($updir, $modSettings['attachmentUploadDir']); |
||
110 | $context['attach_dir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; |
||
111 | |||
112 | updateSettings(array( |
||
113 | 'currentAttachmentUploadDir' => $modSettings['currentAttachmentUploadDir'], |
||
114 | )); |
||
115 | } |
||
116 | |||
117 | return $outputCreation; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Creates a directory |
||
122 | * |
||
123 | * @param string $updir The directory to be created |
||
124 | * |
||
125 | * @return bool False on errors |
||
126 | */ |
||
127 | function automanage_attachments_create_directory($updir) |
||
128 | { |
||
129 | global $smcFunc, $modSettings, $context, $boarddir; |
||
130 | |||
131 | $tree = get_directory_tree_elements($updir); |
||
132 | $count = count($tree); |
||
133 | |||
134 | $directory = attachments_init_dir($tree, $count); |
||
135 | if ($directory === false) |
||
136 | { |
||
137 | // Maybe it's just the folder name |
||
138 | $tree = get_directory_tree_elements($boarddir . DIRECTORY_SEPARATOR . $updir); |
||
139 | $count = count($tree); |
||
140 | |||
141 | $directory = attachments_init_dir($tree, $count); |
||
142 | if ($directory === false) |
||
143 | return false; |
||
144 | } |
||
145 | |||
146 | $directory .= DIRECTORY_SEPARATOR . array_shift($tree); |
||
147 | |||
148 | while (!@is_dir($directory) || $count != -1) |
||
149 | { |
||
150 | if (!@is_dir($directory)) |
||
151 | { |
||
152 | if (!@mkdir($directory, 0755)) |
||
153 | { |
||
154 | $context['dir_creation_error'] = 'attachments_no_create'; |
||
155 | return false; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $directory .= DIRECTORY_SEPARATOR . array_shift($tree); |
||
160 | $count--; |
||
161 | } |
||
162 | |||
163 | // Check if the dir is writable. |
||
164 | if (!smf_chmod($directory)) |
||
165 | { |
||
166 | $context['dir_creation_error'] = 'attachments_no_write'; |
||
167 | return false; |
||
168 | } |
||
169 | |||
170 | // Everything seems fine...let's create the .htaccess |
||
171 | if (!file_exists($directory . DIRECTORY_SEPARATOR . '.htaccess')) |
||
172 | secureDirectory($updir, true); |
||
173 | |||
174 | $sep = (DIRECTORY_SEPARATOR === '\\') ? '\/' : DIRECTORY_SEPARATOR; |
||
175 | $updir = rtrim($updir, $sep); |
||
176 | |||
177 | // Only update if it's a new directory |
||
178 | if (!in_array($updir, $modSettings['attachmentUploadDir'])) |
||
179 | { |
||
180 | $modSettings['currentAttachmentUploadDir'] = max(array_keys($modSettings['attachmentUploadDir'])) + 1; |
||
181 | $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']] = $updir; |
||
182 | |||
183 | updateSettings(array( |
||
184 | 'attachmentUploadDir' => $smcFunc['json_encode']($modSettings['attachmentUploadDir']), |
||
185 | 'currentAttachmentUploadDir' => $modSettings['currentAttachmentUploadDir'], |
||
186 | ), true); |
||
187 | $modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
188 | } |
||
189 | |||
190 | $context['attach_dir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; |
||
191 | return true; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Called when a directory space limit is reached. |
||
196 | * Creates a new directory and increments the directory suffix number. |
||
197 | * |
||
198 | * @return void|bool False on errors, true if successful, nothing if auto-management of attachments is disabled |
||
199 | */ |
||
200 | function automanage_attachments_by_space() |
||
201 | { |
||
202 | global $smcFunc, $modSettings, $boarddir; |
||
203 | |||
204 | if (!isset($modSettings['automanage_attachments']) || (!empty($modSettings['automanage_attachments']) && $modSettings['automanage_attachments'] != 1)) |
||
205 | return; |
||
206 | |||
207 | $basedirectory = !empty($modSettings['use_subdirectories_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : $boarddir; |
||
208 | // Just to be sure: I don't want directory separators at the end |
||
209 | $sep = (DIRECTORY_SEPARATOR === '\\') ? '\/' : DIRECTORY_SEPARATOR; |
||
210 | $basedirectory = rtrim($basedirectory, $sep); |
||
211 | |||
212 | // Get the current base directory |
||
213 | if (!empty($modSettings['use_subdirectories_for_attachments']) && !empty($modSettings['attachment_basedirectories'])) |
||
214 | { |
||
215 | $base_dir = array_search($modSettings['basedirectory_for_attachments'], $modSettings['attachment_basedirectories']); |
||
216 | $base_dir = !empty($modSettings['automanage_attachments']) ? $base_dir : 0; |
||
217 | } |
||
218 | else |
||
219 | $base_dir = 0; |
||
220 | |||
221 | // Get the last attachment directory for that base directory |
||
222 | if (empty($modSettings['last_attachments_directory'][$base_dir])) |
||
223 | $modSettings['last_attachments_directory'][$base_dir] = 0; |
||
224 | // And increment it. |
||
225 | $modSettings['last_attachments_directory'][$base_dir]++; |
||
226 | |||
227 | $updir = $basedirectory . DIRECTORY_SEPARATOR . 'attachments_' . $modSettings['last_attachments_directory'][$base_dir]; |
||
228 | if (automanage_attachments_create_directory($updir)) |
||
229 | { |
||
230 | $modSettings['currentAttachmentUploadDir'] = array_search($updir, $modSettings['attachmentUploadDir']); |
||
231 | updateSettings(array( |
||
232 | 'last_attachments_directory' => $smcFunc['json_encode']($modSettings['last_attachments_directory']), |
||
233 | 'currentAttachmentUploadDir' => $modSettings['currentAttachmentUploadDir'], |
||
234 | )); |
||
235 | $modSettings['last_attachments_directory'] = $smcFunc['json_decode']($modSettings['last_attachments_directory'], true); |
||
236 | |||
237 | return true; |
||
238 | } |
||
239 | else |
||
240 | return false; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Split a path into a list of all directories and subdirectories |
||
245 | * |
||
246 | * @param string $directory A path |
||
247 | * |
||
248 | * @return array|bool An array of all the directories and subdirectories or false on failure |
||
249 | */ |
||
250 | function get_directory_tree_elements($directory) |
||
251 | { |
||
252 | /* |
||
253 | In Windows server both \ and / can be used as directory separators in paths |
||
254 | In Linux (and presumably *nix) servers \ can be part of the name |
||
255 | So for this reasons: |
||
256 | * in Windows we need to explode for both \ and / |
||
257 | * while in linux should be safe to explode only for / (aka DIRECTORY_SEPARATOR) |
||
258 | */ |
||
259 | if (DIRECTORY_SEPARATOR === '\\') |
||
260 | $tree = preg_split('#[\\\/]#', $directory); |
||
261 | else |
||
262 | { |
||
263 | if (substr($directory, 0, 1) != DIRECTORY_SEPARATOR) |
||
264 | return false; |
||
265 | |||
266 | $tree = explode(DIRECTORY_SEPARATOR, trim($directory, DIRECTORY_SEPARATOR)); |
||
267 | } |
||
268 | return $tree; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Return the first part of a path (i.e. c:\ or / + the first directory), used by automanage_attachments_create_directory |
||
273 | * |
||
274 | * @param array $tree An array |
||
275 | * @param int $count The number of elements in $tree |
||
276 | * |
||
277 | * @return string|bool The first part of the path or false on error |
||
278 | */ |
||
279 | function attachments_init_dir(&$tree, &$count) |
||
280 | { |
||
281 | $directory = ''; |
||
282 | // If on Windows servers the first part of the path is the drive (e.g. "C:") |
||
283 | if (DIRECTORY_SEPARATOR === '\\') |
||
284 | { |
||
285 | //Better be sure that the first part of the path is actually a drive letter... |
||
286 | //...even if, I should check this in the admin page...isn't it? |
||
287 | //...NHAAA Let's leave space for users' complains! :P |
||
288 | if (preg_match('/^[a-z]:$/i', $tree[0])) |
||
289 | $directory = array_shift($tree); |
||
290 | else |
||
291 | return false; |
||
292 | |||
293 | $count--; |
||
294 | } |
||
295 | return $directory; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Moves an attachment to the proper directory and set the relevant data into $_SESSION['temp_attachments'] |
||
300 | */ |
||
301 | function processAttachments() |
||
302 | { |
||
303 | global $context, $modSettings, $smcFunc, $txt, $user_info; |
||
304 | |||
305 | // Make sure we're uploading to the right place. |
||
306 | if (!empty($modSettings['automanage_attachments'])) |
||
307 | automanage_attachments_check_directory(); |
||
308 | |||
309 | if (!is_array($modSettings['attachmentUploadDir'])) |
||
310 | $modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
311 | |||
312 | $context['attach_dir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; |
||
313 | |||
314 | // Is the attachments folder actualy there? |
||
315 | if (!empty($context['dir_creation_error'])) |
||
316 | $initial_error = $context['dir_creation_error']; |
||
317 | elseif (!is_dir($context['attach_dir'])) |
||
318 | { |
||
319 | $initial_error = 'attach_folder_warning'; |
||
320 | log_error(sprintf($txt['attach_folder_admin_warning'], $context['attach_dir']), 'critical'); |
||
321 | } |
||
322 | |||
323 | if (!isset($initial_error) && !isset($context['attachments'])) |
||
324 | { |
||
325 | // If this isn't a new post, check the current attachments. |
||
326 | if (isset($_REQUEST['msg'])) |
||
327 | { |
||
328 | $request = $smcFunc['db_query']('', ' |
||
329 | SELECT COUNT(*), SUM(size) |
||
330 | FROM {db_prefix}attachments |
||
331 | WHERE id_msg = {int:id_msg} |
||
332 | AND attachment_type = {int:attachment_type}', |
||
333 | array( |
||
334 | 'id_msg' => (int) $_REQUEST['msg'], |
||
335 | 'attachment_type' => 0, |
||
336 | ) |
||
337 | ); |
||
338 | list ($context['attachments']['quantity'], $context['attachments']['total_size']) = $smcFunc['db_fetch_row']($request); |
||
339 | $smcFunc['db_free_result']($request); |
||
340 | } |
||
341 | else |
||
342 | $context['attachments'] = array( |
||
343 | 'quantity' => 0, |
||
344 | 'total_size' => 0, |
||
345 | ); |
||
346 | } |
||
347 | |||
348 | // Hmm. There are still files in session. |
||
349 | $ignore_temp = false; |
||
350 | if (!empty($_SESSION['temp_attachments']['post']['files']) && count($_SESSION['temp_attachments']) > 1) |
||
351 | { |
||
352 | // Let's try to keep them. But... |
||
353 | $ignore_temp = true; |
||
354 | // If new files are being added. We can't ignore those |
||
355 | foreach ($_FILES['attachment']['tmp_name'] as $dummy) |
||
356 | if (!empty($dummy)) |
||
357 | { |
||
358 | $ignore_temp = false; |
||
359 | break; |
||
360 | } |
||
361 | |||
362 | // Need to make space for the new files. So, bye bye. |
||
363 | if (!$ignore_temp) |
||
364 | { |
||
365 | foreach ($_SESSION['temp_attachments'] as $attachID => $attachment) |
||
366 | if (strpos($attachID, 'post_tmp_' . $user_info['id']) !== false) |
||
367 | unlink($attachment['tmp_name']); |
||
368 | |||
369 | $context['we_are_history'] = $txt['error_temp_attachments_flushed']; |
||
370 | $_SESSION['temp_attachments'] = array(); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | if (!isset($_FILES['attachment']['name'])) |
||
375 | $_FILES['attachment']['tmp_name'] = array(); |
||
376 | |||
377 | if (!isset($_SESSION['temp_attachments'])) |
||
378 | $_SESSION['temp_attachments'] = array(); |
||
379 | |||
380 | // Remember where we are at. If it's anywhere at all. |
||
381 | if (!$ignore_temp) |
||
382 | $_SESSION['temp_attachments']['post'] = array( |
||
383 | 'msg' => !empty($_REQUEST['msg']) ? $_REQUEST['msg'] : 0, |
||
384 | 'last_msg' => !empty($_REQUEST['last_msg']) ? $_REQUEST['last_msg'] : 0, |
||
385 | 'topic' => !empty($topic) ? $topic : 0, |
||
386 | 'board' => !empty($board) ? $board : 0, |
||
387 | ); |
||
388 | |||
389 | // If we have an initial error, lets just display it. |
||
390 | if (!empty($initial_error)) |
||
391 | { |
||
392 | $_SESSION['temp_attachments']['initial_error'] = $initial_error; |
||
393 | |||
394 | // And delete the files 'cos they ain't going nowhere. |
||
395 | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
||
396 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
397 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
398 | |||
399 | $_FILES['attachment']['tmp_name'] = array(); |
||
400 | } |
||
401 | |||
402 | // Loop through $_FILES['attachment'] array and move each file to the current attachments folder. |
||
403 | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
||
404 | { |
||
405 | if ($_FILES['attachment']['name'][$n] == '') |
||
406 | continue; |
||
407 | |||
408 | // First, let's first check for PHP upload errors. |
||
409 | $errors = array(); |
||
410 | if (!empty($_FILES['attachment']['error'][$n])) |
||
411 | { |
||
412 | if ($_FILES['attachment']['error'][$n] == 2) |
||
413 | $errors[] = array('file_too_big', array($modSettings['attachmentSizeLimit'])); |
||
414 | elseif ($_FILES['attachment']['error'][$n] == 6) |
||
415 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_6'], 'critical'); |
||
416 | else |
||
417 | log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$n]]); |
||
418 | if (empty($errors)) |
||
419 | $errors[] = 'attach_php_error'; |
||
420 | } |
||
421 | |||
422 | // Try to move and rename the file before doing any more checks on it. |
||
423 | $attachID = 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()); |
||
424 | $destName = $context['attach_dir'] . '/' . $attachID; |
||
425 | if (empty($errors)) |
||
426 | { |
||
427 | // The reported MIME type of the attachment might not be reliable. |
||
428 | // Fortunately, PHP 5.3+ lets us easily verify the real MIME type. |
||
429 | if (function_exists('mime_content_type')) |
||
430 | $_FILES['attachment']['type'][$n] = mime_content_type($_FILES['attachment']['tmp_name'][$n]); |
||
431 | |||
432 | $_SESSION['temp_attachments'][$attachID] = array( |
||
433 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
434 | 'tmp_name' => $destName, |
||
435 | 'size' => $_FILES['attachment']['size'][$n], |
||
436 | 'type' => $_FILES['attachment']['type'][$n], |
||
437 | 'id_folder' => $modSettings['currentAttachmentUploadDir'], |
||
438 | 'errors' => array(), |
||
439 | ); |
||
440 | |||
441 | // Move the file to the attachments folder with a temp name for now. |
||
442 | if (@move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName)) |
||
443 | smf_chmod($destName, 0644); |
||
444 | else |
||
445 | { |
||
446 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'attach_timeout'; |
||
447 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
448 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
449 | } |
||
450 | } |
||
451 | else |
||
452 | { |
||
453 | $_SESSION['temp_attachments'][$attachID] = array( |
||
454 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
||
455 | 'tmp_name' => $destName, |
||
456 | 'errors' => $errors, |
||
457 | ); |
||
458 | |||
459 | if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
||
460 | unlink($_FILES['attachment']['tmp_name'][$n]); |
||
461 | } |
||
462 | // If there's no errors to this point. We still do need to apply some additional checks before we are finished. |
||
463 | if (empty($_SESSION['temp_attachments'][$attachID]['errors'])) |
||
464 | attachmentChecks($attachID); |
||
465 | } |
||
466 | // Mod authors, finally a hook to hang an alternate attachment upload system upon |
||
467 | // Upload to the current attachment folder with the file name $attachID or 'post_tmp_' . $user_info['id'] . '_' . md5(mt_rand()) |
||
468 | // Populate $_SESSION['temp_attachments'][$attachID] with the following: |
||
469 | // name => The file name |
||
470 | // tmp_name => Path to the temp file ($context['attach_dir'] . '/' . $attachID). |
||
471 | // size => File size (required). |
||
472 | // type => MIME type (optional if not available on upload). |
||
473 | // id_folder => $modSettings['currentAttachmentUploadDir'] |
||
474 | // errors => An array of errors (use the index of the $txt variable for that error). |
||
475 | // Template changes can be done using "integrate_upload_template". |
||
476 | call_integration_hook('integrate_attachment_upload', array()); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Performs various checks on an uploaded file. |
||
481 | * - Requires that $_SESSION['temp_attachments'][$attachID] be properly populated. |
||
482 | * |
||
483 | * @param int $attachID The ID of the attachment |
||
484 | * @return bool Whether the attachment is OK |
||
485 | */ |
||
486 | function attachmentChecks($attachID) |
||
487 | { |
||
488 | global $modSettings, $context, $sourcedir, $smcFunc; |
||
489 | |||
490 | // No data or missing data .... Not necessarily needed, but in case a mod author missed something. |
||
491 | if (empty($_SESSION['temp_attachments'][$attachID])) |
||
492 | $error = '$_SESSION[\'temp_attachments\'][$attachID]'; |
||
493 | |||
494 | elseif (empty($attachID)) |
||
495 | $error = '$attachID'; |
||
496 | |||
497 | elseif (empty($context['attachments'])) |
||
498 | $error = '$context[\'attachments\']'; |
||
499 | |||
500 | elseif (empty($context['attach_dir'])) |
||
501 | $error = '$context[\'attach_dir\']'; |
||
502 | |||
503 | // Let's get their attention. |
||
504 | if (!empty($error)) |
||
505 | fatal_lang_error('attach_check_nag', 'debug', array($error)); |
||
506 | |||
507 | // Just in case this slipped by the first checks, we stop it here and now |
||
508 | if ($_SESSION['temp_attachments'][$attachID]['size'] == 0) |
||
509 | { |
||
510 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'attach_0_byte_file'; |
||
511 | return false; |
||
512 | } |
||
513 | |||
514 | // First, the dreaded security check. Sorry folks, but this shouldn't be avoided. |
||
515 | $size = @getimagesize($_SESSION['temp_attachments'][$attachID]['tmp_name']); |
||
516 | if (isset($context['valid_image_types'][$size[2]])) |
||
517 | { |
||
518 | require_once($sourcedir . '/Subs-Graphics.php'); |
||
519 | if (!checkImageContents($_SESSION['temp_attachments'][$attachID]['tmp_name'], !empty($modSettings['attachment_image_paranoid']))) |
||
520 | { |
||
521 | // It's bad. Last chance, maybe we can re-encode it? |
||
522 | if (empty($modSettings['attachment_image_reencode']) || (!reencodeImage($_SESSION['temp_attachments'][$attachID]['tmp_name'], $size[2]))) |
||
523 | { |
||
524 | // Nothing to do: not allowed or not successful re-encoding it. |
||
525 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'bad_attachment'; |
||
526 | return false; |
||
527 | } |
||
528 | // Success! However, successes usually come for a price: |
||
529 | // we might get a new format for our image... |
||
530 | $old_format = $size[2]; |
||
531 | $size = @getimagesize($_SESSION['temp_attachments'][$attachID]['tmp_name']); |
||
532 | if (!(empty($size)) && ($size[2] != $old_format)) |
||
533 | { |
||
534 | if (isset($context['valid_image_types'][$size[2]])) |
||
535 | $_SESSION['temp_attachments'][$attachID]['type'] = 'image/' . $context['valid_image_types'][$size[2]]; |
||
536 | } |
||
537 | } |
||
538 | } |
||
539 | |||
540 | // Is there room for this sucker? |
||
541 | if (!empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit'])) |
||
542 | { |
||
543 | // Check the folder size and count. If it hasn't been done already. |
||
544 | if (empty($context['dir_size']) || empty($context['dir_files'])) |
||
545 | { |
||
546 | $request = $smcFunc['db_query']('', ' |
||
547 | SELECT COUNT(*), SUM(size) |
||
548 | FROM {db_prefix}attachments |
||
549 | WHERE id_folder = {int:folder_id} |
||
550 | AND attachment_type != {int:type}', |
||
551 | array( |
||
552 | 'folder_id' => $modSettings['currentAttachmentUploadDir'], |
||
553 | 'type' => 1, |
||
554 | ) |
||
555 | ); |
||
556 | list ($context['dir_files'], $context['dir_size']) = $smcFunc['db_fetch_row']($request); |
||
557 | $smcFunc['db_free_result']($request); |
||
558 | } |
||
559 | $context['dir_size'] += $_SESSION['temp_attachments'][$attachID]['size']; |
||
560 | $context['dir_files']++; |
||
561 | |||
562 | // Are we about to run out of room? Let's notify the admin then. |
||
563 | if (empty($modSettings['attachment_full_notified']) && !empty($modSettings['attachmentDirSizeLimit']) && $modSettings['attachmentDirSizeLimit'] > 4000 && $context['dir_size'] > ($modSettings['attachmentDirSizeLimit'] - 2000) * 1024 |
||
564 | || (!empty($modSettings['attachmentDirFileLimit']) && $modSettings['attachmentDirFileLimit'] * .95 < $context['dir_files'] && $modSettings['attachmentDirFileLimit'] > 500)) |
||
565 | { |
||
566 | require_once($sourcedir . '/Subs-Admin.php'); |
||
567 | emailAdmins('admin_attachments_full'); |
||
568 | updateSettings(array('attachment_full_notified' => 1)); |
||
569 | } |
||
570 | |||
571 | // // No room left.... What to do now??? |
||
572 | if (!empty($modSettings['attachmentDirFileLimit']) && $context['dir_files'] > $modSettings['attachmentDirFileLimit'] |
||
573 | || (!empty($modSettings['attachmentDirSizeLimit']) && $context['dir_size'] > $modSettings['attachmentDirSizeLimit'] * 1024)) |
||
574 | { |
||
575 | if (!empty($modSettings['automanage_attachments']) && $modSettings['automanage_attachments'] == 1) |
||
576 | { |
||
577 | // Move it to the new folder if we can. |
||
578 | if (automanage_attachments_by_space()) |
||
579 | { |
||
580 | rename($_SESSION['temp_attachments'][$attachID]['tmp_name'], $context['attach_dir'] . '/' . $attachID); |
||
581 | $_SESSION['temp_attachments'][$attachID]['tmp_name'] = $context['attach_dir'] . '/' . $attachID; |
||
582 | $_SESSION['temp_attachments'][$attachID]['id_folder'] = $modSettings['currentAttachmentUploadDir']; |
||
583 | $context['dir_size'] = 0; |
||
584 | $context['dir_files'] = 0; |
||
585 | } |
||
586 | // Or, let the user know that it ain't gonna happen. |
||
587 | else |
||
588 | { |
||
589 | if (isset($context['dir_creation_error'])) |
||
590 | $_SESSION['temp_attachments'][$attachID]['errors'][] = $context['dir_creation_error']; |
||
591 | else |
||
592 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'ran_out_of_space'; |
||
593 | } |
||
594 | } |
||
595 | else |
||
596 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'ran_out_of_space'; |
||
597 | } |
||
598 | } |
||
599 | |||
600 | // Is the file too big? |
||
601 | $context['attachments']['total_size'] += $_SESSION['temp_attachments'][$attachID]['size']; |
||
602 | if (!empty($modSettings['attachmentSizeLimit']) && $_SESSION['temp_attachments'][$attachID]['size'] > $modSettings['attachmentSizeLimit'] * 1024) |
||
603 | $_SESSION['temp_attachments'][$attachID]['errors'][] = array('file_too_big', array(comma_format($modSettings['attachmentSizeLimit'], 0))); |
||
604 | |||
605 | // Check the total upload size for this post... |
||
606 | if (!empty($modSettings['attachmentPostLimit']) && $context['attachments']['total_size'] > $modSettings['attachmentPostLimit'] * 1024) |
||
607 | $_SESSION['temp_attachments'][$attachID]['errors'][] = array('attach_max_total_file_size', array(comma_format($modSettings['attachmentPostLimit'], 0), comma_format($modSettings['attachmentPostLimit'] - (($context['attachments']['total_size'] - $_SESSION['temp_attachments'][$attachID]['size']) / 1024), 0))); |
||
608 | |||
609 | // Have we reached the maximum number of files we are allowed? |
||
610 | $context['attachments']['quantity']++; |
||
611 | |||
612 | // Set a max limit if none exists |
||
613 | if (empty($modSettings['attachmentNumPerPostLimit']) && $context['attachments']['quantity'] >= 50) |
||
614 | $modSettings['attachmentNumPerPostLimit'] = 50; |
||
615 | |||
616 | if (!empty($modSettings['attachmentNumPerPostLimit']) && $context['attachments']['quantity'] > $modSettings['attachmentNumPerPostLimit']) |
||
617 | $_SESSION['temp_attachments'][$attachID]['errors'][] = array('attachments_limit_per_post', array($modSettings['attachmentNumPerPostLimit'])); |
||
618 | |||
619 | // File extension check |
||
620 | if (!empty($modSettings['attachmentCheckExtensions'])) |
||
621 | { |
||
622 | $allowed = explode(',', strtolower($modSettings['attachmentExtensions'])); |
||
623 | foreach ($allowed as $k => $dummy) |
||
624 | $allowed[$k] = trim($dummy); |
||
625 | |||
626 | if (!in_array(strtolower(substr(strrchr($_SESSION['temp_attachments'][$attachID]['name'], '.'), 1)), $allowed)) |
||
627 | { |
||
628 | $allowed_extensions = strtr(strtolower($modSettings['attachmentExtensions']), array(',' => ', ')); |
||
629 | $_SESSION['temp_attachments'][$attachID]['errors'][] = array('cant_upload_type', array($allowed_extensions)); |
||
630 | } |
||
631 | } |
||
632 | |||
633 | // Undo the math if there's an error |
||
634 | if (!empty($_SESSION['temp_attachments'][$attachID]['errors'])) |
||
635 | { |
||
636 | if (isset($context['dir_size'])) |
||
637 | $context['dir_size'] -= $_SESSION['temp_attachments'][$attachID]['size']; |
||
638 | if (isset($context['dir_files'])) |
||
639 | $context['dir_files']--; |
||
640 | $context['attachments']['total_size'] -= $_SESSION['temp_attachments'][$attachID]['size']; |
||
641 | $context['attachments']['quantity']--; |
||
642 | return false; |
||
643 | } |
||
644 | |||
645 | return true; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * Create an attachment, with the given array of parameters. |
||
650 | * - Adds any additional or missing parameters to $attachmentOptions. |
||
651 | * - Renames the temporary file. |
||
652 | * - Creates a thumbnail if the file is an image and the option enabled. |
||
653 | * |
||
654 | * @param array $attachmentOptions An array of attachment options |
||
655 | * @return bool Whether the attachment was created successfully |
||
656 | */ |
||
657 | function createAttachment(&$attachmentOptions) |
||
658 | { |
||
659 | global $modSettings, $sourcedir, $smcFunc, $context, $txt; |
||
660 | |||
661 | require_once($sourcedir . '/Subs-Graphics.php'); |
||
662 | |||
663 | // If this is an image we need to set a few additional parameters. |
||
664 | $size = @getimagesize($attachmentOptions['tmp_name']); |
||
665 | list ($attachmentOptions['width'], $attachmentOptions['height']) = $size; |
||
666 | |||
667 | // If it's an image get the mime type right. |
||
668 | if (empty($attachmentOptions['mime_type']) && $attachmentOptions['width']) |
||
669 | { |
||
670 | // Got a proper mime type? |
||
671 | if (!empty($size['mime'])) |
||
672 | $attachmentOptions['mime_type'] = $size['mime']; |
||
673 | |||
674 | // Otherwise a valid one? |
||
675 | elseif (isset($context['valid_image_types'][$size[2]])) |
||
676 | $attachmentOptions['mime_type'] = 'image/' . $context['valid_image_types'][$size[2]]; |
||
677 | } |
||
678 | |||
679 | // It is possible we might have a MIME type that isn't actually an image but still have a size. |
||
680 | // For example, Shockwave files will be able to return size but be 'application/shockwave' or similar. |
||
681 | if (!empty($attachmentOptions['mime_type']) && strpos($attachmentOptions['mime_type'], 'image/') !== 0) |
||
682 | { |
||
683 | $attachmentOptions['width'] = 0; |
||
684 | $attachmentOptions['height'] = 0; |
||
685 | } |
||
686 | |||
687 | // Get the hash if no hash has been given yet. |
||
688 | if (empty($attachmentOptions['file_hash'])) |
||
689 | $attachmentOptions['file_hash'] = getAttachmentFilename($attachmentOptions['name'], false, null, true); |
||
690 | |||
691 | // Assuming no-one set the extension let's take a look at it. |
||
692 | if (empty($attachmentOptions['fileext'])) |
||
693 | { |
||
694 | $attachmentOptions['fileext'] = strtolower(strrpos($attachmentOptions['name'], '.') !== false ? substr($attachmentOptions['name'], strrpos($attachmentOptions['name'], '.') + 1) : ''); |
||
695 | if (strlen($attachmentOptions['fileext']) > 8 || '.' . $attachmentOptions['fileext'] == $attachmentOptions['name']) |
||
696 | $attachmentOptions['fileext'] = ''; |
||
697 | } |
||
698 | |||
699 | // This defines which options to use for which columns in the insert query. |
||
700 | // Mods using the hook can add columns and even change the properties of existing columns, |
||
701 | // but if they delete one of these columns, it will be reset to the default defined here. |
||
702 | $attachmentStandardInserts = $attachmentInserts = array( |
||
703 | // Format: 'column' => array('type', 'option') |
||
704 | 'id_folder' => array('int', 'id_folder'), |
||
705 | 'id_msg' => array('int', 'post'), |
||
706 | 'filename' => array('string-255', 'name'), |
||
707 | 'file_hash' => array('string-40', 'file_hash'), |
||
708 | 'fileext' => array('string-8', 'fileext'), |
||
709 | 'size' => array('int', 'size'), |
||
710 | 'width' => array('int', 'width'), |
||
711 | 'height' => array('int', 'height'), |
||
712 | 'mime_type' => array('string-20', 'mime_type'), |
||
713 | 'approved' => array('int', 'approved'), |
||
714 | ); |
||
715 | |||
716 | // Last chance to change stuff! |
||
717 | call_integration_hook('integrate_createAttachment', array(&$attachmentOptions, &$attachmentInserts)); |
||
718 | |||
719 | // Make sure the folder is valid... |
||
720 | $tmp = is_array($modSettings['attachmentUploadDir']) ? $modSettings['attachmentUploadDir'] : $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
721 | $folders = array_keys($tmp); |
||
722 | if (empty($attachmentOptions['id_folder']) || !in_array($attachmentOptions['id_folder'], $folders)) |
||
723 | $attachmentOptions['id_folder'] = $modSettings['currentAttachmentUploadDir']; |
||
724 | |||
725 | // Make sure all required columns are present, in case a mod screwed up. |
||
726 | foreach ($attachmentStandardInserts as $column => $insert_info) |
||
727 | if (!isset($attachmentInserts[$column])) |
||
728 | $attachmentInserts[$column] = $insert_info; |
||
729 | |||
730 | // Set up the columns and values to insert, in the correct order. |
||
731 | $attachmentColumns = array(); |
||
732 | $attachmentValues = array(); |
||
733 | foreach ($attachmentInserts as $column => $insert_info) |
||
734 | { |
||
735 | $attachmentColumns[$column] = $insert_info[0]; |
||
736 | |||
737 | if (!empty($insert_info[0]) && $insert_info[0] == 'int') |
||
738 | $attachmentValues[] = (int) $attachmentOptions[$insert_info[1]]; |
||
739 | else |
||
740 | $attachmentValues[] = $attachmentOptions[$insert_info[1]]; |
||
741 | } |
||
742 | |||
743 | // Create the attachment in the database. |
||
744 | $attachmentOptions['id'] = $smcFunc['db_insert']('', |
||
745 | '{db_prefix}attachments', |
||
746 | $attachmentColumns, |
||
747 | $attachmentValues, |
||
748 | array('id_attach'), |
||
749 | 1 |
||
750 | ); |
||
751 | |||
752 | // Attachment couldn't be created. |
||
753 | if (empty($attachmentOptions['id'])) |
||
754 | { |
||
755 | loadLanguage('Errors'); |
||
756 | log_error($txt['attachment_not_created'], 'general'); |
||
757 | return false; |
||
758 | } |
||
759 | |||
760 | // Now that we have the attach id, let's rename this sucker and finish up. |
||
761 | $attachmentOptions['destination'] = getAttachmentFilename(basename($attachmentOptions['name']), $attachmentOptions['id'], $attachmentOptions['id_folder'], false, $attachmentOptions['file_hash']); |
||
762 | rename($attachmentOptions['tmp_name'], $attachmentOptions['destination']); |
||
763 | |||
764 | // If it's not approved then add to the approval queue. |
||
765 | if (!$attachmentOptions['approved']) |
||
766 | { |
||
767 | $smcFunc['db_insert']('', |
||
768 | '{db_prefix}approval_queue', |
||
769 | array( |
||
770 | 'id_attach' => 'int', 'id_msg' => 'int', |
||
771 | ), |
||
772 | array( |
||
773 | $attachmentOptions['id'], (int) $attachmentOptions['post'], |
||
774 | ), |
||
775 | array() |
||
776 | ); |
||
777 | |||
778 | // Queue background notification task. |
||
779 | $smcFunc['db_insert']( |
||
780 | 'insert', |
||
781 | '{db_prefix}background_tasks', |
||
782 | array( |
||
783 | 'task_file' => 'string', |
||
784 | 'task_class' => 'string', |
||
785 | 'task_data' => 'string', |
||
786 | 'claimed_time' => 'int' |
||
787 | ), |
||
788 | array( |
||
789 | '$sourcedir/tasks/CreateAttachment-Notify.php', |
||
790 | 'CreateAttachment_Notify_Background', |
||
791 | $smcFunc['json_encode']( |
||
792 | array( |
||
793 | 'id' => $attachmentOptions['id'], |
||
794 | ) |
||
795 | ), |
||
796 | 0 |
||
797 | ), |
||
798 | array( |
||
799 | 'id_task' |
||
800 | ) |
||
801 | ); |
||
802 | } |
||
803 | |||
804 | if (empty($modSettings['attachmentThumbnails']) || (empty($attachmentOptions['width']) && empty($attachmentOptions['height']))) |
||
805 | return true; |
||
806 | |||
807 | // Like thumbnails, do we? |
||
808 | if (!empty($modSettings['attachmentThumbWidth']) && !empty($modSettings['attachmentThumbHeight']) && ($attachmentOptions['width'] > $modSettings['attachmentThumbWidth'] || $attachmentOptions['height'] > $modSettings['attachmentThumbHeight'])) |
||
809 | { |
||
810 | if (createThumbnail($attachmentOptions['destination'], $modSettings['attachmentThumbWidth'], $modSettings['attachmentThumbHeight'])) |
||
811 | { |
||
812 | // Figure out how big we actually made it. |
||
813 | $size = @getimagesize($attachmentOptions['destination'] . '_thumb'); |
||
814 | list ($thumb_width, $thumb_height) = $size; |
||
815 | |||
816 | if (!empty($size['mime'])) |
||
817 | $thumb_mime = $size['mime']; |
||
818 | elseif (isset($context['valid_image_types'][$size[2]])) |
||
819 | $thumb_mime = 'image/' . $context['valid_image_types'][$size[2]]; |
||
820 | // Lord only knows how this happened... |
||
821 | else |
||
822 | $thumb_mime = ''; |
||
823 | |||
824 | $thumb_filename = $attachmentOptions['name'] . '_thumb'; |
||
825 | $thumb_size = filesize($attachmentOptions['destination'] . '_thumb'); |
||
826 | $thumb_file_hash = getAttachmentFilename($thumb_filename, false, null, true); |
||
827 | $thumb_path = $attachmentOptions['destination'] . '_thumb'; |
||
828 | |||
829 | // We should check the file size and count here since thumbs are added to the existing totals. |
||
830 | if (!empty($modSettings['automanage_attachments']) && $modSettings['automanage_attachments'] == 1 && !empty($modSettings['attachmentDirSizeLimit']) || !empty($modSettings['attachmentDirFileLimit'])) |
||
831 | { |
||
832 | $context['dir_size'] = isset($context['dir_size']) ? $context['dir_size'] += $thumb_size : $context['dir_size'] = 0; |
||
833 | $context['dir_files'] = isset($context['dir_files']) ? $context['dir_files']++ : $context['dir_files'] = 0; |
||
834 | |||
835 | // If the folder is full, try to create a new one and move the thumb to it. |
||
836 | if ($context['dir_size'] > $modSettings['attachmentDirSizeLimit'] * 1024 || $context['dir_files'] + 2 > $modSettings['attachmentDirFileLimit']) |
||
837 | { |
||
838 | if (automanage_attachments_by_space()) |
||
839 | { |
||
840 | rename($thumb_path, $context['attach_dir'] . '/' . $thumb_filename); |
||
841 | $thumb_path = $context['attach_dir'] . '/' . $thumb_filename; |
||
842 | $context['dir_size'] = 0; |
||
843 | $context['dir_files'] = 0; |
||
844 | } |
||
845 | } |
||
846 | } |
||
847 | // If a new folder has been already created. Gotta move this thumb there then. |
||
848 | if ($modSettings['currentAttachmentUploadDir'] != $attachmentOptions['id_folder']) |
||
849 | { |
||
850 | rename($thumb_path, $context['attach_dir'] . '/' . $thumb_filename); |
||
851 | $thumb_path = $context['attach_dir'] . '/' . $thumb_filename; |
||
852 | } |
||
853 | |||
854 | // To the database we go! |
||
855 | $attachmentOptions['thumb'] = $smcFunc['db_insert']('', |
||
856 | '{db_prefix}attachments', |
||
857 | array( |
||
858 | 'id_folder' => 'int', 'id_msg' => 'int', 'attachment_type' => 'int', 'filename' => 'string-255', 'file_hash' => 'string-40', 'fileext' => 'string-8', |
||
859 | 'size' => 'int', 'width' => 'int', 'height' => 'int', 'mime_type' => 'string-20', 'approved' => 'int', |
||
860 | ), |
||
861 | array( |
||
862 | $modSettings['currentAttachmentUploadDir'], (int) $attachmentOptions['post'], 3, $thumb_filename, $thumb_file_hash, $attachmentOptions['fileext'], |
||
863 | $thumb_size, $thumb_width, $thumb_height, $thumb_mime, (int) $attachmentOptions['approved'], |
||
864 | ), |
||
865 | array('id_attach'), |
||
866 | 1 |
||
867 | ); |
||
868 | |||
869 | if (!empty($attachmentOptions['thumb'])) |
||
870 | { |
||
871 | $smcFunc['db_query']('', ' |
||
872 | UPDATE {db_prefix}attachments |
||
873 | SET id_thumb = {int:id_thumb} |
||
874 | WHERE id_attach = {int:id_attach}', |
||
875 | array( |
||
876 | 'id_thumb' => $attachmentOptions['thumb'], |
||
877 | 'id_attach' => $attachmentOptions['id'], |
||
878 | ) |
||
879 | ); |
||
880 | |||
881 | rename($thumb_path, getAttachmentFilename($thumb_filename, $attachmentOptions['thumb'], $modSettings['currentAttachmentUploadDir'], false, $thumb_file_hash)); |
||
882 | } |
||
883 | } |
||
884 | } |
||
885 | |||
886 | return true; |
||
887 | } |
||
888 | |||
889 | /** |
||
890 | * Assigns the given attachments to the given message ID. |
||
891 | * |
||
892 | * @param $attachIDs array of attachment IDs to assign. |
||
893 | * @param $msgID integer the message ID. |
||
894 | * |
||
895 | * @return boolean false on error or missing params. |
||
896 | */ |
||
897 | function assignAttachments($attachIDs = array(), $msgID = 0) |
||
898 | { |
||
899 | global $smcFunc; |
||
900 | |||
901 | // Oh, come on! |
||
902 | if (empty($attachIDs) || empty($msgID)) |
||
903 | return false; |
||
904 | |||
905 | // "I see what is right and approve, but I do what is wrong." |
||
906 | call_integration_hook('integrate_assign_attachments', array(&$attachIDs, &$msgID)); |
||
907 | |||
908 | // One last check |
||
909 | if (empty($attachIDs)) |
||
910 | return false; |
||
911 | |||
912 | // Perform. |
||
913 | $smcFunc['db_query']('', ' |
||
914 | UPDATE {db_prefix}attachments |
||
915 | SET id_msg = {int:id_msg} |
||
916 | WHERE id_attach IN ({array_int:attach_ids})', |
||
917 | array( |
||
918 | 'id_msg' => $msgID, |
||
919 | 'attach_ids' => $attachIDs, |
||
920 | ) |
||
921 | ); |
||
922 | |||
923 | return true; |
||
924 | } |
||
925 | |||
926 | /** |
||
927 | * Gets an attach ID and tries to load all its info. |
||
928 | * |
||
929 | * @param int $attachID the attachment ID to load info from. |
||
930 | * |
||
931 | * @return mixed If succesful, it will return an array of loaded data. String, most likely a $txt key if there was some error. |
||
932 | */ |
||
933 | function parseAttachBBC($attachID = 0) |
||
934 | { |
||
935 | global $board, $modSettings, $context, $scripturl, $smcFunc; |
||
936 | static $view_attachment_boards; |
||
937 | |||
938 | if (!isset($view_attachment_boards)) |
||
939 | $view_attachment_boards = boardsAllowedTo('view_attachments'); |
||
940 | |||
941 | // Meh... |
||
942 | if (empty($attachID)) |
||
943 | return 'attachments_no_data_loaded'; |
||
944 | |||
945 | // Make it easy. |
||
946 | $msgID = !empty($_REQUEST['msg']) ? (int) $_REQUEST['msg'] : 0; |
||
947 | |||
948 | // Perhaps someone else wants to do the honors? Yes, this also includes dealing with previews ;) |
||
949 | $externalParse = call_integration_hook('integrate_pre_parseAttachBBC', array($attachID, $msgID)); |
||
950 | |||
951 | // "I am innocent of the blood of this just person: see ye to it." |
||
952 | if (!empty($externalParse) && (is_string($externalParse) || is_array($externalParse))) |
||
953 | return $externalParse; |
||
954 | |||
955 | // Are attachments enabled? |
||
956 | if (empty($modSettings['attachmentEnable'])) |
||
957 | return 'attachments_not_enable'; |
||
958 | |||
959 | // There is always the chance someone else has already done our dirty work... |
||
960 | // If so, all pertinent checks were already done. Hopefully... |
||
961 | if (!empty($context['current_attachments']) && !empty($context['current_attachments'][$attachID])) |
||
962 | return $context['current_attachments'][$attachID]; |
||
963 | |||
964 | // If we are lucky enough to be in $board's scope then check it! |
||
965 | if (!empty($board) && $view_attachment_boards !== array(0) && !in_array($board, $view_attachment_boards)) |
||
966 | return 'attachments_not_allowed_to_see'; |
||
967 | |||
968 | // Get the message info associated with this particular attach ID. |
||
969 | $attachInfo = getAttachMsgInfo($attachID); |
||
970 | |||
971 | // There is always the chance this attachment no longer exists or isn't associated to a message anymore... |
||
972 | if (empty($attachInfo) || empty($attachInfo['msg']) && empty($context['preview_message'])) |
||
973 | return 'attachments_no_msg_associated'; |
||
974 | |||
975 | // Hold it! got the info now check if you can see this attachment. |
||
976 | if ($view_attachment_boards !== array(0) && !in_array($attachInfo['board'], $view_attachment_boards)) |
||
977 | return 'attachments_not_allowed_to_see'; |
||
978 | |||
979 | if (empty($context['loaded_attachments'][$attachInfo['msg']])) |
||
980 | prepareAttachsByMsg(array($attachInfo['msg'])); |
||
981 | |||
982 | if (isset($context['loaded_attachments'][$attachInfo['msg']][$attachID])) |
||
983 | $attachContext = $context['loaded_attachments'][$attachInfo['msg']][$attachID]; |
||
984 | |||
985 | // In case the user manually typed the thumbnail's ID into the BBC |
||
986 | elseif (!empty($context['loaded_attachments'][$attachInfo['msg']])) |
||
987 | { |
||
988 | foreach ($context['loaded_attachments'][$attachInfo['msg']] as $foundAttachID => $foundAttach) |
||
989 | { |
||
990 | if ($foundAttach['id_thumb'] == $attachID) |
||
991 | { |
||
992 | $attachContext = $context['loaded_attachments'][$attachInfo['msg']][$foundAttachID]; |
||
993 | $attachID = $foundAttachID; |
||
994 | break; |
||
995 | } |
||
996 | } |
||
997 | } |
||
998 | |||
999 | // Load this particular attach's context. |
||
1000 | if (!empty($attachContext)) |
||
1001 | $attachLoaded = loadAttachmentContext($attachContext['id_msg'], $context['loaded_attachments']); |
||
1002 | |||
1003 | // One last check, you know, gotta be paranoid... |
||
1004 | else |
||
1005 | return 'attachments_no_data_loaded'; |
||
1006 | |||
1007 | // This is the last "if" I promise! |
||
1008 | if (empty($attachLoaded)) |
||
1009 | return 'attachments_no_data_loaded'; |
||
1010 | |||
1011 | else |
||
1012 | $attachContext = $attachLoaded[$attachID]; |
||
1013 | |||
1014 | // No point in keep going further. |
||
1015 | if ($view_attachment_boards !== array(0) && !in_array($attachContext['board'], $view_attachment_boards)) |
||
1016 | return 'attachments_not_allowed_to_see'; |
||
1017 | |||
1018 | // Previewing much? no msg ID has been set yet. |
||
1019 | if (!empty($context['preview_message'])) |
||
1020 | { |
||
1021 | $attachContext['href'] = $scripturl . '?action=dlattach;attach=' . $attachID . ';type=preview'; |
||
1022 | |||
1023 | $attachContext['link'] = '<a href="' . $scripturl . '?action=dlattach;attach=' . $attachID . ';type=preview' . (empty($attachContext['is_image']) ? ';file' : '') . '" class="bbc_link">' . $smcFunc['htmlspecialchars']($attachContext['name']) . '</a>'; |
||
1024 | |||
1025 | // Fix the thumbnail too, if the image has one. |
||
1026 | if (!empty($attachContext['thumbnail']) && !empty($attachContext['thumbnail']['has_thumb'])) |
||
1027 | $attachContext['thumbnail']['href'] = $scripturl . '?action=dlattach;attach=' . $attachContext['thumbnail']['id'] . ';image;type=preview'; |
||
1028 | } |
||
1029 | |||
1030 | // You may or may not want to show this under the post. |
||
1031 | if (!empty($modSettings['dont_show_attach_under_post']) && !isset($context['show_attach_under_post'][$attachID])) |
||
1032 | $context['show_attach_under_post'][$attachID] = $attachID; |
||
1033 | |||
1034 | // Last minute changes? |
||
1035 | call_integration_hook('integrate_post_parseAttachBBC', array(&$attachContext)); |
||
1036 | |||
1037 | // Don't do any logic with the loaded data, leave it to whoever called this function. |
||
1038 | return $attachContext; |
||
1039 | } |
||
1040 | |||
1041 | /** |
||
1042 | * Gets raw info directly from the attachments table. |
||
1043 | * |
||
1044 | * @param array $attachIDs An array of attachments IDs. |
||
1045 | * |
||
1046 | * @return array. |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
1047 | */ |
||
1048 | function getRawAttachInfo($attachIDs) |
||
1049 | { |
||
1050 | global $smcFunc, $modSettings; |
||
1051 | |||
1052 | if (empty($attachIDs)) |
||
1053 | return array(); |
||
1054 | |||
1055 | $return = array(); |
||
1056 | |||
1057 | $request = $smcFunc['db_query']('', ' |
||
1058 | SELECT a.id_attach, a.id_msg, a.id_member, a.size, a.mime_type, a.id_folder, a.filename' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', |
||
1059 | COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . ' |
||
1060 | FROM {db_prefix}attachments AS a' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ' |
||
1061 | LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)') . ' |
||
1062 | WHERE a.id_attach IN ({array_int:attach_ids}) |
||
1063 | LIMIT 1', |
||
1064 | array( |
||
1065 | 'attach_ids' => (array) $attachIDs, |
||
1066 | ) |
||
1067 | ); |
||
1068 | |||
1069 | if ($smcFunc['db_num_rows']($request) != 1) |
||
1070 | return array(); |
||
1071 | |||
1072 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1073 | $return[$row['id_attach']] = array( |
||
1074 | 'name' => $smcFunc['htmlspecialchars']($row['filename']), |
||
1075 | 'size' => $row['size'], |
||
1076 | 'attachID' => $row['id_attach'], |
||
1077 | 'unchecked' => false, |
||
1078 | 'approved' => 1, |
||
1079 | 'mime_type' => $row['mime_type'], |
||
1080 | 'thumb' => $row['id_thumb'], |
||
1081 | ); |
||
1082 | $smcFunc['db_free_result']($request); |
||
1083 | |||
1084 | return $return; |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * Gets all needed message data associated with an attach ID |
||
1089 | * |
||
1090 | * @param int $attachID the attachment ID to load info from. |
||
1091 | * |
||
1092 | * @return array. |
||
0 ignored issues
–
show
|
|||
1093 | */ |
||
1094 | function getAttachMsgInfo($attachID) |
||
1095 | { |
||
1096 | global $smcFunc, $context; |
||
1097 | |||
1098 | if (empty($attachID)) |
||
1099 | return array(); |
||
1100 | |||
1101 | if (!isset($context['loaded_attachments'])) |
||
1102 | $context['loaded_attachments'] = array(); |
||
1103 | |||
1104 | foreach ($context['loaded_attachments'] as $msgRows) |
||
1105 | { |
||
1106 | if (empty($msgRows[$attachID])) |
||
1107 | continue; |
||
1108 | |||
1109 | $row = array( |
||
1110 | 'msg' => $msgRows[$attachID]['id_msg'], |
||
1111 | 'topic' => $msgRows[$attachID]['topic'], |
||
1112 | 'board' => $msgRows[$attachID]['board'], |
||
1113 | ); |
||
1114 | |||
1115 | return $row; |
||
1116 | } |
||
1117 | |||
1118 | $request = $smcFunc['db_query']('', ' |
||
1119 | SELECT a.id_msg AS msg, m.id_topic AS topic, m.id_board AS board |
||
1120 | FROM {db_prefix}attachments AS a |
||
1121 | LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg) |
||
1122 | WHERE id_attach = {int:id_attach} |
||
1123 | LIMIT 1', |
||
1124 | array( |
||
1125 | 'id_attach' => (int) $attachID, |
||
1126 | ) |
||
1127 | ); |
||
1128 | |||
1129 | if ($smcFunc['db_num_rows']($request) != 1) |
||
1130 | return array(); |
||
1131 | |||
1132 | $row = $smcFunc['db_fetch_assoc']($request); |
||
1133 | $smcFunc['db_free_result']($request); |
||
1134 | |||
1135 | return $row; |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * This loads an attachment's contextual data including, most importantly, its size if it is an image. |
||
1140 | * It requires the view_attachments permission to calculate image size. |
||
1141 | * It attempts to keep the "aspect ratio" of the posted image in line, even if it has to be resized by |
||
1142 | * the max_image_width and max_image_height settings. |
||
1143 | * |
||
1144 | * @param int $id_msg ID of the post to load attachments for |
||
1145 | * @param array $attachments An array of already loaded attachments. This function no longer depends on having $topic declared, thus, you need to load the actual topic ID for each attachment. |
||
1146 | * @return array An array of attachment info |
||
1147 | */ |
||
1148 | function loadAttachmentContext($id_msg, $attachments) |
||
1149 | { |
||
1150 | global $modSettings, $txt, $scripturl, $sourcedir, $smcFunc, $context; |
||
1151 | |||
1152 | if (empty($attachments) || empty($attachments[$id_msg])) |
||
1153 | return array(); |
||
1154 | |||
1155 | // Set up the attachment info - based on code by Meriadoc. |
||
1156 | $attachmentData = array(); |
||
1157 | $have_unapproved = false; |
||
1158 | if (isset($attachments[$id_msg]) && !empty($modSettings['attachmentEnable'])) |
||
1159 | { |
||
1160 | foreach ($attachments[$id_msg] as $i => $attachment) |
||
1161 | { |
||
1162 | $attachmentData[$i] = array( |
||
1163 | 'id' => $attachment['id_attach'], |
||
1164 | 'name' => preg_replace('~&#(\\d{1,7}|x[0-9a-fA-F]{1,6});~', '&#\\1;', $smcFunc['htmlspecialchars']($attachment['filename'])), |
||
1165 | 'downloads' => $attachment['downloads'], |
||
1166 | 'size' => ($attachment['filesize'] < 1024000) ? round($attachment['filesize'] / 1024, 2) . ' ' . $txt['kilobyte'] : round($attachment['filesize'] / 1024 / 1024, 2) . ' ' . $txt['megabyte'], |
||
1167 | 'byte_size' => $attachment['filesize'], |
||
1168 | 'href' => $scripturl . '?action=dlattach;topic=' . $attachment['topic'] . '.0;attach=' . $attachment['id_attach'], |
||
1169 | 'link' => '<a href="' . $scripturl . '?action=dlattach;topic=' . $attachment['topic'] . '.0;attach=' . $attachment['id_attach'] . '" class="bbc_link">' . $smcFunc['htmlspecialchars']($attachment['filename']) . '</a>', |
||
1170 | 'is_image' => !empty($attachment['width']) && !empty($attachment['height']) && !empty($modSettings['attachmentShowImages']), |
||
1171 | 'is_approved' => $attachment['approved'], |
||
1172 | 'topic' => $attachment['topic'], |
||
1173 | 'board' => $attachment['board'], |
||
1174 | 'mime_type' => $attachment['mime_type'], |
||
1175 | ); |
||
1176 | |||
1177 | // If something is unapproved we'll note it so we can sort them. |
||
1178 | if (!$attachment['approved']) |
||
1179 | $have_unapproved = true; |
||
1180 | |||
1181 | if (!$attachmentData[$i]['is_image']) |
||
1182 | continue; |
||
1183 | |||
1184 | $attachmentData[$i]['real_width'] = $attachment['width']; |
||
1185 | $attachmentData[$i]['width'] = $attachment['width']; |
||
1186 | $attachmentData[$i]['real_height'] = $attachment['height']; |
||
1187 | $attachmentData[$i]['height'] = $attachment['height']; |
||
1188 | |||
1189 | // Let's see, do we want thumbs? |
||
1190 | if (!empty($modSettings['attachmentThumbnails']) && !empty($modSettings['attachmentThumbWidth']) && !empty($modSettings['attachmentThumbHeight']) && ($attachment['width'] > $modSettings['attachmentThumbWidth'] || $attachment['height'] > $modSettings['attachmentThumbHeight']) && strlen($attachment['filename']) < 249) |
||
1191 | { |
||
1192 | // A proper thumb doesn't exist yet? Create one! |
||
1193 | if (empty($attachment['id_thumb']) || $attachment['thumb_width'] > $modSettings['attachmentThumbWidth'] || $attachment['thumb_height'] > $modSettings['attachmentThumbHeight'] || ($attachment['thumb_width'] < $modSettings['attachmentThumbWidth'] && $attachment['thumb_height'] < $modSettings['attachmentThumbHeight'])) |
||
1194 | { |
||
1195 | $filename = getAttachmentFilename($attachment['filename'], $attachment['id_attach'], $attachment['id_folder']); |
||
1196 | |||
1197 | require_once($sourcedir . '/Subs-Graphics.php'); |
||
1198 | if (createThumbnail($filename, $modSettings['attachmentThumbWidth'], $modSettings['attachmentThumbHeight'])) |
||
1199 | { |
||
1200 | // So what folder are we putting this image in? |
||
1201 | if (!empty($modSettings['currentAttachmentUploadDir'])) |
||
1202 | { |
||
1203 | if (!is_array($modSettings['attachmentUploadDir'])) |
||
1204 | $modSettings['attachmentUploadDir'] = $smcFunc['json_decode']($modSettings['attachmentUploadDir'], true); |
||
1205 | $id_folder_thumb = $modSettings['currentAttachmentUploadDir']; |
||
1206 | } |
||
1207 | else |
||
1208 | { |
||
1209 | $id_folder_thumb = 1; |
||
1210 | } |
||
1211 | |||
1212 | // Calculate the size of the created thumbnail. |
||
1213 | $size = @getimagesize($filename . '_thumb'); |
||
1214 | list ($attachment['thumb_width'], $attachment['thumb_height']) = $size; |
||
1215 | $thumb_size = filesize($filename . '_thumb'); |
||
1216 | |||
1217 | // What about the extension? |
||
1218 | $thumb_ext = isset($context['valid_image_types'][$size[2]]) ? $context['valid_image_types'][$size[2]] : ''; |
||
1219 | |||
1220 | // Figure out the mime type. |
||
1221 | if (!empty($size['mime'])) |
||
1222 | $thumb_mime = $size['mime']; |
||
1223 | else |
||
1224 | $thumb_mime = 'image/' . $thumb_ext; |
||
1225 | |||
1226 | $thumb_filename = $attachment['filename'] . '_thumb'; |
||
1227 | $thumb_hash = getAttachmentFilename($thumb_filename, false, null, true); |
||
1228 | $old_id_thumb = $attachment['id_thumb']; |
||
1229 | |||
1230 | // Add this beauty to the database. |
||
1231 | $attachment['id_thumb'] = $smcFunc['db_insert']('', |
||
1232 | '{db_prefix}attachments', |
||
1233 | array('id_folder' => 'int', 'id_msg' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'size' => 'int', 'width' => 'int', 'height' => 'int', 'fileext' => 'string', 'mime_type' => 'string'), |
||
1234 | array($id_folder_thumb, $id_msg, 3, $thumb_filename, $thumb_hash, (int) $thumb_size, (int) $attachment['thumb_width'], (int) $attachment['thumb_height'], $thumb_ext, $thumb_mime), |
||
1235 | array('id_attach'), |
||
1236 | 1 |
||
1237 | ); |
||
1238 | |||
1239 | if (!empty($attachment['id_thumb'])) |
||
1240 | { |
||
1241 | $smcFunc['db_query']('', ' |
||
1242 | UPDATE {db_prefix}attachments |
||
1243 | SET id_thumb = {int:id_thumb} |
||
1244 | WHERE id_attach = {int:id_attach}', |
||
1245 | array( |
||
1246 | 'id_thumb' => $attachment['id_thumb'], |
||
1247 | 'id_attach' => $attachment['id_attach'], |
||
1248 | ) |
||
1249 | ); |
||
1250 | |||
1251 | $thumb_realname = getAttachmentFilename($thumb_filename, $attachment['id_thumb'], $id_folder_thumb, false, $thumb_hash); |
||
1252 | rename($filename . '_thumb', $thumb_realname); |
||
1253 | |||
1254 | // Do we need to remove an old thumbnail? |
||
1255 | if (!empty($old_id_thumb)) |
||
1256 | { |
||
1257 | require_once($sourcedir . '/ManageAttachments.php'); |
||
1258 | removeAttachments(array('id_attach' => $old_id_thumb), '', false, false); |
||
1259 | } |
||
1260 | } |
||
1261 | } |
||
1262 | } |
||
1263 | |||
1264 | // Only adjust dimensions on successful thumbnail creation. |
||
1265 | if (!empty($attachment['thumb_width']) && !empty($attachment['thumb_height'])) |
||
1266 | { |
||
1267 | $attachmentData[$i]['width'] = $attachment['thumb_width']; |
||
1268 | $attachmentData[$i]['height'] = $attachment['thumb_height']; |
||
1269 | } |
||
1270 | } |
||
1271 | |||
1272 | if (!empty($attachment['id_thumb'])) |
||
1273 | $attachmentData[$i]['thumbnail'] = array( |
||
1274 | 'id' => $attachment['id_thumb'], |
||
1275 | 'href' => $scripturl . '?action=dlattach;topic=' . $attachment['topic'] . '.0;attach=' . $attachment['id_thumb'] . ';image', |
||
1276 | ); |
||
1277 | $attachmentData[$i]['thumbnail']['has_thumb'] = !empty($attachment['id_thumb']); |
||
1278 | |||
1279 | // If thumbnails are disabled, check the maximum size of the image. |
||
1280 | if (!$attachmentData[$i]['thumbnail']['has_thumb'] && ((!empty($modSettings['max_image_width']) && $attachment['width'] > $modSettings['max_image_width']) || (!empty($modSettings['max_image_height']) && $attachment['height'] > $modSettings['max_image_height']))) |
||
1281 | { |
||
1282 | if (!empty($modSettings['max_image_width']) && (empty($modSettings['max_image_height']) || $attachment['height'] * $modSettings['max_image_width'] / $attachment['width'] <= $modSettings['max_image_height'])) |
||
1283 | { |
||
1284 | $attachmentData[$i]['width'] = $modSettings['max_image_width']; |
||
1285 | $attachmentData[$i]['height'] = floor($attachment['height'] * $modSettings['max_image_width'] / $attachment['width']); |
||
1286 | } |
||
1287 | elseif (!empty($modSettings['max_image_width'])) |
||
1288 | { |
||
1289 | $attachmentData[$i]['width'] = floor($attachment['width'] * $modSettings['max_image_height'] / $attachment['height']); |
||
1290 | $attachmentData[$i]['height'] = $modSettings['max_image_height']; |
||
1291 | } |
||
1292 | } |
||
1293 | elseif ($attachmentData[$i]['thumbnail']['has_thumb']) |
||
1294 | { |
||
1295 | // If the image is too large to show inline, make it a popup. |
||
1296 | if (((!empty($modSettings['max_image_width']) && $attachmentData[$i]['real_width'] > $modSettings['max_image_width']) || (!empty($modSettings['max_image_height']) && $attachmentData[$i]['real_height'] > $modSettings['max_image_height']))) |
||
1297 | $attachmentData[$i]['thumbnail']['javascript'] = 'return reqWin(\'' . $attachmentData[$i]['href'] . ';image\', ' . ($attachment['width'] + 20) . ', ' . ($attachment['height'] + 20) . ', true);'; |
||
1298 | else |
||
1299 | $attachmentData[$i]['thumbnail']['javascript'] = 'return expandThumb(' . $attachment['id_attach'] . ');'; |
||
1300 | } |
||
1301 | |||
1302 | if (!$attachmentData[$i]['thumbnail']['has_thumb']) |
||
1303 | $attachmentData[$i]['downloads']++; |
||
1304 | } |
||
1305 | } |
||
1306 | |||
1307 | // Do we need to instigate a sort? |
||
1308 | if ($have_unapproved) |
||
1309 | uasort($attachmentData, function($a, $b) |
||
1310 | { |
||
1311 | if ($a['is_approved'] == $b['is_approved']) |
||
1312 | return 0; |
||
1313 | |||
1314 | return $a['is_approved'] > $b['is_approved'] ? -1 : 1; |
||
1315 | }); |
||
1316 | |||
1317 | return $attachmentData; |
||
1318 | } |
||
1319 | |||
1320 | /** |
||
1321 | * prepare the Attachment api for all messages |
||
1322 | * |
||
1323 | * @param int array $msgIDs the message ID to load info from. |
||
1324 | * |
||
1325 | * @return void. |
||
0 ignored issues
–
show
|
|||
1326 | */ |
||
1327 | function prepareAttachsByMsg($msgIDs) |
||
1328 | { |
||
1329 | global $context, $modSettings, $smcFunc, $user_info; |
||
1330 | |||
1331 | if (empty($context['loaded_attachments'])) |
||
1332 | $context['loaded_attachments'] = array(); |
||
1333 | // Remove all $msgIDs that we already processed |
||
1334 | else |
||
1335 | $msgIDs = array_diff($msgIDs, array_keys($context['loaded_attachments']), array(0)); |
||
1336 | |||
1337 | if (!empty($context['preview_message'])) |
||
1338 | $msgIDs[] = 0; |
||
1339 | |||
1340 | if (!empty($msgIDs)) |
||
1341 | { |
||
1342 | $request = $smcFunc['db_query']('', ' |
||
1343 | SELECT |
||
1344 | a.id_attach, a.id_folder, a.id_msg, a.filename, a.file_hash, COALESCE(a.size, 0) AS filesize, a.downloads, a.approved, m.id_topic AS topic, m.id_board AS board, m.id_member, a.mime_type, |
||
1345 | a.width, a.height' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ', |
||
1346 | COALESCE(thumb.id_attach, 0) AS id_thumb, thumb.width AS thumb_width, thumb.height AS thumb_height') . ' |
||
1347 | FROM {db_prefix}attachments AS a' . (empty($modSettings['attachmentShowImages']) || empty($modSettings['attachmentThumbnails']) ? '' : ' |
||
1348 | LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)') . ' |
||
1349 | LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg) |
||
1350 | WHERE a.attachment_type = {int:attachment_type} |
||
1351 | AND a.id_msg IN ({array_int:message_id})', |
||
1352 | array( |
||
1353 | 'message_id' => $msgIDs, |
||
1354 | 'attachment_type' => 0, |
||
1355 | ) |
||
1356 | ); |
||
1357 | $rows = $smcFunc['db_fetch_all']($request); |
||
1358 | $smcFunc['db_free_result']($request); |
||
1359 | |||
1360 | foreach ($rows as $row) |
||
1361 | { |
||
1362 | // Skip unapproved ones, unless they belong to the user or the user can approve them. |
||
1363 | if (!$row['approved'] && $modSettings['postmod_active'] && !allowedTo('approve_posts') && $row['id_member'] != $user_info['id']) |
||
1364 | continue; |
||
1365 | |||
1366 | if (empty($context['loaded_attachments'][$row['id_msg']])) |
||
1367 | $context['loaded_attachments'][$row['id_msg']] = array(); |
||
1368 | |||
1369 | $context['loaded_attachments'][$row['id_msg']][$row['id_attach']] = $row; |
||
1370 | |||
1371 | // This is better than sorting it with the query... |
||
1372 | ksort($context['loaded_attachments'][$row['id_msg']]); |
||
1373 | } |
||
1374 | } |
||
1375 | } |
||
1376 | |||
1377 | ?> |