attachment.php 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. require_once 'settings.php';
  3. require_once 'inc/include.php';
  4. require_once 'inc/phpZotero.php';
  5. $zotero = new phpZotero($API_key);
  6. $itemkey = $_REQUEST['itemkey'];
  7. $mimeType = $_REQUEST['mime'];
  8. //purge old files from the cache
  9. purge_cache(realpath("./" . $cache_dir), $cache_age);
  10. // set up some stuff
  11. $abs_output = realpath("./" . $cache_dir) . "/" . $itemkey . "_" . date("YmdHis");
  12. $abs_zipfile = realpath("./" . $data_dir . "/" . $itemkey . ".zip");
  13. $writefiles=true;
  14. // check if attachment is already unzipped in cache. if so, point directory there and prevent new unzipping
  15. $dir = opendir(realpath("./" . $cache_dir));
  16. while ($dir_item = readdir($dir)) {
  17. if (is_dir(realpath("./" . $cache_dir) . "/" . $dir_item)) {
  18. if (substr($dir_item, 0, strpos($dir_item, "_")) == $itemkey) {
  19. $abs_output = realpath("./" . $cache_dir) . "/" . $dir_item;
  20. $writefiles = false;
  21. echo("found one");
  22. }
  23. }
  24. }
  25. closedir($dir);
  26. // call the unzipping function to get the filename and unzip the attachment to cache if not already in cache
  27. $result = unzip($abs_zipfile,$abs_output,true,$writefiles);
  28. // send attachment (or error message) to browser
  29. $cacheURL = "";
  30. if(substr($result,0,strlen($abs_output)) == $abs_output) {
  31. if($result == $abs_output) {
  32. $html_output="";
  33. $html_output .= "Display of Websnapshots is not fully implemented yet. Sorry, but this is due to a shortcoming of the zotero server API.<br><br>\n";
  34. $scriptpath = realpath(substr($_SERVER['SCRIPT_FILENAME'],0,strrpos($_SERVER['SCRIPT_FILENAME'],"/")));
  35. if ($scriptpath == substr(realpath("./" . $cache_dir),0,strlen($scriptpath))) {
  36. $cacheURL = "http://" . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'],"/")) . substr($abs_output,strlen($scriptpath));
  37. } else {
  38. $cacheURL = $cache_base_URL;
  39. }
  40. if(strlen($cacheURL)>0) {
  41. $html_output .= "However, the websnap shot has been written to the cache directory and can be accessed there.<br>\n";
  42. $webfilename=findwebfile($abs_output);
  43. if (strlen($webfilename)>0) {
  44. $html_output .= "<a href=\"$cacheURL/$webfilename\" target=\"_blank\">Click here</a> to access the file that looks most like the main file for the websnapshot (or wait 5 seconds to be redirected there).<br>\n";
  45. $html_output .= "If that doens't work, check out <a href=\"$cacheURL\" target=\"_blank\">the entire websnapshot</a> folder and look for the main file yourself.";
  46. $html_output = "<html>\n<head>\n<script type=\"text/javascript\">\n<!--\nfunction delayer(){\nwindow.location = \"$cacheURL/$webfilename\"\n}\n//-->\n</script>\n</head>\n<body onLoad=\"setTimeout('delayer()', 5000)\">\n" . $html_output . "</body>\n</html>";
  47. } else {
  48. $html_output .= "I didn't find a file that looks most like the main file for the websnapshot, ";
  49. $html_output .= "so you might want to check out <a href=\"$cacheURL\" target=\"_blank\">the entire websnapshot</a> folder and look for the main file yourself.";
  50. $html_output = "<html>\n<head>\n</head>\n<body>\n" . $html_output . "</body>\n</html>";
  51. }
  52. } else {
  53. $html_output .= "However, if you use a cache directory which is located in the same directory as this script ";
  54. $html_output .= "($scriptpath) or you provide the base URL to whichever cache directory in the settings.php, ";
  55. $html_output .= "you would be able to use the workaround provided by this script.";
  56. $html_output = "<html>\n<head>\n</head>\n<body>\n" . $html_output . "</body>\n</html>";
  57. }
  58. echo ($html_output);
  59. } else {
  60. header("Content-type: " . $mimeType);
  61. header("Content-Disposition: filename=\"" . pathinfo($result, PATHINFO_BASENAME) . "\"");
  62. readfile($result);
  63. }
  64. } else {
  65. echo("AN ERROR HAS OCCURRED! - " . $result);
  66. }
  67. //purge old files from the cache again if $cache_age=0 (ie immediate deletion of cache files)
  68. if (($cache_age==0) && (strlen($cacheURL)==0)) { // do not purge immediately if web accessible websnapshot has been un zipped
  69. purge_cache(realpath("./" . $cache_dir), -1);
  70. }
  71. ?>