1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ChatApp; |
4
|
|
|
require_once (dirname(__DIR__).'/vendor/autoload.php'); |
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
7
|
|
|
$dotenv->load(); |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Store Message in the Database |
12
|
|
|
*/ |
13
|
|
|
class Reply |
14
|
|
|
{ |
15
|
|
|
protected $connect; |
16
|
|
|
|
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
$this->connect = mysqli_connect( |
20
|
|
|
getenv('DB_HOST'), |
21
|
|
|
getenv('DB_USER'), |
22
|
|
|
getenv('DB_PASSWORD'), |
23
|
|
|
getenv('DB_NAME') |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function replyTo($msg) |
28
|
|
|
{ |
29
|
|
|
if (!empty($msg)) { |
30
|
|
|
//checks for the value send |
31
|
|
|
$userId = $msg->userId; |
32
|
|
|
$receiverID = $msg->name; //stores id of the person whom message is to be sent |
33
|
|
|
$identifier = ""; |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ($receiverID > $userId) { |
36
|
|
|
// geneate specific unique code to store messages |
37
|
|
|
$user1 = $userId; |
38
|
|
|
$user2 = $receiverID; |
39
|
|
|
$identifier = $userId . ":" . $receiverID; |
40
|
|
|
} else { |
41
|
|
|
$user1 = $receiverID; |
42
|
|
|
$user2 = $userId; |
43
|
|
|
$identifier = $receiverID . ":" . $userId; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$reply = addslashes(trim($msg->reply)); // stores the message sent by the user. |
47
|
|
|
|
48
|
|
|
$time = date("D d M Y H:i:s", time() + 16200); // current time |
49
|
|
|
$time_id = date("YmdHis", time() + 16200); //to sort the array on the basis of time |
50
|
|
|
|
51
|
|
|
//the sender id must not be equal to current session id |
52
|
|
|
if ($reply != "" && $receiverID != $userId) { |
53
|
|
|
// check whether the receiver is authorized or registered |
54
|
|
|
$query = "SELECT * from login where login_id = '$receiverID'"; |
55
|
|
|
|
56
|
|
|
$result = $this->connect->query($query); |
57
|
|
|
if ($result->num_rows > 0) { |
58
|
|
|
//check whether he is sending message for thr first time or he has sent messages before |
59
|
|
|
$query = "SELECT * from total_message where identifier = '$identifier'"; |
60
|
|
|
$result = $this->connect->query($query); |
61
|
|
|
if ($result->num_rows > 0) { |
62
|
|
|
// if he has sent messages before Update Total_Message Table |
63
|
|
|
$query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'"; |
64
|
|
|
return $this->updateMessages($query, $identifier, $reply, $userId, $time); |
65
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
// if he sends message for the first time insert Details in Total_Message Table |
68
|
|
|
$query = "INSERT into total_message values('$identifier', 1, '$user1', '$user2', 1, '$time', '$time_id')"; |
69
|
|
|
return $this->updateMessages($query, $identifier, $reply, $userId, $time); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
return "Invalid Authentication"; // if he is unauthorized echo message is failed |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return "Failed"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function updateMessages($query, $identifier, $reply, $userId, $time) |
79
|
|
|
{ |
80
|
|
|
if ($result = $this->connect->query($query)) { |
|
|
|
|
81
|
|
|
//insert message in db |
82
|
|
|
$query = "INSERT into messages values('$identifier', '$reply', '$userId', '$time', null)"; |
83
|
|
|
if ($this->connect->query($query)) { |
84
|
|
|
return "Messages is sent"; // if query is executed return true |
85
|
|
|
} |
86
|
|
|
return "Message is failed"; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.