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

magics

parent 4e944d87
No related branches found
No related tags found
No related merge requests found
-- 2 Apr, 2022 Migrations DROP TABLE IF EXISTS `auth`;
ALTER TABLE `auth` ADD `sec_email` varchar(256) NULL; CREATE TABLE `auth` (
\ No newline at end of file `id` int NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`password` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`phone` varchar(16) NOT NULL,
`active` int NOT NULL DEFAULT '1',
`blocked` int NOT NULL DEFAULT '0',
`sec_email` varchar(256) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phone` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int NOT NULL,
`bio` longtext NOT NULL,
`avatar` varchar(1024) NOT NULL,
`firstname` text NOT NULL,
`lastname` text NOT NULL,
`dob` date DEFAULT NULL,
`instagram` varchar(1024) DEFAULT NULL,
`twitter` varchar(1024) DEFAULT NULL,
`facebook` varchar(1024) DEFAULT NULL,
KEY `id` (`id`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`id`) REFERENCES `auth` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
\ No newline at end of file
...@@ -6,10 +6,10 @@ class Database ...@@ -6,10 +6,10 @@ class Database
public static function getConnection() public static function getConnection()
{ {
if (Database::$conn == null) { if (Database::$conn == null) {
$servername = "mysql.selfmade.ninja"; $servername = get_config('db_server');
$username = "gopikrish"; $username = get_config('db_username');
$password = "krishnan"; $password = get_config('db_password');
$dbname = "gopikrish_photogram"; $dbname = get_config('db_name');
// Create connection // Create connection
$connection = new mysqli($servername, $username, $password, $dbname); $connection = new mysqli($servername, $username, $password, $dbname);
......
...@@ -18,6 +18,14 @@ class Mic ...@@ -18,6 +18,14 @@ class Mic
public $price; public $price;
public static $test; public static $test;
public function __call($name, $arguments)
{
print("\nCalling: $name\n");
print_r($arguments);
print("\n");
return "Hello-return";
}
public static function testFunction() public static function testFunction()
{ {
printf("This is a static function, this can be run without object initialization. "); printf("This is a static function, this can be run without object initialization. ");
......
...@@ -5,6 +5,18 @@ require_once "Database.class.php"; ...@@ -5,6 +5,18 @@ require_once "Database.class.php";
class User class User
{ {
private $conn; private $conn;
public function __call($name, $arguments)
{
$property = preg_replace("/[^0-9a-zA-Z]/", "", substr($name, 3));
$property = strtolower(preg_replace('/\B([A-Z])/', '_$1', $property));
if (substr($name, 0, 3) == "get") {
return $this->_get_data($property);
} elseif (substr($name, 0, 3) == "set") {
return $this->_set_data($property, $arguments[0]);
}
}
public static function signup($user, $pass, $email, $phone) public static function signup($user, $pass, $email, $phone)
{ {
$options = [ $options = [
...@@ -35,7 +47,7 @@ class User ...@@ -35,7 +47,7 @@ class User
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
//if ($row['password'] == $pass) { //if ($row['password'] == $pass) {
if (password_verify($pass, $row['password'])) { if (password_verify($pass, $row['password'])) {
return $row; return $row['username'];
} else { } else {
return false; return false;
} }
...@@ -47,126 +59,143 @@ class User ...@@ -47,126 +59,143 @@ class User
public function __construct($username) public function __construct($username)
{ {
//TODO: Write the code to fetch user data from Database for the given username. If username is not present, throw Exception. //TODO: Write the code to fetch user data from Database for the given username. If username is not present, throw Exception.
$this->conn = Database::getConnection(); $this->conn = Database::getConnection();
$this->username = $username; $this->username = $username;
$this->id = null;
$sql = "SELECT `id` FROM `auth` WHERE `username`= '$username' LIMIT 1"; $sql = "SELECT `id` FROM `auth` WHERE `username`= '$username' LIMIT 1";
$result = $this->conn->query($sql); $result = $this->conn->query($sql);
if ($result->num_rows) { if ($result->num_rows) {
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
$this->id = $row['id']; //Updating this from database $this->id = $row['id']; //Updating this from database
} else throw new Exception("Username does't exist"); } else {
throw new Exception("Username does't exist");
}
} }
//this function helps to retrieve data from the database //this function helps to retrieve data from the database
private function getData($var) private function _get_data($var)
{ {
if (!$this->conn) { if (!$this->conn) {
$this->conn = Database::getConnection(); $this->conn = Database::getConnection();
} }
$sql = "SELECT `$var` FROM `users` WHERE `id` = '$this->id'"; $sql = "SELECT `$var` FROM `users` WHERE `id` = $this->id";
//print($sql);
$result = $this->conn->query($sql); $result = $this->conn->query($sql);
if ($result->num_rows) { if ($result and $result->num_rows == 1) {
//print("Res: ".$result->fetch_assoc()["$var"]);
return $result->fetch_assoc()["$var"]; return $result->fetch_assoc()["$var"];
} else return null; } else {
return null;
}
} }
//This function helps to set the data in the database //This function helps to set the data in the database
private function setData($var, $data) private function _set_data($var, $data)
{ {
if (!$this->conn) { if (!$this->conn) {
$this->conn = Database::getConnection(); $this->conn = Database::getConnection();
} }
$sql = "UPDATE `users` SET `$var`='$data' WHERE `id`='$this->id';"; $sql = "UPDATE `users` SET `$var`='$data' WHERE `id`=$this->id;";
if ($this->conn->query($sql)) { if ($this->conn->query($sql)) {
return true; return true;
} else return false; } else {
} return false;
}
public function authenticate()
{
}
public function setBio($bio)
{
//TODO: Write UPDATE command to change new bio
return $this->setData('bio', $bio);
}
public function getBio()
{
//TODO: Write SELECT command to get the bio.
return $this->getData('bio');
}
public function setAvatar($link)
{
return $this->setData('avatar', $link);
}
public function getAvatar()
{
return $this->getData('avatar');
}
public function setFirstname($name)
{
return $this->setData("firstname", $name);
}
public function getFirstname()
{
return $this->getData('firstname');
}
public function setLastname($name)
{
return $this->setData("lastname", $name);
}
public function getLastname()
{
return $this->getData('lastname');
} }
public function setDob($year, $month, $day) public function setDob($year, $month, $day)
{ {
if (checkdate($month, $day, $year)) { //checking data is valid if (checkdate($month, $day, $year)) { //checking data is valid
return $this->setData('dob', "$year.$month.$day"); return $this->_set_data('dob', "$year.$month.$day");
} else return false; } else {
} return false;
}
public function getDob()
{
return $this->getData('dob');
}
public function setInstagramlink($link)
{
return $this->setData('instagram', $link);
}
public function getInstagramlink()
{
return $this->getData('instagram');
} }
public function setTwitterlink($link) public function getUsername()
{ {
return $this->setData('twitter', $link); return $this->username;
} }
public function getTwitterlink() public function authenticate()
{
return $this->getData('twitter');
}
public function setFacebooklink($link)
{ {
return $this->setData('facebook', $link);
} }
public function getFacebooklink() // public function setBio($bio)
{ // {
return $this->getData('facebook'); // //TODO: Write UPDATE command to change new bio
} // return $this->_set_data('bio', $bio);
// }
// public function getBio()
// {
// //TODO: Write SELECT command to get the bio.
// return $this->_get_data('bio');
// }
// public function setAvatar($link)
// {
// return $this->_set_data('avatar', $link);
// }
// public function getAvatar()
// {
// return $this->_get_data('avatar');
// }
// public function setFirstname($name)
// {
// return $this->_set_data("firstname", $name);
// }
// public function getFirstname()
// {
// return $this->_get_data('firstname');
// }
// public function setLastname($name)
// {
// return $this->_set_data("lastname", $name);
// }
// public function getLastname()
// {
// return $this->_get_data('lastname');
// }
// public function getDob()
// {
// return $this->_get_data('dob');
// }
// public function setInstagramlink($link)
// {
// return $this->_set_data('instagram', $link);
// }
// public function getInstagramlink()
// {
// return $this->_get_data('instagram');
// }
// public function setTwitterlink($link)
// {
// return $this->_set_data('twitter', $link);
// }
// public function getTwitterlink()
// {
// return $this->_get_data('twitter');
// }
// public function setFacebooklink($link)
// {
// return $this->_set_data('facebook', $link);
// }
// public function getFacebooklink()
// {
// return $this->_get_data('facebook');
// }
} }
<?php
include "User.class.php";
$obj = new User('gopi');
echo $obj->getInstagramlink();
...@@ -4,8 +4,23 @@ include_once 'includes/Mic.class.php'; ...@@ -4,8 +4,23 @@ include_once 'includes/Mic.class.php';
include_once 'includes/User.class.php'; include_once 'includes/User.class.php';
include_once 'includes/Database.class.php'; include_once 'includes/Database.class.php';
global $__site_config;
//Note: Change this path if you run this code outside lab.
$__site_config = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/../photogramconfig.json');
Session::start(); Session::start();
function get_config($key, $default=null)
{
global $__site_config;
$array = json_decode($__site_config, true);
if (isset($array[$key])) {
return $array[$key];
} else {
return $default;
}
}
function load_template($name) function load_template($name)
{ {
include $_SERVER['DOCUMENT_ROOT'] . "/photogram/_templates/$name.php"; //not consistant. include $_SERVER['DOCUMENT_ROOT'] . "/photogram/_templates/$name.php"; //not consistant.
......
<?php <?php
include 'libs/load.php'; include 'libs/load.php';
$user = "sibidharan"; $user = "fooboo1";
$pass = isset($_GET['pass']) ? $_GET['pass'] : ''; $pass = isset($_GET['pass']) ? $_GET['pass'] : '';
$result = null; $result = null;
...@@ -11,20 +11,26 @@ if (isset($_GET['logout'])) { ...@@ -11,20 +11,26 @@ if (isset($_GET['logout'])) {
} }
if (Session::get('is_loggedin')) { if (Session::get('is_loggedin')) {
$userdata = Session::get('session_user'); $username = Session::get('session_username');
print("Welcome Back, $userdata[username]"); $userobj = new User($username);
$result = $userdata; print("Welcome Back ".$userobj->getFirstname());
print("<br>".$userobj->getBio());
$userobj->setBio("Making new things...");
print("<br>".$userobj->getBio());
} else { } else {
printf("No session found, trying to login now. <br>"); printf("No session found, trying to login now. <br>");
$result = User::login($user, $pass); $result = User::login($user, $pass);
if ($result) { if ($result) {
echo "Login Success, $result[username]"; $userobj = new User($user);
echo "Login Success ", $userobj->getUsername();
Session::set('is_loggedin', true); Session::set('is_loggedin', true);
Session::set('session_user', $result); Session::set('session_username', $result);
} else { } else {
echo "Login failed, $user <br>"; echo "Login failed, $user <br>";
} }
} }
echo <<<EOL echo <<<EOL
<br><br><a href="logintest.php?logout">Logout</a> <br><br><a href="logintest.php?logout">Logout</a>
EOL; EOL;
...@@ -36,20 +36,14 @@ $mic1->setModel("hyper quad cast"); ...@@ -36,20 +36,14 @@ $mic1->setModel("hyper quad cast");
print("Model of 1st mic is ".$mic1->getModelProxy()); print("Model of 1st mic is ".$mic1->getModelProxy());
print("\n".$mic1->getBrand()); print("\n".$mic1->getBrand());
print("\n".$mic2->getBrand()); print("\n".$mic2->getBrand());
echo $mic1->getVoltage("hello", array(1,2,3,4,5), new Mic("Bose"));
echo $mic1->setUpVoltage("hi", new Mic("cast"), [1,2,3]);
print("\n".$mic->price); print("\n".$mic->price);
print("Value of 10+12 is ".$mic1->add(10, 12)); print("\nValue of 10+12 is ".$mic1->add(10, 12));
print("This is mono font inside pre tag \n"); print("\nThis is mono font inside pre tag \n");
$conn = Database::getConnection();
$conn = Database::getConnection();
$conn = Database::getConnection();
$conn = Database::getConnection();
$conn = Database::getConnection();
$conn = Database::getConnection();
$conn = Database::getConnection();
?> ?>
</pre> </pre>
This is regular font. \ No newline at end of file
\ 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