document.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. if ($type == 'file')
  219. {
  220. $icon = choose_image($basename);
  221. }
  222. else
  223. {
  224. if($basename =='shared_folder')
  225. {
  226. $icon = 'shared_folder.gif';
  227. if (api_is_allowed_to_edit())
  228. {
  229. $basename = get_lang('HelpSharedFolder');
  230. }
  231. else
  232. {
  233. $basename = get_lang('SharedFolder');
  234. }
  235. }
  236. elseif(strstr($basename, 'sf_user_'))
  237. {
  238. $userinfo=Database::get_user_info_from_id(substr($basename,8));
  239. $image_path = UserManager::get_user_picture_path_by_id(substr($basename,8),'web',false, true);
  240. if($image_path['file']=='unknown.jpg')
  241. {
  242. $icon = $image_path['file'];
  243. }
  244. else
  245. {
  246. $icon = '../upload/users/'.substr($basename,8).'/'.$image_path['file'];
  247. }
  248. $basename = api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  249. }
  250. else
  251. {
  252. if(($basename =='audio' || $basename =='flash' || $basename =='images' || $basename =='video') && api_is_allowed_to_edit()==true)
  253. {
  254. $basename = get_lang('HelpDefaultDirDocuments');
  255. }
  256. $icon = 'folder_document.gif';
  257. }
  258. }
  259. return Display::return_icon($icon, $basename, array('hspace'=>'5', 'align' => 'middle', 'height'=> 22, 'width' => 22));
  260. }
  261. /**
  262. * Creates the row of edit icons for a file/folder
  263. *
  264. * @param string $curdirpath current path (cfr open folder)
  265. * @param string $type (file/folder)
  266. * @param string $path dbase path of file/folder
  267. * @param int $visibility (1/0)
  268. * @param int $id dbase id of the document
  269. * @return string html img tags with hyperlinks
  270. */
  271. function build_edit_icons($curdirpath,$type,$path,$visibility,$id,$is_template,$is_read_only=0)
  272. {
  273. if(isset($_SESSION['_gid']))
  274. {
  275. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  276. }
  277. else
  278. {
  279. $req_gid = '';
  280. }
  281. //build URL-parameters for table-sorting
  282. $sort_params = array();
  283. if( isset($_GET['column']))
  284. {
  285. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  286. }
  287. if( isset($_GET['page_nr']))
  288. {
  289. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  290. }
  291. if( isset($_GET['per_page']))
  292. {
  293. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  294. }
  295. if( isset($_GET['direction']))
  296. {
  297. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  298. }
  299. $sort_params = implode('&amp;',$sort_params);
  300. $visibility_icon = ($visibility==0)?'invisible':'visible';
  301. $visibility_command = ($visibility==0)?'set_visible':'set_invisible';
  302. $curdirpath = urlencode($curdirpath);
  303. $modify_icons = '';
  304. if ($is_read_only)
  305. {
  306. $modify_icons = Display::return_icon('edit_na.gif', get_lang('Modify'));
  307. $modify_icons .= '&nbsp;'.Display::return_icon('delete.gif', get_lang('Delete'));
  308. $modify_icons .= '&nbsp;'.Display::return_icon('deplacer_fichier_na.gif', get_lang('Move'));
  309. $modify_icons .= '&nbsp;'.Display::return_icon($visibility_icon.'_na.gif', get_lang('VisibilityCannotBeChanged'));
  310. }
  311. else
  312. {
  313. $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>';
  314. 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) {
  315. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.gif',get_lang('ThisFolderCannotBeDeleted'));
  316. } else {
  317. $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>';
  318. }
  319. $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>';
  320. $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>';
  321. }
  322. if($type == 'file' && pathinfo($path,PATHINFO_EXTENSION)=='html')
  323. {
  324. if($is_template==0)
  325. {
  326. $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>';
  327. }
  328. else{
  329. $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>';
  330. }
  331. }
  332. return $modify_icons;
  333. }
  334. function build_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  335. {
  336. $form = '<form name="move_to" action="'.api_get_self().'" method="post">'."\n";
  337. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  338. $form .= '<div class="row">';
  339. $form .= ' <div class="label">';
  340. $form .= get_lang('MoveTo');
  341. $form .= ' </div>';
  342. $form .= ' <div class="formw">';
  343. $form .= ' <select name="move_to">'."\n";
  344. //group documents cannot be uploaded in the root
  345. if($group_dir=='')
  346. {
  347. if($curdirpath!='/')
  348. {
  349. $form .= '<option value="/">/ ('.get_lang('HomeDirectory').')</option>';
  350. }
  351. if(is_array($folders))
  352. {
  353. foreach ($folders AS $folder)
  354. {
  355. //you cannot move a file to:
  356. //1. current directory
  357. //2. inside the folder you want to move
  358. //3. inside a subfolder of the folder you want to move
  359. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))
  360. {
  361. $path_displayed = $folder;
  362. // if document title is used, we have to display titles instead of real paths...
  363. if(api_get_setting('use_document_title'))
  364. {
  365. $path_displayed = get_titles_of_path($folder);
  366. }
  367. $form .= '<option value="'.$folder.'">'.$path_displayed.'</option>'."\n";
  368. }
  369. }
  370. }
  371. }
  372. else
  373. {
  374. foreach ($folders AS $folder)
  375. {
  376. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))//cannot copy dir into his own subdir
  377. {
  378. if(api_get_setting('use_document_title'))
  379. {
  380. $path_displayed = get_titles_of_path($folder);
  381. }
  382. $display_folder = substr($path_displayed,strlen($group_dir));
  383. $display_folder = ($display_folder == '')?'/ ('.get_lang('HomeDirectory').')':$display_folder;
  384. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  385. }
  386. }
  387. }
  388. $form .= ' </select>'."\n";
  389. $form .= ' </div>';
  390. $form .= '<div class="row">';
  391. $form .= ' <div class="label"></div>';
  392. $form .= ' <div class="formw">';
  393. $form .= ' <button type="submit" class="next" name="move_file_submit">'.get_lang('MoveFile').'</button>'."\n";
  394. $form .= ' </div>';
  395. $form .= '</div>';
  396. $form .= '</form>';
  397. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  398. return $form;
  399. }
  400. /**
  401. * get the path translated with title of docs and folders
  402. * @param string the real path
  403. * @return the path which should be displayed
  404. */
  405. function get_titles_of_path($path)
  406. {
  407. global $tmp_folders_titles;
  408. $nb_slashes = substr_count($path,'/');
  409. $tmp_path = '';
  410. $current_slash_pos = 0;
  411. $path_displayed = '';
  412. for($i=0; $i<$nb_slashes; $i++)
  413. { // foreach folders of the path, retrieve title.
  414. $current_slash_pos = strpos($path,'/',$current_slash_pos+1);
  415. $tmp_path = substr($path,strpos($path,'/',0),$current_slash_pos);
  416. if(empty($tmp_path)) // if empty, then we are in the final part of the path
  417. $tmp_path = $path;
  418. if(!empty($tmp_folders_titles[$tmp_path])) // if this path has soon been stored here we don't need a new query
  419. {
  420. $path_displayed .= $tmp_folders_titles[$tmp_path];
  421. }
  422. else
  423. {
  424. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).' WHERE path LIKE BINARY "'.$tmp_path.'"';
  425. $rs = Database::query($sql,__FILE__,__LINE__);
  426. $tmp_title = '/'.Database::result($rs,0,0);
  427. $path_displayed .= $tmp_title;
  428. $tmp_folders_titles[$tmp_path] = $tmp_title;
  429. }
  430. }
  431. return $path_displayed;
  432. }
  433. /**
  434. * This function displays the name of the user and makes the link tothe user tool.
  435. *
  436. * @param $user_id
  437. * @param $name
  438. * @return a link to the userInfo.php
  439. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  440. * @version february 2006, dokeos 1.8
  441. */
  442. function display_user_link_document($user_id, $name)
  443. {
  444. if ($user_id<>0)
  445. {
  446. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  447. }
  448. else
  449. {
  450. return get_lang('Anonymous');
  451. }
  452. }
  453. function create_dir_form()
  454. {
  455. //create the form that asks for the directory name
  456. $new_folder_text = '<form action="'.api_get_self().'" method="post">';
  457. $new_folder_text .= '<input type="hidden" name="curdirpath" value="'.Security::remove_XSS($_GET['curdirpath']).'" />';
  458. // form title
  459. $new_folder_text .= '<div class="row"><div class="form_header">'.get_lang('CreateDir').'</div></div>';
  460. // folder field
  461. $new_folder_text .= '<div class="row">';
  462. $new_folder_text .= '<div class="label"><span class="form_required">*</span>'.get_lang('NewDir').'</div>';
  463. $new_folder_text .= '<div class="formw"><input type="text" name="dirname" /></div>';
  464. $new_folder_text .= '</div>';
  465. // submit button
  466. $new_folder_text .= '<div class="row">';
  467. $new_folder_text .= '<div class="label">&nbsp;</div>';
  468. $new_folder_text .= '<div class="formw"><button type="submit" class="add" name="create_dir">'.get_lang('CreateFolder').'</button></div>';
  469. $new_folder_text .= '</div>';
  470. $new_folder_text .= '</form>';
  471. $new_folder_text .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  472. return $new_folder_text;
  473. }
  474. ?>