document.inc.php 15 KB

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