document.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php // $Id: document.inc.php 22074 2009-07-14 16:28:37Z jhp1411 $
  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. EXTRA FUNCTIONS FOR DOCUMENT.PHP/UPLOAD.PHP
  23. ==============================================================================
  24. /////////////////////////////////////////////////
  25. //--> leave these here or move them elsewhere? //
  26. /////////////////////////////////////////////////
  27. */
  28. /**
  29. * Builds the form thats enables the user to
  30. * select a directory to browse/upload in
  31. *
  32. * @param array An array containing the folders we want to be able to select
  33. * @param string The current folder (path inside of the "document" directory, including the prefix "/")
  34. * @param string Group directory, if empty, prevents documents to be uploaded (because group documents cannot be uploaded in root)
  35. * @param boolean Whether to change the renderer (this will add a template <span> to the QuickForm object displaying the form)
  36. * @return string html form
  37. */
  38. function build_directory_selector($folders,$curdirpath,$group_dir='',$changeRenderer=false)
  39. {
  40. $folder_titles = array();
  41. if(api_get_setting('use_document_title') == 'true')
  42. {
  43. if (is_array($folders))
  44. {
  45. $escaped_folders = array();
  46. foreach($folders as $key=>$val){$escaped_folders[$key] = Database::escape_string($val);}
  47. $folder_sql = implode("','",$escaped_folders);
  48. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  49. $sql = "SELECT * FROM $doc_table WHERE filetype='folder' AND path IN ('".$folder_sql."')";
  50. $res = Database::query($sql,__FILE__,__LINE__);
  51. $folder_titles = array();
  52. while($obj = Database::fetch_object($res))
  53. {
  54. $folder_titles[$obj->path] = $obj->title;
  55. }
  56. }
  57. }
  58. else
  59. {
  60. if (is_array($folders)){
  61. foreach($folders as $folder)
  62. {
  63. $folder_titles[$folder] = basename($folder);
  64. }
  65. }
  66. }
  67. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  68. $form = new FormValidator('selector','POST',api_get_self());
  69. $parent_select = $form->addElement('select', 'curdirpath', get_lang('CurrentDirectory'),'','onchange="javascript:document.selector.submit()"');
  70. if($changeRenderer==true){
  71. $renderer = $form->defaultRenderer();
  72. $renderer->setElementTemplate('<span>{label} : {element}</span> ','curdirpath');
  73. }
  74. //group documents cannot be uploaded in the root
  75. if(empty($group_dir))
  76. {
  77. $parent_select -> addOption(get_lang('HomeDirectory'),'/');
  78. if(is_array($folders))
  79. {
  80. foreach ($folders as $folder)
  81. {
  82. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  83. $path_parts = explode('/',$folder);
  84. if($folder_titles[$folder]=='shared_folder')
  85. {
  86. $folder_titles[$folder]=get_lang('SharedFolder');
  87. }
  88. elseif(strstr($folder_titles[$folder], 'sf_user_'))
  89. {
  90. $userinfo=Database::get_user_info_from_id(substr($folder_titles[$folder],8));
  91. $folder_titles[$folder]=api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  92. }
  93. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$folder_titles[$folder];
  94. $parent_select -> addOption($label,$folder);
  95. if($selected!='') $parent_select->setSelected($folder);
  96. }
  97. }
  98. }
  99. else
  100. {
  101. foreach ($folders as $folder)
  102. {
  103. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  104. $label = $folder_titles[$folder];
  105. if( $folder == $group_dir)
  106. {
  107. $label = '/ ('.get_lang('HomeDirectory').')';
  108. }
  109. else
  110. {
  111. $path_parts = explode('/',str_replace($group_dir,'',$folder));
  112. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$label;
  113. }
  114. $parent_select -> addOption($label,$folder);
  115. if($selected!='') $parent_select->setSelected($folder);
  116. }
  117. }
  118. $form=$form->toHtml();
  119. return $form;
  120. }
  121. /**
  122. * Create a html hyperlink depending on if it's a folder or a file
  123. *
  124. * @param string $www
  125. * @param string $title
  126. * @param string $path
  127. * @param string $filetype (file/folder)
  128. * @param int $visibility (1/0)
  129. * @param int $show_as_icon - if it is true, only a clickable icon will be shown
  130. * @return string url
  131. */
  132. function create_document_link($www, $title, $path, $filetype, $size, $visibility, $show_as_icon = false)
  133. {
  134. global $dbl_click_id;
  135. if(isset($_SESSION['_gid']))
  136. {
  137. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  138. }
  139. else
  140. {
  141. $req_gid = '';
  142. }
  143. $url_path = urlencode($path);
  144. //add class="invisible" on invisible files
  145. $visibility_class= ($visibility==0)?' class="invisible"':'';
  146. if (!$show_as_icon)
  147. {
  148. //build download link (icon)
  149. $forcedownload_link=($filetype=='folder')?api_get_self().'?'.api_get_cidreq().'&action=downloadfolder&amp;path='.$url_path.$req_gid:api_get_self().'?'.api_get_cidreq().'&amp;action=download&amp;id='.$url_path.$req_gid;
  150. //folder download or file download?
  151. $forcedownload_icon=($filetype=='folder')?'folder_zip.gif':'filesave.gif';
  152. //prevent multiple clicks on zipped folder download
  153. $prevent_multiple_click =($filetype=='folder')?" onclick=\"javascript:if(typeof clic_$dbl_click_id == 'undefined' || clic_$dbl_click_id == false) { clic_$dbl_click_id=true; window.setTimeout('clic_".($dbl_click_id++)."=false;',10000); } else { return false; }\"":'';
  154. }
  155. $target='_self';
  156. if($filetype=='file') {
  157. //check the extension
  158. $ext=explode('.',$path);
  159. $ext=strtolower($ext[sizeof($ext)-1]);
  160. //"htmlfiles" are shown in a frameset
  161. if($ext == 'htm' || $ext == 'html' || $ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png')
  162. {
  163. $url = "showinframes.php?".api_get_cidreq()."&amp;file=".$url_path.$req_gid;
  164. }
  165. else
  166. {
  167. //url-encode for problematic characters (we may not call them dangerous characters...)
  168. $path = str_replace('%2F', '/',$url_path).'?'.api_get_cidreq();
  169. $url=$www.$path;
  170. }
  171. //files that we want opened in a new window
  172. if($ext=='txt' || $ext=='log' || $ext=='css' || $ext=='js') //add here
  173. {
  174. $target='_blank';
  175. }
  176. }
  177. else
  178. {
  179. $url=api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  180. }
  181. //the little download icon
  182. //$tooltip_title = str_replace('?cidReq='.$_GET['cidReq'],'',basename($path));
  183. $tooltip_title = explode('?', basename($path));
  184. $tooltip_title = $tooltip_title[0];
  185. if($tooltip_title=='shared_folder')
  186. {
  187. $tooltip_title_alt=get_lang('SharedFolder');
  188. }
  189. elseif(strstr($tooltip_title, 'sf_user_'))
  190. {
  191. $userinfo=Database::get_user_info_from_id(substr($tooltip_title,8));
  192. $tooltip_title_alt=api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  193. }
  194. else
  195. {
  196. $tooltip_title_alt=$tooltip_title;
  197. }
  198. if (!$show_as_icon)
  199. {
  200. $force_download_html = ($size==0)?'':'<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon($forcedownload_icon, get_lang('Download'),array('height'=>'16', 'width' => '16')).'</a>';
  201. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.$title.'</a>'.$force_download_html;
  202. }
  203. else
  204. {
  205. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $tooltip_title).'</a>';
  206. }
  207. }
  208. /**
  209. * Builds an img html tag for the filetype
  210. *
  211. * @param string $type (file/folder)
  212. * @param string $path
  213. * @return string img html tag
  214. */
  215. function build_document_icon_tag($type, $path)
  216. {
  217. $basename = basename($path);
  218. $current_session_id = api_get_session_id();
  219. $is_allowed_to_edit = api_is_allowed_to_edit(null,true);
  220. if ($type == 'file')
  221. {
  222. $icon = choose_image($basename);
  223. }
  224. else
  225. {
  226. if($basename =='shared_folder')
  227. {
  228. $icon = 'shared_folder.gif';
  229. if ($is_allowed_to_edit)
  230. {
  231. $basename = get_lang('HelpSharedFolder');
  232. }
  233. else
  234. {
  235. $basename = get_lang('SharedFolder');
  236. }
  237. }
  238. elseif(strstr($basename, 'sf_user_'))
  239. {
  240. $userinfo=Database::get_user_info_from_id(substr($basename,8));
  241. $image_path = UserManager::get_user_picture_path_by_id(substr($basename,8),'web',false, true);
  242. if($image_path['file']=='unknown.jpg')
  243. {
  244. $icon = $image_path['file'];
  245. }
  246. else
  247. {
  248. $icon = '../upload/users/'.substr($basename,8).'/'.$image_path['file'];
  249. }
  250. $basename = api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  251. }
  252. else
  253. {
  254. if(($basename =='audio' || $basename =='flash' || $basename =='images' || $basename =='video') && api_is_allowed_to_edit()==true)
  255. {
  256. $basename = get_lang('HelpDefaultDirDocuments');
  257. }
  258. $icon = 'folder_document.gif';
  259. }
  260. }
  261. return Display::return_icon($icon, $basename, array('hspace'=>'5', 'align' => 'middle', 'height'=> 22, 'width' => 22));
  262. }
  263. /**
  264. * Creates the row of edit icons for a file/folder
  265. *
  266. * @param string $curdirpath current path (cfr open folder)
  267. * @param string $type (file/folder)
  268. * @param string $path dbase path of file/folder
  269. * @param int $visibility (1/0)
  270. * @param int $id dbase id of the document
  271. * @return string html img tags with hyperlinks
  272. */
  273. function build_edit_icons($curdirpath,$type,$path,$visibility,$id,$is_template,$is_read_only=0, $session_id=0)
  274. {
  275. if(isset($_SESSION['_gid']))
  276. {
  277. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  278. }
  279. else
  280. {
  281. $req_gid = '';
  282. }
  283. //build URL-parameters for table-sorting
  284. $sort_params = array();
  285. if( isset($_GET['column']))
  286. {
  287. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  288. }
  289. if( isset($_GET['page_nr']))
  290. {
  291. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  292. }
  293. if( isset($_GET['per_page']))
  294. {
  295. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  296. }
  297. if( isset($_GET['direction']))
  298. {
  299. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  300. }
  301. $sort_params = implode('&amp;',$sort_params);
  302. $visibility_icon = ($visibility==0)?'invisible':'visible';
  303. $visibility_command = ($visibility==0)?'set_visible':'set_invisible';
  304. $curdirpath = urlencode($curdirpath);
  305. $modify_icons = '';
  306. $cur_ses = api_get_session_id();
  307. // if document is read only *or* we're in a session and the document
  308. // is from a non-session context, hide the edition capabilities
  309. if ($is_read_only /*or ($session_id!=$cur_ses)*/)
  310. {
  311. $modify_icons = Display::return_icon('edit_na.gif', get_lang('Modify'));
  312. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.gif', get_lang('Delete'));
  313. $modify_icons .= '&nbsp;'.Display::return_icon('deplacer_fichier_na.gif', get_lang('Move'));
  314. $modify_icons .= '&nbsp;'.Display::return_icon($visibility_icon.'_na.gif', get_lang('VisibilityCannotBeChanged'));
  315. }
  316. else
  317. {
  318. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;file='.urlencode($path).$req_gid.'"><img src="../img/edit.gif" border="0" title="'.get_lang('Modify').'" alt="" /></a>';
  319. 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 or strcmp($path,'/chat_files')===0) {
  320. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.gif',get_lang('ThisFolderCannotBeDeleted'));
  321. } else {
  322. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');"><img src="../img/delete.gif" border="0" title="'.get_lang('Delete').'" alt="" /></a>';
  323. }
  324. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;move='.urlencode($path).$req_gid.'"><img src="../img/deplacer_fichier.gif" border="0" title="'.get_lang('Move').'" alt="" /></a>';
  325. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;'.$visibility_command.'='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon.'.gif" border="0" title="'.get_lang('Visible').'" alt="" /></a>';
  326. }
  327. if($type == 'file' && pathinfo($path,PATHINFO_EXTENSION)=='html')
  328. {
  329. if($is_template==0)
  330. {
  331. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;add_as_template='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/wizard_small.gif" border="0" title="'.get_lang('AddAsTemplate').'" alt="'.get_lang('AddAsTemplate').'" /></a>';
  332. }
  333. else{
  334. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/wizard_gray_small.gif" border="0" title="'.get_lang('RemoveAsTemplate').'" alt=""'.get_lang('RemoveAsTemplate').'" /></a>';
  335. }
  336. }
  337. return $modify_icons;
  338. }
  339. function build_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  340. {
  341. $form = '<form name="move_to" action="'.api_get_self().'" method="post">'."\n";
  342. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  343. $form .= '<div class="row">';
  344. $form .= ' <div class="label">';
  345. $form .= get_lang('MoveTo');
  346. $form .= ' </div>';
  347. $form .= ' <div class="formw">';
  348. $form .= ' <select name="move_to">'."\n";
  349. //group documents cannot be uploaded in the root
  350. if($group_dir=='')
  351. {
  352. if($curdirpath!='/')
  353. {
  354. $form .= '<option value="/">/ ('.get_lang('HomeDirectory').')</option>';
  355. }
  356. if(is_array($folders))
  357. {
  358. foreach ($folders AS $folder)
  359. {
  360. //you cannot move a file to:
  361. //1. current directory
  362. //2. inside the folder you want to move
  363. //3. inside a subfolder of the folder you want to move
  364. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))
  365. {
  366. $path_displayed = $folder;
  367. // if document title is used, we have to display titles instead of real paths...
  368. if(api_get_setting('use_document_title'))
  369. {
  370. $path_displayed = get_titles_of_path($folder);
  371. }
  372. $form .= '<option value="'.$folder.'">'.$path_displayed.'</option>'."\n";
  373. }
  374. }
  375. }
  376. }
  377. else
  378. {
  379. foreach ($folders AS $folder)
  380. {
  381. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))//cannot copy dir into his own subdir
  382. {
  383. if(api_get_setting('use_document_title'))
  384. {
  385. $path_displayed = get_titles_of_path($folder);
  386. }
  387. $display_folder = substr($path_displayed,strlen($group_dir));
  388. $display_folder = ($display_folder == '')?'/ ('.get_lang('HomeDirectory').')':$display_folder;
  389. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  390. }
  391. }
  392. }
  393. $form .= ' </select>'."\n";
  394. $form .= ' </div>';
  395. $form .= '<div class="row">';
  396. $form .= ' <div class="label"></div>';
  397. $form .= ' <div class="formw">';
  398. $form .= ' <button type="submit" class="next" name="move_file_submit">'.get_lang('MoveFile').'</button>'."\n";
  399. $form .= ' </div>';
  400. $form .= '</div>';
  401. $form .= '</form>';
  402. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  403. return $form;
  404. }
  405. /**
  406. * get the path translated with title of docs and folders
  407. * @param string the real path
  408. * @return the path which should be displayed
  409. */
  410. function get_titles_of_path($path)
  411. {
  412. global $tmp_folders_titles;
  413. $nb_slashes = substr_count($path,'/');
  414. $tmp_path = '';
  415. $current_slash_pos = 0;
  416. $path_displayed = '';
  417. for($i=0; $i<$nb_slashes; $i++)
  418. { // foreach folders of the path, retrieve title.
  419. $current_slash_pos = strpos($path,'/',$current_slash_pos+1);
  420. $tmp_path = substr($path,strpos($path,'/',0),$current_slash_pos);
  421. if(empty($tmp_path)) // if empty, then we are in the final part of the path
  422. $tmp_path = $path;
  423. if(!empty($tmp_folders_titles[$tmp_path])) // if this path has soon been stored here we don't need a new query
  424. {
  425. $path_displayed .= $tmp_folders_titles[$tmp_path];
  426. }
  427. else
  428. {
  429. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).' WHERE path LIKE BINARY "'.$tmp_path.'"';
  430. $rs = Database::query($sql,__FILE__,__LINE__);
  431. $tmp_title = '/'.Database::result($rs,0,0);
  432. $path_displayed .= $tmp_title;
  433. $tmp_folders_titles[$tmp_path] = $tmp_title;
  434. }
  435. }
  436. return $path_displayed;
  437. }
  438. /**
  439. * This function displays the name of the user and makes the link tothe user tool.
  440. *
  441. * @param $user_id
  442. * @param $name
  443. * @return a link to the userInfo.php
  444. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  445. * @version february 2006, dokeos 1.8
  446. */
  447. function display_user_link_document($user_id, $name)
  448. {
  449. if ($user_id<>0)
  450. {
  451. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  452. }
  453. else
  454. {
  455. return get_lang('Anonymous');
  456. }
  457. }
  458. function create_dir_form()
  459. {
  460. //create the form that asks for the directory name
  461. $new_folder_text = '<form action="'.api_get_self().'" method="post">';
  462. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.Security::remove_XSS($_GET['curdirpath']).'" />';
  463. // form title
  464. $new_folder_text .= '<div class="row"><div class="form_header">'.get_lang('CreateDir').'</div></div>';
  465. // folder field
  466. $new_folder_text .= '<div class="row">';
  467. $new_folder_text .= '<div class="label"><span class="form_required">*</span>'.get_lang('NewDir').'</div>';
  468. $new_folder_text .= '<div class="formw"><input type="text" name="dirname" /></div>';
  469. $new_folder_text .= '</div>';
  470. // submit button
  471. $new_folder_text .= '<div class="row">';
  472. $new_folder_text .= '<div class="label">&nbsp;</div>';
  473. $new_folder_text .= '<div class="formw"><button type="submit" class="add" name="create_dir">'.get_lang('CreateFolder').'</button></div>';
  474. $new_folder_text .= '</div>';
  475. $new_folder_text .= '</form>';
  476. $new_folder_text .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  477. return $new_folder_text;
  478. }
  479. ?>