| Conditions | 9 |
| Paths | 144 |
| Total Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 28 | function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null, $csp=[]) { |
||
| 29 | global $conf; |
||
| 30 | // send mime headers |
||
| 31 | header("Content-Type: $mime"); |
||
| 32 | |||
| 33 | // send security policy if given |
||
| 34 | if ($csp) dokuwiki\HTTP\Headers::contentSecurityPolicy($csp); |
||
|
|
|||
| 35 | |||
| 36 | // calculate cache times |
||
| 37 | if($cache == -1) { |
||
| 38 | $maxage = max($conf['cachetime'], 3600); // cachetime or one hour |
||
| 39 | $expires = time() + $maxage; |
||
| 40 | } else if($cache > 0) { |
||
| 41 | $maxage = $cache; // given time |
||
| 42 | $expires = time() + $maxage; |
||
| 43 | } else { // $cache == 0 |
||
| 44 | $maxage = 0; |
||
| 45 | $expires = 0; // 1970-01-01 |
||
| 46 | } |
||
| 47 | |||
| 48 | // smart http caching headers |
||
| 49 | if($maxage) { |
||
| 50 | if($public) { |
||
| 51 | // cache publically |
||
| 52 | header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); |
||
| 53 | header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage); |
||
| 54 | } else { |
||
| 55 | // cache in browser |
||
| 56 | header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); |
||
| 57 | header('Cache-Control: private, no-transform, max-age='.$maxage); |
||
| 58 | } |
||
| 59 | } else { |
||
| 60 | // no cache at all |
||
| 61 | header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); |
||
| 62 | header('Cache-Control: no-cache, no-transform'); |
||
| 63 | } |
||
| 64 | |||
| 65 | //send important headers first, script stops here if '304 Not Modified' response |
||
| 66 | $fmtime = @filemtime($file); |
||
| 67 | http_conditionalRequest($fmtime); |
||
| 68 | |||
| 69 | // Use the current $file if is $orig is not set. |
||
| 70 | if ( $orig == null ) { |
||
| 71 | $orig = $file; |
||
| 72 | } |
||
| 73 | |||
| 74 | //download or display? |
||
| 75 | if ($dl) { |
||
| 76 | header('Content-Disposition: attachment;' . rfc2231_encode( |
||
| 77 | 'filename', \dokuwiki\Utf8\PhpString::basename($orig)) . ';' |
||
| 78 | ); |
||
| 79 | } else { |
||
| 80 | header('Content-Disposition: inline;' . rfc2231_encode( |
||
| 81 | 'filename', \dokuwiki\Utf8\PhpString::basename($orig)) . ';' |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | //use x-sendfile header to pass the delivery to compatible webservers |
||
| 86 | http_sendfile($file); |
||
| 87 | |||
| 88 | // send file contents |
||
| 89 | $fp = @fopen($file, "rb"); |
||
| 90 | if($fp) { |
||
| 91 | http_rangeRequest($fp, filesize($file), $mime); |
||
| 92 | } else { |
||
| 93 | http_status(500); |
||
| 94 | print "Could not read $file - bad permissions?"; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 201 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.