document_lite.php 38 KB

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