PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/community/www/view_file.php

https://github.com/svn2github/efront-lms
PHP | 135 lines | 98 code | 10 blank | 27 comment | 48 complexity | 596a2b1adb178c95740e8e6c50d9caf4 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. /**
  3. * View file
  4. *
  5. * This file offers the user the ability to view and/or download a file.
  6. *
  7. * @package eFront
  8. * @version 1.0
  9. */
  10. //General initialization and parameters
  11. session_cache_limiter('none');
  12. session_start();
  13. $path = "../libraries/";
  14. //Turn output buffering off, since it messes up files
  15. define("NO_OUTPUT_BUFFERING", true);
  16. /** Configuration file.*/
  17. require_once $path."configuration.php";
  18. //pr($_SERVER);
  19. if (strpos($_SERVER['HTTP_REFERER'], "view_resource.php")){
  20. $bypass_check = true;
  21. }
  22. try {
  23. if(!$bypass_check) {
  24. $currentUser = EfrontUser :: checkUserAccess();
  25. }
  26. } catch (Exception $e) {
  27. //header("HTTP/1.0 500");
  28. //echo EfrontSystem :: printErrorMessage(_RESOURCEREQUESTEDREQUIRESLOGIN);
  29. eF_redirect("index.php?message=".urlencode(_RESOURCEREQUESTEDREQUIRESLOGIN)."&message_type=failure");
  30. exit;
  31. }
  32. session_write_close();
  33. //pr($_SERVER);pr($_GET);exit;
  34. try {
  35. if (isset($_GET['server'])) {
  36. $url = $_SERVER['REQUEST_URI'];
  37. if (strpos($url, 'http') !== 0) { //Otherwise, depending on the QUERY_STRING, parse_url() may not work
  38. $url = G_PROTOCOL.'://'.$_SERVER["HTTP_HOST"].$url;
  39. }
  40. $urlParts = parse_url($url);
  41. $filePath = G_ROOTPATH.'www/'.str_replace(G_SERVERNAME, '', G_PROTOCOL.'://'.$_SERVER['HTTP_HOST'].$urlParts['path']);
  42. try {
  43. $file = new EfrontFile(urldecode($filePath));
  44. } catch (Exception $e) {
  45. $file = new EfrontFile($filePath);
  46. }
  47. } else {
  48. $file = new EfrontFile($_GET['file']);
  49. }
  50. if(!$bypass_check) {
  51. if (preg_match("#content/lessons/(\d+)/#", $file['path'], $matches)) { //the file is a content file. Available to any user enrolled to this lesson.
  52. $result = eF_getTableDataFlat("lessons l, users_to_lessons ul", "id, share_folder", "l.archive=0 and l.id=ul.lessons_ID and ul.archive=0 and ul.users_LOGIN='".$currentUser->user['login']."'");
  53. $legalFolders = array_unique(array_merge($result['id'], $result['share_folder']));
  54. if ($currentUser->user['user_type'] != 'administrator' && $matches[1] && !in_array($matches[1], $legalFolders)) {
  55. throw new EfrontFileException(_YOUCANNOTACCESSTHEREQUESTEDRESOURCE, EfrontFileException::UNAUTHORIZED_ACTION);
  56. }
  57. } else if (preg_match("#content/lessons/scorm_uploaded_files/#", $file['path'], $matches)) { //the file is a temporary scorm exported file
  58. //proceed
  59. } else if (preg_match("#".G_UPLOADPATH."(.*)/projects/#", $file['path'], $matches) || preg_match("#".G_UPLOADPATH."(.*)/tests/#", $file['path'], $matches)) { //this is a project or test file. Check whether the current user has access to it
  60. if ($matches[1] == $_SESSION['s_login']) {
  61. //continue if a user is trying to view his/her own file
  62. } else if ($_SESSION['s_lesson_user_type'] != 'professor' || !$_SESSION['s_lessons_ID']) {
  63. throw new EfrontFileException(_YOUCANNOTACCESSTHEREQUESTEDRESOURCE, EfrontFileException::UNAUTHORIZED_ACTION);
  64. } else if (!eF_checkParameter($matches[1], 'login')) {
  65. throw new EfrontFileException(_YOUCANNOTACCESSTHEREQUESTEDRESOURCE, EfrontFileException::UNAUTHORIZED_ACTION);
  66. } else {
  67. $professorLessons = eF_getTableDataFlat("lessons l, users_to_lessons ul", "id", "l.archive=0 and l.id=ul.lessons_ID and ul.archive=0 and ul.users_LOGIN='".$currentUser->user['login']."'");
  68. $userLessons = eF_getTableDataFlat("lessons l, users_to_lessons ul", "id", "l.archive=0 and l.id=ul.lessons_ID and ul.archive=0 and ul.users_LOGIN='".$matches[1]."'");
  69. if (!in_array($_SESSION['s_lessons_ID'], array_intersect($professorLessons['id'], $userLessons['id']))) {
  70. throw new EfrontFileException(_YOUCANNOTACCESSTHEREQUESTEDRESOURCE, EfrontFileException::UNAUTHORIZED_ACTION);
  71. }
  72. }
  73. } else if (preg_match("#".G_UPLOADPATH."(.*)/avatars/#", $file['path'], $matches) || mb_strpos($file['path'], G_SYSTEMAVATARSPATH) !== false ) {
  74. //proceed
  75. } else if ( mb_strpos($file['path'], G_UPLOADPATH) !== false && mb_strpos($file['path'], G_UPLOADPATH.$currentUser->user['login']) === false) {
  76. throw new EfrontFileException(_YOUCANNOTACCESSTHEREQUESTEDRESOURCE, EfrontFileException::UNAUTHORIZED_ACTION);
  77. }
  78. }
  79. if (strpos($file['path'], G_ROOTPATH.'libraries') !== false && strpos($file['path'], G_ROOTPATH.'libraries/language') === false && $file['mime_type'] != "application/inc") {
  80. throw new EfrontFileException(_ILLEGALPATH.': '.$file['path'], EfrontFileException :: ILLEGAL_PATH);
  81. }
  82. if (isset($_GET['action']) && $_GET['action'] == 'download') {
  83. $file -> sendFile(true);
  84. } else {
  85. cacheHeaders(lastModificationTime(filemtime($file['path'])));
  86. $file -> sendFile(false);
  87. }
  88. } catch (EfrontFileException $e) {
  89. if ($e->getCode() == EfrontFileException::FILE_NOT_EXIST) {
  90. header("HTTP/1.0 404");
  91. }
  92. echo EfrontSystem :: printErrorMessage($e -> getMessage());
  93. }
  94. function cacheHeaders($lastModifiedDate) {
  95. if ($lastModifiedDate) {
  96. if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModifiedDate) {
  97. if (php_sapi_name()=='CGI') {
  98. Header("Status: 304 Not Modified");
  99. } else {
  100. Header("HTTP/1.0 304 Not Modified");
  101. }
  102. exit;
  103. } else {
  104. $gmtDate = gmdate("D, d M Y H:i:s \G\M\T",$lastModifiedDate);
  105. header('Last-Modified: '.$gmtDate);
  106. }
  107. }
  108. }
  109. // This function uses a static variable to track the most recent
  110. // last modification time
  111. function lastModificationTime($time=0) {
  112. static $last_mod ;
  113. if (!isset($last_mod) || $time > $last_mod) {
  114. $last_mod = $time ;
  115. }
  116. return $last_mod ;
  117. }
  118. ?>