[5307] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Download.class.php |
---|
| 4 | */ |
---|
| 5 | /** |
---|
| 6 | * Download de Arquivos |
---|
| 7 | * |
---|
| 8 | * Formulï¿œrios |
---|
| 9 | * <code> |
---|
| 10 | * include '../lib/config.php'; |
---|
| 11 | * include '../includes/classes/Download.class.php'; |
---|
| 12 | * |
---|
| 13 | * $down = new Download('my_files/', 'true'); |
---|
| 14 | * $down-> doDownload('tazmania2.jpg',true); |
---|
| 15 | * |
---|
| 16 | * if($down->getSysError() != ''){ |
---|
| 17 | * echo $down->getSysDebug(); |
---|
| 18 | * echo $down->getSysError(); |
---|
| 19 | * } |
---|
| 20 | * |
---|
| 21 | * </code> |
---|
| 22 | * |
---|
| 23 | * |
---|
| 24 | * |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | class Download { |
---|
| 28 | |
---|
| 29 | public $sysError = ''; |
---|
| 30 | public $sysDebug = ''; |
---|
| 31 | |
---|
| 32 | private $_pathFolder = ''; |
---|
| 33 | private $_pathBase = ""; |
---|
| 34 | private $_file = ""; |
---|
| 35 | private $_fileExtension = ''; |
---|
| 36 | private $_fileMime = ''; |
---|
| 37 | |
---|
| 38 | private $_fileSize = 0; |
---|
| 39 | private $_forceDownload = true; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * ᅵ Construtor do metodo: |
---|
| 43 | * <code> |
---|
| 44 | * $down = $down = new Download('my_files/', 'true'); |
---|
| 45 | * </code> |
---|
| 46 | *ᅵ |
---|
| 47 | * @param string $path Pasta de onde serᅵ feito Donwload |
---|
| 48 | * @param string $base 'true'/'false' caminho true (/var/www/) caminho false |
---|
| 49 | * (/var/www/html/site.../) |
---|
| 50 | */ |
---|
| 51 | |
---|
| 52 | function Download($path, $base) { |
---|
| 53 | |
---|
| 54 | $this->sysDebug .= '<b>Download</b><br>'; |
---|
| 55 | |
---|
| 56 | try{ |
---|
| 57 | //pastas que sï¿œo permitidas fazer download |
---|
| 58 | $arrFolders = array(); |
---|
| 59 | $arrFolders[] = '/'; |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | if(trim($path) != '' && in_array(trim($path), $arrFolders)){ |
---|
| 63 | $this->_pathFolder = trim($path); |
---|
| 64 | $this->sysDebug .= '<b> Pasta: </b> ' . $this->_pathFolder . '<br>'; |
---|
| 65 | }/* else { |
---|
| 66 | $this->sysDebug .= '<b> Pasta Invalida: </b> [' . $path . ']<br>'; |
---|
| 67 | throw new exception(' caminho [' . $path . '] ᅵ invalido.'); |
---|
| 68 | } |
---|
| 69 | */ |
---|
| 70 | if(trim($base) != '' && (trim($base) == 'true' || trim($base) == 'false') ){ |
---|
| 71 | if(trim($base) == 'true'){ |
---|
| 72 | $this->_pathBase = ''; |
---|
| 73 | } else { |
---|
| 74 | $this->_pathBase = _SITEDIR_; |
---|
| 75 | } |
---|
| 76 | $this->sysDebug .= '<b> Base: </b> ' . $this->_pathBase . '<br>'; |
---|
| 77 | } else { |
---|
| 78 | $this->sysDebug .= '<b> Base Invalida: </b> [' . $base . ']<br>'; |
---|
| 79 | throw new exception('Base [' . $base . '] ᅵ invalido.'); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /*if(!is_dir($this->_pathBase . $this->_pathFolder)){ |
---|
| 83 | $this->sysDebug .= '<b> Caminho nï¿œo corresponde a uma pasta valida: </b> [' . $this->_pathBase . $this->_pathFolder . ']<br>'; |
---|
| 84 | throw new exception(' Caminho nï¿œo corresponde a uma pasta valida: </b> [' . $this->_pathBase . $this->_pathFolder . '].'); |
---|
| 85 | } */ |
---|
| 86 | |
---|
| 87 | } catch (exception $e) { |
---|
| 88 | |
---|
| 89 | $this->sysError = '<b>Erro: </b>' . $e->getMessage() . '<br>'; |
---|
| 90 | $this->sysDebug .= '<b>Thows Construtor Exit</b> <br>'; |
---|
| 91 | |
---|
| 92 | $fp = fopen ("/tmp/logDownloadsIndevidos","a+"); |
---|
| 93 | $writeDebug = "Data : " . date('d/m/Y H:m:s') . |
---|
| 94 | " \nPagina : " . $_SERVER['PHP_SELF'] . |
---|
| 95 | " \nIP : " . $_SERVER['REMOTE_ADDR'] . |
---|
| 96 | " \nUsuoid : " . $_SESSION['usuario']['oid'] . |
---|
| 97 | "\n" . $this->sysDebug . " \n\n"; |
---|
| 98 | $writeDebug = strip_tags(html_entity_decode(str_ireplace('<br>', "\n", $writeDebug), ENT_NOQUOTES, 'ISO8859-1')); |
---|
| 99 | fwrite($fp, $writeDebug ); |
---|
| 100 | fclose($fp); |
---|
| 101 | |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * ᅵ Meodo de download de arquivos |
---|
| 108 | * <code> |
---|
| 109 | * $down-> doDownload('file.ext'); |
---|
| 110 | * </code> |
---|
| 111 | *ᅵ |
---|
| 112 | * @param string $file Arquivo que serᅵ feito download |
---|
| 113 | */ |
---|
| 114 | function doDownload($file, $forceDownload = true){ |
---|
| 115 | try{ |
---|
| 116 | |
---|
| 117 | $this->_forceDownload = $forceDownload; |
---|
| 118 | |
---|
| 119 | if(trim($file) != ''){ |
---|
| 120 | $this->_file = trim($file); |
---|
| 121 | $this->sysDebug .= '<b> File Name: </b> [' . $this->_file . ']<br>'; |
---|
| 122 | } else { |
---|
| 123 | $this->sysDebug .= '<b> Base Invalida: </b> [' . $file . ']<br>'; |
---|
| 124 | throw new exception(' Arquivo [' . $file . '] ᅵ invalido.'); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | preg_match('/\.([^\.]*$)/', $this->_file, $extension); |
---|
| 130 | $this->_fileExtension = strtolower(substr($extension[0], 1, strlen($extension[0]))); |
---|
| 131 | $this->sysDebug .= '<b> Formato do Arquivo: </b> [' . $this->_fileExtension . ']<br>'; |
---|
| 132 | |
---|
| 133 | if($this->_fileExtension == 'php'){ |
---|
| 134 | $this->sysDebug .= '<b> Formato de Arquivo invalido: </b> [' . $this->_fileExtension . ']<br>'; |
---|
| 135 | throw new exception('Arquivo invalido.'); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | if(strstr( $this->_pathBase . $this->_pathFolder . $this->_file, '../' )){ |
---|
| 139 | $this->sysDebug .= '<b> Pasta contem caracteres invalidos: </b> [' . $this->_pathBase . $this->_pathFolder . $this->_file . ']<br>'; |
---|
| 140 | throw new exception(' caminho [' . $this->_pathBase . $this->_pathFolder . $this->_file . '] ᅵ invalido.'); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | if(!file_exists($this->_pathBase . $this->_pathFolder . $this->_file)){ |
---|
| 144 | $this->sysDebug .= '<b> Arquivo nï¿œo encontrado: </b> [' . $this->_pathBase . $this->_pathFolder . $this->_file . ']<br>'; |
---|
| 145 | throw new exception('Arquivo nï¿œo encontrado.'); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | $sizeFile = filesize($this->_pathBase . $this->_pathFolder . $this->_file); |
---|
| 149 | if($sizeFile > 0 ){ |
---|
| 150 | $this->_fileSize = $sizeFile; |
---|
| 151 | $this->sysDebug .= '<b> Arquivo com tamanho </b> [' . $this->_fileSize . '] bytes<br>'; |
---|
| 152 | } else { |
---|
| 153 | $this->sysDebug .= '<b> Arquivo com tamanho 0 bytes: </b><br>'; |
---|
| 154 | throw new exception('Arquivo truncado.'); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | $arrExtensions = array(); |
---|
| 158 | $arrExtensions[] = 'jpg'; |
---|
| 159 | $arrExtensions[] = 'jpeg'; |
---|
| 160 | $arrExtensions[] = 'jpe'; |
---|
| 161 | $arrExtensions[] = 'gif'; |
---|
| 162 | $arrExtensions[] = 'png'; |
---|
| 163 | $arrExtensions[] = 'bmp'; |
---|
| 164 | $arrExtensions[] = 'tiff'; |
---|
| 165 | $arrExtensions[] = 'tif'; |
---|
| 166 | $arrExtensions[] = 'doc'; |
---|
| 167 | $arrExtensions[] = 'docx'; |
---|
| 168 | $arrExtensions[] = 'xls'; |
---|
| 169 | $arrExtensions[] = 'xlt'; |
---|
| 170 | $arrExtensions[] = 'xlm'; |
---|
| 171 | $arrExtensions[] = 'xld'; |
---|
| 172 | $arrExtensions[] = 'xla'; |
---|
| 173 | $arrExtensions[] = 'xlc'; |
---|
| 174 | $arrExtensions[] = 'xlw'; |
---|
| 175 | $arrExtensions[] = 'xll'; |
---|
| 176 | $arrExtensions[] = 'ppt'; |
---|
| 177 | $arrExtensions[] = 'pps'; |
---|
| 178 | $arrExtensions[] = 'txt'; |
---|
| 179 | $arrExtensions[] = 'zip'; |
---|
| 180 | $arrExtensions[] = 'rar'; |
---|
| 181 | $arrExtensions[] = 'rtf'; |
---|
| 182 | $arrExtensions[] = 'pdf'; |
---|
| 183 | $arrExtensions[] = 'odt'; |
---|
| 184 | $arrExtensions[] = 'xml'; |
---|
| 185 | $arrExtensions[] = 'html'; |
---|
| 186 | $arrExtensions[] = 'htm'; |
---|
| 187 | $arrExtensions[] = 'csv'; |
---|
| 188 | $arrExtensions[] = 'eml'; |
---|
| 189 | |
---|
| 190 | if(! in_array($this->_fileExtension, $arrExtensions) ){ |
---|
| 191 | $this->sysDebug .= '<b> Extensï¿œo de arquivo nï¿œo permitida</b><br>'; |
---|
| 192 | throw new exception('Extensï¿œo invalida [' . $this->_fileExtension . '].'); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | switch($this->_fileExtension) { |
---|
| 196 | case 'jpg': case 'jpeg': case 'jpe': |
---|
| 197 | $this->_fileMime = 'image/jpeg'; break; |
---|
| 198 | case 'gif': |
---|
| 199 | $this->_fileMime = 'image/gif'; break; |
---|
| 200 | case 'png': |
---|
| 201 | $this->_fileMime = 'image/png'; break; |
---|
| 202 | case 'bmp': |
---|
| 203 | $this->_fileMime = 'image/bmp'; break; |
---|
| 204 | case 'js' : |
---|
| 205 | $this->_fileMime = 'application/x-javascript'; break; |
---|
| 206 | case 'json' : |
---|
| 207 | $this->_fileMime = 'application/json'; break; |
---|
| 208 | case 'tiff' :case 'tif' : |
---|
| 209 | $this->_fileMime = 'image/tiff'; break; |
---|
| 210 | case 'css' : |
---|
| 211 | $this->_fileMime = 'text/css'; break; |
---|
| 212 | case 'xml' : |
---|
| 213 | $this->_fileMime = 'application/xml'; break; |
---|
| 214 | case 'doc' : case 'docx' : |
---|
| 215 | $this->_fileMime = 'application/msword'; break; |
---|
| 216 | case 'xls': case 'xlt': case 'xlm': case 'xld': case 'xla': case 'xlc': case 'xlw' : case 'xll' : |
---|
| 217 | $this->_fileMime = 'application/vnd.ms-excel'; break; |
---|
| 218 | case 'ppt': case 'pps' : |
---|
| 219 | $this->_fileMime = 'application/vnd.ms-powerpoint'; break; |
---|
| 220 | case 'rtf' : |
---|
| 221 | $this->_fileMime = 'application/rtf'; break; |
---|
| 222 | case 'pdf' : |
---|
| 223 | $this->_fileMime = 'application/pdf'; break; |
---|
| 224 | case 'html': case 'htm': case 'php' : |
---|
| 225 | $this->_fileMime = 'text/html'; break; |
---|
| 226 | case 'txt' : |
---|
| 227 | $this->_fileMime = 'text/plain'; break; |
---|
| 228 | case 'mpeg': case 'mpg': case 'mpe' : |
---|
| 229 | $this->_fileMime = 'video/mpeg'; break; |
---|
| 230 | case 'mp3' : |
---|
| 231 | $this->_fileMime = 'audio/mpeg3'; break; |
---|
| 232 | case 'wav' : |
---|
| 233 | $this->_fileMime = 'audio/wav'; break; |
---|
| 234 | case 'aiff': case 'aif' : |
---|
| 235 | $this->_fileMime = 'audio/aiff'; break; |
---|
| 236 | case 'avi' : |
---|
| 237 | $this->_fileMime = 'video/msvideo'; break; |
---|
| 238 | case 'wmv' : |
---|
| 239 | $this->_fileMime = 'video/x-ms-wmv'; break; |
---|
| 240 | case 'mov' : |
---|
| 241 | $this->_fileMime = 'video/quicktime'; break; |
---|
| 242 | case 'zip' : |
---|
| 243 | $this->_fileMime = 'application/zip'; break; |
---|
| 244 | case 'tar' : |
---|
| 245 | $this->_fileMime = 'application/x-tar'; break; |
---|
| 246 | case 'swf' : |
---|
| 247 | $this->_fileMime = 'application/x-shockwave-flash'; break; |
---|
| 248 | default : |
---|
| 249 | $this->_fileMime = 'application/octet-stream'; |
---|
| 250 | } |
---|
| 251 | if ($this->_fileMime == 'application/octet-stream') { |
---|
| 252 | $this->sysDebug .= '<b> Mime Type Default: </b> [' . $this->_fileMime . ']<br>'; |
---|
| 253 | } else { |
---|
| 254 | $this->sysDebug .= '<b> Mime Type Identificado: </b> [' . $this->_fileMime . ']<br>';; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | if($forceDownload === true){ |
---|
| 258 | $this->_forceDownload = "Content-Disposition: attachment; filename=" . $this->_file; |
---|
| 259 | $this->sysDebug .= '<b> Download: </b> [' . $this->_forceDownload . ']<br>'; |
---|
| 260 | } else { |
---|
| 261 | $this->_forceDownload = "Content-Disposition: inline; filename=" . $this->_file; |
---|
| 262 | $this->sysDebug .= '<b> Download: </b> [' . $this->_forceDownload . ']<br>'; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | header("Expires: " . gmdate("D, d M Y H:i:s") . " GMT;"); |
---|
| 266 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT;"); |
---|
| 267 | header("cache-control: post-check=0, pre-check=0, false;"); |
---|
| 268 | header("Pragma: public;"); |
---|
| 269 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0;"); |
---|
| 270 | header("Cache-Control: private, false;"); |
---|
| 271 | header("Content-Transfer-Encoding: binary;"); |
---|
| 272 | header("Content-Length: ". $this->_fileSize . ';'); |
---|
| 273 | header($this->_forceDownload . ';'); |
---|
| 274 | header('Content-type: ' . $this->_fileMime . ';'); |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | readfile($this->_pathBase . $this->_pathFolder . $this->_file); |
---|
| 278 | |
---|
| 279 | /* |
---|
| 280 | //header("Expires: " . gmdate("D, d M Y H:i:s") . " GMT"); |
---|
| 281 | //header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); |
---|
| 282 | //header("cache-control: post-check=0, pre-check=0", false); |
---|
| 283 | header("Pragma: public"); |
---|
| 284 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
---|
| 285 | header("Cache-Control: private",false); |
---|
| 286 | header("Content-Transfer-Encoding: binary"); |
---|
| 287 | header("Content-Length: ". $this->_fileSize ); |
---|
| 288 | header($this->_forceDownload); |
---|
| 289 | header('Content-type: ' . $this->_fileMime . ';'); |
---|
| 290 | //header('Content-Description: ' . $this->_file . ';'); |
---|
| 291 | */ |
---|
| 292 | } catch (exception $e) { |
---|
| 293 | $this->sysError = '<b>Erro: </b>' . $e->getMessage() . '<br>'; |
---|
| 294 | $this->sysDebug .= '<b>Thows UPLOAD Exit</b> <br>'; |
---|
| 295 | |
---|
| 296 | $fp = fopen ("/tmp/logDownloadsIndevidos","a+"); |
---|
| 297 | $writeDebug = "Data : " . date('d/m/Y H:m:s') . |
---|
| 298 | " \nPagina : " . $_SERVER['PHP_SELF'] . |
---|
| 299 | " \nIP : " . $_SERVER['REMOTE_ADDR'] . |
---|
| 300 | " \nUsuoid : " . $_SESSION['usuario']['oid'] . |
---|
| 301 | "\n" . $this->sysDebug . " \n\n"; |
---|
| 302 | $writeDebug = strip_tags(html_entity_decode(str_ireplace('<br>', "\n", $writeDebug), ENT_NOQUOTES, 'ISO8859-1')); |
---|
| 303 | fwrite($fp, $writeDebug ); |
---|
| 304 | fclose($fp); |
---|
| 305 | |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | } |
---|
| 310 | /* |
---|
| 311 | * ï¿œGet susError (Mensagem de erro) |
---|
| 312 | * <code> |
---|
| 313 | * $down-> getSysError(); |
---|
| 314 | * </code> |
---|
| 315 | *ᅵ |
---|
| 316 | */ |
---|
| 317 | function getSysError(){ |
---|
| 318 | $ret = ''; |
---|
| 319 | if(isset($this->sysError{0})){ |
---|
| 320 | $ret = '<font color="#FF0000">' . $this->sysError . '</font>'; |
---|
| 321 | } |
---|
| 322 | return $ret; |
---|
| 323 | } |
---|
| 324 | /** |
---|
| 325 | * ï¿œGet sysDebug (Mensagem do Debug) |
---|
| 326 | * <code> |
---|
| 327 | * $down- > getSysDebug(); |
---|
| 328 | * </code> |
---|
| 329 | *ᅵ |
---|
| 330 | */ |
---|
| 331 | function getSysDebug(){ |
---|
| 332 | return '<pre>' . $this->sysDebug . '</pre>'; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | ?> |
---|