document_lite.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Light version of documents manager (currently disabled)
  5. * @package chamilo.document
  6. */
  7. /**
  8. * Code
  9. */
  10. exit;
  11. // Language files that need to be included
  12. $language_file = array('document', 'slideshow', 'gradebook', 'create_course');
  13. require_once '../inc/global.inc.php';
  14. $this_section = SECTION_COURSES;
  15. require_once 'document.inc.php';
  16. $lib_path = api_get_path(LIBRARY_PATH);
  17. /* Libraries */
  18. require_once $lib_path.'document.lib.php';
  19. api_protect_course_script(true);
  20. $htmlHeadXtra[] = api_get_jqgrid_js();
  21. $course_info = api_get_course_info();
  22. $course_dir = $course_info['path'].'/document';
  23. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  24. $base_work_dir = $sys_course_path.$course_dir;
  25. $http_www = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document';
  26. $dbl_click_id = 0; // Used for avoiding double-click
  27. /* Constants and variables */
  28. $session_id = api_get_session_id();
  29. $course_code = api_get_course_id();
  30. $to_group_id = api_get_group_id();
  31. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  32. $group_member_with_upload_rights = false;
  33. // If the group id is set, we show them group documents
  34. $group_properties = array();
  35. $group_properties['directory'] = null;
  36. // For sessions we should check the parameters of visibility
  37. if (api_get_session_id() != 0) {
  38. $group_member_with_upload_rights = $group_member_with_upload_rights && api_is_allowed_to_session_edit(false, true);
  39. }
  40. //Actions
  41. $document_id = intval($_REQUEST['id']);
  42. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  43. switch ($action) {
  44. case 'download':
  45. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  46. // Check whether the document is in the database
  47. if (empty($document_data)) {
  48. // File not found!
  49. header('HTTP/1.0 404 Not Found');
  50. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  51. $error404 .= '<html><head>';
  52. $error404 .= '<title>404 Not Found</title>';
  53. $error404 .= '</head><body>';
  54. $error404 .= '<h1>Not Found</h1>';
  55. $error404 .= '<p>The requested URL was not found on this server.</p>';
  56. $error404 .= '<hr>';
  57. $error404 .= '</body></html>';
  58. echo $error404;
  59. exit;
  60. }
  61. // Launch event
  62. event_download($document_data['url']);
  63. // Check visibility of document and paths
  64. if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible_by_id(
  65. $document_id,
  66. $course_info,
  67. api_get_session_id(),
  68. api_get_user_id()
  69. )
  70. ) {
  71. api_not_allowed(true);
  72. }
  73. $full_file_name = $base_work_dir.$document_data['path'];
  74. if (Security::check_abs_path($full_file_name, $base_work_dir.'/')) {
  75. DocumentManager::file_send_for_download($full_file_name, true);
  76. }
  77. exit;
  78. break;
  79. case 'downloadfolder' :
  80. if (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin(
  81. )
  82. ) {
  83. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  84. //filter when I am into shared folder, I can donwload only my shared folder
  85. if (is_any_user_shared_folder($document_data['path'], $session_id)) {
  86. if (is_my_shared_folder(
  87. api_get_user_id(),
  88. $document_data['path'],
  89. $session_id
  90. ) || api_is_allowed_to_edit() || api_is_platform_admin()
  91. ) {
  92. require 'downloadfolder.inc.php';
  93. }
  94. } else {
  95. require 'downloadfolder.inc.php';
  96. }
  97. exit;
  98. }
  99. break;
  100. }
  101. //If no actions we proceed to show the document (Hack in order to use document.php?id=X)
  102. if (isset($document_id)) {
  103. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  104. //If the document is not a folder we show the document
  105. if ($document_data) {
  106. $parent_id = $document_data['parent_id'];
  107. //$visibility = DocumentManager::is_visible_by_id($document_id, $course_info, api_get_session_id(), api_get_user_id());
  108. $visibility = DocumentManager::check_visibility_tree(
  109. $document_id,
  110. api_get_course_id(),
  111. api_get_session_id(),
  112. api_get_user_id()
  113. );
  114. if (!empty($document_data['filetype']) && $document_data['filetype'] == 'file') {
  115. if ($visibility && api_is_allowed_to_session_edit()) {
  116. $url = api_get_path(
  117. WEB_COURSE_PATH
  118. ).$course_info['path'].'/document'.$document_data['path'].'?'.api_get_cidreq();
  119. header("Location: $url");
  120. }
  121. exit;
  122. } else {
  123. if (!$visibility && !api_is_allowed_to_edit()) {
  124. api_not_allowed();
  125. }
  126. }
  127. $_GET['curdirpath'] = $document_data['path'];
  128. }
  129. // What's the current path?
  130. // We will verify this a bit further down
  131. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  132. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  133. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  134. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  135. } else {
  136. $curdirpath = '/';
  137. }
  138. $curdirpathurl = urlencode($curdirpath);
  139. } else {
  140. // What's the current path?
  141. // We will verify this a bit further down
  142. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  143. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  144. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  145. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  146. } else {
  147. $curdirpath = '/';
  148. }
  149. $curdirpathurl = urlencode($curdirpath);
  150. // Check the path
  151. // If the path is not found (no document id), set the path to /
  152. $document_id = DocumentManager::get_document_id($course_info, $curdirpath);
  153. if (!$document_id) {
  154. $document_id = DocumentManager::get_document_id($course_info, $curdirpath);
  155. }
  156. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  157. $parent_id = $document_data['parent_id'];
  158. }
  159. $current_folder_id = $document_id;
  160. // Is the document tool visible?
  161. // Check whether the tool is actually visible
  162. $table_course_tool = Database::get_course_table(TABLE_TOOL_LIST);
  163. $course_id = api_get_course_int_id();
  164. $tool_sql = 'SELECT visibility FROM '.$table_course_tool.' WHERE c_id = '.$course_id.' AND name = "'.TOOL_DOCUMENT.'" LIMIT 1';
  165. $tool_result = Database::query($tool_sql);
  166. $tool_row = Database::fetch_array($tool_result);
  167. $tool_visibility = $tool_row['visibility'];
  168. if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $group_member_with_upload_rights)) {
  169. api_not_allowed(true);
  170. }
  171. $htmlHeadXtra[] =
  172. "<script type=\"text/javascript\">
  173. function confirmation (name) {
  174. if (confirm(\" ".get_lang("AreYouSureToDelete")." \"+ name + \" ?\"))
  175. {return true;}
  176. else
  177. {return false;}
  178. }
  179. </script>";
  180. // If they are looking at group documents they can't see the root
  181. if ($to_group_id != 0 && $curdirpath == '/') {
  182. $curdirpath = $group_properties['directory'];
  183. $curdirpathurl = urlencode($group_properties['directory']);
  184. }
  185. // Check visibility of the current dir path. Don't show anything if not allowed
  186. //@todo check this validation for coaches
  187. //if (!$is_allowed_to_edit || api_is_coach()) { before
  188. if (!$is_allowed_to_edit && api_is_coach()) {
  189. if ($curdirpath != '/' && !(DocumentManager::is_visible($curdirpath, $_course, api_get_session_id(), 'folder'))) {
  190. api_not_allowed(true);
  191. }
  192. }
  193. /* MAIN SECTION */
  194. // Slideshow inititalisation
  195. $_SESSION['image_files_only'] = '';
  196. $image_files_only = '';
  197. /* Header */
  198. if ($is_certificate_mode) {
  199. $interbreadcrumb[] = array('url' => '../gradebook/index.php', 'name' => get_lang('Gradebook'));
  200. } else {
  201. if ((isset($_GET['id']) && $_GET['id'] != 0) || isset($_GET['curdirpath']) || isset($_GET['createdir'])) {
  202. $interbreadcrumb[] = array('url' => 'document.php', 'name' => get_lang('Documents'));
  203. } else {
  204. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('Documents'));
  205. }
  206. }
  207. // Interbreadcrumb for the current directory root path
  208. if (empty($document_data['parents'])) {
  209. if (isset($_GET['createdir'])) {
  210. $interbreadcrumb[] = array('url' => $document_data['document_url'], 'name' => $document_data['title']);
  211. } else {
  212. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  213. }
  214. } else {
  215. foreach ($document_data['parents'] as $document_sub_data) {
  216. if (!isset($_GET['createdir']) && $document_sub_data['id'] == $document_data['id']) {
  217. $document_sub_data['document_url'] = '#';
  218. }
  219. $interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']);
  220. }
  221. }
  222. if (isset($_GET['createdir'])) {
  223. $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('CreateDir'));
  224. }
  225. $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
  226. $htmlHeadXtra[] = '<link rel="stylesheet" href="'.$js_path.'jquery-jplayer/skins/chamilo/jplayer.blue.monday.css" type="text/css">';
  227. $htmlHeadXtra[] = '<script type="text/javascript" src="'.$js_path.'jquery-jplayer/jquery.jplayer.min.js"></script>';
  228. $mediaplayer_path = api_get_path(WEB_LIBRARY_PATH).'mediaplayer/player.swf';
  229. $docs_and_folders = DocumentManager::get_all_document_data(
  230. $_course,
  231. $curdirpath,
  232. $to_group_id,
  233. null,
  234. $is_allowed_to_edit || $group_member_with_upload_rights,
  235. false
  236. );
  237. $file_list = $format_list = '';
  238. $count = 1;
  239. if (!empty($docs_and_folders)) {
  240. foreach ($docs_and_folders as $file) {
  241. if ($file['filetype'] == 'file') {
  242. $path_info = pathinfo($file['path']);
  243. $extension = strtolower($path_info['extension']);
  244. //@todo use a js loop to autogenerate this code
  245. if (in_array($extension, array('ogg', 'mp3', 'wav'))) {
  246. $document_data = DocumentManager::get_document_data_by_id($file['id'], api_get_course_id());
  247. if ($extension == 'ogg') {
  248. $extension = 'oga';
  249. }
  250. $jquery .= ' $("#jquery_jplayer_'.$count.'").jPlayer({
  251. ready: function() {
  252. $(this).jPlayer("setMedia", {
  253. '.$extension.' : "'.$document_data['direct_url'].'"
  254. });
  255. },
  256. swfPath: "'.$js_path.'jquery-jplayer",
  257. supplied: "mp3, m4a, oga, ogv, wav",
  258. solution: "flash, html", // Do not change this setting otherwise
  259. cssSelectorAncestor: "#jp_interface_'.$count.'",
  260. });'."\n\n";
  261. $count++;
  262. }
  263. }
  264. }
  265. }
  266. $htmlHeadXtra[] = '<script type="text/javascript">
  267. $(document).ready( function() {
  268. //Experimental changes to preview mp3, ogg files
  269. '.$jquery.'
  270. //Keep this down otherwise the jquery player will not work
  271. for (i=0;i<$(".actions").length;i++) {
  272. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  273. $(".actions:eq("+i+")").hide();
  274. }
  275. }
  276. });
  277. </script>';
  278. // Lib for event log, stats & tracking & record of the access
  279. event_access_tool(TOOL_DOCUMENT);
  280. /* DISPLAY */
  281. if ($to_group_id != 0) { // Add group name after for group documents
  282. $add_group_to_title = ' ('.$group_properties['name'].')';
  283. }
  284. /* Introduction section (editable by course admins) */
  285. if (!empty($_SESSION['_gid'])) {
  286. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  287. } else {
  288. Display::display_introduction_section(TOOL_DOCUMENT);
  289. }
  290. // ACTION MENU
  291. // Copy a file to general my files user's
  292. if (isset($_GET['action']) && $_GET['action'] == 'copytomyfiles' && api_get_setting(
  293. 'users_copy_files'
  294. ) == 'true' && api_get_user_id() != 0
  295. ) {
  296. $clean_get_id = Security::remove_XSS($_GET['id']);
  297. $my_path = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'system');
  298. $user_folder = $my_path['dir'].'my_files/';
  299. $my_path = null;
  300. if (!file_exists($user_folder)) {
  301. $perm = api_get_permissions_for_new_directories();
  302. @mkdir($user_folder, $perm, true);
  303. }
  304. $file = $sys_course_path.$_course['path'].'/document'.$clean_get_id;
  305. $copyfile = $user_folder.basename($clean_get_id);
  306. if (file_exists($copyfile)) {
  307. $message = get_lang('CopyAlreadyDone').'</p><p>'.'<a href="'.api_get_self().'?'.api_get_cidreq(
  308. ).'&amp;curdirpath='.$curdirpath.'">'.get_lang("No").'</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'.api_get_self(
  309. ).'?'.api_get_cidreq(
  310. ).'&amp;curdirpath='.$curdirpath.'&amp;action=copytomyfiles&amp;id='.$clean_get_id.'&amp;copy=yes">'.get_lang(
  311. 'Yes'
  312. ).'</a></p>';
  313. if (!isset($_GET['copy'])) {
  314. Display::display_warning_message($message, false);
  315. }
  316. if (Security::remove_XSS($_GET['copy']) == 'yes') {
  317. if (!copy($file, $copyfile)) {
  318. Display::display_error_message(get_lang('CopyFailed'));
  319. } else {
  320. Display::display_confirmation_message(get_lang('OverwritenFile'));
  321. }
  322. }
  323. } else {
  324. if (!copy($file, $copyfile)) {
  325. Display::display_error_message(get_lang('CopyFailed'));
  326. } else {
  327. Display::display_confirmation_message(get_lang('CopyMade'));
  328. }
  329. }
  330. }
  331. /* MOVE FILE OR DIRECTORY */
  332. //Only teacher and all users into their group and each user into his/her shared folder
  333. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  334. api_get_user_id(),
  335. $curdirpath,
  336. $session_id
  337. ) || is_my_shared_folder(api_get_user_id(), Security::remove_XSS($_POST['move_to']), $session_id)
  338. ) {
  339. if (isset($_GET['move']) && $_GET['move'] != '') {
  340. $my_get_move = intval($_REQUEST['move']);
  341. if (api_is_coach()) {
  342. if (!DocumentManager::is_visible_by_id(
  343. $my_get_move,
  344. $course_info,
  345. api_get_session_id(),
  346. api_get_user_id()
  347. )
  348. ) {
  349. api_not_allowed();
  350. }
  351. }
  352. if (!$is_allowed_to_edit) {
  353. if (DocumentManager::check_readonly($_course, api_get_user_id(), $my_get_move)) {
  354. api_not_allowed();
  355. }
  356. }
  357. $document_to_move = DocumentManager::get_document_data_by_id($my_get_move, api_get_course_id());
  358. $move_path = $document_to_move['path'];
  359. if (!empty($document_to_move)) {
  360. $folders = DocumentManager::get_all_document_folders(
  361. $_course,
  362. $to_group_id,
  363. $is_allowed_to_edit || $group_member_with_upload_rights
  364. );
  365. //filter if is my shared folder. TODO: move this code to build_move_to_selector function
  366. if (is_my_shared_folder(api_get_user_id(), $curdirpath, $session_id) && !$is_allowed_to_edit) {
  367. $main_user_shared_folder_main = '/shared_folder/sf_user_'.api_get_user_id(
  368. ); //only main user shared folder
  369. $main_user_shared_folder_sub = '/shared_folder\/sf_user_'.api_get_user_id().'\//'; //all subfolders
  370. $user_shared_folders = array();
  371. foreach ($folders as $fold) {
  372. if ($main_user_shared_folder_main == $fold || preg_match($main_user_shared_folder_sub, $fold)) {
  373. $user_shared_folders[] = $fold;
  374. }
  375. }
  376. echo '<legend>'.get_lang('Move').'</legend>';
  377. echo build_move_to_selector(
  378. $user_shared_folders,
  379. $move_path,
  380. $my_get_move,
  381. $group_properties['directory']
  382. );
  383. } else {
  384. echo '<legend>'.get_lang('Move').'</legend>';
  385. echo build_move_to_selector($folders, $move_path, $my_get_move, $group_properties['directory']);
  386. }
  387. }
  388. }
  389. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  390. if (!$is_allowed_to_edit) {
  391. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_POST['move_file'])) {
  392. api_not_allowed();
  393. }
  394. }
  395. if (api_is_coach()) {
  396. if (!DocumentManager::is_visible_by_id(
  397. $_POST['move_file'],
  398. $_course,
  399. api_get_session_id(),
  400. api_get_user_id()
  401. )
  402. ) {
  403. api_not_allowed();
  404. }
  405. }
  406. $document_to_move = DocumentManager::get_document_data_by_id($_POST['move_file'], api_get_course_id());
  407. // Security fix: make sure they can't move files that are not in the document table
  408. if (!empty($document_to_move)) {
  409. $real_path_target = $base_work_dir.$_POST['move_to'].'/'.basename($document_to_move['path']);
  410. $fileExist = false;
  411. if (file_exists($real_path_target)) {
  412. $fileExist = true;
  413. }
  414. if (FileManager::move($base_work_dir.$document_to_move['path'], $base_work_dir.$_POST['move_to'])) {
  415. //if (1) {
  416. //$contents = DocumentManager::replace_urls_inside_content_html_when_moving_file(basename($document_to_move['path']), $base_work_dir.dirname($document_to_move['path']), $base_work_dir.$_POST['move_to']);
  417. //exit;
  418. FileManager::update_db_info(
  419. 'update',
  420. $document_to_move['path'],
  421. $_POST['move_to'].'/'.basename($document_to_move['path'])
  422. );
  423. //update database item property
  424. $doc_id = $_POST['move_file'];
  425. if (is_dir($real_path_target)) {
  426. api_item_property_update(
  427. $_course,
  428. TOOL_DOCUMENT,
  429. $doc_id,
  430. 'FolderMoved',
  431. api_get_user_id(),
  432. $to_group_id,
  433. null,
  434. null,
  435. null,
  436. $session_id
  437. );
  438. Display::display_confirmation_message(get_lang('DirMv'));
  439. } elseif (is_file($real_path_target)) {
  440. api_item_property_update(
  441. $_course,
  442. TOOL_DOCUMENT,
  443. $doc_id,
  444. 'DocumentMoved',
  445. api_get_user_id(),
  446. $to_group_id,
  447. null,
  448. null,
  449. null,
  450. $session_id
  451. );
  452. Display::display_confirmation_message(get_lang('DocMv'));
  453. }
  454. // Set the current path
  455. $curdirpath = $_POST['move_to'];
  456. $curdirpathurl = urlencode($_POST['move_to']);
  457. } else {
  458. if ($fileExist) {
  459. if (is_dir($real_path_target)) {
  460. Display::display_error_message(get_lang('DirExists'));
  461. } elseif (is_file($real_path_target)) {
  462. Display::display_error_message(get_lang('FileExists'));
  463. }
  464. } else {
  465. Display::display_error_message(get_lang('Impossible'));
  466. }
  467. }
  468. } else {
  469. Display::display_error_message(get_lang('Impossible'));
  470. }
  471. }
  472. }
  473. /* DELETE FILE OR DIRECTORY */
  474. //Only teacher and all users into their group
  475. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  476. api_get_user_id(),
  477. $curdirpath,
  478. $session_id
  479. )
  480. ) {
  481. if (isset($_GET['delete'])) {
  482. if (!$is_allowed_to_edit) {
  483. if (api_is_coach()) {
  484. if (!DocumentManager::is_visible($_GET['delete'], $_course, api_get_session_id())) {
  485. api_not_allowed();
  486. }
  487. }
  488. if (DocumentManager::check_readonly($_course, api_get_user_id(), $_GET['delete'], '', true)) {
  489. api_not_allowed();
  490. }
  491. }
  492. if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
  493. if (isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(
  494. intval($_GET['delete_certificate_id'])
  495. )
  496. ) {
  497. $default_certificate_id = $_GET['delete_certificate_id'];
  498. DocumentManager::remove_attach_certificate(api_get_course_id(), $default_certificate_id);
  499. }
  500. Display::display_confirmation_message(get_lang('DocDeleted'));
  501. } else {
  502. Display::display_error_message(get_lang('DocDeleteError'));
  503. }
  504. }
  505. if (isset($_POST['action'])) {
  506. switch ($_POST['action']) {
  507. case 'delete':
  508. foreach ($_POST['path'] as $index => & $path) {
  509. if (!$is_allowed_to_edit) {
  510. if (DocumentManager::check_readonly($_course, api_get_user_id(), $path)) {
  511. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  512. break 2;
  513. }
  514. }
  515. }
  516. foreach ($_POST['path'] as $index => & $path) {
  517. if (in_array(
  518. $path,
  519. array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files', '/certificates')
  520. )
  521. ) {
  522. continue;
  523. } else {
  524. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  525. }
  526. }
  527. if (!empty($delete_document)) {
  528. Display::display_confirmation_message(get_lang('DocDeleted'));
  529. }
  530. break;
  531. }
  532. }
  533. }
  534. /* CREATE DIRECTORY */
  535. //Only teacher and all users into their group and any user into his/her shared folder
  536. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  537. api_get_user_id(),
  538. $curdirpath,
  539. $session_id
  540. )
  541. ) {
  542. // Create directory with $_POST data
  543. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  544. // Needed for directory creation
  545. $post_dir_name = $_POST['dirname'];
  546. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  547. Display::display_error_message(get_lang('CannotCreateDir'));
  548. } else {
  549. if (!empty($_POST['dir_id'])) {
  550. $document_data = DocumentManager::get_document_data_by_id($_POST['dir_id'], api_get_course_id());
  551. $curdirpath = $document_data['path'];
  552. }
  553. $added_slash = ($curdirpath == '/') ? '' : '/';
  554. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  555. $dir_name = FileManager::disable_dangerous_file($dir_name);
  556. $dir_check = $base_work_dir.$dir_name;
  557. if (!is_dir($dir_check)) {
  558. $created_dir = FileManager::create_unexisting_directory(
  559. $_course,
  560. api_get_user_id(),
  561. api_get_session_id(),
  562. $to_group_id,
  563. $to_user_id,
  564. $base_work_dir,
  565. $dir_name,
  566. $post_dir_name
  567. );
  568. if ($created_dir) {
  569. Display::display_confirmation_message(
  570. '<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>',
  571. false
  572. );
  573. // Uncomment if you want to enter the created dir
  574. //$curdirpath = $created_dir;
  575. //$curdirpathurl = urlencode($curdirpath);
  576. } else {
  577. Display::display_error_message(get_lang('CannotCreateDir'));
  578. }
  579. } else {
  580. Display::display_error_message(get_lang('CannotCreateDir'));
  581. }
  582. }
  583. }
  584. // Show them the form for the directory name
  585. if (isset($_GET['createdir'])) {
  586. echo create_dir_form($document_id);
  587. }
  588. }
  589. /* VISIBILITY COMMANDS */
  590. //Only teacher
  591. if ($is_allowed_to_edit) {
  592. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  593. // Make visible or invisible?
  594. if (isset($_GET['set_visible'])) {
  595. $update_id = intval($_GET['set_visible']);
  596. $visibility_command = 'visible';
  597. } else {
  598. $update_id = intval($_GET['set_invisible']);
  599. $visibility_command = 'invisible';
  600. }
  601. if (!$is_allowed_to_edit) {
  602. if (api_is_coach()) {
  603. if (!DocumentManager::is_visible_by_id($update_id, $_course, api_get_session_id(), api_get_user_id())) {
  604. api_not_allowed();
  605. }
  606. }
  607. if (DocumentManager::check_readonly($_course, api_get_user_id(), '', $update_id)) {
  608. api_not_allowed();
  609. }
  610. }
  611. // Update item_property to change visibility
  612. if (api_item_property_update(
  613. $_course,
  614. TOOL_DOCUMENT,
  615. $update_id,
  616. $visibility_command,
  617. api_get_user_id(),
  618. null,
  619. null,
  620. null,
  621. null,
  622. $session_id
  623. )
  624. ) {
  625. Display::display_confirmation_message(
  626. get_lang('VisibilityChanged')
  627. ); //don't use ViMod because firt is load ViMdod (Gradebook). VisibilityChanged (trad4all)
  628. } else {
  629. Display::display_error_message(get_lang('ViModProb'));
  630. }
  631. }
  632. }
  633. /* TEMPLATE ACTION */
  634. //Only teacher and all users into their group
  635. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  636. api_get_user_id(),
  637. $curdirpath,
  638. $session_id
  639. )
  640. ) {
  641. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  642. $document_id_for_template = intval($_GET['add_as_template']);
  643. // Create the form that asks for the directory name
  644. $template_text = '<form name="set_document_as_new_template" enctype="multipart/form-data" action="'.api_get_self(
  645. ).'?add_as_template='.$document_id_for_template.'" method="post">';
  646. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  647. $template_text .= '<table><tr><td>';
  648. $template_text .= get_lang('TemplateName').' : </td>';
  649. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  650. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  651. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  652. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  653. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  654. $template_text .= '</table>';
  655. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang(
  656. 'CreateTemplate'
  657. ).'</button>';
  658. $template_text .= '</form>';
  659. // Show the form
  660. Display::display_normal_message($template_text, false);
  661. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  662. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  663. $title = Security::remove_XSS($_POST['template_title']);
  664. //$description = Security::remove_XSS($_POST['template_description']);
  665. $user_id = api_get_user_id();
  666. // Create the template_thumbnails folder in the upload folder (if needed)
  667. if (!is_dir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/')) {
  668. @mkdir(
  669. api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/',
  670. api_get_permissions_for_new_directories()
  671. );
  672. }
  673. // Upload the file
  674. if (!empty($_FILES['template_image']['name'])) {
  675. $upload_ok = FileManager::process_uploaded_file($_FILES['template_image']);
  676. if ($upload_ok) {
  677. // Try to add an extension to the file if it hasn't one
  678. $new_file_name = $_course['sysCode'].'-'.FileManager::add_ext_on_mime(
  679. stripslashes($_FILES['template_image']['name']),
  680. $_FILES['template_image']['type']
  681. );
  682. // Upload dir
  683. $upload_dir = api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/';
  684. // Resize image to max default and end upload
  685. $temp = new Image($_FILES['template_image']['tmp_name']);
  686. $picture_info = $temp->get_image_info();
  687. $max_width_for_picture = 100;
  688. if ($picture_info['width'] > $max_width_for_picture) {
  689. $thumbwidth = $max_width_for_picture;
  690. if (empty($thumbwidth) || $thumbwidth == 0) {
  691. $thumbwidth = $max_width_for_picture;
  692. }
  693. $new_height = round(($thumbwidth / $picture_info['width']) * $picture_info['height']);
  694. $temp->resize($thumbwidth, $new_height, 0);
  695. }
  696. $temp->send_image($upload_dir.$new_file_name);
  697. }
  698. }
  699. DocumentManager::set_document_as_template(
  700. $title,
  701. $description,
  702. $document_id_for_template,
  703. $course_code,
  704. $user_id,
  705. $new_file_name
  706. );
  707. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  708. }
  709. if (isset($_GET['remove_as_template'])) {
  710. $document_id_for_template = intval($_GET['remove_as_template']);
  711. $user_id = api_get_user_id();
  712. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  713. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  714. }
  715. }
  716. // END ACTION MENU
  717. // Attach certificate in the gradebook
  718. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate'] == strval(
  719. intval($_GET['set_certificate'])
  720. )
  721. ) {
  722. if (isset($_GET['cidReq'])) {
  723. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  724. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  725. DocumentManager::attach_gradebook_certificate($course_id, $document_id);
  726. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  727. }
  728. }
  729. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  730. if (isset($_GET['keyword']) && !empty($_GET['keyword'])) {
  731. $docs_and_folders = DocumentManager::get_all_document_data(
  732. $_course,
  733. $curdirpath,
  734. $to_group_id,
  735. null,
  736. $is_allowed_to_edit || $group_member_with_upload_rights,
  737. true
  738. );
  739. } else {
  740. $docs_and_folders = DocumentManager::get_all_document_data(
  741. $_course,
  742. $curdirpath,
  743. $to_group_id,
  744. null,
  745. $is_allowed_to_edit || $group_member_with_upload_rights,
  746. false
  747. );
  748. }
  749. $folders = DocumentManager::get_all_document_folders(
  750. $_course,
  751. $to_group_id,
  752. $is_allowed_to_edit || $group_member_with_upload_rights
  753. );
  754. if ($folders === false) {
  755. $folders = array();
  756. }
  757. $table_footer = '';
  758. $total_size = 0;
  759. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  760. // Create a sortable table with our data
  761. $sortable_data = array();
  762. $count = 1;
  763. foreach ($docs_and_folders as $key => $document_data) {
  764. $row = array();
  765. $row['id'] = $document_data['id'];
  766. //$row['type'] = $document_data['filetype'];
  767. $row['type'] = create_document_link($document_data, true, $count, $is_visible);
  768. // If the item is invisible, wrap it in a span with class invisible
  769. $is_visible = DocumentManager::is_visible_by_id(
  770. $document_data['id'],
  771. $course_info,
  772. api_get_session_id(),
  773. api_get_user_id(),
  774. false
  775. );
  776. $invisibility_span_open = ($is_visible == 0) ? '<span class="muted">' : '';
  777. $invisibility_span_close = ($is_visible == 0) ? '</span>' : '';
  778. // Size (or total size of a directory)
  779. $size = $document_data['filetype'] == 'folder' ? FileManager::get_total_folder_size(
  780. $document_data['path'],
  781. $is_allowed_to_edit
  782. ) : $document_data['size'];
  783. $row['size'] = Text::format_file_size($size);
  784. // Get the title or the basename depending on what we're using
  785. if ($document_data['title'] != '') {
  786. $document_name = $document_data['title'];
  787. } else {
  788. $document_name = basename($document_data['path']);
  789. }
  790. $row['name'] = $document_name;
  791. $row['name'] = create_document_link(
  792. $document_data,
  793. false,
  794. null,
  795. $is_visible
  796. ).$session_img.'<br />'.$invisibility_span_open.'<i>'.nl2br(
  797. htmlspecialchars($document_data['comment'], ENT_QUOTES, $charset)
  798. ).'</i>'.$invisibility_span_close.$user_link;
  799. // Data for checkbox
  800. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  801. $row[] = $document_data['path'];
  802. }
  803. // Hide HotPotatoes Certificates and all css folders
  804. if ($document_data['path'] == '/HotPotatoes_files' || $document_data['path'] == '/certificates' || basename(
  805. $document_data['path']
  806. ) == 'css'
  807. ) {
  808. continue;
  809. }
  810. //Admin setting for Hide/Show the folders of all users
  811. if (api_get_setting('show_users_folders') == 'false' && ($document_data['path'] == '/shared_folder' || strstr(
  812. $document_data['path'],
  813. 'shared_folder_session_'
  814. ))
  815. ) {
  816. continue;
  817. }
  818. //Admin setting for Hide/Show Default folders to all users
  819. if (api_get_setting(
  820. 'show_default_folders'
  821. ) == 'false' && ($document_data['path'] == '/images' || $document_data['path'] == '/flash' || $document_data['path'] == '/audio' || $document_data['path'] == '/video')
  822. ) {
  823. continue;
  824. }
  825. //Admin setting for Hide/Show chat history folder
  826. if (api_get_setting('show_chat_folder') == 'false' && $document_data['path'] == '/chat_files') {
  827. continue;
  828. }
  829. // Show the owner of the file only in groups
  830. $user_link = '';
  831. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  832. if (!empty($document_data['insert_user_id'])) {
  833. $user_info = UserManager::get_user_info_by_id($document_data['insert_user_id']);
  834. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  835. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document(
  836. $document_data['insert_user_id'],
  837. $user_name
  838. ).'</div>';
  839. }
  840. }
  841. // Icons (clickable)
  842. $row[] = create_document_link($document_data, true, $count, $is_visible);
  843. $path_info = pathinfo($document_data['path']);
  844. if (isset($path_info['extension']) && in_array($path_info['extension'], array('ogg', 'mp3', 'wav'))) {
  845. $count++;
  846. }
  847. // Validacion when belongs to a session
  848. $session_img = api_get_session_image($document_data['session_id'], $_user['status']);
  849. // Document title with link
  850. $row[] = create_document_link(
  851. $document_data,
  852. false,
  853. null,
  854. $is_visible
  855. ).$session_img.'<br />'.$invisibility_span_open.'<i>'.nl2br(
  856. htmlspecialchars($document_data['comment'], ENT_QUOTES, $charset)
  857. ).'</i>'.$invisibility_span_close.$user_link;
  858. // Comments => display comment under the document name
  859. $display_size = Text::format_file_size($size);
  860. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  861. // Last edit date
  862. $last_edit_date = $document_data['lastedit_date'];
  863. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  864. //$display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  865. $display_date = date_to_str_ago($last_edit_date);
  866. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  867. // Admins get an edit column
  868. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder(
  869. api_get_user_id(),
  870. $curdirpath,
  871. $session_id
  872. )
  873. ) {
  874. $is_template = isset($document_data['is_template']) ? $document_data['is_template'] : false;
  875. // If readonly, check if it the owner of the file or if the user is an admin
  876. if ($document_data['insert_user_id'] == api_get_user_id() || api_is_platform_admin()) {
  877. $edit_icons = build_edit_icons($document_data, $key, $is_template, 0, $is_visible);
  878. } else {
  879. $edit_icons = build_edit_icons(
  880. $document_data,
  881. $key,
  882. $is_template,
  883. $document_data['readonly'],
  884. $is_visible
  885. );
  886. }
  887. $row[] = $edit_icons;
  888. }
  889. $row[] = $last_edit_date;
  890. $row[] = $size;
  891. $row[] = $document_name;
  892. $total_size = $total_size + $size;
  893. if ((isset($_GET['keyword']) && search_keyword(
  894. $document_name,
  895. $_GET['keyword']
  896. )) || !isset($_GET['keyword']) || empty($_GET['keyword'])
  897. ) {
  898. $sortable_data[] = $row;
  899. }
  900. }
  901. } else {
  902. $sortable_data = '';
  903. $table_footer = get_lang('NoDocsInFolder');
  904. }
  905. //The order is important you need to check the the $column variable in the model.ajax.php file
  906. $columns = array(get_lang('Type'), get_lang('Name'), get_lang('Size'));
  907. //Column config
  908. $column_model = array(
  909. array('name' => 'type', 'index' => 'type', 'width' => '28', 'align' => 'center', 'sortable' => 'false'),
  910. array('name' => 'name', 'index' => 'name', 'width' => '500', 'align' => 'left'),
  911. array('name' => 'size', 'index' => 'size', 'width' => '35', 'align' => 'right', 'sortable' => 'true')
  912. );
  913. //Autowidth
  914. $extra_params['autowidth'] = 'true';
  915. //height auto
  916. $extra_params['height'] = 'auto';
  917. //With this function we can add actions to the jgrid (edit, delete, etc)
  918. $action_links = 'function action_formatter(cellvalue, options, rowObject) {
  919. return \'<a href="?action=edit&id=\'+options.rowId+\'">'.Display::return_icon(
  920. 'edit.png',
  921. get_lang('Edit'),
  922. '',
  923. ICON_SIZE_SMALL
  924. ).'</a>'.
  925. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  926. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  927. )."\'".')) return false;" href="?action=copy&id=\'+options.rowId+\'">'.Display::return_icon(
  928. 'copy.png',
  929. get_lang('Copy'),
  930. '',
  931. ICON_SIZE_SMALL
  932. ).'</a>'.
  933. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
  934. api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
  935. )."\'".')) return false;" href="?action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
  936. 'delete.png',
  937. get_lang('Delete'),
  938. '',
  939. ICON_SIZE_SMALL
  940. ).'</a>'.
  941. '\';
  942. }';
  943. $js_content = Display::grid_js(
  944. 'documents',
  945. '',
  946. $columns,
  947. $column_model,
  948. $extra_params,
  949. $sortable_data,
  950. $action_links,
  951. true
  952. );
  953. $htmlHeadXtra[] = '<script>
  954. $(function() {
  955. // grid definition see the $career->display() function
  956. '.$js_content.'
  957. });
  958. </script>';
  959. require_once 'controller.php';
  960. $controller = new DocumentController();
  961. $tpl = $controller->tpl->get_template('layout/layout_2_col.tpl');
  962. $content = Display::grid_html('documents');
  963. if (!is_null($docs_and_folders)) {
  964. // Show download zipped folder icon
  965. global $total_size;
  966. if (!$is_certificate_mode && $total_size != 0 && (api_get_setting(
  967. 'students_download_folders'
  968. ) == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())
  969. ) {
  970. //for student does not show icon into other shared folder, and does not show into main path (root)
  971. if (is_my_shared_folder(
  972. api_get_user_id(),
  973. $curdirpath,
  974. $session_id
  975. ) && $curdirpath != '/' || api_is_allowed_to_edit() || api_is_platform_admin()
  976. ) {
  977. $link = '<a href="'.api_get_self().'?'.api_get_cidreq(
  978. ).'&amp;action=downloadfolder&amp;id='.$document_id.'">'.Display::return_icon(
  979. 'save_pack.png',
  980. get_lang('Save').' (ZIP)',
  981. '',
  982. ICON_SIZE_MEDIUM
  983. ).'</a>';
  984. }
  985. }
  986. }
  987. $content .= Display::div($link, array('class' => 'right'));
  988. $controller->tpl->assign('content', $content);
  989. $controller->tpl->display($tpl);