document.inc.php 14 KB

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