1
0
Fork 0
mirror of https://github.com/Alamantus/Lexiconga.git synced 2025-06-06 17:26:36 +02:00

Fix errors with PHP classes; fix create-account

This commit is contained in:
Robbie Antenesse 2017-12-24 14:19:42 -07:00
parent 0009f671b7
commit 81f938b2c0
5 changed files with 69 additions and 50 deletions

View file

@ -1,11 +1,16 @@
<?php <?php
class Db { class Db {
private $dbh; private $dbh;
function _construct() { function __construct() {
$this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password'); $this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password');
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} }
public function execute ($query, $params = array()) {
$stmt = $this->dbh->prepare($query);
return $stmt->execute($params);
}
public function query ($query, $params = array()) { public function query ($query, $params = array()) {
$stmt = $this->dbh->prepare($query); $stmt = $this->dbh->prepare($query);
$stmt->execute($params); $stmt->execute($params);

View file

@ -1,8 +1,14 @@
<?php <?php
class Response { class Response {
public static function out ($data, $http_code) { public static function json ($data, $http_code = 200) {
header('Content-Type: application/json'); header('Content-Type: application/json; charset=utf-8');
http_response_code($http_code); http_response_code($http_code);
echo json_encode($data); echo json_encode($data);
}
public static function html ($html, $http_code = 200) {
header('Content-Type: text/html; charset=utf-8');
http_response_code($http_code);
echo $html;
} }
} }

View file

@ -4,7 +4,7 @@ use \Firebase\JWT\JWT;
class Token { class Token {
private $key; private $key;
function _construct() { function __construct() {
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva'; $this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
} }

View file

@ -1,11 +1,11 @@
<?php <?php
require_once('./Db'); require_once('./Db.php');
require_once('./Token'); require_once('./Token.php');
class User { class User {
private $db; private $db;
private $token; private $token;
function _construct () { function __construct () {
$this->db = new Db(); $this->db = new Db();
$this->token = new Token(); $this->token = new Token();
} }
@ -32,63 +32,58 @@ class User {
return false; return false;
} }
public function emailExists ($email) {
$query = 'SELECT * FROM users WHERE email=?';
$user = $this->db->query($query, array($email));
return $user->rowCount() > 0;
}
public function create ($email, $password) { public function create ($email, $password) {
$insert_user_query = 'INSERT INTO users (email, password) VALUES (?, ?)'; $insert_user_query = 'INSERT INTO users (email, password) VALUES (?, ?)';
$password_hash = password_hash($password, PASSWORD_DEFAULT); $password_hash = password_hash($password, PASSWORD_DEFAULT);
// Use a transaction to make sure all pieces are created successfully. $insert_user = $this->db->execute($insert_user_query, array($email, $password_hash));
$this->db->dbh->beginTransaction();
$insert_user = $this->db->query($insert_user_query, array($email, $password_hash));
if ($insert_user === true) { if ($insert_user === true) {
$new_user_id = $this->db->lastInsertId(); $new_user_id = $this->db->lastInsertId();
$token = $this->createDictionary($new_user_id); $token = $this->createDictionary($new_user_id);
if ($token !== false) { if ($token !== false) {
if ($this->db->dbh->commit()) { return $token;
return $token;
}
} }
} }
$this->db->dbh->rollBack();
return false; return false;
} }
public function createDictionary ($user) { public function createDictionary ($user) {
$this->db->dbh->beginTransaction();
$insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)"; $insert_dictionary_query = "INSERT INTO dictionaries (user) VALUES ($user)";
$insert_dictionary = $this->db->query($insert_dictionary_query); $insert_dictionary = $this->db->execute($insert_dictionary_query);
if ($insert_dictionary === true) { if ($insert_dictionary === true) {
$new_dictionary_id = $this->db->lastInsertId(); $new_dictionary_id = $this->db->lastInsertId();
$insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)"; $insert_linguistics_query = "INSERT INTO dictionary_linguistics (dictionary) VALUES ($new_dictionary_id)";
$insert_linguistics = $this->db->query($insert_dictionary_query); $insert_linguistics = $this->db->execute($insert_linguistics_query);
if ($insert_linguistics === true) { if ($insert_linguistics === true) {
if ($this->changeCurrentDictionary($user, $new_dictionary_id)) { if ($this->changeCurrentDictionary($user, $new_dictionary_id)) {
if ($this->db->dbh->commit()) { $user_data = array(
$user_data = array( 'id' => $user,
'id' => $user, 'isMember' => $this->hasMembership($user),
'isMember' => $this->hasMembership($user['id']), 'dictionary' => $new_dictionary_id,
'dictionary' => $new_dictionary_id, );
); return $this->token->encode($user_data);
return $this->token->encode($user_data);
}
} }
} }
} }
$this->db->dbh->rollBack();
return false; return false;
} }
public function changeCurrentDictionary ($user, $dictionary) { public function changeCurrentDictionary ($user, $dictionary) {
$update_query = 'UPDATE users SET current_dictionary=? WHERE id=?'; $update_query = 'UPDATE users SET current_dictionary=? WHERE id=?';
$update = $this->db->query($update_query, array($user, $dictionary)); $update = $this->db->query($update_query, array($dictionary, $user));
if ($update->rowCount() > 0) { if ($update->rowCount() > 0) {
return true; return true;
} }

View file

@ -2,48 +2,61 @@
require_once('./Response.php'); require_once('./Response.php');
require_once('./User.php'); require_once('./User.php');
$action = $_POST['action']; $inputJSON = file_get_contents('php://input');
$token = $_POST['token']; $request= json_decode($inputJSON, true);
$action = isset($request['action']) ? $request['action'] : '';
$token = isset($request['token']) ? $request['token'] : '';
switch ($action) { switch ($action) {
case 'login': { case 'login': {
if ($_POST['email'] && $_POST['password']) { if (isset($request['email']) && isset($request['password'])) {
$user = new User(); $user = new User();
$token = $user->logIn($_POST['email'], $_POST['password']); $token = $user->logIn($request['email'], $request['password']);
if ($token !== false) { if ($token !== false) {
return Response::out(array( return Response::json(array(
'data' => $token, 'data' => $token,
'error' => false, 'error' => false,
), 200); ), 200);
} }
return Response::out(array( return Response::json(array(
'data' => 'Could not log in: incorrect data', 'data' => 'Could not log in: incorrect data',
'error' => true, 'error' => true,
), 400); ), 401);
} }
return Response::out(array( return Response::json(array(
'data' => 'Could not log in: required information missing', 'data' => 'Could not log in: required information missing',
'error' => true, 'error' => true,
), 500); ), 400);
} }
case 'create-account': { case 'create-account': {
if ($_POST['email'] && $_POST['password']) { if (isset($request['email']) && isset($request['password'])) {
$user = new User(); $user = new User();
$token = $user->create($_POST['email'], $_POST['password']); if (!$user->emailExists($request['email'])) {
if ($token !== false) { $token = $user->create($request['email'], $request['password']);
return Response::out(array( if ($token !== false) {
'data' => $token, return Response::json(array(
'error' => false, 'data' => $token,
), 200); 'error' => false,
), 201);
}
return Response::json(array(
'data' => 'Could not create account: database error',
'error' => true,
), 500);
} }
return Response::out(array( return Response::json(array(
'data' => 'Could not create account: incorrect data', 'data' => 'Could not create account: duplicate email',
'error' => true, 'error' => true,
), 400); ), 403);
} }
return Response::out(array( return Response::json(array(
'data' => 'Could not create account: required information missing', 'data' => 'Could not create account: required information missing',
'error' => true, 'error' => true,
), 500); ), 400);
}
default: {
return Response::html('Hi!');
} }
} }