diff --git a/.github/workflows/pr_notification.yml b/.github/workflows/pr_notification.yml deleted file mode 100644 index de44c8b..0000000 --- a/.github/workflows/pr_notification.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Pull Request Notification - -on: - pull_request_target: - types: - - opened - -jobs: - notify: - uses: KeyAuth/.github/.github/workflows/pr_notification_global.yml@main - secrets: - DISCORD_PR: ${{ secrets.DISCORD_PR }} diff --git a/.gitignore b/.gitignore index ea9cf22..02e9551 100644 --- a/.gitignore +++ b/.gitignore @@ -1,45 +1,55 @@ # Nelson Cybersecurity LLC # keyauth.cc - # private api/admin/ + +# private api/discord/ + +# private api/upgrade/ + +# private api/status/ -api/dashboard/ -app/pages/admin-panel.php -app/pages/upgrade.php -app/pages/support.php -changeEmail/ -changeUsername/ -deleteAccount/ -forgot/ -owner/ -paddle/ -purge-redis/ -reset/ -resetUser/ -whoami/ -vendor/ -login/accShare/ -login/emailVerify/ -sitemap.xml -includes/credentials.php -includes/redis.php -app/pages/application-functions.php -app/pages/application-prompts.php -app/pages/bug-bounty.php -app/pages/forms.php -app/pages/staff-panel.php # purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 api/reseller/ + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 api/seller/ + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 panel/ + +#private +app/pages/admin-panel.php + +#private +app/pages/upgrade.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/manage-accs.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/reseller-balance.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/reseller-licenses.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/reseller-users.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/seller-settings.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/seller-logs.php + +# purchase KeyAuth enterprise for these files https://shop.keyauth.cc/product/6361c20e415e9 app/pages/webloader.php + +# contains MySQL credentials, discord webhooks, and API keys +includes/connection.php + +# contains Redis password +/includes/redis.php \ No newline at end of file diff --git a/README.md b/README.md index 6a8df11..179b8c1 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,19 @@ -# KeyAuth Source Code -> Please star :star2: - -> [!WARNING] -> AS OF JANUARY 29th, 2024 - THIS REPOSITORY IS NO LONGER BEING UPDATED! - -[KeyAuth](https://keyauth.win) is an open source authentication system with [cloud-hosted subscriptions](https://keyauth.win/app/?page=upgrade) available as well. +# KeyAuth-Source-Code +KeyAuth is an open source authentication system with cloud-hosted subscriptions available aswell https://keyauth.cc
*You're not allowed to sell KeyAuth. source made avaliable only for you to use* -To submit a suggestion or a bug, please go to https://keyauth.cc/app/?page=forms and submit a form! +View #suggestions and #bugs channel in Discord server if you want to suggest new feature or submit bug https://discord.gg/keyauth [![CodeFactor](https://www.codefactor.io/repository/github/keyauth/keyauth-source-code/badge)](https://www.codefactor.io/repository/github/keyauth/keyauth-source-code) -[![Telegram Chat](https://img.shields.io/endpoint?color=neon&style=flat-square&url=https%3A%2F%2Ftg.sumanjay.workers.dev%2Fkeyauth)](https://t.me/KeyAuth) +[![Discord](https://img.shields.io/discord/1071604144776945814?label=Discord&cacheSeconds=3600)](https://discord.com/invite/keyauth) [![Twitter](https://img.shields.io/twitter/follow/KeyAuth?cacheSeconds=3600)](https://twitter.com/KeyAuth) -[![Screenshot](https://cdn.keyauth.cc/v3/imgs/Screenshot%20from%202025-07-04%2012-07-30.png)](https://keyauth.cc) +[![Screenshot](https://i.imgur.com/rige1nM.png)](https://keyauth.cc) ## Bugs ## -If the default example that isn't added to your software isn't functioning how it should, please go to https://keyauth.cc/app/?page=forms and submit a bug report in the `Report a Bug` modal. +If the default example not added to your software isn't functioning how it should, please join the Discord server https://discord.gg/keyauth and submit the issue in the `#bugs` channel. However, we do **NOT** provide support for adding KeyAuth to your project. If you can't figure this out you should use Google or YouTube to learn more about the programming language you want to sell a program in. @@ -46,12 +42,12 @@ Thank you for your compliance, we work hard on the development of KeyAuth and do - Redis - PHP -No setup support given to non-enterprise users. However for $79.99 you can purchase access to the source code of paid features (developer and seller plan features), setup support 1-on-1 with the owner of KeyAuth, and a tutorial video on how to host KeyAuth for 100% free forever (good preformance & secure also) +No setup support given to non-enterprise users. However for $79.99 you can purchase access to the source code of paid features (developer and seller plan features), setup support one-on-one with the Founder of KeyAuth, and a tutorial video on how to host KeyAuth for 100% free forever (good preformance & secure also) -Purchase enterprise today --> https://keyauth.win/app/?page=upgrade +Purchase enterprise today --> https://shop.keyauth.cc/product/6361c20e415e9 -## Updates ## +Some pages such as the API endpoint that upgrades users after they purchase a subscription have been omitted to prevent violation of the license (No Commercial Access Allowed) -https://keyauth.canny.io/changelog +## Updates ## -Looking for a Discord bot made by the KeyAuth & RestoreCord founder that you can use to backup your Discord members, server settings, and messages? Go to https://vaultcord.com +https://headwayapp.co/keyauth-changelog/ diff --git a/api/1.0/index.php b/api/1.0/index.php index 7c56d9a..72422c4 100644 --- a/api/1.0/index.php +++ b/api/1.0/index.php @@ -1,37 +1,19 @@ getMessage()); - die(json_encode(array("success" => false, "message" => "Error: " . $errorMsg))); -}); - -if(empty($_POST['ownerid'])) { - die(json_encode(array("success" => false, "message" => "No OwnerID specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} - -if(empty($_POST['name'])) { - die(json_encode(array("success" => false, "message" => "No app name specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} - -if(strlen(hex2bin($_POST['ownerid'])) != 10) { - die(json_encode(array("success" => false, "message" => "OwnerID should be 10 characters long. Select app & copy code snippet from https://keyauth.cc/app/"))); +if (isset($_SERVER['HTTP_CDN_HOST'])) { // custom domains https://www.youtube.com/watch?v=a2SROFJ0eYc + $row = misc\cache\fetch('KeyAuthApp:' . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']), "SELECT * FROM `apps` WHERE `customDomainAPI` = '" . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']) . "'", 0); +} else { + $ownerid = misc\etc\sanitize(hex2bin($_POST['ownerid'])); // ownerid of account that owns application + $name = misc\etc\sanitize(hex2bin($_POST['name'])); // application name + $row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = '$ownerid' AND `name` = '$name'", 0); } -$ownerid = misc\etc\sanitize(hex2bin($_POST['ownerid'])); // ownerid of account that owns application -$name = misc\etc\sanitize(hex2bin($_POST['name'])); // application name -$row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = ? AND `name` = ?", [$ownerid, $name], 0); - if ($row == "not_found") { die("KeyAuth_Invalid"); } @@ -48,8 +30,6 @@ $appdisabled = $row['appdisabled']; $hashcheck = $row['hashcheck']; $serverhash = $row['hash']; -$sessionexpiry = $row['session']; -$forceHwid = $row['forceHwid']; $banned = $row['banned']; $owner = $row['owner']; @@ -66,11 +46,14 @@ $noactivesubs = $row['noactivesubs']; $hwidblacked = $row['hwidblacked']; $pausedsub = $row['pausedsub']; +$keyexpired = $row['keyexpired']; $vpnblocked = $row['vpnblocked']; $keybanned = $row['keybanned']; $userbanned = $row['userbanned']; $sessionunauthed = $row['sessionunauthed']; $hashcheckfail = $row['hashcheckfail']; +$sessionexpiry = $row['session']; +$killOtherSessions = $row['killOtherSessions']; // why using null coalescing operators? because if I add a field and it's not in redis cache, it'll be NULL $loggedInMsg = $row['loggedInMsg'] ?? "Logged in!"; @@ -78,8 +61,7 @@ $unTooShort = $row['unTooShort'] ?? "Username too short, try longer one."; $pwLeaked = $row['pwLeaked'] ?? "This password has been leaked in a data breach (not from us), please use a different one."; $chatHitDelay = $row['chatHitDelay'] ?? "Chat slower, you've hit the delay limit"; -$minHwid = $row['minHwid'] ?? 20; - + if ($banned) { die(api\v1_0\Encrypt(json_encode(array( "success" => false, @@ -87,25 +69,18 @@ )), $secret)); } -if ($_GET['host'] == "keyauth.business") { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Please tell the developer of this program to use latest API domain. This domain is old, it will expire in a month." - )), $secret)); -} - switch (hex2bin($_POST['type'])) { case 'init': $ip = api\shared\primary\getIp(); if ($vpnblock) { if (api\shared\primary\vpnCheck($ip)) { - $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = ? AND `app` = ?", [$ip, $secret], 0); - if ($row == "not_found") { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "$vpnblocked" - )), $secret)); - } + $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = '$ip' AND `app` = '$secret'", 0); + if($row == "not_found") { + die(api\v1_0\Encrypt(json_encode(array( + "success" => false, + "message" => "$vpnblocked" + )), $secret)); + } } } @@ -139,7 +114,8 @@ if ($hashcheck) { if (strpos($serverhash, $hash) === false) { if (is_null($serverhash)) { - misc\mysql\query("UPDATE `apps` SET `hash` = ? WHERE `secret` = ?", [$hash, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "UPDATE `apps` SET `hash` = '$hash' WHERE `secret` = '$secret'"); misc\cache\purge('KeyAuthApp:' . $name . ':' . $ownerid); // flush cache for application so new hash takes precedent } else { die(api\v1_0\Encrypt(json_encode(array( @@ -151,51 +127,30 @@ } $enckey = misc\etc\sanitize(api\v1_0\Decrypt($_POST['enckey'], $secret)); + $sessionid = misc\etc\generateRandomString(); + // session init + $time = time() + $sessionexpiry; + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `enckey`,`ip`) VALUES ('$sessionid','$secret', '$time', '".time()."', '$enckey', '$ip')"); - $newSession = false; - $duplicateSession = misc\cache\select("KeyAuthSessionDupe:$secret:$ip"); - if($duplicateSession) { - $sessionid = $duplicateSession; - $updateSession = misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("enckey" => $enckey)); - if(!$updateSession) { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; - } - } - else { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; - } - - // $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT (SELECT COUNT(1) FROM `users` WHERE `app` = ?) AS 'numUsers', (SELECT COUNT(1) FROM `sessions` WHERE `app` = ? AND `validated` = 1 AND `expiry` > ?) AS 'numOnlineUsers', (SELECT COUNT(1) FROM `keys` WHERE `app` = ?) AS 'numKeys' FROM dual", [$secret, $secret, time(), $secret], 0, 3600); + $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT(select count(1) FROM `users` WHERE `app` = '$secret') AS 'numUsers',(select count(1) FROM `sessions` WHERE `app` = '$secret' AND `validated` = 1 AND `expiry` > " . time() . ") AS 'numOnlineUsers',(select count(1) FROM `keys` WHERE `app` = '$secret') AS 'numKeys';", 0, 1800); - $numUsers = "N/A - Use fetchStats() function in latest example"; - $numOnlineUsers = "N/A - Use fetchStats() function in latest example"; - $numKeys = "N/A - Use fetchStats() function in latest example"; + $numUsers = $row['numUsers']; + $numOnlineUsers = $row['numOnlineUsers']; + $numKeys = $row['numKeys']; - echo api\v1_0\Encrypt(json_encode(array( + die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "Initialized", "sessionid" => $sessionid, "appinfo" => array( - "numUsers" => "$numUsers", - "numOnlineUsers" => "$numOnlineUsers", - "numKeys" => "$numKeys", - "version" => "$currentver", + "numUsers" => $numUsers, + "numOnlineUsers" => $numOnlineUsers, + "numKeys" => $numKeys, + "version" => $currentver, "customerPanelLink" => "https://keyauth.cc/panel/$owner/$name/" ) - )), $secret); - - fastcgi_finish_request(); - - if($newSession) { - misc\cache\insert("KeyAuthState:$secret:$sessionid", serialize(array("credential" => NULL, "enckey" => $enckey, "validated" => 0)), $sessionexpiry); - $time = time() + $sessionexpiry; - misc\mysql\query("INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `enckey`,`ip`) VALUES (?, ?, ?, ?, ?, ?)", [$sessionid, $secret, $time, time(), $enckey, $ip]); - $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - misc\cache\insert("KeyAuthSessionDupe:$secret:$ip", $sessionid, $sessionexpiry); - } + )), $secret)); case 'register': // retrieve session info @@ -206,27 +161,13 @@ // Read in username $username = misc\etc\sanitize(api\v1_0\Decrypt($_POST['username'], $enckey)); - if(strlen($username) > 70) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Username must be shorter than 70 characters" - )), $enckey)); - } - // Read in license key $checkkey = misc\etc\sanitize(api\v1_0\Decrypt($_POST['key'], $enckey)); - if(strlen($checkkey) > 70) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - )), $enckey)); - } - // Read in password $password = misc\etc\sanitize(api\v1_0\Decrypt($_POST['pass'], $enckey)); - - // Read in email + + // Read in email $email = misc\etc\sanitize(api\v1_0\Decrypt($_POST['email'], $enckey)); // Read in hwid @@ -261,8 +202,9 @@ )), $enckey)); case 'key_banned': if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -281,8 +223,13 @@ "message" => "$nosublevel" )), $enckey)); default: - misc\mysql\query("UPDATE `sessions` SET `credential` = ?,`validated` = 1 WHERE `id` = ? AND `app` = ?", [$username, $sessionid, $secret]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `credential` = '$username',`validated` = 1 WHERE `id` = '$sessionid' AND `app` = '$secret'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -301,21 +248,22 @@ // Read in key $checkkey = misc\etc\sanitize(api\v1_0\Decrypt($_POST['key'], $enckey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL // search for key - $query = misc\mysql\query("SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = ? AND `app` = ?", [$checkkey, $secret]); + $result = mysqli_query($link, "SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = '$checkkey' AND `app` = '$secret'"); // check if key exists - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { die(api\v1_0\Encrypt(json_encode(array( "success" => false, "message" => "$keynotfound" )), $enckey)); } // if key does exist - elseif ($query->num_rows > 0) { + elseif (mysqli_num_rows($result) > 0) { // get key info - while ($row = mysqli_fetch_array($query->result)) { + while ($row = mysqli_fetch_array($result)) { $expires = $row['expires']; $status = $row['status']; $level = $row['level']; @@ -329,11 +277,11 @@ "message" => "$keyused" )), $enckey)); } - + if (!is_null($banned)) { - if (strpos($keybanned, '{reason}') !== false) { - $keybanned = str_replace("{reason}", $banned, $keybanned); - } + if (strpos($keybanned, '{reason}') !== false) { + $keybanned = str_replace("{reason}", $banned, $keybanned); + } die(api\v1_0\Encrypt(json_encode(array( "success" => false, "message" => "$keybanned" @@ -343,8 +291,8 @@ // add current time to key time $expiry = $expires + time(); - $query = misc\mysql\query("SELECT `name` FROM `subscriptions` WHERE `app` = ? AND `level` = ?", [$secret, $level]); - $subName = mysqli_fetch_array($query->result)['name']; + $result = mysqli_query($link, "SELECT `name` FROM `subscriptions` WHERE `app` = '$secret' AND `level` = '$level'"); + $subName = mysqli_fetch_array($result)['name']; $resp = misc\user\extend($username, $subName, $expiry, 0, $secret); switch ($resp) { @@ -365,9 +313,9 @@ )), $enckey)); case 'success': // set key to used, and set usedby - misc\mysql\query("UPDATE `keys` SET `status` = 'Used', `usedon` = ?, `usedby` = ? WHERE `key` = ? AND `app` = ?", [time(), $username, $checkkey, $secret]); + mysqli_query($link, "UPDATE `keys` SET `status` = 'Used', `usedon` = '" . time() . "', `usedby` = '$username' WHERE `key` = '$checkkey' AND `app` = '$secret'"); misc\cache\purge('KeyAuthKeys:' . $secret . ':' . $checkkey); - misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); + misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "Upgraded successfully" @@ -395,20 +343,6 @@ // Read in password $password = misc\etc\sanitize(api\v1_0\Decrypt($_POST['pass'], $enckey)); - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - )), $enckey)); - } - - if($forceHwid && is_null($hwid)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - )), $enckey)); - } - $resp = api\v1_0\login($username, $password, $hwid, $secret, $hwidenabled); switch ($resp) { case 'un_not_found': @@ -423,8 +357,9 @@ )), $enckey)); case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $username]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$username'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -453,8 +388,13 @@ "message" => "$noactivesubs" )), $enckey)); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ? AND `app` = ?", [$username, $sessionid, $secret]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$username' WHERE `id` = '$sessionid' AND `app` = '$secret'"); + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -469,33 +409,13 @@ $enckey = $session["enckey"]; $checkkey = misc\etc\sanitize(api\v1_0\Decrypt($_POST['key'], $enckey)); - if(strlen($checkkey) > 70) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - )), $enckey)); - } - $hwid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['hwid'], $enckey)); - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - )), $enckey)); - } - - if($forceHwid && is_null($hwid)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - )), $enckey)); - } - $resp = api\v1_0\login($checkkey, $checkkey, $hwid, $secret, $hwidenabled); switch ($resp) { case 'un_not_found': break; // user not registered yet or user was deleted + case 'hwid_mismatch': die(api\v1_0\Encrypt(json_encode(array( "success" => false, @@ -503,8 +423,9 @@ )), $enckey)); case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -533,8 +454,13 @@ "message" => "$noactivesubs" )), $enckey)); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -572,8 +498,9 @@ )), $enckey)); case 'key_banned': if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -592,8 +519,13 @@ "message" => "$nosublevel" )), $enckey)); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -605,7 +537,7 @@ $session = api\shared\primary\getSession($sessionid, $secret); $enckey = $session["enckey"]; - $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT CONCAT(LEFT(`credential`, 10), IF(LENGTH(`credential`) > 10, REPEAT('*', LENGTH(`credential`) - 10), '')) AS `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = ?", [$secret], 1, 1800); + $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = '$secret'", 1, 1800); if ($rows == "not_found") { die(api\v1_0\Encrypt(json_encode(array( @@ -633,42 +565,11 @@ $var = misc\etc\sanitize(api\v1_0\Decrypt($_POST['var'], $enckey)); $data = misc\etc\sanitize(api\v1_0\Decrypt($_POST['data'], $enckey)); - if(is_null($var)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "No variable name provided" - )), $enckey)); - } - - if(is_null($data)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "No variable data provided" - )), $enckey)); - } + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL - if(strlen($data) > 500) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Variable data must be 500 characters or less" - )), $enckey)); - } - - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); - - if ($row != "not_found") { - $readOnly = $row["readOnly"]; - if ($readOnly) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Variable is read only" - )), $enckey)); - } - } + mysqli_query($link, "REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES ('$var', '$data', '" . $session["credential"] . "', '$secret')"); - $query = misc\mysql\query("REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES (?, ?, ?, ?)", [$var, $data, $session["credential"], $secret]); - - if ($query->affected_rows != 0) { + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"]); die(api\v1_0\Encrypt(json_encode(array( "success" => true, @@ -693,7 +594,7 @@ $var = misc\etc\sanitize(api\v1_0\Decrypt($_POST['var'], $enckey)); - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); + $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data` FROM `uservars` WHERE `name` = '$var' AND `user` = '" . $session["credential"] . "' AND `app` = '$secret'", 0); if ($row == "not_found") { die(api\v1_0\Encrypt(json_encode(array( @@ -715,7 +616,7 @@ $enckey = $session["enckey"]; $varid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['varid'], $enckey)); - $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = ? AND `app` = ?", [$varid, $secret], 0); + $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = '$varid' AND `app` = '$secret'", 0); if ($row == "not_found") { die(api\v1_0\Encrypt(json_encode(array( "success" => false, @@ -748,7 +649,7 @@ $hwid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['hwid'], $enckey)); $ip = api\shared\primary\getIp(); - $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = ? OR `ip` = ?) AND `app` = ?", [$hwid, $ip, $secret], 0); + $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = '$hwid' OR `ip` = '$ip') AND `app` = '$secret'", 0); if ($row != "not_found") { die(api\v1_0\Encrypt(json_encode(array( @@ -774,9 +675,9 @@ } $channel = misc\etc\sanitize(api\v1_0\Decrypt($_POST['channel'], $enckey)); - $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ?", [$channel, $secret], 1); + $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret'", 1); - if ($rows == "not_found") { + if ($rows == "not_found") { $rows = []; } @@ -796,23 +697,24 @@ "message" => "$sessionunauthed" )), $enckey)); } + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL $channel = misc\etc\sanitize(api\v1_0\Decrypt($_POST['channel'], $enckey)); - $query = misc\mysql\query("SELECT `delay` FROM `chats` WHERE `name` = ? AND `app` = ?", [$channel, $secret]); + $result = mysqli_query($link, "SELECT `delay` FROM `chats` WHERE `name` = '$channel' AND `app` = '$secret'"); - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { die(api\v1_0\Encrypt(json_encode(array( "success" => false, "message" => "Chat channel not found" )), $enckey)); } - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $delay = $row['delay']; $credential = $session["credential"]; - $query = misc\mysql\query("SELECT `timestamp` FROM `chatmsgs` WHERE `author` = ? AND `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 1", [$credential, $channel, $secret]); + $result = mysqli_query($link, "SELECT `timestamp` FROM `chatmsgs` WHERE `author` = '$credential' AND `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 1"); - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $time = $row['timestamp']; if (time() - $time < $delay) { @@ -822,9 +724,9 @@ )), $enckey)); } - $query = misc\mysql\query("SELECT `time` FROM `chatmutes` WHERE `user` = ? AND `app` = ?", [$credential, $secret]); - if ($query->num_rows != 0) { - $row = mysqli_fetch_array($query->result); + $result = mysqli_query($link, "SELECT `time` FROM `chatmutes` WHERE `user` = '$credential' AND `app` = '$secret'"); + if (mysqli_num_rows($result) != 0) { + $row = mysqli_fetch_array($result); $unmuted = $row["time"]; $unmuted = date("F j, Y, g:i a", $unmuted); die(api\v1_0\Encrypt(json_encode(array( @@ -834,32 +736,14 @@ } $message = misc\etc\sanitize(api\v1_0\Decrypt($_POST['message'], $enckey)); - - if (is_null($message)) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Message can't be blank" - )), $enckey)); - } - - if(strlen($message) > 2000) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Message too long!" - )), $enckey)); - } - - misc\mysql\query("INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES (?, ?, ?, ?, ?)", [$credential, $message, time(), $channel, $secret]); - misc\mysql\query("DELETE FROM `chatmsgs` WHERE `app` = ? AND `channel` = ? AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 50) foo );", [$secret, $channel, $channel, $secret]); + mysqli_query($link, "INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES ('$credential','$message','" . time() . "','$channel','$secret')"); + mysqli_query($link, "DELETE FROM `chatmsgs` WHERE `app` = '$secret' AND `channel` = '$channel' AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 50) foo );"); misc\cache\purge('KeyAuthChatMsgs:' . $secret . ':' . $channel); die(api\v1_0\Encrypt(json_encode(array( "success" => true, "message" => "Successfully sent chat message" )), $enckey)); case 'log': - // client isn't expecting a response body, just flush output right away so program can move on to rest of the code quicker - fastcgi_finish_request(); - // retrieve session info $sessionid = misc\etc\sanitize(hex2bin($_POST['sessionid'])); $session = api\shared\primary\getSession($sessionid, $secret); @@ -871,28 +755,11 @@ $msg = misc\etc\sanitize(api\v1_0\Decrypt($_POST['message'], $enckey)); - if(is_null($msg)) { - die(); - } - - if(strlen($msg) > 275) { - die("Log data too long"); - } - $pcuser = misc\etc\sanitize(api\v1_0\Decrypt($_POST['pcuser'], $enckey)); if (is_null($webhook)) { - $roleCheck = misc\cache\fetch('KeyAuthSellerCheck:' . $owner, "SELECT `role`,`expires` FROM `accounts` WHERE `username` = ?", [$owner], 0); - if($roleCheck['role'] == "tester") { - $query = misc\mysql\query("SELECT count(*) AS 'numLogs' FROM `logs` WHERE `logapp` = ?",[$secret]); - $row = mysqli_fetch_array($query->result); - $numLogs = $row["numLogs"]; - if($numLogs >= 20) { - die(); - } - } - - misc\mysql\query("INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES (?, ?, NULLIF(?, ''), NULLIF(?, ''), ?)", [$currtime, $msg, $credential, $pcuser, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES ('$currtime','$msg',NULLIF('$credential', ''),NULLIF('$pcuser', ''),'$secret')"); die(); } @@ -903,6 +770,13 @@ $ip = api\shared\primary\getIp(); $json_data = json_encode([ + // Username + "username" => "KeyAuth", + + // Avatar URL. + // Uncoment to replace image set in webhook + "avatar_url" => "https://cdn.keyauth.cc/front/assets/img/favicon.png", + // Embeds Array "embeds" => [ [ @@ -948,7 +822,7 @@ $webid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['webid'], $enckey)); - $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = ? AND `app` = ?", [$webid, $secret], 0); + $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = '$webid' AND `app` = '$secret'", 0); if ($row == "not_found") { die(api\v1_0\Encrypt(json_encode(array( "success" => false, @@ -976,7 +850,7 @@ $params = misc\etc\sanitize(api\v1_0\Decrypt($_POST['params'], $enckey)); $body = api\v1_0\Decrypt($_POST['body'], $enckey); $contType = misc\etc\sanitize(api\v1_0\Decrypt($_POST['conttype'], $enckey)); - + $url = $baselink .= urldecode($params); $ch = curl_init($url); @@ -1005,7 +879,7 @@ $fileid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['fileid'], $enckey)); - $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = ? AND `id` = ?", [$secret, $fileid], 0); + $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = '$secret' AND `id` = '$fileid'", 0); if ($row == "not_found") { die(api\v1_0\Encrypt(json_encode(array( @@ -1028,8 +902,8 @@ )), $enckey)); } } - - ini_set('memory_limit', '-1'); + + ini_set('memory_limit', '-1'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); @@ -1063,26 +937,20 @@ "message" => "$sessionunauthed" )), $enckey)); } + + $reason = misc\etc\sanitize($_POST['reason']) ?? "User banned from triggering ban function in the client"; - $reason = misc\etc\sanitize($_POST['reason']) ?? "User banned from triggering ban function in the client"; - - if(strlen($reason) > 99) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Reason must be 99 characters or less" - )), $enckey)); - } - + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL $hwid = misc\etc\sanitize(api\v1_0\Decrypt($_POST['hwid'], $enckey)); if (!empty($hwid)) { - misc\blacklist\add($hwid, "Hardware ID", $secret); + misc\blacklist\add($hwid, "Hardware ID", $secret); } $ip = api\shared\primary\getIp(); - misc\blacklist\add($ip, "IP Address", $secret); + misc\blacklist\add($ip, "IP Address", $secret); - misc\mysql\query("UPDATE `users` SET `banned` = ? WHERE `username` = ? AND `app` = ?", [$reason, $credential, $secret]); + mysqli_query($link, "UPDATE `users` SET `banned` = '$reason' WHERE `username` = '$credential'"); - if ($query->affected_rows != 0) { + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUser:' . $secret . ':' . $credential); die(api\v1_0\Encrypt(json_encode(array( "success" => true, @@ -1112,48 +980,48 @@ "message" => "Session is validated." )), $enckey)); } - case 'changeUsername': + case 'changeUsername': // retrieve session info $sessionid = misc\etc\sanitize(hex2bin($_POST['sessionid'])); $session = api\shared\primary\getSession($sessionid, $secret); $enckey = $session["enckey"]; - - if (!$session["validated"]) { - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "$sessionunauthed" - )), $enckey)); + + if (!$session["validated"]) { + die(api\v1_0\Encrypt(json_encode(array( + "success" => false, + "message" => "$sessionunauthed" + )), $enckey)); } $credential = $session["credential"]; - - $resp = misc\user\changeUsername($credential, $_POST['newUsername'], $secret); - switch ($resp) { - case 'already_used': - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Username already used!" - )), $enckey)); - case 'failure': - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Failed to change username!" - )), $enckey)); - case 'success': - misc\session\killSingular($sessionid, $secret); - die(api\v1_0\Encrypt(json_encode(array( - "success" => true, - "message" => "Successfully changed username, user logged out." - )), $enckey)); - default: - die(api\v1_0\Encrypt(json_encode(array( - "success" => false, - "message" => "Unhandled Error! Contact us if you need help" - )), $enckey)); - } + + $resp = misc\user\changeUsername($credential, $_POST['newUsername'], $secret); + switch($resp) { + case 'already_used': + die(api\v1_0\Encrypt(json_encode(array( + "success" => false, + "message" => "Username already used!" + )), $enckey)); + case 'failure': + die(api\v1_0\Encrypt(json_encode(array( + "success" => false, + "message" => "Failed to change username!" + )), $enckey)); + case 'success': + misc\session\killSingular($sessionid, $secret); + die(api\v1_0\Encrypt(json_encode(array( + "success" => true, + "message" => "Successfully changed username, user logged out." + )), $enckey)); + default: + die(api\v1_0\Encrypt(json_encode(array( + "success" => false, + "message" => "Unhandled Error! Contact us if you need help" + )), $enckey)); + } default: die(json_encode(array( "success" => false, "message" => "Unhandled Type" ))); -} +} \ No newline at end of file diff --git a/api/1.1/index.php b/api/1.1/index.php index 40dd0e9..b2c0769 100644 --- a/api/1.1/index.php +++ b/api/1.1/index.php @@ -1,38 +1,20 @@ getMessage()); - die(json_encode(array("success" => false, "message" => "Error: " . $errorMsg))); -}); - -if(empty(($_POST['ownerid'] ?? $_GET['ownerid']))) { - die(json_encode(array("success" => false, "message" => "No OwnerID specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} +error_reporting(0); // disable useless warnings, should turn this on if you need to debug a problem -if(empty(($_POST['name'] ?? $_GET['name']))) { - die(json_encode(array("success" => false, "message" => "No app name specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} +include '../../includes/misc/autoload.phtml'; +include '../../includes/api/shared/autoload.phtml'; +include '../../includes/api/1.0/autoload.phtml'; -if(strlen(($_POST['ownerid'] ?? $_GET['ownerid'])) != 10) { - die(json_encode(array("success" => false, "message" => "OwnerID should be 10 characters long. Select app & copy code snippet from https://keyauth.cc/app/"))); +if (isset($_SERVER['HTTP_CDN_HOST'])) { // custom domains https://www.youtube.com/watch?v=a2SROFJ0eYc + $row = misc\cache\fetch('KeyAuthApp:' . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']), "SELECT * FROM `apps` WHERE `customDomainAPI` = '" . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']) . "'", 0); +} else { + $ownerid = misc\etc\sanitize($_POST['ownerid'] ?? $_GET['ownerid']); // ownerid of account that owns application + $name = misc\etc\sanitize($_POST['name'] ?? $_GET['name']); // application name + $row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = '$ownerid' AND `name` = '$name'", 0); } -$ownerid = misc\etc\sanitize($_POST['ownerid'] ?? $_GET['ownerid']); // ownerid of account that owns application -$name = misc\etc\sanitize($_POST['name'] ?? $_GET['name']); // application name -$row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = ? AND `name` = ?", [$ownerid, $name], 0); - if ($row == "not_found") { die("KeyAuth_Invalid"); } @@ -49,8 +31,6 @@ $appdisabled = $row['appdisabled']; $hashcheck = $row['hashcheck']; $serverhash = $row['hash']; -$sessionexpiry = $row['session']; -$forceHwid = $row['forceHwid']; $banned = $row['banned']; $owner = $row['owner']; @@ -67,11 +47,14 @@ $noactivesubs = $row['noactivesubs']; $hwidblacked = $row['hwidblacked']; $pausedsub = $row['pausedsub']; +$keyexpired = $row['keyexpired']; $vpnblocked = $row['vpnblocked']; $keybanned = $row['keybanned']; $userbanned = $row['userbanned']; $sessionunauthed = $row['sessionunauthed']; $hashcheckfail = $row['hashcheckfail']; +$sessionexpiry = $row['session']; +$killOtherSessions = $row['killOtherSessions']; // why using null coalescing operators? because if I add a field and it's not in redis cache, it'll be NULL $loggedInMsg = $row['loggedInMsg'] ?? "Logged in!"; @@ -79,7 +62,6 @@ $unTooShort = $row['unTooShort'] ?? "Username too short, try longer one."; $pwLeaked = $row['pwLeaked'] ?? "This password has been leaked in a data breach (not from us), please use a different one."; $chatHitDelay = $row['chatHitDelay'] ?? "Chat slower, you've hit the delay limit"; -$minHwid = $row['minHwid'] ?? 20; if ($banned) { die(json_encode(array( @@ -93,13 +75,13 @@ $ip = api\shared\primary\getIp(); if ($vpnblock) { if (api\shared\primary\vpnCheck($ip)) { - $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = ? AND `app` = ?", [$ip, $secret], 0); - if ($row == "not_found") { - die(json_encode(array( - "success" => false, - "message" => "$vpnblocked" - ))); - } + $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = '$ip' AND `app` = '$secret'", 0); + if($row == "not_found") { + die(json_encode(array( + "success" => false, + "message" => "$vpnblocked" + ))); + } } } @@ -118,7 +100,7 @@ } $ver = misc\etc\sanitize($_POST['ver'] ?? $_GET['ver']); - if (!empty($ver)) { + if (is_numeric($ver)) { if ($ver != $currentver) { // auto-update system die(json_encode(array( @@ -134,7 +116,8 @@ if ($hashcheck) { if (strpos($serverhash, $hash) === false) { if (is_null($serverhash)) { - misc\mysql\query("UPDATE `apps` SET `hash` = ? WHERE `secret` = ?", [$hash, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "UPDATE `apps` SET `hash` = '$hash' WHERE `secret` = '$secret'"); misc\cache\purge('KeyAuthApp:' . $name . ':' . $ownerid); // flush cache for application so new hash takes precedent } else { die(json_encode(array( @@ -145,48 +128,41 @@ } } - $newSession = false; - $duplicateSession = misc\cache\select("KeyAuthSessionDupe:$secret:$ip"); - if($duplicateSession) { - $sessionid = $duplicateSession; - $updateSession = misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 0)); - if(!$updateSession) { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; - } - } - else { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; + $enckey = NULL; + + $row = misc\cache\fetch('KeyAuthStateDuplicates:' . $secret . ':' . $ip, "SELECT `id`, `expiry` FROM `sessions` WHERE `app` = '$secret' AND `ip` = '$ip' AND `validated` = 0 AND `expiry` > " . time() . " LIMIT 1", 0); + if ($row != "not_found") { + $sessionid = $row['id']; + goto dupe; } - - // $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT (SELECT COUNT(1) FROM `users` WHERE `app` = ?) AS 'numUsers', (SELECT COUNT(1) FROM `sessions` WHERE `app` = ? AND `validated` = 1 AND `expiry` > ?) AS 'numOnlineUsers', (SELECT COUNT(1) FROM `keys` WHERE `app` = ?) AS 'numKeys' FROM dual", [$secret, $secret, time(), $secret], 0, 3600); - $numUsers = "N/A - Use fetchStats() function in latest example"; - $numOnlineUsers = "N/A - Use fetchStats() function in latest example"; - $numKeys = "N/A - Use fetchStats() function in latest example"; + $sessionid = misc\etc\generateRandomString(); + // session init + $time = time() + $sessionexpiry; + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `enckey`, `ip`) VALUES ('$sessionid','$secret', '$time', '".time()."', NULLIF('$enckey', ''), '$ip')"); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); + + dupe: + + $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT(select count(1) FROM `users` WHERE `app` = '$secret') AS 'numUsers',(select count(1) FROM `sessions` WHERE `app` = '$secret' AND `validated` = 1 AND `expiry` > " . time() . ") AS 'numOnlineUsers',(select count(1) FROM `keys` WHERE `app` = '$secret') AS 'numKeys';", 0, 1800); - echo json_encode(array( + $numUsers = $row['numUsers']; + $numOnlineUsers = $row['numOnlineUsers']; + $numKeys = $row['numKeys']; + + die(json_encode(array( "success" => true, "message" => "Initialized", "sessionid" => $sessionid, "appinfo" => array( - "numUsers" => "$numUsers", - "numOnlineUsers" => "$numOnlineUsers", - "numKeys" => "$numKeys", - "version" => "$currentver", + "numUsers" => $numUsers, + "numOnlineUsers" => $numOnlineUsers, + "numKeys" => $numKeys, + "version" => $currentver, "customerPanelLink" => "https://keyauth.cc/panel/$owner/$name/" ) - )); - - fastcgi_finish_request(); - - if($newSession) { - misc\cache\insert("KeyAuthState:$secret:$sessionid", serialize(array("credential" => NULL, "enckey" => NULL, "validated" => 0)), $sessionexpiry); - $time = time() + $sessionexpiry; - misc\mysql\query("INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `ip`) VALUES (?, ?, ?, ?, ?)", [$sessionid, $secret, $time, time(), $ip]); - misc\cache\insert("KeyAuthSessionDupe:$secret:$ip", $sessionid, $sessionexpiry); - } + ))); case 'register': // retrieve session info @@ -196,27 +172,13 @@ // Read in username $username = misc\etc\sanitize($_POST['username'] ?? $_GET['username']); - if(strlen($username) > 70) { - die(json_encode(array( - "success" => false, - "message" => "Username must be shorter than 70 characters" - ))); - } - // Read in license key $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); - if(strlen($checkkey) > 70) { - die(json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - ))); - } - // Read in password $password = misc\etc\sanitize($_POST['pass'] ?? $_GET['pass']); - - // Read in email + + // Read in email $email = misc\etc\sanitize($_POST['email'] ?? $_GET['email']); // Read in hwid @@ -251,8 +213,9 @@ ))); case 'key_banned': if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -271,10 +234,16 @@ "message" => "$nosublevel" ))); default: - misc\mysql\query("UPDATE `sessions` SET `credential` = ?,`validated` = 1 WHERE `id` = ?", [$username, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `credential` = '$username',`validated` = 1 WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); die(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -292,12 +261,13 @@ // Read in key $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL // search for key - $query = misc\mysql\query("SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = ? AND `app` = ?", [$checkkey, $secret]); + $result = mysqli_query($link, "SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = '$checkkey' AND `app` = '$secret'"); // check if key exists - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { die(json_encode(array( "success" => false, "message" => "$keynotfound" @@ -305,10 +275,10 @@ } // if key does exist - elseif ($query->num_rows > 0) { + elseif (mysqli_num_rows($result) > 0) { // get key info - while ($row = mysqli_fetch_array($query->result)) { + while ($row = mysqli_fetch_array($result)) { $expires = $row['expires']; $status = $row['status']; $level = $row['level']; @@ -324,9 +294,9 @@ } if (!is_null($banned)) { - if (strpos($keybanned, '{reason}') !== false) { - $keybanned = str_replace("{reason}", $banned, $keybanned); - } + if (strpos($keybanned, '{reason}') !== false) { + $keybanned = str_replace("{reason}", $banned, $keybanned); + } die(json_encode(array( "success" => false, "message" => "$keybanned" @@ -336,8 +306,8 @@ // add current time to key time $expiry = $expires + time(); - $query = misc\mysql\query("SELECT `name` FROM `subscriptions` WHERE `app` = ? AND `level` = ?", [$secret, $level]); - $subName = mysqli_fetch_array($query->result)['name']; + $result = mysqli_query($link, "SELECT `name` FROM `subscriptions` WHERE `app` = '$secret' AND `level` = '$level'"); + $subName = mysqli_fetch_array($result)['name']; $resp = misc\user\extend($username, $subName, $expiry, 0, $secret); switch ($resp) { @@ -358,9 +328,9 @@ ))); case 'success': // set key to used, and set usedby - misc\mysql\query("UPDATE `keys` SET `status` = 'Used', `usedon` = ?, `usedby` = ? WHERE `key` = ? AND `app` = ?", [time(), $username, $checkkey, $secret]); + mysqli_query($link, "UPDATE `keys` SET `status` = 'Used', `usedon` = '" . time() . "', `usedby` = '$username' WHERE `key` = '$checkkey'"); misc\cache\purge('KeyAuthKeys:' . $secret . ':' . $checkkey); - misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); + misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); die(json_encode(array( "success" => true, "message" => "Upgraded successfully" @@ -389,21 +359,7 @@ // optional param for web loader $token = misc\etc\sanitize($_POST['token'] ?? $_GET['token']); - - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - die(json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - ))); - } - - if($forceHwid && is_null($hwid)) { - die(json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - ))); - } - + $resp = api\v1_0\login($username, $password, $hwid, $secret, $hwidenabled, $token); switch ($resp) { case 'un_not_found': @@ -418,8 +374,9 @@ ))); case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $username]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$username'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -448,10 +405,16 @@ "message" => "$noactivesubs" ))); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$username, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$username' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); die(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -465,33 +428,13 @@ $session = api\shared\primary\getSession($sessionid, $secret); $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); - if(strlen($checkkey) > 70) { - die(json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - ))); - } - $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - die(json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - ))); - } - - if($forceHwid && is_null($hwid)) { - die(json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - ))); - } - $resp = api\v1_0\login($checkkey, $checkkey, $hwid, $secret, $hwidenabled); switch ($resp) { case 'un_not_found': break; // user not registered yet or user was deleted + case 'hwid_mismatch': die(json_encode(array( "success" => false, @@ -499,8 +442,9 @@ ))); case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -529,10 +473,16 @@ "message" => "$noactivesubs" ))); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); die(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -570,8 +520,9 @@ ))); case 'key_banned': if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -590,10 +541,16 @@ "message" => "$nosublevel" ))); default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); die(json_encode(array( "success" => true, "message" => "$loggedInMsg", @@ -604,7 +561,7 @@ $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); - $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT CONCAT(LEFT(`credential`, 10), IF(LENGTH(`credential`) > 10, REPEAT('*', LENGTH(`credential`) - 10), '')) AS `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = ?", [$secret], 1, 1800); + $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = '$secret'", 1, 1800); if ($rows == "not_found") { die(json_encode(array( @@ -630,43 +587,10 @@ $var = misc\etc\sanitize($_POST['var'] ?? $_GET['var']); $data = misc\etc\sanitize($_POST['data'] ?? $_GET['data']); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES ('$var', '$data', '" . $session["credential"] . "', '$secret')"); - if(is_null($var)) { - die(json_encode(array( - "success" => true, - "message" => "No variable name provided" - ))); - } - - if(is_null($data)) { - die(json_encode(array( - "success" => true, - "message" => "No variable data provided" - ))); - } - - if(strlen($data) > 500) { - die(json_encode(array( - "success" => true, - "message" => "Variable data must be 500 characters or less" - ))); - } - - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); - - if ($row != "not_found") { - $readOnly = $row["readOnly"]; - if ($readOnly) { - die(json_encode(array( - "success" => true, - "message" => "Variable is read only" - ))); - } - } - - $query = misc\mysql\query("REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES (?, ?, ?, ?)", [$var, $data, $session["credential"], $secret]); - - if ($query->affected_rows != 0) { + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"]); die(json_encode(array( "success" => true, @@ -690,7 +614,7 @@ $var = misc\etc\sanitize($_POST['var'] ?? $_GET['var']); - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); + $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data` FROM `uservars` WHERE `name` = '$var' AND `user` = '" . $session["credential"] . "' AND `app` = '$secret'", 0); if ($row == "not_found") { die(json_encode(array( @@ -712,7 +636,7 @@ $varid = misc\etc\sanitize($_POST['varid'] ?? $_GET['varid']); - $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = ? AND `app` = ?", [$varid, $secret], 0); + $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = '$varid' AND `app` = '$secret'", 0); if ($row == "not_found") { die(json_encode(array( @@ -746,7 +670,7 @@ $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); $ip = api\shared\primary\getIp(); - $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = ? OR `ip` = ?) AND `app` = ?", [$hwid, $ip, $secret], 0); + $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = '$hwid' OR `ip` = '$ip') AND `app` = '$secret'", 0); if ($row != "not_found") { die(json_encode(array( @@ -771,9 +695,9 @@ } $channel = misc\etc\sanitize($_POST['channel'] ?? $_GET['channel']); - $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ?", [$channel, $secret], 1); + $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret'", 1); - if ($rows == "not_found") { + if ($rows == "not_found") { $rows = []; } @@ -794,21 +718,22 @@ } $channel = misc\etc\sanitize($_POST['channel'] ?? $_GET['channel']); - $query = misc\mysql\query("SELECT `delay` FROM `chats` WHERE `name` = ? AND `app` = ?", [$channel, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `delay` FROM `chats` WHERE `name` = '$channel' AND `app` = '$secret'"); - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { die(json_encode(array( "success" => false, "message" => "Chat channel not found" ))); } - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $delay = $row['delay']; $credential = $session["credential"]; - $query = misc\mysql\query("SELECT `timestamp` FROM `chatmsgs` WHERE `author` = ? AND `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 1", [$credential, $channel, $secret]); + $result = mysqli_query($link, "SELECT `timestamp` FROM `chatmsgs` WHERE `author` = '$credential' AND `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 1"); - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $time = $row['timestamp']; if (time() - $time < $delay) { @@ -818,9 +743,9 @@ ))); } - $query = misc\mysql\query("SELECT `time` FROM `chatmutes` WHERE `user` = ? AND `app` = ?", [$credential, $secret]); - if ($query->num_rows != 0) { - $row = mysqli_fetch_array($query->result); + $result = mysqli_query($link, "SELECT `time` FROM `chatmutes` WHERE `user` = '$credential' AND `app` = '$secret'"); + if (mysqli_num_rows($result) != 0) { + $row = mysqli_fetch_array($result); $unmuted = $row["time"]; $unmuted = date("F j, Y, g:i a", $unmuted); die(json_encode(array( @@ -830,32 +755,14 @@ } $message = misc\etc\sanitize($_POST['message'] ?? $_GET['message']); - - if (is_null($message)) { - die(json_encode(array( - "success" => false, - "message" => "Message can't be blank" - ))); - } - - if(strlen($message) > 2000) { - die(json_encode(array( - "success" => false, - "message" => "Message too long!" - ))); - } - - misc\mysql\query("INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES (?, ?, ?, ?, ?)", [$credential, $message, time(), $channel, $secret]); - misc\mysql\query("DELETE FROM `chatmsgs` WHERE `app` = ? AND `channel` = ? AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 50) foo );", [$secret, $channel, $channel, $secret]); + mysqli_query($link, "INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES ('$credential','$message','" . time() . "','$channel','$secret')"); + mysqli_query($link, "DELETE FROM `chatmsgs` WHERE `app` = '$secret' AND `channel` = '$channel' AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 50) foo );"); misc\cache\purge('KeyAuthChatMsgs:' . $secret . ':' . $channel); die(json_encode(array( "success" => true, "message" => "Successfully sent chat message" ))); case 'log': - // client isn't expecting a response body, just flush output right away so program can move on to rest of the code quicker - fastcgi_finish_request(); - // retrieve session info $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); @@ -866,28 +773,11 @@ $msg = misc\etc\sanitize($_POST['message'] ?? $_GET['message']); - if(is_null($msg)) { - die(); - } - - if(strlen($msg) > 275) { - die("Log data too long"); - } - $pcuser = misc\etc\sanitize($_POST['pcuser'] ?? $_GET['pcuser']); if (is_null($webhook)) { - $roleCheck = misc\cache\fetch('KeyAuthSellerCheck:' . $owner, "SELECT `role`,`expires` FROM `accounts` WHERE `username` = ?", [$owner], 0); - if($roleCheck['role'] == "tester") { - $query = misc\mysql\query("SELECT count(*) AS 'numLogs' FROM `logs` WHERE `logapp` = ?",[$secret]); - $row = mysqli_fetch_array($query->result); - $numLogs = $row["numLogs"]; - if($numLogs >= 20) { - die(); - } - } - - misc\mysql\query("INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES (?, ?, NULLIF(?, ''), NULLIF(?, ''), ?)", [$currtime, $msg, $credential, $pcuser, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES ('$currtime','$msg',NULLIF('$credential', ''),NULLIF('$pcuser', ''),'$secret')"); die(); } @@ -898,6 +788,13 @@ $ip = api\shared\primary\getIp(); $json_data = json_encode([ + // Username + "username" => "KeyAuth", + + // Avatar URL. + // Uncoment to replace image set in webhook + "avatar_url" => "https://cdn.keyauth.cc/front/assets/img/favicon.png", + // Embeds Array "embeds" => [ [ @@ -943,7 +840,7 @@ $session = api\shared\primary\getSession($sessionid, $secret); $webid = misc\etc\sanitize($_POST['webid'] ?? $_GET['webid']); - $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = ? AND `app` = ?", [$webid, $secret], 0); + $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = '$webid' AND `app` = '$secret'", 0); if ($row == "not_found") { die(json_encode(array( "success" => false, @@ -998,7 +895,7 @@ $fileid = misc\etc\sanitize($_POST['fileid'] ?? $_GET['fileid']); - $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = ? AND `id` = ?", [$secret, $fileid], 0); + $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = '$secret' AND `id` = '$fileid'", 0); if ($row == "not_found") { die(json_encode(array( @@ -1021,8 +918,8 @@ ))); } } - - ini_set('memory_limit', '-1'); + + ini_set('memory_limit', '-1'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); @@ -1056,25 +953,19 @@ "message" => "$sessionunauthed" ))); } + + $reason = misc\etc\sanitize($_POST['reason'] ?? $_GET['reason']) ?? "User banned from triggering ban function in the client"; - $reason = misc\etc\sanitize($_POST['reason'] ?? $_GET['reason']) ?? "User banned from triggering ban function in the client"; - - if(strlen($reason) > 99) { - die(json_encode(array( - "success" => false, - "message" => "Reason must be 99 characters or less" - ))); - } - + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); if (!empty($hwid)) { misc\blacklist\add($hwid, "Hardware ID", $secret); } $ip = api\shared\primary\getIp(); - misc\blacklist\add($ip, "IP Address", $secret); + misc\blacklist\add($ip, "IP Address", $secret); - misc\mysql\query("UPDATE `users` SET `banned` = ? WHERE `username` = ? AND `app` = ?", [$reason, $credential, $secret]); - if ($query->affected_rows != 0) { + mysqli_query($link, "UPDATE `users` SET `banned` = '$reason' WHERE `username` = '$credential'"); + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUser:' . $secret . ':' . $credential); die(json_encode(array( "success" => true, @@ -1103,11 +994,11 @@ "message" => "Session is validated." ))); } - case 'changeUsername': + case 'changeUsername': $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); - - if (!$session["validated"]) { + + if (!$session["validated"]) { die(json_encode(array( "success" => false, "message" => "$sessionunauthed" @@ -1115,31 +1006,31 @@ } $credential = $session["credential"]; - - $resp = misc\user\changeUsername($credential, $_POST['newUsername'] ?? $_GET['newUsername'], $secret); - switch ($resp) { - case 'already_used': - die(json_encode(array( - "success" => false, - "message" => "Username already used!" - ))); - case 'failure': - die(json_encode(array( - "success" => false, - "message" => "Failed to change username!" - ))); - case 'success': - misc\session\killSingular($sessionid, $secret); - die(json_encode(array( - "success" => true, - "message" => "Successfully changed username, user logged out." - ))); - default: - die(json_encode(array( - "success" => false, - "message" => "Unhandled Error! Contact us if you need help" - ))); - } + + $resp = misc\user\changeUsername($credential, $_POST['newUsername'] ?? $_GET['newUsername'], $secret); + switch($resp) { + case 'already_used': + die(json_encode(array( + "success" => false, + "message" => "Username already used!" + ))); + case 'failure': + die(json_encode(array( + "success" => false, + "message" => "Failed to change username!" + ))); + case 'success': + misc\session\killSingular($sessionid, $secret); + die(json_encode(array( + "success" => true, + "message" => "Successfully changed username, user logged out." + ))); + default: + die(json_encode(array( + "success" => false, + "message" => "Unhandled Error! Contact us if you need help" + ))); + } default: die(json_encode(array( "success" => false, diff --git a/api/1.2/index.php b/api/1.2/index.php index 7c701e5..575c3dc 100644 --- a/api/1.2/index.php +++ b/api/1.2/index.php @@ -1,42 +1,19 @@ getMessage()); - die(json_encode(array("success" => false, "message" => "Error: " . $errorMsg))); -}); - -if(empty(($_POST['ownerid'] ?? $_GET['ownerid']))) { - die(json_encode(array("success" => false, "message" => "No OwnerID specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} - -if(empty(($_POST['name'] ?? $_GET['name']))) { - die(json_encode(array("success" => false, "message" => "No app name specified. Select app & copy code snippet from https://keyauth.cc/app/"))); -} - -if(strlen(($_POST['ownerid'] ?? $_GET['ownerid'])) != 10) { - die(json_encode(array("success" => false, "message" => "OwnerID should be 10 characters long. Select app & copy code snippet from https://keyauth.cc/app/"))); +if (isset($_SERVER['HTTP_CDN_HOST'])) { // custom domains https://www.youtube.com/watch?v=a2SROFJ0eYc + $row = misc\cache\fetch('KeyAuthApp:' . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']), "SELECT * FROM `apps` WHERE `customDomainAPI` = '" . misc\etc\sanitize($_SERVER['HTTP_CDN_HOST']) . "'", 0); +} else { + $ownerid = misc\etc\sanitize($_POST['ownerid'] ?? $_GET['ownerid']); // ownerid of account that owns application + $name = misc\etc\sanitize($_POST['name'] ?? $_GET['name']); // application name + $row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = '$ownerid' AND `name` = '$name'", 0); } -if (misc\cache\rateLimit("KeyAuthAppLimit:" . ($_POST['ownerid'] ?? $_GET['ownerid']), 1, 60, 200)) { - die(json_encode(array("success" => false, "message" => "This application has sent too many requests. Try again in a minute."))); -} - -$ownerid = misc\etc\sanitize($_POST['ownerid'] ?? $_GET['ownerid']); // ownerid of account that owns application -$name = misc\etc\sanitize($_POST['name'] ?? $_GET['name']); // application name -$row = misc\cache\fetch('KeyAuthApp:' . $name . ':' . $ownerid, "SELECT * FROM `apps` WHERE `ownerid` = ? AND `name` = ?", [$ownerid, $name], 0); - if ($row == "not_found") { die("KeyAuth_Invalid"); } @@ -53,10 +30,6 @@ $appdisabled = $row['appdisabled']; $hashcheck = $row['hashcheck']; $serverhash = $row['hash']; -$sessionexpiry = $row['session']; -$forceEncryption = $row['forceEncryption']; -$forceHwid = $row['forceHwid']; -$tokensystem = $row['tokensystem']; $banned = $row['banned']; $owner = $row['owner']; @@ -73,12 +46,15 @@ $noactivesubs = $row['noactivesubs']; $hwidblacked = $row['hwidblacked']; $pausedsub = $row['pausedsub']; +$keyexpired = $row['keyexpired']; $vpnblocked = $row['vpnblocked']; $keybanned = $row['keybanned']; $userbanned = $row['userbanned']; $sessionunauthed = $row['sessionunauthed']; $hashcheckfail = $row['hashcheckfail']; -$invalid_token = $row['tokeninvalid']; +$sessionexpiry = $row['session']; +$killOtherSessions = $row['killOtherSessions']; +$forceEncryption = $row['forceEncryption']; // why using null coalescing operators? because if I add a field and it's not in redis cache, it'll be NULL $loggedInMsg = $row['loggedInMsg'] ?? "Logged in!"; @@ -86,19 +62,6 @@ $unTooShort = $row['unTooShort'] ?? "Username too short, try longer one."; $pwLeaked = $row['pwLeaked'] ?? "This password has been leaked in a data breach (not from us), please use a different one."; $chatHitDelay = $row['chatHitDelay'] ?? "Chat slower, you've hit the delay limit"; -$minHwid = $row['minHwid'] ?? 20; - -if($ownerid == "hTmfnZOYPe") { - $response = json_encode(array( - "success" => false, - "message" => "Jao é um golpista, prova aqui https://keyauth.cc/jao/" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); -} if ($banned) { die(json_encode(array( @@ -109,18 +72,6 @@ switch ($_POST['type'] ?? $_GET['type']) { case 'init': - if(strlen($_POST['enckey']) > 35) { - $response = json_encode(array( - "success" => false, - "message" => "The paramater \"enckey\" is too long. Must be 35 characters or less." - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - } - if ($forceEncryption) { if (!isset($_POST['enckey']) && !isset($_GET['enckey'])) { $response = json_encode(array( @@ -137,18 +88,18 @@ $ip = api\shared\primary\getIp(); if ($vpnblock) { if (api\shared\primary\vpnCheck($ip)) { - $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = ? AND `app` = ?", [$ip, $secret], 0); - if ($row == "not_found") { - $response = json_encode(array( - "success" => false, - "message" => "$vpnblocked" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - } + $row = misc\cache\fetch('KeyAuthWhitelist:' . $secret . ':' . $ip, "SELECT 1 FROM `whitelist` WHERE `ip` = '$ip' AND `app` = '$secret'", 0); + if($row == "not_found") { + $response = json_encode(array( + "success" => false, + "message" => "$vpnblocked" + )); + + $sig = hash_hmac('sha256', $response, $secret); + header("signature: {$sig}"); + + die($response); + } } } @@ -176,93 +127,8 @@ die($response); } - if ($tokensystem) { - - list($token, $thash) = [misc\etc\sanitize($_POST["token"] ?? $_GET["token"]) , misc\etc\sanitize($_POST["thash"] ?? $_GET["thash"])]; - - if (!isset($token)) { - - $response = json_encode(array( - "success" => false, - "message" => "Token Must Be Provided" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - } - - if (!isset($thash)) { - $response = json_encode(array( - "success" => false, - "message" => "Hash Must Be Provided" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - } - - $verification = misc\token\checktoken("check_data", $token, $secret, null, $thash); - - switch ($verification) { - case str_contains($verification, 'token_blacklisted'): - $reason = str_ireplace("token_blacklisted, ", "", $verification); - $response = json_encode(array( - "success" => false, - "message" => "The Token Has Been Blacklisted For The Following Reason: " . $reason . " Contact Application Developer If This Is A Mistake" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - case 'invalid_token': - - $response = json_encode(array( - "success" => false, - "message" => $invalid_token - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - - case 'hash_mismatch': - - $response = json_encode(array( - "success" => false, - "message" => "Token File Hashes Must Be The Same" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - - case 'success': - break; - - default: - - $response = json_encode(array( - "success" => false, - "message" => "Unknown Error Occured" - )); - - $sig = hash_hmac('sha256', $response, $secret); - header("signature: {$sig}"); - - die($response); - - } - } - $ver = misc\etc\sanitize($_POST['ver'] ?? $_GET['ver']); - if (!empty($ver)) { + if (is_numeric($ver)) { if ($ver != $currentver) { // auto-update system $response = json_encode(array( @@ -283,7 +149,8 @@ if ($hashcheck) { if (strpos($serverhash, $hash) === false) { if (is_null($serverhash)) { - misc\mysql\query("UPDATE `apps` SET `hash` = ? WHERE `secret` = ?", [$hash, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "UPDATE `apps` SET `hash` = '$hash' WHERE `secret` = '$secret'"); misc\cache\purge('KeyAuthApp:' . $name . ':' . $ownerid); // flush cache for application so new hash takes precedent } else { $response = json_encode(array( @@ -300,56 +167,47 @@ } $enckey = !is_null($_POST['enckey'] ?? $_GET['enckey']) ? misc\etc\sanitize($_POST['enckey'] ?? $_GET['enckey']) . "-" . $secret : NULL; - $newSession = false; - $duplicateSession = misc\cache\select("KeyAuthSessionDupe:$secret:$ip"); - if($duplicateSession) { - $sessionid = $duplicateSession; - - $updateSession = misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 0, "enckey" => $enckey)); - if(!$updateSession) { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; + $sessionid = misc\etc\generateRandomString(); + + if (is_null($enckey)) { + $row = misc\cache\fetch('KeyAuthStateDuplicates:' . $secret . ':' . $ip, "SELECT `id`, `expiry` FROM `sessions` WHERE `app` = '$secret' AND `ip` = '$ip' AND `validated` = 0 AND `expiry` > " . time() . " LIMIT 1", 0); + if ($row != "not_found") { + $sessionid = $row['id']; + goto dupe; } } - else { - $sessionid = misc\etc\generateRandomString(); - $newSession = true; - } + // session init + $time = time() + $sessionexpiry; + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `enckey`, `ip`) VALUES ('$sessionid','$secret', '$time', '".time()."', NULLIF('$enckey', ''), '$ip')"); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); + + dupe: - // $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT (SELECT COUNT(1) FROM `users` WHERE `app` = ?) AS 'numUsers', (SELECT COUNT(1) FROM `sessions` WHERE `app` = ? AND `validated` = 1 AND `expiry` > ?) AS 'numOnlineUsers', (SELECT COUNT(1) FROM `keys` WHERE `app` = ?) AS 'numKeys' FROM dual", [$secret, $secret, time(), $secret], 0, 3600); + $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT(select count(1) FROM `users` WHERE `app` = '$secret') AS 'numUsers',(select count(1) FROM `sessions` WHERE `app` = '$secret' AND `validated` = 1 AND `expiry` > " . time() . ") AS 'numOnlineUsers',(select count(1) FROM `keys` WHERE `app` = '$secret') AS 'numKeys';", 0, 1800); - $numUsers = "N/A - Use fetchStats() function in latest example"; - $numOnlineUsers = "N/A - Use fetchStats() function in latest example"; - $numKeys = "N/A - Use fetchStats() function in latest example"; + $numUsers = $row['numUsers']; + $numOnlineUsers = $row['numOnlineUsers']; + $numKeys = $row['numKeys']; $resp = json_encode(array( "success" => true, "message" => "Initialized", "sessionid" => $sessionid, "appinfo" => array( - "numUsers" => "$numUsers", - "numOnlineUsers" => "$numOnlineUsers", - "numKeys" => "$numKeys", - "version" => "$currentver", + "numUsers" => $numUsers, + "numOnlineUsers" => $numOnlineUsers, + "numKeys" => $numKeys, + "version" => $currentver, "customerPanelLink" => "https://keyauth.cc/panel/$owner/$name/" ), - "newSession" => $newSession, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $resp, $secret) : 'No encryption key supplied'; header("signature: {$sig}"); - echo $resp; - - fastcgi_finish_request(); - - if($newSession) { - misc\cache\insert("KeyAuthState:$secret:$sessionid", serialize(array("credential" => NULL, "enckey" => $enckey, "validated" => 0)), $sessionexpiry); - $time = time() + $sessionexpiry; - misc\mysql\query("INSERT INTO `sessions` (`id`, `app`, `expiry`, `created_at`, `enckey`, `ip`) VALUES (?, ?, ?, ?, NULLIF(?, ''), ?)", [$sessionid, $secret, $time, time(), $enckey, $ip]); - misc\cache\insert("KeyAuthSessionDupe:$secret:$ip", $sessionid, $sessionexpiry); - } + die($resp); case 'register': // retrieve session info @@ -360,44 +218,18 @@ // Read in username $username = misc\etc\sanitize($_POST['username'] ?? $_GET['username']); - if(strlen($username) > 70) { - $response = json_encode(array( - "success" => false, - "message" => "Username must be shorter than 70 characters" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - // Read in license key $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); - if(strlen($checkkey) > 70) { - $response = json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - // Read in password $password = misc\etc\sanitize($_POST['pass'] ?? $_GET['pass']); - - // Read in email + + // Read in email $email = misc\etc\sanitize($_POST['email'] ?? $_GET['email']); // Read in hwid $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); - $registerSuccess = false; - $resp = api\v1_0\register($username, $checkkey, $password, $email, $hwid, $secret); switch ($resp) { case 'username_taken': @@ -431,9 +263,10 @@ )); break; case 'key_banned': + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -455,28 +288,28 @@ )); break; default: - $registerSuccess = true; + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `credential` = '$username',`validated` = 1 WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); + $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); $response = json_encode(array( "success" => true, "message" => "$loggedInMsg", "info" => $resp, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); break; } $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; header("signature: {$sig}"); - echo $response; - - fastcgi_finish_request(); - - if ($registerSuccess) { - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); - misc\mysql\query("UPDATE `sessions` SET `credential` = ?,`validated` = 1 WHERE `id` = ?", [$username, $sessionid]); - } - die(); + die($response); case 'upgrade': // retrieve session info $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); @@ -489,12 +322,13 @@ // Read in key $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL // search for key - $query = misc\mysql\query("SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = ? AND `app` = ?", [$checkkey, $secret]); + $result = mysqli_query($link, "SELECT `banned`, `expires`, `status`, `level` FROM `keys` WHERE `key` = '$checkkey' AND `app` = '$secret'"); // check if key exists - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { $response = json_encode(array( "success" => false, "message" => "$keynotfound" @@ -507,10 +341,10 @@ } // if key does exist - elseif ($query->num_rows > 0) { + elseif (mysqli_num_rows($result) > 0) { // get key info - while ($row = mysqli_fetch_array($query->result)) { + while ($row = mysqli_fetch_array($result)) { $expires = $row['expires']; $status = $row['status']; $level = $row['level']; @@ -531,9 +365,9 @@ } if (!is_null($banned)) { - if (strpos($keybanned, '{reason}') !== false) { - $keybanned = str_replace("{reason}", $banned, $keybanned); - } + if (strpos($keybanned, '{reason}') !== false) { + $keybanned = str_replace("{reason}", $banned, $keybanned); + } $response = json_encode(array( "success" => false, "message" => "$keybanned" @@ -548,8 +382,8 @@ // add current time to key time $expiry = $expires + time(); - $query = misc\mysql\query("SELECT `name` FROM `subscriptions` WHERE `app` = ? AND `level` = ?", [$secret, $level]); - $subName = mysqli_fetch_array($query->result)['name']; + $result = mysqli_query($link, "SELECT `name` FROM `subscriptions` WHERE `app` = '$secret' AND `level` = '$level'"); + $subName = mysqli_fetch_array($result)['name']; $resp = misc\user\extend($username, $subName, $expiry, 0, $secret); switch ($resp) { @@ -573,13 +407,13 @@ break; case 'success': // set key to used, and set usedby - misc\mysql\query("UPDATE `keys` SET `status` = 'Used', `usedon` = ?, `usedby` = ? WHERE `key` = ? AND `app` = ?", [time(), $username, $checkkey, $secret]); + mysqli_query($link, "UPDATE `keys` SET `status` = 'Used', `usedon` = '" . time() . "', `usedby` = '$username' WHERE `key` = '$checkkey'"); misc\cache\purge('KeyAuthKeys:' . $secret . ':' . $checkkey); - misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); + misc\cache\purge('KeyAuthSubs:' . $secret . ':' . $username); $response = json_encode(array( "success" => true, "message" => "Upgraded successfully", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); break; default: @@ -612,30 +446,6 @@ // optional param for web loader $token = misc\etc\sanitize($_POST['token'] ?? $_GET['token']); - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - $response = json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - - if($forceHwid && is_null($hwid)) { - $response = json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - $resp = api\v1_0\login($username, $password, $hwid, $secret, $hwidenabled, $token); switch ($resp) { case 'un_not_found': @@ -652,8 +462,9 @@ break; case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $username]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$username'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -687,17 +498,23 @@ )); break; default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$username, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $username)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$username' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$username' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); $response = json_encode(array( "success" => true, "message" => "$loggedInMsg", "info" => $resp, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); - break; + break; } $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -710,44 +527,8 @@ $enckey = $session["enckey"]; $checkkey = misc\etc\sanitize($_POST['key'] ?? $_GET['key']); - if(strlen($checkkey) > 70) { - $response = json_encode(array( - "success" => false, - "message" => "Key must be shorter than 70 characters" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); - if(strlen($hwid) < $minHwid && !is_null($hwid)) { - $response = json_encode(array( - "success" => false, - "message" => "HWID must be {$minHwid} or more characters, change this in app settings." - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - - if($forceHwid && is_null($hwid)) { - $response = json_encode(array( - "success" => false, - "message" => "Force HWID is enabled, disable in app settings if you want to use blank HWIDs" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : NULL; - header("signature: {$sig}"); - - die($response); - } - $resp = api\v1_0\login($checkkey, $checkkey, $hwid, $secret, $hwidenabled); switch ($resp) { @@ -762,8 +543,9 @@ break; case 'user_banned': if (strpos($userbanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `users` WHERE `app` = ? AND `username` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `users` WHERE `app` = '$secret' AND `username` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $userbanned = str_replace("{reason}", $reason, $userbanned); } @@ -797,15 +579,21 @@ )); break; default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); $response = json_encode(array( "success" => true, "message" => "$loggedInMsg", "info" => $resp, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); break; } @@ -851,8 +639,9 @@ break; case 'key_banned': if (strpos($keybanned, '{reason}') !== false) { - $query = misc\mysql\query("SELECT `banned` FROM `keys` WHERE `app` = ? AND `key` = ?", [$secret, $checkkey]); - $row = mysqli_fetch_array($query->result); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `banned` FROM `keys` WHERE `app` = '$secret' AND `key` = '$checkkey'"); + $row = mysqli_fetch_array($result); $reason = $row['banned']; $keybanned = str_replace("{reason}", $reason, $keybanned); } @@ -874,13 +663,21 @@ )); break; default: - misc\mysql\query("UPDATE `sessions` SET `validated` = 1,`credential` = ? WHERE `id` = ?", [$checkkey, $sessionid]); - misc\cache\update('KeyAuthState:'.$secret.':'.$sessionid.'', array("validated" => 1, "credential" => $checkkey)); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + if ($killOtherSessions) { + mysqli_query($link, "DELETE FROM `sessions` WHERE `id` != '$sessionid' AND `credential` = '$checkkey' AND `app` = '$secret'"); + misc\cache\purgePattern('KeyAuthState:' . $secret); + } + mysqli_query($link, "UPDATE `sessions` SET `validated` = 1,`credential` = '$checkkey' WHERE `id` = '$sessionid'"); + misc\cache\purge('KeyAuthState:' . $secret . ':' . $sessionid); + + $ip = api\shared\primary\getIp(); + misc\cache\purge('KeyAuthStateDuplicates:' . $secret . ':' . $ip); $response = json_encode(array( "success" => true, "message" => "$loggedInMsg", "info" => $resp, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); } $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; @@ -891,79 +688,67 @@ $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); $enckey = $session["enckey"]; + + $un = misc\etc\sanitize($_POST['username'] ?? $_GET['username']); + $email = strtolower(misc\etc\sanitize($_POST['email'] ?? $_GET['email'])); - $un = misc\etc\sanitize($_POST['username'] ?? $_GET['username']); - $email = strtolower(misc\etc\sanitize($_POST['email'] ?? $_GET['email'])); - - $row = misc\cache\fetch('KeyAuthUser:' . $secret . ':' . $un, "SELECT * FROM `users` WHERE `username` = ? AND `app` = ?", [$un, $secret], 0); - if ($row == "not_found") { - $response = json_encode(array( + $row = misc\cache\fetch('KeyAuthUser:' . $secret . ':' . $un, "SELECT * FROM `users` WHERE `username` = '$un' AND `app` = '$secret'", 0); + if ($row == "not_found") { + $response = json_encode(array( "success" => false, "message" => "No user found with that username!" )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - - $emailHashed = $row['email']; - - if (sha1($email) != $emailHashed) { - if(is_null($emailHashed)) { - $response = json_encode(array( - "success" => false, - "message" => "Email address not provided during register, ask developer to edit your account and change email." - )); - } - else { - $response = json_encode(array( - "success" => false, - "message" => "Email address doesn't match!" - )); - } - } else { - $algos = array( - 'ripemd128', - 'md5', - 'md4', - 'tiger128,4', - 'haval128,3', - 'haval128,4', - 'haval128,5' - ); - $emailSecret = hash($algos[array_rand($algos)], misc\etc\generateRandomString()); - - misc\mysql\query("INSERT INTO `resetUsers` (`secret`, `email`, `username`, `app`, `time`) VALUES (?, SHA1(?), ?, ?, ?)", [$emailSecret, $email, $un, $secret, time()]); - - $body = '
-

