1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace ChatApp; |
4
|
|
|
require_once (dirname(__DIR__) . '/vendor/autoload.php'); |
5
|
|
|
use ChatApp\Session; |
6
|
|
|
use ChatApp\Time; |
7
|
|
|
use ChatApp\User; |
8
|
|
|
use Dotenv\Dotenv; |
9
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
10
|
|
|
$dotenv->load(); |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class Conversation |
17
|
|
|
{ |
18
|
|
|
protected $connect; |
19
|
|
|
protected $array; |
20
|
|
|
protected $obTime; |
21
|
|
|
protected $obUser; |
22
|
|
|
|
23
|
|
View Code Duplication |
public function __construct($sessionId) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
session_id($sessionId); |
26
|
|
|
@session_start(); |
|
|
|
|
27
|
|
|
$this->connect = mysqli_connect( |
28
|
|
|
getenv('DB_HOST'), |
29
|
|
|
getenv('DB_USER'), |
30
|
|
|
getenv('DB_PASSWORD'), |
31
|
|
|
getenv('DB_NAME') |
32
|
|
|
); |
33
|
|
|
session_write_close(); |
34
|
|
|
$this->obTime = new Time(); |
35
|
|
|
$this->obUser = new User(); |
36
|
|
|
$this->array = array(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function conversationLoad($msg, $para) |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
$flag = 1; |
|
|
|
|
43
|
|
|
if(Session::get('start') != null && isset($msg)) |
44
|
|
|
{ |
45
|
|
|
$add_load = 0; |
46
|
|
|
$userId = Session::get('start'); |
47
|
|
|
$msg = json_decode($msg); |
48
|
|
|
$username = $msg->username; |
49
|
|
|
$load = $msg->load; |
50
|
|
|
|
51
|
|
|
$fetch = $this->obUser->userDetails($username, $para); |
52
|
|
|
|
53
|
|
|
if($fetch != NULL) |
54
|
|
|
{ |
55
|
|
|
$login_id = (int)$fetch['login_id']; |
56
|
|
|
|
57
|
|
|
// Unique Identifier |
58
|
|
View Code Duplication |
if($login_id > $userId) |
|
|
|
|
59
|
|
|
$identifier = $userId.':'.$login_id; |
60
|
|
|
else |
61
|
|
|
$identifier = $login_id.':'.$userId; |
62
|
|
|
|
63
|
|
|
$query = "SELECT total_messages from total_message where identifier = '$identifier'"; |
64
|
|
|
if($result = $this->connect->query($query)) |
65
|
|
|
{ |
66
|
|
|
if($result->num_rows > 0) |
67
|
|
|
{ |
68
|
|
|
$total = $result->fetch_assoc(); |
69
|
|
|
$total = $total['total_messages']; |
70
|
|
|
if($total - $load > 0) |
71
|
|
|
if($total - $load > 10) |
72
|
|
|
$add_load = $load + 10; |
73
|
|
|
else |
74
|
|
|
$add_load = $total; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$query = "SELECT message, time, sent_by FROM messages WHERE identifier_message_number = '$identifier' ORDER BY id DESC limit ".$load; |
79
|
|
|
if($result = $this->connect->query($query)) |
80
|
|
|
{ |
81
|
|
|
if($result->num_rows > 0) |
82
|
|
|
{ |
83
|
|
|
while($row = $result->fetch_assoc()) |
84
|
|
|
{ |
85
|
|
|
$row['time'] = $this->obTime->timeConversion($row['time']); |
86
|
|
|
$row = array_merge($row,['start' => $userId]); |
87
|
|
|
$this->array = array_merge($this->array, [$row]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->array = array_merge([['name' => $fetch['name'], 'username' => $fetch['username'], 'id' => $fetch['login_id'], 'load' => $add_load, 'login_status' => $fetch['login_status'], 'type' => 1]], $this->array); |
91
|
|
|
return json_encode($this->array); |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
return json_encode([['name' => $fetch['name'], 'username' => $fetch['username'], 'id' => $fetch['login_id'], 'login_status' => $fetch['login_status'], 'type' => 0]]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
else |
99
|
|
|
{ |
100
|
|
|
echo "Query Failed"; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
else |
104
|
|
|
{ |
105
|
|
|
echo "Query Failed"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
else |
109
|
|
|
{ |
110
|
|
|
header('Location:../login.php'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->connect->close(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.