From 6a1130368d90a89fbb9b2f32cf595697bf0a9f8a Mon Sep 17 00:00:00 2001
From: gopikrishnan <gopikrishnan8015@gmail.com>
Date: Sat, 2 Apr 2022 21:12:16 +0000
Subject: [PATCH] updated User.class.php

---
 _templates/_head.php             |  10 ++-
 _templates/_signup.php           |  96 ++++++++++++++--------------
 index.php                        |  12 ++--
 libs/includes/Database.class.php |   8 +--
 libs/includes/User.class.php     | 104 +++++++++++++++++++++++++++++--
 libs/includes/test.php           |   5 ++
 libs/load.php                    |   2 +-
 login.php                        |  10 +--
 signup.php                       |  10 +--
 9 files changed, 176 insertions(+), 81 deletions(-)
 create mode 100644 libs/includes/test.php

diff --git a/_templates/_head.php b/_templates/_head.php
index b851178e..d2f3905b 100644
--- a/_templates/_head.php
+++ b/_templates/_head.php
@@ -7,12 +7,10 @@
     <title>Login to Photogram</title>
 
     <!-- Bootstrap core CSS -->
-    <link href="/app/assets/dist/css/bootstrap.min.css" rel="stylesheet">
+    <link href="/photogram/assets/dist/css/bootstrap.min.css" rel="stylesheet">
     <title>Photogram by LAHTP</title>
-    <?if (file_exists($_SERVER['DOCUMENT_ROOT'].'/app/css/'.basename($_SERVER['PHP_SELF'], ".php").".css")) {?>
-    <link
-        href="/app/css/<?=basename($_SERVER['PHP_SELF'], ".php")?>.css"
-        rel="stylesheet">
-    <?}?>
+    <? if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/photogram/css/' . basename($_SERVER['PHP_SELF'], ".php") . ".css")) { ?>
+        <link href="/photogram/css/<?= basename($_SERVER['PHP_SELF'], ".php") ?>.css" rel="stylesheet">
+    <? } ?>
 
 </head>
