add.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * scientia
  4. *
  5. * Copyright 2023 - 2024 Johannes Keßler
  6. *
  7. * https://www.bananas-playground.net/projekt/scientia/
  8. *
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  12. *
  13. * You should have received a copy of the
  14. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  15. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  16. */
  17. /**
  18. * add endpoint. Accepts only POST and valid post body as json
  19. */
  20. mb_http_output('UTF-8');
  21. mb_internal_encoding('UTF-8');
  22. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  23. ## check request
  24. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  25. if(!empty($_urlToParse)) {
  26. # see http://de2.php.net/manual/en/regexp.reference.unicode.php
  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. ## config
  32. require_once('config/config.php');
  33. ## set the error reporting
  34. ini_set('log_errors',true);
  35. ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
  36. if(DEBUG === true) {
  37. ini_set('display_errors',true);
  38. }
  39. else {
  40. ini_set('display_errors',false);
  41. }
  42. # time settings
  43. date_default_timezone_set(TIMEZONE);
  44. # required libs
  45. require_once('lib/summoner.class.php');
  46. if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true));
  47. ## check if request is valid
  48. $_create = false;
  49. $filteredData = '';
  50. if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') {
  51. $payload = json_decode(file_get_contents('php://input'), true);
  52. if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true));
  53. if(!empty($payload)) {
  54. if(isset($payload['asl']) && !empty($payload['asl'])
  55. && isset($payload['data']) && !empty($payload['data'])
  56. && isset(UPLOAD_SECRET[$payload['asl']])
  57. ) {
  58. if(DEBUG) error_log("[DEBUG] Valid payload so far");
  59. $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
  60. if(!empty($filteredData)) {
  61. if(DEBUG) error_log("[DEBUG] Validated payload");
  62. $_create = true;
  63. }
  64. }
  65. }
  66. }
  67. ## default response
  68. $contentType = 'Content-Type: application/json; charset=utf-8';
  69. $httpResponseCode = 200;
  70. $contentBody = array(
  71. 'message' => '',
  72. 'status' => $httpResponseCode
  73. );
  74. ## break here secret empty or false
  75. if($_create === false) {
  76. header('X-PROVIDED-BY: scientia');
  77. header($contentType);
  78. http_response_code($httpResponseCode);
  79. echo json_encode($contentBody);
  80. exit();
  81. }
  82. ## DB connection
  83. $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
  84. if ($DB->connect_errno) exit('Can not connect to MySQL Server');
  85. $DB->set_charset("utf8mb4");
  86. $DB->query("SET collation_connection = 'utf8mb4_bin'");
  87. $driver = new mysqli_driver();
  88. $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
  89. require_once 'lib/entry.class.php';
  90. $Entry = new Entry($DB);
  91. $do = $Entry->create($filteredData);
  92. if(!empty($do)) {
  93. $contentBody['message'] = INSTALL_URL . PATH_WEBROOT . date('/Y/m/d/').$do;
  94. }
  95. else {
  96. $hash = md5($do.time());
  97. error_log("[ERROR] $hash Can not create. ". var_export($do,true));
  98. $contentBody['message'] = "Something went wrong. $hash";
  99. $contentBody['status'] = 500;
  100. }
  101. # return
  102. header('X-PROVIDED-BY: scientia');
  103. header($contentType);
  104. http_response_code($httpResponseCode);
  105. echo json_encode($contentBody);
  106. $DB->close();