start.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. *
  4. * @package WebODF Integration Elgg
  5. */
  6. elgg_register_event_handler('init', 'system', 'webodf_elgg_init');
  7. function webodf_elgg_init() {
  8. global $CONFIG;
  9. elgg_register_page_handler('webodf_elgg','page_handler_webodf_elgg');
  10. elgg_register_plugin_hook_handler('public_pages', 'walled_garden', 'expages_public_pages');
  11. }
  12. function expages_public_pages($hook, $type, $return_value, $params) {
  13. $allowed_pages = array();
  14. $allowed_pages[] = 'webodf_elgg/*.*/*.*';
  15. return $allowed_pages;
  16. }
  17. function page_handler_webodf_elgg($page) {
  18. // Read the URI parameters based on <siteurl>/gdocspreview/param1/param2
  19. $file_guid = $page[0];
  20. $timestamp = intval($page[1]);
  21. if (isset($timestamp)) {
  22. // This is a hack, but it works. It will prevent anyone or Google from
  23. // storing the public URL to access our private files. There is a 90
  24. // second window in which the file is accessible. After that period,
  25. // if you try to access the file from the same URL, it will be denied.
  26. $date = new DateTime();
  27. $max = $date->format('U') + 90;
  28. $min = $date->format('U') - 90;
  29. if (($timestamp > $min) && ($timestamp < $max)) {
  30. // keep track of the old access level
  31. $old_access = elgg_get_ignore_access();
  32. // temporarily make the access level public
  33. elgg_set_ignore_access(true);
  34. $file = get_entity($file_guid);
  35. if (!$file) {
  36. register_error(elgg_echo("file:downloadfailed"));
  37. forward();
  38. }
  39. $mime = $file->getMimeType();
  40. if (!$mime) {
  41. $mime = "application/octet-stream";
  42. }
  43. $filename = $file->originalfilename;
  44. // fix for IE https issue
  45. header("Pragma: public");
  46. header("Content-type: $mime");
  47. if (strpos($mime, "image/") !== false || $mime == "application/pdf") {
  48. header("Content-Disposition: inline; filename=\"$filename\"");
  49. } else {
  50. header("Content-Disposition: attachment; filename=\"$filename\"");
  51. }
  52. ob_clean();
  53. flush();
  54. readfile($file->getFilenameOnFilestore());
  55. // restore the access level
  56. elgg_set_ignore_access($old_access);
  57. exit;
  58. } else {
  59. register_error(elgg_echo("file:downloadfailed"));
  60. forward();
  61. }
  62. } else {
  63. register_error(elgg_echo("file:downloadfailed"));
  64. forward();
  65. }
  66. }
  67. ?>