-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_main.php
216 lines (170 loc) · 6.19 KB
/
library_main.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
function logout()
{
if ($_SESSION['loggedIn'] == 1)
{
//session variable is registered, the user is ready to logout
session_unset();
session_destroy();
}
}
function Login($username, $userpassword)
{
$success = false;
$dbh = SqlConnect();
$stmt = $dbh->prepare("SELECT password FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
if ($stmt->execute())
{
$row = $stmt->fetch();
$storedPassword = $row['password'];
if ($storedPassword == md5($userpassword))
{
$_SESSION['loggedIn'] = 1;
$_SESSION['user'] = $username;
$success = true;
}
}
return $success;
}
function Register($username, $regkey, $userpassword)
{
$success = false;
$dbh = SqlConnect();
if (Login($username, $regkey))
{
$stmt = $dbh->prepare("UPDATE users SET password = :userpassword WHERE username = :username");
$stmt->bindParam(':userpassword', md5($userpassword));
$stmt->bindParam(':username', $username);
if ($stmt->execute())
$success = true;
}
return $success;
}
function GetProfileRow($username)
{
$dbh = SqlConnect();
$stmt = $dbh->prepare('SELECT username, firstname, lastname, city, state,
purchaseprice, purchasedate, mortgagebalance, secondmortgage,
housephoto, twittername FROM profiles p
JOIN users u ON p.userid = u.userid
WHERE u.username = :username');
$stmt->bindParam(':username', $username);
if ($stmt->execute())
$row = $stmt->fetch();
else
$row = null;
return $row;
}
function GetProfileEvents($username)
{
$dbh = SqlConnect();
$stmt = $dbh->prepare("SELECT from_unixtime(entrydate,
get_format(date, 'USA')) AS date,
title,
description
FROM timeline t
JOIN users u ON t.userid = u.userid
JOIN profiles p ON p.userid = u.userid
WHERE u.username = :username
ORDER BY entrydate ASC");
$stmt->bindParam(':username', $username);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
if ($stmt->execute())
return $stmt->fetchAll();
else
return '';
}
function GetProfileList()
{
$dbh = SqlConnect();
$stmt = $dbh->prepare('SELECT p.firstname, u.username FROM profiles p
JOIN users u ON p.userid = u.userid');
if ($stmt->execute())
$rows = $stmt->fetchAll();
else
$rows = null;
return $rows;
}
function SubmitProfileRow($username, $firstname, $lastname, $city, $state,
$purchaseprice, $purchasedate, $mortgagebalance,
$secondmortgage, $housephoto, $twittername)
{
$success = false;
$dbh = SqlConnect();
$stmt = $dbh->prepare('UPDATE profiles p, users u
SET p.firstname = :firstname,
p.lastname = :lastname, p.city = :city, p.state = :state,
p.purchasedate = :purchasedate, p.purchaseprice = :purchaseprice,
p.mortgagebalance = :mortgagebalance, p.secondmortgage = :secondmortgage,
p.housephoto = :housephoto, p.twittername = :twittername
WHERE p.userid = u.userid AND
u.username = :username');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':city', $city);
$stmt->bindParam(':state', $state);
$stmt->bindParam(':purchaseprice', $purchaseprice);
$stmt->bindParam(':purchasedate', $purchasedate);
$stmt->bindParam(':mortgagebalance', $mortgagebalance);
$stmt->bindParam(':secondmortgage', $secondmortgage);
$stmt->bindParam(':housephoto', $housephoto);
$stmt->bindParam(':twittername', $twittername);
if ($stmt->execute())
$success = true;
return $success;
}
function SubmitTimelineEntry($username, $title, $description, $entrydate)
{
$success = false;
$dbh = SqlConnect();
$stmt = $dbh->prepare('INSERT INTO timeline
(userid, profileid, title, description, entrydate)
VALUES ( (SELECT userid FROM users u WHERE username = :username),
(SELECT p.profileid FROM profiles p JOIN users u ON p.userid = u.userid WHERE u.username = :username),
:title,
:description,
:entrydate)');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':entrydate', $entrydate);
if ($stmt->execute())
$success = true;
return $success;
}
function LoadNavBar()
{
if (IsLoggedIn())
{
print("Welcome back ". $_SESSION['user'] . ". Add an update to the <a href=\"updatetimeline.php\">timeline</a>, edit your <a href=\"profile.php\">profile</a>, or <a href=\"logout.php\">logout</a>");
}
else
print('If you\'re a contributor, feel free to <a href="login.php">login</a>. If you want to play, <a href="mailto:[email protected]">drop us a line.</a>');
print($sidebar);
}
function IsLoggedIn()
{
if ($_SESSION['loggedIn'] == 1)
return true;
else
return false;
}
function SqlConnect()
{
$dbfilename = '../../planetrefidb.txt';
$dbfile = fopen($dbfilename, "r");
$dbuser = trim(fgets($dbfile));
$dbpassword = trim(fgets($dbfile));
$dbhost = trim(fgets($dbfile));
$dbname = trim(fgets($dbfile));
$dbh = new PDO('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpassword, array(PDO::ATTR_PERSISTENT => true));
return $dbh;
}
function SqlDisconnect($dbh)
{
$dbh = null;
}
?>