document.php 31 KB

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