-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.php
399 lines (327 loc) · 10.3 KB
/
profile.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/*
Runner's Medium
http://www.runnersmedium.com/
profile.php
show user profile info, snapshot and friend actions if applicable
copyright 2009 Mark Baltrusaitis <http://josieprogramme.com>
*/
require('lib/base.php');
$signedin = $user->signinCheck();
if (!isset($_GET['user'])) {
// no user specified, redirect to index or current user's profile
if($signedin) {
redirect(profile().$user->username());
} else {
redirect(root());
}
}
$thisPage = $_GET['user'];
$private = true;
$myFriend = false;
// is this your page?
$myPage = ($user->username() == $thisPage) ? true : false;
// check if user exists
$result = $conn->query('SELECT a.id, a.picture, a.ispublic FROM users AS a WHERE a.username = \''.mysql_real_escape_string($thisPage).'\' LIMIT 1');
if($conn->rowCount($result) > 0) {
$title = 'Runner\'s Medium - '.$thisPage.'\'s Profile';
$line = $conn->fetchAssoc($result);
$theid = mysql_real_escape_string($line['id']);
// is this user my friend?
if (!$myPage && $signedin) {
$myid = mysql_real_escape_string($user->ID());
$result = $conn->query('SELECT COUNT(*) FROM friends WHERE (user = '.$theid.' AND friend = '.$myid.' AND state = 2) OR (friend = '.$theid.' AND user = '.$myid.' AND state = 2)');
if ($conn->getRow($result)) {
$myFriend = true;
}
}
// check profile privacy
if(!$myPage && !$myFriend && $line['ispublic'] == 0) {
$private = true;
} else {
// show profile
$private = false;
// select profile data
$result = $conn->query('SELECT id, ispublic, name, about, location, url, why, gender, picture, units,
DATE_FORMAT(NOW(), \'%Y\') - DATE_FORMAT(birthday, \'%Y\') - (DATE_FORMAT(NOW(), \'00-%m-%d\') < DATE_FORMAT(birthday, \'00-%m-%d\')) AS age
FROM users WHERE id = '.$theid.' LIMIT 1');
// add data to profile component
$line = $conn->fetchAssoc($result);
$profile = new profileComponent($thisPage);
// populate profile data
foreach ($line as $key => $value) {
$profile->$key = $value;
}
// user units preference
if ($profile->units == '0') {
$units = 'mi';
} else {
$units = 'km';
}
// query summary
$result = $conn->query('SELECT COUNT(id) AS runs, ROUND(SUM(distance), 2) AS total, SEC_TO_TIME(SUM(TIME_TO_SEC(duration))/SUM(distance)) AS pace
FROM runs WHERE user = '.$theid);
$line = $conn->fetchAssoc($result);
// populate summary
foreach ($line as $key => $value) {
$profile->$key = $value;
}
// query friends
$friends = $conn->query('SELECT b.username AS username, b.picture AS picture FROM friends AS a JOIN users AS b on a.friend = b.id
WHERE user = '.$theid.' AND state = 2
UNION SELECT b.username AS username, b.picture AS picture FROM friends AS a JOIN users AS b ON a.user = b.id
WHERE friend = '.$theid.' AND state = 2 LIMIT 6');
if ($signedin) {
$curuser = mysql_real_escape_string($user->ID());
} else {
$curuser = 'NULL';
}
// query recent activity
$myruns = $conn->query('SELECT 0 AS liked, a.id, a.name, ROUND(a.distance, 2) AS distance, a.comments, a.duration AS time,
TIME_TO_SEC(a.duration) AS secs,
a.date AS thedate,
a.created AS created,
DATE_FORMAT(a.date, \'%b %e %Y\') AS date,
SEC_TO_TIME(TIME_TO_SEC(a.duration)/a.distance) AS pace,
b.name AS type, c.name AS course,
IF((SELECT id FROM likes WHERE run = a.id AND user = '.$curuser.'), 1, 0) AS liked
FROM runs AS a LEFT JOIN (runtypes AS b) ON (a.type = b.id)
LEFT JOIN (courses AS c) ON (a.course = c.id)
WHERE a.user = '.$theid.'
ORDER BY thedate DESC, created DESC LIMIT 5');
}
} else {
require('notfound.php');
exit;
}
$thisPage = format($thisPage);
$body = 'id="profile"';
require('header.php');
?>
<div id="content">
<?php
messages($error, $message);
if(!$private) :
?>
<div id="main">
<div class="pic">
<?php
// show pic
echo $profile->showPicture();
?>
</div>
<div id="myusername">
<h2><?php echo $thisPage; ?></h2>
</div>
<br class="clear" />
<?php if ($profile->runs > 0) :
// user has logged a run
?>
<div class="snapshot">
I have logged <em><?php echo format($profile->runs); ?></em> run<?php if ($profile->runs != 1) {
echo's';
} ?> for a total of
<em><?php echo format_d($profile->total); ?></em> <?php echo $units; ?>. My average pace is
<em><?php echo format_t($profile->pace) ?></em> per <?php echo $units; ?>.
</div>
<h3>Recent Activity</h3>
<table id="feed">
<colgroup>
<col class="alt" />
<col />
<col class="alt" />
<col />
</colgroup>
<thead>
<tr>
<td>Date</td>
<td>Details</td>
<td>Times</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
<?php
while ($line = $conn->fetchAssoc($myruns)) :
// id
$runid = format($line['id']);
// date
if ($line['date'] == date('M j Y')) {
$date = 'today';
} else {
$date = substr($line['date'], 0, -5);
}
// no zero distance
if (notempty($line['distance'])) {
$dist = format_d($line['distance']);
} else {
$dist = 0;
}
?>
<tr id="item-<?php echo $runid; ?>">
<td>
<h4><?php echo format($date); ?></h4>
</td>
<td>
<em><?php echo $dist; ?></em> <?php echo format($user->getUnits(true)); ?> <span class="type"><?php echo format($line['type']); ?></span> run
<?php
// course
if (!is_null($line['course'])) {
echo '@'.format($line['course']);
}
?>
</td>
<td>
time: <em><?php echo format_t($line['time']); ?></em> <br />
</td>
<td>
<a <?php if ($signedin) {
echo 'onclick="like('.$runid.')"';
} else {
echo 'href="'.root().'signin"';
} ?> class="like<?php if ($signedin && $line['liked']) {
echo ' liked';
} ?>" id="like-<?php echo $runid; ?>"><?php if ($signedin && $line['liked']) {
echo 'unlike';
} else {
echo 'like';
}?> this run</a>
</td>
</tr>
<?php endwhile; // results loop ?>
</tbody>
</table>
<?php
endif; // runs logged
?>
</div>
<div id="side">
<div id="info">
<?php
if (notempty($profile->name)) {
echo '<strong>Name</strong> '.format($profile->name).'<br />';
}
if (notempty($profile->age)) {
echo '<strong>'.format($profile->age).'</strong> year old runner ';
}
if (notempty($profile->location)) {
echo 'from <strong>'.format($profile->location).'</strong><br />';
} elseif (notempty($profile->age)) {
echo '<br />';
}
if (notempty($profile->about)) {
echo '<strong>Bio</strong> '.format($profile->about).'<br />';
}
if (notempty($profile->why)) {
echo '<strong>I run because</strong> '.format($profile->why).'<br />';
} elseif (notempty($profile->about)) {
echo '<br />';
}
if (notempty($profile->url)) {
// insert http if not there
if (preg_match('/^http:\/\//', $profile->url) == 0) {
$profile->url = 'http://'.$profile->url;
}
echo '<strong>Web</strong> <a href="'.format($profile->url).'" rel="external nofollow">My Site</a>';
}
?>
</div>
<?php
if ($signedin) {
echo '<div id="actions">';
echo '<h3>Actions</h3>';
echo '<ul>';
if ($myPage) {
// my profile
echo '<li><a href="'.root().'settings/editprofile">edit my profile</a></li>';
} elseif ($myFriend) {
// my friend
echo '<li id="nudge"><a onclick="nudge('.format($theid).')">nudge '.$thisPage.'</a></li>';
//echo '<li><a href="'.root().'newmessage?to=$thisPage">message '.$thisPage.'</a></li>';
} else {
// friend actions
echo '<li id="addtofriends"><a onclick="addFriend('.format($theid).')">add '.$thisPage.' to my friends</a></li>';
}
echo '</ul>';
echo '</div>';
}
?>
<div id="people">
<?php
$friendCount = $conn->rowCount($friends);
if ($friendCount > 0) {
// show friends
echo '<div class="heading">';
echo '<h2>Running Friends</h2>';
echo '<a class="seeall" href="'.root().$thisPage.'/friends">('.format($friendCount).') see all</a>';
echo '</div>';
while($line = $conn->fetchAssoc($friends)) {
// set to default if picture path is null
if (is_null($line['picture'])) {
$picture = root().DEFAULT_PIC;
} else {
$picture = root().PIC_DIR.format($line['picture']);
}
$theuser = format($line['username']);
echo '<a href="'.profile().$theuser.'"><img src="'.$picture.'" title="'.$theuser.'" alt="'.$theuser.'" class="picture" /></a>';
}
}
?>
</div>
</div>
<?php
else:
?>
<div id="main">
<div class="pic">
<?php
// show pic
echo $user->getAnyPicture($thisPage, $line['picture']);
?>
</div>
<div id="myusername"><h2><?php echo $thisPage; ?></h2></div>
<div class="private">
This profile is private.
</div>
</div>
<div id="side">
<?php
if ($signedin) {
echo '<div id="actions">';
echo '<h3>Actions</h3>';
echo '<ul>';
if ($myPage) {
// my profile
echo '<li><a href="'.root().'settings/editprofile">edit my profile</a></li>';
} elseif ($myFriend) {
// my friend
echo '<li id="nudge"><a onclick="nudge('.format($theid).')">nudge '.$thisPage.'</a></li>';
//echo '<li><a href="'.root().'newmessage?to=$thisPage">message '.$thisPage.'</a></li>';
} else {
// friend actions
echo '<li id="addtofriends"><a onclick="addFriend('.format($theid).')">add '.$thisPage.' to my friends</a></li>';
}
echo '</ul>';
echo '</div>';
}
?>
</div>
<?php
endif; // no error
?>
</div>
<?php
$scripts = <<<EOD
<script type="text/javascript">
jQuery(function() {
jQuery('#people img').tooltip({
track: true,
delay: 0,
showURL: false,
fade: 250
});
});
</script>
EOD;
require('footer.php');
?>