showinframes.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file will show documents in a separate frame.
  5. * We don't like frames, but it was the best of two bad things.
  6. *
  7. * display html files within Chamilo - html files have the Chamilo header.
  8. *
  9. * --- advantages ---
  10. * users "feel" like they are in Chamilo,
  11. * and they can use the navigation context provided by the header.
  12. * --- design ---
  13. * a file gets a parameter (an html file) and shows
  14. * - chamilo header
  15. * - html file from parameter
  16. * - (removed) chamilo footer
  17. *
  18. * @version 0.6
  19. *
  20. * @author Roan Embrechts (roan.embrechts@vub.ac.be)
  21. *
  22. * @package chamilo.document
  23. */
  24. require_once __DIR__.'/../inc/global.inc.php';
  25. api_protect_course_script();
  26. $header_file = isset($_GET['file']) ? Security::remove_XSS($_GET['file']) : null;
  27. $document_id = (int) $_GET['id'];
  28. $originIsLearnpath = isset($_GET['origin']) && $_GET['origin'] === 'learnpathitem';
  29. $courseInfo = api_get_course_info();
  30. $course_code = api_get_course_id();
  31. $session_id = api_get_session_id();
  32. if (empty($courseInfo)) {
  33. api_not_allowed(true);
  34. }
  35. $show_web_odf = false;
  36. // Generate path
  37. if (!$document_id) {
  38. $document_id = DocumentManager::get_document_id($courseInfo, $header_file);
  39. }
  40. $document_data = DocumentManager::get_document_data_by_id(
  41. $document_id,
  42. $course_code,
  43. true,
  44. $session_id
  45. );
  46. if ($session_id != 0 && !$document_data) {
  47. $document_data = DocumentManager::get_document_data_by_id(
  48. $document_id,
  49. $course_code,
  50. true,
  51. 0
  52. );
  53. }
  54. if (empty($document_data)) {
  55. api_not_allowed(true);
  56. }
  57. $header_file = $document_data['path'];
  58. $name_to_show = $document_data['title'];
  59. $path_array = explode('/', str_replace('\\', '/', $header_file));
  60. $path_array = array_map('urldecode', $path_array);
  61. $header_file = implode('/', $path_array);
  62. $file = Security::remove_XSS(urldecode($document_data['path']));
  63. $file_root = $courseInfo['path'].'/document'.str_replace('%2F', '/', $file);
  64. $file_url_sys = api_get_path(SYS_COURSE_PATH).$file_root;
  65. $file_url_web = api_get_path(WEB_COURSE_PATH).$file_root;
  66. $is_allowed_to_edit = api_is_allowed_to_edit();
  67. //fix the screen when you try to access a protected course through the url
  68. $is_allowed_in_course = api_is_allowed_in_course() || $is_allowed_to_edit;
  69. if ($is_allowed_in_course == false) {
  70. api_not_allowed(true);
  71. }
  72. // Check user visibility.
  73. $is_visible = DocumentManager::check_visibility_tree(
  74. $document_id,
  75. api_get_course_info(),
  76. api_get_session_id(),
  77. api_get_user_id(),
  78. api_get_group_id(),
  79. false
  80. );
  81. if (!$is_allowed_to_edit && !$is_visible) {
  82. api_not_allowed(true);
  83. }
  84. $pathinfo = pathinfo($header_file);
  85. $playerSupportedFiles = ['mp3', 'mp4', 'ogv', 'flv', 'm4v', 'webm'];
  86. $playerSupported = false;
  87. if (in_array(strtolower($pathinfo['extension']), $playerSupportedFiles)) {
  88. $playerSupported = true;
  89. }
  90. $group_id = api_get_group_id();
  91. $current_group = GroupManager::get_group_properties($group_id);
  92. $current_group_name = $current_group['name'];
  93. if (isset($group_id) && $group_id != '') {
  94. $interbreadcrumb[] = [
  95. 'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
  96. 'name' => get_lang('Groups'),
  97. ];
  98. $interbreadcrumb[] = [
  99. 'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
  100. 'name' => get_lang('Group area').' '.$current_group_name,
  101. ];
  102. $name_to_show = explode('/', $name_to_show);
  103. unset($name_to_show[1]);
  104. $name_to_show = implode('/', $name_to_show);
  105. }
  106. $interbreadcrumb[] = [
  107. 'url' => './document.php?curdirpath='.dirname($header_file).'&'.api_get_cidreq(),
  108. 'name' => get_lang('Documents'),
  109. ];
  110. if (empty($document_data['parents'])) {
  111. if (isset($_GET['createdir'])) {
  112. $interbreadcrumb[] = [
  113. 'url' => $document_data['document_url'],
  114. 'name' => $document_data['title'],
  115. ];
  116. } else {
  117. $interbreadcrumb[] = [
  118. 'url' => '#',
  119. 'name' => $document_data['title'],
  120. ];
  121. }
  122. } else {
  123. foreach ($document_data['parents'] as $document_sub_data) {
  124. if (!isset($_GET['createdir']) && $document_sub_data['id'] == $document_data['id']) {
  125. $document_sub_data['document_url'] = '#';
  126. }
  127. $interbreadcrumb[] = [
  128. 'url' => $document_sub_data['document_url'],
  129. 'name' => $document_sub_data['title'],
  130. ];
  131. }
  132. }
  133. $this_section = SECTION_COURSES;
  134. $nameTools = get_lang('Documents');
  135. /**
  136. * Main code section.
  137. */
  138. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  139. //header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
  140. header('Last-Modified: Wed, 01 Jan 2100 00:00:00 GMT');
  141. header('Cache-Control: no-cache, must-revalidate');
  142. header('Pragma: no-cache');
  143. $browser_display_title = 'Documents - '.Security::remove_XSS($_GET['cidReq']).' - '.$file;
  144. // Only admins get to see the "no frames" link in pageheader.php, so students get a header that's not so high
  145. $frameheight = 135;
  146. if (api_is_course_admin()) {
  147. $frameheight = 165;
  148. }
  149. $frameReady = Display::getFrameReadyBlock('#mainFrame');
  150. $web_odf_supported_files = DocumentManager::get_web_odf_extension_list();
  151. // PDF should be displayed with viewerJS
  152. $web_odf_supported_files[] = 'pdf';
  153. if (in_array(strtolower($pathinfo['extension']), $web_odf_supported_files)) {
  154. $show_web_odf = true;
  155. $htmlHeadXtra[] = '
  156. <script>
  157. resizeIframe = function() {
  158. var bodyHeight = $("body").height();
  159. var topbarHeight = $("#topbar").height();
  160. $("#viewerJSContent").height((bodyHeight - topbarHeight));
  161. }
  162. $(function() {
  163. $(window).resize(resizeIframe());
  164. });
  165. </script>'
  166. ;
  167. }
  168. // Activate code highlight.
  169. $isChatFolder = false;
  170. if (isset($document_data['parents']) && isset($document_data['parents'][0])) {
  171. $chatFolder = $document_data['parents'][0];
  172. if (isset($chatFolder['path']) && $chatFolder['path'] == '/chat_files') {
  173. $isChatFolder = true;
  174. }
  175. }
  176. if ($isChatFolder) {
  177. $htmlHeadXtra[] = api_get_js('highlight/highlight.pack.js');
  178. $htmlHeadXtra[] = api_get_css(api_get_path(WEB_CSS_PATH).'chat.css');
  179. $htmlHeadXtra[] = api_get_css(
  180. api_get_path(WEB_LIBRARY_PATH).'javascript/highlight/styles/github.css'
  181. );
  182. $htmlHeadXtra[] = '
  183. <script>
  184. hljs.initHighlightingOnLoad();
  185. </script>';
  186. }
  187. $execute_iframe = true;
  188. if ($playerSupported) {
  189. $extension = api_strtolower($pathinfo['extension']);
  190. $execute_iframe = false;
  191. }
  192. if ($show_web_odf) {
  193. $execute_iframe = false;
  194. }
  195. if (!$playerSupported && $execute_iframe) {
  196. $htmlHeadXtra[] = '<script>
  197. <!--
  198. var jQueryFrameReadyConfigPath = \''.api_get_jquery_web_path().'\';
  199. -->
  200. </script>';
  201. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.frameready.js"></script>';
  202. $htmlHeadXtra[] = '<script>
  203. // Fixes the content height of the frame
  204. $(function() {
  205. $(\'#mainFrame\').on(\'load\', function () {
  206. this.style.height = (this.contentWindow.document.body.scrollHeight + 50) + \'px\';
  207. });
  208. '.$frameReady.'
  209. });
  210. </script>';
  211. }
  212. if ($originIsLearnpath) {
  213. Display::display_reduced_header();
  214. } else {
  215. Display::display_header();
  216. }
  217. $file_url = api_get_path(WEB_COURSE_PATH).$courseInfo['path'].'/document'.$header_file;
  218. $file_url_web = $file_url.'?'.api_get_cidreq();
  219. if ($show_web_odf) {
  220. echo '<div class="text-center">';
  221. $browser = api_get_navigator();
  222. $pdfUrl = api_get_path(WEB_LIBRARY_PATH).'javascript/ViewerJS/index.html#'.$file_url;
  223. if ($browser['name'] == 'Mozilla' && preg_match('|.*\.pdf|i', $header_file)) {
  224. $pdfUrl = $file_url;
  225. }
  226. echo '<div id="viewerJS">';
  227. echo '<iframe id="viewerJSContent" frameborder="0" allowfullscreen="allowfullscreen" webkitallowfullscreen style="width:100%;"
  228. src="'.$pdfUrl.'">
  229. </iframe>';
  230. echo '</div>';
  231. echo '</div>';
  232. }
  233. if ($playerSupported) {
  234. echo DocumentManager::generateMediaPreview($file_url_web, $extension);
  235. }
  236. if ($execute_iframe) {
  237. if ($isChatFolder) {
  238. $content = Security::remove_XSS(file_get_contents($file_url_sys));
  239. echo $content;
  240. } else {
  241. $parentId = $document_data['parent_id'];
  242. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id='.$parentId;
  243. $actionsLeft = Display::url(
  244. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  245. $url
  246. );
  247. $groupMemberWithEditRights = false;
  248. $groupId = api_get_group_id();
  249. if (!empty($groupId)) {
  250. $groupInfo = GroupManager::get_group_properties($groupId);
  251. if ($groupInfo) {
  252. $groupMemberWithEditRights = GroupManager::allowUploadEditDocument(
  253. api_get_user_id(),
  254. api_get_course_int_id(),
  255. $groupInfo,
  256. $document_data
  257. );
  258. }
  259. }
  260. $allowToEdit = api_is_allowed_to_edit(null, true) || $groupMemberWithEditRights;
  261. if ($allowToEdit) {
  262. $actionsLeft .= Display::url(
  263. Display::return_icon(
  264. 'edit.png',
  265. get_lang('Edit'),
  266. '',
  267. ICON_SIZE_MEDIUM
  268. ),
  269. api_get_path(WEB_CODE_PATH).'document/edit_document.php?'.api_get_cidreq().'&id='.$document_id
  270. );
  271. $titleToShow = addslashes(basename($document_data['title']));
  272. $urlDeleteParams = http_build_query(
  273. [
  274. 'action' => 'delete_item',
  275. 'id' => $parentId,
  276. 'deleteid' => $document_data['id'],
  277. ]
  278. );
  279. $actionsLeft .= Display::url(
  280. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_MEDIUM),
  281. '#',
  282. [
  283. 'data-item-title' => $titleToShow,
  284. 'data-href' => api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(
  285. ).'&'.$urlDeleteParams,
  286. 'data-toggle' => 'modal',
  287. 'data-target' => '#confirm-delete',
  288. ]
  289. );
  290. $actionsLeft .= Display::url(
  291. Display::return_icon('pdf.png', get_lang('Export to PDF format'), [], ICON_SIZE_MEDIUM),
  292. api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq(
  293. ).'&action=export_to_pdf&id='.$document_id
  294. );
  295. }
  296. echo $toolbar = Display::toolbarAction('actions-documents', [$actionsLeft]);
  297. echo '<iframe
  298. id="mainFrame"
  299. name="mainFrame"
  300. border="0"
  301. frameborder="0"
  302. scrolling="no"
  303. style="width:100%;" height="600"
  304. src="'.$file_url_web.'&rand='.mt_rand(1, 10000).'"
  305. height="500" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>';
  306. }
  307. }
  308. Display::display_footer();