AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / file_manager / zip.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 $page = 'file_manager';
14
15 define('TR_INCLUDE_PATH', '../include/');
16 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
17 require_once(TR_INCLUDE_PATH.'lib/pclzip.lib.php');
18 require_once(TR_INCLUDE_PATH.'lib/pclzip_callback.lib.php');
19 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
20 require_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
21
22 global $_course_id;
23 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
24 $coursesDAO = new CoursesDAO();
25
26 $popup = $_REQUEST['popup'];
27 $framed = $_REQUEST['framed'];
28
29 if (isset($_POST['cancel'])) {
30         $msg->addFeedback('CANCELLED');
31         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_POST['popup'].SEP.'framed='.$_POST['framed'].SEP.'_course_id='.$_course_id);
32         exit;
33 }
34
35         $path = TR_CONTENT_DIR . $_course_id.'/';
36
37         if ($_REQUEST['pathext'] != '') {
38                 $pathext = $_REQUEST['pathext'];
39         }
40         if ($_REQUEST['file'] != '') {
41                 $file = $_REQUEST['file'];
42         }
43
44         if (strpos($file, '..') !== false) {
45                 require(TR_INCLUDE_PATH.'header.inc.php');
46                 $msg->printErrors('UNKNOWN');
47                 require(TR_INCLUDE_PATH.'footer.inc.php');
48                 exit;
49         }
50
51         $path_parts = pathinfo($pathext.$file);
52
53         $temp_name = substr($file, 0, -strlen('.'.$path_parts['extension']));
54
55         $zip = new PclZip($path.$pathext.$file);
56
57         if (($list = $zip->listContent()) == 0) {
58                 die("Error : ".$zip->errorInfo(true));
59         }
60
61 /*****************************************************************/
62         $totalBytes = 0;
63         $translated_file_names = array();
64
65         for ($i=0; $i<sizeof($list); $i++) {
66                 $path_parts = pathinfo($list[$i]['stored_filename']);
67                 if ($path_parts['dirname'] == '.') {
68                         $path_parts['dirname'] = '';
69                 } else {
70                         $path_parts['dirname'] .= '/';
71                 }
72                 $ext = $path_parts['extension'];
73
74                 $is_dir = false;
75                 if($list[$i]['folder']) {
76
77                         $filename = $list[$i]['stored_filename'];
78                         $fileicon = '<img src="images/folder.gif" alt="'._AT('folder').'" />';
79
80                         $is_dir = true;
81
82                 } else if ($ext == 'zip') {
83
84                         $totalBytes += $list[$i]['size'];
85                         $filename = $list[$i]['stored_filename'];
86                         $fileicon = '<img src="images/icon-zip.gif" alt="'._AT('zip_archive').'" height="16" width="16" border="0" />';
87
88                 } else {
89                         $totalBytes += $list[$i]['size'];
90                         $filename = $list[$i]['stored_filename'];
91                         $fileicon = '<img src="images/icon_minipost.gif" alt="'._AT('file').'" height="11" width="16" />';
92                 }
93                 
94                 if ($is_dir) {
95                         $dirs[strtolower($filename)] .= '<tr>
96                                 <td>'.$filename.'</td>';
97
98                                 $dirs[strtolower($filename)] .= '<td class="row1" align="right">'.FileUtility::get_human_size($list[$i]['size']).' </td>';
99                                 $dirs[strtolower($filename)] .= '<td class="row1">&nbsp;';
100                                 
101                                 $dirs[strtolower($filename)] .= AT_date(_AT('filemanager_date_format'), $filedata[10], TR_DATE_UNIX_TIMESTAMP);
102                                         
103                                 $dirs[strtolower($filename)] .= '&nbsp;</td>';
104
105                                 $dirs[strtolower($filename)] .= '</tr>';
106                 } else {
107
108                         $files[strtolower($filename)] .= '<tr>
109                                 <td>';
110
111                                 if (in_array($ext, $IllegalExtentions)) {
112                                         $files[strtolower($filename)] .=  '<span style="text-decoration: line-through;" title="'._AT('illegal_file').'">'.$filename.'</span>';
113                                 } else {
114                                         $files[strtolower($filename)] .= $filename;
115                                         
116                                         $trans_name = str_replace(' ', '_', $path_parts['basename']);
117                                         $trans_name = preg_replace("/[^A-Za-z0-9._\-]/", '', $trans_name);
118
119                                         if (in_array($path_parts['dirname'].$trans_name, $translated_file_names)) {
120                                                 $trans_count = 2;
121                                                 while (in_array($trans_name, $translated_file_names)) {
122                                                         $part = substr($trans_name, 0, -strlen($ext)- 1 - (2*($trans_count-2)));
123                                                         $trans_name = $part.'_'.$trans_count.'.'.$ext;
124                                                         $trans_count++;
125                                                         if ($trans_count>15){
126                                                                 exit; // INF loop safety thing..
127                                                         }
128                                                 }
129                                         }
130                                         
131                                         $translated_file_names[$list[$i]['index']] = $path_parts['dirname'].$trans_name;
132
133                                         if ($path_parts['dirname'].$trans_name != $filename) {
134                                                 $files[strtolower($filename)] .= ' => '.$trans_name;
135                                         }
136                                         
137                                 }
138                                         
139                                 $files[strtolower($filename)] .= '</td>';
140
141                                 $files[strtolower($filename)] .= '<td align="right">'.FileUtility::get_human_size($list[$i]['size']).' </td>';
142                                 $files[strtolower($filename)] .= '<td>&nbsp;';
143                                 
144                                 $files[strtolower($filename)] .= AT_date(_AT('filemanager_date_format'), $list[$i]['mtime'], TR_DATE_UNIX_TIMESTAMP);
145                                         
146                                 $files[strtolower($filename)] .= '</td>';
147                 
148                                 $files[strtolower($filename)] .= '</tr>';
149                 }
150         }
151
152         $row = $coursesDAO->get($_course_id);
153         $my_MaxCourseSize       = $row['max_quota'];
154         $my_MaxFileSize     = $row['max_file_size'];
155
156         $course_total = FileUtility::dirsize($path);
157         if ($my_MaxCourseSize == TR_COURSESIZE_UNLIMITED) {
158                 $total_after = 1;
159         } else {
160                 $my_MaxCourseSize = $MaxCourseSize;
161                 $total_after = FileUtility::get_human_size($my_MaxCourseSize-$course_total-$totalBytes);
162         }
163 //      else{
164 //              $total_after = get_human_size($my_MaxCourseSize - $course_total - $totalBytes);
165 //      }
166
167         // if $total_after < 0: redirect with error msg
168
169         if (isset($_POST['submit']) && ($total_after > 0)) {
170                 $_POST['custom_path'] = trim($_POST['custom_path']);
171                 $_POST['custom_path'] = str_replace(' ', '_', $_POST['custom_path']);
172
173                 /* anything else should be okay, since we're on *nix.. hopefully */
174                 $_POST['custom_path'] = preg_replace('/[^a-zA-Z0-9._\/]/', '', $_POST['custom_path']);
175
176                 if (strpos($_POST['pathext'].$_POST['custom_path'], '..') !== false) {
177                         $msg->addError('UNKNOWN');
178                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'framed='.$_POST['framed'].SEP.'popup='.$_POST['popup'].SEP.'_course_id='.$_course_id);
179                         exit;
180                 } else if ($zip->extract(       PCLZIP_OPT_PATH,                $path. $_POST['pathext'] . $_POST['custom_path'],  
181                                                         PCLZIP_CB_PRE_EXTRACT,  'preExtractCallBack')                   == 0) {
182
183                         echo ("Error : ".$zip->errorInfo(true));
184                 } else {
185                         $msg->addFeedback('ARCHIVE_EXTRACTED');
186                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_POST['popup'].SEP.'framed='.$_POST['framed'].SEP.'_course_id='.$_course_id);
187                         exit;
188                 }
189
190                 header('Location: index.php'.SEP.'_course_id='.$_course_id);
191                 exit;
192         }
193
194         require(TR_INCLUDE_PATH.'header.inc.php');
195
196         if ($total_after <= 0) {
197                 $msg->printErrors('NO_SPACE_LEFT');
198         } else {
199 ?>
200                 <form method="post" action="file_manager/zip.php">
201                 <input type="hidden" name="pathext" value="<?php echo AT_print($_GET['pathext'], 'input.hidden'); ?>" />
202                 <input type="hidden" name="file"    value="<?php echo AT_print($_GET['file'], 'input.hidden'); ?>" />
203                 <input type="hidden" name="popup"   value="<?php echo AT_print($_GET['popup'], 'input.hidden'); ?>" />
204                 <input type="hidden" name="framed"   value="<?php echo AT_print($_GET['framed'], 'input.hidden'); ?>" />
205                 <input type="hidden" name="_course_id" value="<?php echo $_course_id; ?>" />
206                 <div class="input-form">
207                         <div class="row">
208                                 <p><?php echo _AT('zip_illegal_contents'); ?></p>
209                                 <p><?php echo _AT('extract_tip'); ?></p>
210                         </div>
211
212                         <div class="row">
213                                 <span class="required" title="<?php echo _AT('required_field'); ?>">*</span><?php echo _AT('directory_name'); ?><br />
214                                 <input type="text" name="custom_path" value="<?php echo AT_print($temp_name, 'input.text'); ?>" />
215                         </div>
216
217                         <div class="row buttons">
218                                 <input type="submit" name="submit" value="<?php echo _AT('extract'); ?>" /> 
219                                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
220                         </div>
221                 </div>
222                 </form>
223 <?php
224         } // end if
225 ?>
226
227 <table class="data static" summary="" rules="groups">
228 <thead>
229 <tr>
230         <th><?php echo _AT('name'); ?></th>
231         <th><?php echo _AT('size'); ?></th>
232         <th><?php echo _AT('date'); ?></th>
233 </tr>
234 </thead>
235 <tbody>
236         <?php
237         if (is_array($dirs)) {
238                 foreach($dirs as $x => $y) {
239                         echo $y;
240                 }
241         }
242
243         if (is_array($files)) {
244                 foreach($files as $x => $y) {
245                         echo $y;
246                 }
247         }
248 ?>
249 </tbody>
250 <tfoot>
251 <tr>
252         <td align="right"><?php echo _AT('archive_total'); ?>:</td>
253         <td align="right"><?php echo FileUtility::get_human_size($totalBytes); ?></td>
254         <td>&nbsp;</td>
255 </tr>
256 <tr>
257         <td align="right"><?php echo _AT('course_total_zip'); ?>:</td>
258         <td align="right"><?php echo FileUtility::get_human_size($course_total); ?></td>
259         <td>&nbsp;</td>
260 </tr>
261 <tr>
262         <td align="right"><?php echo _AT('course_available_zip1'); ?>:</td>
263         <td align="right"><?php
264                         if ($my_MaxCourseSize == TR_COURSESIZE_UNLIMITED) {
265                                 echo _AT('unlimited');
266                         } else {
267                                 echo FileUtility::get_human_size($my_MaxCourseSize-$course_total);
268                         } ?></td>
269         <td>&nbsp;</td>
270 </tr>
271 <tr>
272         <td align="right"><?php echo _AT('course_available_zip2'); ?>:</td>
273         <td align="right"><?php
274                         if ($my_MaxCourseSize == TR_COURSESIZE_UNLIMITED) {
275                                 echo _AT('unlimited');
276                         } else {
277                                 if ($total_after <= 0) {
278                                         echo '<span style="color: red;">';
279                                         echo $total_after;
280                                         echo '</span>';
281                                 } else {
282                                         echo $total_after;
283                                 }
284                         } ?></td>
285         <td>&nbsp;</td>
286 </tr>
287 </tfoot>
288 </table>
289
290 <?php require(TR_INCLUDE_PATH.'footer.inc.php'); ?>