@@ -1,7 +1,7 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ChatApp; |
| 4 | -require_once (dirname(__DIR__) . '/vendor/autoload.php'); |
|
| 4 | +require_once (dirname(__DIR__).'/vendor/autoload.php'); |
|
| 5 | 5 | use ChatApp\Session; |
| 6 | 6 | use Dotenv\Dotenv; |
| 7 | 7 | $dotenv = new Dotenv(dirname(__DIR__)); |
@@ -30,37 +30,37 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | public function replyTo($msg) |
| 32 | 32 | { |
| 33 | - $msg = json_decode($msg); //decode json value |
|
| 34 | - if(Session::get('start') != null && !empty($msg)) //checks for session login and the value send |
|
| 33 | + $msg = json_decode($msg); //decode json value |
|
| 34 | + if (Session::get('start') != null && !empty($msg)) //checks for session login and the value send |
|
| 35 | 35 | { |
| 36 | 36 | $userId = Session::get('start'); |
| 37 | 37 | $identifier = $msg->name; |
| 38 | 38 | |
| 39 | - $receiverID = $identifier; //stores id of the person whom message is to be sent |
|
| 39 | + $receiverID = $identifier; //stores id of the person whom message is to be sent |
|
| 40 | 40 | |
| 41 | - if($identifier > $userId) // geneate specific unique code to store messages |
|
| 42 | - $identifier = $userId . ":" . $identifier; |
|
| 41 | + if ($identifier > $userId) // geneate specific unique code to store messages |
|
| 42 | + $identifier = $userId.":".$identifier; |
|
| 43 | 43 | else |
| 44 | - $identifier = $identifier . ":" . $userId; |
|
| 44 | + $identifier = $identifier.":".$userId; |
|
| 45 | 45 | |
| 46 | 46 | $reply = addslashes(trim($msg->reply[0])); // stores the message sent by the user. |
| 47 | 47 | |
| 48 | - $time = date("D d M Y H:i:s", time() + 16200); // current time |
|
| 48 | + $time = date("D d M Y H:i:s", time() + 16200); // current time |
|
| 49 | 49 | $time_id = date("YmdHis", time() + 16200); //to sort the array on the basis of time |
| 50 | 50 | |
| 51 | 51 | //the sender id must not be equal to current session id |
| 52 | - if($reply != "" && $receiverID != $userId) |
|
| 52 | + if ($reply != "" && $receiverID != $userId) |
|
| 53 | 53 | { |
| 54 | 54 | // check whether the receiver is authorized or registered |
| 55 | 55 | $query = "SELECT * from login where login_id = '$receiverID'"; |
| 56 | 56 | |
| 57 | 57 | $result = $this->connect->query($query); |
| 58 | - if($result->num_rows > 0) // if true |
|
| 58 | + if ($result->num_rows > 0) // if true |
|
| 59 | 59 | { |
| 60 | 60 | //check whether he is sending message for thr first time or he has sent messages before |
| 61 | 61 | $query = "SELECT * from total_message where identifier = '$identifier'"; |
| 62 | 62 | $result = $this->connect->query($query); |
| 63 | - if($result->num_rows>0) // if he has sent messages before |
|
| 63 | + if ($result->num_rows > 0) // if he has sent messages before |
|
| 64 | 64 | { |
| 65 | 65 | // Update Total_Message Table |
| 66 | 66 | $query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'"; |
@@ -70,15 +70,15 @@ discard block |
||
| 70 | 70 | else // if he sends message for the first time |
| 71 | 71 | { |
| 72 | 72 | $length = strlen($userId); |
| 73 | - if(substr($identifier, 0, $length) == $userId) // generate specific unique code |
|
| 73 | + if (substr($identifier, 0, $length) == $userId) // generate specific unique code |
|
| 74 | 74 | { |
| 75 | - $user2 = substr($identifier, $length+1); |
|
| 75 | + $user2 = substr($identifier, $length + 1); |
|
| 76 | 76 | $user1 = $userId; |
| 77 | 77 | } |
| 78 | 78 | else |
| 79 | 79 | { |
| 80 | 80 | $user2 = $userId; |
| 81 | - $length = strlen($identifier) - $length-1; |
|
| 81 | + $length = strlen($identifier) - $length - 1; |
|
| 82 | 82 | $user1 = substr($identifier, 0, $length); |
| 83 | 83 | } |
| 84 | 84 | // insert Details in Total_Message Table |
@@ -100,13 +100,13 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | public function updateMessages($query, $identifier, $reply, $userId, $time) |
| 102 | 102 | { |
| 103 | - if($result = $this->connect->query($query)) |
|
| 103 | + if ($result = $this->connect->query($query)) |
|
| 104 | 104 | { |
| 105 | 105 | //insert message in db |
| 106 | 106 | $query = "INSERT into messages values('$identifier', '$reply', '$userId', '$time', null)"; |
| 107 | - if($this->connect->query($query)) |
|
| 107 | + if ($this->connect->query($query)) |
|
| 108 | 108 | { |
| 109 | - return "Messages is sent"; // if query is executed return true |
|
| 109 | + return "Messages is sent"; // if query is executed return true |
|
| 110 | 110 | } |
| 111 | 111 | else |
| 112 | 112 | { |
@@ -31,17 +31,21 @@ discard block |
||
| 31 | 31 | public function replyTo($msg) |
| 32 | 32 | { |
| 33 | 33 | $msg = json_decode($msg); //decode json value |
| 34 | - if(Session::get('start') != null && !empty($msg)) //checks for session login and the value send |
|
| 34 | + if(Session::get('start') != null && !empty($msg)) { |
|
| 35 | + //checks for session login and the value send |
|
| 35 | 36 | { |
| 36 | 37 | $userId = Session::get('start'); |
| 38 | + } |
|
| 37 | 39 | $identifier = $msg->name; |
| 38 | 40 | |
| 39 | 41 | $receiverID = $identifier; //stores id of the person whom message is to be sent |
| 40 | 42 | |
| 41 | - if($identifier > $userId) // geneate specific unique code to store messages |
|
| 43 | + if($identifier > $userId) { |
|
| 44 | + // geneate specific unique code to store messages |
|
| 42 | 45 | $identifier = $userId . ":" . $identifier; |
| 43 | - else |
|
| 44 | - $identifier = $identifier . ":" . $userId; |
|
| 46 | + } else { |
|
| 47 | + $identifier = $identifier . ":" . $userId; |
|
| 48 | + } |
|
| 45 | 49 | |
| 46 | 50 | $reply = addslashes(trim($msg->reply[0])); // stores the message sent by the user. |
| 47 | 51 | |
@@ -55,27 +59,31 @@ discard block |
||
| 55 | 59 | $query = "SELECT * from login where login_id = '$receiverID'"; |
| 56 | 60 | |
| 57 | 61 | $result = $this->connect->query($query); |
| 58 | - if($result->num_rows > 0) // if true |
|
| 62 | + if($result->num_rows > 0) { |
|
| 63 | + // if true |
|
| 59 | 64 | { |
| 60 | 65 | //check whether he is sending message for thr first time or he has sent messages before |
| 61 | 66 | $query = "SELECT * from total_message where identifier = '$identifier'"; |
| 67 | + } |
|
| 62 | 68 | $result = $this->connect->query($query); |
| 63 | - if($result->num_rows>0) // if he has sent messages before |
|
| 69 | + if($result->num_rows>0) { |
|
| 70 | + // if he has sent messages before |
|
| 64 | 71 | { |
| 65 | 72 | // Update Total_Message Table |
| 66 | 73 | $query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'"; |
| 74 | + } |
|
| 67 | 75 | return $this->updateMessages($query, $identifier, $reply, $userId, $time); |
| 68 | 76 | |
| 69 | - } |
|
| 70 | - else // if he sends message for the first time |
|
| 77 | + } else // if he sends message for the first time |
|
| 71 | 78 | { |
| 72 | 79 | $length = strlen($userId); |
| 73 | - if(substr($identifier, 0, $length) == $userId) // generate specific unique code |
|
| 80 | + if(substr($identifier, 0, $length) == $userId) { |
|
| 81 | + // generate specific unique code |
|
| 74 | 82 | { |
| 75 | 83 | $user2 = substr($identifier, $length+1); |
| 76 | - $user1 = $userId; |
|
| 77 | 84 | } |
| 78 | - else |
|
| 85 | + $user1 = $userId; |
|
| 86 | + } else |
|
| 79 | 87 | { |
| 80 | 88 | $user2 = $userId; |
| 81 | 89 | $length = strlen($identifier) - $length-1; |
@@ -85,14 +93,12 @@ discard block |
||
| 85 | 93 | $query = "INSERT into total_message values('$identifier', 1, '$user1', '$user2', 1, '$time', '$time_id')"; |
| 86 | 94 | return $this->updateMessages($query, $identifier, $reply, $userId, $time); |
| 87 | 95 | } |
| 88 | - } |
|
| 89 | - else // if he is unauthorized echo message is failed |
|
| 96 | + } else // if he is unauthorized echo message is failed |
|
| 90 | 97 | { |
| 91 | 98 | return "Invalid Authentication"; |
| 92 | 99 | } |
| 93 | 100 | } |
| 94 | - } |
|
| 95 | - else |
|
| 101 | + } else |
|
| 96 | 102 | { |
| 97 | 103 | return "Failed"; |
| 98 | 104 | } |
@@ -107,8 +113,7 @@ discard block |
||
| 107 | 113 | if($this->connect->query($query)) |
| 108 | 114 | { |
| 109 | 115 | return "Messages is sent"; // if query is executed return true |
| 110 | - } |
|
| 111 | - else |
|
| 116 | + } else |
|
| 112 | 117 | { |
| 113 | 118 | return "Message is failed"; |
| 114 | 119 | } |