diff --git a/api/external_ping_api/README b/api/external_ping_api/README new file mode 100644 index 00000000..7e9ae590 --- /dev/null +++ b/api/external_ping_api/README @@ -0,0 +1,4 @@ + +README / Instruction: + + diff --git a/api/external_ping_api/config_sample.php b/api/external_ping_api/config_sample.php new file mode 100644 index 00000000..1680d6a3 --- /dev/null +++ b/api/external_ping_api/config_sample.php @@ -0,0 +1,21 @@ + \ No newline at end of file diff --git a/api/external_ping_api/data/unze4u.txt b/api/external_ping_api/data/unze4u.txt new file mode 100644 index 00000000..47ecedde --- /dev/null +++ b/api/external_ping_api/data/unze4u.txt @@ -0,0 +1 @@ +1607040433 \ No newline at end of file diff --git a/api/external_ping_api/index.php b/api/external_ping_api/index.php new file mode 100644 index 00000000..50573f3c --- /dev/null +++ b/api/external_ping_api/index.php @@ -0,0 +1,273 @@ +"; + + +/* +Identifier: CKrQsyvQRNJIMjZxb8JkkYoQTkJHqMI4jBcONhX1Yvsn9ZmgrFqRt9iQYNNMHYOZ6AHQhv7qFQUmBflxEe3DNMExHuVNDWFNboaIGLaaR391m8b4NApkI97ty4D8RaMG8WugCFGmeSBHwp6tS4fpay +Date: 03.12.2020 +*/ +echo ""; + + + + +/* +############################################################################################## +CODING +############################################################################################## +*/ + + + +//Imports +//Check key - $_GET["key"]; is read by keyit.php +include_once("keyit.php"); +//Load options +include_once("config.php"); + + +//Check if script is active +if($is_active==false){ + exit(); +} + + + + +//Get vars to choose action + + + //load and check GET vars + $index = check_and_give_me_get_input("i"); //index + + + $index_value = check_and_give_me_get_input("iv"); //index_value + + + + +//check if both vars are set +if (isset($index) == false || isset($index_value)== false){ + throw new Exception('GET vars index (i) and index_value (iv) are not set correctly, try it by manual.'); +}else{ +//run get or set + +//check if they are set +switch ($index) { + case "get": + //GET + if (run_get($index_value,$old_time_definition)==true){}else{ + //Error + throw new Exception('run_get function failed.'); + } + break; + case "set": + //SET + if (run_set($index_value)==true){}else{ + //Error + throw new Exception('run_set function failed.'); + } + + break; + default: + throw new Exception('GET vars index (i) and index_value (iv) are not set correctly, try it by manual.'); +} + + +} + + + + + +// Functions + + +//Main Functions +function run_get($index_value, $old_time_definition){ + //make HTML comment + echo ""; + + // generate file path + $file = "data/" . $index_value . ".txt"; + + //check if file exist + if (test_file_existing($file)==false){ + + if($create_non_exsisting_files=true){ + //create new dot file + //Write timestamp in new file + write_in_file($file,"0"); + } + else{ + //Throw exception, user should add file + throw new Exception('index: ' . $index_value . " unknown. Please create it first."); + } + } + + //Open File + $information_file = read_from_file($file); + + //Get now timestamp + $date = new DateTime(); + $timestamp_now = $date->getTimestamp(); + + //calculate time diff + $time_diff = $timestamp_now - $information_file ; + + + // Check if older than + if ($information_file + ($old_time_definition) < $timestamp_now){ + + + + //Last updtae is older than x time + echo "Older than " . $old_time_definition . "sec. Timediff: " . $time_diff . " sec." ; + + + }else{ + // Last update is NOT older than x time + echo "Last update " . $information_file . "Timediff: " . $time_diff . " sec." ; + + + } + + + + //Secure file + secure_file($file); + + + //giveback ok + return true; + + +} + + +function run_set($index_value){ + //make HTML comment + echo ""; + + + // generate file path + $file = "data/" . $index_value . ".txt"; + + + //Get now timestamp + $date = new DateTime(); + $timestamp_now = $date->getTimestamp(); + + + //Write update timestamp in file + write_in_file($file,$timestamp_now); + + + // Giveout OK + echo "OK: " . $timestamp_now ; + + + //Secure file + secure_file($file); + + + //return ok + return true; +} + + + + + +// Subfunctions + + +function check_and_give_me_get_input($get_var_name_input){ + + if ((isset($_GET[$get_var_name_input])) && (!empty($_GET[$get_var_name_input]))) { + + if(ctype_alnum($_GET[$get_var_name_input])==true){ + //allright + return $_GET[$get_var_name_input]; + }else{ + // Get Var is empty + throw new Exception('GET var: ' . $get_var_name_input . " are not only numbers or letters."); + } + + }else{ + // Get Var is empty + throw new Exception('GET var: ' . $get_var_name_input . " is empty."); + } + +} +function read_from_file($file_name){ + + try { +//Open File + $myfile = fopen($file_name, "r") or die("Unable to open file!:" . $file_name); + + //Read File + $information = fread($myfile,filesize($file_name)); + //Close file + fclose($myfile); + + + return $information; + } catch (Exception $e) { + throw new Exception( $e->getMessage()); + } + +} + + +function write_in_file($file_name,$text){ + + try { + + $myfile = fopen($file_name, "w") or die("Unable to open file!:" . $file_name); + fwrite($myfile, $text); + fclose($myfile); + + return true; + + } catch (Exception $e) { + throw new Exception( $e->getMessage()); + } + +} + + +function test_file_existing($file_name){ + + if (file_exists($file_name)) { + return true; + } else { + return false; + } + +} + + + + +function secure_file($file_path){ + try { + chmod($file_path, 0600); + } catch (Exception $e) { + throw new Exception( "WARNING: The program was not able to secure the " . $file_path . " Errordetails:" . $e->getMessage()); + } + } + + + +?> \ No newline at end of file diff --git a/api/external_ping_api/key.config b/api/external_ping_api/key.config new file mode 100644 index 00000000..d1a86c18 --- /dev/null +++ b/api/external_ping_api/key.config @@ -0,0 +1 @@ +WTn9cYxz5PWadaVqEdki0FwhmLkjje5RXgDVTAlWdq3Uvv5hR1i0TI1AFN7Z \ No newline at end of file diff --git a/api/external_ping_api/keyit.php b/api/external_ping_api/keyit.php new file mode 100644 index 00000000..8e4a0feb --- /dev/null +++ b/api/external_ping_api/keyit.php @@ -0,0 +1,191 @@ +"; + + +/* +Identifier: sj3SjMAfJDRIK5YLWkcprM1gVYjgmZuUHK1bJ5Qa96m6ARkK5hMysb5ruC4oxXYjiWNcCuYvJ7mkSKRzMKNXdiFLeoY2Wn1jYTNAGqXoqBYyu1eFrvWm3Pj7y7soMwULKDxQYiKZTs43xPDiPmVjhM +Date: 03.12.2020 +*/ +echo ""; + + + +//Load options +include_once("config.php"); + + + +//check file exist +if (file_exists($filename)) { + + + $key = $_GET[$key_name]; + + //open file and read it + $myfile = fopen($filename, "r") or die("keyit.php: 1 Unable to open file!"); + $filekey =fread($myfile,filesize($filename)); + + + if($key == $filekey) + { + //Right key + + + //Check if we need a new key (new key every 120 days) + + + + //check if it is more than x days + $lastmodified= filemtime($filename); + + + //whats now daytime + $datetime = new DateTime(); + $datetime = $datetime->getTimestamp(); + + + /*** if file is 24 hours (86400 seconds) old then delete it ***/ + //8035200 sek ~3 monate + //5356800 sek ~ 2 Monate + //3456000 sek ~ 40 tage + //2678400 sek ~ 1 Monat + //1209600 sek ~ 14 Tage + //864000 sek ~ 10 tage + + + if( $lastmodified + ($option_newkeydays*(24*60*60)) < $datetime){ + //Its longer than x days, create new key + + + //generate new key + $newkey = createnewkeyfile($filename, $key_length ); + + //Send mail to recipients +$mail_subject ="Key changing: " . date("Y.m.d H:i:s");; +$mail_message="The key of the file " . getCurrentUrl() . " has changed to: " . $newkey; +sendmailtorecipients($email_ricipiants_contacts,$mail_subject,$mail_message,false); + + + + }else{ +//Key is not old enought to get changed + } + + + + + }else{ + //Wrong key + header('HTTP/1.0 403 Forbidden'); + echo 'keyit.php: You are forbidden!'; + exit; + die; + } + + + + + +}else{ + //File does not exist + //Create it with key + $newkey = createnewkeyfile($filename,$key_length ); + + //Send mail to recipients +$mail_subject ="Key created: " . date("Y.m.d H:i:s");; +$mail_message="The key of the file " . getCurrentUrl() . " has created: " . $newkey; +sendmailtorecipients($email_ricipiants_contacts,$mail_subject,$mail_message,false); + +} + + + + + + +//Secure file agains reading +securekeyfileagainstreading($filename); + + + + + +//Funktionen / Functions + +//########################################################### + +function generateRandomString($length = 10) { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; +} + + +function writeinfile($filename,$text){ + $myfile = fopen($filename, "w") or die("keyit.php: 2 Unable to open file!"); + fwrite($myfile, $text); + fclose($myfile); +} + + + +function createnewkeyfile($filename, $key_length ){ + +//create new key +$newkey = generateRandomString($key_length); + + //create new file and safe it + writeinfile($filename,$newkey); + //secure key file + securekeyfileagainstreading($filename); + + +return $newkey; + +} + +function securekeyfileagainstreading($filename){ + chmod($filename, 0600); +} + + +function sendmailtorecipients($contacts_array,$subject,$message,$output=false){ +// $contacts array +// $contacts = array("youremailaddress@yourdomain.com","youremailaddress@yourdomain.com"); +//....as many email address as you need + + foreach($contacts_array as $contact) { + + $to = $contact; + mail($to, $subject, $message); + + //Outpu of sending message + if($output == true){ + echo "keyit.php: Send mail to " . $to . "with the subject " . $subject . " and the text " . $message . "...
"; + } + + } + + +} + + + function getCurrentUrl() { + return ((empty($_SERVER['HTTPS'])) ? 'http' : 'https') . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; + } + +?> \ No newline at end of file