Created
October 29, 2018 11:27
-
-
Save ikishanoza/5c9fee091224a7e4879f3892d43e88c8 to your computer and use it in GitHub Desktop.
Simple login, Signup and Image upload using PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Accept: application/json"); | |
header("Content-Type: application/json"); | |
define('LOGIN_SUCCESS_MESSAGE', 'You are successfully login'); | |
define('LOGIN_FAIL_MESSAGE', 'Invalid email or password'); | |
define('SIGNUP_SUCCESS_MESSAGE', 'Signup successfull please login with email and password'); | |
define('SIGNUP_FAIL_MESSAGE', 'Signup fails'); | |
define('MAIL_SEND_SUCCESS', 'Mail send successfully.'); | |
define('MAIL_SEND_FAIL', 'Error while sending the mail'); | |
define('DATA_SUCCESSFULLY_SUBMITTED', 'Data stored successfully.'); | |
define('FAIL_DATA_SUMBIT', 'error while submitting data.'); | |
class Web_service { | |
protected $email = ""; | |
protected $psw = ""; | |
protected $login_status = false; | |
protected $upload_status = false; | |
protected $db = ""; | |
protected $host = ""; | |
protected $database = ""; | |
protected $user = ""; | |
protected $password = ""; | |
function __construct() { | |
$this->db = $this->Connect($this->host, $this->database, $this->user, $this->password); | |
} | |
public function Connect($host, $database, $user, $password) { | |
$this->db = mysqli_connect($host, $user, $password, $database); | |
if (mysqli_connect_errno()) { | |
echo "Failed to connect to MySQL: " . mysqli_connect_error(); | |
} else { | |
return $this->db; | |
} | |
} | |
function doLogin($data) { | |
$email = htmlspecialchars(strip_tags($data->email)); | |
$psw = htmlspecialchars(strip_tags($data->password)); | |
$return_arr = array(); | |
$return_arr["results"] = array(); | |
$return_object = array(); | |
if (!empty($email) && !empty($psw)) { | |
$query = 'SELECT username FROM registration where username="' . $email . '"and password="' . $psw . '"'; | |
$result = mysqli_query($this->db, $query); | |
if (mysqli_num_rows($result) > 0) { | |
$row = mysqli_fetch_row($result); | |
$this->login_status = true; | |
$return_arr = array( | |
"status" => true, | |
"message" => LOGIN_SUCCESS_MESSAGE, | |
"username" => $row[0] | |
); | |
} else { | |
$this->login_status = false; | |
$return_arr = array( | |
"status" => false, | |
"message" => LOGIN_FAIL_MESSAGE | |
); | |
} | |
echo json_encode($return_arr); | |
} | |
} | |
function doSignup($data) { | |
$email = htmlspecialchars(strip_tags($data->email)); | |
$psw = htmlspecialchars(strip_tags($data->password)); | |
$return_arr = array(); | |
$return_arr["results"] = array(); | |
$return_object = array(); | |
if (!empty($email) && !empty($psw)) { | |
$query = 'INSERT INTO users SET email= "' . $email . '", password= "' . md5($psw) . '", status=1'; | |
$result = mysqli_query($this->db, $query); | |
if (mysqli_affected_rows($this->db) > 0) { | |
$this->login_status = true; | |
$return_arr = array( | |
"status" => true, | |
"message" => SIGNUP_SUCCESS_MESSAGE | |
); | |
} else { | |
$this->login_status = false; | |
$return_arr = array( | |
"status" => false, | |
"message" => SIGNUP_FAIL_MESSAGE | |
); | |
} | |
echo json_encode($return_arr); | |
} | |
} | |
function forgotPassword($data) { | |
$email = htmlspecialchars(strip_tags($data->email)); | |
$return_arr = array(); | |
$return_arr["results"] = array(); | |
$return_object = array(); | |
if (!empty($email)) { | |
$query = 'SELECT * from users WHERE email= "' . $email . '"'; | |
$result = mysqli_query($this->db, $query); | |
if (mysqli_num_rows($result) > 0) { | |
$new_password = $this->get_random_string(); | |
$update_query = 'UPDATE users SET password= "' . md5($new_password) . '" WHERE email= "' . $email . '"'; | |
$update_result = mysqli_query($this->db, $update_query); | |
if (mysqli_affected_rows($this->db) > 0) { | |
$message = "Username : " . $email . "\n"; | |
$message .= "Password : " . $new_password; | |
mail($email, "New Login Credentials", $message); | |
$return_arr = array( | |
"status" => true, | |
"message" => MAIL_SEND_SUCCESS | |
); | |
} | |
} else { | |
$return_arr = array( | |
"status" => false, | |
"message" => MAIL_SEND_FAIL | |
); | |
} | |
echo json_encode($return_arr); | |
} | |
} | |
function getPlants() { | |
$return_arr = array(); | |
$return_arr["results"] = array(); | |
$query = 'SELECT plantid, notes, status, hrsread from plant'; | |
$result = mysqli_query($this->db, $query); | |
if (mysqli_num_rows($result) > 0) { | |
$plants = array(); | |
while ($row = mysqli_fetch_assoc($result)) { | |
$plant = array(); | |
$plant['notes'] = $row['notes']; | |
$plant['status'] = $row['status']; | |
$plant['hrsread'] = $row['hrsread']; | |
$plants[$row['plantid']][] = $plant; | |
} | |
$return_arr = array( | |
"status" => true, | |
"message" => "success", | |
"data" => $plants | |
); | |
} else { | |
$return_arr = array( | |
"status" => false, | |
"message" => "fail", | |
"data" => array() | |
); | |
} | |
echo json_encode($return_arr); | |
} | |
function submitData($data, $files) { | |
$target_dir = "uploads"; | |
$file_name_array = array(); | |
$return_arr = array(); | |
$return_arr["results"] = array(); | |
$plant = htmlspecialchars(strip_tags($data->plant)); | |
$description = htmlspecialchars(strip_tags($data->description)); | |
$user_id = htmlspecialchars(strip_tags($data->user_id)); | |
$plant_id = htmlspecialchars(strip_tags($data->plant_id)); | |
$status = htmlspecialchars(strip_tags($data->status)); | |
$hrsread = htmlspecialchars(strip_tags($data->hrsread)); | |
if (!file_exists($target_dir)) { | |
mkdir($target_dir, 07777, true); | |
} | |
for ($i = 0; $i < count($files['tmp_name']); $i++) { | |
$file_name = $plant_id . "_" . date('YmdHi') . "_" . rand() . ".jpg"; | |
if (move_uploaded_file($files['tmp_name'][$i], $target_dir . "/" . $file_name)) { | |
$query = 'INSERT INTO `images`(`imageref`, `date`, `plantid`, `user`, `notes`) VALUES ("' . $file_name . '","' . date('Y-m-d H:i:s') . '","' . $plant_id . '","' . $user_id . '","' . $description . '")'; | |
$file_name_array[$i] = $query; | |
$result = mysqli_query($this->db, $query); | |
if (mysqli_affected_rows($this->db) > 0) { | |
$this->upload_status = $i; | |
} | |
} else { | |
} | |
} | |
$update_plant_query = 'UPDATE `plant` SET hrsread= "'.$hrsread.'",`status`="' . $status . '",`notes`="' . $description . '",`updatedby`="' . $user_id . '",`updateddate`="' . date('Y-m-d H:i:s') . '" WHERE `plantid` = "' . $plant_id . '"'; | |
$update_result = mysqli_query($this->db, $update_plant_query); | |
if (mysqli_affected_rows($this->db) > 0) { | |
$return_arr = array( | |
"status" => true, | |
"message" => DATA_SUCCESSFULLY_SUBMITTED, | |
"upload_status" => $this->upload_status == (count($files['tmp_name']) - 1) ? true : false, | |
"update_status" => true | |
); | |
} else { | |
$return_arr = array( | |
"status" => true, | |
"message" => DATA_SUCCESSFULLY_SUBMITTED, | |
"upload_status" => $this->upload_status == (count($files['tmp_name']) - 1) ? true : false, | |
"update_status" => false | |
); | |
} | |
echo json_encode($return_arr); | |
} | |
function get_random_string($length = 8) { | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$string = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$string .= $characters[mt_rand(0, strlen($characters) - 1)]; | |
} | |
return $string; | |
} | |
} | |
$ws = new Web_service(); | |
$data = json_decode($_POST['formData']); | |
$method = $data[0]->method; | |
switch ($method) { | |
case 'login': | |
$ws->doLogin($data[0]); | |
break; | |
case 'signup': | |
$ws->doSignup($data[0]); | |
break; | |
case 'forgotpass': | |
$ws->forgotPassword($data[0]); | |
break; | |
case 'getPlants': | |
$ws->getPlants(); | |
break; | |
case 'submitData': | |
$files = $_FILES['image']; | |
$ws->submitData($data[0], $files); | |
break; | |
case 'syncData': | |
$ws->syncData($data); | |
break; | |
default: | |
break; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment