tagging as ATutor 1.5.4-release
[atutor.git] / tools / filemanager / upload.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2004 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$
14
15 define('AT_INCLUDE_PATH', '../../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
18
19 if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {
20         authenticate(AT_PRIV_CONTENT);
21 }
22
23 $_SESSION['done'] = 1;
24 $popup = $_REQUEST['popup'];
25 $framed = $_REQUEST['framed'];
26
27 /* get this courses MaxQuota and MaxFileSize: */
28 $sql    = "SELECT max_quota, max_file_size FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
29 $result = mysql_query($sql, $db);
30 $row    = mysql_fetch_array($result);
31 $my_MaxCourseSize       = $row['max_quota'];
32 $my_MaxFileSize = $row['max_file_size'];
33
34         if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
35                 $my_MaxCourseSize = $MaxCourseSize;
36         }
37         if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
38                 $my_MaxFileSize = $MaxFileSize;
39         } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
40                 $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
41         }
42
43 $path = AT_CONTENT_DIR . $_SESSION['course_id'].'/'.$_POST['pathext'];
44
45 if (isset($_POST['submit'])) {
46
47         if($_FILES['uploadedfile']['name'])     {
48
49                 $_FILES['uploadedfile']['name'] = trim($_FILES['uploadedfile']['name']);
50                 $_FILES['uploadedfile']['name'] = str_replace(' ', '_', $_FILES['uploadedfile']['name']);
51
52                 $path_parts = pathinfo($_FILES['uploadedfile']['name']);
53                 $ext = $path_parts['extension'];
54
55                 /* check if this file extension is allowed: */
56                 /* $IllegalExtentions is defined in ./include/config.inc.php */
57                 if (in_array($ext, $IllegalExtentions)) {
58                         $errors = array('FILE_ILLEGAL', $ext);
59                         $msg->addError($errors);
60                         header('Location: index.php?pathext='.$_POST['pathext']);
61                         exit;
62                 }
63
64                 /* also have to handle the 'application/x-zip-compressed'  case */
65                 if (   ($_FILES['uploadedfile']['type'] == 'application/x-zip-compressed')
66                         || ($_FILES['uploadedfile']['type'] == 'application/zip')
67                         || ($_FILES['uploadedfile']['type'] == 'application/x-zip')){
68                         $is_zip = true;                                         
69                 }
70
71         
72                 /* anything else should be okay, since we're on *nix.. hopefully */
73                 $_FILES['uploadedfile']['name'] = str_replace(array(' ', '/', '\\', ':', '*', '?', '"', '<', '>', '|', '\''), '', $_FILES['uploadedfile']['name']);
74
75
76                 /* if the file size is within allowed limits */
77                 if( ($_FILES['uploadedfile']['size'] > 0) && ($_FILES['uploadedfile']['size'] <= $my_MaxFileSize) ) {
78
79                         /* if adding the file will not exceed the maximum allowed total */
80                         $course_total = dirsize($path);
81
82                         if ((($course_total + $_FILES['uploadedfile']['size']) <= ($my_MaxCourseSize + $MaxCourseFloat)) || ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED)) {
83
84                                 /* check if this file exists first */
85                                 if (file_exists($path.$_FILES['uploadedfile']['name'])) {
86                                         /* this file already exists, so we want to prompt for override */
87
88                                         /* save it somewhere else, temporarily first                    */
89                                         /* file_name.time ? */
90                                         $_FILES['uploadedfile']['name'] = substr(time(), -4).'.'.$_FILES['uploadedfile']['name'];
91
92                                         $f = array('FILE_EXISTS',
93                                                                         substr($_FILES['uploadedfile']['name'], 5), 
94                                                                         $_FILES['uploadedfile']['name'],
95                                                                         $_POST['pathext'],
96                                                                         $_GET['popup'],
97                                                                         SEP);
98                                         $msg->addFeedback($f);
99                                 }
100
101                                 /* copy the file in the directory */
102                                 $result = move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $path.$_FILES['uploadedfile']['name'] );
103
104                                 if (!$result) {
105                                         require(AT_INCLUDE_PATH.'header.inc.php');
106                                         $msg->printErrors('FILE_NOT_SAVED');
107                                         echo '<a href="tools/filemanager/index.php?pathext=' . $_POST['pathext'] . SEP . 'popup=' . $_GET['popup'] . '">' . _AT('back') . '</a>';
108                                         require(AT_INCLUDE_PATH.'footer.inc.php');
109                                         exit;
110                                 } else {
111                                         if ($is_zip) {
112                                                 $f = array('FILE_UPLOADED_ZIP',
113                                                                                 urlencode($_POST['pathext']), 
114                                                                                 urlencode($_FILES['uploadedfile']['name']), 
115                                                                                 $_GET['popup'],
116                                                                                 SEP);
117                                                 $msg->addFeedback($f);
118                                                 
119                                                 header('Location: index.php?pathext=' . $_POST['pathext'] . SEP . 'popup=' . $_GET['popup']);
120                                                 exit;
121                                         } /* else */
122
123                                         $msg->addFeedback('FILE_UPLOADED');
124
125                                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_GET['popup']);
126                                         exit;
127                                 }
128                         } else {
129                                 $msg->addError(array('MAX_STORAGE_EXCEEDED', get_human_size($my_MaxCourseSize)));
130                                 header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_GET['popup']);
131                                 exit;
132                         }
133                 } else {
134                         $msg->addError(array('FILE_TOO_BIG', get_human_size($my_MaxFileSize)));
135                         header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_GET['popup']);
136                         exit;
137                 }
138         } else {
139                 $msg->addError('FILE_NOT_SELECTED');
140                 header('Location: index.php?pathext='.$_POST['pathext'].SEP.'popup='.$_GET['popup']);
141                 exit;
142         }
143 }
144
145
146 ?>