PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/_core/file_manager/upload.php

https://github.com/harriswong/ATutor
PHP | 179 lines | 145 code | 14 blank | 20 comment | 18 complexity | 90153120801ef0543695aa3c230d2aaf MD5 | raw file
  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. if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {
  18. authenticate(AT_PRIV_CONTENT);
  19. }
  20. $_SESSION['done'] = 1;
  21. $popup = $_REQUEST['popup'];
  22. $framed = $_REQUEST['framed'];
  23. $alter = $_REQUEST['alter'];
  24. //echo $_REQUEST['cid'];
  25. //echo $_REQUEST['tab'];
  26. //echo $alter;
  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. if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
  34. $my_MaxCourseSize = $MaxCourseSize;
  35. }
  36. if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
  37. $my_MaxFileSize = $MaxFileSize;
  38. } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
  39. $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
  40. }
  41. $path = AT_CONTENT_DIR . $_SESSION['course_id'].'/'.$_POST['pathext'];
  42. if (isset($_POST['submit'])) {
  43. if($_FILES['file']) {
  44. $_FILES['uploadedfile'] = $_FILES['file'];
  45. }
  46. if($_FILES['uploadedfile']['name']) {
  47. $_FILES['uploadedfile']['name'] = trim($_FILES['uploadedfile']['name']);
  48. $_FILES['uploadedfile']['name'] = str_replace(' ', '_', $_FILES['uploadedfile']['name']);
  49. $path_parts = pathinfo($_FILES['uploadedfile']['name']);
  50. $ext = $path_parts['extension'];
  51. /* check if this file extension is allowed: */
  52. /* $IllegalExtentions is defined in ./include/config.inc.php */
  53. if (in_array($ext, $IllegalExtentions)) {
  54. $errors = array('FILE_ILLEGAL', $ext);
  55. $msg->addError($errors);
  56. handleAjaxUpload(500);
  57. 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']);
  58. exit;
  59. }
  60. /* also have to handle the 'application/x-zip-compressed' case */
  61. if ( ($_FILES['uploadedfile']['type'] == 'application/x-zip-compressed')
  62. || ($_FILES['uploadedfile']['type'] == 'application/zip')
  63. || ($_FILES['uploadedfile']['type'] == 'application/x-zip')){
  64. $is_zip = true;
  65. }
  66. /* anything else should be okay, since we're on *nix.. hopefully */
  67. $_FILES['uploadedfile']['name'] = str_replace(array(' ', ',', '/', '\\', ':', ';', '*', '?', '"', '<', '>', '|', '\''), '', $_FILES['uploadedfile']['name']);
  68. /* if the file size is within allowed limits */
  69. if( ($_FILES['uploadedfile']['size'] > 0) && ($_FILES['uploadedfile']['size'] <= $my_MaxFileSize) ) {
  70. /* if adding the file will not exceed the maximum allowed total */
  71. $course_total = dirsize($path);
  72. if ((($course_total + $_FILES['uploadedfile']['size']) <= ($my_MaxCourseSize + $MaxCourseFloat)) || ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED)) {
  73. /* check if this file exists first */
  74. if (file_exists($path.$_FILES['uploadedfile']['name'])) {
  75. /* this file already exists, so we want to prompt for override */
  76. /* save it somewhere else, temporarily first */
  77. /* file_name.time ? */
  78. $_FILES['uploadedfile']['name'] = substr(time(), -4).'.'.$_FILES['uploadedfile']['name'];
  79. $f = array('FILE_EXISTS',
  80. substr($_FILES['uploadedfile']['name'], 5),
  81. $_FILES['uploadedfile']['name'],
  82. $_POST['pathext'],
  83. $_GET['popup'],
  84. SEP);
  85. $msg->addFeedback($f);
  86. }
  87. /* copy the file in the directory */
  88. $result = move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $path.$_FILES['uploadedfile']['name'] );
  89. if (!$result) {
  90. require(AT_INCLUDE_PATH.'header.inc.php');
  91. $msg->printErrors('FILE_NOT_SAVED');
  92. 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>';
  93. require(AT_INCLUDE_PATH.'footer.inc.php');
  94. exit;
  95. } else {
  96. if ($is_zip) {
  97. $f = array('FILE_UPLOADED_ZIP',
  98. urlencode($_POST['pathext']),
  99. urlencode($_FILES['uploadedfile']['name']),
  100. $_GET['popup'],
  101. SEP);
  102. $msg->addFeedback($f);
  103. handleAjaxUpload(200);
  104. if ($alter)
  105. header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab']);
  106. else
  107. 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']);
  108. exit;
  109. } /* else */
  110. // uploading an alternative content object
  111. if ($_GET['a_type'] > 0) {
  112. 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']));
  113. }
  114. else {
  115. $msg->addFeedback('FILE_UPLOADED');
  116. handleAjaxUpload(200);
  117. if ($alter)
  118. header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab']);
  119. else
  120. 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']);
  121. }
  122. exit;
  123. }
  124. } else {
  125. $msg->addError(array('MAX_STORAGE_EXCEEDED', get_human_size($my_MaxCourseSize)));
  126. handleAjaxUpload(500);
  127. if ($alter)
  128. header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab']);
  129. else
  130. 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']);
  131. exit;
  132. }
  133. } else {
  134. $msg->addError(array('FILE_TOO_BIG', get_human_size($my_MaxFileSize)));
  135. handleAjaxUpload(500);
  136. if ($alter)
  137. header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab']);
  138. else
  139. 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']);
  140. exit;
  141. }
  142. } else {
  143. $msg->addError('FILE_NOT_SELECTED');
  144. handleAjaxUpload(500);
  145. if ($alter)
  146. header('Location: '.$_base_href.'editor/edit_content.php?cid='.$_REQUEST['cid'].SEP . 'pathext='.$_POST['pathext'].SEP. 'popup='.$_GET['popup'].SEP. 'tab='.$_REQUEST['tab']);
  147. else
  148. 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']);
  149. exit;
  150. }
  151. }
  152. ?>