dropbox_submit.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /*
  4. * PREVENT RESUBMITING
  5. * This part checks if the $dropbox_unid var has the same ID
  6. * as the session var $dropbox_uniqueid that was registered as a session
  7. * var before.
  8. * The resubmit prevention only works with GET requests, because it gives some annoying
  9. * behaviours with POST requests.
  10. */
  11. /**
  12. * FORM SUBMIT
  13. * - VALIDATE POSTED DATA
  14. * - UPLOAD NEW FILE
  15. */
  16. if (isset($_POST['submitWork'])) {
  17. $error = false;
  18. $errormsg = '';
  19. /**
  20. * FORM SUBMIT : VALIDATE POSTED DATA
  21. */
  22. // the author or description field is empty
  23. if (!isset($_POST['authors']) || !isset($_POST['description'])) {
  24. $error = true;
  25. $errormsg = get_lang('BadFormData');
  26. } elseif (!isset($_POST['recipients']) || count($_POST['recipients']) <= 0) {
  27. $error = true;
  28. $errormsg = get_lang('NoUserSelected');
  29. } else {
  30. $thisIsAMailing = false;
  31. $thisIsJustUpload = false;
  32. foreach ($_POST['recipients'] as $rec) {
  33. if ($rec == 'mailing') {
  34. $thisIsAMailing = true;
  35. } elseif ($rec == 'upload') {
  36. $thisIsJustUpload = true;
  37. } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
  38. echo '401';
  39. die(get_lang('BadFormData').' (code 401)');
  40. } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
  41. echo '402';
  42. die(get_lang('BadFormData').' (code 402)');
  43. }
  44. }
  45. // we are doing a mailing but an additional recipient is selected
  46. if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
  47. $error = true;
  48. $errormsg = get_lang('MailingSelectNoOther');
  49. } // we are doing a just upload but an additional recipient is selected.
  50. elseif ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
  51. $error = true;
  52. $errormsg = get_lang('MailingJustUploadSelectNoOther');
  53. } elseif (empty($_FILES['file']['name'])) {
  54. $error = true;
  55. $errormsg = get_lang('NoFileSpecified');
  56. }
  57. }
  58. //check if $_POST['cb_overwrite'] is true or false
  59. $dropbox_overwrite = false;
  60. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  61. $dropbox_overwrite = true;
  62. }
  63. /**
  64. * FORM SUBMIT : UPLOAD NEW FILE
  65. */
  66. if (!$error) {
  67. $dropbox_filename = $_FILES['file']['name'];
  68. $dropbox_filesize = $_FILES['file']['size'];
  69. $dropbox_filetype = $_FILES['file']['type'];
  70. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  71. if ($dropbox_filesize <= 0 || $dropbox_filesize > dropbox_cnf('maxFilesize')) {
  72. $errormsg = get_lang(
  73. 'TooBig'
  74. ); // TODO: The "too big" message does not fit in the case of uploading zero-sized file.
  75. $error = true;
  76. } elseif (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  77. die(get_lang('BadFormData').' (code 403)');
  78. }
  79. if (!$error) {
  80. // Try to add an extension to the file if it hasn't got one
  81. $dropbox_filename = FileManager::add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  82. // Replace dangerous characters
  83. $dropbox_filename = api_replace_dangerous_char($dropbox_filename);
  84. // Transform any .php file in .phps fo security
  85. $dropbox_filename = FileManager::php2phps($dropbox_filename);
  86. if (!FileManager::filter_extension($dropbox_filename)) {
  87. $error = true;
  88. $errormsg = get_lang('UplUnableToSaveFileFilteredExtension');
  89. } else {
  90. // set title
  91. $dropbox_title = $dropbox_filename;
  92. // set author
  93. if ($_POST['authors'] == '') {
  94. $_POST['authors'] = getUserNameFromId($_user['user_id']);
  95. }
  96. if ($dropbox_overwrite) {
  97. $dropbox_person = new Dropbox_Person($_user['user_id'], api_is_course_admin(), $is_courseTutor);
  98. foreach ($dropbox_person->sentWork as $w) {
  99. if ($w->title == $dropbox_filename) {
  100. if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) {
  101. $error = true;
  102. $errormsg = get_lang('MailingNonMailingError');
  103. }
  104. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  105. $error = true;
  106. $errormsg = get_lang('MailingJustUploadSelectNoOther');
  107. }
  108. $dropbox_filename = $w->filename;
  109. $found = true;
  110. break;
  111. }
  112. }
  113. } else {
  114. // rename file to login_filename_uniqueId format
  115. $dropbox_filename = getLoginFromId($_user['user_id']).'_'.$dropbox_filename.'_'.uniqid('');
  116. }
  117. if (!is_dir(dropbox_cnf('sysPath'))) {
  118. //The dropbox subdir doesn't exist yet so make it and create the .htaccess file
  119. mkdir(dropbox_cnf('sysPath'), api_get_permissions_for_new_directories()) or die(get_lang(
  120. 'ErrorCreatingDir'
  121. ).' (code 404)');
  122. $fp = fopen(dropbox_cnf('sysPath').'/.htaccess', 'w') or die(get_lang(
  123. 'ErrorCreatingDir'
  124. ).' (code 405)');
  125. fwrite(
  126. $fp,
  127. "AuthName AllowLocalAccess
  128. AuthType Basic
  129. order deny,allow
  130. deny from all
  131. php_flag zlib.output_compression off"
  132. ) or die(get_lang('ErrorCreatingDir').' (code 406)');
  133. }
  134. if ($error) {
  135. } elseif ($thisIsAMailing) {
  136. if (preg_match(dropbox_cnf('mailingZipRegexp'), $dropbox_title)) {
  137. $newWorkRecipients = dropbox_cnf('mailingIdBase');
  138. } else {
  139. $error = true;
  140. $errormsg = $dropbox_title.': '.get_lang('MailingWrongZipfile');
  141. }
  142. } elseif ($thisIsJustUpload) {
  143. $newWorkRecipients = array();
  144. } else {
  145. // Creating the array that contains all the users who will receive the file
  146. $newWorkRecipients = array();
  147. foreach ($_POST['recipients'] as $rec) {
  148. if (strpos($rec, 'user_') === 0) {
  149. $newWorkRecipients[] = substr($rec, strlen('user_'));
  150. } elseif (strpos($rec, 'group_') === 0) {
  151. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_')));
  152. foreach ($userList as $usr) {
  153. if (!in_array(
  154. $usr['user_id'],
  155. $newWorkRecipients
  156. ) && $usr['user_id'] != $_user['user_id']
  157. ) {
  158. $newWorkRecipients[] = $usr['user_id'];
  159. }
  160. }
  161. }
  162. }
  163. }
  164. // After uploading the file, create the db entries
  165. if (!$error) {
  166. @move_uploaded_file($dropbox_filetmpname, dropbox_cnf('sysPath').'/'.$dropbox_filename)
  167. or die(get_lang('UploadError').' (code 407)');
  168. new Dropbox_SentWork($_user['user_id'], $dropbox_title, $_POST['description'], strip_tags(
  169. $_POST['authors']
  170. ), $dropbox_filename, $dropbox_filesize, $newWorkRecipients);
  171. }
  172. }
  173. }
  174. } //end if(!$error)
  175. /**
  176. * SUBMIT FORM RESULTMESSAGE
  177. */
  178. if (!$error) {
  179. $return_message = get_lang('FileUploadSucces');
  180. } else {
  181. $return_message = $errormsg;
  182. }
  183. } // end if ( isset( $_POST['submitWork']))
  184. function findRecipient($thisFile)
  185. {
  186. // string result = error message, array result = [user_id, lastname, firstname, status]
  187. global $nameParts, $preFix, $preLen, $postFix, $postLen;
  188. if (preg_match(dropbox_cnf('mailingFileRegexp'), $thisFile, $matches)) {
  189. $thisName = $matches[1];
  190. if (api_substr($thisName, 0, $preLen) == $preFix) {
  191. if ($postLen == 0 || api_substr($thisName, -$postLen) == $postFix) {
  192. $thisRecip = api_substr($thisName, $preLen, api_strlen($thisName) - $preLen - $postLen);
  193. if ($thisRecip) {
  194. return getUser($thisRecip);
  195. }
  196. return ' <'.get_lang('MailingFileNoRecip', '').'>';
  197. } else {
  198. return ' <'.get_lang('MailingFileNoPostfix', '').$postFix.'>';
  199. }
  200. } else {
  201. return ' <'.get_lang('MailingFileNoPrefix', '').$preFix.'>';
  202. }
  203. } else {
  204. return ' <'.get_lang('MailingFileFunny', '').'>';
  205. }
  206. }
  207. function getUser($thisRecip)
  208. {
  209. // string result = error message, array result = [user_id, lastname, firstname]
  210. global $var, $sel;
  211. if (isset($students)) {
  212. unset($students);
  213. }
  214. $result = Database::query($sel.$thisRecip."'");
  215. while (($res = Database::fetch_array($result))) {
  216. $students[] = $res;
  217. }
  218. Database::free_result($result);
  219. if (count($students) == 1) {
  220. return ($students[0]);
  221. } elseif (count($students) > 1) {
  222. return ' <'.get_lang('MailingFileRecipDup', '').$var."= $thisRecip>";
  223. } else {
  224. return ' <'.get_lang('MailingFileRecipNotFound', '').$var."= $thisRecip>";
  225. }
  226. }
  227. /**
  228. * DELETE RECEIVED OR SENT FILES - EDIT FEEDBACK
  229. * - DELETE ALL RECEIVED FILES
  230. * - DELETE 1 RECEIVED FILE
  231. * - DELETE ALL SENT FILES
  232. * - DELETE 1 SENT FILE
  233. * - EDIT FEEDBACK
  234. */
  235. if (isset($_GET['deleteReceived']) || isset($_GET['deleteSent'])
  236. || isset($_GET['showFeedback']) || isset($_GET['editFeedback'])
  237. ) {
  238. if ($_GET['mailing']) {
  239. getUserOwningThisMailing($_GET['mailing'], $_user['user_id'], '408');
  240. $dropbox_person = new Dropbox_Person($_GET['mailing'], api_is_course_admin(), $is_courseTutor);
  241. } else {
  242. $dropbox_person = new Dropbox_Person($_user['user_id'], api_is_course_admin(), $is_courseTutor);
  243. }
  244. if (isset($_SESSION['sentOrder'])) {
  245. $dropbox_person->orderSentWork($_SESSION['sentOrder']);
  246. }
  247. if (isset($_SESSION['receivedOrder'])) {
  248. $dropbox_person->orderReceivedWork($_SESSION['receivedOrder']);
  249. }
  250. /*if (!$dropbox_person->isCourseAdmin || ! $dropbox_person->isCourseTutor) {
  251. die(get_lang('GeneralError').' (code 408)');
  252. }*/
  253. $tellUser = get_lang('FileDeleted');
  254. if (isset($_GET['deleteReceived'])) {
  255. if ($_GET['deleteReceived'] == 'all') {
  256. $dropbox_person->deleteAllReceivedWork();
  257. } elseif (is_numeric($_GET['deleteReceived'])) {
  258. $dropbox_person->deleteReceivedWork($_GET['deleteReceived']);
  259. } else {
  260. die(get_lang('GeneralError').' (code 409)');
  261. }
  262. } elseif (isset($_GET['deleteSent'])) {
  263. if ($_GET['deleteSent'] == 'all') {
  264. $dropbox_person->deleteAllSentWork();
  265. } elseif (is_numeric($_GET['deleteSent'])) {
  266. $dropbox_person->deleteSentWork($_GET['deleteSent']);
  267. } else {
  268. die(get_lang('GeneralError').' (code 410)');
  269. }
  270. } elseif (isset($_GET['showFeedback'])) {
  271. $w = new Dropbox_SentWork($id = $_GET['showFeedback']);
  272. if ($w->uploader_id != $_user['user_id']) {
  273. getUserOwningThisMailing($w->uploader_id, $_user['user_id'], '411');
  274. }
  275. foreach ($w->recipients as $r) {
  276. if (($fb = $r['feedback'])) {
  277. $fbarray[$r['feedback_date'].$r['name']] = $r['name'].' '.get_lang(
  278. 'SentOn',
  279. ''
  280. ).' '.$r['feedback_date'].":\n".$fb;
  281. }
  282. }
  283. if ($fbarray) {
  284. krsort($fbarray);
  285. echo '<textarea class="dropbox_feedbacks">',
  286. htmlspecialchars(implode("\n\n", $fbarray), ENT_QUOTES, api_get_system_encoding()), '</textarea>', "\n";
  287. } else {
  288. echo '<textarea class="dropbox_feedbacks">&nbsp;</textarea>', "\n";
  289. }
  290. $tellUser = get_lang('ShowFeedback');
  291. } else { // if ( isset( $_GET['editFeedback'])) {
  292. $id = $_GET['editFeedback'];
  293. $found = false;
  294. foreach ($dropbox_person->receivedWork as $w) {
  295. if ($w->id == $id) {
  296. $found = true;
  297. break;
  298. }
  299. }
  300. if (!$found) {
  301. die(get_lang('GeneralError').' (code 415)');
  302. }
  303. echo '<form method="post" action="index.php">', "\n",
  304. '<input type="hidden" name="feedbackid" value="',
  305. $id, '"/>', "\n",
  306. '<textarea name="feedbacktext" class="dropbox_feedbacks">',
  307. htmlspecialchars($w->feedback, ENT_QUOTES, api_get_system_encoding()), '</textarea>', "<br />\n",
  308. '<input type="submit" name="feedbacksubmit" value="', get_lang('Ok', ''), '"/>', "\n",
  309. '</form>', "\n";
  310. $tellUser = get_lang('GiveFeedback');
  311. }
  312. /**
  313. * RESULTMESSAGE FOR DELETE FILE OR EDIT FEEDBACK
  314. */
  315. $return_message = get_lang('BackList');
  316. }