diff --git a/web/application/controllers/base.php b/web/application/controllers/base.php new file mode 100644 index 0000000..f44a5b0 --- /dev/null +++ b/web/application/controllers/base.php @@ -0,0 +1,70 @@ +sessionData && !isset($this->sessionData->userId)) { + header("Location: /login"); + return false; + } else { + return true; + } + } + + protected function loadRender($template, $parameters=array()) { + $newParameters = array_merge($parameters, ["session" => $this->sessionData, "config" => $this->config, "user" => $this->user]); + return parent::loadRender($template, $newParameters); + } + + protected function setupSession() { + if (isset($_COOKIE["session"])) { + $validSession = Sessions::getByField("sessionid", $_COOKIE["session"]); + if ($validSession) { + try { + $this->session = $validSession[0]; + $this->sessionData = json_decode($this->session->data); + } catch (\Exception $e) { } + } else { + $bytes = openssl_random_pseudo_bytes(10, $bool); + $sessionId = bin2hex($bytes); + $this->session = new Sessions(); + $this->session->ip = $_SERVER["REMOTE_ADDR"]; + $this->session->userAgent = $_SERVER["HTTP_USER_AGENT"]; + $this->session->sessionid = $sessionId; + $this->session->save(); + setcookie("session", $sessionId, 2147483647, "/"); + } + } else { + $bytes = openssl_random_pseudo_bytes(10, $bool); + $sessionId = bin2hex($bytes); + $this->session = new Sessions(); + $this->session->ip = $_SERVER["REMOTE_ADDR"]; + $this->session->userAgent = $_SERVER["HTTP_USER_AGENT"]; + $this->session->sessionid = $sessionId; + $this->session->id = $this->session->save(); + setcookie("session", $sessionId, 2147483647, "/"); + } + } + + public function __construct($config, $core, $tpl) + { + parent::__construct($config, $core, $tpl); + $core->setupDatabaseConnection(); + $this->setupSession(); + + if (isset($_POST["csrfmiddlewaretoken"])) { + if ($_POST["csrfmiddlewaretoken"] != $_COOKIE["csrftoken"]) { + throw new \Exception("CSRF tokens did not match"); + } + } + } +} \ No newline at end of file diff --git a/web/application/controllers/main.php b/web/application/controllers/main.php index f14308e..319c5b2 100644 --- a/web/application/controllers/main.php +++ b/web/application/controllers/main.php @@ -1,11 +1,156 @@ user) { - + echo $this->loadRender("main.html"); + } + + public function info($page = null) { + if (!$page) { + echo $this->loadRender("info1.html"); + } else { + if (is_numeric($page)) { + echo $this->loadRender("info$page.html"); + } else { + $notfound = new \system\engine\HF_Status($this->config, $this->core); + echo $notfound->Status404(); + } + } + } + + public function chat() { + $this->session->setData("waiting", true); + $toUser = $this->session->getData("toUser"); + if ($toUser) { + /** @var \application\models\Sessions $otherUserSession */ + $otherUserSession = \application\models\Sessions::getByField("id", $toUser); + if ($otherUserSession) { + $otherUserSession = $otherUserSession[0]; + $otherUserSession->setData("waiting", "true"); + $otherUserSession->setData("toUser", null); + $otherUserSession->save(); + } } + $this->session->setData("toUser", null); + $this->session->save(); + echo $this->loadRender("chat.html"); + } + + public function match() { + $result = false; + + echo json_encode($result); + } + + public function sessionset($key) { + if (in_array($key, ["interests", "gender", "looking"])) { + $this->session->setData($key, $_POST[$key]); + } + } + + public function send() { + $message = new application\models\Messages(); + $message->user_from = $this->session->id; + $message->user_to = $this->session->getData("toUser"); + $message->message = $_POST["message"]; + $message->save(); + } + + public function read() { + $result = false; + $search = false; + + // work around for SQLite + $lock = \application\models\Settings::getSetting("readLock"); + + while($lock) { + $lock = false; + } + \application\models\Settings::setSetting("readLock", (int)true); + + // Check if the current user is talking to someone + $toUser = $this->session->getData("toUser"); + /** @var \application\models\Sessions $session */ + $otherSession = \application\models\Sessions::getByField("id", $this->session->getData("toUser")); + if ($otherSession) { + $otherSession = $otherSession[0]; + // If they aren't waiting and the current toUser is this user.. + if ($otherSession->getData("waiting") && $otherSession->getData("toUser") != $this->session->id) { + $search = true; + } + } else { + $search = true; + } + + // search for someone else in waiting queue + /** @var \application\models\Sessions $firstResult */ + $firstResult = null; + if ($search) { + $allSessions = \application\models\Sessions::all(); + shuffle($allSessions); + shuffle($allSessions); + /** @var \application\models\Sessions $session */ + foreach ($allSessions as $session) { + if ($session->getData("toUser") == $this->session->id && $this->session->getData("toUser") == null) { + // "kick the other user" + $session->setData("toUser", null); + $session->setData("waiting", false); + continue; + } + if ($session->getData("waiting") && $session->id != $this->session->id) { + $firstResult = $session; + $interestWeight = []; + $gender1Weight = true; + $gender2Weight = true; + try { + $interestWeight = array_intersect($this->session->getData("interests"), $session->getData("interests")); + $gender1Weight = in_array($session->getData("gender"), $this->session->getData("looking")); + $gender2Weight = in_array($this->session->getData("gender"), $session->getData("looking")); + } catch (\Exception $e) { } + if ($gender1Weight && $gender2Weight && count($interestWeight) > 0) { + $result = true; + $session->setData("waiting", false); + $session->setData("toUser", $this->session->id); + $this->session->setData("toUser", $session->id); + $this->session->setData("waiting", false); + $session->save(); + $this->session->save(); + break; + } + } + } + + // If no match was made - match with first session + if ($firstResult && !$result) { + $firstResult->setData("waiting", false); + $firstResult->setData("toUser", $this->session->id); + $this->session->setData("toUser", $firstResult->id); + $this->session->setData("waiting", false); + $firstResult->save(); + $this->session->save(); + $result = true; + } + + if (!$result) { + \application\models\Settings::setSetting("readLock", (int)false); + echo json_encode(false); + return; + } + } + + + + // return any messages waiting to be delivered + $messages = \application\models\Messages::getByField("user_to", $this->session->id); + $return = []; + foreach($messages as $message) { + $return[] = $message->user_from . ": " . $message->message; + $message->delete(); + } + + echo json_encode($return); + \application\models\Settings::setSetting("readLock", (int)false); } } \ No newline at end of file diff --git a/web/application/migrations/1.php b/web/application/migrations/1.php new file mode 100644 index 0000000..ece6e38 --- /dev/null +++ b/web/application/migrations/1.php @@ -0,0 +1,14 @@ +data, true); + $raw[$key] = $val; + $this->data = json_encode($raw); + } + + public function getData($key) { + $raw = json_decode($this->data, true); + if (isset($raw[$key])) { + return $raw[$key]; + } else { + return null; + } + } } \ No newline at end of file diff --git a/web/application/models/settings.php b/web/application/models/settings.php new file mode 100644 index 0000000..4a40d01 --- /dev/null +++ b/web/application/models/settings.php @@ -0,0 +1,32 @@ +value; + } else { + return null; + } + } + + public static function setSetting($key, $val) { + $setting = \application\models\Settings::getByField("setting", $key); + if (!$setting) { + $setting = new \application\models\Settings(); + $setting->setting = $key; + } else { + $setting = $setting[0]; + } + $setting->value = $val; + $setting->save(); + } +} \ No newline at end of file diff --git a/web/application/views/base.html b/web/application/views/base.html index 570223e..fd066ad 100644 --- a/web/application/views/base.html +++ b/web/application/views/base.html @@ -9,6 +9,8 @@ + + @@ -20,15 +22,43 @@ + +