index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /**
  19. * This is a simple web client which can be hosted where you want.
  20. * copy the config.default.php file to config.php and update its settings
  21. */
  22. const DEBUG = false;
  23. require_once 'config.php';
  24. # Encoding and error reporting setting
  25. mb_http_output('UTF-8');
  26. mb_internal_encoding('UTF-8');
  27. error_reporting(-1); // E_ALL & E_STRICT
  28. # default time setting
  29. date_default_timezone_set('Europe/Berlin');
  30. # check request
  31. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  32. if(!empty($_urlToParse)) {
  33. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  34. die('Malformed request. Make sure you know what you are doing.');
  35. }
  36. }
  37. ini_set('display_errors',false);
  38. if(DEBUG === true) {
  39. ini_set('display_errors',true);
  40. }
  41. if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])
  42. || $_SERVER['PHP_AUTH_USER'] !== FRONTEND_USERNAME || $_SERVER['PHP_AUTH_PW'] !== FRONTEND_PASSWORD
  43. ) {
  44. header('WWW-Authenticate: Basic realm="Skynet"');
  45. header('HTTP/1.0 401 Unauthorized');
  46. echo 'No Access...';
  47. exit;
  48. }
  49. $statusMessage = "";
  50. if(isset($_POST['doSome'])) {
  51. $_text = trim($_POST['asText']);
  52. $_file = $_FILES['uploadFile'];
  53. if(!empty($_text) && !empty($_file['tmp_name'])) {
  54. $statusMessage = "One option. Not both at the same time.";
  55. }
  56. elseif (!empty($_text)) {
  57. $_tmpfile = tmpfile();
  58. fwrite($_tmpfile, $_text);
  59. $data['pasty'] = curl_file_create(stream_get_meta_data($_tmpfile)['uri']);
  60. }
  61. elseif(!empty($_file['tmp_name'])) {
  62. if($_file['error'] === UPLOAD_ERR_OK) {
  63. $data['pasty'] = curl_file_create($_file['tmp_name']);
  64. }
  65. else {
  66. $statusMessage = "Upload of selected file failed.";
  67. }
  68. }
  69. if(empty($statusMessage)) {
  70. $data['dl'] = THE_SECRET;
  71. $call = curlPostUploadCall(THE_ENDPOINT,$data);
  72. $statusMessage = "Something went wrong. ".var_export($call,true);
  73. $json = json_decode($call,true);
  74. if(!empty($call) && $json != NULL) {
  75. if (isset($json['message']) && $json['status'] == "200") {
  76. $statusMessage = $json['message'];
  77. }
  78. }
  79. }
  80. }
  81. ?>
  82. <html lang="en">
  83. <head>
  84. <title>selfpaste - add a new one</title>
  85. </head>
  86. <body>
  87. <?php if(!empty($statusMessage)) { ?>
  88. <p><?php echo $statusMessage; ?></p>
  89. <?php } ?>
  90. <form method="post" enctype="multipart/form-data" action="">
  91. <p>
  92. <textarea name="asText" cols="100" rows="20"></textarea>
  93. </p>
  94. <p><input type="file" name="uploadFile"></p>
  95. <p><input type="submit" value="send" name="doSome"></p>
  96. </form>
  97. </body>
  98. </html>
  99. <?php
  100. /**
  101. * functions start here
  102. */
  103. /**
  104. * execute a curl call to the given $url
  105. *
  106. * @param string $url The request url
  107. * @param array $data
  108. * @param string $port
  109. * @return bool|mixed
  110. */
  111. function curlPostUploadCall(string $url,array $data, string $port=''): mixed {
  112. $ret = false;
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_URL, $url);
  115. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  116. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
  117. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  118. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  119. curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
  120. curl_setopt($ch, CURLOPT_POST,1);
  121. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  122. if(!empty($port)) {
  123. curl_setopt($ch, CURLOPT_PORT, $port);
  124. }
  125. $do = curl_exec($ch);
  126. if(is_string($do) === true) {
  127. $ret = $do;
  128. }
  129. else {
  130. error_log(var_export(curl_error($ch),true),3,'./sp-webclient.log');
  131. }
  132. curl_close($ch);
  133. return $ret;
  134. }