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