Advertisement
Guest User

admin.php

a guest
Apr 10th, 2020
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 KB | None | 0 0
  1. <?php
  2.  
  3. class Admins{
  4.     private $host = "localhost";
  5.     private $database = "xover";
  6.     private $username = "root";
  7.     private $password = "";
  8.     private $conn;
  9.  
  10.     public function __construct() {
  11.         $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->database, $this->username, $this->password);
  12.         $this->conn->exec("SET NAMES 'utf8';");
  13.     }
  14.  
  15.     public function checkAdminLogin($array) {
  16.         session_start();
  17.         $args = array("username" => $array["username"]);
  18.         $sql = "SELECT * FROM user WHERE username = :username";
  19.         $result = $this->conn->prepare($sql);
  20.         $result->execute($args);
  21.  
  22.         if ($result->rowCount() > 0){
  23.             $row = $result->fetchObject();
  24.             // session_start();
  25.             $_SESSION["username"] = $row->username;
  26.             $_SESSION["admin_login"] = true;
  27.             // session_write_close();
  28.  
  29.             $result_data["valid"] = true;
  30.             $result_data["msg"] = "Successfully login";
  31.         }
  32.         else{
  33.             $result_data["valid"] = false;
  34.             $result_data["msg"] = "You have entered wrong username or password. Please try again";
  35.         }
  36.  
  37.         return json_encode($result_data);
  38.     }
  39.  
  40.     public function addArticle($array, $files){
  41.         $random_string = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 5, 8);
  42.         //get extension
  43.         $extImage = pathinfo($_FILES['featured_image']['name'][0],PATHINFO_EXTENSION);
  44.  
  45.         //rename image
  46.         $filename = $random_string.".".$extImage;
  47.         $date = date("Y-m-d");
  48.  
  49.         $args = array("featured_image" => $filename, "title" => $array["title"], "description" => $array["description"], "content" => $array["content"], "category" => $array["category"], "status" => $array["status"], "paid" => $array["paid"], "price" => $array["price"], "date" => $date);
  50.         $sql = "INSERT INTO articles SET featured_image = :featured_image, title = :title, description = :description, content = :content, category = :category, status = :status, paid = :paid, price = :price, createdAt = :date";
  51.         $result = $this->conn->prepare($sql);
  52.         $result->execute($args);
  53.  
  54.         $imagePath = "images/featured_image_articles/".$fileName;
  55.         move_uploaded_file($_FILES['featured_image']['tmp_name'][0], $imagePath);
  56.  
  57.         if($result){
  58.             $result_data["valid"] = true;
  59.             $result_data["msg"] = "Successfully uploaded new article";
  60.         }
  61.         else{
  62.             $result_data["valid"] = false;
  63.             $result_data["msg"] = "Failed to upload article. Please try again";
  64.         }
  65.         print_r($result);
  66.  
  67.         return json_encode($result_data);
  68.     }
  69.    
  70. }
  71.  
  72. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement