PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Server/php/store-and-email/submit.php

https://github.com/zykloid/feedbackreporter
PHP | 133 lines | 78 code | 29 blank | 26 comment | 13 complexity | f6e57e88b78ee471ceace3dbf6aad51d MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2008, Torsten Curdt
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. include 'config.php';
  18. include 'errorlog.php';
  19. function dirs($dir)
  20. {
  21. $fh = opendir($dir);
  22. while($entryName = readdir($fh)) {
  23. if ($entryName{0} != '.') {
  24. $dirArray[$entryName] = $entryName;
  25. }
  26. }
  27. closedir($fh);
  28. return $dirArray;
  29. }
  30. function uniq()
  31. {
  32. return date('Y-m-d\\TH:i:s-') . md5(getmypid().uniqid(rand()).$_SERVER[SERVER_NAME]);
  33. }
  34. // Support projects with spaces such as "MainMenu Pro" by removing the spaces
  35. // so that the submission folder becomes "MainMenuPro"
  36. $project_raw = preg_replace('/\s+/', '', $_GET['project']);
  37. $project = preg_replace('/[^(0-9A-Za-z)]*/', '', $project_raw);
  38. if ($project != $project_raw) {
  39. echo "ERR 007\n";
  40. echo "project name mismatch";
  41. exit;
  42. }
  43. $project_dir = $feedback_dir . $project . '/';
  44. if(!is_dir($project_dir)) {
  45. // no project directory
  46. if (!$create_project_dirs) {
  47. // no project directory (and not configured to create one)
  48. echo "ERR 002\n";
  49. echo "no such project";
  50. exit;
  51. }
  52. if (count(dirs($feedback_dir)) > $feedback_max_project) {
  53. // too many projects
  54. echo "ERR 009\n";
  55. echo "too many projects";
  56. exit;
  57. }
  58. // create project dir
  59. if (!mkdir($project_dir)) {
  60. // failed to create project directory
  61. echo "ERR 008\n";
  62. echo "could not create project dir";
  63. exit;
  64. }
  65. }
  66. $submission_dir = $project_dir . uniq() . '/';
  67. if (!mkdir($submission_dir)) {
  68. // failed to create submission directory
  69. echo "ERR 003\n";
  70. echo "failed to create submission directory";
  71. exit;
  72. }
  73. foreach($feedback_files as $file) {
  74. $dest = $submission_dir . '/' . $file;
  75. $fh = fopen($dest, "w");
  76. if (!$fh) {
  77. // failed to create file
  78. echo "ERR 004 $file\n";
  79. echo "failed to create file";
  80. continue;
  81. }
  82. fwrite($fh, $_POST[$file]);
  83. fclose($fh);
  84. echo "OK 004 $file\n";
  85. }
  86. if (count($_FILES) > $feedback_max_files) {
  87. // too many files submitted
  88. echo "ERR 005\n";
  89. echo "too many files";
  90. exit;
  91. }
  92. $i = 0;
  93. foreach($_FILES as $file) {
  94. $dest = $submission_dir . "file-$i";
  95. if(!move_uploaded_file($file['tmp_name'], $dest)){
  96. // failed to move uploaded file
  97. echo 'ERR 006 ' . $file['error'] . "\n";
  98. echo "failed to move file";
  99. } else {
  100. echo 'OK 006 ' . $file['name'] . "\n";
  101. }
  102. chmod($dest, 0644);
  103. }
  104. ?>