index.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  15. *
  16. * 2019 - 2023 https://://www.bananas-playground.net/projekt/selfpaste
  17. */
  18. # global debug setting
  19. const DEBUG = false;
  20. # Encoding and error reporting setting
  21. mb_http_output('UTF-8');
  22. mb_internal_encoding('UTF-8');
  23. error_reporting(-1); // E_ALL & E_STRICT
  24. # default time setting
  25. date_default_timezone_set('Europe/Berlin');
  26. # check request
  27. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  28. if(!empty($_urlToParse)) {
  29. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  30. die('Malformed request. Make sure you know what you are doing.');
  31. }
  32. }
  33. const ERROR_LOG_FILE = './logs/error.log';
  34. const CREATE_LOG = './logs/create.log';
  35. # error reporting
  36. ini_set('log_errors',true);
  37. if(DEBUG) {
  38. ini_set('display_errors',true);
  39. }
  40. else {
  41. ini_set('display_errors',false);
  42. }
  43. # static helper class
  44. require_once 'lib/summoner.class.php';
  45. # config file
  46. require_once 'config.php';
  47. # upload / file handling
  48. require_once 'lib/mancubus.class.php';
  49. $_short = false;
  50. if(isset($_GET['s']) && !empty($_GET['s'])) {
  51. $_short = trim($_GET['s']);
  52. $_short = Summoner::validate($_short,'nospace') ? $_short : false;
  53. }
  54. $_create = false;
  55. if(isset($_POST['dl']) && !empty($_POST['dl'])
  56. && isset($_FILES['pasty']) && !empty($_FILES['pasty'])
  57. && isset(SELFPASTE_UPLOAD_SECRET[$_POST['dl']])) {
  58. $_create = true;
  59. }
  60. $contentType = 'Content-type: text/html; charset=UTF-8';
  61. $contentView = 'welcome';
  62. $httpResponseCode = 200;
  63. if(!empty($_short)) {
  64. $contentType = 'Content-type: text/plain; charset=UTF-8';
  65. $contentView = 'view';
  66. $httpResponseCode = 404;
  67. $contentBody = 'File not found.';
  68. $_t = Summoner::b64sl_unpack_id($_short);
  69. $_t = (string)$_t;
  70. $_p = Summoner::forwardslashStringToPath($_t);
  71. $_requestFile = str_ends_with(SELFPASTE_UPLOAD_DIR,'/') ? SELFPASTE_UPLOAD_DIR : SELFPASTE_UPLOAD_DIR.'/';
  72. $_requestFile .= $_p;
  73. $_requestFile .= $_t;
  74. if(is_readable($_requestFile)) {
  75. $contentBody = $_requestFile;
  76. $httpResponseCode = 200;
  77. }
  78. }
  79. elseif ($_create === true) {
  80. $contentView = 'created';
  81. $contentType = 'Content-type:application/json;charset=utf-8';
  82. $httpResponseCode = 400;
  83. $_message = 'Something went wrong.';
  84. $_file = $_FILES['pasty'];
  85. $_fileObj = new Mancubus();
  86. if($_fileObj->load($_FILES['pasty']) === true) {
  87. $_fileObj->setSaveFilename();
  88. $_fileObj->setShort();
  89. $_fileObj->setStoragePath();
  90. $_fileObj->setShortURL();
  91. $_do = $_fileObj->process();
  92. $_message = $_do['message'];
  93. if($_do['status'] === true) {
  94. $httpResponseCode = 200;
  95. if(defined('LOG_CREATION') && LOG_CREATION === true) {
  96. error_log(date("c")." ".$_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]."\n",3,CREATE_LOG);
  97. }
  98. }
  99. }
  100. $contentBody = array(
  101. 'message' => $_message,
  102. 'status' => $httpResponseCode
  103. );
  104. }
  105. header('X-PROVIDED-BY: selfpaste');
  106. header($contentType);
  107. http_response_code($httpResponseCode);
  108. if(file_exists('view/'.$contentView.'.inc.php')) {
  109. require_once 'view/'.$contentView.'.inc.php';
  110. }
  111. else {
  112. error_log('Content body file missing. '.var_export($_SERVER,true),3,ERROR_LOG_FILE);
  113. http_response_code(400);
  114. die('Well, something went wrong...');
  115. }