document.php 33 KB

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