]> 91.132.146.200 Git - scientia.git/commitdiff
first working version of the API
authorBanana <mail@bananas-playground.net>
Sat, 26 Mar 2022 10:09:15 +0000 (11:09 +0100)
committerBanana <mail@bananas-playground.net>
Sat, 26 Mar 2022 10:09:15 +0000 (11:09 +0100)
documentation/api.txt [new file with mode: 0644]
webroot/api.php
webroot/config/config.php.default
webroot/lib/summoner.class.php

diff --git a/documentation/api.txt b/documentation/api.txt
new file mode 100644 (file)
index 0000000..4675cfb
--- /dev/null
@@ -0,0 +1,28 @@
+Example request:
+
+POST http://example.tld/api.php
+Content-Type: application/json; charset=utf-8
+Accept: application/json
+
+{
+       "asl": "YOUR-KEY",
+       "data": "TEXT DATA TO BE SAVED"
+}
+
+
+Example response (success):
+
+Content-Type: application/json
+{
+       "message": "http://example.tld/2022/03/26/DFzn",
+       "status": 200
+}
+
+
+Example response (failure):
+
+Content-Type: application/json
+{
+       "message": "Something went wrong. HASHCODE",
+       "status": 500
+}
index 1279a0fa55c9d1a3dcd8c722ae99974966b13583..419668fa91995a0c30a88a164b3a25c8ad336774 100644 (file)
@@ -48,12 +48,28 @@ date_default_timezone_set(TIMEZONE);
 require_once('lib/summoner.class.php');
 
 
+if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true));
 ## check if request is valid
 $_create = false;
-if(isset($_POST['asl']) && !empty($_POST['asl'])
-    && isset($_FILES['data']) && !empty($_FILES['data'])
-    && isset(SELFPASTE_UPLOAD_SECRET[$_POST['asl']])) {
-    $_create = true;
+$filteredData = '';
+if ($_SERVER['REQUEST_METHOD'] === 'POST' && $_SERVER['CONTENT_TYPE'] === 'application/json; charset=utf-8') {
+       $payload = json_decode(file_get_contents('php://input'), true);
+       if(DEBUG) error_log("[DEBUG] Dump payload ".var_export($payload,true));
+       if(!empty($payload)) {
+               if(isset($payload['asl']) && !empty($payload['asl'])
+                       && isset($payload['data']) && !empty($payload['data'])
+                       && isset(UPLOAD_SECRET[$payload['asl']])
+               ) {
+                       if(DEBUG) error_log("[DEBUG] Valid payload so far");
+                       if(!empty($payload['data'])) {
+                               $filteredData = filter_var($payload['data'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
+                               if(!empty($filteredData)) {
+                                       if(DEBUG) error_log("[DEBUG] Validated payload");
+                                       $_create = true;
+                               }
+                       }
+               }
+       }
 }
 
 ## default response
@@ -69,7 +85,8 @@ if($_create === false) {
     header('X-PROVIDED-BY: scientia');
     header($contentType);
     http_response_code($httpResponseCode);
-    echo json_encode($data);
+    echo json_encode($contentBody);
+    exit();
 }
 
 # database object
@@ -81,4 +98,23 @@ if ($DB->connect_errno) exit('Can not connect to MySQL Server');
 $DB->set_charset("utf8mb4");
 $DB->query("SET collation_connection = 'utf8mb4_unicode_ci'");
 $driver = new mysqli_driver();
-$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
\ No newline at end of file
+$driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
+
+require_once 'lib/entry.class.php';
+$Entry = new Entry($DB);
+$do = $Entry->create($filteredData);
+if($do !== false) {
+       $contentBody['message'] = date('/Y/m/d/').$do;
+}
+else {
+       $hash = md5($do.time());
+       error_log("[ERROR] $hash Can not create. ". var_export($do,true));
+       $contentBody['message'] = "Something went wrong. $hash";
+       $contentBody['status'] = 500;
+}
+
+# return
+header('X-PROVIDED-BY: scientia');
+header($contentType);
+http_response_code($httpResponseCode);
+echo json_encode($contentBody);
index 3cf972ece5f3e2b24b4b3da1514839e6059f399c..b8e8a60c8ddae770ca35eba3c6fe834f69743028 100644 (file)
  * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
  */
 
- # set to true if you need debug messages in error log file
- define('DEBUG',true);
- # set to ture if you need query log messages in error log file.
- define('QUERY_DEBUG',true);
+# set to true if you need debug messages in error log file
+define('DEBUG',true);
+# set to ture if you need query log messages in error log file.
+define('QUERY_DEBUG',true);
 
- # timezone settings
- define('TIMEZONE','Europe/Berlin');
+# timezone settings
+define('TIMEZONE','Europe/Berlin');
 
- # path settings
- define('PATH_ABSOLUTE','/absolute/path/scientia/webroot');
- define('PATH_SYSTEMOUT',PATH_ABSOLUTE.'/systemout');
- define('PATH_WEBROOT','/absolute/path');
+# path settings
+define('PATH_ABSOLUTE','/absolute/path/scientia/webroot');
+define('PATH_SYSTEMOUT',PATH_ABSOLUTE.'/systemout');
+define('PATH_WEBROOT','/absolute/path');
 
- # database config
- define('DB_HOST','127.0.0.1');
- define('DB_USERNAME','user');
- define('DB_PASSWORD','test');
- define('DB_NAME','scientia');
- define('DB_PREFIX','sc'); # a _ is added automatically as separation
+# database config
+define('DB_HOST','127.0.0.1');
+define('DB_USERNAME','user');
+define('DB_PASSWORD','test');
+define('DB_NAME','scientia');
+define('DB_PREFIX','sc'); # a _ is added automatically as separation
 
- # username and password for authentication
- define('FRONTEND_USERNAME','user');
- define('FRONTEND_PASSWORD','pass');
+# username and password for authentication
+define('FRONTEND_USERNAME','user');
+define('FRONTEND_PASSWORD','pass');
 
 # API config
 # this is your installation secret. Could be anything.
@@ -48,4 +48,4 @@ define('UPLOAD_SECRET',
         'PLEASE CHANGE YOUR SECRET' => 'Your description for this secret #1',
         'PLEASE CHANGE YOUR SECRET' => 'Your description for this secret #2'
     )
-);
\ No newline at end of file
+);
index ca3db79a7360644775df3156dd4ead1dff0e9714..d23dad90272788f2274d73fea9138374489cc715 100644 (file)
@@ -158,7 +158,6 @@ class Summoner {
         * @return string
         */
     static function b64sl_pack_id(int $id): string {
-       error_log($id);
         $id = intval($id);
         $ida = ($id > 0xFFFFFFFF ? $id >> 32 : 0);     // 32 bit big endian, top
         $idb = ($id & 0xFFFFFFFF);                     // 32 bit big endian, bottom