document.inc.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php // $Id: document.inc.php 17556 2009-01-07 10:46:20Z herodoto $
  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(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 = api_sql_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. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$folder_titles[$folder];
  85. $parent_select -> addOption($label,$folder);
  86. if($selected!='') $parent_select->setSelected($folder);
  87. }
  88. }
  89. }
  90. else
  91. {
  92. foreach ($folders as $folder)
  93. {
  94. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  95. $label = $folder_titles[$folder];
  96. if( $folder == $group_dir)
  97. {
  98. $label = '/ ('.get_lang('HomeDirectory').')';
  99. }
  100. else
  101. {
  102. $path_parts = explode('/',str_replace($group_dir,'',$folder));
  103. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$label;
  104. }
  105. $parent_select -> addOption($label,$folder);
  106. if($selected!='') $parent_select->setSelected($folder);
  107. }
  108. }
  109. $form=$form->toHtml();
  110. return $form;
  111. }
  112. /**
  113. * Create a html hyperlink depending on if it's a folder or a file
  114. *
  115. * @param string $www
  116. * @param string $title
  117. * @param string $path
  118. * @param string $filetype (file/folder)
  119. * @param int $visibility (1/0)
  120. * @param int $show_as_icon - if it is true, only a clickable icon will be shown
  121. * @return string url
  122. */
  123. function create_document_link($www, $title, $path, $filetype, $size, $visibility, $show_as_icon = false)
  124. {
  125. global $dbl_click_id;
  126. if(isset($_SESSION['_gid']))
  127. {
  128. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  129. }
  130. else
  131. {
  132. $req_gid = '';
  133. }
  134. $url_path = urlencode($path);
  135. //add class="invisible" on invisible files
  136. $visibility_class= ($visibility==0)?' class="invisible"':'';
  137. if (!$show_as_icon)
  138. {
  139. //build download link (icon)
  140. $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;
  141. //folder download or file download?
  142. $forcedownload_icon=($filetype=='folder')?'folder_zip.gif':'filesave.gif';
  143. //prevent multiple clicks on zipped folder download
  144. $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; }\"":'';
  145. }
  146. $target='_top';
  147. if($filetype=='file') {
  148. //check the extension
  149. $ext=explode('.',$path);
  150. $ext=strtolower($ext[sizeof($ext)-1]);
  151. //"htmlfiles" are shown in a frameset
  152. if($ext == 'htm' || $ext == 'html' || $ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png')
  153. {
  154. $url = "showinframes.php?".api_get_cidreq()."&amp;file=".$url_path.$req_gid;
  155. }
  156. else
  157. {
  158. //url-encode for problematic characters (we may not call them dangerous characters...)
  159. $path = str_replace('%2F', '/',$url_path).'?'.api_get_cidreq();
  160. $url=$www.$path;
  161. }
  162. //files that we want opened in a new window
  163. if($ext=='txt') //add here
  164. {
  165. $target='_blank';
  166. }
  167. }
  168. else
  169. {
  170. $url=api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  171. }
  172. //the little download icon
  173. //$tooltip_title = str_replace('?cidReq='.$_GET['cidReq'],'',basename($path));
  174. $tooltip_title = explode('?', basename($path));
  175. $tooltip_title = $tooltip_title[0];
  176. if (!$show_as_icon)
  177. {
  178. $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>';
  179. return '<a href="'.$url.'" title="'.$tooltip_title.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.$title.'</a>'.$force_download_html;
  180. }
  181. else
  182. {
  183. return '<a href="'.$url.'" title="'.$tooltip_title.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $tooltip_title).'</a>';
  184. }
  185. }
  186. /**
  187. * Builds an img html tag for the filetype
  188. *
  189. * @param string $type (file/folder)
  190. * @param string $path
  191. * @return string img html tag
  192. */
  193. function build_document_icon_tag($type, $path)
  194. {
  195. $basename = basename($path);
  196. if ($type == 'file')
  197. {
  198. $icon = choose_image($basename);
  199. }
  200. else
  201. {
  202. if($basename =='shared_folder')
  203. {
  204. $icon = 'shared_folder.gif';
  205. $basename = get_lang('SharedFolder');
  206. }
  207. else
  208. {
  209. $icon = 'folder_document.gif';
  210. }
  211. }
  212. return Display::return_icon($icon, $basename, array('hspace'=>'5', 'align' => 'middle'));
  213. }
  214. /**
  215. * Creates the row of edit icons for a file/folder
  216. *
  217. * @param string $curdirpath current path (cfr open folder)
  218. * @param string $type (file/folder)
  219. * @param string $path dbase path of file/folder
  220. * @param int $visibility (1/0)
  221. * @param int $id dbase id of the document
  222. * @return string html img tags with hyperlinks
  223. */
  224. function build_edit_icons($curdirpath,$type,$path,$visibility,$id,$is_template,$is_read_only=0)
  225. {
  226. if(isset($_SESSION['_gid']))
  227. {
  228. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  229. }
  230. else
  231. {
  232. $req_gid = '';
  233. }
  234. //build URL-parameters for table-sorting
  235. $sort_params = array();
  236. if( isset($_GET['column']))
  237. {
  238. $sort_params[] = 'column='.$_GET['column'];
  239. }
  240. if( isset($_GET['page_nr']))
  241. {
  242. $sort_params[] = 'page_nr='.$_GET['page_nr'];
  243. }
  244. if( isset($_GET['per_page']))
  245. {
  246. $sort_params[] = 'per_page='.$_GET['per_page'];
  247. }
  248. if( isset($_GET['direction']))
  249. {
  250. $sort_params[] = 'direction='.$_GET['direction'];
  251. }
  252. $sort_params = implode('&amp;',$sort_params);
  253. $visibility_icon = ($visibility==0)?'invisible':'visible';
  254. $visibility_command = ($visibility==0)?'set_visible':'set_invisible';
  255. $curdirpath = urlencode($curdirpath);
  256. $modify_icons = '';
  257. if ($is_read_only)
  258. {
  259. $modify_icons = Display::return_icon('edit_na.gif', get_lang('Modify'));
  260. $modify_icons .= '&nbsp;'.Display::return_icon('delete.gif', get_lang('Delete'));
  261. $modify_icons .= '&nbsp;'.Display::return_icon('deplacer_fichier_na.gif', get_lang('Move'));
  262. $modify_icons .= '&nbsp;'.Display::return_icon($visibility_icon.'_na.gif', get_lang('Visible'));
  263. }
  264. else
  265. {
  266. $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>';
  267. if (strcmp($path,'/audio')===0 or strcmp($path,'/flash')===0 or strcmp($path,'/images')===0) {
  268. $modify_icons .= '&nbsp;<img src="../img/delete_na.gif" border="0" title="'.get_lang('Delete').'" alt="'.get_lang('Delete').'" /></a>';
  269. } else {
  270. $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>';
  271. }
  272. $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>';
  273. $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>';
  274. }
  275. if($type == 'file' && pathinfo($path,PATHINFO_EXTENSION)=='html')
  276. {
  277. if($is_template==0)
  278. {
  279. $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>';
  280. }
  281. else{
  282. $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>';
  283. }
  284. }
  285. return $modify_icons;
  286. }
  287. function build_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  288. {
  289. $form = '<form name="move_to" action="'.api_get_self().'" method="post">'."\n";
  290. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  291. $form .= get_lang('MoveTo').' <select name="move_to">'."\n";
  292. //group documents cannot be uploaded in the root
  293. if($group_dir=='')
  294. {
  295. if($curdirpath!='/')
  296. {
  297. $form .= '<option value="/">/ ('.get_lang('HomeDirectory').')</option>';
  298. }
  299. if(is_array($folders))
  300. {
  301. foreach ($folders AS $folder)
  302. {
  303. //you cannot move a file to:
  304. //1. current directory
  305. //2. inside the folder you want to move
  306. //3. inside a subfolder of the folder you want to move
  307. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))
  308. {
  309. $path_displayed = $folder;
  310. // if document title is used, we have to display titles instead of real paths...
  311. if(api_get_setting('use_document_title'))
  312. {
  313. $path_displayed = get_titles_of_path($folder);
  314. }
  315. $form .= '<option value="'.$folder.'">'.$path_displayed.'</option>'."\n";
  316. }
  317. }
  318. }
  319. }
  320. else
  321. {
  322. foreach ($folders AS $folder)
  323. {
  324. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))//cannot copy dir into his own subdir
  325. {
  326. if(api_get_setting('use_document_title'))
  327. {
  328. $path_displayed = get_titles_of_path($folder);
  329. }
  330. $display_folder = substr($path_displayed,strlen($group_dir));
  331. $display_folder = ($display_folder == '')?'/ ('.get_lang('HomeDirectory').')':$display_folder;
  332. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  333. }
  334. }
  335. }
  336. $form .= '</select>'."\n";
  337. $form .= '<input type="submit" name="move_file_submit" value="'.get_lang('Ok').'" />'."\n";
  338. $form .= '</form>';
  339. return $form;
  340. }
  341. /**
  342. * get the path translated with title of docs and folders
  343. * @param string the real path
  344. * @return the path which should be displayed
  345. */
  346. function get_titles_of_path($path)
  347. {
  348. global $tmp_folders_titles;
  349. $nb_slashes = substr_count($path,'/');
  350. $tmp_path = '';
  351. $current_slash_pos = 0;
  352. $path_displayed = '';
  353. for($i=0; $i<$nb_slashes; $i++)
  354. { // foreach folders of the path, retrieve title.
  355. $current_slash_pos = strpos($path,'/',$current_slash_pos+1);
  356. $tmp_path = substr($path,strpos($path,'/',0),$current_slash_pos);
  357. if(empty($tmp_path)) // if empty, then we are in the final part of the path
  358. $tmp_path = $path;
  359. if(!empty($tmp_folders_titles[$tmp_path])) // if this path has soon been stored here we don't need a new query
  360. {
  361. $path_displayed .= $tmp_folders_titles[$tmp_path];
  362. }
  363. else
  364. {
  365. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).' WHERE path LIKE BINARY "'.$tmp_path.'"';
  366. $rs = api_sql_query($sql,__FILE__,__LINE__);
  367. $tmp_title = '/'.Database::result($rs,0,0);
  368. $path_displayed .= $tmp_title;
  369. $tmp_folders_titles[$tmp_path] = $tmp_title;
  370. }
  371. }
  372. return $path_displayed;
  373. }
  374. /**
  375. * This function displays the name of the user and makes the link tothe user tool.
  376. *
  377. * @param $user_id
  378. * @param $name
  379. * @return a link to the userInfo.php
  380. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  381. * @version february 2006, dokeos 1.8
  382. */
  383. function display_user_link_document($user_id, $name)
  384. {
  385. if ($user_id<>0)
  386. {
  387. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  388. }
  389. else
  390. {
  391. return get_lang('Anonymous');
  392. }
  393. }
  394. ?>