document_lite.php 40 KB

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