| Conditions | 30 |
| Paths | 0 |
| Total Lines | 109 |
| Code Lines | 72 |
| 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 |
||
| 83 | |||
| 84 | public function giveFile($file) { |
||
| 85 | if (!file_exists($file) && file_exists(mb_convert_encoding($file, 'Windows-1251', 'UTF-8'))) { |
||
| 86 | $file = mb_convert_encoding($file, 'Windows-1251', 'UTF-8'); |
||
| 87 | } |
||
| 88 | if (!file_exists($file)) { |
||
| 89 | header('HTTP/1.1 404 Not Found'); |
||
| 90 | exit(); |
||
| 91 | } |
||
| 92 | |||
| 93 | $fileinfo = pathinfo($file); |
||
| 94 | if (empty($fileinfo['extension'])) { |
||
| 95 | header('HTTP/1.1 404 Not Found'); |
||
| 96 | exit(); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!empty($_GET['resize'])) { |
||
| 100 | |||
| 101 | $allow_resize = false; |
||
| 102 | if (App::$cur->db->connect) { |
||
| 103 | $type = Files\Type::get($fileinfo['extension'], 'ext'); |
||
| 104 | $allow_resize = $type ? $type->allow_resize : false; |
||
| 105 | } |
||
| 106 | if (!$type && in_array(strtolower($fileinfo['extension']), array('png', 'jpg', 'jpeg', 'gif'))) { |
||
| 107 | $allow_resize = true; |
||
| 108 | } |
||
| 109 | if ($allow_resize) { |
||
| 110 | |||
| 111 | $sizes = explode('x', $_GET['resize']); |
||
| 112 | $sizes[0] = intval($sizes[0]); |
||
| 113 | if (isset($sizes[1])) { |
||
| 114 | $sizes[1] = intval($sizes[1]); |
||
| 115 | } else { |
||
| 116 | $sizes[1] = 0; |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!$sizes[0] || !$sizes[1]) { |
||
| 120 | header('HTTP/1.1 404 Not Found'); |
||
| 121 | exit(); |
||
| 122 | } elseif ($sizes[0] > 2000 || $sizes[1] > 2000) { |
||
| 123 | header('HTTP/1.1 404 Not Found'); |
||
| 124 | exit(); |
||
| 125 | } else { |
||
| 126 | $dir = App::$primary->path; |
||
| 127 | |||
| 128 | if (!empty($_GET['resize_crop'])) { |
||
| 129 | if (in_array($_GET['resize_crop'], array('q', 'c'))) { |
||
| 130 | $crop = $_GET['resize_crop']; |
||
| 131 | } else { |
||
| 132 | $crop = 'c'; |
||
| 133 | } |
||
| 134 | } elseif (!empty($_GET['resize_quadro'])) { |
||
| 135 | $crop = 'q'; |
||
| 136 | } else { |
||
| 137 | $crop = ''; |
||
| 138 | } |
||
| 139 | $pos = 'center'; |
||
| 140 | if (!empty($_GET['resize_pos']) && in_array($_GET['resize_pos'], array('top', 'center'))) { |
||
| 141 | $pos = $_GET['resize_pos']; |
||
| 142 | } |
||
| 143 | $file = INJI_BASE_DIR . rtrim(Statics::file($file, $sizes[0] . 'x' . $sizes[1], $crop, $pos, true), '/'); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | $request = getallheaders(); |
||
| 149 | if (isset($request['If-Modified-Since'])) { |
||
| 150 | // Разделяем If-Modified-Since (Netscape < v6 отдаёт их неправильно) |
||
| 151 | $modifiedSince = explode(';', $request['If-Modified-Since']); |
||
| 152 | |||
| 153 | // Преобразуем запрос клиента If-Modified-Since в таймштамп |
||
| 154 | $modifiedSince = strtotime($modifiedSince[0]); |
||
| 155 | } else { |
||
| 156 | // Устанавливаем время модификации в ноль |
||
| 157 | $modifiedSince = 0; |
||
| 158 | } |
||
| 159 | |||
| 160 | header("Cache-control: public"); |
||
| 161 | header("Accept-Ranges: bytes"); |
||
| 162 | header("Pragma: public"); |
||
| 163 | header("Content-Length: " . filesize($file)); |
||
| 164 | header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($file)) . ' GMT'); |
||
| 165 | header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 60 * 60 * 24 * 256) . ' GMT'); |
||
| 166 | if (filemtime($file) <= $modifiedSince && (!isset($_SERVER['HTTP_CACHE_CONTROL']) || $_SERVER['HTTP_CACHE_CONTROL'] != 'no-cache')) { |
||
| 167 | // Разгружаем канал передачи данных! |
||
| 168 | header('HTTP/1.1 304 Not Modified'); |
||
| 169 | exit(); |
||
| 170 | } |
||
| 171 | |||
| 172 | header('Content-Description: File Transfer'); |
||
| 173 | if (isset($this->mimes[strtolower($fileinfo['extension'])])) { |
||
| 174 | header("Content-Type: " . $this->mimes[strtolower($fileinfo['extension'])]); |
||
| 175 | } |
||
| 176 | |||
| 177 | if (isset($_GET['frustrate_dl']) || in_array($fileinfo['extension'], array('doc', 'docx', 'xls', 'xlsx'))) { |
||
| 178 | |||
| 179 | $fileName = $fileinfo['filename'] . '.' . $fileinfo['extension']; |
||
| 180 | if (App::$cur->db->connect) { |
||
| 181 | $fileObj = Files\File::get(['path', '%/' . $fileinfo['filename'] . '.' . $fileinfo['extension'], 'LIKE']); |
||
| 182 | if ($fileObj) { |
||
| 183 | $fileName = $fileObj->original_name; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | header('Content-Disposition: attachment; filename="' . $fileName . '"'); |
||
| 187 | } |
||
| 188 | |||
| 189 | header('Content-Transfer-Encoding: binary'); |
||
| 190 | |||
| 191 | readfile($file); |
||
| 192 | exit(); |
||
| 194 | } |