@@ -13,8 +13,9 @@ discard block |
||
| 13 | 13 | * @version 2.1 Beta 4 |
| 14 | 14 | */ |
| 15 | 15 | |
| 16 | -if (!defined('SMF')) |
|
| 16 | +if (!defined('SMF')) { |
|
| 17 | 17 | die('No direct access...'); |
| 18 | +} |
|
| 18 | 19 | |
| 19 | 20 | class Attachments |
| 20 | 21 | { |
@@ -70,16 +71,18 @@ discard block |
||
| 70 | 71 | |
| 71 | 72 | $this->_sa = !empty($_REQUEST['sa']) ? $smcFunc['htmlspecialchars']($smcFunc['htmltrim']($_REQUEST['sa'])) : false; |
| 72 | 73 | |
| 73 | - if ($this->_canPostAttachment && $this->_sa && in_array($this->_sa, $this->_subActions)) |
|
| 74 | - $this->{$this->_sa}(); |
|
| 74 | + if ($this->_canPostAttachment && $this->_sa && in_array($this->_sa, $this->_subActions)) { |
|
| 75 | + $this->{$this->_sa}(); |
|
| 76 | + } |
|
| 75 | 77 | |
| 76 | 78 | // Just send a generic message. |
| 77 | - else |
|
| 78 | - $this->setResponse(array( |
|
| 79 | + else { |
|
| 80 | + $this->setResponse(array( |
|
| 79 | 81 | 'text' => $this->_sa == 'add' ? 'attach_error_title' : 'attached_file_deleted_error', |
| 80 | 82 | 'type' => 'error', |
| 81 | 83 | 'data' => false, |
| 82 | 84 | )); |
| 85 | + } |
|
| 83 | 86 | |
| 84 | 87 | // Back to the future, oh, to the browser! |
| 85 | 88 | $this->sendResponse(); |
@@ -95,12 +98,13 @@ discard block |
||
| 95 | 98 | $attachID = !empty($_REQUEST['attach']) && is_numeric($_REQUEST['attach']) ? (int) $_REQUEST['attach'] : 0; |
| 96 | 99 | |
| 97 | 100 | // Need something to work with. |
| 98 | - if (!$attachID || (!empty($_SESSION['already_attached']) && !isset($_SESSION['already_attached'][$attachID]))) |
|
| 99 | - return $this->setResponse(array( |
|
| 101 | + if (!$attachID || (!empty($_SESSION['already_attached']) && !isset($_SESSION['already_attached'][$attachID]))) { |
|
| 102 | + return $this->setResponse(array( |
|
| 100 | 103 | 'text' => 'attached_file_deleted_error', |
| 101 | 104 | 'type' => 'error', |
| 102 | 105 | 'data' => false, |
| 103 | 106 | )); |
| 107 | + } |
|
| 104 | 108 | |
| 105 | 109 | // Lets pass some params and see what happens :P |
| 106 | 110 | $affectedMessage = removeAttachments(array('id_attach' => $attachID), '', true, true); |
@@ -119,19 +123,21 @@ discard block |
||
| 119 | 123 | public function add() |
| 120 | 124 | { |
| 121 | 125 | // You gotta be able to post attachments. |
| 122 | - if (!$this->_canPostAttachment) |
|
| 123 | - return $this->setResponse(array( |
|
| 126 | + if (!$this->_canPostAttachment) { |
|
| 127 | + return $this->setResponse(array( |
|
| 124 | 128 | 'text' => 'attached_file_cannot', |
| 125 | 129 | 'type' => 'error', |
| 126 | 130 | 'data' => false, |
| 127 | 131 | )); |
| 132 | + } |
|
| 128 | 133 | |
| 129 | 134 | // Process them at once! |
| 130 | 135 | $this->processAttachments(); |
| 131 | 136 | |
| 132 | 137 | // The attachments was created and moved the the right folder, time to update the DB. |
| 133 | - if (!empty($_SESSION['temp_attachments'])) |
|
| 134 | - $this->createAtttach(); |
|
| 138 | + if (!empty($_SESSION['temp_attachments'])) { |
|
| 139 | + $this->createAtttach(); |
|
| 140 | + } |
|
| 135 | 141 | |
| 136 | 142 | // Set the response. |
| 137 | 143 | $this->setResponse(); |
@@ -144,8 +150,9 @@ discard block |
||
| 144 | 150 | { |
| 145 | 151 | global $context, $modSettings, $smcFunc, $user_info, $txt; |
| 146 | 152 | |
| 147 | - if (!isset($_FILES['attachment']['name'])) |
|
| 148 | - $_FILES['attachment']['tmp_name'] = array(); |
|
| 153 | + if (!isset($_FILES['attachment']['name'])) { |
|
| 154 | + $_FILES['attachment']['tmp_name'] = array(); |
|
| 155 | + } |
|
| 149 | 156 | |
| 150 | 157 | // If there are attachments, calculate the total size and how many. |
| 151 | 158 | $context['attachments']['total_size'] = 0; |
@@ -155,25 +162,30 @@ discard block |
||
| 155 | 162 | if (isset($_REQUEST['msg'])) |
| 156 | 163 | { |
| 157 | 164 | $context['attachments']['quantity'] = count($context['current_attachments']); |
| 158 | - foreach ($context['current_attachments'] as $attachment) |
|
| 159 | - $context['attachments']['total_size'] += $attachment['size']; |
|
| 165 | + foreach ($context['current_attachments'] as $attachment) { |
|
| 166 | + $context['attachments']['total_size'] += $attachment['size']; |
|
| 167 | + } |
|
| 160 | 168 | } |
| 161 | 169 | |
| 162 | 170 | // A bit of house keeping first. |
| 163 | - if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1) |
|
| 164 | - unset($_SESSION['temp_attachments']); |
|
| 171 | + if (!empty($_SESSION['temp_attachments']) && count($_SESSION['temp_attachments']) == 1) { |
|
| 172 | + unset($_SESSION['temp_attachments']); |
|
| 173 | + } |
|
| 165 | 174 | |
| 166 | 175 | // Our infamous SESSION var, we are gonna have soo much fun with it! |
| 167 | - if (!isset($_SESSION['temp_attachments'])) |
|
| 168 | - $_SESSION['temp_attachments'] = array(); |
|
| 176 | + if (!isset($_SESSION['temp_attachments'])) { |
|
| 177 | + $_SESSION['temp_attachments'] = array(); |
|
| 178 | + } |
|
| 169 | 179 | |
| 170 | 180 | // Make sure we're uploading to the right place. |
| 171 | - if (!empty($modSettings['automanage_attachments'])) |
|
| 172 | - automanage_attachments_check_directory(); |
|
| 181 | + if (!empty($modSettings['automanage_attachments'])) { |
|
| 182 | + automanage_attachments_check_directory(); |
|
| 183 | + } |
|
| 173 | 184 | |
| 174 | 185 | // Is the attachments folder actually there? |
| 175 | - if (!empty($context['dir_creation_error'])) |
|
| 176 | - $this->_generalErrors[] = $context['dir_creation_error']; |
|
| 186 | + if (!empty($context['dir_creation_error'])) { |
|
| 187 | + $this->_generalErrors[] = $context['dir_creation_error']; |
|
| 188 | + } |
|
| 177 | 189 | |
| 178 | 190 | // The current attach folder ha some issues... |
| 179 | 191 | elseif (!is_dir($this->_attchDir)) |
@@ -198,13 +210,12 @@ discard block |
||
| 198 | 210 | ); |
| 199 | 211 | list ($context['attachments']['quantity'], $context['attachments']['total_size']) = $smcFunc['db_fetch_row']($request); |
| 200 | 212 | $smcFunc['db_free_result']($request); |
| 201 | - } |
|
| 202 | - |
|
| 203 | - else |
|
| 204 | - $context['attachments'] = array( |
|
| 213 | + } else { |
|
| 214 | + $context['attachments'] = array( |
|
| 205 | 215 | 'quantity' => 0, |
| 206 | 216 | 'total_size' => 0, |
| 207 | 217 | ); |
| 218 | + } |
|
| 208 | 219 | |
| 209 | 220 | // Check for other general errors here. |
| 210 | 221 | |
@@ -212,9 +223,10 @@ discard block |
||
| 212 | 223 | if (!empty($this->_generalErrors)) |
| 213 | 224 | { |
| 214 | 225 | // And delete the files 'cos they ain't going nowhere. |
| 215 | - foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
|
| 216 | - if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
|
| 226 | + foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) { |
|
| 227 | + if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
|
| 217 | 228 | unlink($_FILES['attachment']['tmp_name'][$n]); |
| 229 | + } |
|
| 218 | 230 | |
| 219 | 231 | $_FILES['attachment']['tmp_name'] = array(); |
| 220 | 232 | |
@@ -225,26 +237,29 @@ discard block |
||
| 225 | 237 | // Loop through $_FILES['attachment'] array and move each file to the current attachments folder. |
| 226 | 238 | foreach ($_FILES['attachment']['tmp_name'] as $n => $dummy) |
| 227 | 239 | { |
| 228 | - if ($_FILES['attachment']['name'][$n] == '') |
|
| 229 | - continue; |
|
| 240 | + if ($_FILES['attachment']['name'][$n] == '') { |
|
| 241 | + continue; |
|
| 242 | + } |
|
| 230 | 243 | |
| 231 | 244 | // First, let's first check for PHP upload errors. |
| 232 | 245 | $errors = array(); |
| 233 | 246 | if (!empty($_FILES['attachment']['error'][$n])) |
| 234 | 247 | { |
| 235 | - if ($_FILES['attachment']['error'][$n] == 2) |
|
| 236 | - $errors[] = array('file_too_big', array($modSettings['attachmentSizeLimit'])); |
|
| 237 | - |
|
| 238 | - else |
|
| 239 | - log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$n]]); |
|
| 248 | + if ($_FILES['attachment']['error'][$n] == 2) { |
|
| 249 | + $errors[] = array('file_too_big', array($modSettings['attachmentSizeLimit'])); |
|
| 250 | + } else { |
|
| 251 | + log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_' . $_FILES['attachment']['error'][$n]]); |
|
| 252 | + } |
|
| 240 | 253 | |
| 241 | 254 | // Log this one, because... |
| 242 | - if ($_FILES['attachment']['error'][$n] == 6) |
|
| 243 | - log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_6'], 'critical'); |
|
| 255 | + if ($_FILES['attachment']['error'][$n] == 6) { |
|
| 256 | + log_error($_FILES['attachment']['name'][$n] . ': ' . $txt['php_upload_error_6'], 'critical'); |
|
| 257 | + } |
|
| 244 | 258 | |
| 245 | 259 | // Weird, no errors were cached, still fill out a generic one. |
| 246 | - if (empty($errors)) |
|
| 247 | - $errors[] = 'attach_php_error'; |
|
| 260 | + if (empty($errors)) { |
|
| 261 | + $errors[] = 'attach_php_error'; |
|
| 262 | + } |
|
| 248 | 263 | } |
| 249 | 264 | |
| 250 | 265 | // Try to move and rename the file before doing any more checks on it. |
@@ -256,8 +271,9 @@ discard block |
||
| 256 | 271 | { |
| 257 | 272 | // The reported MIME type of the attachment might not be reliable. |
| 258 | 273 | // Fortunately, PHP 5.3+ lets us easily verify the real MIME type. |
| 259 | - if (function_exists('mime_content_type')) |
|
| 260 | - $_FILES['attachment']['type'][$n] = mime_content_type($_FILES['attachment']['tmp_name'][$n]); |
|
| 274 | + if (function_exists('mime_content_type')) { |
|
| 275 | + $_FILES['attachment']['type'][$n] = mime_content_type($_FILES['attachment']['tmp_name'][$n]); |
|
| 276 | + } |
|
| 261 | 277 | |
| 262 | 278 | $_SESSION['temp_attachments'][$attachID] = array( |
| 263 | 279 | 'name' => $smcFunc['htmlspecialchars'](basename($_FILES['attachment']['name'][$n])), |
@@ -269,16 +285,18 @@ discard block |
||
| 269 | 285 | ); |
| 270 | 286 | |
| 271 | 287 | // Move the file to the attachments folder with a temp name for now. |
| 272 | - if (@move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName)) |
|
| 273 | - smf_chmod($destName, 0644); |
|
| 288 | + if (@move_uploaded_file($_FILES['attachment']['tmp_name'][$n], $destName)) { |
|
| 289 | + smf_chmod($destName, 0644); |
|
| 290 | + } |
|
| 274 | 291 | |
| 275 | 292 | // This is madness!! |
| 276 | 293 | else |
| 277 | 294 | { |
| 278 | 295 | // File couldn't be moved. |
| 279 | 296 | $_SESSION['temp_attachments'][$attachID]['errors'][] = 'attach_timeout'; |
| 280 | - if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
|
| 281 | - unlink($_FILES['attachment']['tmp_name'][$n]); |
|
| 297 | + if (file_exists($_FILES['attachment']['tmp_name'][$n])) { |
|
| 298 | + unlink($_FILES['attachment']['tmp_name'][$n]); |
|
| 299 | + } |
|
| 282 | 300 | } |
| 283 | 301 | } |
| 284 | 302 | |
@@ -291,13 +309,15 @@ discard block |
||
| 291 | 309 | 'errors' => $errors, |
| 292 | 310 | ); |
| 293 | 311 | |
| 294 | - if (file_exists($_FILES['attachment']['tmp_name'][$n])) |
|
| 295 | - unlink($_FILES['attachment']['tmp_name'][$n]); |
|
| 312 | + if (file_exists($_FILES['attachment']['tmp_name'][$n])) { |
|
| 313 | + unlink($_FILES['attachment']['tmp_name'][$n]); |
|
| 314 | + } |
|
| 296 | 315 | } |
| 297 | 316 | |
| 298 | 317 | // If there's no errors to this point. We still do need to apply some additional checks before we are finished. |
| 299 | - if (empty($_SESSION['temp_attachments'][$attachID]['errors'])) |
|
| 300 | - attachmentChecks($attachID); |
|
| 318 | + if (empty($_SESSION['temp_attachments'][$attachID]['errors'])) { |
|
| 319 | + attachmentChecks($attachID); |
|
| 320 | + } |
|
| 301 | 321 | } |
| 302 | 322 | |
| 303 | 323 | // Mod authors, finally a hook to hang an alternate attachment upload system upon |
@@ -344,14 +364,15 @@ discard block |
||
| 344 | 364 | |
| 345 | 365 | $_SESSION['already_attached'][$attachmentOptions['attachID']] = $attachmentOptions['attachID']; |
| 346 | 366 | |
| 347 | - if (!empty($attachmentOptions['thumb'])) |
|
| 348 | - $_SESSION['already_attached'][$attachmentOptions['thumb']] = $attachmentOptions['thumb']; |
|
| 367 | + if (!empty($attachmentOptions['thumb'])) { |
|
| 368 | + $_SESSION['already_attached'][$attachmentOptions['thumb']] = $attachmentOptions['thumb']; |
|
| 369 | + } |
|
| 349 | 370 | |
| 350 | - if ($this->_msg) |
|
| 351 | - assignAttachments($_SESSION['already_attached'], $this->_msg); |
|
| 371 | + if ($this->_msg) { |
|
| 372 | + assignAttachments($_SESSION['already_attached'], $this->_msg); |
|
| 373 | + } |
|
| 352 | 374 | } |
| 353 | - } |
|
| 354 | - else |
|
| 375 | + } else |
|
| 355 | 376 | { |
| 356 | 377 | // Sort out the errors for display and delete any associated files. |
| 357 | 378 | $log_these = array('attachments_no_create', 'attachments_no_write', 'attach_timeout', 'ran_out_of_space', 'cant_access_upload_path', 'attach_0_byte_file'); |
@@ -363,14 +384,16 @@ discard block |
||
| 363 | 384 | if (!is_array($error)) |
| 364 | 385 | { |
| 365 | 386 | $attachmentOptions['errors'][] = $txt[$error]; |
| 366 | - if (in_array($error, $log_these)) |
|
| 367 | - log_error($attachment['name'] . ': ' . $txt[$error], 'critical'); |
|
| 387 | + if (in_array($error, $log_these)) { |
|
| 388 | + log_error($attachment['name'] . ': ' . $txt[$error], 'critical'); |
|
| 389 | + } |
|
| 390 | + } else { |
|
| 391 | + $attachmentOptions['errors'][] = vsprintf($txt[$error[0]], $error[1]); |
|
| 368 | 392 | } |
| 369 | - else |
|
| 370 | - $attachmentOptions['errors'][] = vsprintf($txt[$error[0]], $error[1]); |
|
| 371 | 393 | } |
| 372 | - if (file_exists($attachment['tmp_name'])) |
|
| 373 | - unlink($attachment['tmp_name']); |
|
| 394 | + if (file_exists($attachment['tmp_name'])) { |
|
| 395 | + unlink($attachment['tmp_name']); |
|
| 396 | + } |
|
| 374 | 397 | } |
| 375 | 398 | |
| 376 | 399 | // You don't need to know. |
@@ -382,8 +405,9 @@ discard block |
||
| 382 | 405 | } |
| 383 | 406 | |
| 384 | 407 | // Temp save this on the db. |
| 385 | - if (!empty($_SESSION['already_attached'])) |
|
| 386 | - $this->_attachSuccess = $_SESSION['already_attached']; |
|
| 408 | + if (!empty($_SESSION['already_attached'])) { |
|
| 409 | + $this->_attachSuccess = $_SESSION['already_attached']; |
|
| 410 | + } |
|
| 387 | 411 | |
| 388 | 412 | unset($_SESSION['temp_attachments']); |
| 389 | 413 | } |
@@ -403,14 +427,16 @@ discard block |
||
| 403 | 427 | if ($this->_sa == 'add') |
| 404 | 428 | { |
| 405 | 429 | // Is there any generic errors? made some sense out of them! |
| 406 | - if ($this->_generalErrors) |
|
| 407 | - foreach ($this->_generalErrors as $k => $v) |
|
| 430 | + if ($this->_generalErrors) { |
|
| 431 | + foreach ($this->_generalErrors as $k => $v) |
|
| 408 | 432 | $this->_generalErrors[$k] = (is_array($v) ? vsprintf($txt[$v[0]], $v[1]) : $txt[$v]); |
| 433 | + } |
|
| 409 | 434 | |
| 410 | 435 | // Gotta urlencode the filename. |
| 411 | - if ($this->_attachResults) |
|
| 412 | - foreach ($this->_attachResults as $k => $v) |
|
| 436 | + if ($this->_attachResults) { |
|
| 437 | + foreach ($this->_attachResults as $k => $v) |
|
| 413 | 438 | $this->_attachResults[$k]['name'] = urlencode($this->_attachResults[$k]['name']); |
| 439 | + } |
|
| 414 | 440 | |
| 415 | 441 | $this->_response = array( |
| 416 | 442 | 'files' => $this->_attachResults ? $this->_attachResults : false, |
@@ -419,9 +445,10 @@ discard block |
||
| 419 | 445 | } |
| 420 | 446 | |
| 421 | 447 | // Rest of us mere mortals gets no special treatment... |
| 422 | - elseif (!empty($data)) |
|
| 423 | - if (!empty($data['text']) && !empty($txt[$data['text']])) |
|
| 448 | + elseif (!empty($data)) { |
|
| 449 | + if (!empty($data['text']) && !empty($txt[$data['text']])) |
|
| 424 | 450 | $this->_response['text'] = $txt[$data['text']]; |
| 451 | + } |
|
| 425 | 452 | } |
| 426 | 453 | |
| 427 | 454 | protected function sendResponse() |
@@ -430,11 +457,11 @@ discard block |
||
| 430 | 457 | |
| 431 | 458 | ob_end_clean(); |
| 432 | 459 | |
| 433 | - if (!empty($modSettings['CompressedOutput'])) |
|
| 434 | - @ob_start('ob_gzhandler'); |
|
| 435 | - |
|
| 436 | - else |
|
| 437 | - ob_start(); |
|
| 460 | + if (!empty($modSettings['CompressedOutput'])) { |
|
| 461 | + @ob_start('ob_gzhandler'); |
|
| 462 | + } else { |
|
| 463 | + ob_start(); |
|
| 464 | + } |
|
| 438 | 465 | |
| 439 | 466 | // Set the header. |
| 440 | 467 | header('Content-Type: application/json; charset='. $context['character_set'] .''); |