index.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  5. *
  6. * You should have received a copy of the
  7. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  8. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  9. *
  10. * 2019 - 2020 https://://www.bananas-playground.net/projekt/selfpaste
  11. */
  12. # global debug setting
  13. define('DEBUG',false);
  14. # Encoding and error reporting setting
  15. mb_http_output('UTF-8');
  16. mb_internal_encoding('UTF-8');
  17. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  18. # default time setting
  19. date_default_timezone_set('Europe/Berlin');
  20. # check request
  21. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
  22. if(!empty($_urlToParse)) {
  23. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  24. die('Malformed request. Make sure you know what you are doing.');
  25. }
  26. }
  27. # error reporting
  28. ini_set('log_errors',true);
  29. ini_set('error_log','./logs/error.log');
  30. if(DEBUG === true) {
  31. ini_set('display_errors',true);
  32. }
  33. else {
  34. ini_set('display_errors',false);
  35. }
  36. # static helper class
  37. require_once 'lib/summoner.class.php';
  38. # config file
  39. require_once 'config.php';
  40. # upload / file handling
  41. require_once 'lib/mancubus.class.php';
  42. $_short = false;
  43. if(isset($_GET['s']) && !empty($_GET['s'])) {
  44. $_short = trim($_GET['s']);
  45. $_short = Summoner::validate($_short,'nospace') ? $_short : false;
  46. }
  47. $_create = false;
  48. if(isset($_POST['dl']) && !empty($_POST['dl'])
  49. && isset($_FILES['pasty']) && !empty($_FILES['pasty'])
  50. && $_POST['dl'] === SELFPASTE_UPLOAD_SECRET) {
  51. $_create = true;
  52. }
  53. $contentType = 'Content-type: text/html; charset=UTF-8';
  54. $contentView = 'welcome';
  55. $httpResponseCode = 200;
  56. if(!empty($_short)) {
  57. $contentType = 'Content-type: text/plain; charset=UTF-8';
  58. $contentView = 'view';
  59. $httpResponseCode = 404;
  60. $contentBody = 'File not found.';
  61. $_t = Summoner::b64sl_unpack_id($_short);
  62. $_t = (string)$_t;
  63. $_p = Summoner::forwardslashStringToPath($_t);
  64. $_requestFile = Summoner::endsWith(SELFPASTE_UPLOAD_DIR,'/') ? SELFPASTE_UPLOAD_DIR : SELFPASTE_UPLOAD_DIR.'/';
  65. $_requestFile .= $_p;
  66. $_requestFile .= $_t;
  67. if(is_readable($_requestFile)) {
  68. $contentBody = $_requestFile;
  69. $httpResponseCode = 200;
  70. }
  71. }
  72. elseif ($_create === true) {
  73. $contentView = 'created';
  74. $contentType = 'Content-type:application/json;charset=utf-8';
  75. $httpResponseCode = 400;
  76. $_message = 'Something went wrong.';
  77. $_file = $_FILES['pasty'];
  78. $_fileObj = new Mancubus();
  79. if($_fileObj->load($_FILES['pasty']) === true) {
  80. $_fileObj->setSaveFilename();
  81. $_fileObj->setShort();
  82. $_fileObj->setStoragePath();
  83. $_fileObj->setShortURL();
  84. $_do = $_fileObj->process();
  85. $_message = $_do['message'];
  86. if($_do['status'] === true) {
  87. $httpResponseCode = 200;
  88. }
  89. }
  90. $contentBody = array(
  91. 'message' => $_message,
  92. 'status' => $httpResponseCode
  93. );
  94. }
  95. header('X-PROVIDED-BY: selfpaste');
  96. header($contentType);
  97. http_response_code($httpResponseCode);
  98. if(file_exists('view/'.$contentView.'.inc.php')) {
  99. require_once 'view/'.$contentView.'.inc.php';
  100. }
  101. else {
  102. error_log('Content body file missing. '.var_export($_SERVER,true),3,'./logs/error.log');
  103. http_response_code(400);
  104. die('Well, something went wrong...');
  105. }