Hello '.$un.',

-

You recently requested to reset your password for the software '.$name.'. Use the button below to reset it. The link is only valid for the next 24 hours.

- - - - - - -

Thanks, -
The KeyAuth team

-
'; - misc\email\send($un, $email, $body, "KeyAuth - Password Reset for {$name}"); - $response = json_encode(array( - "success" => true, - "message" => "Successfully sent email to change password.", - "nonce" => misc\etc\generateRandomString(32) + $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; + header("signature: {$sig}"); + + die($response); + } + + $emailHashed = $row['email']; + + if (sha1($email) != $emailHashed) { + $response = json_encode(array( + "success" => false, + "message" => "Email address do not match!" )); - } - + } + else { + $algos = array( + 'ripemd128', + 'md5', + 'md4', + 'tiger128,4', + 'haval128,3', + 'haval128,4', + 'haval128,5' + ); + $emailSecret = hash($algos[array_rand($algos)], misc\etc\generateRandomString()); + + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + ini_set('display_errors', 1); + ini_set('display_startup_errors', 1); + error_reporting(E_ALL); + mysqli_query($link, "INSERT INTO `resetUsers` (`secret`, `email`, `username`, `app`, `time`) VALUES ('$emailSecret', SHA1('$email'), '$un', '$secret', '" . time() . "')"); + $htmlContent = " + + + KeyAuth - You Requested A Password Reset + + +

