Skip to content
Snippets Groups Projects
Commit 3808e5a0 authored by Sibidharan's avatar Sibidharan :speech_balloon:
Browse files

API for Notes - start

parent 3c4645a1
No related branches found
No related tags found
No related merge requests found
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
require_once("REST.api.php");
require_once("lib/Database.class.php");
require_once("lib/Signup.class.php");
class API extends REST {
public $data = "";
private $DB_SERVER = "localhost";
private $DB_USER = "root";
private $DB_PASSWORD = "";
private $DB_NAME = "apis";
private $db = NULL;
public function __construct(){
parent::__construct(); // Init parent contructor
//read database config from ../../env.json
/*
file env.json
{
"database": "apis",
"username": "root",
"password": "",
"server": "localhost"
}
*/
$config_json = file_get_contents('../../env.json');
$config = json_decode($config_json, true);
$this->DB_SERVER = $config['server'];
$this->DB_USER = $config['username'];
$this->DB_PASSWORD = $config['password'];
$this->DB_NAME = $config['database'];
$this->dbConnect(); // Initiate Database connection
}
/*
Database connection
*/
private function dbConnect(){
if ($this->db != NULL) {
return $this->db;
} else {
$this->db = mysqli_connect($this->DB_SERVER,$this->DB_USER,$this->DB_PASSWORD, $this->DB_NAME);
if (!$this->db) {
die("Connection failed: ".mysqli_connect_error());
} else {
return $this->db;
}
}
parent::__construct(); // Init parent contructor
$this->db = Database::getConnection(); // Initiate Database connection
}
/*
......@@ -128,6 +91,20 @@
return bin2hex($bytes);
}
private function gen_hash(){
if(isset($this->_request['pass'])){
$s = new Signup("", $this->_request['pass'], "");
$hash = $s->hashPassword();
$data = [
"hash" => $hash,
"val" => $this->_request['pass'],
"verify" => password_verify($this->_request['pass'], $hash)
];
$data = $this->json($data);
$this->response($data,200);
}
}
......
<?php
<?php
class Database {
static $db;
public static function getConnection(){
$config_json = file_get_contents('../../env.json');
$config = json_decode($config_json, true);
if (Database::$db != NULL) {
return Database::$db;
} else {
Database::$db = mysqli_connect($config['server'],$config['username'],$config['password'], $config['database']);
if (!Database::$db) {
die("Connection failed: ".mysqli_connect_error());
} else {
return Database::$db;
}
}
}
}
\ No newline at end of file
<?php
require_once('Database.class.php');
class Signup {
private $username;
private $password;
private $email;
private $db;
public function __construct($username, $password, $email){
$this->db = Database::getConnection();
$this->username = $username;
$this->password = $password;
$this->email = $email;
}
public function getInsertID(){
}
public function hashPassword(){
return password_hash($this->$password, PASSWORD_BCRYPT);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment