diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index dd4c951..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.gitignore b/android/.gitignore similarity index 100% rename from .gitignore rename to android/.gitignore diff --git a/.idea/codeStyles/Project.xml b/android/.idea/codeStyles/Project.xml similarity index 100% rename from .idea/codeStyles/Project.xml rename to android/.idea/codeStyles/Project.xml diff --git a/android/.idea/inspectionProfiles/Project_Default.xml b/android/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..146ab09 --- /dev/null +++ b/android/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/android/.idea/runConfigurations.xml similarity index 100% rename from .idea/runConfigurations.xml rename to android/.idea/runConfigurations.xml diff --git a/app/.gitignore b/android/app/.gitignore similarity index 100% rename from app/.gitignore rename to android/app/.gitignore diff --git a/app/build.gradle b/android/app/build.gradle similarity index 100% rename from app/build.gradle rename to android/app/build.gradle diff --git a/app/google-services.json b/android/app/google-services.json similarity index 100% rename from app/google-services.json rename to android/app/google-services.json diff --git a/app/proguard-rules.pro b/android/app/proguard-rules.pro similarity index 100% rename from app/proguard-rules.pro rename to android/app/proguard-rules.pro diff --git a/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml similarity index 100% rename from app/src/main/AndroidManifest.xml rename to android/app/src/main/AndroidManifest.xml diff --git a/app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java similarity index 85% rename from app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java rename to android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java index bdeadfe..dd9c996 100644 --- a/app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java +++ b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/FBMService.java @@ -35,16 +35,17 @@ public class FBMService extends FirebaseMessagingService // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ - Log.i("FB::MessageReceived<1>", "From: " + remoteMessage.getFrom()); + Log.i("FB::MessageReceived", "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { - Log.i("FB::MessageReceived<3>", "Payload: " + remoteMessage.getData()); + Log.i("FB::MessageReceived", "Payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { - Log.i("FB::MessageReceived<2>", "Payload: " + remoteMessage.getNotification().getBody()); + Log.i("FB::MessageReceived", "Notify_Title: " + remoteMessage.getNotification().getTitle()); + Log.i("FB::MessageReceived", "Notify_Body: " + remoteMessage.getNotification().getBody()); } // Also if you intend on generating your own notifications as a result of a received FCM diff --git a/app/src/main/java/com/blackforestbytes/simplecloudnotifier/MainActivity.java b/android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/MainActivity.java similarity index 100% rename from app/src/main/java/com/blackforestbytes/simplecloudnotifier/MainActivity.java rename to android/app/src/main/java/com/blackforestbytes/simplecloudnotifier/MainActivity.java diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml similarity index 100% rename from app/src/main/res/drawable-v24/ic_launcher_foreground.xml rename to android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/android/app/src/main/res/drawable/ic_launcher_background.xml similarity index 100% rename from app/src/main/res/drawable/ic_launcher_background.xml rename to android/app/src/main/res/drawable/ic_launcher_background.xml diff --git a/app/src/main/res/drawable/icon.png b/android/app/src/main/res/drawable/icon.png similarity index 100% rename from app/src/main/res/drawable/icon.png rename to android/app/src/main/res/drawable/icon.png diff --git a/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml similarity index 100% rename from app/src/main/res/layout/activity_main.xml rename to android/app/src/main/res/layout/activity_main.xml diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml similarity index 100% rename from app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml rename to android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml similarity index 100% rename from app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml rename to android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher.png b/android/app/src/main/res/mipmap-hdpi/ic_launcher.png similarity index 100% rename from app/src/main/res/mipmap-hdpi/ic_launcher.png rename to android/app/src/main/res/mipmap-hdpi/ic_launcher.png diff --git a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png similarity index 100% rename from app/src/main/res/mipmap-hdpi/ic_launcher_round.png rename to android/app/src/main/res/mipmap-hdpi/ic_launcher_round.png diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher.png b/android/app/src/main/res/mipmap-mdpi/ic_launcher.png similarity index 100% rename from app/src/main/res/mipmap-mdpi/ic_launcher.png rename to android/app/src/main/res/mipmap-mdpi/ic_launcher.png diff --git a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png similarity index 100% rename from app/src/main/res/mipmap-mdpi/ic_launcher_round.png rename to android/app/src/main/res/mipmap-mdpi/ic_launcher_round.png diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xhdpi/ic_launcher.png similarity index 100% rename from app/src/main/res/mipmap-xhdpi/ic_launcher.png rename to android/app/src/main/res/mipmap-xhdpi/ic_launcher.png diff --git a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png similarity index 100% rename from app/src/main/res/mipmap-xhdpi/ic_launcher_round.png rename to android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png similarity index 100% rename from app/src/main/res/mipmap-xxhdpi/ic_launcher.png rename to android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png diff --git a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png similarity index 100% rename from app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png rename to android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png similarity index 100% rename from app/src/main/res/mipmap-xxxhdpi/ic_launcher.png rename to android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png diff --git a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png b/android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png similarity index 100% rename from app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png rename to android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png diff --git a/app/src/main/res/values/colors.xml b/android/app/src/main/res/values/colors.xml similarity index 100% rename from app/src/main/res/values/colors.xml rename to android/app/src/main/res/values/colors.xml diff --git a/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml similarity index 100% rename from app/src/main/res/values/strings.xml rename to android/app/src/main/res/values/strings.xml diff --git a/app/src/main/res/values/styles.xml b/android/app/src/main/res/values/styles.xml similarity index 100% rename from app/src/main/res/values/styles.xml rename to android/app/src/main/res/values/styles.xml diff --git a/build.gradle b/android/build.gradle similarity index 100% rename from build.gradle rename to android/build.gradle diff --git a/gradle.properties b/android/gradle.properties similarity index 100% rename from gradle.properties rename to android/gradle.properties diff --git a/gradle/wrapper/gradle-wrapper.jar b/android/gradle/wrapper/gradle-wrapper.jar similarity index 100% rename from gradle/wrapper/gradle-wrapper.jar rename to android/gradle/wrapper/gradle-wrapper.jar diff --git a/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties similarity index 100% rename from gradle/wrapper/gradle-wrapper.properties rename to android/gradle/wrapper/gradle-wrapper.properties diff --git a/gradlew b/android/gradlew similarity index 100% rename from gradlew rename to android/gradlew diff --git a/gradlew.bat b/android/gradlew.bat similarity index 100% rename from gradlew.bat rename to android/gradlew.bat diff --git a/settings.gradle b/android/settings.gradle similarity index 100% rename from settings.gradle rename to android/settings.gradle diff --git a/web/.gitignore b/web/.gitignore new file mode 100644 index 0000000..d692dff --- /dev/null +++ b/web/.gitignore @@ -0,0 +1,183 @@ + +# Created by https://www.gitignore.io/api/git,windows,intellij,phpstorm+all + +### Git ### +# Created by git for backups. To disable backups in Git: +# $ git config --global mergetool.keepBackup false +*.orig + +# Created by git when using merge tools for conflicts +*.BACKUP.* +*.BASE.* +*.LOCAL.* +*.REMOTE.* +*_BACKUP_*.txt +*_BASE_*.txt +*_LOCAL_*.txt +*_REMOTE_*.txt + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/sonarlint + +### PhpStorm+all ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff + +# Generated files + +# Sensitive or high-churn files + +# Gradle + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules + +# CMake + +# Mongo Explorer plugin + +# File-based project format + +# IntelliJ + +# mpeltonen/sbt-idea plugin + +# JIRA plugin + +# Cursive Clojure plugin + +# Crashlytics plugin (for Android Studio and IntelliJ) + +# Editor-based Rest Client + +# Android studio 3.1+ serialized cache file + +### PhpStorm+all Patch ### +# Ignores the whole .idea folder and all .iml files +# See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 + +.idea/ + +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +# End of https://www.gitignore.io/api/git,windows,intellij,phpstorm+all + + + +################# + +config.php \ No newline at end of file diff --git a/web/lib/httpful.phar b/web/lib/httpful.phar new file mode 100644 index 0000000..87fb392 Binary files /dev/null and b/web/lib/httpful.phar differ diff --git a/web/model.php b/web/model.php new file mode 100644 index 0000000..7182c40 --- /dev/null +++ b/web/model.php @@ -0,0 +1,73 @@ + PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => false, + ]; + + return Statics::$DB = new PDO($dsn, $_config['user'], $_config['password'], $opt); +} + +function generateRandomAuthKey() +{ + $random = ''; + for ($i = 0; $i < 64; $i++) + + try { + switch (random_int(1, 3)) { + case 1: + $random .= chr(random_int(ord('0'), ord('9'))); + break; + case 2: + $random .= chr(random_int(ord('A'), ord('Z'))); + break; + case 3: + $random .= chr(random_int(ord('a'), ord('z'))); + break; + + } + } + catch (Exception $e) + { + die(json_encode(['success' => false, 'message' => 'Internal error - no randomness'])); + } + return $random; +} + +function sendPOST($url, $body, $header) +{ + $builder = \Httpful\Request::post($url); + + $builder->body($body); + + foreach ($header as $k => $v) $builder->addHeader($k, $v); + + $response = $builder->send(); + + if ($response->code != 200) throw new Exception("Repsponse code: " . $response->code); + + return $response->body; +} diff --git a/web/register.php b/web/register.php new file mode 100644 index 0000000..cd6a99c --- /dev/null +++ b/web/register.php @@ -0,0 +1,17 @@ + false, 'message' => 'Missing parameter [[fcm_token]]'])); + +$fcmtoken = $_GET['fcm_token']; +$user_key = generateRandomAuthKey(); + +$pdo = getDatabase(); + +$stmt = $pdo->prepare('INSERT INTO users (user_key, fcm_token, timestamp_accessed) VALUES (:key, :token, NOW())'); +$stmt->execute(['key' => $user_key, 'token' => $fcmtoken]); +$user_id = $pdo->lastInsertId('user_id'); + +echo json_encode(['success' => true, 'user_id' => $user_id, 'user_key' => $user_key, 'message' => 'new user registered']); +return 0; \ No newline at end of file diff --git a/web/schema.sql b/web/schema.sql new file mode 100644 index 0000000..048e43d --- /dev/null +++ b/web/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE `users` +( + `id` INT(11) NOT NULL AUTO_INCREMENT, + `auth_token` VARCHAR(64) NOT NULL, + `fcm_token` VARCHAR(256) NULL DEFAULT NULL, + `messages_sent` INT(11) NOT NULL DEFAULT '0', + `timestamp_created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + `timestamp_accessed` DATETIME NULL DEFAULT NULL, + PRIMARY KEY (`id`) +); diff --git a/web/send.php b/web/send.php new file mode 100644 index 0000000..7259dfb --- /dev/null +++ b/web/send.php @@ -0,0 +1,72 @@ + false, 'message' => 'Missing parameter [[user_id]]'])); +if (!isset($_GET['user_key'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[user_token]]'])); +if (!isset($_GET['message_title'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[message_title]]'])); + +$user_id = $_GET['user_id']; +$user_key = $_GET['user_key']; +$message = $_GET['message_title']; +$content = isset($_POST['message_content']) ? $_POST['message_content'] : ''; + +//---------------------- + +$pdo = getDatabase(); + +$stmt = $pdo->prepare('SELECT user_id, user_key, fcm_token FROM users WHERE user_id = :uid LIMIT 1'); +$stmt->execute(['uid' => $user_id]); + +$datas = $stmt->fetchAll(PDO::FETCH_ASSOC); +if (count($datas)<=0) die(json_encode(['success' => false, 'message' => 'No User found'])); +$data = $datas[0]; + +if ($data === null) die(json_encode(['success' => false, 'message' => 'User not found'])); +if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'message' => 'UserID not found'])); +if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'message' => 'Authentification failed'])); + +$fcm = $data['fcm_token']; + + +$url = "https://fcm.googleapis.com/fcm/send"; +$payload = json_encode( +[ + 'to' => $fcm, + //'dry_run' => true, + 'notification' => + [ + 'title' => $message, + 'body' => $content, + ], + 'data' => + [ + 'title' => $message, + 'body' => $content, + ] +]); +$header= +[ + 'Authorization' => 'key=' . getConfig()['firebase']['server_key'], + 'Content-Type' => 'application/json', +]; + +try +{ + $httpresult = sendPOST($url, $payload, $header); +} +catch (Exception $e) +{ + die(json_encode(['success' => false, 'message' => 'Exception: ' . $e->getMessage()])); +} + +var_dump($httpresult); + + +$stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), messages_sent=messages_sent+1 WHERE user_id = :uid'); +$stmt->execute(['uid' => $user_id]); + + +echo (json_encode(['success' => true, 'message' => 'Message sent'])); +return 0; \ No newline at end of file diff --git a/web/update.php b/web/update.php new file mode 100644 index 0000000..bf29a15 --- /dev/null +++ b/web/update.php @@ -0,0 +1,46 @@ + false, 'message' => 'Missing parameter [[user_id]]'])); +if (!isset($_GET['user_key'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[user_key]]'])); +if (!isset($_GET['message'])) die(json_encode(['success' => false, 'message' => 'Missing parameter [[message]]'])); + +$user_id = $_GET['user_id']; +$user_key = $_GET['token']; +$fcm_token = isset($_GET['token']) ? $_GET['token'] : null; + +//---------------------- + +$pdo = getDatabase(); + +$stmt = $pdo->prepare('SELECT user_id, user_key FROM users WHERE user_id = :uid LIMIT 1'); +$stmt->execute(['uid' => $user_id]); + +$datas = $stmt->fetchAll(PDO::FETCH_ASSOC); +if (count($datas)<=0) die(json_encode(['success' => false, 'message' => 'No User found'])); +$data = $datas[0]; + +if ($data === null) die(json_encode(['success' => false, 'message' => 'User not found'])); +if ($data['user_id'] !== (int)$user_id) die(json_encode(['success' => false, 'message' => 'UserID not found'])); +if ($data['user_key'] !== $user_key) die(json_encode(['success' => false, 'message' => 'Authentification failed'])); + +$new_userkey = generateRandomAuthKey(); + +if ($fcm_token === null) +{ + $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), user_key=:at WHERE user_id = :uid'); + $stmt->execute(['uid' => $user_id, 'at' => $new_userkey]); + + echo json_encode(['success' => true, 'user_id' => $user_id, 'user_key' => $new_userkey, 'message' => 'user updated']); + return 0; +} +else +{ + $stmt = $pdo->prepare('UPDATE users SET timestamp_accessed=NOW(), fcm_token=:ft, user_key=:at WHERE user_id = :uid'); + $stmt->execute(['uid' => $user_id, 'ft' => $fcm_token, 'at' => $new_userkey]); + + echo json_encode(['success' => true, 'user_id' => $user_id, 'user_key' => $new_userkey, 'message' => 'user updated']); + return 0; +} \ No newline at end of file