index.php 4.0 KB

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