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