|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Reply Class Doc Comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package OpenChat |
|
9
|
|
|
* @author Ankit Jain <[email protected]> |
|
10
|
|
|
* @license The MIT License (MIT) |
|
11
|
|
|
* @link https://github.com/ankitjain28may/openchat |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace ChatApp; |
|
14
|
|
|
require_once dirname(__DIR__).'/vendor/autoload.php'; |
|
15
|
|
|
use mysqli; |
|
16
|
|
|
use Dotenv\Dotenv; |
|
17
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
|
18
|
|
|
$dotenv->load(); |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Store Message in the Database |
|
23
|
|
|
* |
|
24
|
|
|
* @category PHP |
|
25
|
|
|
* @package OpenChat |
|
26
|
|
|
* @author Ankit Jain <[email protected]> |
|
27
|
|
|
* @license The MIT License (MIT) |
|
28
|
|
|
* @link https://github.com/ankitjain28may/openchat |
|
29
|
|
|
*/ |
|
30
|
|
|
class Reply |
|
31
|
|
|
{ |
|
32
|
|
|
/* |
|
33
|
|
|
|-------------------------------------------------------------------------- |
|
34
|
|
|
| Reply Class |
|
35
|
|
|
|-------------------------------------------------------------------------- |
|
36
|
|
|
| |
|
37
|
|
|
| Store Message in the Database |
|
38
|
|
|
| |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
protected $connect; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a new class instance. |
|
45
|
|
|
* |
|
46
|
|
|
* @return void |
|
|
|
|
|
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->connect = new mysqli( |
|
51
|
|
|
getenv('DB_HOST'), |
|
52
|
|
|
getenv('DB_USER'), |
|
53
|
|
|
getenv('DB_PASSWORD'), |
|
54
|
|
|
getenv('DB_NAME') |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
date_default_timezone_set('Asia/Kolkata'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Store Message in Db so as to send message to other members |
|
62
|
|
|
* |
|
63
|
|
|
* @param object $msg To store user id and massage |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function replyTo($msg) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!empty($msg)) { |
|
70
|
|
|
// checks for the value send |
|
71
|
|
|
$userId = $msg->userId; |
|
72
|
|
|
// stores id of the person whom message is to be sent |
|
73
|
|
|
$receiverID = $msg->name; |
|
74
|
|
|
$identifier; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if ($receiverID > $userId) { |
|
77
|
|
|
// geneate specific unique code to store messages |
|
78
|
|
|
$user1 = $userId; |
|
79
|
|
|
$user2 = $receiverID; |
|
80
|
|
|
$identifier = $userId.":".$receiverID; |
|
81
|
|
|
} else { |
|
82
|
|
|
$user1 = $receiverID; |
|
83
|
|
|
$user2 = $userId; |
|
84
|
|
|
$identifier = $receiverID.":".$userId; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// stores the message sent by the user. |
|
88
|
|
|
$reply = addslashes(trim($msg->reply)); |
|
89
|
|
|
// current time |
|
90
|
|
|
$time = date("D d M Y H:i:s"); |
|
91
|
|
|
// echo $time; |
|
92
|
|
|
// to sort the array on the basis of time |
|
93
|
|
|
$time_id = date("YmdHis"); |
|
94
|
|
|
|
|
95
|
|
|
// the sender id must not be equal to current session id |
|
96
|
|
|
if ($reply != "" && $receiverID != $userId) { |
|
97
|
|
|
// check whether the receiver is authorized or registered |
|
98
|
|
|
$query = "SELECT * from login where login_id = '$receiverID'"; |
|
99
|
|
|
|
|
100
|
|
|
$result = $this->connect->query($query); |
|
101
|
|
|
if ($result->num_rows > 0) { |
|
102
|
|
|
// check whether he is sending message |
|
103
|
|
|
// for the first time or he has sent messages before |
|
104
|
|
|
$query = "SELECT * from total_message where |
|
105
|
|
|
identifier = '$identifier'"; |
|
106
|
|
|
$result = $this->connect->query($query); |
|
107
|
|
|
if ($result->num_rows > 0) { |
|
108
|
|
|
// if he has sent messages before Update Total_Message Table |
|
109
|
|
|
$query = "UPDATE total_message SET |
|
110
|
|
|
total_messages = total_messages + 1, |
|
111
|
|
|
time = '$time', unread = 1, |
|
112
|
|
|
id = '$time_id' WHERE identifier = '$identifier'"; |
|
113
|
|
|
|
|
114
|
|
|
return $this->updateMessages( |
|
115
|
|
|
$query, $identifier, $reply, $userId, $time |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
} else { |
|
119
|
|
|
// if he sends message for the first time |
|
120
|
|
|
// insert Details in Total_Message Table |
|
121
|
|
|
$query = "INSERT into total_message values( |
|
122
|
|
|
'$identifier', 1, '$user1', '$user2', 1, |
|
123
|
|
|
'$time', '$time_id' |
|
124
|
|
|
)"; |
|
125
|
|
|
return $this->updateMessages( |
|
126
|
|
|
$query, $identifier, $reply, $userId, $time |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
// if he is unauthorized echo message is failed |
|
131
|
|
|
return "Invalid Authentication"; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
return "Failed"; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* To Store Message in DB Based on Identifier |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $query To store the query performed |
|
141
|
|
|
* @param string $identifier To store unique id |
|
142
|
|
|
* @param string $reply To store message |
|
143
|
|
|
* @param string $userId To store userid |
|
144
|
|
|
* @param string $time To store time |
|
145
|
|
|
* |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function updateMessages($query, $identifier, $reply, $userId, $time) |
|
149
|
|
|
{ |
|
150
|
|
|
if ($result = $this->connect->query($query)) { |
|
|
|
|
|
|
151
|
|
|
//insert message in db |
|
152
|
|
|
$query = "INSERT into messages values( |
|
153
|
|
|
'$identifier', '$reply', '$userId', '$time', null |
|
154
|
|
|
)"; |
|
155
|
|
|
if ($this->connect->query($query)) { |
|
156
|
|
|
// if query is executed return true |
|
157
|
|
|
return "Messages is sent\n"; |
|
158
|
|
|
} |
|
159
|
|
|
return "Message is failed\n"; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.