made a copy
[atutor.git] / include / html / filemanager_display_alternatives.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay & Joel Kronenberg        */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id: filemanager_display_alternatives.inc.php 7208 2008-07-04 16:07:24Z silvia $
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16
17 define('AT_INCLUDE_PATH', '../../include/');
18 //require(AT_INCLUDE_PATH.'vitals.inc.php');
19 //require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
20
21 global $db;
22
23 //from tools/filemanager/index.php
24 if ((isset($_REQUEST['popup']) && $_REQUEST['popup']) && 
25         (!isset($_REQUEST['framed']) || !$_REQUEST['framed'])) {
26         $popup = TRUE;
27         $framed = FALSE;
28 } else if (isset($_REQUEST['framed']) && $_REQUEST['framed'] &&
29          isset($_REQUEST['popup']) && $_REQUEST['popup']) {
30         $popup = TRUE;
31         $framed = TRUE;
32 } else {
33         $popup = FALSE;
34         $framed = FALSE;
35 }
36 // end tools/filemanager/index.php
37
38
39 function get_file_extension($file_name) {
40         $ext = pathinfo($file_name);
41         return $ext['extension'];
42 }
43
44 function get_file_type_icon($file_name) {
45         static $mime;
46
47         $ext = get_file_extension($file_name);
48
49         if (!isset($mime)) {
50                 require(AT_INCLUDE_PATH .'lib/mime.inc.php');
51         }
52
53         if (isset($mime[$ext]) && $mime[$ext][1]) {
54                 return $mime[$ext][1];
55         }
56         return 'generic';
57 }
58
59 function get_relative_path($src, $dest) {
60         if ($src == '') {
61                 $path = $dest;
62         } else if (substr($dest, 0, strlen($src)) == $src) {
63                 $path = substr($dest, strlen($src) + 1);
64         } else {
65                 $path = '../' . $dest;
66         }
67
68         return $path;
69 }
70
71 // get the course total in Bytes 
72 $course_total = dirsize($current_path);
73
74 $framed = intval($_GET['framed']);
75 $popup = intval($_GET['popup']);
76
77 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
78         $get_file = 'get.php/';
79 } else {
80         $get_file = 'content/' . $_SESSION['course_id'] . '/';
81 }
82
83 echo '<p>'._AT('current_path').' ';
84
85 if ($pathext != '') {
86         echo '<a href="'.$_SERVER['PHP_SELF'].'?cid='.$cid. SEP . 'popup=' . $popup . SEP . 'framed=' . $framed .SEP. 'tab='.$current_tab.'">'._AT('home').'</a> ';
87 }
88 else {
89         echo _AT('home');
90 }
91
92 if ($pathext == '') {
93         $pathext = urlencode($_POST['pathext']);
94 }
95
96 if ($pathext != '') {
97         $bits = explode('/', $pathext);
98
99         foreach ($bits as $bit) {
100                 if ($bit != '') {
101                         $bit_path .= $bit . '/';
102                         echo ' / ';
103
104                         if ($bit_path == $pathext) {
105                                 echo $bit;
106                         } else {
107                                 echo '<a href="'.$_SERVER['PHP_SELF'].'?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed . '">' . $bit . '</a>';
108                         }
109                 }
110         }
111         $bit_path = "";
112         $bit = "";
113 }
114 echo '</p>';
115
116 if ($popup == TRUE) {
117         $totalcol = 6;
118 } else {
119         $totalcol = 5;
120 }
121 $labelcol = 3;
122
123 if (TRUE || $framed != TRUE) {
124
125         if ($_GET['overwrite'] != '') {
126                 // get file name, out of the full path
127                 $path_parts = pathinfo($current_path.$_GET['overwrite']);
128
129                 if (!file_exists($path_parts['dirname'].'/'.$pathext.$path_parts['basename'])
130                         || !file_exists($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5))) {
131                         /* source and/or destination does not exist */
132                         $msg->addErrors('CANNOT_OVERWRITE_FILE');
133                 } else {
134                         @unlink($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
135                         $result = @rename($path_parts['dirname'].'/'.$pathext.$path_parts['basename'], $path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
136
137                         if ($result) {
138                                 $msg->addFeedback('FILE_OVERWRITE');
139                         } else {
140                                 $msg->addErrors('CANNOT_OVERWRITE_FILE');
141                         }
142                 }
143         }
144         
145         echo '<fieldset><legend class="group_form">'._AT('add').'</legend>';
146         echo '<table cellspacing="1" cellpadding="0" border="0" summary="" align="center">';
147         echo '<tr><td colspan="2">';
148
149
150 if( $MakeDirOn ) {
151                 if ($depth < $MaxDirDepth) {
152                         echo '<input type="text" name="dirname" size="20" /> ';
153                         echo '<input type="hidden" name="mkdir_value" value="true" /> ';
154                         echo '<input type="submit" name="mkdir" value="'._AT('create_folder').'" class="button" />';
155                         echo '&nbsp;<small class="spacer">'._AT('keep_it_short').'</small>';
156                 } else {
157                         echo _AT('depth_reached');
158                 }
159         }
160         echo '<input type="hidden" name="pathext" value="'.$pathext.'" />';
161         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
162 //      echo '</form>';
163         echo '</td></tr>';
164
165         $my_MaxCourseSize = $system_courses[$_SESSION['course_id']]['max_quota'];
166
167         // upload file 
168         if (($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED) 
169                 || (($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) && ($course_total < $MaxCourseSize))
170                 || ($my_MaxCourseSize-$course_total > 0)) {
171                 echo '<tr><td  colspan="1">';
172                 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$my_MaxFileSize.'" />';
173                 echo '<input type="file" name="uploadedfile" id="uploadedfile" class="formfield" size="20" />';
174                 echo '<input type="submit" name="upload" value="'._AT('upload').'" class="button" />';
175                 echo '<input type="hidden" name="pathext" value="'.$pathext.'" />  ';
176                 echo '<input type="hidden" name="alter" value="TRUE" />  ';
177
178                 if ($popup == TRUE) {
179                         echo '<input type="hidden" name="popup" value="1" />';
180                 }
181                 echo '</td></tr></table></fieldset>';
182
183         } else {
184                 echo '</table>';
185                 echo '</fieldset>';
186                 $msg->printInfos('OVER_QUOTA');
187         }
188         echo '<br />';
189 }
190 // Directory and File listing 
191
192
193 echo '<input type="hidden" name="pathext" value ="'.$pathext.'" />';
194 ?>
195
196 <table class="data static" summary="" border="0" rules="groups" style="width: 90%">
197 <thead>
198 <tr>
199 <!--    <th scope="col"><input type="checkbox" name="checkall" onclick="Checkall(checkform);" id="selectall" title="<?php echo _AT('select_all'); ?>" /></th>
200 <th>&nbsp;</th>-->      
201         <th>&nbsp;</th>
202         <th>&nbsp;</th>
203         <th scope="col"><?php echo _AT('name');   ?></th>
204         
205 <!--    <th scope="col"><?php //echo _AT('date');   ?></th>
206         <th scope="col"><?php //echo _AT('size');   ?></th>-->
207 </tr>
208 </thead>
209
210 <tfoot>
211 <tr>
212         <td colspan="3" align="right">
213                 <?php echo '<input type="submit" name="add" value="'._AT('add').'" class="button"/>';?>
214                 
215         </td>
216 </tr>
217 </tfoot>
218
219 <?php
220
221 if($pathext) : ?>
222         <tr>
223                 <td colspan="3"><a href="<?php echo $_SERVER['PHP_SELF'].'?cid='.$cid. SEP .'back=1'.SEP.'pathext='.$pathext.SEP. 'popup=' . $popup .SEP. 'framed=' . $framed .SEP.'cp='.$_GET['cp'] . SEP. 'tab='.$current_tab; ?>"><img src="<?php echo$_base_href;?>images/arrowicon.gif" border="0" height="11" width="10" alt="" /> <?php echo _AT('back'); ?></a></td>
224         </tr>
225 <?php endif; ?>
226 <?php
227 $totalBytes = 0;
228
229 if ($dir == '')
230         $dir=opendir($current_path);
231
232 // loop through folder to get files and directory listing
233 while (false !== ($file = readdir($dir)) ) {
234
235         // if the name is not a directory 
236         if( ($file == '.') || ($file == '..') ) {
237                 continue;
238         }
239
240         // get some info about the file
241         $filedata = stat($current_path.$pathext.$file);
242         $path_parts = pathinfo($file);
243         $ext = strtolower($path_parts['extension']);
244
245         $is_dir = false;
246
247         // if it is a directory change the file name to a directory link 
248         if(is_dir($current_path.$pathext.$file)) {
249                 $size = dirsize($current_path.$pathext.$file.'/');
250                 $totalBytes += $size;
251                 $filename = '<a href="'.$_SERVER['PHP_SELF'].'?cid='.$cid.SEP.'pathext='.urlencode($pathext.$file.'/').SEP.'popup='.$popup.SEP.'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'tab='.$current_tab.SEP.'alternatives='.$_POST['alternatives'].'">'.$file.'</a>';
252                 $fileicon = '&nbsp;';
253                 $fileicon .= '<img src="'.$_base_href.'images/folder.gif" alt="'._AT('folder').':'.$file.'" height="18" width="20" class="img-size-fm1" />'."\n";
254                 $fileicon .= '&nbsp;';
255                 if(!$MakeDirOn) {
256                         $deletelink = '';
257                 }
258
259                 $is_dir = true;
260         } else if ($ext == 'zip') {
261
262                 $totalBytes += $filedata[7];
263                 $filename = $file;
264                 $fileicon = '&nbsp;<img src="'.$_base_href.'images/icon-zip.gif" alt="'._AT('zip_archive').':'.$file.'" height="16" width="16" border="0" class="img-size-fm2" />&nbsp;'."\n";
265
266         } else {
267                 $totalBytes += $filedata[7];
268                 $filename = $file;
269                 $fileicon = '&nbsp;<img src="'.$_base_href.'images/file_types/'.get_file_type_icon($filename).'.gif" height="16" width="16" alt="" title="" class="img-size-fm2" />&nbsp;'."\n";
270         } 
271         $file1 = strtolower($file);
272         // create listing for dirctor or file
273         if ($is_dir) {
274                 
275                 $dirs[$file1] .= '<tr><td>&nbsp;</td>'."\n";
276                 $dirs[$file1] .= '<td>'.$fileicon.'</td>'."\n";
277                 $dirs[$file1] .= '&nbsp;';
278                 $dirs[$file1] .= '<td>'.$filename.'</td>'."\n";
279                 $dirs[$file1] .= '</tr>'."\n";
280         
281         } else {
282                 if (!isset($cp))
283                 {
284                         // set content path $cp
285                         $sql = "SELECT content_path FROM ".TABLE_PREFIX."content WHERE content_id=".$cid;
286                         $result = mysql_query($sql, $db);
287                         $row = mysql_fetch_assoc($result);
288                         $cp = $row["content_path"];
289                 }
290                 
291                 $file_with_relative_path = get_relative_path($cp, $pathext.urlencode($file));
292                 
293                 $files[$file1] .= '<tr> <td  align="center">';
294                 $files[$file1] .= '<input type="radio" id="'.$file_with_relative_path.'" value="'.$file_with_relative_path.'" name="radio_alt"/> </td>'."\n";
295                 $files[$file1] .= '<td align="center">'.$fileicon.'</td>'."\n";
296
297                 $files[$file1] .= '<td ><label for="'.$file.'">&nbsp;';
298
299                 $files[$file1] .= '<a href="'.$file_with_relative_path.'">'.$file.'</a>';
300
301                 $files[$file1] .= '&nbsp;</label></td></tr>'."\n";
302                 
303         }
304 } // end while
305
306 // sort listing and output directories
307 if (is_array($dirs)) {
308         ksort($dirs, SORT_STRING);
309         foreach($dirs as $x => $y) {
310                 echo $y;
311         }
312 }
313
314 //sort listing and output files
315 if (is_array($files)) {
316         ksort($files, SORT_STRING);
317         foreach($files as $x => $y) {
318                 echo $y;
319         }
320 }
321
322 echo '</table>'."\n";
323
324 closedir($dir);
325
326 ?>
327 <script type="text/javascript">
328 //<!--
329
330 function openWindow(page) {
331         newWindow = window.open(page, "progWin", "width=400,height=200,toolbar=no,location=no");
332         newWindow.focus();
333 }
334 //-->
335 </script>
336