Fix errors with PHP classes; fix create-account
This commit is contained in:
parent
0009f671b7
commit
81f938b2c0
|
@ -1,11 +1,16 @@
|
|||
<?php
|
||||
class Db {
|
||||
private $dbh;
|
||||
function _construct() {
|
||||
function __construct() {
|
||||
$this->dbh = new PDO('mysql:host=localhost;dbname=lexiconga;charset=utf8', 'root', 'password');
|
||||
$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()) {
|
||||
$stmt = $this->dbh->prepare($query);
|
||||
$stmt->execute($params);
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
<?php
|
||||
class Response {
|
||||
public static function out ($data, $http_code) {
|
||||
header('Content-Type: application/json');
|
||||
public static function json ($data, $http_code = 200) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
http_response_code($http_code);
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ use \Firebase\JWT\JWT;
|
|||
|
||||
class Token {
|
||||
private $key;
|
||||
function _construct() {
|
||||
function __construct() {
|
||||
$this->key = 'ˈkɑːn.læŋ.ɪŋ_4eva';
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
require_once('./Db');
|
||||
require_once('./Token');
|
||||
require_once('./Db.php');
|
||||
require_once('./Token.php');
|
||||
|
||||
class User {
|
||||
private $db;
|
||||
private $token;
|
||||
function _construct () {
|
||||
function __construct () {
|
||||
$this->db = new Db();
|
||||
$this->token = new Token();
|
||||
}
|
||||
|
@ -32,63 +32,58 @@ class User {
|
|||
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) {
|
||||
$insert_user_query = 'INSERT INTO users (email, password) VALUES (?, ?)';
|
||||
$password_hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
|
||||
// Use a transaction to make sure all pieces are created successfully.
|
||||
$this->db->dbh->beginTransaction();
|
||||
|
||||
$insert_user = $this->db->query($insert_user_query, array($email, $password_hash));
|
||||
$insert_user = $this->db->execute($insert_user_query, array($email, $password_hash));
|
||||
if ($insert_user === true) {
|
||||
$new_user_id = $this->db->lastInsertId();
|
||||
|
||||
$token = $this->createDictionary($new_user_id);
|
||||
|
||||
if ($token !== false) {
|
||||
if ($this->db->dbh->commit()) {
|
||||
return $token;
|
||||
}
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->dbh->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createDictionary ($user) {
|
||||
$this->db->dbh->beginTransaction();
|
||||
|
||||
$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) {
|
||||
$new_dictionary_id = $this->db->lastInsertId();
|
||||
|
||||
$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 ($this->changeCurrentDictionary($user, $new_dictionary_id)) {
|
||||
if ($this->db->dbh->commit()) {
|
||||
$user_data = array(
|
||||
'id' => $user,
|
||||
'isMember' => $this->hasMembership($user['id']),
|
||||
'dictionary' => $new_dictionary_id,
|
||||
);
|
||||
return $this->token->encode($user_data);
|
||||
}
|
||||
$user_data = array(
|
||||
'id' => $user,
|
||||
'isMember' => $this->hasMembership($user),
|
||||
'dictionary' => $new_dictionary_id,
|
||||
);
|
||||
return $this->token->encode($user_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->dbh->rollBack();
|
||||
return false;
|
||||
}
|
||||
|
||||
public function changeCurrentDictionary ($user, $dictionary) {
|
||||
$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) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -2,48 +2,61 @@
|
|||
require_once('./Response.php');
|
||||
require_once('./User.php');
|
||||
|
||||
$action = $_POST['action'];
|
||||
$token = $_POST['token'];
|
||||
$inputJSON = file_get_contents('php://input');
|
||||
$request= json_decode($inputJSON, true);
|
||||
|
||||
$action = isset($request['action']) ? $request['action'] : '';
|
||||
$token = isset($request['token']) ? $request['token'] : '';
|
||||
|
||||
switch ($action) {
|
||||
case 'login': {
|
||||
if ($_POST['email'] && $_POST['password']) {
|
||||
if (isset($request['email']) && isset($request['password'])) {
|
||||
$user = new User();
|
||||
$token = $user->logIn($_POST['email'], $_POST['password']);
|
||||
$token = $user->logIn($request['email'], $request['password']);
|
||||
if ($token !== false) {
|
||||
return Response::out(array(
|
||||
return Response::json(array(
|
||||
'data' => $token,
|
||||
'error' => false,
|
||||
), 200);
|
||||
}
|
||||
return Response::out(array(
|
||||
return Response::json(array(
|
||||
'data' => 'Could not log in: incorrect data',
|
||||
'error' => true,
|
||||
), 400);
|
||||
), 401);
|
||||
}
|
||||
return Response::out(array(
|
||||
return Response::json(array(
|
||||
'data' => 'Could not log in: required information missing',
|
||||
'error' => true,
|
||||
), 500);
|
||||
), 400);
|
||||
}
|
||||
case 'create-account': {
|
||||
if ($_POST['email'] && $_POST['password']) {
|
||||
if (isset($request['email']) && isset($request['password'])) {
|
||||
$user = new User();
|
||||
$token = $user->create($_POST['email'], $_POST['password']);
|
||||
if ($token !== false) {
|
||||
return Response::out(array(
|
||||
'data' => $token,
|
||||
'error' => false,
|
||||
), 200);
|
||||
if (!$user->emailExists($request['email'])) {
|
||||
$token = $user->create($request['email'], $request['password']);
|
||||
if ($token !== false) {
|
||||
return Response::json(array(
|
||||
'data' => $token,
|
||||
'error' => false,
|
||||
), 201);
|
||||
}
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: database error',
|
||||
'error' => true,
|
||||
), 500);
|
||||
}
|
||||
return Response::out(array(
|
||||
'data' => 'Could not create account: incorrect data',
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: duplicate email',
|
||||
'error' => true,
|
||||
), 400);
|
||||
), 403);
|
||||
}
|
||||
return Response::out(array(
|
||||
return Response::json(array(
|
||||
'data' => 'Could not create account: required information missing',
|
||||
'error' => true,
|
||||
), 500);
|
||||
), 400);
|
||||
}
|
||||
|
||||
default: {
|
||||
return Response::html('Hi!');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue