index.php 4.3 KB

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