| Conditions | 28 |
| Paths | 596 |
| Total Lines | 160 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 272 | public function action_sportal_attach() |
||
| 273 | { |
||
| 274 | global $txt, $modSettings, $context; |
||
| 275 | |||
| 276 | // Some defaults that we need. |
||
| 277 | $context['no_last_modified'] = true; |
||
| 278 | |||
| 279 | // Make sure some attachment was requested and they can view them |
||
| 280 | if (!isset($_GET['article'], $_GET['attach'])) |
||
| 281 | { |
||
| 282 | throw new Elk_Exception('no_access', false); |
||
| 283 | } |
||
| 284 | |||
| 285 | // No funny business, you need to have access to the article to see its attachments |
||
| 286 | if (sportal_article_access($_GET['article']) === false) |
||
| 287 | { |
||
| 288 | throw new Elk_Exception('no_access', false); |
||
| 289 | } |
||
| 290 | |||
| 291 | // We need to do some work on attachments. |
||
| 292 | $id_article = (int) $_GET['article']; |
||
| 293 | $id_attach = (int) $_GET['attach']; |
||
| 294 | |||
| 295 | if (isset($_GET['thumb'])) |
||
| 296 | $attachment = sportal_get_attachment_thumb_from_article($id_article, $id_attach); |
||
| 297 | else |
||
| 298 | $attachment = sportal_get_attachment_from_article($id_article, $id_attach); |
||
| 299 | |||
| 300 | if (empty($attachment)) |
||
| 301 | { |
||
| 302 | throw new Elk_Exception('no_access', false); |
||
| 303 | } |
||
| 304 | |||
| 305 | list ($real_filename, $file_hash, $file_ext, $id_attach, $attachment_type, $mime_type, $width, $height) = $attachment; |
||
| 306 | $filename = $modSettings['sp_articles_attachment_dir'] . '/' . $id_attach . '_' . $file_hash . '.elk'; |
||
| 307 | |||
| 308 | // This is done to clear any output that was made before now. |
||
| 309 | while (ob_get_level() > 0) |
||
| 310 | { |
||
| 311 | @ob_end_clean(); |
||
| 312 | } |
||
| 313 | |||
| 314 | ob_start(); |
||
| 315 | header('Content-Encoding: none'); |
||
| 316 | |||
| 317 | // No point in a nicer message, because this is supposed to be an attachment anyway... |
||
| 318 | if (!file_exists($filename)) |
||
| 319 | { |
||
| 320 | loadLanguage('Errors'); |
||
| 321 | |||
| 322 | header((preg_match('~HTTP/1\.[01]~i', $_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0') . ' 404 Not Found'); |
||
| 323 | header('Content-Type: text/plain; charset=UTF-8'); |
||
| 324 | |||
| 325 | // We need to die like this *before* we send any anti-caching headers as below. |
||
| 326 | die('404 - ' . $txt['attachment_not_found']); |
||
| 327 | } |
||
| 328 | |||
| 329 | // If it hasn't been modified since the last time this attachment was retrieved, |
||
| 330 | // there's no need to display it again. |
||
| 331 | if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) |
||
| 332 | { |
||
| 333 | list ($modified_since) = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']); |
||
| 334 | if (strtotime($modified_since) >= filemtime($filename)) |
||
| 335 | { |
||
| 336 | @ob_end_clean(); |
||
| 337 | |||
| 338 | // Answer the question - no, it hasn't been modified ;). |
||
| 339 | header('HTTP/1.1 304 Not Modified'); |
||
| 340 | } |
||
| 341 | exit(0); |
||
|
|
|||
| 342 | } |
||
| 343 | |||
| 344 | // Check whether the ETag was sent back, and cache based on that... |
||
| 345 | $eTag = '"' . substr($id_attach . $real_filename . filemtime($filename), 0, 64) . '"'; |
||
| 346 | if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && strpos($_SERVER['HTTP_IF_NONE_MATCH'], $eTag) !== false) |
||
| 347 | { |
||
| 348 | @ob_end_clean(); |
||
| 349 | |||
| 350 | header('HTTP/1.1 304 Not Modified'); |
||
| 351 | exit(0); |
||
| 352 | } |
||
| 353 | |||
| 354 | // Send the attachment headers. |
||
| 355 | header('Content-Transfer-Encoding: binary'); |
||
| 356 | header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 525600 * 60) . ' GMT'); |
||
| 357 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filename)) . ' GMT'); |
||
| 358 | header('Accept-Ranges: bytes'); |
||
| 359 | header('Connection: close'); |
||
| 360 | header('ETag: ' . $eTag); |
||
| 361 | |||
| 362 | // Make sure the mime type warrants an inline display. |
||
| 363 | if (isset($_GET['image']) && !empty($mime_type) && strpos($mime_type, 'image/') !== 0) |
||
| 364 | { |
||
| 365 | unset($_GET['image']); |
||
| 366 | } |
||
| 367 | // Does this have a mime type? |
||
| 368 | elseif (!empty($mime_type) && (isset($_GET['image']) || !in_array($file_ext, array('jpg', 'gif', 'jpeg', 'x-ms-bmp', 'png', 'psd', 'tiff', 'iff')))) |
||
| 369 | { |
||
| 370 | header('Content-Type: ' . strtr($mime_type, array('image/bmp' => 'image/x-ms-bmp'))); |
||
| 371 | } |
||
| 372 | else |
||
| 373 | { |
||
| 374 | header('Content-Type: application/octet-stream'); |
||
| 375 | } |
||
| 376 | |||
| 377 | $disposition = !isset($_GET['image']) ? 'attachment' : 'inline'; |
||
| 378 | $fileName = str_replace('"', '', $filename); |
||
| 379 | |||
| 380 | // Send as UTF-8 if the name requires that |
||
| 381 | $altName = ''; |
||
| 382 | if (preg_match('~[\x80-\xFF]~', $fileName)) |
||
| 383 | { |
||
| 384 | $altName = "; filename*=UTF-8''" . rawurlencode($fileName); |
||
| 385 | } |
||
| 386 | header('Content-Disposition: ' . $disposition . '; filename="' . $fileName . '"' . $altName); |
||
| 387 | |||
| 388 | // If this has an "image extension" - but isn't actually an image - then ensure it isn't cached cause of silly IE. |
||
| 389 | if (!isset($_GET['image']) && in_array($file_ext, array('gif', 'jpg', 'bmp', 'png', 'jpeg', 'tiff'))) |
||
| 390 | { |
||
| 391 | header('Pragma: no-cache'); |
||
| 392 | header('Cache-Control: no-cache'); |
||
| 393 | } |
||
| 394 | else |
||
| 395 | { |
||
| 396 | header('Cache-Control: max-age=' . (525600 * 60) . ', private'); |
||
| 397 | } |
||
| 398 | |||
| 399 | if (empty($modSettings['enableCompressedOutput']) || filesize($filename) > 4194304) |
||
| 400 | { |
||
| 401 | header('Content-Length: ' . filesize($filename)); |
||
| 402 | } |
||
| 403 | |||
| 404 | // Try to buy some time... |
||
| 405 | @set_time_limit(600); |
||
| 406 | |||
| 407 | // Since we don't do output compression for files this large... |
||
| 408 | if (filesize($filename) > 4194304) |
||
| 409 | { |
||
| 410 | // Forcibly end any output buffering going on. |
||
| 411 | while (ob_get_level() > 0) |
||
| 412 | { |
||
| 413 | @ob_end_clean(); |
||
| 414 | } |
||
| 415 | |||
| 416 | $fp = fopen($filename, 'rb'); |
||
| 417 | while (!feof($fp)) |
||
| 418 | { |
||
| 419 | echo fread($fp, 8192); |
||
| 420 | |||
| 421 | flush(); |
||
| 422 | } |
||
| 423 | fclose($fp); |
||
| 424 | } |
||
| 425 | // On some of the less-bright hosts, readfile() is disabled. It's just a faster, more byte safe, version of what's in the if. |
||
| 426 | elseif (@readfile($filename) === null) |
||
| 427 | { |
||
| 428 | echo file_get_contents($filename); |
||
| 429 | } |
||
| 430 | |||
| 431 | obExit(false); |
||
| 432 | } |
||
| 434 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.