api.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. mb_http_output('UTF-8');
  18. mb_internal_encoding('UTF-8');
  19. ini_set('error_reporting',-1); // E_ALL & E_STRICT
  20. ## check request
  21. $_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
  22. if(!empty($_urlToParse)) {
  23. # see http://de2.php.net/manual/en/regexp.reference.unicode.php
  24. if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
  25. die('Malformed request. Make sure you know what you are doing.');
  26. }
  27. }
  28. ## config
  29. require_once('config/config.php');
  30. ## set the error reporting
  31. ini_set('log_errors',true);
  32. ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
  33. if(DEBUG === true) {
  34. ini_set('display_errors',true);
  35. }
  36. else {
  37. ini_set('display_errors',false);
  38. }
  39. # time settings
  40. date_default_timezone_set(TIMEZONE);
  41. # required libs
  42. require_once('lib/summoner.class.php');
  43. if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true));
  44. ## check if request is valid
  45. $_create = false;
  46. $filteredData = '';
  47. if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json; charset=UTF-8') {
  48. $payload = json_decode(file_get_contents('php://input'), true);
  49. if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true));
  50. if(!empty($payload)) {
  51. if(isset($payload['asl']) && !empty($payload['asl'])
  52. && isset($payload['data']) && !empty($payload['data'])
  53. && isset(UPLOAD_SECRET[$payload['asl']])
  54. ) {
  55. if(DEBUG) error_log("[DEBUG] Valid payload so far");
  56. $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
  57. if(!empty($filteredData)) {
  58. if(DEBUG) error_log("[DEBUG] Validated payload");
  59. $_create = true;
  60. }
  61. }
  62. }
  63. }
  64. ## default response
  65. $contentType = 'Content-Type: application/json; charset=utf-8';
  66. $httpResponseCode = 200;
  67. $contentBody = array(
  68. 'message' => '',
  69. 'status' => $httpResponseCode
  70. );
  71. ## break here secret empty or false
  72. if($_create === false) {
  73. header('X-PROVIDED-BY: scientia');
  74. header($contentType);
  75. http_response_code($httpResponseCode);
  76. echo json_encode($contentBody);
  77. exit();
  78. }
  79. ## DB connection
  80. $DB = new mysqli(DB_HOST, DB_USERNAME,DB_PASSWORD, DB_NAME);
  81. if ($DB->connect_errno) exit('Can not connect to MySQL Server');
  82. $DB->set_charset("utf8mb4");
  83. $DB->query("SET collation_connection = 'utf8mb4_bin'");
  84. $driver = new mysqli_driver();
  85. $driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
  86. require_once 'lib/entry.class.php';
  87. $Entry = new Entry($DB);
  88. $do = $Entry->create($filteredData);
  89. if(!empty($do)) {
  90. $contentBody['message'] = INSTALL_URL . PATH_WEBROOT . date('/Y/m/d/').$do;
  91. }
  92. else {
  93. $hash = md5($do.time());
  94. error_log("[ERROR] $hash Can not create. ". var_export($do,true));
  95. $contentBody['message'] = "Something went wrong. $hash";
  96. $contentBody['status'] = 500;
  97. }
  98. # return
  99. header('X-PROVIDED-BY: scientia');
  100. header($contentType);
  101. http_response_code($httpResponseCode);
  102. echo json_encode($contentBody);
  103. $DB->close();