dropbox_submit.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. }
  50. // we are doing a just upload but an additional recipient is selected.
  51. elseif ( $thisIsJustUpload && ( count($_POST['recipients']) != 1)) {
  52. $error = true;
  53. $errormsg = get_lang('MailingJustUploadSelectNoOther');
  54. } elseif (empty($_FILES['file']['name'])) {
  55. $error = true;
  56. $errormsg = get_lang('NoFileSpecified');
  57. }
  58. }
  59. //check if $_POST['cb_overwrite'] is true or false
  60. $dropbox_overwrite = false;
  61. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  62. $dropbox_overwrite = true;
  63. }
  64. /**
  65. * FORM SUBMIT : UPLOAD NEW FILE
  66. */
  67. if (!$error) {
  68. $dropbox_filename = $_FILES['file']['name'];
  69. $dropbox_filesize = $_FILES['file']['size'];
  70. $dropbox_filetype = $_FILES['file']['type'];
  71. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  72. if ($dropbox_filesize <= 0 || $dropbox_filesize > dropbox_cnf('maxFilesize')) {
  73. $errormsg = get_lang('TooBig'); // TODO: The "too big" message does not fit in the case of uploading zero-sized file.
  74. $error = true;
  75. } elseif (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  76. die(get_lang('BadFormData').' (code 403)');
  77. }
  78. if (!$error) {
  79. // Try to add an extension to the file if it hasn't got one
  80. $dropbox_filename = add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  81. // Replace dangerous characters
  82. $dropbox_filename = api_replace_dangerous_char($dropbox_filename);
  83. // Transform any .php file in .phps fo security
  84. $dropbox_filename = php2phps($dropbox_filename);
  85. if (!filter_extension($dropbox_filename)) {
  86. $error = true;
  87. $errormsg = get_lang('UplUnableToSaveFileFilteredExtension');
  88. } else {
  89. // set title
  90. $dropbox_title = $dropbox_filename;
  91. // set author
  92. if ($_POST['authors'] == '') {
  93. $_POST['authors'] = getUserNameFromId($_user['user_id']);
  94. }
  95. if ($dropbox_overwrite) {
  96. $dropbox_person = new Dropbox_Person($_user['user_id'], $is_courseAdmin, $is_courseTutor);
  97. foreach ($dropbox_person->sentWork as $w) {
  98. if ($w->title == $dropbox_filename) {
  99. if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) {
  100. $error = true;
  101. $errormsg = get_lang('MailingNonMailingError');
  102. }
  103. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  104. $error = true;
  105. $errormsg = get_lang('MailingJustUploadSelectNoOther');
  106. }
  107. $dropbox_filename = $w->filename;
  108. $found = true;
  109. break;
  110. }
  111. }
  112. } else {
  113. // rename file to login_filename_uniqueId format
  114. $dropbox_filename = getLoginFromId( $_user['user_id']) . '_' . $dropbox_filename . '_'.uniqid('');
  115. }
  116. if (!is_dir(dropbox_cnf('sysPath'))) {
  117. //The dropbox subdir doesn't exist yet so make it and create the .htaccess file
  118. mkdir(dropbox_cnf('sysPath'), api_get_permissions_for_new_directories()) or die(get_lang('ErrorCreatingDir').' (code 404)');
  119. $fp = fopen(dropbox_cnf('sysPath').'/.htaccess', 'w') or die(get_lang('ErrorCreatingDir').' (code 405)');
  120. fwrite($fp, "AuthName AllowLocalAccess
  121. AuthType Basic
  122. order deny,allow
  123. deny from all
  124. php_flag zlib.output_compression off") or die(get_lang('ErrorCreatingDir').' (code 406)');
  125. }
  126. if ($error) {
  127. } elseif ($thisIsAMailing) {
  128. if (preg_match(dropbox_cnf('mailingZipRegexp'), $dropbox_title)) {
  129. $newWorkRecipients = dropbox_cnf('mailingIdBase');
  130. } else {
  131. $error = true;
  132. $errormsg = $dropbox_title . ': ' . get_lang('MailingWrongZipfile');
  133. }
  134. } elseif ($thisIsJustUpload) {
  135. $newWorkRecipients = array();
  136. } else {
  137. // Creating the array that contains all the users who will receive the file
  138. $newWorkRecipients = array();
  139. foreach ($_POST['recipients'] as $rec) {
  140. if (strpos($rec, 'user_') === 0) {
  141. $newWorkRecipients[] = substr($rec, strlen('user_'));
  142. } elseif (strpos($rec, 'group_') === 0) {
  143. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_')));
  144. foreach ($userList as $usr) {
  145. if (!in_array($usr['user_id'], $newWorkRecipients) && $usr['user_id'] != $_user['user_id']) {
  146. $newWorkRecipients[] = $usr['user_id'];
  147. }
  148. }
  149. }
  150. }
  151. }
  152. // After uploading the file, create the db entries
  153. if (!$error) {
  154. @move_uploaded_file($dropbox_filetmpname, dropbox_cnf('sysPath') . '/' . $dropbox_filename)
  155. or die(get_lang('UploadError').' (code 407)');
  156. new Dropbox_SentWork($_user['user_id'], $dropbox_title, $_POST['description'], strip_tags($_POST['authors']), $dropbox_filename, $dropbox_filesize, $newWorkRecipients);
  157. }
  158. }
  159. }
  160. } //end if(!$error)
  161. /**
  162. * SUBMIT FORM RESULTMESSAGE
  163. */
  164. if (!$error) {
  165. $return_message = get_lang('FileUploadSucces');
  166. } else {
  167. $return_message = $errormsg;
  168. }
  169. }
  170. /**
  171. * DELETE RECEIVED OR SENT FILES - EDIT FEEDBACK
  172. * - DELETE ALL RECEIVED FILES
  173. * - DELETE 1 RECEIVED FILE
  174. * - DELETE ALL SENT FILES
  175. * - DELETE 1 SENT FILE
  176. * - EDIT FEEDBACK
  177. */
  178. if (isset($_GET['deleteReceived']) || isset($_GET['deleteSent'])
  179. || isset( $_GET['showFeedback']) || isset( $_GET['editFeedback'])) {
  180. if ($_GET['mailing']) {
  181. getUserOwningThisMailing($_GET['mailing'], $_user['user_id'], '408');
  182. $dropbox_person = new Dropbox_Person($_GET['mailing'], $is_courseAdmin, $is_courseTutor);
  183. } else {
  184. $dropbox_person = new Dropbox_Person($_user['user_id'], $is_courseAdmin, $is_courseTutor);
  185. }
  186. if (isset($_SESSION['sentOrder'])) {
  187. $dropbox_person->orderSentWork($_SESSION['sentOrder']);
  188. }
  189. if (isset($_SESSION['receivedOrder'])) {
  190. $dropbox_person->orderReceivedWork($_SESSION['receivedOrder']);
  191. }
  192. /*if (!$dropbox_person->isCourseAdmin || ! $dropbox_person->isCourseTutor) {
  193. die(get_lang('GeneralError').' (code 408)');
  194. }*/
  195. $tellUser = get_lang('FileDeleted');
  196. if (isset($_GET['deleteReceived'])) {
  197. if ($_GET['deleteReceived'] == 'all') {
  198. $dropbox_person->deleteAllReceivedWork();
  199. } elseif (is_numeric($_GET['deleteReceived'])) {
  200. $dropbox_person->deleteReceivedWork( $_GET['deleteReceived']);
  201. } else {
  202. die(get_lang('GeneralError').' (code 409)');
  203. }
  204. } elseif (isset( $_GET['deleteSent'])) {
  205. if ($_GET['deleteSent'] == 'all') {
  206. $dropbox_person->deleteAllSentWork( );
  207. } elseif (is_numeric($_GET['deleteSent'])) {
  208. $dropbox_person->deleteSentWork($_GET['deleteSent']);
  209. } else {
  210. die(get_lang('GeneralError').' (code 410)');
  211. }
  212. } elseif (isset($_GET['showFeedback'])) {
  213. $w = new Dropbox_SentWork($id = $_GET['showFeedback']);
  214. if ($w->uploader_id != $_user['user_id']) {
  215. getUserOwningThisMailing($w->uploader_id, $_user['user_id'], '411');
  216. }
  217. foreach ($w -> recipients as $r) {
  218. if (($fb = $r['feedback'])) {
  219. $fbarray[$r['feedback_date'].$r['name']] = $r['name'].' '.get_lang('SentOn', '').' '.$r['feedback_date'].":\n".$fb;
  220. }
  221. }
  222. if ($fbarray) {
  223. krsort($fbarray);
  224. echo '<textarea class="dropbox_feedbacks">',
  225. htmlspecialchars(implode("\n\n", $fbarray), ENT_QUOTES, api_get_system_encoding()), '</textarea>', "\n";
  226. } else {
  227. echo '<textarea class="dropbox_feedbacks">&nbsp;</textarea>', "\n";
  228. }
  229. $tellUser = get_lang('ShowFeedback');
  230. } else { // if ( isset( $_GET['editFeedback'])) {
  231. $id = $_GET['editFeedback'];
  232. $found = false;
  233. foreach ($dropbox_person->receivedWork as $w) {
  234. if ($w->id == $id) {
  235. $found = true;
  236. break;
  237. }
  238. }
  239. if (!$found) die(get_lang('GeneralError').' (code 415)');
  240. echo '<form method="post" action="index.php">', "\n",
  241. '<input type="hidden" name="feedbackid" value="',
  242. $id, '"/>', "\n",
  243. '<textarea name="feedbacktext" class="dropbox_feedbacks">',
  244. htmlspecialchars($w->feedback, ENT_QUOTES, api_get_system_encoding()), '</textarea>', "<br />\n",
  245. '<input type="submit" name="feedbacksubmit" value="', get_lang('Ok', ''), '"/>', "\n",
  246. '</form>', "\n";
  247. $tellUser = get_lang('GiveFeedback');
  248. }
  249. /**
  250. * RESULTMESSAGE FOR DELETE FILE OR EDIT FEEDBACK
  251. */
  252. $return_message = get_lang('BackList');
  253. }