\ No newline at end of file
diff --git a/_templates/_signup.php b/_templates/_signup.php
index 94400eab..0ee4d37d 100644
--- a/_templates/_signup.php
+++ b/_templates/_signup.php
@@ -12,56 +12,52 @@ if (isset($_POST['username']) and isset($_POST['password']) and !empty($_POST['p
 ?>
 
 <?php
-    if ($signup) {
-        if (!$error) {
-            ?>
-<main class="container">
-    <div class="bg-light p-5 rounded mt-3">
-        <h1>Signup Success</h1>
-        <p class="lead">Now you can login from <a href="/login.php">here</a>.</p>
+if ($signup) {
+    if (!$error) {
+?>
+        <main class="container">
+            <div class="bg-light p-5 rounded mt-3">
+                <h1>Signup Success</h1>
+                <p class="lead">Now you can login from <a href="/photogram/login.php">here</a>.</p>
 
-    </div>
-</main>
-<?php
-        } else {
-            ?>
-<main class="container">
-    <div class="bg-light p-5 rounded mt-3">
-        <h1>Signup Fail</h1>
-        <p class="lead">Something went wrong, <?=$error?>
-        </p>
-    </div>
-</main>
-<?php
-        }
+            </div>
+        </main>
+    <?php
     } else {
-        ?>
-<main class="form-signup">
-    <form method="post" action="signup.php">
-        <img class="mb-4" src="https://git.selfmade.ninja/uploads/-/system/appearance/logo/1/Logo_Dark.png" alt=""
-            height="50">
-        <h1 class="h3 mb-3 fw-normal">Signup here</h1>
-        <div class="form-floating">
-            <input name="username" type="text" class="form-control" id="floatingInputUsername"
-                placeholder="name@example.com">
-            <label for="floatingInputUsername">Username</label>
-        </div>
-        <div class="form-floating">
-            <input name="phone" type="text" class="form-control" id="floatingInputUsername"
-                placeholder="name@example.com">
-            <label for="floatingInputUsername">Phone</label>
-        </div>
-        <div class="form-floating">
-            <input name="email_address" type="email" class="form-control" id="floatingInput"
-                placeholder="name@example.com">
-            <label for="floatingInput">Email address</label>
-        </div>
-        <div class="form-floating">
-            <input name="password" type="password" class="form-control" id="floatingPassword" placeholder="Password">
-            <label for="floatingPassword">Password</label>
-        </div>
-        <button class="w-100 btn btn-lg btn-primary hvr-grow-rotate" type="submit">Sign up</button>
-    </form>
-</main>
-<?php
+    ?>
+        <main class="container">
+            <div class="bg-light p-5 rounded mt-3">
+                <h1>Signup Fail</h1>
+                <p class="lead">Something went wrong, <?= $error ?>
+                </p>
+            </div>
+        </main>
+    <?php
     }
+} else {
+    ?>
+    <main class="form-signup">
+        <form method="post" action="signup.php">
+            <img class="mb-4" src="https://git.selfmade.ninja/uploads/-/system/appearance/logo/1/Logo_Dark.png" alt="" height="50">
+            <h1 class="h3 mb-3 fw-normal">Signup here</h1>
+            <div class="form-floating">
+                <input name="username" type="text" class="form-control" id="floatingInputUsername" placeholder="name@example.com">
+                <label for="floatingInputUsername">Username</label>
+            </div>
+            <div class="form-floating">
+                <input name="phone" type="text" class="form-control" id="floatingInputUsername" placeholder="name@example.com">
+                <label for="floatingInputUsername">Phone</label>
+            </div>
+            <div class="form-floating">
+                <input name="email_address" type="email" class="form-control" id="floatingInput" placeholder="name@example.com">
+                <label for="floatingInput">Email address</label>
+            </div>
+            <div class="form-floating">
+                <input name="password" type="password" class="form-control" id="floatingPassword" placeholder="Password">
+                <label for="floatingPassword">Password</label>
+            </div>
+            <button class="w-100 btn btn-lg btn-primary hvr-grow-rotate" type="submit">Sign up</button>
+        </form>
+    </main>
+<?php
+}
diff --git a/index.php b/index.php
index 7e218f32..fa137133 100644
--- a/index.php
+++ b/index.php
@@ -6,20 +6,20 @@ include 'libs/load.php';
 
 <!doctype html>
 <html lang="en">
-<?load_template('_head');?>
+<? load_template('_head'); ?>
 
 <body>
 
-    <?load_template('_header');?>
+    <? load_template('_header'); ?>
     <main>
 
-        <?load_template('_calltoaction');?>
+        <? load_template('_calltoaction'); ?>
 
-        <?load_template('_photogram');?>
+        <? load_template('_photogram'); ?>
 
     </main>
-    <?load_template('_footer');?>
-    <script src="/app/assets/dist/js/bootstrap.bundle.min.js"></script>
+    <? load_template('_footer'); ?>
+    <script src="/photogram/assets/dist/js/bootstrap.bundle.min.js"></script>
 
 
 </body>
diff --git a/libs/includes/Database.class.php b/libs/includes/Database.class.php
index af71ea50..5cc94d35 100644
--- a/libs/includes/Database.class.php
+++ b/libs/includes/Database.class.php
@@ -7,10 +7,10 @@ class Database
     {
         if (Database::$conn == null) {
             $servername = "mysql.selfmade.ninja";
-            $username = "sibidharan";
-            $password = "gifSaw-nycdag-6kifwa";
-            $dbname = "sibidharan_newdb";
-        
+            $username = "gopikrish";
+            $password = "krishnan";
+            $dbname = "gopikrish_photogram";
+
             // Create connection
             $connection = new mysqli($servername, $username, $password, $dbname);
             // Check connection
diff --git a/libs/includes/User.class.php b/libs/includes/User.class.php
index 65f7fa72..7c603d72 100644
--- a/libs/includes/User.class.php
+++ b/libs/includes/User.class.php
@@ -1,5 +1,7 @@
 <?php
 
+require_once "Database.class.php";
+
 class User
 {
     private $conn;
@@ -19,7 +21,7 @@ class User
             // echo "Error: " . $sql . "<br>" . $conn->error;
             $error = $conn->error;
         }
-    
+
         // $conn->close();
         return $error;
     }
@@ -44,12 +46,41 @@ 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->conn->query();
         $this->username = $username;
+        $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");
+    }
 
-        //TODO: Write the code to fetch user data from Database for the given username. If username is not present, throw Exception.
-        $this->id = null; //Update this from database.
+    //this function helps to retrieve data from the database
+    private function getData($var)
+    {
+        if (!$this->conn) {
+            $this->conn = Database::getConnection();
+        }
+        $sql = "SELECT `$var` FROM `users` WHERE `id` = '$this->id'";
+        $result = $this->conn->query($sql);
+        if ($result->num_rows) {
+            return $result->fetch_assoc()["$var"];
+        } else  return null;
+    }
+
+    //This function helps to  set the data in the database
+    private function setData($var, $data)
+    {
+        if (!$this->conn) {
+            $this->conn = Database::getConnection();
+        }
+        $sql = "UPDATE `users` SET `$var`='$data' WHERE `id`='$this->id';";
+        if ($this->conn->query($sql)) {
+            return true;
+        } else return false;
     }
 
     public function authenticate()
@@ -59,18 +90,83 @@ class User
     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)
+    {
+        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');
+    }
+
+    public function setTwitterlink($link)
+    {
+        return $this->setData('twitter', $link);
+    }
+
+    public function getTwitterlink()
+    {
+        return $this->getData('twitter');
+    }
+    public function setFacebooklink($link)
+    {
+        return $this->setData('facebook', $link);
+    }
+
+    public function getFacebooklink()
+    {
+        return $this->getData('facebook');
     }
 }
diff --git a/libs/includes/test.php b/libs/includes/test.php
new file mode 100644
index 00000000..d4784dff
--- /dev/null
+++ b/libs/includes/test.php
@@ -0,0 +1,5 @@
+<?php
+include "User.class.php";
+
+$obj = new User('gopi');
+echo $obj->getInstagramlink();
diff --git a/libs/load.php b/libs/load.php
index 7c322bf9..a4f56620 100644
--- a/libs/load.php
+++ b/libs/load.php
@@ -8,7 +8,7 @@ Session::start();
 
 function load_template($name)
 {
-    include $_SERVER['DOCUMENT_ROOT']."/app/_templates/$name.php"; //not consistant.
+    include $_SERVER['DOCUMENT_ROOT'] . "/photogram/_templates/$name.php"; //not consistant.
 }
 
 function validate_credentials($username, $password)
diff --git a/login.php b/login.php
index e9616c00..dbcdb2b3 100644
--- a/login.php
+++ b/login.php
@@ -6,18 +6,18 @@ include 'libs/load.php';
 
 <!doctype html>
 <html lang="en">
-<?load_template('_head');?>
+<? load_template('_head'); ?>
 
 <body>
 
-    <?load_template('_header');?>
+    <? load_template('_header'); ?>
     <main>
 
-        <?load_template('_login');?>
+        <? load_template('_login'); ?>
 
     </main>
-    <?load_template('_footer');?>
-    <script src="/app/assets/dist/js/bootstrap.bundle.min.js"></script>
+    <? load_template('_footer'); ?>
+    <script src="/photogram/assets/dist/js/bootstrap.bundle.min.js"></script>
 
 
 </body>
diff --git a/signup.php b/signup.php
index 8cc7d334..3f9c105a 100644
--- a/signup.php
+++ b/signup.php
@@ -6,18 +6,18 @@ include 'libs/load.php';
 
 <!doctype html>
 <html lang="en">
-<?load_template('_head');?>
+<? load_template('_head'); ?>
 
 <body>
 
-    <?load_template('_header');?>
+    <? load_template('_header'); ?>
     <main>
 
-        <?load_template('_signup');?>
+        <? load_template('_signup'); ?>
 
     </main>
-    <?load_template('_footer');?>
-    <script src="/app/assets/dist/js/bootstrap.bundle.min.js"></script>
+    <? load_template('_footer'); ?>
+    <script src="/photogram/assets/dist/js/bootstrap.bundle.min.js"></script>
 
 
 </body>
-- 
GitLab