Guest User

Untitled

a guest
Jan 15th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. <?
  2.  
  3. //Database Information
  4.  
  5. $dbhost = "31.170.160.76";
  6. $dbname = "testdatabase";
  7. $dbuser = "(personalinformation)";
  8. $dbpass = "tested123";
  9.  
  10.  
  11. //Connect to database
  12.  
  13. mysql_connect ( $dbhost, $dbuser, $dbpass) or die("Could not connect: ".mysql_error());
  14. mysql_select_db($dbname) or die(mysql_error());
  15.  
  16. //executes a given sql query with the params and returns an array as result
  17. function query() {
  18. global $link;
  19. $debug = false;
  20.  
  21. //get the sql query
  22. $args = func_get_args();
  23. $sql = array_shift($args);
  24.  
  25. //secure the input
  26. for ($i=0;$i<count($args);$i++) {
  27. $args[$i] = urldecode($args[$i]);
  28. $args[$i] = mysqli_real_escape_string($link, $args[$i]);
  29. }
  30.  
  31. //build the final query
  32. $sql = vsprintf($sql, $args);
  33.  
  34. if ($debug) print $sql;
  35.  
  36. //execute and fetch the results
  37. $result = mysqli_query($link, $sql);
  38. if (mysqli_errno($link)==0 && $result) {
  39.  
  40. $rows = array();
  41.  
  42. if ($result!==true)
  43. while ($d = mysqli_fetch_assoc($result)) {
  44. array_push($rows,$d);
  45. }
  46.  
  47. //return json
  48. return array('result'=>$rows);
  49.  
  50. } else {
  51.  
  52. //error
  53. return array('error'=>'Database error');
  54. }
  55. }
  56.  
  57. //loads up the source image, resizes it and saves with -thumb in the file name
  58. function thumb($srcFile, $sideInPx) {
  59.  
  60. $image = imagecreatefromjpeg($srcFile);
  61. $width = imagesx($image);
  62. $height = imagesy($image);
  63.  
  64. $thumb = imagecreatetruecolor($sideInPx, $sideInPx);
  65.  
  66. imagecopyresized($thumb,$image,0,0,0,0,$sideInPx,$sideInPx,$width,$height);
  67.  
  68. imagejpeg($thumb, str_replace(".jpg","-thumb.jpg",$srcFile), 85);
  69.  
  70. imagedestroy($thumb);
  71. imagedestroy($image);
  72. }
  73.  
  74. ?>
  75.  
  76. <?
  77. session_start();
  78. require("lib.php");
  79. require("api.php");
  80.  
  81. header("Content-Type: application/json");
  82.  
  83. switch ($_POST['command']) {
  84. case "login":
  85. login($_POST['username'], $_POST['password']); break;
  86.  
  87. case "register":
  88. register($_POST['username'], $_POST['password']); break;
  89.  
  90. }
  91.  
  92. exit();
  93. ?>
  94.  
  95. <?php
  96.  
  97. function errorJson($msg){
  98. print json_encode(array('error'=>$msg));
  99. exit();
  100. }
  101.  
  102. function register($user, $pass) {
  103. //check if username exists
  104. $login = query("SELECT username FROM login WHERE username='%s' limit 1", $user);
  105. if (count($login['result'])>0) {
  106. errorJson('Username already exists');
  107.  
  108. //try to register the user
  109. $result = query("INSERT INTO login(username, pass) VALUES('%s','%s')", $user, $pass);
  110. if (!$result['error']) {
  111. //success
  112. login($user, $pass);
  113. } else {
  114. //error
  115. errorJson('Registration failed');
  116. }
  117. }
  118. }
  119. function login($user, $pass) {
  120. $result = query("SELECT IdUser, username FROM login WHERE username='%s' AND pass='%s' limit 1", $user, $pass);
  121.  
  122. if (count($result['result'])>0) {
  123. //authorized
  124. $_SESSION['IdUser'] = $result['result'][0]['IdUser'];
  125. print json_encode($result);
  126. } else {
  127. //not authorized
  128. errorJson('Authorization failed');
  129. }
  130. }
  131.  
  132.  
  133. ?>
Add Comment
Please, Sign In to add comment