You requested a password reset through the app {$name}

+

Please go to https://keyauth.cc/resetUser/?secret={$emailSecret}

+

Also, in case you forgot, your username is: {$un}

+

Thanks,
KeyAuth.

+ + "; + misc\email\send($un, $email, $htmlContent, "KeyAuth - Password Reset for {$name}"); + $response = json_encode(array( + "success" => true, + "message" => "Successfully sent email to change password.", + "nonce" => misc\etc\generateRandomString(32) + )); + } + $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -973,7 +758,7 @@ $session = api\shared\primary\getSession($sessionid, $secret); $enckey = $session["enckey"]; - $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT CONCAT(LEFT(`credential`, 10), IF(LENGTH(`credential`) > 10, REPEAT('*', LENGTH(`credential`) - 10), '')) AS `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = ?", [$secret], 1, 1800); + $rows = misc\cache\fetch('KeyAuthOnlineUsers:' . $secret, "SELECT DISTINCT `credential` FROM `sessions` WHERE `validated` = 1 AND `app` = '$secret'", 1, 1800); if ($rows == "not_found") { $response = json_encode(array( @@ -981,46 +766,17 @@ "message" => "No online users found!" )); } - else { - $response = json_encode(array( - "success" => true, - "message" => "Successfully fetched online users.", - "users" => $rows, - "nonce" => misc\etc\generateRandomString(32) - )); - } - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - case 'fetchStats': - $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); - $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - - $row = misc\cache\fetch('KeyAuthAppStats:' . $secret, "SELECT (SELECT COUNT(1) FROM `users` WHERE `app` = ?) AS 'numUsers', (SELECT COUNT(1) FROM `sessions` WHERE `app` = ? AND `validated` = 1 AND `expiry` > ?) AS 'numOnlineUsers', (SELECT COUNT(1) FROM `keys` WHERE `app` = ?) AS 'numKeys' FROM dual", [$secret, $secret, time(), $secret], 0, 3600); - - $numUsers = $row['numUsers']; - $numOnlineUsers = $row['numOnlineUsers']; - $numKeys = $row['numKeys']; $response = json_encode(array( "success" => true, - "message" => "Successfully fetched stats", - "appinfo" => array( - "numUsers" => "$numUsers", - "numOnlineUsers" => "$numOnlineUsers", - "numKeys" => "$numKeys", - "version" => "$currentver", - "customerPanelLink" => "https://keyauth.cc/panel/$owner/$name/" - ), - "nonce" => misc\etc\generateRandomString(32) + "message" => "Successfully fetched online users.", + "users" => $rows, + "nonce" => misc\etc\generateRandomString(32) )); - + $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); - + die($response); case 'setvar': $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); @@ -1040,60 +796,15 @@ $var = misc\etc\sanitize($_POST['var'] ?? $_GET['var']); $data = misc\etc\sanitize($_POST['data'] ?? $_GET['data']); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES ('$var', '$data', '" . $session["credential"] . "', '$secret')"); - if(is_null($var)) { - $response = json_encode(array( - "success" => true, - "message" => "No variable name provided" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - die($response); - } - - if(is_null($data)) { - $response = json_encode(array( - "success" => true, - "message" => "No variable data provided" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - die($response); - } - - if(strlen($data) > 500) { - $response = json_encode(array( - "success" => true, - "message" => "Variable data must be 500 characters or less" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - die($response); - } - - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); - - if ($row != "not_found") { - $readOnly = $row["readOnly"]; - if ($readOnly) { - $response = json_encode(array( - "success" => false, - "message" => "Variable is read only" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - die($response); - } - } - - $query = misc\mysql\query("REPLACE INTO `uservars` (`name`, `data`, `user`, `app`) VALUES (?, ?, ?, ?)", [$var, $data, $session["credential"], $secret]); - - if ($query->affected_rows != 0) { + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"]); $response = json_encode(array( "success" => true, "message" => "Successfully set variable", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; @@ -1108,7 +819,7 @@ "success" => false, "message" => "Failed to set variable" )); - die($response); + die($response); } case 'getvar': $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); @@ -1128,7 +839,7 @@ $var = misc\etc\sanitize($_POST['var'] ?? $_GET['var']); - $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data`, `readOnly` FROM `uservars` WHERE `name` = ? AND `user` = ? AND `app` = ?", [$var, $session["credential"], $secret], 0); + $row = misc\cache\fetch('KeyAuthUserVar:' . $secret . ':' . $var . ':' . $session["credential"], "SELECT `data` FROM `uservars` WHERE `name` = '$var' AND `user` = '" . $session["credential"] . "' AND `app` = '$secret'", 0); if ($row == "not_found") { $response = json_encode(array( @@ -1147,7 +858,7 @@ "success" => true, "message" => "Successfully retrieved variable", "response" => $data, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1161,7 +872,7 @@ $varid = misc\etc\sanitize($_POST['varid'] ?? $_GET['varid']); - $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = ? AND `app` = ?", [$varid, $secret], 0); + $row = misc\cache\fetch('KeyAuthVar:' . $secret . ':' . $varid, "SELECT `msg`, `authed` FROM `vars` WHERE `varid` = '$varid' AND `app` = '$secret'", 0); if ($row == "not_found") { $response = json_encode(array( @@ -1194,7 +905,7 @@ $response = json_encode(array( "success" => true, "message" => "$msg", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1209,13 +920,13 @@ $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); $ip = api\shared\primary\getIp(); - $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = ? OR `ip` = ?) AND `app` = ?", [$hwid, $ip, $secret], 0); + $row = misc\cache\fetch('KeyAuthBlacklist:' . $secret . ':' . $ip . ':' . $hwid, "SELECT 1 FROM `bans` WHERE (`hwid` = '$hwid' OR `ip` = '$ip') AND `app` = '$secret'", 0); if ($row != "not_found") { $response = json_encode(array( "success" => true, "message" => "Client is blacklisted", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1248,9 +959,9 @@ } $channel = misc\etc\sanitize($_POST['channel'] ?? $_GET['channel']); - $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ?", [$channel, $secret], 1); + $rows = misc\cache\fetch('KeyAuthChatMsgs:' . $secret . ':' . $channel, "SELECT `author`, `message`, `timestamp` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret'", 1); - if ($rows == "not_found") { + if ($rows == "not_found") { $rows = []; } @@ -1258,7 +969,7 @@ "success" => true, "message" => "Successfully retrieved chat messages", "messages" => $rows, - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1281,9 +992,10 @@ } $channel = misc\etc\sanitize($_POST['channel'] ?? $_GET['channel']); - $query = misc\mysql\query("SELECT `delay` FROM `chats` WHERE `name` = ? AND `app` = ?", [$channel, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + $result = mysqli_query($link, "SELECT `delay` FROM `chats` WHERE `name` = '$channel' AND `app` = '$secret'"); - if ($query->num_rows < 1) { + if (mysqli_num_rows($result) < 1) { $response = json_encode(array( "success" => false, "message" => "Chat channel not found" @@ -1294,12 +1006,12 @@ die($response); } - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $delay = $row['delay']; $credential = $session["credential"]; - $query = misc\mysql\query("SELECT `timestamp` FROM `chatmsgs` WHERE `author` = ? AND `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 1", [$credential, $channel, $secret]); + $result = mysqli_query($link, "SELECT `timestamp` FROM `chatmsgs` WHERE `author` = '$credential' AND `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 1"); - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $time = $row['timestamp']; if (time() - $time < $delay) { @@ -1313,9 +1025,9 @@ die($response); } - $query = misc\mysql\query("SELECT `time` FROM `chatmutes` WHERE `user` = ? AND `app` = ?", [$credential, $secret]); - if ($query->num_rows != 0) { - $row = mysqli_fetch_array($query->result); + $result = mysqli_query($link, "SELECT `time` FROM `chatmutes` WHERE `user` = '$credential' AND `app` = '$secret'"); + if (mysqli_num_rows($result) != 0) { + $row = mysqli_fetch_array($result); $unmuted = $row["time"]; $unmuted = date("F j, Y, g:i a", $unmuted); $response = json_encode(array( @@ -1329,45 +1041,19 @@ } $message = misc\etc\sanitize($_POST['message'] ?? $_GET['message']); - - if (is_null($message)) { - $response = json_encode(array( - "success" => false, - "message" => "Message can't be blank" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - - if(strlen($message) > 2000) { - $response = json_encode(array( - "success" => false, - "message" => "Message too long!" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - - misc\mysql\query("INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES (?, ?, ?, ?, ?)", [$credential, $message, time(), $channel, $secret]); - misc\mysql\query("DELETE FROM `chatmsgs` WHERE `app` = ? AND `channel` = ? AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = ? AND `app` = ? ORDER BY `id` DESC LIMIT 50) foo );", [$secret, $channel, $channel, $secret]); + mysqli_query($link, "INSERT INTO `chatmsgs` (`author`, `message`, `timestamp`, `channel`,`app`) VALUES ('$credential','$message','" . time() . "','$channel','$secret')"); + mysqli_query($link, "DELETE FROM `chatmsgs` WHERE `app` = '$secret' AND `channel` = '$channel' AND `id` NOT IN ( SELECT `id` FROM ( SELECT `id` FROM `chatmsgs` WHERE `channel` = '$channel' AND `app` = '$secret' ORDER BY `id` DESC LIMIT 50) foo );"); misc\cache\purge('KeyAuthChatMsgs:' . $secret . ':' . $channel); $response = json_encode(array( "success" => true, "message" => "Successfully sent chat message", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); die($response); case 'log': - // client isn't expecting a response body, just flush output right away so program can move on to rest of the code quicker - fastcgi_finish_request(); - // retrieve session info $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); @@ -1379,28 +1065,11 @@ $msg = misc\etc\sanitize($_POST['message'] ?? $_GET['message']); - if(is_null($msg)) { - die("No log data specified"); - } - - if(strlen($msg) > 275) { - die("Log data too long"); - } - $pcuser = misc\etc\sanitize($_POST['pcuser'] ?? $_GET['pcuser']); if (is_null($webhook)) { - $roleCheck = misc\cache\fetch('KeyAuthSellerCheck:' . $owner, "SELECT `role`,`expires` FROM `accounts` WHERE `username` = ?", [$owner], 0); - if($roleCheck['role'] == "tester") { - $query = misc\mysql\query("SELECT count(*) AS 'numLogs' FROM `logs` WHERE `logapp` = ?",[$secret]); - $row = mysqli_fetch_array($query->result); - $numLogs = $row["numLogs"]; - if($numLogs >= 20) { - die(); - } - } - - misc\mysql\query("INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES (?, ?, NULLIF(?, ''), NULLIF(?, ''), ?)", [$currtime, $msg, $credential, $pcuser, $secret]); + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL + mysqli_query($link, "INSERT INTO `logs` (`logdate`, `logdata`, `credential`, `pcuser`,`logapp`) VALUES ('$currtime','$msg',NULLIF('$credential', ''),NULLIF('$pcuser', ''),'$secret')"); die(); } @@ -1411,6 +1080,13 @@ $ip = api\shared\primary\getIp(); $json_data = json_encode([ + // Username + "username" => "KeyAuth", + + // Avatar URL. + // Uncoment to replace image set in webhook + "avatar_url" => "https://cdn.keyauth.cc/front/assets/img/favicon.png", + // Embeds Array "embeds" => [ [ @@ -1457,7 +1133,7 @@ $enckey = $session["enckey"]; $webid = misc\etc\sanitize($_POST['webid'] ?? $_GET['webid']); - $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = ? AND `app` = ?", [$webid, $secret], 0); + $row = misc\cache\fetch('KeyAuthWebhook:' . $secret . ':' . $webid, "SELECT `baselink`, `useragent`, `authed` FROM `webhooks` WHERE `webid` = '$webid' AND `app` = '$secret'", 0); if ($row == "not_found") { $response = json_encode(array( "success" => false, @@ -1512,7 +1188,7 @@ "success" => true, "message" => "Webhook request successful", "response" => "$response", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $resp, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1526,7 +1202,7 @@ $fileid = misc\etc\sanitize($_POST['fileid'] ?? $_GET['fileid']); - $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = ? AND `id` = ?", [$secret, $fileid], 0); + $row = misc\cache\fetch('KeyAuthFile:' . $secret . ':' . $fileid, "SELECT `name`, `url`, `authed` FROM `files` WHERE `app` = '$secret' AND `id` = '$fileid'", 0); if ($row == "not_found") { $response = json_encode(array( @@ -1559,8 +1235,8 @@ die($response); } } - - ini_set('memory_limit', '-1'); + + ini_set('memory_limit', '-1'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); @@ -1585,7 +1261,7 @@ "success" => true, "message" => "File download successful", "contents" => "$contents", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1610,21 +1286,10 @@ die($response); } + + $reason = misc\etc\sanitize($_POST['reason'] ?? $_GET['reason']) ?? "User banned from triggering ban function in the client"; - $reason = misc\etc\sanitize($_POST['reason'] ?? $_GET['reason']) ?? "User banned from triggering ban function in the client"; - - if(strlen($reason) > 99) { - $response = json_encode(array( - "success" => false, - "message" => "Reason must be 99 characters or less" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - + include_once (($_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/panel" || $_SERVER['DOCUMENT_ROOT'] == "/usr/share/nginx/html/api") ? "/usr/share/nginx/html" : $_SERVER['DOCUMENT_ROOT']) . '/includes/connection.php'; // create connection with MySQL $hwid = misc\etc\sanitize($_POST['hwid'] ?? $_GET['hwid']); if (!empty($hwid)) { misc\blacklist\add($hwid, "Hardware ID", $secret); @@ -1632,13 +1297,13 @@ $ip = api\shared\primary\getIp(); misc\blacklist\add($ip, "IP Address", $secret); - misc\mysql\query("UPDATE `users` SET `banned` = ? WHERE `username` = ? AND `app` = ?", [$reason, $credential, $secret]); - if ($query->affected_rows != 0) { + mysqli_query($link, "UPDATE `users` SET `banned` = '$reason' WHERE `username` = '$credential'"); + if (mysqli_affected_rows($link) != 0) { misc\cache\purge('KeyAuthUser:' . $secret . ':' . $credential); $response = json_encode(array( "success" => true, "message" => "Successfully Banned User", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); @@ -1674,274 +1339,67 @@ $response = json_encode(array( "success" => true, "message" => "Session is validated.", - "nonce" => misc\etc\generateRandomString(32) + "nonce" => misc\etc\generateRandomString(32) )); $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); die($response); } - case 'changeUsername': + case 'changeUsername': $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - - if (!$session["validated"]) { + $enckey = $session["enckey"]; + + if (!$session["validated"]) { $response = json_encode(array( "success" => false, "message" => "$sessionunauthed" )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; + $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); die($response); } $credential = $session["credential"]; - - $resp = misc\user\changeUsername($credential, $_POST['newUsername'] ?? $_GET['newUsername'], $secret); - switch ($resp) { - case 'already_used': - $response = json_encode(array( - "success" => false, - "message" => "Username already used!" - )); - break; - case 'failure': - $response = json_encode(array( - "success" => false, - "message" => "Failed to change username!" - )); - break; - case 'success': - misc\session\killSingular($sessionid, $secret); - $response = json_encode(array( - "success" => true, - "message" => "Successfully changed username, user logged out.", - "nonce" => misc\etc\generateRandomString(32) - )); - break; - default: - $response = json_encode(array( - "success" => false, - "message" => "Unhandled Error! Contact us if you need help" - )); - break; - } - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; + + $resp = misc\user\changeUsername($credential, $_POST['newUsername'] ?? $_GET['newUsername'], $secret); + switch($resp) { + case 'already_used': + $response = json_encode(array( + "success" => false, + "message" => "Username already used!" + )); + break; + case 'failure': + $response = json_encode(array( + "success" => false, + "message" => "Failed to change username!" + )); + break; + case 'success': + misc\session\killSingular($sessionid, $secret); + $response = json_encode(array( + "success" => true, + "message" => "Successfully changed username, user logged out.", + "nonce" => misc\etc\generateRandomString(32) + )); + break; + default: + $response = json_encode(array( + "success" => false, + "message" => "Unhandled Error! Contact us if you need help" + )); + break; + } + $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; header("signature: {$sig}"); die($response); - case 'logout': - $sessionid = misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']); - $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - misc\session\killSingular($sessionid, $secret); - $response = json_encode(array( - "success" => true, - "message" => "Successfully logged out.", - "nonce" => misc\etc\generateRandomString(32) - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - case '2faenable': - - list($sessionid, $code) = [misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']), misc\etc\sanitize($_POST["code"] ?? $_GET["code"])]; - $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - - if (!$session["validated"]) { - $response = json_encode(array( - "success" => false, - "message" => "$sessionunauthed" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - - $row = misc\cache\fetch('KeyAuthUser:' . $secret . ':' . $session["credential"], "SELECT * FROM `users` WHERE `username` = ? AND `app` = ?", [$session["credential"], $secret], 0); - - if ($row["2fa"]) { - - $response = json_encode(array( - "success" => false, - "message" => "2fa is already enabled on this account" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - include_once '../../auth/GoogleAuthenticator.php'; - - $_2fa = new GoogleAuthenticator(); - - if (empty($code) || is_null($code)) { - - $secret_code = $_2fa->createSecret(); - - misc\cache\insert('KeyAuthTwoFactorAuthentication:' . $session["credential"], $secret_code, 300); - - $qrcode = str_replace(urlencode("https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl="), " ", $_2fa->getQRCodeGoogleUrl($session["credential"], $secret_code, 'KeyAuth')); - - $response = json_encode(array( - "success" => true, - "2fa" => array( - "secret_code" => $secret_code, - "QRCode" => "otpauth://totp/" . $session["credential"] . "?secret=" . $secret_code . "&issuer=KeyAuth" - ) - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - else { - - $secret_code = misc\cache\select('KeyAuthTwoFactorAuthentication:' . $session["credential"]); - - if (!$secret_code) { - - $response = json_encode(array( - "success" => true, - "message" => "2fa session has expired" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - if ($_2fa->verifyCode($secret_code, $code, 2)) { - - misc\mysql\query("UPDATE `users` SET `2fa` = ?, `googleAuthCode` = ? WHERE `app` = ? AND `username` = ?", [1, $secret_code, $secret, $session["credential"]]); - misc\cache\purge('KeyAuthUser:' . $secret . ':' . $session["credential"]); - misc\cache\purge('KeyAuthTwoFactorAuthentication: ' . $session["credential"]); - - $response = json_encode(array( - "success" => true, - "message" => "2fa successfully activated" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - else { - - $response = json_encode(array( - "success" => true, - "message" => "Invalid code please try again" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - } - case '2fadisable': - - list($sessionid, $code) = [misc\etc\sanitize($_POST['sessionid'] ?? $_GET['sessionid']), misc\etc\sanitize($_POST["code"] ?? $_GET["code"])]; - - if (empty($code) || is_null($code)) { - - $response = json_encode(array( - "success" => false, - "message" => "Please provide the 2fa code to disable 2fa" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - $session = api\shared\primary\getSession($sessionid, $secret); - $enckey = $session["enckey"]; - - if (!$session["validated"]) { - $response = json_encode(array( - "success" => false, - "message" => "$sessionunauthed" - )); - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - } - - $row = misc\cache\fetch('KeyAuthUser:' . $secret . ':' . $session["credential"], "SELECT * FROM `users` WHERE `username` = ? AND `app` = ?", [$session["credential"], $secret], 0); - - if (!$row["2fa"]) { - - $response = json_encode(array( - "success" => false, - "message" => "2fa is not enabled on this account" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - include_once '../../auth/GoogleAuthenticator.php'; - - $_2fa = new GoogleAuthenticator(); - - $secret_code = $row["googleAuthCode"]; - - if ($_2fa->verifyCode($secret_code, $code, 2)) { - - misc\mysql\query("UPDATE `users` SET `2fa` = ?, `googleAuthCode` = ? WHERE `app` = ? AND `username` = ?", [0, NULL, $secret, $row["username"]]); - misc\cache\purge('KeyAuthUser:' . $secret . ':' . $session["credential"]); - - $response = json_encode(array( - "success" => true, - "message" => "2fa successfully activated" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - else { - - $response = json_encode(array( - "success" => true, - "message" => "Invalid code please try again" - )); - - $sig = !is_null($enckey) ? hash_hmac('sha256', $response, $enckey) : 'No encryption key supplied'; - header("signature: {$sig}"); - - die($response); - - } - - default: - die(json_encode(array( - "success" => false, - "message" => "The value inputted for type paramater was not found" - ))); - } + default: + die(json_encode(array( + "success" => false, + "message" => "The value inputted for type paramater was not found" + ))); +} \ No newline at end of file diff --git a/api/dashboard/webauthn.php b/api/dashboard/webauthn.php index 161e7be..c0fae3e 100644 --- a/api/dashboard/webauthn.php +++ b/api/dashboard/webauthn.php @@ -32,21 +32,21 @@ * * ------------------------------------------------------------ */ -include '../../includes/misc/autoload.phtml'; + require_once '../../vendor/lbuchs/webauthn/src/WebAuthn.php'; try { // read get argument and post body $fn = filter_input(INPUT_GET, 'fn'); - if(empty($fn)) { - die("Can't access directly"); - } + if(empty($fn)) { + die("Can't access directly"); + } $post = trim(file_get_contents('php://input')); - $userId = bin2hex(openssl_random_pseudo_bytes(10)); + $userId = bin2hex(openssl_random_pseudo_bytes(10)); if ($post) { $post = json_decode($post); } - - session_start(); + + session_start(); // Formats $formats = array(); $formats[] = 'none'; @@ -86,13 +86,14 @@ $ids = array(); // load registrations from session stored there by processCreate. - - $query = misc\mysql\query("SELECT * FROM `securityKeys` WHERE `username` = ?", [$_SESSION['pendingUsername']]); - if ($query->num_rows > 0) { - while ($row = mysqli_fetch_array($query->result)) { - $ids[] = base64_decode($row["credentialId"]); - } - } + include '../../includes/connection.php'; + + $result = mysqli_query($link, "SELECT * FROM `securityKeys` WHERE `username` = '".$_SESSION['pendingUsername']."'"); + if (mysqli_num_rows($result) > 0) { + while ($row = mysqli_fetch_array($result)) { + $ids[] = base64_decode($row["credentialId"]); + } + } if (count($ids) === 0) { throw new Exception('No security key registrations found for this user!'); @@ -121,11 +122,13 @@ $data = $WebAuthn->processCreate($clientDataJSON, $attestationObject, $challenge, 0, false, false); unset($_SESSION['challenge']); // disgard challenge array from session file, no longer needed - - $name = misc\etc\sanitize($_GET['name']); - - misc\mysql\query("INSERT INTO `securityKeys` (`username`, `name`, `credentialId`, `credentialPublicKey`) VALUES (?, ?, ?, ?)", [$_SESSION['username'], $name, base64_encode($data->credentialId), $data->credentialPublicKey]); - misc\mysql\query("UPDATE `accounts` SET `securityKey` = 1 WHERE `username` = ?", [$_SESSION['username']]); + + include '../../includes/connection.php'; + include '../../includes/misc/autoload.phtml'; + + $name = misc\etc\sanitize($_GET['name']); + mysqli_query($link, "INSERT INTO `securityKeys` (`username`, `name`, `credentialId`, `credentialPublicKey`) VALUES ('".$_SESSION['username']."', '$name', '".base64_encode($data->credentialId)."', '".$data->credentialPublicKey."')"); + mysqli_query($link, "UPDATE `accounts` SET `securityKey` = 1 WHERE `username` = '" . $_SESSION['username'] . "'"); $return = new stdClass(); $return->success = true; @@ -152,16 +155,17 @@ // looking up correspondending public key of the credential id // you should also validate that only ids of the given user name // are taken for the login. - - $query = misc\mysql\query("SELECT * FROM `securityKeys` WHERE `username` = ?", [$_SESSION['pendingUsername']]); - if ($query->num_rows > 0) { - while ($row = mysqli_fetch_array($query->result)) { - if(base64_decode($row["credentialId"]) === $id) { - $credentialPublicKey = $row["credentialPublicKey"]; + include '../../includes/connection.php'; + + $result = mysqli_query($link, "SELECT * FROM `securityKeys` WHERE `username` = '".$_SESSION['pendingUsername']."'"); + if (mysqli_num_rows($result) > 0) { + while ($row = mysqli_fetch_array($result)) { + if(base64_decode($row["credentialId"]) === $id) { + $credentialPublicKey = $row["credentialPublicKey"]; break; - } - } - } + } + } + } if ($credentialPublicKey === null) { throw new Exception('This security key wasn\'t found!'); @@ -169,10 +173,10 @@ // process the get request. throws WebAuthnException if it fails $WebAuthn->processGet($clientDataJSON, $authenticatorData, $signature, $credentialPublicKey, $challenge, null, 0); - - unset($_SESSION['challenge']); // disgard challenge array from session file, no longer needed - $_SESSION['username'] = $_SESSION['pendingUsername']; - unset($_SESSION['pendingUsername']); + + unset($_SESSION['challenge']); // disgard challenge array from session file, no longer needed + $_SESSION['username'] = $_SESSION['pendingUsername']; + unset($_SESSION['pendingUsername']); $return = new stdClass(); $return->success = true; diff --git a/api/index.php b/api/index.php index be08028..72c4e06 100644 --- a/api/index.php +++ b/api/index.php @@ -1,3 +1,3 @@ '
', + "referrer"=>$row['referrer'] ?? 'N/A', + "username"=>$row['username'], + "action"=>$row['action'], + ); + } + + ## Response + $response = array( + "draw" => intval($draw), + "iTotalRecords" => $totalRecords, + "iTotalDisplayRecords" => $totalRecordwithFilter, + "aaData" => $data + ); + + die(json_encode($response)); + +} + +die("Request not from datatables, aborted."); \ No newline at end of file diff --git a/app/black-fetch.php b/app/black-fetch.php index 16bc522..3056299 100644 --- a/app/black-fetch.php +++ b/app/black-fetch.php @@ -1,96 +1,71 @@ getMessage()); - die("Error: " . $errorMsg); -}); - if (session_status() === PHP_SESSION_NONE) { - session_start(); + session_start(); } -if ($_SESSION['role'] == "Reseller") { - die("Resellers can't access this."); +if($_SESSION['role'] == "Reseller") { + die("Resellers can't access this."); } -if (!isset($_SESSION['app'])) { - die("Application not selected."); +if(!isset($_SESSION['app'])) { + die("Application not selected."); } -if (isset($_POST['draw'])) { - - // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ - - $draw = intval($_POST['draw']); - $row = intval($_POST['start']); - $rowperpage = intval($_POST['length']); // Rows display per page - $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index - $columnName = misc\etc\sanitize($_POST['columns'][$columnIndex]['data']); // Column name - $columnSortOrder = misc\etc\sanitize($_POST['order'][0]['dir']); // asc or desc - $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value - - // whitelist certain column names and sort orders to prevent SQL injection - if (!in_array($columnName, array("type"))) { - die("Column name is not whitelisted."); - } - - if (!in_array($columnSortOrder, array("desc", "asc"))) { - die("Column sort order is not whitelisted."); - } - - if (!is_null($searchValue)) { - $query = misc\mysql\query("select * from `bans` WHERE (`ip` like ? or `hwid` like ? or `type` like ? ) and app = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, ["%" . $searchValue . "%", "%" . $searchValue . "%", "%" . $searchValue . "%", $_SESSION['app']]); - } - else { - $query = misc\mysql\query("select * from `bans` WHERE app = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, [$_SESSION['app']]); - } - - $data = array(); - - while ($row = mysqli_fetch_assoc($query->result)) { - $data[] = array( - "data" => '' . ($row["hwid"] ?? $row["ip"]) . '', - "type" => $row["type"], - "actions" => - '
- -
- -
- - - - - -
', - ); - } - - ## Response - $response = array( - "draw" => intval($draw), - "aaData" => $data - ); +if(isset($_POST['draw']) ) { + + // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ + + $draw = misc\etc\sanitize($_POST['draw']); + $row = misc\etc\sanitize($_POST['start']); + $rowperpage = misc\etc\sanitize($_POST['length']); // Rows display per page + $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index + $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value + + ## Search + $searchQuery = " "; + if($searchValue != ''){ + $searchQuery = " and (`ip` like '%".$searchValue."%' or + `hwid` like '%".$searchValue."%' or + `type` like'%".$searchValue."%' ) "; + } + + ## Total number of records without filtering + $sel = mysqli_query($link,"select count(1) as allcount from `bans` where app = '".$_SESSION['app']."'"); + $records = mysqli_fetch_assoc($sel); + $totalRecords = $records['allcount']; + + ## Total number of record with filtering + $sel = mysqli_query($link,"select count(1) as allcount from `bans` WHERE 1 ".$searchQuery." and app = '".$_SESSION['app']."'"); + $records = mysqli_fetch_assoc($sel); + $totalRecordwithFilter = $records['allcount']; + + ## Fetch records + $empQuery = "select * from `bans` WHERE 1 ".$searchQuery." and app = '".$_SESSION['app']."' limit ".$row.",".$rowperpage; + // echo $empQuery; + $empRecords = mysqli_query($link, $empQuery); + $data = array(); + + while ($row = mysqli_fetch_assoc($empRecords)) { + $data[] = array( + "data"=>$row["hwid"] ?? $row["ip"], + "type"=>$row["type"], + "actions"=>'
', + ); + } + + ## Response + $response = array( + "draw" => intval($draw), + "iTotalRecords" => $totalRecords, + "iTotalDisplayRecords" => $totalRecordwithFilter, + "aaData" => $data + ); + + die(json_encode($response)); - die(json_encode($response)); } -die("Request not from datatables, aborted."); +die("Request not from datatables, aborted."); \ No newline at end of file diff --git a/app/download-types.php b/app/download-types.php deleted file mode 100644 index fdb3a75..0000000 --- a/app/download-types.php +++ /dev/null @@ -1,169 +0,0 @@ - array(), - "subscription" => array() - ) - ); - - $jsondata = json_decode($jsonarray); - - $userquery = misc\mysql\query("SELECT * FROM `users` WHERE `app` = ?", [$_SESSION['app']]); - - while ($row = mysqli_fetch_array($userquery->result)) { - - $userjson = array( - "username" => $row["username"], - "email" => $row["email"], - "password" => $row["password"], - "hwid" => $row["hwid"], - "banned" => $row["banned"], - "ip" => $row["ip"] - ); - - array_push($jsondata->users, $userjson); - } - - $subscriptionquery = misc\mysql\query("SELECT * FROM `subs` WHERE `app` = ? ", [$_SESSION['app']]); - - while ($row = mysqli_fetch_array($subscriptionquery->result)) { - - $subjson = array( - "user" => $row["user"], - "subscription" => "default", - "expiry" => $row["expiry"] - ); - - array_push($jsondata->subscription, $subjson); - } - - $newjson = json_encode($jsondata); - - header('Content-Description: File Transfer'); - header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename="KeyAuthUsers.json"'); - header('Expires: 0'); - header('Cache-Control: must-revalidate'); - header('Pragma: public'); - header('Content-Length: ' . strlen($newjson)); - - - die($newjson); - case 'licenses': - $query = misc\mysql\query("SELECT * FROM `keys` WHERE `app` = ?",[$_SESSION['app']]); - - // Create an array to hold the keys - $keysArray = array(); - - while ($row = mysqli_fetch_array($query->result)) { - // Add each key to the array - $keysArray[] = array( - "key" => $row['key'], - "level" => $row['level'], - "expiry" => $row['expires'] / 86400 - ); - } - - // Convert the array to a JSON string - $jsonData = json_encode($keysArray); - - header('Content-Description: File Transfer'); - header('Content-Type: application/json'); // Set the content type to JSON - header('Content-Disposition: attachment; filename="KeyAuthKeys.json"'); - header('Expires: 0'); - header('Cache-Control: must-revalidate'); - header('Pragma: public'); - header('Content-Length: ' . strlen($jsonData)); - - echo $jsonData; - - die($stringData); - case 'logs': - $jsonarray = json_encode( - array( - "logs" => array() - ) - ); - - $jsondata = json_decode($jsonarray); - - $userlogquery = misc\mysql\query("SELECT * FROM `logs` WHERE `logapp` = ?", [$_SESSION['app']]); - - while ($row = mysqli_fetch_array($userlogquery->result)) { - - $userlogjson = array( - "logdate" => $row["logdate"], - "logdata" => $row["logdata"], - "credential" => $row["credential"], - "pcuser" => $row["pcuser"] - ); - - array_push($jsondata->logs, $userlogjson); - } - - - $newjson = json_encode($jsondata); - - header('Content-Description: File Transfer'); - header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename="KeyAuthUserLogs.json"'); - header('Expires: 0'); - header('Cache-Control: must-revalidate'); - header('Pragma: public'); - header('Content-Length: ' . strlen($newjson)); - - - die($newjson); - case 'auditLog': - $jsonarray = json_encode( - array( - "auditLog" => array() - ) - ); - - $jsondata = json_decode($jsonarray); - - $userquery = misc\mysql\query("SELECT * FROM `auditLog` WHERE `app` = ?", [$_SESSION['app']]); - - while ($row = mysqli_fetch_array($userquery->result)) { - - $userjson = array( - "id" => $row["id"], - "user" => $row["user"], - "event" => $row["event"], - "time" => $row["time"], - ); - - array_push($jsondata->auditLog, $userjson); - } - - - $newjson = json_encode($jsondata); - - header('Content-Description: File Transfer'); - header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename="KeyAuthAuditLogs.json"'); - header('Expires: 0'); - header('Cache-Control: must-revalidate'); - header('Pragma: public'); - header('Content-Length: ' . strlen($newjson)); - - die($newjson); - default: - echo 'Invalid Type or Type does not Exist'; -} diff --git a/app/index.php b/app/index.php index 017960d..82ec462 100644 --- a/app/index.php +++ b/app/index.php @@ -1,4 +1,5 @@ getMessage()); - \dashboard\primary\error($errorMsg); -}); - $username = $_SESSION['username']; -$query = misc\mysql\query("SELECT * FROM `accounts` WHERE `username` = ?",[$username]); -$row = mysqli_fetch_array($query->result); +($result = mysqli_query($link, "SELECT * FROM `accounts` WHERE `username` = '$username'")) or die(mysqli_error($link)); +$row = mysqli_fetch_array($result); $banned = $row['banned']; $lastreset = $row['lastreset']; -if (!is_null($banned) || $_SESSION['logindate'] < $lastreset || $query->num_rows < 1) { +if (!is_null($banned) || $_SESSION['logindate'] < $lastreset || mysqli_num_rows($result) < 1) { echo ""; session_destroy(); exit(); @@ -39,27 +29,27 @@ $role = $row['role']; $permissions = $row['permissions']; $admin = $row['admin']; -$staff = $row['staff']; -$formBanned = $row['formBanned']; $twofactor = $row['twofactor']; $_SESSION['role'] = $role; $expires = $row['expires']; -if (in_array($role, array("developer", "seller"))) { +if (in_array($role, array( + "developer", + "seller" +))) { $_SESSION['timeleft'] = dashboard\primary\expireCheck($username, $expires); } if (!$_SESSION['app']) // no app selected yet { - $query = misc\mysql\query("SELECT `secret`, `name`, `banned`, `sellerkey` FROM `apps` WHERE `owner` = ? AND `ownerid` = ?",[$_SESSION['username'], $_SESSION['ownerid']]); // select all apps where owner is current user - if ($query->num_rows == 1) // if the user only owns one app, load that app (they can still change app after it's loaded) + $result = mysqli_query($link, "SELECT * FROM `apps` WHERE `owner` = '" . $_SESSION['username'] . "'"); // select all apps where owner is current user + if (mysqli_num_rows($result) == 1) // if the user only owns one app, load that app (they can still change app after it's loaded) { - $row = mysqli_fetch_array($query->result); + $row = mysqli_fetch_array($result); $_SESSION['name'] = $row["name"]; $_SESSION["selectedApp"] = $row["name"]; $_SESSION['app'] = $row["secret"]; - $_SESSION['sellerkey'] = $row["sellerkey"]; } } ?> @@ -68,88 +58,87 @@ - - - - - - - - - - - - - + + Keyauth - Open Source Auth + + + + + + + + + + + + - - - - - - - - - + - -
-
-
- -
-
- - Loading taking a while? Please - feel free to return. - -
- - - -
-
- - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/layout/_footer.php b/app/layout/_footer.php new file mode 100644 index 0000000..5982213 --- /dev/null +++ b/app/layout/_footer.php @@ -0,0 +1,16 @@ + + + \ No newline at end of file diff --git a/app/layout/_loader.php b/app/layout/_loader.php new file mode 100644 index 0000000..7187055 --- /dev/null +++ b/app/layout/_loader.php @@ -0,0 +1,9 @@ + +
+ Logo +
+ + Loading... +
+
+ \ No newline at end of file diff --git a/app/layout/_scrolltop.php b/app/layout/_scrolltop.php new file mode 100644 index 0000000..a9d0a55 --- /dev/null +++ b/app/layout/_scrolltop.php @@ -0,0 +1,14 @@ + +
+ + + + + + + + +
+ \ No newline at end of file diff --git a/app/layout/aside.php b/app/layout/aside.php deleted file mode 100644 index f78d9d5..0000000 --- a/app/layout/aside.php +++ /dev/null @@ -1,190 +0,0 @@ - - - - -
diff --git a/app/layout/aside/_base.php b/app/layout/aside/_base.php new file mode 100644 index 0000000..e32e62e --- /dev/null +++ b/app/layout/aside/_base.php @@ -0,0 +1,53 @@ + + + +
+ + + + + +
+ + + + + +
+ + +
+ \ No newline at end of file diff --git a/app/layout/aside/_menu.php b/app/layout/aside/_menu.php new file mode 100644 index 0000000..64bfac9 --- /dev/null +++ b/app/layout/aside/_menu.php @@ -0,0 +1,348 @@ + + + + + + + + + + + + +
+ + + + +
+ + + + \ No newline at end of file diff --git a/app/layout/aside/resellermenu.php b/app/layout/aside/resellermenu.php new file mode 100644 index 0000000..2ce2507 --- /dev/null +++ b/app/layout/aside/resellermenu.php @@ -0,0 +1,72 @@ + + + + + + + + + + + + +
+ + + + +
+ + + + \ No newline at end of file diff --git a/app/layout/breadcrumb.php b/app/layout/breadcrumb.php deleted file mode 100644 index 924b92f..0000000 --- a/app/layout/breadcrumb.php +++ /dev/null @@ -1,22 +0,0 @@ - diff --git a/app/layout/footer.php b/app/layout/footer.php deleted file mode 100644 index 0d0603f..0000000 --- a/app/layout/footer.php +++ /dev/null @@ -1,17 +0,0 @@ - diff --git a/app/layout/header/_base.php b/app/layout/header/_base.php new file mode 100644 index 0000000..8ba6ae1 --- /dev/null +++ b/app/layout/header/_base.php @@ -0,0 +1,52 @@ + +
+ +
+ +
+
+ + + + + + + + +
+
+ + +
+ + Logo + +
+ + +
+ +
+ + + +
+ + +
+ + + +
+ +
+ +
+ +
+ \ No newline at end of file diff --git a/app/layout/header/_menu.php b/app/layout/header/_menu.php new file mode 100644 index 0000000..9b1c19d --- /dev/null +++ b/app/layout/header/_menu.php @@ -0,0 +1,31 @@ + + +
+ + + +
+ \ No newline at end of file diff --git a/app/layout/master.php b/app/layout/master.php index 7abcf05..0b5a8cb 100644 --- a/app/layout/master.php +++ b/app/layout/master.php @@ -1,39 +1,39 @@ - - -
- - - - - - -
-
- - -
- - + + + +
+ +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ +
+ + + + +
+
+
+ + + \ No newline at end of file diff --git a/app/layout/page-title/_default.php b/app/layout/page-title/_default.php new file mode 100644 index 0000000..c80c549 --- /dev/null +++ b/app/layout/page-title/_default.php @@ -0,0 +1,13 @@ + + +
+ +

+ + +

+ +
+ \ No newline at end of file diff --git a/app/layout/profile.php b/app/layout/profile.php deleted file mode 100644 index c03531d..0000000 --- a/app/layout/profile.php +++ /dev/null @@ -1,92 +0,0 @@ -num_rows > 0) { - while ($row_ = mysqli_fetch_array($query->result)) { - $acclogs = $row_['acclogs']; - $expiry = $row_["expires"]; - $emailVerify = $row_["emailVerify"]; - } -} - -if ($_SERVER['REQUEST_METHOD'] === 'POST') { - if (isset($_POST['logout'])) { - session_destroy(); - header('Location: /login'); - exit; - } -} - -?> - -
-
- - -
- -
-
-
- " alt="profile image" /> - -
- -
- - - " . strtoupper($role) . " PLAN

"; - ?> - - '', - 'developer' => '', - 'seller' => '', - 'Reseller' => '', - 'Manager' => '', - 'default' => '' - }; - echo $display; - - if ($role === 'developer' || $role === 'seller') { - echo ''; - } - ?> -
-
-
diff --git a/app/layout/toolbars/_toolbar-1.php b/app/layout/toolbars/_toolbar-1.php new file mode 100644 index 0000000..651d46b --- /dev/null +++ b/app/layout/toolbars/_toolbar-1.php @@ -0,0 +1,11 @@ + +
+ +
+ + + + +
+ +
\ No newline at end of file diff --git a/app/layout/topbar.php b/app/layout/topbar.php deleted file mode 100644 index 978bce8..0000000 --- a/app/layout/topbar.php +++ /dev/null @@ -1,133 +0,0 @@ - - - diff --git a/app/layout/topbar/_base.php b/app/layout/topbar/_base.php new file mode 100644 index 0000000..a3ff659 --- /dev/null +++ b/app/layout/topbar/_base.php @@ -0,0 +1,49 @@ + +
+ +
+

+
+ +
+ + +
+ + + + + + + + +
+
+ + + + + + + + +
+
+ +
+ \ No newline at end of file diff --git a/app/layout/topbar/partials/_user-menu.php b/app/layout/topbar/partials/_user-menu.php new file mode 100644 index 0000000..4d8b78a --- /dev/null +++ b/app/layout/topbar/partials/_user-menu.php @@ -0,0 +1,43 @@ + + + \ No newline at end of file diff --git a/app/license-download.php b/app/license-download.php new file mode 100644 index 0000000..7b23efc --- /dev/null +++ b/app/license-download.php @@ -0,0 +1,49 @@ +getMessage()); -}); - if (session_status() === PHP_SESSION_NONE) { - session_start(); + session_start(); } if ($_SESSION['role'] == "Reseller") { - die("Resellers can't access this."); + die("Resellers can't access this."); } if (!isset($_SESSION['app'])) { - dashboard\primary\error("Application not selected"); - die("Application not selected."); + die("Application not selected."); } if (isset($_POST['draw'])) { - // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ - - $draw = intval($_POST['draw']); - $row = intval($_POST['start']); - $rowperpage = intval($_POST['length']); // Rows display per page - $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index - $columnName = misc\etc\sanitize($_POST['columns'][$columnIndex]['data']); // Column name - $columnSortOrder = misc\etc\sanitize($_POST['order'][0]['dir']); // asc or desc - $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value - - // whitelist certain column names and sort orders to prevent SQL injection - if (!in_array($columnName, array("key", "gendate", "genby", "expires", "note", "usedon", "usedby", "status"))) { - die("Column name is not whitelisted."); - } - - if (!in_array($columnSortOrder, array("desc", "asc"))) { - die("Column sort order is not whitelisted."); - } - - if (!is_null($searchValue)) { - $query = misc\mysql\query("select * from `keys` WHERE (`key` like ? or `note` like ? or `genby` like ? or `usedby` like ? ) and app = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, ["%" . $searchValue . "%", "%" . $searchValue . "%", "%" . $searchValue . "%", "%" . $searchValue . "%", $_SESSION['app']]); - } - else { - $query = misc\mysql\query("select * from `keys` WHERE app = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, [$_SESSION['app']]); - } - - $data = array(); - - while ($row = mysqli_fetch_assoc($query->result)) { - - ## If only one or two keys exists then we will use custom margin to fix the bugging menu - $banBtns = ""; - if ($row['status'] == "Banned") { - $banBtns = ''; - } else { - $banBtns = ''; - } - - $MarginManager = ""; - if ($query->num_rows < 2) { - $MarginManager = "margin-bottom: 20px;"; - } else { - $MarginManager = "margin-bottom: 0px;"; - } - - $data[] = array( - "key" => $row['key'], - "gendate" => '
', - "genby" => $row['genby'], - "expires" => timeconversion($row["expires"]), - "note" => $row['note'] ?? 'N/A', - "usedon" => (!is_null($row["usedon"])) ? '
' : 'N/A', - "usedby" => ($row["usedby"] == $row['key']) ? 'Same as key' : $row["usedby"] ?? 'N/A', - "status" => '' . $row['status'] . '', - - "actions" => ' -
- -
- -
    -
  • - -
  • -
  • - ' . $banBtns . ' -
  • -
  • - -
  • -
-
- - -
- ', - ); - } - - ## Response - $response = array( - "draw" => intval($draw), - "aaData" => $data - ); - - die(json_encode($response)); + // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ + + $draw = misc\etc\sanitize($_POST['draw']); + $row = misc\etc\sanitize($_POST['start']); + $rowperpage = misc\etc\sanitize($_POST['length']); // Rows display per page + $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index + $columnName = misc\etc\sanitize($_POST['columns'][$columnIndex]['data']); // Column name + $columnSortOrder = misc\etc\sanitize($_POST['order'][0]['dir']); // asc or desc + $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value + + ## Search + $searchQuery = " "; + if ($searchValue != '') { + $searchQuery = " and (`key` like '%" . $searchValue . "%' or + `note` like '%" . $searchValue . "%' or + `genby` like'%" . $searchValue . "%' or + `usedby` like'%" . $searchValue . "%' ) "; + } + + ## Total number of records without filtering + $sel = mysqli_query($link, "select count(1) as allcount from `keys` where app = '" . $_SESSION['app'] . "'"); + $records = mysqli_fetch_assoc($sel); + $totalRecords = $records['allcount']; + + ## Total number of record with filtering + $sel = mysqli_query($link, "select count(1) as allcount from `keys` WHERE 1 " . $searchQuery . " and app = '" . $_SESSION['app'] . "'"); + $records = mysqli_fetch_assoc($sel); + $totalRecordwithFilter = $records['allcount']; + + ## Fetch records + $empQuery = "select * from `keys` WHERE 1 " . $searchQuery . " and app = '" . $_SESSION['app'] . "' order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage; + // echo $empQuery; + $empRecords = mysqli_query($link, $empQuery); + + $data = array(); + + while ($row = mysqli_fetch_assoc($empRecords)) { + + ## If only one or two keys exists then we will use custom margin to fix the bugging menu + $banBtns = ""; + if ($row['status'] == "Banned") { $banBtns = ''; } else { $banBtns = 'Ban'; } + + $MarginManager = ""; + if ($totalRecordwithFilter < 2) { $MarginManager = "margin-bottom: 20px;"; } else { $MarginManager = "margin-bottom: 0px;"; } + + $data[] = array( + "key" => $row['key'], + "gendate" => '
', + "genby" => $row['genby'], + "expires" => ($row["expires"] / 86400) . ' Day(s)', + "note" => $row['note'] ?? 'N/A', + "usedon" => (!is_null($row["usedon"])) ? '
' : 'N/A', + "usedby" => ($row["usedby"] == $row['key']) ? 'Same as key' : $row["usedby"] ?? 'N/A', + "status" => '', + "actions" => '
', + ); + } + + ## Response + $response = array( + "draw" => intval($draw), + "iTotalRecords" => $totalRecords, + "iTotalDisplayRecords" => $totalRecordwithFilter, + "aaData" => $data + ); + + die(json_encode($response)); } -die("Request not from datatables, aborted."); +die("Request not from datatables, aborted."); \ No newline at end of file diff --git a/app/log-fetch.php b/app/log-fetch.php index f89fc05..fde22b4 100644 --- a/app/log-fetch.php +++ b/app/log-fetch.php @@ -1,75 +1,74 @@ getMessage()); -}); - if (session_status() === PHP_SESSION_NONE) { - session_start(); + session_start(); } -if ($_SESSION['role'] == "Reseller") { - die("Resellers can't access this."); +if($_SESSION['role'] == "Reseller") { + die("Resellers can't access this."); } -if (!isset($_SESSION['app'])) { - dashboard\primary\error("Application not selected"); - die("Application not selected."); +if(!isset($_SESSION['app'])) { + die("Application not selected."); } -if (isset($_POST['draw'])) { - - // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ - - $draw = intval($_POST['draw']); - $row = intval($_POST['start']); - $rowperpage = intval($_POST['length']); // Rows display per page - $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index - $columnName = misc\etc\sanitize($_POST['columns'][$columnIndex]['data']); // Column name - $columnSortOrder = misc\etc\sanitize($_POST['order'][0]['dir']); // asc or desc - $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value - - // whitelist certain column names and sort orders to prevent SQL injection - if (!in_array($columnName, array("logdate", "logdata", "credential", "pcuser"))) { - die("Column name is not whitelisted."); - } - - if (!in_array($columnSortOrder, array("desc", "asc"))) { - die("Column sort order is not whitelisted."); - } - - if (!is_null($searchValue)) { - $query = misc\mysql\query("select * from `logs` WHERE (`logdata` like ? or `credential` like ? or `pcuser` like ? ) and logapp = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, ["%" . $searchValue . "%", "%" . $searchValue . "%", "%" . $searchValue . "%", $_SESSION['app']]); - } - else { - $query = misc\mysql\query("select * from `logs` WHERE logapp = ? order by `" . $columnName . "` " . $columnSortOrder . " limit " . $row . "," . $rowperpage, [$_SESSION['app']]); - } - - $data = array(); - - while ($row = mysqli_fetch_assoc($query->result)) { - $data[] = array( - "logdate" => '
', - "logdata" => $row['logdata'], - "credential" => $row['credential'] ?? 'N/A', - "pcuser" => $row['pcuser'] ?? 'N/A', - ); - } - - ## Response - $response = array( - "draw" => intval($draw), - "aaData" => $data - ); +if(isset($_POST['draw']) ) { + + // credits to https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/ + + $draw = misc\etc\sanitize($_POST['draw']); + $row = misc\etc\sanitize($_POST['start']); + $rowperpage = misc\etc\sanitize($_POST['length']); // Rows display per page + $columnIndex = misc\etc\sanitize($_POST['order'][0]['column']); // Column index + $columnName = misc\etc\sanitize($_POST['columns'][$columnIndex]['data']); // Column name + $columnSortOrder = misc\etc\sanitize($_POST['order'][0]['dir']); // asc or desc + $searchValue = misc\etc\sanitize($_POST['search']['value']); // Search value + + ## Search + $searchQuery = " "; + if($searchValue != ''){ + $searchQuery = " and (`logdata` like '%".$searchValue."%' or + `credential` like '%".$searchValue."%' or + `pcuser` like'%".$searchValue."%' ) "; + } + + ## Total number of records without filtering + $sel = mysqli_query($link,"select count(1) as allcount from `logs` where logapp = '".$_SESSION['app']."'"); + $records = mysqli_fetch_assoc($sel); + $totalRecords = $records['allcount']; + + ## Total number of record with filtering + $sel = mysqli_query($link,"select count(1) as allcount from `logs` WHERE 1 ".$searchQuery." and logapp = '".$_SESSION['app']."'"); + $records = mysqli_fetch_assoc($sel); + $totalRecordwithFilter = $records['allcount']; + + ## Fetch records + $empQuery = "select * from `logs` WHERE 1 ".$searchQuery." and logapp = '".$_SESSION['app']."' order by `".$columnName."` ".$columnSortOrder." limit ".$row.",".$rowperpage; + // echo $empQuery; + $empRecords = mysqli_query($link, $empQuery); + $data = array(); + + while ($row = mysqli_fetch_assoc($empRecords)) { + $data[] = array( + "logdate"=>'
', + "logdata"=>$row['logdata'], + "credential"=>$row['credential'] ?? 'N/A', + "pcuser"=>$row['pcuser'] ?? 'N/A', + ); + } + + ## Response + $response = array( + "draw" => intval($draw), + "iTotalRecords" => $totalRecords, + "iTotalDisplayRecords" => $totalRecordwithFilter, + "aaData" => $data + ); + + die(json_encode($response)); - die(json_encode($response)); } -die("Request not from datatables, aborted."); +die("Request not from datatables, aborted."); \ No newline at end of file diff --git a/app/pages/account-logs.php b/app/pages/account-logs.php index 2f9dde5..7844da5 100644 --- a/app/pages/account-logs.php +++ b/app/pages/account-logs.php @@ -1,53 +1,54 @@ - -
-
-
- -

Account Logs

-

View the event history of your account.

-
-
- -
- - - - - - - - - - result)) - { - $rows[] = $r; - } - - foreach ($rows as $row) - { - ?> - - - - - - - - -
DateIPUser-Agent
- -
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DateIP AddressUser Agent
+ +
+ +
+ \ No newline at end of file diff --git a/app/pages/account-settings.php b/app/pages/account-settings.php index 3c0f7ec..12f6af1 100644 --- a/app/pages/account-settings.php +++ b/app/pages/account-settings.php @@ -1,494 +1,550 @@ that's the only thing you need on this page."); } - $twofactor = $row['twofactor']; - require_once '../auth/GoogleAuthenticator.php'; $gauth = new GoogleAuthenticator(); -if ($row["googleAuthCode"] == NULL) -{ +if ($row["googleAuthCode"] == NULL) { $code_2factor = $gauth->createSecret(); - misc\mysql\query("UPDATE `accounts` SET `googleAuthCode` = ? WHERE `username` = ?", [$code_2factor, $_SESSION['username']]); -} -else -{ + $integrate_code = mysqli_query($link, "UPDATE `accounts` SET `googleAuthCode` = '$code_2factor' WHERE `username` = '" . $_SESSION['username'] . "'") or die(mysqli_error($link)); +} else { $code_2factor = $row["googleAuthCode"]; } - $google_QR_Code = $gauth->getQRCodeGoogleUrl($_SESSION['username'], $code_2factor, 'KeyAuth'); +?> + -$query = misc\mysql\query("SELECT * FROM `accounts` WHERE `username` = ?", [$_SESSION['username']]); -if ($query->num_rows > 0) -{ - while ($row = mysqli_fetch_array($query->result)) - { - $acclogs = $row['acclogs']; - $expiry = $row["expires"]; - $emailVerify = $row["emailVerify"]; + +
+ + + + + 0) { + while ($row = mysqli_fetch_array($result)) { + $acclogs = $row['acclogs']; + $expiry = $row["expires"]; + $emailVerify = $row["emailVerify"]; + } } -} -if (isset($_POST['updatesettings'])) - { + if (isset($_POST['updatesettings'])) { $pfp = misc\etc\sanitize($_POST['pfp']); $acclogs = misc\etc\sanitize($_POST['acclogs']); $emailVerify = misc\etc\sanitize($_POST['emailVerify']); - misc\mysql\query("UPDATE `accounts` SET `acclogs` = ? WHERE `username` = ?", [$acclogs, $_SESSION['username']]); - - if ($acclogs == 0) - { - misc\mysql\query("DELETE FROM `acclogs` WHERE `username` = ?", [$_SESSION['username']]); // delete all account logs + mysqli_query($link, "UPDATE `accounts` SET `acclogs` = '$acclogs' WHERE `username` = '" . $_SESSION['username'] . "'"); + if ($acclogs == 0) { + mysqli_query($link, "DELETE FROM `acclogs` WHERE `username` = '" . $_SESSION['username'] . "'"); // delete all account logs } - - misc\mysql\query("UPDATE `accounts` SET `emailVerify` = ? WHERE `username` = ?", [$emailVerify, $_SESSION['username']]); - if (isset($_POST['pfp']) && trim($_POST['pfp']) != '') - { + mysqli_query($link, "UPDATE `accounts` SET `emailVerify` = '$emailVerify' WHERE `username` = '" . $_SESSION['username'] . "'"); + if (isset($_POST['pfp']) && trim($_POST['pfp']) != '') { if (!filter_var($pfp, FILTER_VALIDATE_URL)) { dashboard\primary\error("Invalid Url For Profile Image!"); - echo ""; + echo ""; return; } - if (strpos($pfp, "file:///") !== false) { - dashboard\primary\error("Url must start with https://"); - echo ""; + if (strpos($pfp, "file:///") !== false) { + dashboard\primary\error("Url must start with https://"); + echo ""; return; - } + } $_SESSION['img'] = $pfp; - misc\mysql\query("UPDATE `accounts` SET `img` = ? WHERE `username` = ?", [$pfp, $_SESSION['username']]); + mysqli_query($link, "UPDATE `accounts` SET `img` = '$pfp' WHERE `username` = '" . $_SESSION['username'] . "'"); } dashboard\primary\success("Updated Account Settings!"); } - if (isset($_POST['submit_code'])) - { - $code = misc\etc\sanitize($_POST['scan_code1'] . ($_POST['scan_code2']) . ($_POST['scan_code3']) . ($_POST['scan_code4']) . ($_POST['scan_code5']) . ($_POST['scan_code6'])); + if (isset($_POST['submit_code'])) { + + if (empty($_POST['scan_code'])) { + dashboard\primary\error("You must fill in all the fields!"); + } + + $code = misc\etc\sanitize($_POST['scan_code']); - $query = misc\mysql\query("SELECT `googleAuthCode` from `accounts` WHERE `username` = ?", [$_SESSION['username']]); + $user_result = mysqli_query($link, "SELECT * from `accounts` WHERE `username` = '" . $_SESSION['username'] . "'") or die(mysqli_error($link)); + + while ($row = mysqli_fetch_array($user_result)) { - while ($row = mysqli_fetch_array($query->result)) - { $secret_code = $row['googleAuthCode']; } $checkResult = $gauth->verifyCode($secret_code, $code, 2); - if ($checkResult) - { - $query = misc\mysql\query("UPDATE `accounts` SET `twofactor` = '1' WHERE `username` = ?", [$_SESSION['username']]); - if ($query->affected_rows > 0) - { - echo ""; + if ($checkResult) { + $enable_2factor = mysqli_query($link, "UPDATE `accounts` SET `twofactor` = '1' WHERE `username` = '" . $_SESSION['username'] . "'") or die(mysqli_error($link)); + if ($enable_2factor) { dashboard\primary\success("Two-factor security has been successfully activated on your account!"); - dashboard\primary\wh_log($logwebhook, "{$username} has enabled 2FA", $webhookun); - } - else - { - echo ""; - dashboard\primary\wh_log($logwebhook, "{$username} has disabled 2FA", $webhookun); - dashboard\primary\success("Two-factor security has been successfully disabled on your account!"); + } else { + dashboard\primary\error("There was a problem trying to activate security on your account!"); } - } - else - { - dashboard\primary\error("Invalid 2FA code! Make sure your device time settings are synced."); + } else { + dashboard\primary\error("The code entered is incorrect"); } } - if (isset($_POST['submit_code_disable'])) - { - $code = misc\etc\sanitize($_POST['scan_code1'] . ($_POST['scan_code2']) . ($_POST['scan_code2']) . ($_POST['scan_code3']) . ($_POST['scan_code4']) . ($_POST['scan_code5'])); + if (isset($_POST['submit_code_disable'])) { + + if (empty($_POST['scan_code'])) { + dashboard\primary\error("You must fill in all the fields!"); + } - $query = misc\mysql\query("SELECT `googleAuthCode` from `accounts` WHERE `username` = ?", [$_SESSION['username']]); + $code = misc\etc\sanitize($_POST['scan_code']); - while ($row = mysqli_fetch_array($query->result)) - { + $user_result = mysqli_query($link, "SELECT * from `accounts` WHERE `username` = '" . $_SESSION['username'] . "'") or die(mysqli_error($link)); + + while ($row = mysqli_fetch_array($user_result)) { $secret_code = $row['googleAuthCode']; } $checkResult = $gauth->verifyCode($secret_code, $code, 2); - if ($checkResult) - { - $query = misc\mysql\query("UPDATE `accounts` SET `twofactor` = '0', `googleAuthCode` = NULL WHERE `username` = ?", [$_SESSION['username']]); - - if ($query->affected_rows > 0) - { - dashboard\primary\success("Successfully disabled 2FA!"); - } - else - { - dashboard\primary\error("Failed to disable 2FA!"); + if ($checkResult) { + $enable_2factor = mysqli_query($link, "UPDATE `accounts` SET `twofactor` = '0' WHERE `username` = '" . $_SESSION['username'] . "'") or die(mysqli_error($link)); + + if ($enable_2factor) { + dashboard\primary\success("Two-factor security has been successfully disabled on your account!"); + } else { + dashboard\primary\error("There was a problem trying to activate security on your account!"); } - } - else - { - dashboard\primary\error("Invalid 2FA code! Make sure your device time settings are synced."); + } else { + dashboard\primary\error("The code entered is incorrect"); } } - - if (isset($_POST['deleteWebauthn'])) - { - $name = misc\etc\sanitize($_POST['deleteWebauthn']); - - $query = misc\mysql\query("DELETE FROM `securityKeys` WHERE `name` = ? AND `username` = ?", [$name, $_SESSION['username']]); - - if ($query->affected_rows > 0) - { - $query = misc\mysql\query("SELECT 1 FROM `securityKeys` WHERE `username` = ?", [$_SESSION['username']]); - if ($query->num_rows == 0) - { - misc\mysql\query("UPDATE `accounts` SET `securityKey` = 0 WHERE `username` = ?", [$_SESSION['username']]); - } + + if (isset($_POST['deleteWebauthn'])) { + $name = misc\etc\sanitize($_POST['deleteWebauthn']); + + mysqli_query($link, "DELETE FROM `securityKeys` WHERE `name` = '$name' AND `username` = '".$_SESSION['username']."'"); + + if (mysqli_affected_rows($link) > 0) { + $result = mysqli_query($link, "SELECT 1 FROM `securityKeys` WHERE `username` = '".$_SESSION['username']."'"); + if (mysqli_num_rows($result) == 0) { + mysqli_query($link, "UPDATE `accounts` SET `securityKey` = 0 WHERE `username` = '" . $_SESSION['username'] . "'"); + } dashboard\primary\success("Successfully deleted security key"); - } - else - { + } else { dashboard\primary\error("Failed to delete security key!"); } - } + } -?> + ?> + +
+ +
+ +
+ +
+
+ +
+ + + +
+ + + +
+ +
+
+ +
+ + -
-
-
- -

Account Settings

-

Manage your account.

-
-
-
-
- -
-
- - +
-
- - +
+
+
+ +
+ + + +
+ +
Change password here + https://' . ($_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME']) . '/forgot/'; ?> +
+ +
-
- -
+
+ +
+ + + +
+ + + +
- -
- " - readonly> -
- - -
- Free Forever " - autocomplete="on" readonly> - - - - +
+ +
+ + + +
+ +
Change email here + https://' . ($_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME']) . '/changeEmail/'; ?> +
+ +
+
+
+ +
+ + + +
+ +
Change username here + https://' . ($_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME']) . '/changeUsername/'; ?> +
+ +
- - -
- -
+
- - +
- Enable 2FA'; - } else { - echo 'Disable 2FA'; - } - ?> - - - - - Change Password - - - Change Email - - - Change Username - - - Delete Account - - - - - - -