document.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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 (!(DocumentManager::is_visible($curdirpath, $_course) || $is_allowed_to_edit)) {
  188. api_not_allowed();
  189. }
  190. /* Constants and variables */
  191. $course_quota = DocumentManager::get_course_quota();
  192. $current_session_id = api_get_session_id();
  193. /* Create shared folders */
  194. if($current_session_id==0){
  195. //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.
  196. if (!file_exists($base_work_dir.'/shared_folder')) {
  197. $usf_dir_title = get_lang('SharedFolder');
  198. $usf_dir_name = '/shared_folder';
  199. $to_group_id = 0;
  200. $visibility = 0;
  201. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  202. }
  203. // Create dynamic user shared folder
  204. if (!file_exists($base_work_dir.'/shared_folder/sf_user_'.api_get_user_id())) {
  205. $usf_dir_title = api_get_person_name($_user['firstName'], $_user['lastName']);
  206. $usf_dir_name = '/shared_folder/sf_user_'.api_get_user_id();
  207. $to_group_id = 0;
  208. $visibility = 1;
  209. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  210. }
  211. }
  212. else{
  213. //Create shared folder session
  214. if (!file_exists($base_work_dir.'/shared_folder_session_'.$current_session_id)) {
  215. $usf_dir_title = get_lang('SharedFolder').' ('.api_get_session_name($current_session_id).')';
  216. $usf_dir_name = '/shared_folder_session_'.$current_session_id;
  217. $to_group_id = 0;
  218. $visibility = 0;
  219. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  220. }
  221. //Create dynamic user shared folder into a shared folder session
  222. if (!file_exists($base_work_dir.'/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id())) {
  223. $usf_dir_title = api_get_person_name($_user['firstName'], $_user['lastName']).' ('.api_get_session_name($current_session_id).')';
  224. $usf_dir_name = '/shared_folder_session_'.$current_session_id.'/sf_user_'.api_get_user_id();
  225. $to_group_id = 0;
  226. $visibility = 1;
  227. create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $usf_dir_name, $usf_dir_title, $visibility);
  228. }
  229. }
  230. /* MAIN SECTION */
  231. if (isset($_GET['action']) && $_GET['action'] == 'download') {
  232. $my_get_id = Security::remove_XSS($_GET['id']);
  233. // Check whether the document is in the database
  234. if (!DocumentManager::get_document_id($_course, $my_get_id)) {
  235. // File not found!
  236. header('HTTP/1.0 404 Not Found');
  237. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  238. $error404 .= '<html><head>';
  239. $error404 .= '<title>404 Not Found</title>';
  240. $error404 .= '</head><body>';
  241. $error404 .= '<h1>Not Found</h1>';
  242. $error404 .= '<p>The requested URL was not found on this server.</p>';
  243. $error404 .= '<hr>';
  244. $error404 .= '</body></html>';
  245. echo $error404;
  246. exit;
  247. }
  248. // Launch event
  249. event_download($my_get_id);
  250. // Check visibility of document and paths
  251. if (!($is_allowed_to_edit || $group_member_with_upload_rights) && !DocumentManager::is_visible($my_get_id, $_course)) {
  252. api_not_allowed();
  253. }
  254. $doc_url = $my_get_id;
  255. $full_file_name = $base_work_dir.$doc_url;
  256. DocumentManager::file_send_for_download($full_file_name, true);
  257. exit;
  258. }
  259. // Download a folder
  260. if (isset($_GET['action']) && $_GET['action'] == 'downloadfolder' && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  261. //filter when I am into shared folder, I can donwload only my shared folder
  262. if(is_any_user_shared_folder($_GET['path'],$current_session_id))
  263. {
  264. if(is_my_shared_folder($_user['user_id'], $_GET['path'], $current_session_id) || api_is_allowed_to_edit() || api_is_platform_admin())
  265. {
  266. require 'downloadfolder.inc.php';
  267. }
  268. }
  269. else
  270. {
  271. require 'downloadfolder.inc.php';
  272. }
  273. }
  274. // Slideshow inititalisation
  275. $_SESSION['image_files_only'] = '';
  276. $image_files_only = '';
  277. /* Header */
  278. if ($is_certificate_mode) {
  279. $interbreadcrumb[]= array('url' => '../gradebook/index.php', 'name' => get_lang('Gradebook'));
  280. } else {
  281. $interbreadcrumb[]= array('url' => '', 'name' => get_lang('Documents'));
  282. }
  283. // Interbreadcrumb for the current directory root path
  284. $dir_array = explode('/', $curdirpath);
  285. $array_len = count($dir_array);
  286. if (!$is_certificate_mode) {
  287. if ($array_len > 1) {
  288. if (empty($_SESSION['_gid'])) {
  289. $url_dir = 'document.php?&curdirpath=/';
  290. $interbreadcrumb[] = array('url' => $url_dir, 'name' => get_lang('HomeDirectory'));
  291. }
  292. }
  293. }
  294. $dir_acum = '';
  295. for ($i = 0; $i < $array_len; $i++) {
  296. $url_dir = 'document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  297. //Max char 80
  298. $url_to_who = cut($dir_array[$i],80);
  299. if ($is_certificate_mode) {
  300. $interbreadcrumb[] = array('url' => $url_dir.'&selectcat='.Security::remove_XSS($_GET['selectcat']), 'name' => $url_to_who);
  301. } else {
  302. $interbreadcrumb[] = array('url' => $url_dir, 'name' => $url_to_who);
  303. }
  304. $dir_acum .= $dir_array[$i].'/';
  305. }
  306. Display::display_header('','Doc');
  307. // Lib for event log, stats & tracking & record of the access
  308. event_access_tool(TOOL_DOCUMENT);
  309. /* DISPLAY */
  310. if ($to_group_id != 0) { // Add group name after for group documents
  311. $add_group_to_title = ' ('.$group_properties['name'].')';
  312. }
  313. /* Introduction section (editable by course admins) */
  314. if (!empty($_SESSION['_gid'])) {
  315. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  316. } else {
  317. Display::display_introduction_section(TOOL_DOCUMENT);
  318. }
  319. // Copy a file to general my files user's
  320. if (isset($_GET['action']) && $_GET['action'] == 'copytomyfiles' && api_get_setting('users_copy_files') == 'true' && api_get_user_id() != 0) {
  321. $clean_get_id = Security::remove_XSS($_GET['id']);
  322. $user_folder = api_get_path(SYS_CODE_PATH).'upload/users/'.api_get_user_id().'/my_files/';
  323. if (!file_exists($user_folder)) {
  324. @mkdir($user_folder, $permissions_for_new_directories, true);
  325. }
  326. $file = $sys_course_path.$_course['path'].'/document'.$clean_get_id;
  327. $copyfile = $user_folder.basename($clean_get_id);
  328. if (file_exists($copyfile)) {
  329. $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>';
  330. if (!isset($_GET['copy'])){
  331. Display::display_warning_message($message,false);
  332. }
  333. if (Security::remove_XSS($_GET['copy']) == 'yes'){
  334. if (!copy($file, $copyfile)) {
  335. Display::display_error_message(get_lang('CopyFailed'));
  336. }else{
  337. Display::display_confirmation_message(get_lang('OverwritenFile'));
  338. }
  339. }
  340. }else{
  341. if (!copy($file, $copyfile)) {
  342. Display::display_error_message(get_lang('CopyFailed'));
  343. }else{
  344. Display::display_confirmation_message(get_lang('CopyMade'));
  345. }
  346. }
  347. }
  348. if ($is_allowed_to_edit || $group_member_with_upload_rights) { // TEACHER ONLY
  349. /* MOVE FILE OR DIRECTORY */
  350. $my_get_move = Security::remove_XSS($_GET['move']);
  351. if (isset($_GET['move']) && $_GET['move'] != '') {
  352. if (!$is_allowed_to_edit) {
  353. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  354. api_not_allowed();
  355. }
  356. }
  357. if (DocumentManager::get_document_id($_course, $my_get_move)) {
  358. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  359. echo '<div class="row"><div class="form_header">'.get_lang('Move').'</div></div>';
  360. echo build_move_to_selector($folders, Security::remove_XSS($_GET['curdirpath']), $my_get_move, $group_properties['directory']);
  361. }
  362. }
  363. if (isset($_POST['move_to']) && isset($_POST['move_file'])) {
  364. if (!$is_allowed_to_edit) {
  365. if (DocumentManager::check_readonly($_course, $_user['user_id'], $my_get_move)) {
  366. api_not_allowed();
  367. }
  368. }
  369. require_once $lib_path.'fileManage.lib.php';
  370. // This is needed for the update_db_info function
  371. //$dbTable = $_course['dbNameGlu'].'document';
  372. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  373. // Security fix: make sure they can't move files that are not in the document table
  374. if (DocumentManager::get_document_id($_course, $_POST['move_file'])) {
  375. if (move($base_work_dir.$_POST['move_file'], $base_work_dir.$_POST['move_to'])) {
  376. update_db_info('update', $_POST['move_file'], $_POST['move_to'].'/'.basename($_POST['move_file']));
  377. // Set the current path
  378. $curdirpath = $_POST['move_to'];
  379. $curdirpathurl = urlencode($_POST['move_to']);
  380. Display::display_confirmation_message(get_lang('DirMv'));
  381. } else {
  382. Display::display_error_message(get_lang('Impossible'));
  383. }
  384. } else {
  385. Display::display_error_message(get_lang('Impossible'));
  386. }
  387. }
  388. /* DELETE FILE OR DIRECTORY */
  389. if (isset($_GET['delete'])) {
  390. if (!$is_allowed_to_edit) {
  391. if (DocumentManager::check_readonly($_course, $_user['user_id'], $_GET['delete'], '', true)) {
  392. api_not_allowed();
  393. }
  394. }
  395. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  396. if (DocumentManager::delete_document($_course, $_GET['delete'], $base_work_dir)) {
  397. if ( isset($_GET['delete_certificate_id']) && $_GET['delete_certificate_id'] == strval(intval($_GET['delete_certificate_id']))) {
  398. $course_id = api_get_course_id();
  399. $default_certificate_id = $_GET['delete_certificate_id'];
  400. DocumentManager::remove_attach_certificate($course_id, $default_certificate_id);
  401. }
  402. Display::display_confirmation_message(get_lang('DocDeleted'));
  403. } else {
  404. Display::display_error_message(get_lang('DocDeleteError'));
  405. }
  406. }
  407. if (isset($_POST['action'])) {
  408. switch ($_POST['action']) {
  409. case 'delete':
  410. foreach ($_POST['path'] as $index => & $path) {
  411. if (!$is_allowed_to_edit) {
  412. if (DocumentManager::check_readonly($_course, $_user['user_id'], $path)) {
  413. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  414. break 2;
  415. }
  416. }
  417. }
  418. foreach ($_POST['path'] as $index => & $path) {
  419. if (in_array($path, array('/audio', '/flash', '/images', '/shared_folder', '/video', '/chat_files', '/certificates'))) {
  420. continue;
  421. } else {
  422. $delete_document = DocumentManager::delete_document($_course, $path, $base_work_dir);
  423. }
  424. }
  425. if (!empty($delete_document)) {
  426. Display::display_confirmation_message(get_lang('DocDeleted'));
  427. }
  428. break;
  429. }
  430. }
  431. /* CREATE DIRECTORY */
  432. // Create directory with $_POST data
  433. if (isset($_POST['create_dir']) && $_POST['dirname'] != '') {
  434. // Needed for directory creation
  435. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  436. $post_dir_name = Security::remove_XSS($_POST['dirname']);
  437. if ($post_dir_name == '../' || $post_dir_name == '.' || $post_dir_name == '..') {
  438. Display::display_error_message(get_lang('CannotCreateDir'));
  439. } else {
  440. $added_slash = ($curdirpath == '/') ? '' : '/';
  441. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  442. $dir_name = disable_dangerous_file($dir_name);
  443. $dir_check = $base_work_dir.$dir_name;
  444. if (!is_dir($dir_check)) {
  445. $created_dir = create_unexisting_directory($_course, $_user['user_id'], $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name);
  446. if ($created_dir) {
  447. Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>', false);
  448. // Uncomment if you want to enter the created dir
  449. //$curdirpath = $created_dir;
  450. //$curdirpathurl = urlencode($curdirpath);
  451. } else {
  452. Display::display_error_message(get_lang('CannotCreateDir'));
  453. }
  454. } else {
  455. Display::display_error_message(get_lang('CannotCreateDir'));
  456. }
  457. }
  458. }
  459. // Show them the form for the directory name
  460. if (isset($_GET['createdir'])) {
  461. // Show the form
  462. echo create_dir_form();
  463. }
  464. /* VISIBILITY COMMANDS */
  465. if ((isset($_GET['set_invisible']) && !empty($_GET['set_invisible'])) || (isset($_GET['set_visible']) && !empty($_GET['set_visible'])) && $_GET['set_visible'] != '*' && $_GET['set_invisible'] != '*') {
  466. // Make visible or invisible?
  467. if (isset($_GET['set_visible'])) {
  468. $update_id = $_GET['set_visible'];
  469. $visibility_command = 'visible';
  470. } else {
  471. $update_id = $_GET['set_invisible'];
  472. $visibility_command = 'invisible';
  473. }
  474. if (!$is_allowed_to_edit) {
  475. if(DocumentManager::check_readonly($_course, $_user['user_id'], '', $update_id)) {
  476. api_not_allowed();
  477. }
  478. }
  479. // Update item_property to change visibility
  480. if (api_item_property_update($_course, TOOL_DOCUMENT, $update_id, $visibility_command, $_user['user_id'], null, null, null, null, $current_session_id)) {
  481. Display::display_confirmation_message(get_lang('ViMod'));
  482. } else {
  483. Display::display_error_message(get_lang('ViModProb'));
  484. }
  485. }
  486. /* TEMPLATE ACTION */
  487. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  488. $document_id_for_template = intval($_GET['add_as_template']);
  489. // Create the form that asks for the directory name
  490. $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">';
  491. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  492. $template_text .= '<table><tr><td>';
  493. $template_text .= get_lang('TemplateName').' : </td>';
  494. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  495. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  496. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  497. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  498. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  499. $template_text .= '</table>';
  500. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang('CreateTemplate').'</button>';
  501. $template_text .= '</form>';
  502. // Show the form
  503. Display::display_normal_message($template_text, false);
  504. } elseif (isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  505. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  506. $title = Security::remove_XSS($_POST['template_title']);
  507. //$description = Security::remove_XSS($_POST['template_description']);
  508. $course_code = api_get_course_id();
  509. $user_id = api_get_user_id();
  510. // Create the template_thumbnails folder in the upload folder (if needed)
  511. if (!is_dir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/')) {
  512. @mkdir(api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/', api_get_permissions_for_new_directories());
  513. }
  514. // Upload the file
  515. if (!empty($_FILES['template_image']['name'])) {
  516. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  517. $upload_ok = process_uploaded_file($_FILES['template_image']);
  518. if ($upload_ok) {
  519. // Try to add an extension to the file if it hasn't one
  520. $new_file_name = $_course['sysCode'].'-'.add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  521. // Upload dir
  522. $upload_dir = api_get_path(SYS_PATH).'courses/'.$_course['path'].'/upload/template_thumbnails/';
  523. // Resize image to max default and end upload
  524. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  525. $temp = new image($_FILES['template_image']['tmp_name']);
  526. $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
  527. $max_width_for_picture = 100;
  528. if ($picture_infos[0] > $max_width_for_picture) {
  529. $thumbwidth = $max_width_for_picture;
  530. if (empty($thumbwidth) || $thumbwidth == 0) {
  531. $thumbwidth = $max_width_for_picture;
  532. }
  533. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  534. $temp->resize($thumbwidth, $new_height, 0);
  535. }
  536. $type = $picture_infos[2];
  537. switch (!empty($type)) {
  538. case 2 : $temp->send_image('JPG', $upload_dir.$new_file_name);
  539. break;
  540. case 3 : $temp->send_image('PNG', $upload_dir.$new_file_name);
  541. break;
  542. case 1 : $temp->send_image('GIF', $upload_dir.$new_file_name);
  543. break;
  544. }
  545. }
  546. }
  547. DocumentManager::set_document_as_template($title, $description, $document_id_for_template, $course_code, $user_id, $new_file_name);
  548. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  549. }
  550. if (isset($_GET['remove_as_template'])) {
  551. $document_id_for_template = intval($_GET['remove_as_template']);
  552. $course_code = api_get_course_id();
  553. $user_id = api_get_user_id();
  554. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  555. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  556. }
  557. } // END is allowed to edit
  558. // Attach certificate in the gradebook
  559. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && isset($_GET['set_certificate']) && $_GET['set_certificate'] == strval(intval($_GET['set_certificate']))) {
  560. if (isset($_GET['cidReq'])) {
  561. $course_id = Security::remove_XSS($_GET['cidReq']); // course id
  562. $document_id = Security::remove_XSS($_GET['set_certificate']); // document id
  563. DocumentManager::attach_gradebook_certificate ($course_id,$document_id);
  564. Display::display_normal_message(get_lang('IsDefaultCertificate'));
  565. }
  566. }
  567. /* GET ALL DOCUMENT DATA FOR CURDIRPATH */
  568. if(isset($_GET['keyword']) && !empty($_GET['keyword'])){
  569. $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);
  570. }else{
  571. $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);
  572. }
  573. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit || $group_member_with_upload_rights);
  574. if ($folders === false) {
  575. $folders = array();
  576. }
  577. echo '<div class="actions">';
  578. if ($is_allowed_to_edit || $group_member_with_upload_rights){
  579. /* BUILD SEARCH FORM */
  580. echo '<span style="display:inline-block;">';
  581. $form = new FormValidator('search_document', 'get', '', '', null, false);
  582. $renderer = & $form->defaultRenderer();
  583. $renderer->setElementTemplate('<span>{element}</span> ');
  584. $form->add_textfield('keyword', '', false);
  585. $form->addElement('style_submit_button', 'submit', get_lang('Search'), 'class="search"');
  586. $form->display();
  587. echo '</span>';
  588. }
  589. /* GO TO PARENT DIRECTORY */
  590. if ($curdirpath!= '/' && $curdirpath != $group_properties['directory'] && !$is_certificate_mode) {
  591. ?>
  592. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath) == '\\') ? '/' : dirname($curdirpath)).$req_gid; ?>">
  593. <?php Display::display_icon('folder_up.gif', get_lang('Up')); echo get_lang('Up'); ?></a>&nbsp;
  594. <?php
  595. }
  596. if ($is_certificate_mode && $curdirpath != '/certificates') {
  597. ?>
  598. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath) == '\\') ? '/' : dirname($curdirpath)).$req_gid; ?>">
  599. <?php Display::display_icon('folder_up.gif', get_lang('Up')); echo get_lang('Up'); ?></a>&nbsp;
  600. <?php
  601. }
  602. if (isset($docs_and_folders) && is_array($docs_and_folders)) {
  603. //echo('<pre>');
  604. //print_r($docs_and_folders);
  605. //echo('</pre>');
  606. // Do we need the title field for the document name or not?
  607. // We get the setting here, so we only have to do it once
  608. $use_document_title = api_get_setting('use_document_title');
  609. // Create a sortable table with our data
  610. $sortable_data = array();
  611. while (list($key, $id) = each($docs_and_folders)) {
  612. $row = array();
  613. // If the item is invisible, wrap it in a span with class invisible
  614. $invisibility_span_open = ($id['visibility'] == 0) ? '<span class="invisible">' : '';
  615. $invisibility_span_close = ($id['visibility'] == 0) ? '</span>' : '';
  616. // Size (or total size of a directory)
  617. $size = $id['filetype'] == 'folder' ? get_total_folder_size($id['path'], $is_allowed_to_edit) : $id['size'];
  618. // Get the title or the basename depending on what we're using
  619. if ($use_document_title == 'true' && $id['title'] != '') {
  620. $document_name = $id['title'];
  621. } else {
  622. $document_name = basename($id['path']);
  623. }
  624. // Data for checkbox
  625. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  626. $row[] = $id['path'];
  627. }
  628. // Show the owner of the file only in groups
  629. $user_link = '';
  630. if (isset($_SESSION['_gid']) && $_SESSION['_gid'] != '') {
  631. if (!empty($id['insert_user_id'])) {
  632. $user_info = UserManager::get_user_info_by_id($id['insert_user_id']);
  633. $user_name = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  634. $user_link = '<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document($id['insert_user_id'], $user_name).'</div>';
  635. }
  636. }
  637. // Icons (clickable)
  638. //$row[]= build_document_icon_tag($id['filetype'],$id['path']);
  639. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility'], true);
  640. // Validacion when belongs to a session
  641. $session_img = api_get_session_image($id['session_id'], $_user['status']);
  642. // Document title with hyperlink
  643. $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;
  644. // Comments => display comment under the document name
  645. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  646. $display_size = format_file_size($size);
  647. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  648. // Last edit date
  649. $last_edit_date = $id['lastedit_date'];
  650. $last_edit_date = api_get_local_time($last_edit_date, null, date_default_timezone_get());
  651. $display_date = date_to_str_ago($last_edit_date).'<br /><span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
  652. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  653. // Admins get an edit column
  654. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  655. $is_template = isset($id['is_template']) ? $id['is_template'] : false;
  656. // If readonly, check if it the owner of the file or if the user is an admin
  657. if ($id['insert_user_id'] == $_user['user_id'] || api_is_platform_admin()) {
  658. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, 0);
  659. } else {
  660. $edit_icons = build_edit_icons($curdirpath, $id['filetype'], $id['path'], $id['visibility'], $key, $is_template, $id['readonly']);
  661. }
  662. $row[] = $edit_icons;
  663. }
  664. $row[] = $last_edit_date;
  665. $row[] = $size;
  666. $total_size = $total_size + $size;
  667. if ((isset ($_GET['keyword']) && search_keyword($document_name, $_GET['keyword'])) || !isset($_GET['keyword']) || empty($_GET['keyword'])) {
  668. $sortable_data[] = $row;
  669. }
  670. }
  671. } else {
  672. $sortable_data = '';
  673. $table_footer = '<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  674. }
  675. $column_show = array();
  676. if ($is_allowed_to_edit || $group_member_with_upload_rights || is_my_shared_folder($_user['user_id'], $curdirpath, $current_session_id)) {
  677. // TODO:check enable more options for shared folders
  678. /* CREATE NEW DOCUMENT OR NEW DIRECTORY / GO TO UPLOAD / DOWNLOAD ZIPPED FOLDER */
  679. // Create new document
  680. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $curdirpath, $current_session_id)) {
  681. ?>
  682. <a href="create_document.php?<?php echo api_get_cidreq(); ?>&dir=<?php echo $curdirpathurl.$req_gid; ?>">
  683. <?php Display::display_icon('filenew.gif', get_lang('CreateDoc')); echo get_lang('CreateDoc'); ?></a>&nbsp;
  684. <?php
  685. }
  686. // Create new certificate
  687. if ($is_certificate_mode) {
  688. ?>
  689. <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']); ?>">
  690. <?php Display::display_icon('filenew.gif', get_lang('CreateCertificate')); echo get_lang('CreateCertificate'); ?></a>&nbsp;
  691. <?php
  692. }
  693. // File upload link
  694. $upload_name = $is_certificate_mode ? get_lang('UploadCertificate') : get_lang('UplUploadDocument');
  695. ?>
  696. <a href="upload.php?<?php echo api_get_cidreq(); ?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>">
  697. <?php Display::display_icon('submit_file.gif', $upload_name); echo $upload_name; ?></a>&nbsp;
  698. <?php
  699. // Create directory
  700. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $curdirpath, $current_session_id)) {
  701. ?>
  702. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq(); ?>&curdirpath=<?php echo $curdirpathurl.$req_gid; ?>&amp;createdir=1">
  703. <?php Display::display_icon('folder_new.gif', get_lang('CreateDir')); echo get_lang('CreateDir'); ?></a>&nbsp;
  704. <a href="quota.php?<?php echo api_get_cidreq(); ?>">
  705. <?php Display::display_icon('statistics.gif', get_lang('ShowCourseQuotaUse')); echo get_lang('ShowCourseQuotaUse'); ?></a>&nbsp;
  706. <?php
  707. }
  708. }
  709. if (!is_null($docs_and_folders)) {
  710. // Show download zipped folder icon
  711. global $total_size;
  712. if (!$is_certificate_mode && $total_size != 0 && (api_get_setting('students_download_folders') == 'true' || api_is_allowed_to_edit() || api_is_platform_admin())) {
  713. //don't show icon into shared folder, and don´t show into main path (root)
  714. if (!is_shared_folder($curdirpath, $current_session_id) && $curdirpath!='/' || api_is_allowed_to_edit() || api_is_platform_admin())
  715. {
  716. 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';
  717. }
  718. }
  719. }
  720. // Slideshow by Patrick Cool, May 2004
  721. require 'document_slideshow.inc.php';
  722. if ($image_present && !isset($_GET['keyword']) ) {
  723. 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>';
  724. }
  725. echo '</div>';
  726. if (!$is_certificate_mode) {
  727. echo build_directory_selector($folders, $curdirpath, (isset($group_properties['directory']) ? $group_properties['directory'] : array()), true);
  728. }
  729. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  730. $column_show[] = 1;
  731. }
  732. $column_show[] = 1;
  733. $column_show[] = 1;
  734. $column_show[] = 1;
  735. $column_show[] = 1;
  736. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  737. $column_show[] = 1;
  738. }
  739. $column_show[] = 0;
  740. $column_order = array();
  741. if (count($row) == 8) {
  742. $column_order[3] = 7;
  743. $column_order[4] = 6;
  744. } elseif (count($row) == 6) {
  745. $column_order[2] = 5;
  746. $column_order[3] = 4;
  747. }
  748. $default_column = $is_allowed_to_edit ? 2 : 1;
  749. $tablename = $is_allowed_to_edit ? 'teacher_table' : 'student_table';
  750. $table = new SortableTableFromArrayConfig($sortable_data, $default_column, 20, $tablename, $column_show, $column_order, 'ASC');
  751. if(isset($_GET['keyword'])){
  752. $query_vars['keyword'] = Security::remove_XSS($_GET['keyword']);
  753. }else{
  754. $query_vars['curdirpath'] = $curdirpath;
  755. }
  756. if (isset($_SESSION['_gid'])) {
  757. $query_vars['gidReq'] = $_SESSION['_gid'];
  758. }
  759. $query_vars['cidReq'] = api_get_course_id();
  760. $table->set_additional_parameters($query_vars);
  761. $column = 0;
  762. if (($is_allowed_to_edit || $group_member_with_upload_rights) && count($docs_and_folders) > 1) {
  763. $table->set_header($column++, '', false,array ('style' => 'width:30px;'));
  764. }
  765. $table->set_header($column++, get_lang('Type'),true,array ('style' => 'width:30px;'));
  766. $table->set_header($column++, get_lang('Name'));
  767. //$column_header[] = array(get_lang('Comment'), true); // Display comment under the document name
  768. $table->set_header($column++, get_lang('Size'),true,array ('style' => 'width:50px;'));
  769. $table->set_header($column++, get_lang('Date'),true,array ('style' => 'width:150px;'));
  770. // Admins get an edit column
  771. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  772. $table->set_header($column++, get_lang('Modify'), false,array ('style' => 'width:150px;'));
  773. }
  774. // Actions on multiple selected documents
  775. // TODO: Currently only delete action -> take only DELETE right into account
  776. if (count($docs_and_folders) > 1) {
  777. if ($is_allowed_to_edit || $group_member_with_upload_rights) {
  778. $form_actions = array();
  779. $form_action['delete'] = get_lang('Delete');
  780. $table->set_form_actions($form_action, 'path');
  781. }
  782. }
  783. $table->display();
  784. if (!empty($table_footer)) {
  785. echo $table_footer;
  786. }
  787. // Footer
  788. Display::display_footer()
  789. ?>