Skip to content

Commit

Permalink
merging whit branch master cuz there's no reason for two
Browse files Browse the repository at this point in the history
  • Loading branch information
toddync committed Oct 10, 2023
1 parent 695511c commit 28888ad
Show file tree
Hide file tree
Showing 49 changed files with 12,507 additions and 0 deletions.
213 changes: 213 additions & 0 deletions Socket/Chat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
<?php
namespace chat;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use mysqli;

require("search_and_show_msgs.php");
require("search_and_show_users.php");
require("send_msg.php");
require("add_rel.php");
require("login.php");
require("sign.php");
require("group_creation.php");
require("msgs_group.php");


class Chat implements MessageComponentInterface {

protected $clients;
protected $user = array();

protected $mysqli;

public function __construct() {
//echo msgs("","1","7");
//echo str_contains("<!<!<action>!>!> <!msg!>oioioioioioi<!msg!>","<!<!<action>!>!>");
//print_r( explode("<!msg!>","<!<!<usr>!>!> <!msg!>oioioioioioi<!msg!> ") );
$this->clients = new \SplObjectStorage;
$this->mysqli = new mysqli("localhost","root", "", "Message_app");
}

public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);

echo "New connection! ({$conn->resourceId})\n";

$conn -> send("<!msg!>oi<!msg!> <!<!<gived msg>!>!> ");
}

public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

/*$words = explode(" ", $msg);
if(sizeof($words) > 1){
$to = explode("<!reciever!>",$msg);
$sender = explode("<!sender!>",$msg);
$action = $words[2];
unset($words[0]);
unset($words[1]);
unset($words[2]);
$Msg = explode("<!msg!>",$msg);
} else {
$action ="usr";
}*/

if( str_contains($msg,"<!<!<request msg>!>!>") == 1 ){

$search = explode("<!search!>",$msg);
$chat = explode("<!chat!>",$msg);
$user = explode("<!user!>",$msg);

$from -> send("<!<!<requested msg>!>!> <!msg!>".msgs($search[1],$chat[1],$user[1])."<!msg!>");

} else if (str_contains($msg,"<!<!<user>!>!>") == 1 ) {
$id = explode("<!userId!>",$msg);
$this->user[$from->resourceId] = $id[1];



//$from -> send("oi");

$query = "UPDATE `chat` SET `Status` = 'online' WHERE `chat`.`id` = ".$this->user[$from->resourceId]."";
mysqli_query($this->mysqli, $query);

foreach ($this->clients as $client) {

if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send("<!<!<usr>!>!>");
echo("s");
}
}

} else if (str_contains($msg,"<!<!<requested user>!>!>") == 1 ) {

$id = explode("<!id!>",$msg);

$from -> send("<!<!<requested user>!>!> <!user!>". users($id[1]) ."<!user!>");

} else if(str_contains($msg,"<!<!<sended msg>!>!>") == 1 ){

$to = explode("<!reciever!>",$msg);
$sender = explode("<!sender!>",$msg);
$date = explode("<!date!>",$msg);
$Msg = explode("<!msg!>",$msg);
$msg_date = explode("<!msg_date!>",$msg);
$msg_id = explode("<!msg_id!>",$msg);
$msg_id = $msg_id[1];


if(in_array($to[1], $this->user)){
foreach ($this->clients as $client) {
//echo "---\n";
// print_r($this->user);
//echo "---\n";
//print_r($client);
if ( $this->user[$client->resourceId] == $to[1]) {
// The receiver is only the intended reciever
$client->send("<!<!<msg>!>!> <!msg!>".$Msg[1]."<!msg!> <!sender!>".$sender[1]."<!sender!> <!date!>".$msg_date[1]."<!date!>");
}
}
}

$bool = send_msg($sender[1], $to[1], $Msg[1], $date[1]);

$id = explode("<!id!>", $bool);

if (str_contains($bool,"sucess") == 1 )
{

$from -> send("<!<!<sended msg>!>!> <!msg_id!>$msg_id<!msg_id!> <!id!>".$id[1]."<!id!> <!sucess!>");

} else {

$from -> send("<!<!<sended msg>!>!> <!msg_id!>$msg_id<!msg_id!> <!fail!>");

}
} else if (str_contains($msg,"<!<!<relation>!>!>") == 1 ) {
$id1 = explode("<!id1!>",$msg);
$id2 = explode("<!id2!>",$msg);
$response = add_rel($id1[1], $id2[1]);

if($response == "in"){
$from -> send("<!<!<relation>!>!> <!fail!>");
} else {
$from -> send("<!<!<relation>!>!> <!sucess!>");
}
} else if (str_contains($msg,"<!<!<login>!>!>") == 1 ) {
$email = explode("<!email!>", $msg);
$pass = explode("<!pass!>", $msg);

$truth = login($email[1], $pass[1]);

if (str_contains($truth,"<!fail mail!>") == 1 ) {
$from -> send("<!failed!> <!mail!>");
} else if (str_contains($truth,"<!fail pass!>") == 1 ) {
$from -> send("<!failed!> <!pass!>");
} else {
$id = explode("<!id!>", $truth);
$img = explode("<!img!>", $truth);

$from -> send("<!passed!> <!id!>".$id[1]."<!id!> <!img!>".$img[1]."<!img!>");

}
} else if (str_contains($msg,"<!<!<sign>!>!>") == 1 ) {
$email = explode("<!email!>", $msg);
$pass = explode("<!pass!>", $msg);
$username = explode("<!U-name!>", $msg);

$from -> send(signUp($username[1],$email[1], $pass[1]));

} else if (str_contains($msg,"<!<!<group_create>!>!>") == 1 ) {
$user = explode("<!user!>", $msg);
$name = explode("<!name!>", $msg);

$sucsess = Create_group($name[1],$user[1]);
$id = explode("<!id!>", $sucsess);

$response = add_rel($user, $id[1]);

$from->send("<!<!<usr>!>!>");

} else if (str_contains($msg,"<!<!<group_msg>!>!>") == 1 ) {

$search = explode("<!search!>",$msg);
$chat = explode("<!chat!>",$msg);
$user = explode("<!user!>",$msg);


$from -> send("<!<!<requested msg>!>!> <!msg!>".msgs_group($search[1], $chat[1], $user[1])."<!msg!>");


} else if (str_contains($msg,"<!<!<group_remove>!>!>") == 1 ) {

}
}

public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
$this->clients->detach($conn);

if(array_key_exists( $conn->resourceId , $this->user )){
echo "Connection {$conn->resourceId} has disconnected\n";
$query = "UPDATE `chat` SET `Status` = 'offline' WHERE `chat`.`id` = ".$this->user[$conn->resourceId];
mysqli_query($this->mysqli, $query);
}

foreach ($this->clients as $client) {
$client->send("<!<!<usr>!>!>");
}

}

public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";

// $conn->close();
}
}
24 changes: 24 additions & 0 deletions Socket/add_rel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

function add_rel($id1, $id2){

$mysqli = new mysqli("localhost","root", "", "Message_app");

$sql = "SELECT * FROM chat WHERE id = '$id2'";
$consult = mysqli_query($mysqli, $sql);

$sql = "SELECT * FROM `users_relation` WHERE `usr1`= '$id1' AND `usr2`= '$id2'";
$consult2 = mysqli_query($mysqli, $sql);

if (mysqli_num_rows($consult) > 0 && mysqli_num_rows($consult2) == 0){

$query= "INSERT INTO `users_relation` (`usr1`, `usr2`) VALUES ('".$id1."', '".$id2."'), ('".$id2."', '".$id1."')";

mysqli_query($mysqli, $query);
echo "success";

} else {
return "in";
}

}
32 changes: 32 additions & 0 deletions Socket/group_creation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

function Create_group($name, $user){

$mysqli = new mysqli("localhost","root", "", "Message_app");

$return = "<!<!<group>!>!>";

$sql = "SELECT * FROM chat WHERE `Name`='$name' AND `grp`='yes' AND `Owner`='$user'";
$consulta = mysqli_query($mysqli, $sql);

if (mysqli_num_rows($consulta) != 0) {
$return .= " <!fail!>";
} else {
//$senhaS = password_hash($senha, PASSWORD_DEFAULT);
$sql = "INSERT INTO chat (`Name`, `Owner`, `Private_key`, `Public_key`, `Status`, `grp`) VALUES ('$name', '$user', '1', '1', 'offline', 'yes')";
mysqli_query($mysqli, $sql);

$sql = "SELECT * FROM chat WHERE `Name`='$name' AND `grp`='yes' AND `Owner`='$user'";
$result = mysqli_query($mysqli, $sql);

while($row = mysqli_fetch_array($result))
{
$return = "<!id!>".$row["id"]."<!id!>";
}

$return .= " <!sucess!>";
}

return $return;
}
?>
55 changes: 55 additions & 0 deletions Socket/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
*/

function login($email, $senha){
$mysqli = new mysqli("localhost","root", "", "Message_app");

$return = "";


$sql = "SELECT * FROM chat WHERE Email = '$email'";
$consulta = mysqli_query($mysqli, $sql);
$coluna = mysqli_fetch_array($consulta);

if (mysqli_num_rows($consulta) == 0){

$return .= "<!fail mail!>";
$return .= $email;
$return .= "<!fail mail!>";

} else if ($senha != $coluna["Password"]){
$return .= "<!fail pass!>";
} else {

$return .= "<!id!>";
$return .= $coluna["id"];
$return .= "<!id!>";

if ($coluna["Image"] != ""){

$return .= "<!img!>";
$return .= $coluna["Image"];
$return .= "<!img!>";

}else{

$return .= "<!img!>";
$return .= "images/default.png";
$return .= "<!img!>";

}
}

return $return;
}
?>
Loading

0 comments on commit 28888ad

Please sign in to comment.