diff --git a/__migrations.sql b/__migrations.sql index a3944bfa41a0822753dec15e0ecaaa9a392da366..d0b27acf6301bf01ad5ee09649665dbe2299ec74 100644 --- a/__migrations.sql +++ b/__migrations.sql @@ -1,2 +1,31 @@ --- 2 Apr, 2022 Migrations -ALTER TABLE `auth` ADD `sec_email` varchar(256) NULL; \ No newline at end of file +DROP TABLE IF EXISTS `auth`; +CREATE TABLE `auth` ( + `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 diff --git a/libs/includes/Database.class.php b/libs/includes/Database.class.php index 5cc94d35f8c4bcfb54699758118b8b7add584f19..bdf354dbfc018cc2a9842fc6988cb955eee1eff8 100644 --- a/libs/includes/Database.class.php +++ b/libs/includes/Database.class.php @@ -6,10 +6,10 @@ class Database public static function getConnection() { if (Database::$conn == null) { - $servername = "mysql.selfmade.ninja"; - $username = "gopikrish"; - $password = "krishnan"; - $dbname = "gopikrish_photogram"; + $servername = get_config('db_server'); + $username = get_config('db_username'); + $password = get_config('db_password'); + $dbname = get_config('db_name'); // Create connection $connection = new mysqli($servername, $username, $password, $dbname); diff --git a/libs/includes/Mic.class.php b/libs/includes/Mic.class.php index 2c62fe3a74bdf6372c1decca67b3a6d6b1fd0ac5..8eabc1bcf36290098107ace9acaa085bd0db4346 100644 --- a/libs/includes/Mic.class.php +++ b/libs/includes/Mic.class.php @@ -18,6 +18,14 @@ class Mic public $price; public static $test; + public function __call($name, $arguments) + { + print("\nCalling: $name\n"); + print_r($arguments); + print("\n"); + return "Hello-return"; + } + public static function testFunction() { printf("This is a static function, this can be run without object initialization. "); diff --git a/libs/includes/User.class.php b/libs/includes/User.class.php index 7c603d72ac6f8423d73aa121e06da9dbf094283b..8434fbf60d2060e3f408439df2ab72182a1b7f73 100644 --- a/libs/includes/User.class.php +++ b/libs/includes/User.class.php @@ -5,6 +5,18 @@ require_once "Database.class.php"; class User { 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) { $options = [ @@ -35,7 +47,7 @@ class User $row = $result->fetch_assoc(); //if ($row['password'] == $pass) { if (password_verify($pass, $row['password'])) { - return $row; + return $row['username']; } else { return false; } @@ -47,126 +59,143 @@ class User 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. - $this->conn = Database::getConnection(); $this->username = $username; + $this->id = null; $sql = "SELECT `id` FROM `auth` WHERE `username`= '$username' LIMIT 1"; $result = $this->conn->query($sql); if ($result->num_rows) { $row = $result->fetch_assoc(); $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 - private function getData($var) + private function _get_data($var) { if (!$this->conn) { $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); - if ($result->num_rows) { + if ($result and $result->num_rows == 1) { + //print("Res: ".$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 - private function setData($var, $data) + private function _set_data($var, $data) { if (!$this->conn) { $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)) { return true; - } 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'); + } else { + return false; + } } public function setDob($year, $month, $day) { if (checkdate($month, $day, $year)) { //checking data is valid - return $this->setData('dob', "$year.$month.$day"); - } 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'); + return $this->_set_data('dob', "$year.$month.$day"); + } else { + return false; + } } - public function setTwitterlink($link) + public function getUsername() { - return $this->setData('twitter', $link); + return $this->username; } - public function getTwitterlink() - { - return $this->getData('twitter'); - } - public function setFacebooklink($link) + public function authenticate() { - return $this->setData('facebook', $link); } - public function getFacebooklink() - { - return $this->getData('facebook'); - } + // public function setBio($bio) + // { + // //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'); + // } } diff --git a/libs/includes/test.php b/libs/includes/test.php deleted file mode 100644 index d4784dff31b1fc420d1c9f3c9cb6c8f7ea058124..0000000000000000000000000000000000000000 --- a/libs/includes/test.php +++ /dev/null @@ -1,5 +0,0 @@ -<?php -include "User.class.php"; - -$obj = new User('gopi'); -echo $obj->getInstagramlink(); diff --git a/libs/load.php b/libs/load.php index a4f566206f25dbcb13becb17b248bc4dcf211058..99d2effd28f8452c581f5d965d7b1fc7b68c6932 100644 --- a/libs/load.php +++ b/libs/load.php @@ -4,8 +4,23 @@ include_once 'includes/Mic.class.php'; include_once 'includes/User.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(); +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) { include $_SERVER['DOCUMENT_ROOT'] . "/photogram/_templates/$name.php"; //not consistant. diff --git a/logintest.php b/logintest.php index 0afdfe0b168d7ed813a174f15822e22c6b8eca1b..c0b442c9848b1dc450e6f99c2c54b82032b41a05 100644 --- a/logintest.php +++ b/logintest.php @@ -1,7 +1,7 @@ <?php include 'libs/load.php'; -$user = "sibidharan"; +$user = "fooboo1"; $pass = isset($_GET['pass']) ? $_GET['pass'] : ''; $result = null; @@ -11,20 +11,26 @@ if (isset($_GET['logout'])) { } if (Session::get('is_loggedin')) { - $userdata = Session::get('session_user'); - print("Welcome Back, $userdata[username]"); - $result = $userdata; + $username = Session::get('session_username'); + $userobj = new User($username); + print("Welcome Back ".$userobj->getFirstname()); + print("<br>".$userobj->getBio()); + $userobj->setBio("Making new things..."); + print("<br>".$userobj->getBio()); } else { printf("No session found, trying to login now. <br>"); $result = User::login($user, $pass); + if ($result) { - echo "Login Success, $result[username]"; + $userobj = new User($user); + echo "Login Success ", $userobj->getUsername(); Session::set('is_loggedin', true); - Session::set('session_user', $result); + Session::set('session_username', $result); } else { echo "Login failed, $user <br>"; } } + echo <<<EOL <br><br><a href="logintest.php?logout">Logout</a> EOL; diff --git a/test.php b/test.php index 4bc873eb0149a288e9a1001ccdaa728a0dc10a51..cbe4d3a6e8bc58d361e40750167d2650370b5b46 100644 --- a/test.php +++ b/test.php @@ -36,20 +36,14 @@ $mic1->setModel("hyper quad cast"); print("Model of 1st mic is ".$mic1->getModelProxy()); print("\n".$mic1->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("Value of 10+12 is ".$mic1->add(10, 12)); -print("This is mono font inside pre tag \n"); +print("\nValue of 10+12 is ".$mic1->add(10, 12)); +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> -This is regular font. \ No newline at end of file +</pre> \ No newline at end of file