PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/get.php

http://kfm.googlecode.com/
PHP | 119 lines | 102 code | 0 blank | 17 comment | 24 complexity | 928b556d34695abebf3aaa7239d44ccb MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * KFM - Kae's File Manager
  4. *
  5. * get.php - retrieves a specified file
  6. * can also resize an image on demand
  7. *
  8. * @category None
  9. * @package None
  10. * @author Kae Verens <kae@verens.com>
  11. * @author Benjamin ter Kuile <bterkuile@gmail.com>
  12. * @license docs/license.txt for licensing
  13. * @link http://kfm.verens.com/
  14. */
  15. require_once 'initialise.php';
  16. if (isset($_SERVER['REDIRECT_QUERY_STRING'])&&$_SERVER['REDIRECT_QUERY_STRING']) {
  17. $arr = explode(',', $_SERVER['REDIRECT_QUERY_STRING']);
  18. foreach ($arr as $r) {
  19. $arr2 = explode('=', $r);
  20. if(count($arr2)>1)$_GET[$arr2[0]] = $arr2[1];
  21. }
  22. }
  23. // { rebuild $_GET (in case it's been mangled by something)
  24. $uri = $_SERVER['REQUEST_URI'];
  25. if (strpos($uri,'?')===false) $uri=str_replace('/get.php/','/get.php?',$uri);
  26. $uri2 = explode('?', $uri);
  27. $parts = count($uri2)>1 ? explode('&', $uri2[1]) : array();
  28. foreach ($parts as $part) {
  29. $arr = explode('=', $part);
  30. if (!(count($arr)>1)) continue;
  31. list($varname, $varval) = $arr;
  32. $_GET[$varname] = urldecode($varval);
  33. }
  34. // }
  35. if(isset($_GET['uri'])){
  36. if (strpos($_GET['uri'], '../')!==false) {
  37. die('no hacking, thank you');
  38. }
  39. $bits=explode('/',$_GET['uri']);
  40. $fname=array_pop($bits);
  41. $dir=0;
  42. $dirs = explode(DIRECTORY_SEPARATOR, trim(join('/',$bits), ' '.DIRECTORY_SEPARATOR));
  43. $subdir = kfmDirectory::getInstance(1);
  44. $startup_sequence_array = array();
  45. foreach ($dirs as $dirname) {
  46. $subdir = $subdir->getSubdir($dirname);
  47. if(!$subdir)break;
  48. $dir= $subdir->id;
  49. }
  50. foreach($subdir->getFiles() as $file){
  51. if($file->name==$fname){
  52. $_GET['id']=$file->id;
  53. break;
  54. }
  55. }
  56. }
  57. $id=@$_GET['id'];
  58. if (!is_numeric($id)) {
  59. echo kfm_lang('errorInvalidID');
  60. exit;
  61. }
  62. $extension = 'unknown';
  63. if (isset($_GET['type'])&&$_GET['type']=='thumb') {
  64. $path = WORKPATH.'thumbs/'.$id;
  65. $name = $id;
  66. } else {
  67. if (isset($_GET['width'])&&isset($_GET['height'])) {
  68. $width = (int)$_GET['width'];
  69. $height = (int)$_GET['height'];
  70. $image = kfmImage::getInstance($id);
  71. if (!$image) {
  72. echo kfm_lang('errorFileIDNotFound', $id);
  73. exit;
  74. }
  75. if($width>$image->width)$width=$image->width;
  76. if($height>$image->height)$height=$image->height;
  77. $h=0;$s=0;$l=0;
  78. if(isset($_GET['hsl'])){
  79. list($h,$s,$l)=explode(':',$_GET['hsl']);
  80. }
  81. $image->setThumbnail($width, $height,$h,$s,$l);
  82. $name = $image->thumb_id;
  83. $path = $image->thumb_path;
  84. $extension = $image->getExtension();
  85. } else {
  86. $file = kfmFile::getInstance($id);
  87. if (!$file) {
  88. echo kfm_lang('errorFileIDNotFound', $id);
  89. exit;
  90. }
  91. $path = $file->path;
  92. $name = $file->name;
  93. $extension = $file->getExtension();
  94. }
  95. }
  96. // { headers
  97. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) $name = preg_replace('/\./', '%2e', $name, substr_count($name, '.')-1);
  98. @set_time_limit(0);
  99. header('Cache-Control: max-age = 2592000');
  100. header('Expires-Active: On');
  101. header('Expires: Fri, 1 Jan 2500 01:01:01 GMT');
  102. header('Pragma:');
  103. $filesize=filesize($path);
  104. header('Content-Length: '.(string)(filesize($path)));
  105. if (isset($_GET['forcedownload'])) {
  106. header('Content-Type: force/download');
  107. header('Content-Disposition: attachment; filename="'.$name.'"');
  108. } else header('Content-Type: '.Mimetype::get($extension));
  109. header('Content-Transfer-Encoding: binary');
  110. // }
  111. if ($file = fopen($path, 'rb')) { // send file
  112. while ((!feof($file))&&(connection_status()==0)) {
  113. print(fread($file, 1024*8));
  114. flush();
  115. }
  116. fclose($file);
  117. }
  118. if(file_exists('api/log_retrieved_file.php'))require 'api/log_retrieved_file.php';
  119. return((connection_status()==0) and !connection_aborted());