document.php 32 KB

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