moved code up one level to eliminate the docs subdirectory
[acontent.git] / file_manager / filemanager_display.inc.php
1 <?php
2 /************************************************************************/
3 /* AContent                                                             */
4 /************************************************************************/
5 /* Copyright (c) 2010                                                   */
6 /* Inclusive Design Institute                                           */
7 /*                                                                      */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 if (!defined('TR_INCLUDE_PATH')) { exit; }
14 require_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
15
16 global $_course_id, $MaxCourseSize, $_content_id;
17
18 $editable_file_types = array('txt', 'html', 'htm', 'xml', 'css', 'asc', 'csv', 'sql');
19 $_SESSION['prefs']['PREF_CONTENT_EDITOR'] = 1;
20
21 $coursesDAO = new CoursesDAO();
22
23 function get_file_extension($file_name) {
24         $ext = pathinfo($file_name);
25         return $ext['extension'];
26 }
27
28 function get_file_type_icon($file_name) {
29         static $mime;
30
31         $ext = get_file_extension($file_name);
32
33         if (!isset($mime)) {
34                 require(TR_INCLUDE_PATH .'lib/mime.inc.php');
35         }
36
37         if (isset($mime[$ext]) && $mime[$ext][1]) {
38                 return $mime[$ext][1];
39         }
40         return 'generic';
41 }
42
43 function get_relative_path($src, $dest) {
44         if ($src == '') {
45                 $path = $dest;
46         } else if (substr($dest, 0, strlen($src)) == $src) {
47                 $path = substr($dest, strlen($src) + 1);
48         } else {
49                 $depth = substr_count($src, '/');
50                 for ($i = 0; $i < $depth + 1; $i++)  // $depth+1 because the last '/' is not recorded in content.content_path
51                         $path .= '../';
52                 $path .= $dest;
53         }
54
55         return $path;
56 }
57
58 // get the course total in Bytes 
59 $course_total = FileUtility::dirsize($current_path);
60
61 $framed = intval($_GET['framed']);
62 $popup = intval($_GET['popup']);
63 $cp = $_GET['cp'];
64 $cid = $_content_id;        // content id, used at "adapted content" page, => add/edit alternatives
65 $pid = intval($_GET['pid']);        // primary resource id, used at "adapted content" page, => add/edit alternatives
66 $a_type = intval($_GET['a_type']);  // alternative_type, used at "adapted content" page, => add/edit alternatives
67
68 if (defined('TR_FORCE_GET_FILE') && TR_FORCE_GET_FILE) {
69         $get_file = 'get.php/';
70 } else {
71         $get_file = 'content/' . $_course_id . '/';
72 }
73
74 function fm_path(){
75         global $pathext, $framed, $popup, $cp, $cid, $pid, $a_type, $_course_id;
76 echo '<p>'._AT('current_path').' ';
77
78 if (isset($pathext) && $pathext != '') {
79         echo '<a href="'.$_SERVER['PHP_SELF'].'?popup=' . $popup . SEP . 'framed=' . $framed.SEP . 'cp=' . $cp.SEP . '_cid=' . $cid.SEP . 'pid=' . $pid.SEP . 'a_type=' . $a_type.SEP.'_course_id='.$_course_id.'">'._AT('home').'</a> ';
80 }
81 else {
82         $pathext = '';
83         echo _AT('home');
84 }
85
86
87 if ($pathext == '' && isset($_POST['pathext'])) {
88
89         $pathext = urlencode($_POST['pathext']);
90 }
91
92 if ($pathext != '') {
93         $bits = explode('/', $pathext);
94
95         foreach ($bits as $bit) {
96                 if ($bit != '') {
97                         $bit_path .= $bit . '/';
98                         echo ' / ';
99
100                         if ($bit_path == $pathext) {
101                                 echo $bit;
102                         } else {
103                                 echo '<a href="'.$_SERVER['PHP_SELF'].'?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'_cid='.$cid.SEP.'a_type='.$a_type.SEP.'_course_id='.$_course_id.'">' . $bit . '</a>';
104                         }
105                 }
106         }
107         $bit_path = "";
108         $bit = "";
109 }
110 echo '</p>';
111
112 }
113
114 if ($popup == TRUE) {
115         $totalcol = 6;
116 } else {
117         $totalcol = 5;
118 }
119 $labelcol = 3;
120
121 if (TRUE || $framed != TRUE) {
122
123
124         if ($_GET['overwrite'] != '') {
125                 // get file name, out of the full path
126                 $path_parts = pathinfo($current_path.$_GET['overwrite']);
127
128                 if (!file_exists($path_parts['dirname'].'/'.$pathext.$path_parts['basename'])
129                         || !file_exists($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5))) {
130                         /* source and/or destination does not exist */
131                         $msg->addErrors('CANNOT_OVERWRITE_FILE');
132                 } else {
133                         @unlink($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
134                         $result = @rename($path_parts['dirname'].'/'.$pathext.$path_parts['basename'], $path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
135
136                         if ($result) {
137                                 $msg->addFeedback('FILE_OVERWRITE');
138                         } else {
139                                 $msg->addErrors('CANNOT_OVERWRITE_FILE');
140                         }
141                 }
142         }
143         
144         // filemanager listing table
145         // make new directory 
146         echo '<div class="input-form"><fieldset class="group_form"><legend class="group_form">'._AT('add_file_folder').'</legend>'."\n";
147         echo '  <div class="row">'."\n";
148         echo '          <form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext != '') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup='.$popup.SEP.'cp='.SEP.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">'."\n";
149         if( $MakeDirOn ) {
150                 if ($depth < $MaxDirDepth) {
151                         echo '          <label for="dirname">'._AT('create_folder_here').'</label><br />'."\n";
152                         echo '          &nbsp;<small class="spacer">'._AT('keep_it_short').'</small><br />'."\n";
153                         echo '          <input type="text" name="dirname" id="dirname" size="20" /> '."\n";
154                         echo '          <input type="hidden" name="mkdir_value" value="true" /> '."\n";
155                         echo '          <input type="submit" name="mkdir" value="'._AT('create_folder').'" class="button" />'."\n";
156                 } else {
157                         echo _AT('depth_reached')."\n";
158                 }
159         }
160         echo '          <input type="hidden" name="pathext" value="'.$pathext.'" />'."\n";
161         echo '          </form>'."\n";
162         echo '  </div>'."\n";
163
164         echo '  <div class="row"><hr /></div>'."\n";
165
166     // If flash is available, provide the option of using Fluid's uploader or the basic uploader
167         if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
168                 echo '<div class="row">'."\n";
169                 if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
170                         $fluid_on = 'checked="checked"';
171                 echo '(<input type="checkbox" id="fluid_on" name="fluid_on" value="yes" '.$fluid_on.' /> '."\n";
172                 echo '<label for="fluid_on" >'._AT('enable_uploader').'</label>)'."\n";
173                 echo '</div>'."\n";
174         }
175         // Create a new file
176         echo '  <div class="row" style="float: left;"><input type="button" class="button" name="new_file" value="' . _AT('file_manager_new') . '" onclick="window.location.href=\''.TR_BASE_HREF.'file_manager/new.php?pathext=' . urlencode($pathext) . SEP . 'framed=' . $framed . SEP . 'popup=' . $popup . SEP.'_course_id='.$_course_id. '\'"/></div>'."\n";
177
178     $course_row = $coursesDAO->get($_course_id);
179     if ($course_row['max_quota'] == '' || is_null($course_row['max_quota']))
180         $my_MaxCourseSize = $MaxCourseSize;
181
182         // upload file 
183         if ($course_row['max_quota'] == TR_COURSESIZE_UNLIMITED || $my_MaxCourseSize-$course_total > 0) {
184                 echo '  <div class="row" style="float: left;">'._AT('OR').'</div>'."\n".'       <div class="row" style="float: left;">'."\n";
185                 if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
186                 ?>
187                 <div id="uploader-error-container"></div>
188                         <div id="fluid-container">
189                                 <div id="uploader">
190                                 <!-- Basic upload controls, used when JavaScript is unavailable -->
191         <form method="post" enctype="multipart/form-data" class="fl-progEnhance-basic">
192             <p>Use the Browse button to add a file, and the Save button to upload it.</p>
193             <input name="fileData" type="file" />
194             <input type="hidden" name="_course_id" value ="<?php echo $_course_id; ?>" />
195             <input class="fl-uploader-basic-save" type="submit" value="Save"/>
196         </form>
197         
198         <!-- Uploader container -->
199         <form class="flc-uploader fl-uploader fl-progEnhance-enhanced" method="get" enctype="multipart/form-data">
200             
201             <!-- File Queue, which is split up into two separate tables: one for the header and body -->
202             <table class="fl-uploader-header">
203                         <tr>
204                                         <th class="fl-uploader-file-name">File Name</th>
205                                         <th class="fl-uploader-file-size">Size</th>
206                                         <th class="fl-uploader-file-actions"></th>
207                                 </tr>
208             </table>
209             
210             <!-- File Queue body, which is the default container for the FileQueueView component -->
211             <table summary="The list of files" class="flc-uploader-queue fl-uploader-queue">
212                                 <caption>File Upload Queue:</caption>
213                                 <tbody>
214                                         <!-- Template for file row -->
215                                         <tr class="flc-uploader-file-tmplt flc-uploader-file">
216                                                 <td class="flc-uploader-file-name fl-uploader-file-name">File Name Placeholder</td>
217                                                 <td class="flc-uploader-file-size fl-uploader-file-size">0 KB</td>
218                                                 <td class="fl-uploader-file-actions">
219                                                         <button type="button" class="flc-uploader-file-action" tabindex="-1"></button>
220                                                 </td>
221                                         </tr>
222                                         
223                                         <!-- Template for error info row -->
224                                         <tr class="flc-uploader-file-error-tmplt fl-uploader-file-error">
225                                                 <td colspan="3" class="flc-uploader-file-error"></td>
226                                         </tr>
227                                 </tbody>
228                         </table>
229             
230             <!-- File progress bar template, used to generate progress bars for each file in the queue -->
231             <div class="flc-uploader-file-progressor-tmplt fl-uploader-file-progress"></div>            
232
233             <!-- Initial instructions -->
234             <div class="flc-uploader-browse-instructions fl-uploader-browse-instructions">
235                 Choose <em>Browse files</em> to add files to the queue. 
236             </div>            
237
238             <!-- Status footer -->
239             <div class="flc-uploader-queue-footer fl-uploader-queue-footer fl-fix">
240                 <div class="flc-uploader-total-progress-text fl-uploader-total-progress-text fl-force-left">
241                     Total: 0 files (0 KB)
242                 </div>
243                 <div class="fl-text-align-right fl-force-right">
244                     <span class="flc-uploader-button-browse fl-uploader-browse">
245                         <span class="flc-uploader-button-browse-text">Browse files</span>
246                     </span>
247                 </div>
248                 <!-- Total progress bar -->
249                 <div class="flc-uploader-total-progress fl-uploader-total-progress-okay"></div>
250                 <div class="flc-uploader-errorsPanel fl-uploader-errorsPanel">
251                      <div class="fl-uploader-errorsPanel-header"><span class="flc-uploader-errorPanel-header">Warnings:</span></div>
252     
253                      <!-- The markup for each error section will be rendered into these containers. -->
254                      <div class="flc-uploader-errorPanel-section-fileSize"></div>
255                      <div class="flc-uploader-errorPanel-section-numFiles"></div>
256                      
257                      <!-- Error section template.-->
258                      <div class="flc-uploader-errorPanel-section-tmplt fl-uploader-hidden-templates">
259                          <div class="flc-uploader-errorPanel-section-title fl-uploader-errorPanel-section-title">
260                              x files were too y and were not added to the queue.
261                          </div>
262                          
263                          <div class="flc-uploader-errorPanel-section-details fl-uploader-errorPanel-section-details">
264                              <p>The following files were not added:</p>
265                              <p class="flc-uploader-errorPanel-section-files">file_1, file_2, file_3, file_4, file_5 </p>
266                          </div>
267                          
268                          <button type="button" class="flc-uploader-errorPanel-section-toggleDetails fl-uploader-errorPanel-section-toggleDetails">Hide this list</button>
269                          <button type="button" class="flc-uploader-errorPanel-section-removeButton fl-uploader-errorPanel-section-removeButton">
270                              <span class="flc-uploader-erroredButton-text fl-uploader-hidden">Remove error</span>
271                          </button>
272                      </div>
273                  </div>                
274             </div>
275             
276             <!-- Upload buttons -->
277             <div class="fl-uploader-buttons">
278                 <button type="button" class="flc-uploader-button-pause fl-uploader-button-stop fl-uploader-hidden">Stop Upload</button>
279                 <button type="button" class="flc-uploader-button-upload fl-uploader-button-upload fl-uploader-dim">Upload</button>
280             </div>
281             
282             <div class="flc-uploader-status-region fl-offScreen-hidden"></div>
283         </form>        
284             
285         <script type="text/javascript">
286             var myUploader = fluid.uploader(".flc-uploader", {
287                 queueSettings: {
288                     uploadURL: '<?php echo TR_BASE_HREF; ?>file_manager/upload.php',
289                     fileUploadLimit: 5,
290                     fileQueueLimit: 2,
291                     postParams: {pathext: '<?php echo $pathext; ?>', type: 'ajax', submit: 'submit', _course_id: '<?php echo $_course_id; ?>'},
292                     fileSizeLimit: <?php echo $my_MaxCourseSize/1024; ?>
293                 },
294                 events: {
295                     onSuccess: {
296                         event: "onFileSuccess",
297                         args: [
298                             {
299                                 fileName: "{arguments}.0.name",
300                                 responseText: "{arguments}.1"
301                             }
302                         ]
303                     },
304                     onError: {
305                         event: "onFileError",
306                         args: [
307                             {
308                                 fileName: "{arguments}.0.name",
309                                 statusCode: "{arguments}.2",
310                                 responseText: "{arguments}.3.responseText"
311                             }
312                         ]
313                     }
314                 },
315                 listeners: {
316                         onSuccess: function (response){
317                                 // example assumes that the server code passes the new image URL in the serverData
318                                 console.log("Success triggered", response);
319                                 jQuery('#uploader-error-container').html(response.responseText);
320                         }, 
321                         onError: function(response) {
322                         console.log("Error triggered", response);
323                         jQuery('#uploader-error-container').html(response.responseText);
324                     },
325                     onUploadStart: function() {
326                         jQuery('#uploader-error-container').html("");
327                     },
328                     afterUploadComplete: function () {
329                         window.location = "<?php echo TR_BASE_HREF . 'file_manager/index.php?popup='.$popup.SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.SEP.'_course_id='.$_course_id; ?>"
330                     }
331                     },
332                     components: {
333                     strategy: {
334                         options: {
335                             flashMovieSettings: {
336                                 flashURL: "<?php echo TR_BASE_HREF; ?>include/jscripts/infusion/lib/swfupload/flash/swfupload.swf",
337                                 flashButtonImageURL: "<?php echo TR_BASE_HREF; ?>include/jscripts/infusion/components/uploader/images/browse.png"
338                             }
339                         }
340                     }
341                 }
342             });
343             
344             //bind fluid checkbox
345             jQuery('#fluid_on').bind("click", function() {
346                 toggleform('simple-container', 'fluid-container'); 
347                 setCheckboxCookie(this, 'fluid_on=yes', 'fluid_on=no','December 31, 2099');
348                 console.log('hey');
349             });
350             
351             //hide multifile uploader if it's not checked 
352             if (!jQuery('#fluid_on').attr('checked')) {
353                 jQuery('#fluid-container').hide();
354             }
355         </script>
356                                 </div>
357                         </div>
358                 <?php
359                 if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
360                                 echo '<div id="simple-container" style="display: none;">';
361                         else
362                                 echo '<div id="simple-container">';
363                 } else {
364                         // Display as regular if there's no Flash detected
365                         echo '<div id="simple-container">'."\n";
366                 }
367
368                 // Simple single file uploader
369                 echo '<form onsubmit="openWindow(\''.TR_BASE_HREF.'home/prog.php\');" name="form1" method="post" action="file_manager/upload.php?popup='.$popup.SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'" enctype="multipart/form-data">';
370                 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$my_MaxFileSize.'" />';
371                 
372                 echo '<label for="uploadedfile">'._AT('upload_files').'</label><br />'."\n";
373                 echo '<input type="file" name="uploadedfile" id="uploadedfile" class="formfield" size="20" /> ';
374                 echo '<input type="submit" name="submit" value="'._AT('upload').'" class="button" />';
375                 echo '<input type="hidden" name="_course_id" value ="'.$_course_id.'" />';
376                 echo '<input type="hidden" name="pathext" value="'.$pathext.'" />  ';
377
378                 if ($popup == TRUE) {
379                         echo '<input type="hidden" name="popup" value="1" />';
380                 }
381                 echo '</form>';
382                 echo '</div>';
383                 echo '          </div>'."\n".'  </fieldset></div>';
384         } else {
385                 echo '  </fieldset></div>'."\n";
386                 $msg->printInfos('OVER_QUOTA');
387         }
388 }
389
390
391 // Directory and File listing 
392 echo '<form name="checkform" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : '').'popup='.$popup .SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'_cid='.$cid.SEP.'a_type='.$a_type.'" method="post">';
393 echo '<input type="hidden" name="pathext" value ="'.$pathext.'" />';
394 echo '<input type="hidden" name="_course_id" value ="'.$_course_id.'" />';
395 if ($cid > 0) echo '<input type="hidden" name="_cid" value ="'.$cid.'" />';
396
397 // display the section to use a remote URL as an alternative
398 if ($a_type > 0) {
399 ?>
400 <div class="input-form" style="min-height:10px">
401 <fieldset class="group_form" style="min-height: 0px;"><legend class="group_form"><?php echo _AT('use_url_as_alternative'); ?></legend>
402         <div class="row">
403           <input name="remote_alternative" id="remote_alternative" value="http://" size="60" />
404           <input class="button" type="button" name="alternative" value="<?php echo _AT('use_as_alternative'); ?>" onclick="javascript: setURLAlternative();" />
405         </div>
406 </fieldset>
407 </div>
408 <?php }?>
409
410 <table class="data static" summary="" border="0" rules="groups" style="width: 90%">
411 <thead>
412 <tr>
413 <td colspan="5">
414 <?php fm_path(); ?>
415 </td>
416 </tr>
417 <tr>
418         <th scope="col"><input type="checkbox" name="checkall" onclick="Checkall(checkform);" id="selectall" title="<?php echo _AT('select_all'); ?>" /></th>
419         <th>&nbsp;</th>
420         <th scope="col"><?php echo _AT('name');   ?></th>
421         <th scope="col"><?php echo _AT('date');   ?></th>
422         <th scope="col"><?php echo _AT('size');   ?></th>
423 </tr>
424 </thead>
425 <tfoot>
426 <tr>
427         <td colspan="5"><input type="submit" name="rename" value="<?php echo _AT('rename'); ?>" /> 
428                 <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>" /> 
429                 <input type="submit" name="move"   value="<?php echo _AT('move'); ?>" /></td>
430 </tr>
431
432 <tr>
433         <td colspan="4" align="right"><strong><?php echo _AT('directory_total'); ?>:</strong></td>
434         <td align="right">&nbsp;<strong><?php echo FileUtility::get_human_size(FileUtility::dirsize($current_path.$pathext.$file.'/')); ?></strong>&nbsp;</td>
435 </tr>
436
437 <tr>
438         <td colspan="4" align="right"><strong><?php echo _AT('course_total'); ?>:</strong></td>
439         <td align="right">&nbsp;<strong><?php echo FileUtility::get_human_size($course_total); ?></strong>&nbsp;</td>
440 </tr>
441 <tr>
442         <td colspan="4" align="right"><strong><?php echo _AT('course_available'); ?>:</strong></td>
443         <td align="right"><strong><?php
444                 if ($my_MaxCourseSize == TR_COURSESIZE_UNLIMITED) {
445                         echo _AT('unlimited');
446                 } else if ($my_MaxCourseSize == TR_COURSESIZE_DEFAULT) {
447                         echo FileUtility::get_human_size($MaxCourseSize-$course_total);
448                 } else {
449                         echo FileUtility::get_human_size($my_MaxCourseSize-$course_total);
450                 } ?></strong>&nbsp;</td>
451 </tr>
452 </tfoot>
453 <?php
454
455
456 if($pathext) : ?>
457         <tr>
458                 <td colspan="5"><a href="<?php echo $_SERVER['PHP_SELF'].'?back=1'.SEP.'pathext='.$pathext.SEP. 'popup=' . $popup .SEP. 'framed=' . $framed .SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'_cid='.$cid.SEP.'a_type='.$a_type.SEP.'_course_id='.$_course_id; ?>"><img src="images/arrowicon.gif" border="0" height="11" width="10" alt="" /> <?php echo _AT('back'); ?></a></td>
459         </tr>
460 <?php endif; ?>
461 <?php
462 $totalBytes = 0;
463
464 if ($dir == '')
465         $dir=opendir($current_path);
466         
467 // loop through folder to get files and directory listing
468 while (false !== ($file = readdir($dir)) ) {
469
470         // if the name is not a directory 
471         if( ($file == '.') || ($file == '..') ) {
472                 continue;
473         }
474
475         // get some info about the file
476         $filedata = stat($current_path.$pathext.$file);
477         $path_parts = pathinfo($file);
478         $ext = strtolower($path_parts['extension']);
479
480         $is_dir = false;
481
482         // if it is a directory change the file name to a directory link 
483         if(is_dir($current_path.$pathext.$file)) {
484                 $size = FileUtility::dirsize($current_path.$pathext.$file.'/');
485                 $totalBytes += $size;
486                 $filename = '<a href="'.$_SERVER['PHP_SELF'].'?pathext='.urlencode($pathext.$file.'/'). SEP . 'popup=' . $popup . SEP . 'framed='. $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'_cid='.$cid.SEP.'a_type='.$a_type.SEP.'_course_id='.$_course_id.'">'.$file.'</a>';
487                 $fileicon = '&nbsp;';
488                 $fileicon .= '<img src="images/folder.gif" alt="'._AT('folder').':'.$file.'" height="18" width="20" class="img-size-fm1" />';
489                 $fileicon .= '&nbsp;';
490                 if(!$MakeDirOn) {
491                         $deletelink = '';
492                 }
493
494                 $is_dir = true;
495         } else if ($ext == 'zip') {
496
497                 $totalBytes += $filedata[7];
498                 $filename = $file;
499                 $fileicon = '&nbsp;<img src="images/icon-zip.gif" alt="'._AT('zip_archive').':'.$file.'" height="16" width="16" border="0" class="img-size-fm2" />&nbsp;';
500
501         } else {
502                 $totalBytes += $filedata[7];
503                 $filename = $file;
504                 $fileicon = '&nbsp;<img src="images/file_types/'.get_file_type_icon($filename).'.gif" height="16" width="16" alt="" title="" class="img-size-fm2" />&nbsp;';
505         } 
506         $file1 = strtolower($file);
507         // create listing for dirctor or file
508         if ($is_dir) {
509                 
510                 $dirs[$file1] .= '<tr><td  align="center" width="0%">';
511                 $dirs[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/></td>';
512                 $dirs[$file1] .= '<td  align="center"><label for="'.$file.'" >'.$fileicon.'</label></td>';
513                 $dirs[$file1] .= '<td >&nbsp;';
514                 $dirs[$file1] .= $filename.'</td>';
515                 $dirs[$file1] .= '<td  align="right">&nbsp;';
516                 $dirs[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], TR_DATE_UNIX_TIMESTAMP);
517                 $dirs[$file1] .= '&nbsp;</td>';
518                 $dirs[$file1] .= '<td  align="right">';
519                 $dirs[$file1] .= FileUtility::get_human_size($size).'</td></tr>';
520
521                 
522         } else {
523                 $files[$file1] .= '<tr> <td  align="center">';
524                 $files[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/> </td>';
525                 $files[$file1] .= '<td  align="center"><label for="'.$file.'">'.$fileicon.'</label></td>';
526                 $files[$file1] .= '<td >&nbsp;';
527
528                 if ($framed) {
529                         $files[$file1] .= '<a href="'.$get_file.$pathext.urlencode($filename).'">'.$filename.'</a>';
530                 } else {
531                         $files[$file1] .= '<a href="file_manager/preview.php?file='.$pathext.$filename.SEP.'pathext='.urlencode($pathext).SEP.'popup='.$popup.SEP.'_course_id='.$_course_id.'">'.$filename.'</a>';
532                 }
533
534                 if ($ext == 'zip') {
535                         $files[$file1] .= ' <a href="file_manager/zip.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'file=' . urlencode($file) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed .SEP.'_course_id='.$_course_id.'">';
536                         $files[$file1] .= '<img src="images/archive.gif" border="0" alt="'._AT('extract_archive').'" title="'._AT('extract_archive').'"height="16" width="11" class="img-size-fm3" />';
537                         $files[$file1] .= '</a>';
538                 }
539
540                 if (in_array($ext, $editable_file_types)) {
541                         $files[$file1] .= ' <a href="file_manager/edit.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup=' . $popup . SEP . 'framed=' . $framed . SEP.'_course_id='.$_course_id. SEP . 'file=' . urlencode($file) . '">';
542                         $files[$file1] .= '<img src="images/edit.gif" border="0" alt="'._AT('edit').'" title="'._AT('edit').'" height="15" width="18" class="img-size-fm4" />';
543                         $files[$file1] .= '</a>';
544                 }
545
546                 $files[$file1] .= '&nbsp;</td>';
547
548                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
549
550                 if ($popup == TRUE) {
551                         if ($a_type > 0)  // define content alternative
552                         {
553                                 $files[$file1] .= '<input class="button" type="button" name="alternative" value="' ._AT('use_as_alternative') . '" onclick="javascript: setAlternative(\''.get_relative_path($_GET['cp'], $pathext).$file.'\', \''.TR_BASE_HREF.$get_file.$pathext.urlencode($file).'\', \''.$cid.'\', \''.$pid.'\', \''.$a_type.'\');" />&nbsp;';
554                         }
555                         else
556                                 $files[$file1] .= '<input class="button" type="button" name="insert" value="' ._AT('insert') . '" onclick="javascript:insertFile(\'' . $file . '\', \'' . get_relative_path($_GET['cp'], $pathext) . '\', \'' . $ext . '\', \'' .$_SESSION['prefs']['PREF_CONTENT_EDITOR']. '\');" />&nbsp;';
557                 }
558
559                 $files[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], TR_DATE_UNIX_TIMESTAMP);
560                 $files[$file1] .= '&nbsp;</td>';
561                 
562                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
563                 $files[$file1] .= FileUtility::get_human_size($filedata[7]).'</td></tr>';
564         }
565 } // end while
566
567 // sort listing and output directories
568 if (is_array($dirs)) {
569         ksort($dirs, SORT_STRING);
570         foreach($dirs as $x => $y) {
571                 echo $y;
572         }
573 }
574
575 //sort listing and output files
576 if (is_array($files)) {
577         ksort($files, SORT_STRING);
578         foreach($files as $x => $y) {
579                 echo $y;
580         }
581 }
582
583 echo '</table></form>';
584 ?>
585
586 <script type="text/javascript">
587 //<!--
588 function insertFile(fileName, pathTo, ext, ed_pref) { 
589         // pathTo + fileName should be relative to current path (specified by the Content Package Path)
590         if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "png") {
591                 var info = "<?php echo _AT('alternate_text'); ?>";
592                 var html = '<img src="' + pathTo+fileName + '" border="0" alt="' + info + '" />';
593         } else if (ext == "mpg" || ext == "avi" || ext == "wmv" || ext == "mov" || ext == "swf" || ext == "mp3" || ext == "wav" || ext == "ogg" || ext == "mid" ||ext == "flv"|| ext == "mp4") {
594                 var html = '[media]'+ pathTo + fileName + '[/media]';
595         } else {
596                 var info = "<?php echo _AT('put_link'); ?>";
597                 var html = '<a href="' + pathTo+fileName + '">' + info + '</a>';
598         }
599         insertLink(html);
600 //      insertLink(html, ed_pref);
601 }
602
603 //function insertLink(html, ed_pref)
604 function insertLink(html)
605 {
606 //    if (window.opener) {
607 //        var isNotVisual = window.opener.document.form.html.checked;
608 //    }
609
610         if (!window.opener || $('#html:checked').val() !== null) {
611                 if (!window.opener && window.parent.tinyMCE)
612                         window.parent.tinyMCE.execCommand('mceInsertContent', false, html);
613                 else
614                         if (window.opener && window.opener.tinyMCE)
615                                 window.opener.tinyMCE.execCommand('mceInsertContent', false, html);
616         } else {
617                         insertAtCursor(window.opener.document.form.body_text, html);
618         }
619 }
620
621 function insertAtCursor(myField, myValue) {
622         //IE support
623         if (window.opener.document.selection) {
624                 myField.focus();
625                 sel = window.opener.document.selection.createRange();
626                 sel.text = myValue;
627         }
628         //MOZILLA/NETSCAPE support
629         else if (myField.selectionStart || myField.selectionStart == '0') {
630                 var startPos = myField.selectionStart;
631                 var endPos = myField.selectionEnd;
632                 myField.value = myField.value.substring(0, startPos)
633                 + myValue
634                 + myField.value.substring(endPos, myField.value.length);
635                 myField.focus();
636         } else {
637                 myField.value += myValue;
638                 myField.focus();
639         }
640 }
641
642 // This function does:
643 // 1. save into db via ajax
644 // 2. set the according field in opener window to the selected file
645 // 3. close file manager itself
646 function setAlternative(file, file_preview_link, cid, pid, a_type) {
647         jQuery.post("<?php echo TR_BASE_HREF; ?>home/editor/save_alternative.php", 
648                         {"pid":pid, "a_type":a_type, "alternative":file}, 
649                         function(data) {});
650
651         link_html = '\
652       <a href="'+file_preview_link+'" title="<?php echo _AT('new_window'); ?>" target="_new">'+file+'</a><br /> \
653       <a href="#" onclick="trans.utility.poptastic(\\\'<?php echo TR_BASE_HREF; ?>file_manager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>cp=<?php echo $cp.SEP; ?>_cid='+cid+'<?php echo SEP; ?>pid='+pid+'<?php echo SEP; ?>a_type='+a_type+'\\\');return false;" title="<?php echo _AT('new_window'); ?>"> \
654         <img src="<?php echo TR_BASE_HREF; ?>images/alter.png" border="0" title="<?php echo _AT('alter'); ?>" alt="<?php echo _AT('alter'); ?>" /> \
655       </a> \
656       <a href="#" onclick="removeAlternative(\\\'<?php echo $cp; ?>\\\', '+cid+','+pid+','+a_type+');return false;"> \
657         <img src="<?php echo TR_BASE_HREF; ?>images/remove.gif" border="0" title="<?php echo _AT('remove'); ?>" alt="<?php echo _AT('remove'); ?>" /> \
658       </a> \
659     </div> \
660 ';
661         eval("window.opener.document.getElementById(\""+pid+"_"+a_type+"\").innerHTML = '"+link_html+"'");
662         
663         window.close();
664 }
665
666 // This function validates the url then call setAlternative()
667 function setURLAlternative() {
668         remote_url = jQuery('#remote_alternative').val();
669         if (remote_url == '' || remote_url == 'http://') {
670                 alert("<?php echo _AT('empty_url'); ?>");
671                 return false;
672         }
673         setAlternative(remote_url, remote_url, '<?php echo $cid; ?>.', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
674 }
675
676 <?php  if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") { ?>
677 // set a cookie
678 function setCheckboxCookie(obj, value1, value2, date)
679 {
680         var today = new Date();
681         var the_date = new Date(date);
682         var the_cookie_date = the_date.toGMTString();
683         if (obj.checked==true)
684                 var the_cookie = value1 + ";expires=" + the_cookie_date;
685         else
686                 var the_cookie = value2 + ";expires=" + the_cookie_date;
687         document.cookie = the_cookie;
688 }
689
690 // toggle the view between div object and button
691 function toggleform(id, link) {
692         var obj = document.getElementById(id);
693         var btn = document.getElementById(link);
694
695         if (obj.style.display == "none") {
696                 //show
697                 obj.style.display='';
698                 obj.focus();
699
700                 btn.style.display = 'none';
701
702
703         } else {
704                 //hide
705                 obj.style.display='none';
706                 btn.style.display = '';
707         }
708 }
709 <?php } ?>
710
711 <?php 
712 // When uploading a file as an alternative content, set the alternative field in the opener window 
713 // and close "file manager" once the upload is successful
714 if ($a_type > 0 && isset($_GET['uploadfile']) && $_GET['uploadfile'] <> '') { ?>
715 function setAlternativeAndClose() {
716         setAlternative('<?php echo get_relative_path($_GET['cp'], $pathext).$_GET['uploadfile']; ?>', '<?php echo TR_BASE_HREF.$get_file.$pathext.urlencode($_GET['uploadfile']); ?>', '<?php echo $_content_id; ?>', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
717         window.close();
718 }
719
720 window.onload=setAlternativeAndClose;
721 <?php } ?>
722
723 //-->
724 </script>