document.inc.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php // $Id: document.inc.php 10839 2007-01-23 09:35:28Z elixir_julian $
  2. /*
  3. ==============================================================================
  4. EXTRA FUNCTIONS FOR DOCUMENT.PHP/UPLOAD.PHP
  5. ==============================================================================
  6. /////////////////////////////////////////////////
  7. //--> leave these here or move them elsewhere? //
  8. /////////////////////////////////////////////////
  9. */
  10. /**
  11. * Builds the form thats enables the user to
  12. * select a directory to browse/upload in
  13. *
  14. * @param array $folders
  15. * @param string $curdirpath
  16. * @param string $group_dir
  17. * @return string html form
  18. */
  19. function build_directory_selector($folders,$curdirpath,$group_dir='',$changeRenderer)
  20. {
  21. $folder_titles = array();
  22. if(get_setting('use_document_title') == 'true')
  23. {
  24. $escaped_folders = $folders;
  25. array_walk($escaped_folders, 'mysql_real_escape_string');
  26. $folder_sql = implode("','",$escaped_folders);
  27. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  28. $sql = "SELECT * FROM $doc_table WHERE filetype='folder' AND path IN ('".$folder_sql."')";
  29. $res = api_sql_query($sql,__FILE__,__LINE__);
  30. $folder_titles = array();
  31. while($obj = mysql_fetch_object($res))
  32. {
  33. $folder_titles[$obj->path] = $obj->title;
  34. }
  35. }
  36. else
  37. {
  38. foreach($folders as $folder)
  39. {
  40. $folder_titles[$folder] = basename($folder);
  41. }
  42. }
  43. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  44. $form = new FormValidator('selector','POST',$_SERVER['PHP_SELF']);
  45. $parent_select = $form->addElement('select', 'curdirpath', get_lang('CurrentDirectory'),'','onchange="javascript:document.selector.submit()"');
  46. if($changeRenderer==true){
  47. $renderer = $form->defaultRenderer();
  48. $renderer->setElementTemplate('<span>{label} : {element}</span> ','curdirpath');
  49. }
  50. //group documents cannot be uploaded in the root
  51. if($group_dir=='')
  52. {
  53. $parent_select -> addOption(get_lang('Root'),'/');
  54. if(is_array($folders))
  55. {
  56. foreach ($folders AS $folder)
  57. {
  58. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  59. $path_parts = explode('/',$folder);
  60. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$folder_titles[$folder];
  61. $parent_select -> addOption($label,$folder);
  62. if($selected!='') $parent_select->setSelected($folder);
  63. }
  64. }
  65. }
  66. else
  67. {
  68. foreach ($folders AS $folder)
  69. {
  70. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  71. $label = $folder_titles[$folder];
  72. if( $folder == $group_dir)
  73. {
  74. $label = '/ ('.get_lang('Root').')';
  75. }
  76. else
  77. {
  78. $path_parts = explode('/',str_replace($group_dir,'',$folder));
  79. $label = str_repeat('&nbsp;&nbsp;&nbsp;',count($path_parts)-2).' &mdash; '.$label;
  80. }
  81. $parent_select -> addOption($label,$folder);
  82. if($selected!='') $parent_select->setSelected($folder);
  83. }
  84. }
  85. $form=$form->toHtml();
  86. return $form;
  87. }
  88. function display_document_options()
  89. {
  90. $message = "<a href=\"quota.php\">".get_lang("ShowCourseQuotaUse")."</a>";
  91. echo /*"<div id=\"smallmessagebox\">"
  92. .*/ "<p>" . $message . "</p>"
  93. /*. "</div>"*/;
  94. }
  95. /**
  96. * Create a html hyperlink depending on if it's a folder or a file
  97. *
  98. * @param string $www
  99. * @param string $title
  100. * @param string $path
  101. * @param string $filetype (file/folder)
  102. * @param int $visibility (1/0)
  103. * @return string url
  104. */
  105. function create_document_link($www,$title,$path,$filetype,$size,$visibility)
  106. {
  107. global $dbl_click_id;
  108. if(isset($_SESSION['_gid']))
  109. {
  110. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  111. }
  112. else
  113. {
  114. $req_gid = '';
  115. }
  116. $url_path = urlencode($path);
  117. //add class="invisible" on invisible files
  118. $visibility_class= ($visibility==0)?' class="invisible"':'';
  119. //build download link (icon)
  120. $forcedownload_link=($filetype=='folder')?$_SERVER['PHP_SELF'].'?action=downloadfolder&amp;path='.$url_path.$req_gid:$_SERVER['PHP_SELF'].'?action=download&amp;id='.$url_path.$req_gid;
  121. //folder download or file download?
  122. $forcedownload_icon=($filetype=='folder')?'folder_zip.gif':'filesave.gif';
  123. //prevent multiple clicks on zipped folder download
  124. $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; }\"":'';
  125. $target='_top';
  126. if($filetype=='file') {
  127. //check the extension
  128. $ext=explode('.',$path);
  129. $ext=strtolower($ext[sizeof($ext)-1]);
  130. //"htmlfiles" are shown in a frameset
  131. if($ext == 'htm' || $ext == 'html' || $ext == 'gif' || $ext == 'jpg' || $ext == 'jpeg' || $ext == 'png')
  132. {
  133. $url = "showinframes.php?".api_get_cidreq()."&amp;file=".$url_path.$req_gid;
  134. }
  135. else
  136. {
  137. //url-encode for problematic characters (we may not call them dangerous characters...)
  138. $path = str_replace('%2F', '/',$url_path).'?'.api_get_cidreq();
  139. $url=$www.$path;
  140. }
  141. //files that we want opened in a new window
  142. if($ext=='txt') //add here
  143. {
  144. $target='_blank';
  145. }
  146. }
  147. else {
  148. $url=$_SERVER['PHP_SELF'].'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  149. }
  150. //the little download icon
  151. $force_download_html = ($size==0)?'':'<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'><img width="16" height="16" src="'.api_get_path(WEB_CODE_PATH).'img/'.$forcedownload_icon.'" alt="" /></a>';
  152. $tooltip_title = str_replace('?cidReq='.$_GET['cidReq'],'',basename($path));
  153. return '<a href="'.$url.'" title="'.$tooltip_title.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.$title.'</a>'.$force_download_html;
  154. }
  155. /**
  156. * Builds an img html tag for the filetype
  157. *
  158. * @param string $type (file/folder)
  159. * @param string $path
  160. * @return string img html tag
  161. */
  162. function build_document_icon_tag($type,$path)
  163. {
  164. $icon='folder_document.gif';
  165. if($type=='file')
  166. {
  167. $icon=choose_image(basename($path));
  168. }
  169. return '<img src="'.api_get_path(WEB_CODE_PATH).'img/'.$icon.'" border="0" hspace="5" align="middle" alt="" />';
  170. }
  171. /**
  172. * Creates the row of edit icons for a file/folder
  173. *
  174. * @param string $curdirpath current path (cfr open folder)
  175. * @param string $type (file/folder)
  176. * @param string $path dbase path of file/folder
  177. * @param int $visibility (1/0)
  178. * @param int $id dbase id of the document
  179. * @return string html img tags with hyperlinks
  180. */
  181. function build_edit_icons($curdirpath,$type,$path,$visibility,$id)
  182. {
  183. if(isset($_SESSION['_gid']))
  184. {
  185. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  186. }
  187. else
  188. {
  189. $req_gid = '';
  190. }
  191. //build URL-parameters for table-sorting
  192. $sort_params = array();
  193. if( isset($_GET['column']))
  194. {
  195. $sort_params[] = 'column='.$_GET['column'];
  196. }
  197. if( isset($_GET['page_nr']))
  198. {
  199. $sort_params[] = 'page_nr='.$_GET['page_nr'];
  200. }
  201. if( isset($_GET['per_page']))
  202. {
  203. $sort_params[] = 'per_page='.$_GET['per_page'];
  204. }
  205. if( isset($_GET['direction']))
  206. {
  207. $sort_params[] = 'direction='.$_GET['direction'];
  208. }
  209. $sort_params = implode('&amp;',$sort_params);
  210. $visibility_icon = ($visibility==0)?'invisible':'visible';
  211. $visibility_command = ($visibility==0)?'set_visible':'set_invisible';
  212. $curdirpath = urlencode($curdirpath);
  213. $modify_icons = '<a href="edit_document.php?curdirpath='.$curdirpath.'&amp;file='.urlencode($path).$gid_req.'"><img src="../img/edit.gif" border="0" title="'.get_lang('Modify').'" alt="" /></a>';
  214. $modify_icons .= '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$gid_req.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');"><img src="../img/delete.gif" border="0" title="'.get_lang('Delete').'" alt="" /></a>';
  215. $modify_icons .= '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?curdirpath='.$curdirpath.'&amp;move='.urlencode($path).$gid_req.'"><img src="../img/deplacer_fichier.gif" border="0" title="'.get_lang('Move').'" alt="" /></a>';
  216. $modify_icons .= '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?curdirpath='.$curdirpath.'&amp;'.$visibility_command.'='.$id.$gid_req.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon.'.gif" border="0" title="'.get_lang('Visible').'" alt="" /></a>';
  217. return $modify_icons;
  218. }
  219. function build_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  220. {
  221. $form = '<form name="move_to" action="'.$_SERVER['PHP_SELF'].'" method="post">'."\n";
  222. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  223. $form .= get_lang('MoveTo').' <select name="move_to">'."\n";
  224. //group documents cannot be uploaded in the root
  225. if($group_dir=='')
  226. {
  227. if($curdirpath!='/')
  228. {
  229. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  230. }
  231. if(is_array($folders))
  232. {
  233. foreach ($folders AS $folder)
  234. {
  235. //you cannot move a file to:
  236. //1. current directory
  237. //2. inside the folder you want to move
  238. //3. inside a subfolder of the folder you want to move
  239. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))
  240. {
  241. $form .= '<option value="'.$folder.'">'.$folder.'</option>'."\n";
  242. }
  243. }
  244. }
  245. }
  246. else
  247. {
  248. foreach ($folders AS $folder)
  249. {
  250. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))//cannot copy dir into his own subdir
  251. {
  252. $display_folder = substr($folder,strlen($group_dir));
  253. $display_folder = ($display_folder == '')?'/ ('.get_lang('Root').')':$display_folder;
  254. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  255. }
  256. }
  257. }
  258. $form .= '</select>'."\n";
  259. $form .= '<input type="submit" name="move_file_submit" value="'.get_lang('Ok').'" />'."\n";
  260. $form .= '</form>';
  261. return $form;
  262. }
  263. ?>