2b44f689db2c4efc0b01dc5293247c538fb0dd2c
[acontent.git] / docs / file_manager / upload.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 define('TR_INCLUDE_PATH', '../include/');
14 require_once(TR_INCLUDE_PATH.'vitals.inc.php');
15 require_once(TR_INCLUDE_PATH.'classes/DAO/CoursesDAO.class.php');
16 require_once(TR_INCLUDE_PATH.'classes/FileUtility.class.php');
17
18 global $_course_id;
19 Utility::authenticate(TR_PRIV_ISAUTHOR_OF_CURRENT_COURSE);
20 $coursesDAO = new CoursesDAO();
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 $row = $coursesDAO->get($_course_id);
34 $my_MaxCourseSize = $row['max_quota'];
35 $my_MaxFileSize = $row['max_file_size'];
36
37 if ($my_MaxCourseSize != TR_COURSESIZE_UNLIMITED) $my_MaxCourseSize = $MaxCourseSize;
38 $my_MaxFileSize = FileUtility::megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
39
40 //      if ($my_MaxCourseSize == TR_COURSESIZE_DEFAULT) {
41 //              $my_MaxCourseSize = $MaxCourseSize;
42 //      }
43 //      if ($my_MaxFileSize == TR_FILESIZE_DEFAULT) {
44 //              $my_MaxFileSize = $MaxFileSize;
45 //      } else if ($my_MaxFileSize == TR_FILESIZE_SYSTEM_MAX) {
46 //              $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
47 //      }
48
49 $path = TR_CONTENT_DIR . $_course_id.'/'.$_POST['pathext'];
50
51 if (isset($_POST['submit'])) {
52     if($_FILES['file']) {
53        $_FILES['uploadedfile'] = $_FILES['file']; 
54     }
55         if($_FILES['uploadedfile']['name'])     {
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                 /* check if this file extension is allowed: */
62                 /* $IllegalExtentions is defined in ./include/config.inc.php */
63                 if (in_array($ext, $IllegalExtentions)) {
64                         $errors = array('FILE_ILLEGAL', $ext);
65                         $msg->addError($errors);
66                         FileUtility::handleAjaxUpload(500);
67                         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'].SEP.'_course_id='.$_course_id);
68                         exit;
69                 }
70
71                 /* also have to handle the 'application/x-zip-compressed'  case */
72                 if (   ($_FILES['uploadedfile']['type'] == 'application/x-zip-compressed')
73                         || ($_FILES['uploadedfile']['type'] == 'application/zip')
74                         || ($_FILES['uploadedfile']['type'] == 'application/x-zip')){
75                         $is_zip = true;                                         
76                 }
77
78         
79                 /* anything else should be okay, since we're on *nix.. hopefully */
80                 $_FILES['uploadedfile']['name'] = str_replace(array(' ', '/', '\\', ':', '*', '?', '"', '<', '>', '|', '\''), '', $_FILES['uploadedfile']['name']);
81
82                 /* if the file size is within allowed limits */
83                 if( ($_FILES['uploadedfile']['size'] > 0) && ($_FILES['uploadedfile']['size'] <= $my_MaxFileSize) ) {
84
85                         /* if adding the file will not exceed the maximum allowed total */
86                         $course_total = FileUtility::dirsize($path);
87
88                         if ((($course_total + $_FILES['uploadedfile']['size']) <= $my_MaxCourseSize) || ($my_MaxCourseSize == TR_COURSESIZE_UNLIMITED)) {
89
90                                 /* check if this file exists first */
91                                 if (file_exists($path.$_FILES['uploadedfile']['name'])) {
92                                         /* this file already exists, so we want to prompt for override */
93
94                                         /* save it somewhere else, temporarily first                    */
95                                         /* file_name.time ? */
96                                         $_FILES['uploadedfile']['name'] = substr(time(), -4).'.'.$_FILES['uploadedfile']['name'];
97
98                                         $f = array('FILE_EXISTS',
99                                                                         substr($_FILES['uploadedfile']['name'], 5),
100                                                                         $_FILES['uploadedfile']['name']);
101                                         $msg->addFeedback($f);
102                                 }
103
104                                 /* copy the file in the directory */
105                                 $result = move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $path.$_FILES['uploadedfile']['name'] );
106
107                                 if (!$result) {
108                                         require(TR_INCLUDE_PATH.'header.inc.php');
109                                         $msg->printErrors('FILE_NOT_SAVED');
110                                         echo '<a href="../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'].SEP.'_course_id='.$_course_id.'">' . _AT('back') . '</a>';
111                                         require(TR_INCLUDE_PATH.'footer.inc.php');
112                                         exit;
113                                 } else {
114                                         if ($is_zip) {
115                                                 $f = array('FILE_UPLOADED_ZIP',
116                                                                                 urlencode($_POST['pathext']), 
117                                                                                 urlencode($_FILES['uploadedfile']['name']), 
118                                                                                 $_GET['popup'],
119                                                                                 $_course_id,
120                                                                                 SEP);
121                                                 $msg->addFeedback($f);
122                                                 FileUtility::handleAjaxUpload(200);
123                                                 if ($alter)
124                                                         header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab'].SEP.'_course_id='.$_course_id);
125                                                 else
126                                                         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.'_course_id='.$_course_id);
127                                                 exit;
128                                         } /* else */
129
130                                         // uploading an alternative content object
131                                         if ($_GET['a_type'] > 0) {
132                                                 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']).SEP.'_course_id='.$_course_id);
133                                         }
134                                         else {
135                                                 $msg->addFeedback('FILE_UPLOADED');
136                                                 FileUtility::handleAjaxUpload(200);
137                                                 
138                                                 if ($alter)
139                                                         header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab'].SEP.'_course_id='.$_course_id);
140                                                 else
141                                                         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.'_course_id='.$_course_id);
142                                         }
143                                         exit;
144                                 }
145                         } else {
146                                 $msg->addError(array('MAX_STORAGE_EXCEEDED', get_human_size($my_MaxCourseSize)));
147                                 FileUtility::handleAjaxUpload(500);
148                                 if ($alter)
149                                                         header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab'].SEP.'_course_id='.$_course_id);
150                                                 else
151                                                         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.'_course_id='.$_course_id);
152                                                 
153                                 exit;
154                         }
155                 } else {
156                         $msg->addError(array('FILE_TOO_BIG', get_human_size($my_MaxFileSize)));
157                         FileUtility::handleAjaxUpload(500);
158                         if ($alter)
159                                                         header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab'].SEP.'_course_id='.$_course_id);
160                                                 else
161                                                         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.'_course_id='.$_course_id);
162                                                 
163                         exit;
164                 }
165         } else {
166                 $msg->addError('FILE_NOT_SELECTED');
167                 FileUtility::handleAjaxUpload(500);
168                 if ($alter)
169                         header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab'].SEP.'_course_id='.$_course_id);
170                 else
171                         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.'_course_id='.$_course_id);
172                 exit;
173         }
174 }
175
176 ?>