You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
4 years ago
|
<?php
|
||
|
class Connection {
|
||
|
private $charset= 'utf8mb4';
|
||
|
protected function connect (){
|
||
|
include "../../creds.php";
|
||
|
$dsn= "mysql:host=$sqlserver;charset=$this->charset";
|
||
|
try {
|
||
|
$pdo= new pdo ($dsn,$sqluser,$sqlpass);
|
||
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||
|
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false );
|
||
|
$pdo->setattribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
||
|
$pdo->exec('USE '.$dbname.'_chat');
|
||
|
return $pdo;
|
||
|
}
|
||
|
catch (Exception $e) {
|
||
|
echo 'Error: '.$e->getMessage();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
class Model extends Connection {
|
||
|
public function getUsers($id){
|
||
|
//$sql="SELECT * FROM users WHERE user_id != ?";
|
||
|
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://".$_SERVER['HTTP_HOST']."/paredu";
|
||
|
$pdo = $this->connect();
|
||
|
$pdo->exec('USE paredu');
|
||
|
$sql = "SELECT * FROM `auth` WHERE `id` != ?";
|
||
|
$stmt=$pdo->prepare($sql);
|
||
|
$stmt->execute([$id]);
|
||
|
//$result=$stmt->fetchAll();
|
||
|
$result = array();
|
||
|
$fetch = $stmt->fetchAll();
|
||
|
for($i = 0; $i<count($fetch); $i++){
|
||
|
$sndarray = array();
|
||
|
$sndarray["id"] = $fetch[$i]["id"];
|
||
|
if($fetch[$i]["fullname"] != 0){
|
||
|
$sndarray["name"] = $fetch[$i]["fullname"];
|
||
|
}else{
|
||
|
$sndarray["name"] = $fetch[$i]["username"];
|
||
|
}
|
||
|
$sndarray["userimg"] = $url."/API/request.php?type=image&subtype=user&id=".$fetch[$i]["id"];
|
||
|
$result[$i] = $sndarray;
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function insertChat($id,$user2_id, $message, $time){
|
||
|
$sql="INSERT INTO chats (chat_user_id, chat_user2_id, chat_message, chat_sent_by, chat_date) VALUES(?,?,?,?,?)";
|
||
|
$stmt=$this->connect()->prepare($sql);
|
||
|
$stmt->execute([$id,$user2_id, $message, $id, $time]);
|
||
|
$result= $stmt->fetchAll();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function thisUser($user_name, $pass){
|
||
|
$sql="SELECT * From users WHERE user_name =? AND users_pass= ?";
|
||
|
$stmt= $this->connect()->prepare($sql);
|
||
|
$stmt->execute([$user_name, $pass]);
|
||
|
$result=$stmt->fetchAll();
|
||
|
return $result;
|
||
|
}
|
||
|
|
||
|
public function getChats($id1, $id2){
|
||
|
$sql="SELECT * FROM chats WHERE chat_user_id = ? and chat_user2_id=? OR chat_user_id=? and chat_user2_id=?";
|
||
|
$stmt=$this->connect()->prepare($sql);
|
||
|
$stmt->execute([$id1, $id2, $id2, $id1]);
|
||
|
$result=$stmt->fetchAll();
|
||
|
return $result;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
?>
|