-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmsg.php
111 lines (91 loc) · 2.22 KB
/
libmsg.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
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
<?php
function httpDeath($code) {
$table = array(
200 => "OK",
400 => "Bad Request",
401 => "Unauthorized",
404 => "Not Found",
503 => "Service Unavailable"
);
header("HTTP/1.0 $code $table[$code]");
exit();
}
function startSQLConnection() {
include '/home/guelphseven/password.php';
if(mysql_connect('localhost', 'messaging', $MYSQL_PASS)) {
if(mysql_select_db('messaging')) {
return true;
}
}
return false;
}
function safeArrayValue($array, $key) {
if(array_key_exists($key, $array)) {
return $array[$key];
} else {
return NULL;
}
}
function isValidUsername($username) {
if(!ctype_alnum($username) || strlen($username) < 4 || strlen($username) > 20) {
return false;
}
return true;
}
function usernameToID($username) {
if(isValidUsername($username)) {
if($result = mysql_query("SELECT id FROM users WHERE username = '$username';")) {
$row = mysql_fetch_assoc($result);
return $row['id'];
}
}
return NULL;
}
function usernameFromID($id) {
if($result = mysql_query("SELECT username FROM users WHERE id = '$id';")) {
$row = mysql_fetch_assoc($result);
return $row['username'];
}
return NULL;
}
function isAuthenticatedUser($username, $password) {
if(isValidUsername($username)) {
$hash = md5($username . $password);
if($result = mysql_query("SELECT id FROM users WHERE username = '$username' AND password = '$hash';")) {
if(mysql_num_rows($result) > 0) {
return true;
}
}
}
return false;
}
function canWriteToFeed($username, $password, $recipient) {
if(!isAuthenticatedUser($username, $password)) {
return false;
}
if(NULL === ($reader = usernameToID($recipient))) {
return false;
}
if(NULL === ($writer = usernameToID($username))) {
return false;
}
if($result = mysql_query("SELECT COUNT(*) FROM access WHERE reader = '$reader' AND writer = '$writer';")) {
if(mysql_num_rows($result) > 0) {
return true;
}
}
return false;
}
function writeToFeed($reader, $writer, $data) {
if(NULL === ($reader_id = usernameToID($reader))) {
return false;
}
if(NULL === ($writer_id = usernameToID($writer))) {
return false;
}
if(mysql_query("INSERT INTO feeds (id, origin, post) VALUES ('$reader_id', '$writer_id', '$data');")) {
return true;
}
return false;
}
?>