document.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Homepage script for the documents tool
  5. *
  6. * This script allows the user to manage files and directories on a remote http
  7. * server.
  8. * The user can : - navigate through files and directories.
  9. * - upload a file
  10. * - delete, copy a file or a directory
  11. * - edit properties & content (name, comments, html content)
  12. * The script is organised in four sections.
  13. *
  14. * 1) Execute the command called by the user
  15. * Note: somme commands of this section are organised in two steps.
  16. * The script always begins with the second step,
  17. * so it allows to return more easily to the first step.
  18. *
  19. * Note (March 2004) some editing functions (renaming, commenting)
  20. * are moved to a separate page, edit_document.php. This is also
  21. * where xml and other stuff should be added.
  22. * 2) Define the directory to display
  23. * 3) Read files and directories from the directory defined in part 2
  24. * 4) Display all of that on an HTML page
  25. *
  26. * @todo eliminate code duplication with document/document.php, scormdocument.php
  27. *
  28. * @package chamilo.document
  29. */
  30. /* INIT SECTION */
  31. // Language files that need to be included
  32. $language_file = array('document', 'slideshow', 'gradebook');
  33. require_once '../inc/global.inc.php';
  34. $this_section = SECTION_COURSES;
  35. require_once 'document.inc.php';
  36. $lib_path = api_get_path(LIBRARY_PATH);
  37. require_once $lib_path.'usermanager.lib.php';
  38. require_once $lib_path.'document.lib.php';
  39. require_once $lib_path.'fileUpload.lib.php';
  40. require_once $lib_path.'sortabletable.class.php';
  41. require_once $lib_path.'formvalidator/FormValidator.class.php';
  42. api_protect_course_script(true);
  43. //jquery thickbox already called from main/inc/header.inc.php
  44. $htmlHeadXtra[] = '<script type="text/javascript">
  45. $(document).ready( function() {
  46. for (i=0;i<$(".actions").length;i++) {
  47. if ($(".actions:eq("+i+")").html()=="<table border=\"0\"></table>" || $(".actions:eq("+i+")").html()=="" || $(".actions:eq("+i+")").html()==null) {
  48. $(".actions:eq("+i+")").hide();
  49. }
  50. }
  51. } );
  52. </script>';
  53. // Session
  54. if (isset($_GET['id_session'])) {
  55. $_SESSION['id_session'] = intval($_GET['id_session']);
  56. }
  57. // Create directory certificates
  58. $course_id = api_get_course_id();
  59. DocumentManager::create_directory_certificate_in_course($course_id);
  60. // Show preview
  61. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_preview']) && $_GET['set_preview'] == strval(intval($_GET['set_preview']))) {
  62. if (isset($_GET['set_preview'])) {
  63. // Generate document HTML
  64. $course_id = api_get_course_id();
  65. $content_html = DocumentManager::replace_user_info_into_html($course_id);
  66. $new_content_html = $content_html;
  67. $path_image = api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/images/gallery';
  68. $new_content_html = str_replace('../images/gallery', $path_image, $new_content_html);
  69. $path_image_in_default_course = api_get_path(WEB_CODE_PATH).'default_course_document';
  70. $new_content_html = str_replace('/main/default_course_document', $path_image_in_default_course, $new_content_html);
  71. $new_content_html = str_replace('/main/img/', api_get_path(WEB_IMG_PATH), $new_content_html);
  72. echo '
  73. <style media="print" type="text/css">
  74. #imprimir {
  75. visibility:hidden;
  76. }
  77. </style>';
  78. echo '<a href="javascript:window.print();" style="float:right; padding:4px;" id="imprimir"><img src="../img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
  79. print_r($new_content_html);
  80. exit;
  81. }
  82. }
  83. // Is the document tool visible?
  84. // Check whether the tool is actually visible
  85. $table_course_tool = Database::get_course_table(TABLE_TOOL_LIST, $_course['dbName']);
  86. $tool_sql = 'SELECT visibility FROM ' . $table_course_tool . ' WHERE name = "'. TOOL_DOCUMENT .'" LIMIT 1';
  87. $tool_result = Database::query($tool_sql);
  88. $tool_row = Database::fetch_array($tool_result);
  89. $tool_visibility = $tool_row['visibility'];
  90. if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $group_member_with_upload_rights)) {
  91. api_not_allowed(true);
  92. }
  93. $htmlHeadXtra[] =
  94. "<script type=\"text/javascript\">
  95. function confirmation (name)
  96. {
  97. if (confirm(\" ". get_lang("AreYouSureToDelete") ." \"+ name + \" ?\"))
  98. {return true;}
  99. else
  100. {return false;}
  101. }
  102. </script>";
  103. /*
  104. Variables
  105. - some need defining before inclusion of libraries
  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. // I'm in the certification module?
  118. $is_certificate_mode = DocumentManager::is_certificate_mode($curdirpath);
  119. $course_dir = $_course['path'].'/document';
  120. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  121. $base_work_dir = $sys_course_path.$course_dir;
  122. $http_www = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document';
  123. $dbl_click_id = 0; // Used for avoiding double-click
  124. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  125. $group_member_with_upload_rights = false;
  126. // If the group id is set, we show them group documents
  127. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  128. // Needed for group related stuff
  129. require_once $lib_path.'groupmanager.lib.php';
  130. // Get group info
  131. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  132. $noPHP_SELF = true;
  133. // Let's assume the user cannot upload files for the group
  134. $group_member_with_upload_rights = false;
  135. if ($group_properties['doc_state'] == 2) { // Documents are private
  136. if ($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'], $_SESSION['_gid'])) { // Only courseadmin or group members (members + tutors) allowed
  137. $to_group_id = $_SESSION['_gid'];
  138. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  139. $interbreadcrumb[] = array('url' => '../group/group.php', 'name' => get_lang('Groups'));
  140. $interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.$_SESSION['_gid'], 'name' => get_lang('GroupSpace').' '.$group_properties['name']);
  141. //they are allowed to upload
  142. $group_member_with_upload_rights = true;
  143. } else {
  144. $to_group_id = 0;
  145. $req_gid = '';
  146. }
  147. } elseif ($group_properties['doc_state'] == 1) { // Documents are public
  148. $to_group_id = $_SESSION['_gid'];
  149. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  150. $interbreadcrumb[] = array('url' => '../group/group.php', 'name' => get_lang('Groups'));
  151. $interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.$_SESSION['_gid'], 'name' => get_lang('GroupSpace').' '.$group_properties['name']);
  152. //allowed to upload?
  153. if ($is_allowed_to_edit || GroupManager::is_subscribed($_user['user_id'], $_SESSION['_gid'])) { // Only courseadmin or group members can upload
  154. $group_member_with_upload_rights = true;
  155. }
  156. } else { // Documents not active for this group
  157. $to_group_id = 0;
  158. $req_gid = '';
  159. }
  160. $_SESSION['group_member_with_upload_rights'] = $group_member_with_upload_rights;
  161. } else {
  162. $to_group_id = 0;
  163. $req_gid = '';
  164. }
  165. // For sessions we should check the parameters of visibility
  166. if (api_get_session_id() != 0) {
  167. $group_member_with_upload_rights = $group_member_with_upload_rights && api_is_allowed_to_session_edit(false, true);
  168. }
  169. /* Libraries */
  170. require_once $lib_path.'fileDisplay.lib.php';
  171. require_once $lib_path.'document.lib.php';
  172. require_once $lib_path.'tablesort.lib.php';
  173. require_once $lib_path.'fileUpload.lib.php';
  174. // Check the path
  175. // If the path is not found (no document id), set the path to /
  176. if (!DocumentManager::get_document_id($_course, $curdirpath)) {
  177. $curdirpath = '/';
  178. // Urlencoded version
  179. $curdirpathurl = '%2F';
  180. }
  181. // If they are looking at group documents they can't see the root
  182. if ($to_group_id != 0 && $curdirpath == '/') {
  183. $curdirpath = $group_properties['directory'];
  184. $curdirpathurl = urlencode($group_properties['directory']);
  185. }
  186. // Check visibility of the current dir path. Don't show anything if not allowed
  187. if (!$is_allowed_to_edit || api_is_coach()) {
  188. if (!(DocumentManager::is_visible($curdirpath, $_course, api_get_session_id()))) {
  189. api_not_allowed();
  190. }
  191. }
  192. /* Constants and variables */
  193. $course_quota = DocumentManager::get_course_quota();
  194. $current_session_id = api_get_session_id();
  195. /* Create shared folders */
  196. if($current_session_id==0){
  197. //Create shared folder. Necessary for courses recycled. Allways session_id should be zero. Allway should be created from a base course, never from a session.
  198. if (!file_exists($base_work_dir.'/shared_folder')) {
  199. $usf_dir_title = get_lang('SharedFolder');
  200. $usf_dir_name = '/shared_folder';
  201. $to_group_id = 0;
  202. $visibility = 0;
  203. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  204. }
  205. // Create dynamic user shared folder
  206. if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.api_get_user_id())) {
  207. $usf_dir_title = api_get_person_name($_user['firstName'], $_user['lastName']);
  208. $usf_dir_name = '/shared_folder/sf_user_'.api_get_user_id();
  209. $to_group_id = 0;
  210. $visibility = 1;
  211. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  212. }
  213. }
  214. else{
  215. //Create shared folder session
  216. if (!file_exists($base_work_dir.'/shared_folder_session_'.$current_session_id)) {
  217. $usf_dir_title = get_lang('SharedFolder').' ('.api_get_session_name($current_session_id).')';
  218. $usf_dir_name = '/shared_folder_session_'.$current_session_id;
  219. $to_group_id = 0;
  220. $visibility = 0;
  221. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  222. }
  223. //Create dynamic user shared folder into a shared folder session
  224. if (!file_exists($base_work_dir.'/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id())) {
  225. $usf_dir_title = api_get_person_name($_user['firstName'], $_user['lastName']).' ('.api_get_session_name($current_session_id).')';
  226. $usf_dir_name = '/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id();
  227. $to_group_id = 0;
  228. $visibility = 1;
  229. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  230. }
  231. }
  232. /* MAIN SECTION */
  233. if (isset($_GET['action']) && $_GET['action'] == 'download') {
  234. $my_get_id = Security::remove_XSS($_GET['id']);
  235. // Check whether the document is in the database
  236. if (!DocumentManager::get_document_id($_course, $my_get_id)) {
  237. // File not found!
  238. header('HTTP/1.0 404 Not Found');
  239. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  240. $error404 .= '<html><head>';
  241. $error404 .= '<title>404 Not Found</title>';
  242. $error404 .= '</head><body>';
  243. $error404 .= '<h1>Not Found</h1>';
  244. $error404 .= '<p>The requested URL was not found on this server.</p>';
  245. $error404 .= '<hr>';
  246. $error404 .= '</body></html>';
  247. echo $error404;
  248. exit;
  249. }
  250. // Launch event
  251. event_download($my_get_id);
  252. // Check visibility of document and paths
  253. if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible($my_get_id, $_course)) {
  254. api_not_allowed();
  255. }
  256. $doc_url = $my_get_id;
  257. $full_file_name = $base_work_dir.$doc_url;
  258. DocumentManager::file_send_for_download($full_file_name, true);
  259. exit;
  260. }
  261. // Download a folder
  262. if (isset($_GET['action']) && $_GET['action'] == 'downloadfolder' && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  263. //filter when I am into shared folder, I can donwload only my shared folder
  264. if(is_any_user_shared_folder($_GET['path'],$current_session_id))
  265. {
  266. if(is_my_shared_folder($_user['user_id'], $_GET['path'], $current_session_id) || api_is_allowed_to_edit() || api_is_platform_admin())
  267. {
  268. require 'downloadfolder.inc.php';
  269. }
  270. }
  271. else
  272. {
  273. require 'downloadfolder.inc.php';
  274. }
  275. }
  276. // Slideshow inititalisation
  277. $_SESSION['image_files_only'] = '';
  278. $image_files_only = '';
  279. /* Header */
  280. if ($is_certificate_mode) {
  281. $interbreadcrumb[]= array('url' => '../gradebook/index.php', 'name' => get_lang('Gradebook'));
  282. } else {
  283. $interbreadcrumb[]= array('url' => '', 'name' => get_lang('Documents'));
  284. }
  285. // Interbreadcrumb for the current directory root path
  286. $dir_array = explode('/', $curdirpath);
  287. $array_len = count($dir_array);
  288. /*
  289. TODO:check and delete this code
  290. if (!$is_certificate_mode) {
  291. if ($array_len > 1) {
  292. if (empty($_SESSION['_gid'])) {
  293. $url_dir = 'document.php?&curdirpath=/';
  294. $interbreadcrumb[] = array('url' => $url_dir, 'name' => get_lang('HomeDirectory'));
  295. }
  296. }
  297. }
  298. */
  299. $dir_acum = '';
  300. for ($i = 0; $i < $array_len; $i++) {
  301. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  302. //Max char 80
  303. $url_to_who = cut($dir_array[$i],80);
  304. if ($is_certificate_mode) {
  305. $interbreadcrumb[] = array('url' => $url_dir.'&selectcat='.Security::remove_XSS($_GET['selectcat']), 'name' => $url_to_who);
  306. }
  307. else{
  308. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  309. }
  310. //does not repeat the name group in the url
  311. if (!empty($_SESSION['_gid'])) {
  312. unset($dir_array[1]);
  313. }
  314. $dir_acum .= $dir_array[$i].'/';
  315. }
  316. if (isset($_GET['createdir'])) {
  317. $interbreadcrumb[] = array('url' => '', 'name' => get_lang('CreateDir'));
  318. }
  319. Display::display_header('','Doc');
  320. // Lib for event log, stats & tracking & record of the access
  321. event_access_tool(TOOL_DOCUMENT);
  322. /* DISPLAY */
  323. if ($to_group_id != 0) { // Add group name after for group documents
  324. $add_group_to_title = ' ('.$group_properties['name'].')';
  325. }
  326. /* Introduction section (editable by course admins) */
  327. if (!empty($_SESSION['_gid'])) {
  328. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  329. } else {
  330. Display::display_introduction_section(TOOL_DOCUMENT);
  331. }
  332. // Copy a file to general my files user's
  333. if (isset($_GET['action']) && $_GET['action'] == 'copytomyfiles' && api_get_setting('users_copy_files') == 'true' && api_get_user_id() != 0) {
  334. $clean_get_id = Security::remove_XSS($_GET['id']);
  335. $user_folder = api_get_path(SYS_CODE_PATH).'upload/users/'.api_get_user_id().'/my_files/';
  336. if (!file_exists($user_folder)) {
  337. @mkdir($user_folder, $permissions_for_new_directories, true);
  338. }
  339. $file = $sys_course_path.$_course['path'].'/document'.$clean_get_id;
  340. $copyfile = $user_folder.basename($clean_get_id);
  341. if (file_exists($copyfile)) {
  342. $message = get_lang('CopyAlreadyDone').'</p><p>'.'<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$_GET['curdirpath'].'">'.get_lang("No").'</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$_GET['curdirpath'].'&amp;action=copytomyfiles&amp;id='.$clean_get_id.'&amp;copy=yes">'.get_lang("Yes").'</a></p>';
  343. if (!isset($_GET['copy'])){
  344. Display::display_warning_message($message,false);
  345. }
  346. if (Security::remove_XSS($_GET['copy']) == 'yes'){
  347. if (!copy($file, $copyfile)) {
  348. Display::display_error_message(get_lang('CopyFailed'));
  349. }else{
  350. Display::display_confirmation_message(get_lang('OverwritenFile'));
  351. }
  352. }
  353. }else{
  354. if (!copy($file, $copyfile)) {
  355. Display::display_error_message(get_lang('CopyFailed'));
  356. }else{
  357. Display::display_confirmation_message(get_lang('CopyMade'));
  358. }
  359. }
  360. }
  361. if ($is_allowed_to_edit || $group_member_with_upload_rights) { // TEACHER ONLY
  362. /* MOVE FILE OR DIRECTORY */
  363. $my_get_move = Security::remove_XSS($_GET['move']);
  364. if (isset($_GET['move']) && $_GET['move'] != '') {
  365. if (api_is_coach()) {
  366. if (!DocumentManager::is_visible_by_id($my_get_move, $_course,api_get_session_id())) {
  367. api_not_allowed();
  368. }
  369. }
  370. if (!$is_allowed_to_edit) {
  371. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  372. api_not_allowed();
  373. }
  374. }
  375. if (DocumentManager::get_document_id($_course, $my_get_move)) {
  376. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  377. echo '<div class="row"><div class="form_header">'.get_lang('Move').'</div></div>';
  378. echo build_move_to_selector($folders, Security::remove_XSS($_GET['curdirpath']), $my_get_move, $group_properties['directory']);
  379. }
  380. }
  381. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  382. if (!$is_allowed_to_edit) {
  383. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  384. api_not_allowed();
  385. }
  386. }
  387. if (api_is_coach()) {
  388. if (!DocumentManager::is_visible_by_id($my_get_move, $_course,api_get_session_id())) {
  389. api_not_allowed();
  390. }
  391. }
  392. require_once $lib_path.'fileManage.lib.php';
  393. // This is needed for the update_db_info function
  394. //$dbTable = $_course['dbNameGlu'].'document';
  395. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  396. // Security fix: make sure they can't move files that are not in the document table
  397. if (DocumentManager::get_document_id($_course, $_POST['move_file'])) {
  398. if (move($base_work_dir.$_POST['move_file'], $base_work_dir.$_POST['move_to'])) {
  399. update_db_info('update', $_POST['move_file'], $_POST['move_to'].'/'.basename($_POST['move_file']));
  400. // Set the current path
  401. $curdirpath = $_POST['move_to'];
  402. $curdirpathurl = urlencode($_POST['move_to']);
  403. Display::display_confirmation_message(get_lang('DirMv'));
  404. } else {
  405. Display::display_error_message(get_lang('Impossible'));
  406. }
  407. } else {
  408. Display::display_error_message(get_lang('Impossible'));
  409. }
  410. }
  411. /* DELETE FILE OR DIRECTORY */
  412. if (isset($_GET['delete'])) {
  413. if (api_is_coach()) {
  414. if (!DocumentManager::is_visible($_GET['delete'], $_course)) {
  415. api_not_allowed();
  416. }
  417. }
  418. if (!$is_allowed_to_edit) {
  419. if (DocumentManager::check_readonly($_course, $_user['user_id'], $_GET['delete'], '', true)) {
  420. api_not_allowed();
  421. }
  422. }
  423. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  424. if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
  425. if ( isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(intval($_GET['delete_certificate_id']))) {
  426. $course_id = api_get_course_id();
  427. $default_certificate_id = $_GET['delete_certificate_id'];
  428. DocumentManager::remove_attach_certificate($course_id, $default_certificate_id);
  429. }
  430. Display::display_confirmation_message(get_lang('DocDeleted'));
  431. } else {
  432. Display::display_error_message(get_lang('DocDeleteError'));
  433. }
  434. }
  435. if (isset($_POST['action'])) {
  436. switch ($_POST['action']) {
  437. case 'delete':
  438. foreach ($_POST['path'] as $index => & $path) {
  439. if (!$is_allowed_to_edit) {
  440. if (DocumentManager::check_readonly($_course, $_user['user_id'], $path)) {
  441. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  442. break 2;
  443. }
  444. }
  445. }
  446. foreach ($_POST['path'] as $index => & $path) {
  447. if (in_array($path, array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files', '/certificates'))) {
  448. continue;
  449. } else {
  450. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  451. }
  452. }
  453. if (!empty($delete_document)) {
  454. Display::display_confirmation_message(get_lang('DocDeleted'));
  455. }
  456. break;
  457. }
  458. }
  459. /* CREATE DIRECTORY */
  460. // Create directory with $_POST data
  461. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  462. // Needed for directory creation
  463. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  464. $post_dir_name = Security::remove_XSS($_POST['dirname']);
  465. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  466. Display::display_error_message(get_lang('CannotCreateDir'));
  467. } else {
  468. $added_slash = ($curdirpath == '/') ? '' : '/';
  469. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  470. $dir_name = disable_dangerous_file($dir_name);
  471. $dir_check = $base_work_dir.$dir_name;
  472. if (!is_dir($dir_check)) {
  473. $created_dir = create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name);
  474. if ($created_dir) {
  475. Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false);
  476. // Uncomment if you want to enter the created dir
  477. //$curdirpath = $created_dir;
  478. //$curdirpathurl = urlencode($curdirpath);
  479. } else {
  480. Display::display_error_message(get_lang('CannotCreateDir'));
  481. }
  482. } else {
  483. Display::display_error_message(get_lang('CannotCreateDir'));
  484. }
  485. }
  486. }
  487. // Show them the form for the directory name
  488. if (isset($_GET['createdir'])) {
  489. echo create_dir_form();
  490. }
  491. /* VISIBILITY COMMANDS */
  492. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  493. // Make visible or invisible?
  494. if (isset($_GET['set_visible'])) {
  495. $update_id = $_GET['set_visible'];
  496. $visibility_command = 'visible';
  497. } else {
  498. $update_id = $_GET['set_invisible'];
  499. $visibility_command = 'invisible';
  500. }
  501. if (api_is_coach()) {
  502. if (!DocumentManager::is_visible_by_id($update_id, $_course)) {
  503. api_not_allowed();
  504. }
  505. }
  506. if (!$is_allowed_to_edit) {
  507. if(DocumentManager::check_readonly($_course, $_user['user_id'], '', $update_id)) {
  508. api_not_allowed();
  509. }
  510. }
  511. // Update item_property to change visibility
  512. if (api_item_property_update($_course, TOOL_DOCUMENT, $update_id, $visibility_command, $_user['user_id'], null, null, null, null, $current_session_id)) {
  513. Display::display_confirmation_message(get_lang('VisibilityChanged'));//don't use ViMod because firt is load ViMdod (Gradebook). VisibilityChanged (trad4all)
  514. } else {
  515. Display::display_error_message(get_lang('ViModProb'));
  516. }
  517. }
  518. /* TEMPLATE ACTION */
  519. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  520. $document_id_for_template = intval($_GET['add_as_template']);
  521. // Create the form that asks for the directory name
  522. $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">';
  523. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  524. $template_text .= '<table><tr><td>';
  525. $template_text .= get_lang('TemplateName').' : </td>';
  526. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  527. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  528. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  529. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  530. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  531. $template_text .= '</table>';
  532. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang('CreateTemplate').'</button>';
  533. $template_text .= '</form>';
  534. // Show the form
  535. Display::display_normal_message($template_text, false);
  536. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  537. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  538. $title = Security::remove_XSS($_POST['template_title']);
  539. //$description = Security::remove_XSS($_POST['template_description']);
  540. $course_code = api_get_course_id();
  541. $user_id = api_get_user_id();
  542. // Create the template_thumbnails folder in the upload folder (if needed)
  543. if (!is_dir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/')) {
  544. @mkdir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/', api_get_permissions_for_new_directories());
  545. }
  546. // Upload the file
  547. if (!empty($_FILES['template_image']['name'])) {
  548. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  549. $upload_ok = process_uploaded_file($_FILES['template_image']);
  550. if ($upload_ok) {
  551. // Try to add an extension to the file if it hasn't one
  552. $new_file_name = $_course['sysCode'].'-'.add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  553. // Upload dir
  554. $upload_dir = api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/';
  555. // Resize image to max default and end upload
  556. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  557. $temp = new image($_FILES['template_image']['tmp_name']);
  558. $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
  559. $max_width_for_picture = 100;
  560. if ($picture_infos[0] > $max_width_for_picture) {
  561. $thumbwidth = $max_width_for_picture;
  562. if (empty($thumbwidth) || $thumbwidth == 0) {
  563. $thumbwidth = $max_width_for_picture;
  564. }
  565. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  566. $temp->resize($thumbwidth, $new_height, 0);
  567. }
  568. $type = $picture_infos[2];
  569. switch (!empty($type)) {
  570. case 2 : $temp->send_image('JPG', $upload_dir.$new_file_name);
  571. break;
  572. case 3 : $temp->send_image('PNG', $upload_dir.$new_file_name);
  573. break;
  574. case 1 : $temp->send_image('GIF', $upload_dir.$new_file_name);
  575. break;
  576. }
  577. }
  578. }
  579. DocumentManager::set_document_as_template($title, $description, $document_id_for_template, $course_code, $user_id, $new_file_name);
  580. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  581. }
  582. if (isset($_GET['remove_as_template'])) {
  583. $document_id_for_template = intval($_GET['remove_as_template']);
  584. $course_code = api_get_course_id();
  585. $user_id = api_get_user_id();
  586. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  587. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  588. }
  589. } // END is allowed to edit
  590. // Attach certificate in the gradebook
  591. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate'] == strval(intval($_GET['set_certificate']))) {
  592. if (isset($_GET['cidReq'])) {
  593. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  594. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  595. DocumentManager::attach_gradebook_certificate ($course_id,$document_id);
  596. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  597. }
  598. }
  599. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  600. if(isset($_GET['keyword']) && !empty($_GET['keyword'])){
  601. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, $search=true);
  602. }else{
  603. $docs_and_folders = DocumentManager::get_all_document_data($_course, $curdirpath, $to_group_id, null, $is_allowed_to_edit || $group_member_with_upload_rights, $search=false);
  604. }
  605. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  606. if ($folders === false) {
  607. $folders = array();
  608. }
  609. echo '<div class="actions">';
  610. if ($is_allowed_to_edit || $group_member_with_upload_rights){
  611. /* BUILD SEARCH FORM */
  612. echo '<span style="display:inline-block;">';
  613. $form = new FormValidator('search_document', 'get', '', '', null, false);
  614. $renderer = & $form->defaultRenderer();
  615. $renderer->setElementTemplate('<span>{element}</span> ');
  616. $form->add_textfield('keyword', '', false);
  617. $form->addElement('style_submit_button', 'submit', get_lang('Search'), 'class="search"');
  618. $form->display();
  619. echo '</span>';
  620. }
  621. /* GO TO PARENT DIRECTORY */
  622. if ($curdirpath!= '/' && $curdirpath != $group_properties['directory'] && !$is_certificate_mode) {
  623. ?>
  624. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath) == '\\') ? '/' : dirname($curdirpath)).$req_gid; ?>">
  625. <?php Display::display_icon('folder_up.gif', get_lang('Up')); echo get_lang('Up'); ?></a>&nbsp;
  626. <?php
  627. }
  628. if ($is_certificate_mode && $curdirpath != '/certificates') {
  629. ?>
  630. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath) == '\\') ? '/' : dirname($curdirpath)).$req_gid; ?>">
  631. <?php Display::display_icon('folder_up.gif', get_lang('Up')); echo get_lang('Up'); ?></a>&nbsp;
  632. <?php
  633. }
  634. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  635. //echo('<pre>');
  636. //print_r($docs_and_folders);
  637. //echo('</pre>');
  638. // Do we need the title field for the document name or not?
  639. // We get the setting here, so we only have to do it once
  640. $use_document_title = api_get_setting('use_document_title');
  641. // Create a sortable table with our data
  642. $sortable_data = array();
  643. //while (list($key, $id) = each($docs_and_folders)) {
  644. foreach($docs_and_folders as $key=>$id) {
  645. $row = array();
  646. // If the item is invisible, wrap it in a span with class invisible
  647. $invisibility_span_open = ($id['visibility'] == 0) ? '<span class="invisible">' : '';
  648. $invisibility_span_close = ($id['visibility'] == 0) ? '</span>' : '';
  649. // Size (or total size of a directory)
  650. $size = $id['filetype'] == 'folder' ? get_total_folder_size($id['path'], $is_allowed_to_edit) : $id['size'];
  651. // Get the title or the basename depending on what we're using
  652. if ($use_document_title == 'true' && $id['title'] != '') {
  653. $document_name = $id['title'];
  654. } else {
  655. $document_name = basename($id['path']);
  656. }
  657. // Data for checkbox
  658. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  659. $row[] = $id['path'];
  660. }
  661. // Show the owner of the file only in groups
  662. $user_link = '';
  663. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  664. if (!empty($id['insert_user_id'])) {
  665. $user_info = UserManager::get_user_info_by_id($id['insert_user_id']);
  666. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  667. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document($id['insert_user_id'], $user_name).'</div>';
  668. }
  669. }
  670. // Icons (clickable)
  671. //$row[]= build_document_icon_tag($id['filetype'],$id['path']);
  672. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility'], true);
  673. // Validacion when belongs to a session
  674. $session_img = api_get_session_image($id['session_id'], $_user['status']);
  675. // Document title with hyperlink
  676. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility']).$session_img.'<br />'.$invisibility_span_open.nl2br(htmlspecialchars($id['comment'],ENT_QUOTES,$charset)).$invisibility_span_close.$user_link;
  677. // Comments => display comment under the document name
  678. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  679. $display_size = format_file_size($size);
  680. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  681. // Last edit date
  682. $last_edit_date = $id['lastedit_date'];
  683. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  684. $display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  685. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  686. // Admins get an edit column
  687. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  688. $is_template = isset($id['is_template']) ? $id['is_template'] : false;
  689. // If readonly, check if it the owner of the file or if the user is an admin
  690. if ($id['insert_user_id'] == $_user['user_id'] || api_is_platform_admin()) {
  691. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, 0);
  692. } else {
  693. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, $id['readonly']);
  694. }
  695. $row[] = $edit_icons;
  696. }
  697. $row[] = $last_edit_date;
  698. $row[] = $size;
  699. $total_size = $total_size + $size;
  700. if ((isset ($_GET['keyword']) && search_keyword($document_name, $_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])) {
  701. $sortable_data[] = $row;
  702. }
  703. }
  704. } else {
  705. $sortable_data = '';
  706. $table_footer = '<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  707. }
  708. $column_show = array();
  709. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder($_user['user_id'], $curdirpath, $current_session_id)) {
  710. // TODO:check enable more options for shared folders
  711. /* CREATE NEW DOCUMENT OR NEW DIRECTORY / GO TO UPLOAD / DOWNLOAD ZIPPED FOLDER */
  712. // Create new document
  713. if (!$is_certificate_mode) {
  714. ?>
  715. <a href="create_document.php?<?php echo api_get_cidreq(); ?>&dir=<?php echo $curdirpathurl.$req_gid; ?>">
  716. <?php Display::display_icon('filenew.gif', get_lang('CreateDoc')); echo get_lang('CreateDoc'); ?></a>&nbsp;
  717. <?php
  718. // Create new draw
  719. if (api_get_setting('enabled_support_svg') == 'true'){
  720. if (api_browser_support('svg')){
  721. ?>
  722. <a href="create_drawing.php?<?php echo api_get_cidreq(); ?>&dir=<?php echo $curdirpathurl.$req_gid; ?>">
  723. <?php Display::display_icon('draw_new.png', get_lang('Draw')); echo get_lang('Draw'); ?></a>&nbsp;
  724. <?php
  725. }else{
  726. Display::display_icon('draw_new.png', get_lang('BrowserDontSupportsSVG')); echo get_lang('Draw').'&nbsp;';
  727. }
  728. }
  729. }
  730. // Create new certificate
  731. if ($is_certificate_mode) {
  732. ?>
  733. <a href="create_document.php?<?php echo api_get_cidreq(); ?>&dir=<?php echo $curdirpathurl.$req_gid; ?>&certificate=true&<?php echo 'selectcat='.Security::remove_XSS($_GET['selectcat']); ?>">
  734. <?php Display::display_icon('filenew.gif', get_lang('CreateCertificate')); echo get_lang('CreateCertificate'); ?></a>&nbsp;
  735. <?php
  736. }
  737. // File upload link
  738. $upload_name = $is_certificate_mode ? get_lang('UploadCertificate') : get_lang('UplUploadDocument');
  739. ?>
  740. <a href="upload.php?<?php echo api_get_cidreq(); ?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>">
  741. <?php Display::display_icon('submit_file.gif', $upload_name); echo $upload_name; ?></a>&nbsp;
  742. <?php
  743. // Create directory
  744. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $curdirpath, $current_session_id)) {
  745. ?>
  746. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq(); ?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>&amp;createdir=1">
  747. <?php Display::display_icon('folder_new.gif', get_lang('CreateDir')); echo get_lang('CreateDir'); ?></a>&nbsp;
  748. <a href="quota.php?<?php echo api_get_cidreq(); ?>">
  749. <?php Display::display_icon('statistics.gif', get_lang('ShowCourseQuotaUse')); echo get_lang('ShowCourseQuotaUse'); ?></a>&nbsp;
  750. <?php
  751. }
  752. }
  753. if (!is_null($docs_and_folders)) {
  754. // Show download zipped folder icon
  755. global $total_size;
  756. if (!$is_certificate_mode && $total_size != 0 && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  757. //don't show icon into shared folder, and don´t show into main path (root)
  758. if (!is_shared_folder($curdirpath, $current_session_id) && $curdirpath!='/' || api_is_allowed_to_edit() || api_is_platform_admin())
  759. {
  760. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&action=downloadfolder&path='.$curdirpathurl.'">'.Display::display_icon('zip_save.gif', get_lang('Save').' (ZIP)'). get_lang('Save').' (ZIP)</a>&nbsp';
  761. }
  762. }
  763. }
  764. // Slideshow by Patrick Cool, May 2004
  765. require 'document_slideshow.inc.php';
  766. if ($image_present && !isset($_GET['keyword']) ) {
  767. echo '<a href="slideshow.php?'.api_get_cidreq().'&curdirpath='.$curdirpathurl.'"><img src="../img/images_gallery.gif" border="0" title="'.get_lang('ViewSlideshow').'"/>'.get_lang('ViewSlideshow').'</a>';
  768. }
  769. echo '</div>';
  770. if (!$is_certificate_mode) {
  771. echo build_directory_selector($folders, $curdirpath, (isset($group_properties['directory']) ? $group_properties['directory'] : array()), true);
  772. }
  773. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  774. $column_show[] = 1;
  775. }
  776. $column_show[] = 1;
  777. $column_show[] = 1;
  778. $column_show[] = 1;
  779. $column_show[] = 1;
  780. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  781. $column_show[] = 1;
  782. }
  783. $column_show[] = 0;
  784. $column_order = array();
  785. if (count($row) == 8) {
  786. $column_order[3] = 7;
  787. $column_order[4] = 6;
  788. } elseif (count($row) == 6) {
  789. $column_order[2] = 5;
  790. $column_order[3] = 4;
  791. }
  792. $default_column = $is_allowed_to_edit ? 2 : 1;
  793. $tablename = $is_allowed_to_edit ? 'teacher_table' : 'student_table';
  794. $table = new SortableTableFromArrayConfig($sortable_data, $default_column, 20, $tablename, $column_show, $column_order, 'ASC');
  795. if(isset($_GET['keyword'])){
  796. $query_vars['keyword'] = Security::remove_XSS($_GET['keyword']);
  797. }else{
  798. $query_vars['curdirpath'] = $curdirpath;
  799. }
  800. if (isset($_SESSION['_gid'])) {
  801. $query_vars['gidReq'] = $_SESSION['_gid'];
  802. }
  803. $query_vars['cidReq'] = api_get_course_id();
  804. $table->set_additional_parameters($query_vars);
  805. $column = 0;
  806. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  807. $table->set_header($column++, '', false,array ('style' => 'width:30px;'));
  808. }
  809. $table->set_header($column++, get_lang('Type'),true,array ('style' => 'width:30px;'));
  810. $table->set_header($column++, get_lang('Name'));
  811. //$column_header[] = array(get_lang('Comment'), true); // Display comment under the document name
  812. $table->set_header($column++, get_lang('Size'),true,array ('style' => 'width:50px;'));
  813. $table->set_header($column++, get_lang('Date'),true,array ('style' => 'width:150px;'));
  814. // Admins get an edit column
  815. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  816. $table->set_header($column++, get_lang('Modify'), false,array ('style' => 'width:150px;'));
  817. }
  818. // Actions on multiple selected documents
  819. // TODO: Currently only delete action -> take only DELETE right into account
  820. if (count($docs_and_folders) > 1) {
  821. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  822. $form_actions = array();
  823. $form_action['delete'] = get_lang('Delete');
  824. $table->set_form_actions($form_action, 'path');
  825. }
  826. }
  827. $table->display();
  828. if (!empty($table_footer)) {
  829. echo $table_footer;
  830. }
  831. // Footer
  832. Display::display_footer()
  833. ?>