Advertisement
coderboy

PHP Code

Aug 19th, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. // Roblox Agent Data
  5. $roblox_token = "0";
  6.  
  7. // Database
  8. $sql_server = "localhost";
  9. $sql_username = "roblox";
  10. $sql_password = "Eje%xpv%sfbmmz%uijol%j%xbt%hpjoh%up%hjwf%zpv%nz%qbttxpsc";
  11. $sql_databse = "roblox";
  12. $conn = new mysqli($sql_server, $sql_username, $sql_password, $sql_databse);
  13.  
  14. // Table ENUMS
  15. $table_users = "users";
  16. $table_tokens = "userTokens";
  17. $table_2FA = "verifyRequests";
  18.  
  19. // Prepared Statements
  20. $user_istatement = $conn->prepare("SELECT * FROM $table_users WHERE username='?'");
  21. $user_fstatement = $conn->prepare("INSERT INTO $table_users (uuid, robloxid, username, password) VALUES (?, ?, ?, ?)");
  22. $token_istatement = $conn->prepare("INSERT INTO $table_tokens (issued, expires, userUuid, tokenhash) VALUES (?, ?, ?, ?)");
  23. $token_fstatement = $conn->prepare("SELECT * FROM $table_tokens WHERE tokenhash='?'");
  24. $R2FA_a_istatement = $conn->prepare("INSERT INTO $table_2FA (uuid, verificationCode) VALUES (?, ?)");
  25. $R2FA_b_istatement = $conn->prepare("UPDATE $table_2FA SET ?='?' WHERE uuid='?'");
  26. $R2FA_fstatement = $conn->prepare("SELECT * FROM $table_2FA WHERE uuid='?'");
  27.  
  28. // Settings
  29. $token_length = (2*60)*1000; // in ms [ (2*60)*1000 = 2 mins ]
  30.  
  31. // Main Functions
  32. function generatePIN($digits = 4){
  33. $i = 0; //counter
  34. $pin = ""; //our default pin is blank.
  35. while($i < $digits){
  36. //generate a random number between 0 and 9.
  37. $pin .= mt_rand(0, 9);
  38. $i++;
  39. }
  40. return $pin;
  41. }
  42.  
  43. function getR2FAData($id) {
  44. $reply = array();
  45.  
  46. $R2FA_fstatement->bind_param("s", $id);
  47.  
  48. if($R2FA_fstatement->execute()){
  49. $result = $R2FA_fstatement->get_result();
  50. if($result->nom_rows > 0){
  51. while($row = $result->fetch_assoc()){
  52. // return data
  53. $reply["row"]=$row;
  54. return $reply;
  55. }
  56. } else {
  57. // No Data
  58. $reply["status"] = -2;
  59. return $reply;
  60. }
  61. } else {
  62. // Error
  63. $reply["status"] = -3;
  64. return $reply;
  65. }
  66. }
  67.  
  68. function updateR2FA($id, $vals) {
  69. foreach ($id as $name=>$value) {
  70. if ($name=="userUuid") {
  71. $token_istatement->bind_param("sss", $name, $value, $id);
  72. $token_istatement->execute();
  73. } elseif ($name=="verificationCode") {
  74. $token_istatement->bind_param("sss", $name, $value, $id);
  75. $token_istatement->execute();
  76. } elseif ($name=="status") {
  77. $token_istatement->bind_param("sss", $name, $value, $id);
  78. $token_istatement->execute();
  79. }
  80. }
  81. }
  82.  
  83. function verifyR2FA($id, $pin) {
  84. $data = getR2FAData($id);
  85.  
  86. if ( md5($pin) == $data["row"]["verificationCode"] ) {
  87. return true;
  88. } else {
  89. return false;
  90. }
  91.  
  92. }
  93.  
  94. function newR2FA() {
  95.  
  96. $pin_p = generatePIN();
  97. $pin_s = md5($pin_p);
  98. $id = md5( ( $pin_s . strval( time() ) ) );
  99.  
  100. $token_istatement->bind_param("ss", $id, $pin_p);
  101. $token_istatement->execute();
  102.  
  103. return array("pin"=>$pin_s, "id"=>$id);
  104. }
  105.  
  106. function getTokenData($token) {
  107. $reply = array();
  108.  
  109. $token_fstatement->bind_param("s", $username);
  110.  
  111. if($token_fstatement->execute()){
  112. $result = $token_fstatement->get_result();
  113. if($result->nom_rows > 0){
  114. while($row = $result->fetch_assoc()){
  115. // return data
  116. $reply["issued"]=$row["issued"];
  117. $reply["expires"]=$row["expires"];
  118. $reply["id"]=$row["userUuid"];
  119. $reply["public"]=$row["tokenhash"];
  120.  
  121. return $reply;
  122. }
  123. } else {
  124. // No Data
  125. $reply["status"] = -2;
  126. return $reply;
  127. }
  128. } else {
  129. // Error
  130. $reply["status"] = -3;
  131. return $reply;
  132. }
  133. }
  134.  
  135. function validateToken($public, $private) {
  136. $now = time();
  137.  
  138. $tokenData = getTokenData($public);
  139. // Still valid ( time ). Check if the token private/public match
  140. if ( $public == md5( $private ) ) {
  141. // Do a second check with the data from the DB
  142. if ( $public == $tokenData["public"] ) {
  143.  
  144. if ( md5($private) == $tokenData["public"] ) {
  145. if ($now <= inval($tokenData["expires"])) {
  146. // valid
  147. return 1;
  148. } else {
  149. // time expired
  150. return -1;
  151. }
  152. } else {
  153. // INVALID TOKEN
  154. return -4;
  155. }
  156.  
  157. } else {
  158. // mismatched data (publics)
  159. return -3;
  160. }
  161.  
  162. } else {
  163. // Input values invalid
  164. return -2;
  165. }
  166.  
  167. }
  168.  
  169. function createToken($userID) {
  170. // {issued,expires,uuid,hash}
  171.  
  172. $now = time();
  173. $private = ("token@" . $userID . ":" . time() . "/" . rand());
  174. $public = md5($private);
  175. $issued = strval($now);
  176. $expires = strval( ($now + $token_length) );
  177.  
  178.  
  179. $token_istatement->bind_param("ssss", $issued, $expires, $userID, $public);
  180. $token_istatement->execute();
  181.  
  182. // Create an object to return this data
  183. $token = array();
  184. $token["user"] = $userID;
  185. $token["token-private"] = $private;
  186. $token["token-public"] = $public;
  187. $token["issued"] = $issued;
  188. $token["expires"] = $expires;
  189. return $token;
  190. }
  191.  
  192. function validateLogin($username, $password) {
  193. $reply = array();
  194.  
  195. $login_istatement->bind_param("s", $username);
  196.  
  197. if($login_istatement->execute()){
  198. $result = $login_istatement->get_result();
  199. if($result->nom_rows > 0){
  200. while($row = $result->fetch_assoc()){
  201. // check if valid
  202. if ($row["password"]==$password) {
  203. // Valid Login
  204. $reply["status"] = 1;
  205. $reply["success"] = true;
  206. $reply["username"] = $username;
  207. $reply["uuid"] = $row["uuid"];
  208. $reply["robloxID"] = $row["robloxid"];
  209. return $reply;
  210. } else {
  211. // Invalid Login
  212. $reply["status"] = -1;
  213. $reply["success"] = false;
  214. return $reply;
  215. }
  216. }
  217. } else {
  218. // No Data
  219. $reply["status"] = -2;
  220. $reply["success"] = false;
  221. return $reply;
  222. }
  223. } else {
  224. // Error
  225. $reply["status"] = -3;
  226. $reply["success"] = false;
  227. return $reply;
  228. }
  229. }
  230.  
  231. function createUser($username, $password, $robloxID, $R2FAid, $R2FApin) {
  232. $responce = array();
  233.  
  234. $status = verifyR2FA($R2FAid, $R2FApin);
  235. if (!status) {
  236. return array("status"=>false, "reason"=>"unverified");
  237. }
  238.  
  239. $uuid = md5( ( "user::" . md5( $username ) . ":" . $password . md5( time() ) ) );
  240.  
  241. $token_istatement->bind_param("ssss", $uuid, $robloxID, $username, $password);
  242. $token_istatement->execute();
  243.  
  244. return array("status"=>"okay", "reason"=>"account created");
  245. }
  246.  
  247. // Main Segment
  248. $task = $_GET["o"];
  249.  
  250. $mode = array();
  251.  
  252. if (isset($_GET["m"])) {
  253. $m = $_GET["m"];
  254. $mode["type"] = $m;
  255. if ($m=="r") {
  256. $url = $_GET["url"];
  257. $mode["url"] = $url;
  258. }
  259. } else {
  260. // use defaults
  261. $mode["type"] = "d";
  262. }
  263.  
  264. function reply($vals) {
  265. if ($mode["type"]=="d") {
  266. echo json_encode($vals);
  267. die( json_encode($vals) );
  268. } elseif ($mode["type"]=="r") {
  269. // Create a JS redirrect
  270. $url = $mode["url"];
  271.  
  272. echo "<form id='r' action='$url' method='post'>";
  273. foreach ($vals as $a => $b) {
  274. echo "<input type='hidden' name='$a' value='$b' />";
  275. }
  276. echo "</form>";
  277.  
  278. echo "You will soon be redirrected.<br>";
  279. echo "If your browser does not redirrect you, please click this link:";
  280. echo "<script>document.getElementById('r).submit();</script>";
  281. }
  282. }
  283.  
  284. if ($task=="login") {
  285.  
  286. $username = $_POST["username"];
  287. $password = md5($_POST["password"]);
  288.  
  289. $status = validateLogin($username, $password);
  290.  
  291. if ($stats["success"]) {
  292. $token = createToken($status["uuid"]);
  293. $_SESSION["session-public"] = $token["token-public"];
  294. $_SESSION["session-private"] = $token["token-private"];
  295. reply(array(
  296. "status"=>"okay"
  297. ));
  298. } else {
  299. reply(array(
  300. "status"=>"invalidLogin"
  301. ));
  302. }
  303.  
  304. } elseif ($task=="newR2FA") {
  305. $data = newR2FA(); // {pin, id}
  306. reply($data);
  307. } elseif ($task=="R2FAStatus") {
  308. $id = $_GET["id"];
  309. $data = getR2FAData($id);
  310. if ($data["status"]==1) {
  311. reply(array("status"=>("verified")));
  312. } elseif ($data["status"]==0) {
  313. reply(array("status"=>("waiting")));
  314. }
  315. } elseif ($task=="signup") {
  316. $R2FA_id = $_POST["R2faid"];
  317. $R2FA_pin = $_POST["R2fapin"];
  318. $uername = $_POST["username"];
  319. $password = md5($_POST["password"]);
  320.  
  321. $R2FA_data = getR2FAData($R2FA_id);
  322. if ($R2FA_data["status"]!=1) {
  323. reply(array("status"=>"failed","error-code"=>"0x2F","message"=>"Roblox account not assosiated!"));
  324. }
  325.  
  326. $robloxID = $R2FA_data["userUuid"];
  327.  
  328. $status = createUser($username, $password, $robloxID, $R2FA_id, $R2FA_pin);
  329. reply($status);
  330. }
  331.  
  332. echo "WORKING";
  333. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement