示例#1
0
 public function showPage()
 {
     if (isset($_POST["register-data"]) && isset($_POST["register-password"])) {
         try {
             $client = new Client();
             $client->authenticate($_POST["register-data"], Client::AUTH_HTTP_TOKEN);
             $user = $client->api('current_user')->show();
             $repos = [];
             foreach ($client->api('current_user')->repositories('member', 'updated', 'desc') as $repo) {
                 $repos[] = ["name" => $repo["full_name"], "isPrivate" => $repo["private"]];
             }
             if (strlen($_POST["register-password"]) >= 6) {
                 Users::createUser($user["login"], $client->api('current_user')->emails()->all()[0], $_POST["register-password"], $_POST["register-data"], $repos);
                 Channels::addChannels($repos);
                 echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Register", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user])]);
             } else {
                 echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Register", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["error" => "Passwords must be at least 6 characters long."])]);
             }
         } catch (\Exception $e) {
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Register", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["error" => "Oh no! Your registration couldn't be completed. Do you already have an account? Is your token valid?"])]);
         }
     } else {
         echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Register", "content" => $this->getTemplateEngine()->render($this->getTemplate(), [])]);
     }
     //echo $this->getTemplateEngine()->render($this->getTemplate(), []);
 }
示例#2
0
 public static function destroySession()
 {
     if (isset($_SESSION['login-data'])) {
         $data = explode("\\", $_SESSION['login-data']);
         Users::deleteSession($data[0], $data[1]);
         unset($_SESSION['login-data']);
     }
 }
示例#3
0
 public static function isRepoOwner($user, $repo)
 {
     foreach (Users::getUser($user)["repos"] as $testRepo) {
         if ($testRepo["name"] === $repo) {
             return true;
         }
     }
     return false;
 }
示例#4
0
 /**
  * Triggered when a client sends data through the socket
  * @param  \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
  * @param  string $msg The message received
  * @throws \Exception
  */
 function onMessage(ConnectionInterface $from, $msg)
 {
     $json = json_decode($msg, true);
     Logger::debug("WS message received: {$msg}");
     switch ($json["type"]) {
         case "message":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                     $this->sendMessageToChannel($json["payload"]["message"], $from, $json["payload"]["channel"]);
                     if ($this->isBridged()) {
                         $this->irc->sendMessageToChannel($json["payload"]["message"], "{$this->clients[$from]->getUser()["_id"]}!{$this->clients[$from]->getUser()["_id"]}@{$from->remoteAddress}", $json["payload"]["channel"]);
                     }
                 }
             }
             break;
         case "channel":
             if ($this->clients[$from]->isAuthenticated()) {
                 if ($json["payload"]["verb"] == "add") {
                     $reply = $json;
                     $chan = Channels::getChannel($json["payload"]["channel"]);
                     if ($chan !== null) {
                         if (!$chan["private"] && !in_array($this->clients[$from]->getUser()['_id'], $chan["banned"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } elseif (Users::isRepoOwner($this->clients[$from]->getUser()["_id"], $json["payload"]["channel"])) {
                             $reply["payload"]["verb"] = "add";
                             $this->clients[$from]->addChannel($json["payload"]["channel"]);
                         } else {
                             $reply["payload"]["verb"] = "error";
                         }
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 } elseif ($json["payload"]["verb"] == "remove") {
                     $reply = $json;
                     if ($this->clients[$from]->isMemberOf($json["payload"]["channel"])) {
                         $reply["payload"]["verb"] = "remove";
                         $this->clients[$from]->removeChannel($json["payload"]["channel"]);
                     } else {
                         $reply["payload"]["verb"] = "error";
                     }
                     $from->send(json_encode($reply));
                 }
             }
             break;
         case "auth":
             $from->send(json_encode(["type" => "authreply", "payload" => ["done" => $this->clients[$from]->authenticate($json["payload"])]]));
             break;
         default:
             Logger::warning("Bad message got.");
             break;
     }
 }
示例#5
0
 public function showPage()
 {
     if (isset($_POST["login-username"]) && isset($_POST["login-password"])) {
         if (Users::checkLogin($_POST["login-username"], $_POST["login-password"])) {
             SessionStore::createSession($_POST["login-username"]);
             (new index())->showPage("You are now logged in.");
         } else {
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Login", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["error" => "Your username or password appear incorrect."])]);
         }
     } else {
         echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Login", "content" => $this->getTemplateEngine()->render($this->getTemplate(), [])]);
     }
 }
示例#6
0
 public function authenticate($payload)
 {
     if (isset($payload["key"])) {
         $data = explode("\$\$", $payload["key"]);
         $user = Users::getUser($data[0]);
         if ($user !== false) {
             if (isset($user["sessions"][$data[1]]) && $user["sessions"][$data[1]]["ip"] === $this->connectionInterface->remoteAddress) {
                 $this->user = $user;
                 $this->authenticated = true;
                 return true;
             } else {
                 return false;
             }
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
示例#7
0
 public function showPage()
 {
     $user = SessionStore::getCurrentSession();
     $sessions = [];
     foreach ($user["sessions"] as $id => $data) {
         $data["id"] = $id;
         $sessions[] = $data;
     }
     $user["sessions"] = $sessions;
     if (isset($_POST['new-password'])) {
         if (strlen($_POST['new-password']) >= 6) {
             Users::changePassword($user["_id"], $_POST['new-password']);
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user, "message" => "Your password has been changed."])]);
         } else {
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user, "error" => "Failed to change password. Are you sure it's six chars or longer?"])]);
         }
     } elseif (isset($_GET['reloadrepos'])) {
         try {
             $client = new Client();
             $client->authenticate($user['token'], Client::AUTH_HTTP_TOKEN);
             $repos = [];
             foreach ($client->api('current_user')->repositories('member', 'updated', 'desc') as $repo) {
                 $repos[] = ["name" => $repo["full_name"], "isPrivate" => $repo["private"]];
             }
             Users::updateRepos($user['_id'], $repos);
             Channels::addChannels($repos);
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user, "message" => "Your repositories have been reloaded."])]);
         } catch (\Exception $e) {
             echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user, "error" => "Failed to update repositories."])]);
         }
     } elseif (isset($_GET["changetoken"])) {
         echo "This feature is still being worked on.";
     } elseif (isset($_GET["destroysession"])) {
         Users::deleteSession($user['_id'], $_GET["destroysession"]);
         echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user, "message" => "The session has been closed. The client is now logged out."])]);
     } else {
         echo $this->getTemplateEngine()->render($this->getTemplateSnip("page"), ["title" => "Profile", "content" => $this->getTemplateEngine()->render($this->getTemplate(), ["user" => $user])]);
     }
 }
示例#8
0
 public function handleMessage($msg)
 {
     Logger::debug("IRC message received: {$msg}");
     $msg = explode(" ", $msg);
     switch ($msg[0]) {
         case "PASS":
             $this->password = trim($msg[1]);
             break;
         case "NICK":
             foreach ($this->server->getClients() as $client) {
                 if (strtolower($client->getNick()) === $msg[1]) {
                     $this->sendNumerical("NOTICE AUTH :*** That nick is already in use..");
                     break;
                 }
             }
             $this->setNick($msg[1]);
             break;
         case "USER":
             if ($this->password) {
                 if (Users::checkLogin($msg[1], $this->password)) {
                     $this->setIdent($msg[1]);
                     $this->setRealName($msg[4]);
                     $this->sendNumerical("001 {$this->nick} :Welcome to the Internet Relay Network {$this}");
                     $this->sendNumerical("002 {$this->nick} :Your host is shogchat, running version a development build.");
                     $this->sendNumerical("003 {$this->nick} :This server was created " . @date('r'));
                     $this->sendNumerical("375 {$this->nick} :=== Welcome to ShogChat IRC Bridge ====");
                     $this->sendNumerical("372 {$this->nick} :You are connected to the IRC bridge as {$this->nick}. All of your GitHub repos have a channel, just join the channel with the name of your repo and start chatting. If you get an error saying the channel doesn't exist, you might need to reload your repos on our website.");
                     $this->sendNumerical("372 {$this->nick} :-----------------------------");
                     $this->sendNumerical("372 {$this->nick} :(ShogChat)->shogAllTheChats()");
                     $this->sendNumerical("376 {$this->nick} :End of MOTD");
                     $this->sendNumerical("NICK " . $this->nick);
                     $this->authenticated = true;
                     $this->user = Users::getUser($msg[1]);
                 } else {
                     $this->sendNumerical("464 {$this->nick} :The password you have set is incorrect.");
                     $this->close();
                 }
             } else {
                 $this->sendNumerical("464 {$this->nick} :You haven't set a password.");
                 $this->close();
             }
             break;
         case "JOIN":
             if ($this->isAuthenticated()) {
                 $chans = explode(",", $msg[1]);
                 foreach ($chans as $chan) {
                     $chan = Channels::getChannel(trim(substr($chan, 1)));
                     if ($chan != null) {
                         if (!$chan["private"]) {
                             if (!in_array($this->getUser()['_id'], $chan["banned"])) {
                                 $this->addChannel($chan["_id"]);
                                 $this->sendNumerical("332 {$this->nick} #{$chan["_id"]} :This is a public channel.");
                             } else {
                                 $this->sendNumerical("474 {$this->nick} :You are banned from this channel.");
                             }
                         } else {
                             if (Users::isRepoOwner($this->getUser()['_id'], $chan["_id"])) {
                                 $this->addChannel($chan["_id"]);
                                 $this->sendNumerical("332 {$this->nick} #{$chan["_id"]} :This is a private channel.");
                             } else {
                                 $this->sendNumerical("473 {$this->nick} :This is a private channel, you need an invite.");
                             }
                         }
                     } else {
                         $this->sendNumerical("403 {$this->nick} :That repository isn't registered with ShogChat.");
                     }
                 }
             }
             break;
         case "PRIVMSG":
             $chan = substr($msg[1], 1);
             if ($this->isMemberOf($chan)) {
                 if ($msg[2][0] == ":") {
                     $msg[2] = substr($msg[2], 1);
                     $msg[2] = implode(" ", array_slice($msg, 2));
                 }
                 $this->server->sendMessageToChannel($msg[2], $this, $chan);
                 $this->server->getChatServer()->sendMessageToChannel($msg[2], $this->ident, $chan);
             }
             break;
         case "PONG":
             //Nothing needed?
             break;
         case "QUIT":
             $this->close();
             break;
         case "PART":
             //TODO
             break;
         case "USERS":
             $this->sendNumerical("395 {$this->nick} :ShogChat can't display users in channel.");
             break;
         case "WHOIS":
             //TODO check nick
             $this->sendNumerical("311 {$this->nick} {$this->nick} {$this->ident} {$this->host} * :{$this->realName}");
             $this->sendNumerical("318 {$this->nick} {$this->nick} :End of /WHOIS list.");
             break;
         default:
             Logger::warning($msg[0] . " is an unrecognized IRC command.");
             break;
     }
 }