foreveralone

foreveralone Commit Details


Date:2016-02-21 17:37:08 (8 years 10 months ago)
Author:Natalie Adams
Branch:master
Commit:ebae6761824f7abe8ca528b405cfeaa10c720e8d
Parents: 7dfeb968edae49776c4ae545fffc5313de92d773
Message:Adding application

Changes:

File differences

web/application/controllers/base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
use \application\models\Sessions;
abstract class base extends \system\engine\HF_Controller {
/** @var \application\models\Users $user */
protected $user = null;
/** @var \application\models\Sessions $session */
protected $session = null;
protected $sessionData = null;
protected $loginRequired = true;
protected $sessionRequired = true;
protected function isLoggedIn() {
if (!$this->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");
}
}
}
}
web/application/controllers/main.php
11
22
3
3
44
55
66
7
8
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
935
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
10155
11156
<?php
class main extends \system\engine\HF_Controller
class main extends base
{
public function index()
{
if ($this->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);
}
}
web/application/migrations/1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
use \vendor\DB\DB;
echo "Creating session table..." . PHP_EOL;
$autoIncrement = \system\engine\HF_Model::AUTOINCREMENT_SQLITE;
DB::query("CREATE TABLE sessions (
id INTEGER PRIMARY KEY $autoIncrement,
sessionid VARCHAR(255),
ip VARCHAR(255),
userAgent VARCHAR(255),
data TEXT
)");
web/application/migrations/2.php
1
2
3
4
5
6
7
8
9
10
11
<?php
use \vendor\DB\DB;
$autoIncrement = \system\engine\HF_Model::AUTOINCREMENT_SQLITE;
DB::query("CREATE TABLE messages (
id INTEGER PRIMARY KEY $autoIncrement,
user_to INTEGER,
user_from INTEGER,
message VARCHAR(255)
)");
web/application/migrations/3.php
1
2
3
4
5
6
7
8
9
10
<?php
use \vendor\DB\DB;
DB::query("CREATE TABLE settings
(
id INTEGER PRIMARY KEY,
setting TEXT,
value TEXT
);");
web/application/models/messages.php
1
2
3
4
5
6
7
8
9
10
11
12
<?php
namespace application\models;
use system\engine\HF_Model;
class Messages extends HF_Model
{
public $user_to;
public $user_from;
public $message;
}
web/application/models/sessions.php
77
88
99
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1025
public $ip;
public $userAgent;
public $data;
public function setData($key, $val) {
$raw = json_decode($this->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;
}
}
}
web/application/models/settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
namespace application\models;
use system\engine\HF_Model;
class Settings extends HF_Model
{
public $setting;
public $value;
public static function getSetting($key) {
$setting = \application\models\Settings::getByField("setting", $key);
if ($setting) {
return $setting[0]->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();
}
}
web/application/views/base.html
99
1010
1111
12
13
1214
1315
1416
......
2022
2123
2224
25
26
2327
2428
2529
2630
2731
2832
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2960
3061
31
3262
3363
3464
<link rel="stylesheet" type="text/css" href="./media/css/jqueryui.css" />
<link rel="stylesheet" type="text/css" href="./media/css/jquery-confirm.min.css" />
<link rel="stylesheet" type="text/css" href="./media/css/waitMe.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2-rc.1/css/select2.min.css" rel="stylesheet" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" integrity="sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ==" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>
<script src="./media/js/jquery-confirm.min.js"></script>
<script src="./media/js/waitMe.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2-rc.1/js/select2.min.js"></script>
<title>{{title}}</title>
<style type="text/css">
.text-center {
text-align: center;
}
</style>
<style type="text/css">
.centered {
position: fixed;
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
}
html, body, .container-table {
height: 100%;
}
.container-table {
display: table;
}
.vertical-center-row {
display: table-cell;
vertical-align: middle;
}
.intro-text {
color: black;
font-size: 50px;
font-family: ‘Lucida Sans Unicode’, ‘Lucida Grande’, sans-serif;
}
</style>
</head>
<body>
{% include "menu.html" %}
{% block content %}{% endblock %}
</body>
</html>
web/application/views/chat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{% extends "base.html" %}
{% block content %}
<div class="centered" id="container">
<textarea disabled="disabled" autocomplete="off" cols="100" rows="25" id="chat"></textarea>
<br>
<input autocomplete="off" style="width: 725px;" placeholder="Your message here..." type="text" id="input">
</div>
<script type="text/javascript">
$(function () {
var searching = true;
function appendChat(text) {
var chatVal = $("#chat").val();
chatVal += text + "\n";
$("#chat").val(chatVal);
}
function poll() {
setTimeout(function() {
// hit the read endpoint
// the read endpoint will return false if matching is in progress
// otherwise will return an array
$.ajax({
url: "./main/read/", success: function (data) {
if (data === false) {
$("#container").waitMe({
effect: "ios",
text: "Finding match. Please wait...",
bg: 'rgba(255,255,255,0.7)',
color: '#000',
maxSize: ''
});
searching = true;
} else {
$("#container").waitMe("hide");
if (searching) {
appendChat("Friendly Replicator Android (Fran): Match found!");
searching = !searching;
}
if (data) {
$.each(data, function (index, value) {
appendChat(value);
});
}
}
}, dataType: "json", complete: poll
});
}, 2000);
}
$('#input').keydown(function (e){
if(e.keyCode == 13){
var val = $("#input").val();
$("#input").val("");
appendChat("You: " + val);
$.post("./main/send/", {message : val});
}
});
$("#container").waitMe({
effect: "ios",
text: "Finding match. Please wait...",
bg: 'rgba(255,255,255,0.7)',
color: '#000',
maxSize: ''
});
poll();
});
</script>
{% endblock %}
web/application/views/info1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{% extends "base.html" %}
{% block content %}
<div class="container container-table">
<div class="row vertical-center-row">
<div class="text-center col-md-6 col-md-offset-3">
<div>I just need a pieces of information from you so I can match you with your other half.</div>
<br>
<form role="form">
<div class="form-group">
<label for="interests">What are your interests (you may select more than one)?</label>
<select class="form-control" id="interests" multiple>
<option value="running">Running</option>
<option value="anime">Anime</option>
<option value="programmer">Programming</option>
<option value="android">Android</option>
</select>
</div>
<nav>
<ul class="pager">
<li><a href="./main/info/2">Next</a></li>
</ul>
</nav>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$("#interests").select2();
$("#interests").on("change", function () {
$.post("./main/sessionset/interests/", {"interests": $(this).val()});
});
});
</script>
{% endblock %}
web/application/views/info2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{% extends "base.html" %}
{% block content %}
<div class="container container-table">
<div class="row vertical-center-row">
<div class="text-center col-md-6 col-md-offset-3">
<div>I just need a pieces of information from you so I can match you with your other half.</div>
<br>
<form role="form">
<div class="form-group">
<label for="gender">What is your gender?</label>
<select class="form-control" id="gender">
<option value=""></option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="transgender">Transgender</option>
<option value="other">Other</option>
<option value="agender">Agender</option>
<option value="transexual">Transexual</option>
<option value="pangender">Pangender</option>
<option value="genderfluid">Genderfluid</option>
<option value="transfeminine">Transfeminine</option>
<option value="transmasculine">Transmasculine</option>
<option value="trigenderpyrofox">Tri-gender pyrofox (from the forest planet)</option>
</select>
</div>
<nav>
<ul class="pager">
<li><a href="./main/info/1">Prev</a></li>
<li><a href="./main/info/3">Next</a></li>
</ul>
</nav>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$("#gender").select2();
$("#gender").on("change", function () {
$.post("./main/sessionset/gender/", {"gender": $(this).val()});
});
});
</script>
{% endblock %}
web/application/views/info3.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{% extends "base.html" %}
{% block content %}
<div class="container container-table">
<div class="row vertical-center-row">
<div class="text-center col-md-6 col-md-offset-3">
<div>I just need a pieces of information from you so I can match you with your other half.</div>
<br>
<form role="form">
<div class="form-group">
<label for="looking">Who are you looking for?</label>
<select class="form-control" id="looking" multiple>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="transgender">Transgender</option>
<option value="other">Other</option>
<option value="agender">Agender</option>
<option value="transexual">Transexual</option>
<option value="pangender">Pangender</option>
<option value="genderfluid">Genderfluid</option>
<option value="transfeminine">Transfeminine</option>
<option value="transmasculine">Transmasculine</option>
<option value="trigenderpyrofox">Tri-gender pyrofox (from the forest planet)</option>
</select>
</div>
<nav>
<ul class="pager">
<li><a href="./main/info/1">Prev</a></li>
<li><a href="./main/chat/">Find</a></li>
</ul>
</nav>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(function () {
$("#looking").select2();
$("#looking").on("change", function () {
$.post("./main/sessionset/looking/", {"looking": $(this).val()});
});
});
</script>
{% endblock %}
web/application/views/main.html
22
33
44
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
566
667
{% block content %}
<div class="centered">
<div id="intro" class="intro-text">
Hi there.
</div>
<div style="text-align: center"><a href="./main/info">Skip</a></div>
</div>
<script type="text/javascript">
$(function () {
// Queue infomercial - There has to be a better way!
var counter = 0;
var text = ["Hi there.", "You are here because you believe you are forever alone.",
"Not anymore."];
var text2 = ['your new friend.',
'your new running partner.',
'your special someone.'];
function introText1() {
if (counter == 3) {
counter = 0;
$("#intro").fadeOut(1000).html('I will help you find <div id="find">your soul mate.</div>').fadeIn(1000);
setTimeout(function () {
$("#find").fadeOut(1000);
}, 1000);
setTimeout(function () {
introText2();
}, 2000);
} else {
$("#intro").text(text[counter]).fadeIn(1500);
setTimeout(function () {
$("#intro").fadeOut(1000);
}, 2000);
counter++;
setTimeout(function () {
introText1();
}, 3000);
}
}
function introText2() {
if (counter <= 2) {
console.log(counter);
$("#find").html(text2[counter]).fadeIn(1500);
counter++;
setTimeout(function () {
if (counter <= 2) {
$("#find").fadeOut(1000);
}
}, 2000);
setTimeout(function () {
introText2();
}, 3000)
}
}
introText1();
});
</script>
{% endblock %}

Archive Download the corresponding diff file

Branches

Tags

Number of commits:
Page rendered in 0.09794s using 14 queries.