index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. # Encoding and error reporting setting
  19. mb_http_output('UTF-8');
  20. mb_internal_encoding('UTF-8');
  21. error_reporting(-1); // E_ALL & E_STRICT
  22. # config file
  23. require_once 'config.php';
  24. # default time setting
  25. date_default_timezone_set(TIMEZONE);
  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. # error reporting
  34. ini_set('log_errors',true);
  35. if(DEBUG) {
  36. ini_set('display_errors',true);
  37. }
  38. else {
  39. ini_set('display_errors',false);
  40. }
  41. # static helper class
  42. require_once 'lib/summoner.class.php';
  43. # upload / file handling
  44. require_once 'lib/mancubus.class.php';
  45. $_short = false;
  46. if(isset($_GET['s']) && !empty($_GET['s'])) {
  47. $_short = trim($_GET['s']);
  48. $_short = Summoner::validate($_short,'nospace') ? $_short : false;
  49. }
  50. $_create = false;
  51. if(isset($_POST['dl']) && !empty($_POST['dl'])
  52. && isset($_FILES['pasty']) && !empty($_FILES['pasty'])
  53. && isset(SELFPASTE_UPLOAD_SECRET[$_POST['dl']])) {
  54. $_create = true;
  55. }
  56. $contentType = 'Content-type: text/html; charset=UTF-8';
  57. $contentView = 'welcome';
  58. $httpResponseCode = 200;
  59. if(!empty($_short)) {
  60. $contentType = 'Content-type: text/plain; charset=UTF-8';
  61. $contentView = 'view';
  62. $httpResponseCode = 404;
  63. $contentBody = 'File not found.';
  64. $_t = Summoner::b64sl_unpack_id($_short);
  65. $_t = (string)$_t;
  66. $_p = Summoner::forwardslashStringToPath($_t);
  67. $_requestFile = str_ends_with(SELFPASTE_UPLOAD_DIR,'/') ? SELFPASTE_UPLOAD_DIR : SELFPASTE_UPLOAD_DIR.'/';
  68. $_requestFile .= $_p;
  69. $_requestFile .= $_t;
  70. if(is_readable($_requestFile)) {
  71. $contentBody = $_requestFile;
  72. $httpResponseCode = 200;
  73. }
  74. }
  75. elseif ($_create === true) {
  76. $contentView = 'created';
  77. $contentType = 'Content-type:application/json;charset=utf-8';
  78. $httpResponseCode = 400;
  79. $_message = 'Something went wrong.';
  80. $_file = $_FILES['pasty'];
  81. $_fileObj = new Mancubus();
  82. if($_fileObj->load($_FILES['pasty']) === true) {
  83. $_fileObj->setSaveFilename();
  84. $_fileObj->setShort();
  85. $_fileObj->setStoragePath();
  86. $_fileObj->setShortURL();
  87. $_do = $_fileObj->process();
  88. $_message = $_do['message'];
  89. if($_do['status'] === true) {
  90. $httpResponseCode = 200;
  91. if(defined('LOG_CREATION') && LOG_CREATION === true) {
  92. Summoner::createLog($_message." ".SELFPASTE_UPLOAD_SECRET[$_POST['dl']]);
  93. }
  94. }
  95. }
  96. $contentBody = array(
  97. 'message' => $_message,
  98. 'status' => $httpResponseCode
  99. );
  100. }
  101. header('X-PROVIDED-BY: selfpaste');
  102. header($contentType);
  103. http_response_code($httpResponseCode);
  104. if(file_exists('view/'.$contentView.'.inc.php')) {
  105. require_once 'view/'.$contentView.'.inc.php';
  106. }
  107. else {
  108. Summoner::syslog('Content body file missing. '.Summoner::cleanForLog($_SERVER));
  109. http_response_code(400);
  110. die('Well, something went wrong...');
  111. }