document.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. // Is the document tool visible?
  80. // Check whether the tool is actually visible
  81. $table_course_tool = Database::get_course_table(TABLE_TOOL_LIST, $_course['dbName']);
  82. $tool_sql = 'SELECT visibility FROM ' . $table_course_tool . ' WHERE name = "'. TOOL_DOCUMENT .'" LIMIT 1';
  83. $tool_result = api_sql_query($tool_sql,__FILE__,__LINE__);
  84. $tool_row = Database::fetch_array($tool_result);
  85. $tool_visibility = $tool_row['visibility'];
  86. if ($tool_visibility == '0' && $to_group_id == '0' && !($is_allowed_to_edit || $group_member_with_upload_rights))
  87. {
  88. api_not_allowed(true);
  89. }
  90. $htmlHeadXtra[] =
  91. "<script type=\"text/javascript\">
  92. function confirmation (name)
  93. {
  94. if (confirm(\" ". get_lang("AreYouSureToDelete") ." \"+ name + \" ?\"))
  95. {return true;}
  96. else
  97. {return false;}
  98. }
  99. </script>";
  100. /*
  101. -----------------------------------------------------------
  102. Variables
  103. - some need defining before inclusion of libraries
  104. -----------------------------------------------------------
  105. */
  106. //what's the current path?
  107. //we will verify this a bit further down
  108. if(isset($_GET['curdirpath']) && $_GET['curdirpath']!='') {
  109. $curdirpath = Security::remove_XSS($_GET['curdirpath']);
  110. } elseif (isset($_POST['curdirpath']) && $_POST['curdirpath']!='') {
  111. $curdirpath = Security::remove_XSS($_POST['curdirpath']);
  112. } else {
  113. $curdirpath = '/';
  114. }
  115. $curdirpathurl = urlencode($curdirpath);
  116. $course_dir = $_course['path']."/document";
  117. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  118. $base_work_dir = $sys_course_path.$course_dir;
  119. $http_www = api_get_path('WEB_COURSE_PATH').$_course['path'].'/document';
  120. $dbl_click_id = 0; // used to avoid double-click
  121. $is_allowed_to_edit = api_is_allowed_to_edit();
  122. $group_member_with_upload_rights = false;
  123. //if the group id is set, we show them group documents
  124. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='')
  125. {
  126. //needed for group related stuff
  127. include_once(api_get_path(LIBRARY_PATH) . 'groupmanager.lib.php');
  128. //get group info
  129. $group_properties = GroupManager::get_group_properties($_SESSION['_gid']);
  130. $noPHP_SELF=true;
  131. //let's assume the user cannot upload files for the group
  132. $group_member_with_upload_rights = false;
  133. if($group_properties['doc_state']==2) //documents are private
  134. {
  135. if($is_allowed_to_edit || GroupManager :: is_user_in_group($_user['user_id'],$_SESSION['_gid'])) //only courseadmin or group members (members + tutors) allowed
  136. {
  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. }
  144. else
  145. {
  146. $to_group_id = 0;
  147. $req_gid = '';
  148. }
  149. }
  150. elseif($group_properties['doc_state']==1) //documents are public
  151. {
  152. $to_group_id = $_SESSION['_gid'];
  153. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  154. $interbreadcrumb[]= array ("url"=>"../group/group.php", "name"=> get_lang('Groups'));
  155. $interbreadcrumb[]= array ("url"=>"../group/group_space.php?gidReq=".$_SESSION['_gid'], "name"=> get_lang('GroupSpace').' ('.$group_properties['name'].')');
  156. //allowed to upload?
  157. if($is_allowed_to_edit || GroupManager::is_subscribed($_user['user_id'],$_SESSION['_gid'])) //only courseadmin or group members can upload
  158. {
  159. $group_member_with_upload_rights = true;
  160. }
  161. }
  162. else //documents not active for this group
  163. {
  164. $to_group_id = 0;
  165. $req_gid = '';
  166. }
  167. $_SESSION['group_member_with_upload_rights'] = $group_member_with_upload_rights;
  168. }
  169. else
  170. {
  171. $to_group_id = 0;
  172. $req_gid = '';
  173. }
  174. /*
  175. -----------------------------------------------------------
  176. Libraries
  177. -----------------------------------------------------------
  178. */
  179. //the main_api.lib.php, database.lib.php and display.lib.php
  180. //libraries are included by default
  181. require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php';
  182. require_once api_get_path(LIBRARY_PATH) . 'document.lib.php';
  183. require_once api_get_path(LIBRARY_PATH) . 'tablesort.lib.php';
  184. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  185. //-----------------------------------------------------------
  186. //check the path
  187. //if the path is not found (no document id), set the path to /
  188. if(!DocumentManager::get_document_id($_course,$curdirpath))
  189. {
  190. $curdirpath = '/';
  191. //urlencoded version
  192. $curdirpathurl = '%2F';
  193. }
  194. //if they are looking at group documents they can't see the root
  195. if($to_group_id!=0 && $curdirpath=='/')
  196. {
  197. $curdirpath = $group_properties['directory'];
  198. $curdirpathurl = urlencode($group_properties['directory']);
  199. }
  200. //-----------------------------------------------------------
  201. // check visibility of the current dir path. Don't show anything if not allowed
  202. if (!(DocumentManager::is_visible($curdirpath, $_course)||$is_allowed_to_edit)){
  203. api_not_allowed();
  204. }
  205. /*
  206. -----------------------------------------------------------
  207. Constants and variables
  208. -----------------------------------------------------------
  209. */
  210. $course_quota = DocumentManager::get_course_quota();
  211. /*
  212. ==============================================================================
  213. MAIN SECTION
  214. ==============================================================================
  215. */
  216. //-------------------------------------------------------------------//
  217. if (isset($_GET['action']) && $_GET['action']=="download")
  218. {
  219. $my_get_id=Security::remove_XSS($_GET['id']);
  220. //check if the document is in the database
  221. if(!DocumentManager::get_document_id($_course,$my_get_id))
  222. {
  223. //file not found!
  224. header('HTTP/1.0 404 Not Found');
  225. $error404 = '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">';
  226. $error404 .= '<html><head>';
  227. $error404 .= '<title>404 Not Found</title>';
  228. $error404 .= '</head><body>';
  229. $error404 .= '<h1>Not Found</h1>';
  230. $error404 .= '<p>The requested URL was not found on this server.</p>';
  231. $error404 .= '<hr>';
  232. $error404 .= '</body></html>';
  233. echo($error404);
  234. exit;
  235. }
  236. // launch event
  237. event_download($my_get_id);
  238. // check visibility of document and paths
  239. if (!($is_allowed_to_edit || $group_member_with_upload_rights) &&
  240. !DocumentManager::is_visible($my_get_id, $_course)){
  241. api_not_allowed();
  242. }
  243. $doc_url=$my_get_id;
  244. $full_file_name = $base_work_dir.$doc_url;
  245. DocumentManager::file_send_for_download($full_file_name,true);
  246. exit;
  247. }
  248. //-------------------------------------------------------------------//
  249. //download of an completed folder
  250. if(isset($_GET['action']) && $_GET['action']=="downloadfolder")
  251. {
  252. include('downloadfolder.inc.php');
  253. }
  254. //-------------------------------------------------------------------//
  255. // slideshow inititalisation
  256. $_SESSION['image_files_only']='';
  257. $image_files_only='';
  258. /*
  259. -----------------------------------------------------------
  260. Header
  261. -----------------------------------------------------------
  262. */
  263. $interbreadcrumb[]= array ('url'=>'', 'name'=> get_lang('Document'));
  264. //------interbreadcrumb for the current directory root path
  265. $dir_array=explode("/",$curdirpath);
  266. $array_len=count($dir_array);
  267. if ($array_len >1)
  268. {
  269. if(empty($_SESSION['_gid']))
  270. {
  271. $url_dir='document.php?&curdirpath=/';
  272. $interbreadcrumb[]= array ('url'=>$url_dir, 'name'=> get_lang('HomeDirectory'));
  273. }
  274. }
  275. $dir_acum='';
  276. for ($i=0; $i<$array_len;$i++)
  277. {
  278. if($dir_array[$i] =='shared_folder')
  279. {
  280. $dir_array[$i]=get_lang('SharedFolder');
  281. }
  282. elseif(strstr($dir_array[$i], 'sf_user_'))
  283. {
  284. $userinfo=Database::get_user_info_from_id(substr($dir_array[$i], 8));
  285. $dir_array[$i]=api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  286. }
  287. $url_dir='document.php?&curdirpath='.$dir_acum.$dir_array[$i];
  288. $interbreadcrumb[]= array ('url'=>$url_dir, 'name'=> $dir_array[$i]);
  289. $dir_acum.=$dir_array[$i].'/';
  290. }
  291. Display::display_header('','Doc');
  292. $is_allowed_to_edit = api_is_allowed_to_edit();
  293. /*
  294. * Lib for event log, stats & tracking
  295. * plus record of the access
  296. */
  297. event_access_tool(TOOL_DOCUMENT);
  298. /*
  299. ==============================================================================
  300. DISPLAY
  301. ==============================================================================
  302. */
  303. if($to_group_id !=0) //add group name after for group documents
  304. {
  305. $add_group_to_title = ' ('.$group_properties['name'].')';
  306. }
  307. //api_display_tool_title($tool_name.$add_group_to_title);
  308. /*
  309. -----------------------------------------------------------
  310. Introduction section
  311. (editable by course admins)
  312. -----------------------------------------------------------
  313. */
  314. if(!empty($_SESSION['_gid']))
  315. {
  316. Display::display_introduction_section(TOOL_DOCUMENT.$_SESSION['_gid']);
  317. }
  318. else
  319. {
  320. Display::display_introduction_section(TOOL_DOCUMENT);
  321. }
  322. /*============================================================================*/
  323. if($is_allowed_to_edit || $group_member_with_upload_rights) // TEACHER ONLY
  324. {
  325. /*======================================
  326. MOVE FILE OR DIRECTORY
  327. ======================================*/
  328. $my_get_move=Security::remove_XSS($_GET['move']);
  329. if (isset($_GET['move']) && $_GET['move']!='')
  330. {
  331. if (!$is_allowed_to_edit)
  332. {
  333. if(DocumentManager::check_readonly($_course,$_user['user_id'],$my_get_move))
  334. {
  335. api_not_allowed();
  336. }
  337. }
  338. if(DocumentManager::get_document_id($_course,$my_get_move))
  339. {
  340. $folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit || $group_member_with_upload_rights);
  341. echo '<div class="row"><div class="form_header">'.get_lang('Move').'</div></div>';
  342. echo build_move_to_selector($folders,Security::remove_XSS($_GET['curdirpath']),$my_get_move,$group_properties['directory']);
  343. }
  344. }
  345. if (isset($_POST['move_to']) && isset($_POST['move_file']))
  346. {
  347. if (!$is_allowed_to_edit)
  348. {
  349. if(DocumentManager::check_readonly($_course,$_user['user_id'],$my_get_move))
  350. {
  351. api_not_allowed();
  352. }
  353. }
  354. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  355. //this is needed for the update_db_info function
  356. //$dbTable = $_course['dbNameGlu']."document";
  357. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  358. //security fix: make sure they can't move files that are not in the document table
  359. if(DocumentManager::get_document_id($_course,$_POST['move_file']))
  360. {
  361. if ( move($base_work_dir.$_POST['move_file'],$base_work_dir.$_POST['move_to']) )
  362. {
  363. update_db_info("update", $_POST['move_file'], $_POST['move_to']."/".basename($_POST['move_file']));
  364. //set the current path
  365. $curdirpath = $_POST['move_to'];
  366. $curdirpathurl = urlencode($_POST['move_to']);
  367. Display::display_confirmation_message(get_lang('DirMv'));
  368. }
  369. else
  370. {
  371. Display::display_error_message(get_lang('Impossible'));
  372. }
  373. }
  374. else
  375. {
  376. Display::display_error_message(get_lang('Impossible'));
  377. }
  378. }
  379. /*======================================
  380. DELETE FILE OR DIRECTORY
  381. ======================================*/
  382. if ( isset($_GET['delete']) )
  383. {
  384. if (!$is_allowed_to_edit)
  385. {
  386. if(DocumentManager::check_readonly($_course,$_user['user_id'],$_GET['delete'],'',true))
  387. {
  388. api_not_allowed();
  389. }
  390. }
  391. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  392. if(DocumentManager::delete_document($_course,$_GET['delete'],$base_work_dir))
  393. {
  394. Display::display_confirmation_message(get_lang('DocDeleted'));
  395. }
  396. else
  397. {
  398. Display::display_error_message(get_lang('DocDeleteError'));
  399. }
  400. }
  401. if( isset($_POST['action']))
  402. {
  403. switch($_POST['action'])
  404. {
  405. case 'delete':
  406. foreach($_POST['path'] as $index => $path)
  407. {
  408. if (!$is_allowed_to_edit)
  409. {
  410. if(DocumentManager::check_readonly($_course,$_user['user_id'],$path))
  411. {
  412. Display::display_error_message(get_lang('CantDeleteReadonlyFiles'));
  413. break 2;
  414. }
  415. }
  416. }
  417. foreach($_POST['path'] as $index => $path)
  418. {
  419. 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) {
  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. /*======================================
  432. CREATE DIRECTORY
  433. ======================================*/
  434. //create directory with $_POST data
  435. if(isset($_POST['create_dir']) && $_POST['dirname']!='')
  436. {
  437. //needed for directory creation
  438. include_once(api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php');
  439. $post_dir_name=Security::remove_XSS($_POST['dirname']);
  440. if ($post_dir_name=='../' || $post_dir_name=='.' || $post_dir_name=='..')
  441. {
  442. Display::display_error_message(get_lang('CannotCreateDir'));
  443. }
  444. else
  445. {
  446. $added_slash = ($curdirpath=='/')?'':'/';
  447. $dir_name = $curdirpath.$added_slash.replace_dangerous_char($post_dir_name);
  448. $clean_val=disable_dangerous_file($dir_name);
  449. $clean_val=replace_accents($dir_name);
  450. $dir_name=$clean_val;
  451. $dir_check=$base_work_dir.''.$dir_name;
  452. if(!is_dir($dir_check))
  453. {
  454. $created_dir = create_unexisting_directory($_course,$_user['user_id'],$to_group_id,$to_user_id,$base_work_dir,$dir_name,$post_dir_name);
  455. if($created_dir)
  456. {
  457. Display::display_confirmation_message('<span title="'.$created_dir.'">'.get_lang('DirCr').'</span>',false);
  458. //uncomment if you want to enter the created dir
  459. //$curdirpath = $created_dir;
  460. //$curdirpathurl = urlencode($curdirpath);
  461. }
  462. else
  463. {
  464. Display::display_error_message(get_lang('CannotCreateDir'));
  465. }
  466. }
  467. else
  468. {
  469. Display::display_error_message(get_lang('CannotCreateDir'));
  470. }
  471. }
  472. }
  473. //show them the form for the directory name
  474. if(isset($_GET['createdir']))
  475. {
  476. //show the form
  477. echo create_dir_form();
  478. }
  479. /*======================================
  480. VISIBILITY COMMANDS
  481. ======================================*/
  482. 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']<>'*')
  483. {
  484. //make visible or invisible?
  485. if(isset($_GET['set_visible']))
  486. {
  487. $update_id = $_GET['set_visible'];
  488. $visibility_command = 'visible';
  489. }
  490. else
  491. {
  492. $update_id = $_GET['set_invisible'];
  493. $visibility_command = 'invisible';
  494. }
  495. if (!$is_allowed_to_edit)
  496. {
  497. if(DocumentManager::check_readonly($_course,$_user['user_id'],'',$update_id))
  498. {
  499. api_not_allowed();
  500. }
  501. }
  502. //update item_property to change visibility
  503. if(api_item_property_update($_course, TOOL_DOCUMENT, $update_id, $visibility_command, $_user['user_id']))
  504. {
  505. Display::display_confirmation_message(get_lang("ViMod"));
  506. }
  507. else
  508. {
  509. Display::display_error_message(get_lang("ViModProb"));
  510. }
  511. }
  512. /*======================================
  513. TEMPLATE ACTION
  514. ======================================*/
  515. if (isset($_GET['add_as_template']) && !isset($_POST['create_template'])) {
  516. $document_id_for_template = intval($_GET['add_as_template']);
  517. //create the form that asks for the directory name
  518. $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">';
  519. $template_text .= '<input type="hidden" name="curdirpath" value="'.$curdirpath.'" />';
  520. $template_text .= '<table><tr><td>';
  521. $template_text .= get_lang('TemplateName').' : </td>';
  522. $template_text .= '<td><input type="text" name="template_title" /></td></tr>';
  523. //$template_text .= '<tr><td>'.get_lang('TemplateDescription').' : </td>';
  524. //$template_text .= '<td><textarea name="template_description"></textarea></td></tr>';
  525. $template_text .= '<tr><td>'.get_lang('TemplateImage').' : </td>';
  526. $template_text .= '<td><input type="file" name="template_image" id="template_image" /></td></tr>';
  527. $template_text .= '</table>';
  528. $template_text .= '<button type="submit" class="add" name="create_template">'.get_lang('CreateTemplate').'</button>';
  529. $template_text .= '</form>';
  530. //show the form
  531. Display::display_normal_message($template_text,false);
  532. } elseif(isset($_GET['add_as_template']) && isset($_POST['create_template'])) {
  533. $document_id_for_template = intval(Database::escape_string($_GET['add_as_template']));
  534. $title=Security::remove_XSS($_POST['template_title']);
  535. //$description = Security::remove_XSS($_POST['template_description']);
  536. $course_code = api_get_course_id();
  537. $user_id = api_get_user_id();
  538. // create the template_thumbnails folder in the upload folder (if needed)
  539. if (!is_dir(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/')) {
  540. $perm = api_get_setting('permissions_for_new_directories');
  541. $perm = octdec(!empty($perm)?$perm:'0770');
  542. $res = @mkdir(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/',$perm);
  543. }
  544. // upload the file
  545. if (!empty($_FILES['template_image']['name'])) {
  546. echo 'uploading';
  547. include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  548. $upload_ok = process_uploaded_file($_FILES['template_image']);
  549. if ($upload_ok) {
  550. // Try to add an extension to the file if it hasn't one
  551. $new_file_name = $_course['sysCode'].'-'.add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  552. // upload dir
  553. $upload_dir = api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/';
  554. // resize image to max default and end upload
  555. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  556. $temp = new image($_FILES['template_image']['tmp_name']);
  557. $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
  558. $max_width_for_picture = 100;
  559. if ($picture_infos[0]>$max_width_for_picture) {
  560. $thumbwidth = $max_width_for_picture;
  561. if (empty($thumbwidth) or $thumbwidth==0) {
  562. $thumbwidth=$max_width_for_picture;
  563. }
  564. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  565. $temp->resize($thumbwidth,$new_height,0);
  566. }
  567. $type=$picture_infos[2];
  568. switch (!empty($type)) {
  569. case 2 : $temp->send_image('JPG',$upload_dir.$new_file_name);
  570. break;
  571. case 3 : $temp->send_image('PNG',$upload_dir.$new_file_name);
  572. break;
  573. case 1 : $temp->send_image('GIF',$upload_dir.$new_file_name);
  574. break;
  575. }
  576. }
  577. }
  578. DocumentManager::set_document_as_template($title, $description, $document_id_for_template, $course_code, $user_id, $new_file_name);
  579. Display::display_confirmation_message(get_lang('DocumentSetAsTemplate'));
  580. }
  581. if(isset($_GET['remove_as_template'])) {
  582. $document_id_for_template = intval($_GET['remove_as_template']);
  583. $course_code = api_get_course_id();
  584. $user_id = api_get_user_id();
  585. DocumentManager::unset_document_as_template($document_id_for_template, $course_code, $user_id);
  586. Display::display_confirmation_message(get_lang('DocumentUnsetAsTemplate'));
  587. }
  588. } // END is allowed to edit
  589. /*
  590. -----------------------------------------------------------
  591. GET ALL DOCUMENT DATA FOR CURDIRPATH
  592. -----------------------------------------------------------
  593. */
  594. $docs_and_folders = DocumentManager::get_all_document_data($_course,$curdirpath,$to_group_id,NULL,$is_allowed_to_edit || $group_member_with_upload_rights);
  595. ?>
  596. <?php
  597. $folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit || $group_member_with_upload_rights);
  598. if($folders===false)
  599. {
  600. $folders = array();
  601. }
  602. ?>
  603. <?php
  604. echo '<div class="actions">';
  605. /* GO TO PARENT DIRECTORY */
  606. if ($curdirpath!= '/'&& $curdirpath!=$group_properties['directory'])
  607. {
  608. ?>
  609. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&curdirpath=<?php echo urlencode((dirname($curdirpath)=='\\')?'/':dirname($curdirpath)).$req_gid ?>">
  610. <img src="../img/folder_up.gif" border="0" title="<?php echo get_lang('Up'); ?>" alt="" />
  611. <?php echo get_lang('Up'); ?></a>&nbsp;
  612. <?php
  613. }
  614. if(isset($docs_and_folders) && is_array($docs_and_folders))
  615. {
  616. //echo('<pre>');
  617. //print_r($docs_and_folders);
  618. //echo('</pre>');
  619. //*************************************************************************************************
  620. //do we need the title field for the document name or not?
  621. //we get the setting here, so we only have to do it once
  622. $use_document_title = api_get_setting('use_document_title');
  623. //create a sortable table with our data
  624. $sortable_data = array();
  625. while (list ($key, $id) = each($docs_and_folders))
  626. {
  627. $row = array ();
  628. //if the item is invisible, wrap it in a span with class invisible
  629. $invisibility_span_open = ($id['visibility']==0)?'<span class="invisible">':'';
  630. $invisibility_span_close = ($id['visibility']==0)?'</span>':'';
  631. //size (or total size of a directory)
  632. $size = $id['filetype']=='folder' ? get_total_folder_size($id['path'],$is_allowed_to_edit) : $id['size'];
  633. //get the title or the basename depending on what we're using
  634. if ($use_document_title=='true' AND $id['title']<>'') {
  635. $document_name=$id['title'];
  636. } else {
  637. $document_name=basename($id['path']);
  638. //Juan Carlos Raña: Get firstname and lastname when folder is in shared_folder.
  639. //TODO: check if is also necessary (above else)
  640. if(strstr($document_name, 'sf_user_'))
  641. {
  642. $userinfo=Database::get_user_info_from_id(substr($document_name, 8));
  643. $document_name=api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  644. }
  645. elseif(strstr($document_name, 'shared_folder'))
  646. {
  647. $document_name=get_lang('SharedFolder');
  648. }
  649. }
  650. //data for checkbox
  651. if (($is_allowed_to_edit || $group_member_with_upload_rights) AND count($docs_and_folders)>1) {
  652. $row[] = $id['path'];
  653. }
  654. // Show the Owner of the file only in groups
  655. $user_link='';
  656. if(isset($_SESSION['_gid']) && $_SESSION['_gid']!='') {
  657. if (!empty($id['insert_user_id'])) {
  658. $user_info=UserManager::get_user_info_by_id($id['insert_user_id']);
  659. $user_name=api_get_person_name($user_info['firstname'], $user_info['lastname']);
  660. $user_link='<div class="document_owner">'.get_lang('Owner').': '.display_user_link_document($id['insert_user_id'],$user_name).'</div>';
  661. }
  662. }
  663. //icons (clickable)
  664. //$row[]= build_document_icon_tag($id['filetype'],$id['path']);
  665. $row[] = create_document_link($http_www, $document_name, $id['path'], $id['filetype'], $size, $id['visibility'], true);
  666. //document title with hyperlink
  667. $row[] = create_document_link($http_www,$document_name,$id['path'],$id['filetype'],$size,$id['visibility']).'<br />'.$invisibility_span_open.nl2br(htmlspecialchars($id['comment'],ENT_QUOTES,$charset)).$invisibility_span_close.$user_link;
  668. //comments => display comment under the document name
  669. //$row[] = $invisibility_span_open.nl2br(htmlspecialchars($id['comment'])).$invisibility_span_close;
  670. $display_size = format_file_size($size);
  671. $row[] = '<span style="display:none;">'.$size.'</span>'.$invisibility_span_open.$display_size.$invisibility_span_close;
  672. //last edit date
  673. $last_edit_date=$id['lastedit_date'];
  674. $display_date = date_to_str_ago($last_edit_date).'<br><span class="dropbox_date">'.$last_edit_date.'</span>';
  675. $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;
  676. //admins get an edit column
  677. if ($is_allowed_to_edit || $group_member_with_upload_rights)
  678. {
  679. $is_template = (isset($id['is_template'])?$id['is_template']:false);
  680. // if readonly, check if it the owner of the file or if the user is an admin
  681. if ($id['insert_user_id'] == $_user['user_id'] || api_is_platform_admin())
  682. {
  683. $edit_icons = build_edit_icons($curdirpath,$id['filetype'],$id['path'],$id['visibility'],$key, $is_template,0);
  684. }
  685. else
  686. {
  687. $edit_icons = build_edit_icons($curdirpath,$id['filetype'],$id['path'],$id['visibility'],$key, $is_template,$id['readonly']);
  688. }
  689. $row[] = $edit_icons;
  690. }
  691. $row[] = $last_edit_date;
  692. $row[] = $size;
  693. $total_size=$total_size+$size;
  694. $sortable_data[] = $row;
  695. }
  696. //*******************************************************************************************
  697. }
  698. else
  699. {
  700. $sortable_data='';
  701. $table_footer='<div style="text-align:center;"><strong>'.get_lang('NoDocsInFolder').'</strong></div>';
  702. }
  703. $column_show=array();
  704. if ($is_allowed_to_edit || $group_member_with_upload_rights)
  705. {
  706. /* CREATE NEW DOCUMENT OR NEW DIRECTORY / GO TO UPLOAD / DOWNLOAD ZIPPED FOLDER */
  707. ?>
  708. <!-- create new document or directory -->
  709. <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>
  710. <a href="create_document.php?<?php echo api_get_cidreq();?>&dir=<?php echo $curdirpathurl.$req_gid; ?>"><?php echo get_lang("CreateDoc"); ?></a>&nbsp;&nbsp;
  711. <!-- file upload link -->
  712. <a href="upload.php?<?php echo api_get_cidreq();?>&path=<?php echo $curdirpathurl.$req_gid; ?>"><img src="../img/submit_file.gif" border="0" title="<?php echo get_lang('UplUploadDocument'); ?>" alt="" /></a>
  713. <a href="upload.php?<?php echo api_get_cidreq();?>&path=<?php echo $curdirpathurl.$req_gid; ?>"><?php echo get_lang('UplUploadDocument'); ?></a>&nbsp;
  714. <!-- create directory -->
  715. <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>
  716. <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;
  717. <a href="quota.php?<?php echo api_get_cidreq();?>"><?php Display::display_icon('statistics.gif', get_lang("ShowCourseQuotaUse")); ?><?php echo get_lang("ShowCourseQuotaUse"); ?></a>
  718. <?php
  719. }
  720. if ($docs_and_folders!=null) {
  721. global $total_size;
  722. if ($total_size!=0){ ?>
  723. <!-- download zipped folder -->
  724. <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>
  725. <a href="<?php echo api_get_self(); ?>?<?php echo api_get_cidreq();?>&action=downloadfolder"><?php echo get_lang("Save"); ?> (ZIP)</a>&nbsp;
  726. <?php
  727. }
  728. }
  729. // Slideshow by Patrick Cool, May 2004
  730. include("document_slideshow.inc.php");
  731. if ($image_present)
  732. {
  733. 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>";
  734. }
  735. echo "</div>";
  736. echo(build_directory_selector($folders,$curdirpath,(isset($group_properties['directory'])?$group_properties['directory']:array()),true));
  737. //==============================================================================
  738. if (($is_allowed_to_edit || $group_member_with_upload_rights) AND count($docs_and_folders)>1)
  739. {
  740. $column_show[]=1;
  741. }
  742. $column_show[]=1;
  743. $column_show[]=1;
  744. $column_show[]=1;
  745. $column_show[]=1;
  746. if ($is_allowed_to_edit || $group_member_with_upload_rights)
  747. {
  748. $column_show[]=1;
  749. }
  750. $column_show[]=0;
  751. $column_order=array();
  752. if ( count($row) == 8 )
  753. {
  754. $column_order[]=1;
  755. $column_order[]=2;
  756. $column_order[]=7;
  757. $column_order[]=6;
  758. $column_order[]=5;
  759. } else if ( count($row) == 6 ) {
  760. $column_order[]=1;
  761. $column_order[]=2;
  762. $column_order[]=7;
  763. $column_order[]=4;
  764. }
  765. $default_column = $is_allowed_to_edit ? 2 : 1;
  766. $tablename = $is_allowed_to_edit ? 'teacher_table' : 'student_table';
  767. $table = new SortableTableFromArrayConfig($sortable_data,$default_column,20,$tablename,$column_show,$column_order,'ASC');
  768. $query_vars['curdirpath'] = $curdirpath;
  769. if(isset($_SESSION['_gid']))
  770. {
  771. $query_vars['gidReq'] = $_SESSION['_gid'];
  772. }
  773. $query_vars['cidReq'] = api_get_course_id();
  774. $table->set_additional_parameters($query_vars);
  775. $column = 0;
  776. if (($is_allowed_to_edit || $group_member_with_upload_rights) AND count($docs_and_folders)>1)
  777. {
  778. $table->set_header($column++,'',false);
  779. }
  780. $table->set_header($column++,get_lang('Type'));
  781. $table->set_header($column++,get_lang('Name'));
  782. //$column_header[] = array(get_lang('Comment'),true); => display comment under the document name
  783. $table->set_header($column++,get_lang('Size'));
  784. $table->set_header($column++,get_lang('Date'));
  785. //admins get an edit column
  786. if ($is_allowed_to_edit || $group_member_with_upload_rights)
  787. {
  788. $table->set_header($column++,get_lang('Modify'),false);
  789. }
  790. //actions on multiple selected documents
  791. //currently only delete action -> take only DELETE right into account
  792. if (count($docs_and_folders)>1)
  793. {
  794. if ($is_allowed_to_edit || $group_member_with_upload_rights)
  795. {
  796. $form_actions = array();
  797. $form_action['delete'] = get_lang('Delete');
  798. $table->set_form_actions($form_action,'path');
  799. }
  800. }
  801. $table->display();
  802. if(!empty($table_footer))
  803. {
  804. echo $table_footer;
  805. }
  806. // footer
  807. Display::display_footer();
  808. ?>