forked from drlippman/IMathAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getpostlist.php
213 lines (205 loc) · 10.2 KB
/
getpostlist.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
<?php
//IMathAS: New threads and messages list for a course using remoteaccess code
//for mobile access w/o logging in
//(c) 2009 David Lippman
$init_skip_csrfp = true;
require("init_without_validate.php");
$keyString = Sanitize::simpleString($_GET['key']);
if (!empty($_COOKIE['remoteaccess']) && strlen($_COOKIE['remoteaccess'])==10) {
$keyString = Sanitize::simpleString($_COOKIE['remoteaccess']);
} else if (empty($keyString) || strlen(trim($keyString))!=10) {
echo "Key Error";
setcookie('remoteaccess',$keyString, time()+60*60*24*30*365*10,'','',true,true);
exit;
} else {
//setcookie('remoteaccess',$keyString, time()+60*60*24*30*365*10,'','',true,true);
}
//look up user
$stm = $DBH->prepare("SELECT id FROM imas_users WHERE remoteaccess=:remoteaccess");
$stm->execute(array(':remoteaccess'=>$keyString));
if ($stm->rowCount()==0) {
echo "Access key invalid";
exit;
}
$userid = $stm->fetchColumn(0);
$tzoffset = Sanitize::onlyInt($_GET['tzoffset']);
$now = time();
function tzdate($string,$time) {
global $tzoffset;
//$dstoffset = date('I',time()) - date('I',$time);
//return gmdate($string, $time-60*($tzoffset+60*$dstoffset));
$serveroffset = date('Z') + $tzoffset*60;
return date($string, $time-$serveroffset);
//return gmdate($string, $time-60*$tzoffset);
}
function formatdate($date) {
return date("D n/j/y, g:i a",$date);
//return tzdate("D n/j/y, g:i a",$date);
//return tzdate("M j, Y, g:i a",$date);
}
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?><!DOCTYPE html>
<html>
<head>
<meta name = "viewport" content = "width = device-width">
<title><?php echo $installname;?> Posts and Messages</title>
</head>
<body>
<div style="font-weight: bold"><?php echo $installname;?> Posts and Messages</div>
<div style="color: gray; font-size: 80%">Last updated <?php echo formatdate(time());?> <a href="javascript:location.reload(true)">Refresh</a></div>
<?php
$courseforums = array();
$forumthreads = array();
$forumname = array();
$lastpost = array();
$forumcourse = array();
if ($_GET['limit'] != 'take') {//teach or all
$query = "SELECT imas_forums.courseid, imas_forums.name, imas_forums.id, imas_forum_threads.id as tid, imas_forum_threads.lastposttime FROM imas_forum_threads ";
$query .= "JOIN imas_forums ON imas_forum_threads.forumid=imas_forums.id ";
$query .= "JOIN imas_teachers ON imas_forums.courseid=imas_teachers.courseid AND imas_teachers.userid=:userid ";
$query .= "LEFT JOIN imas_forum_views as mfv ON mfv.threadid=imas_forum_threads.id AND mfv.userid=:useridB ";
$query .= "WHERE (imas_forum_threads.lastposttime>mfv.lastview OR (mfv.lastview IS NULL)) ";
$query .= "ORDER BY imas_forum_threads.lastposttime DESC LIMIT 30";
$stm = $DBH->prepare($query);
$stm->execute(array(':userid'=>$userid, ':useridB'=>$userid));
while ($line = $stm->fetch(PDO::FETCH_ASSOC)) {
if (!isset($courseforums[$line['courseid']])) {
$courseforums[$line['courseid']] = array();
}
if (!isset( $forumthreads[$line['id']])) {
$forumthreads[$line['id']]= array();
}
//add forum to courseforums list if not already there
if (!in_array($line['id'], $courseforums[$line['courseid']])) {
$courseforums[$line['courseid']][] = $line['id'];
}
$forumcourse[$line['id']] = $line['courseid'];
//add thread to forumthreads list if not already there
if (!in_array($line['tid'], $forumthreads[$line['id']])) {
$forumthreads[$line['id']][] = $line['tid'];
}
$forumname[$line['id']] = Sanitize::stripHtmlTags($line['name']);
$lastpost[$line['tid']] = formatdate($line['lastposttime']);
}
}
if ($_GET['limit'] != 'teach') { //take or all
$query = "SELECT imas_forums.courseid, imas_forums.name, imas_forums.id, imas_forum_threads.id as tid, imas_forum_threads.lastposttime FROM imas_forum_threads ";
$query .= "JOIN imas_forums ON imas_forum_threads.forumid=imas_forums.id ";
$query .= "JOIN imas_students ON imas_forums.courseid=imas_students.courseid AND imas_students.userid=:userid ";
$query .= "LEFT JOIN imas_forum_views as mfv ON mfv.threadid=imas_forum_threads.id AND mfv.userid=:useridB ";
$query .= "WHERE (imas_forum_threads.lastposttime>mfv.lastview OR (mfv.lastview IS NULL)) AND imas_forum_threads.lastposttime<:now ";
$query .= "AND (imas_forum_threads.stugroupid=0 OR imas_forum_threads.stugroupid IN (SELECT stugroupid FROM imas_stugroupmembers WHERE userid=:useridC)) ";
$query .= "ORDER BY imas_forum_threads.lastposttime DESC LIMIT 30";
$stm = $DBH->prepare($query);
$stm->execute(array(':userid'=>$userid, ':useridB'=>$userid, ':now'=>$now, ':useridC'=>$userid));
while ($line = $stm->fetch(PDO::FETCH_ASSOC)) {
if (!isset($courseforums[$line['courseid']])) {
$courseforums[$line['courseid']] = array();
}
if (!isset( $forumthreads[$line['id']])) {
$forumthreads[$line['id']]= array();
}
//add forum to courseforums list if not already there
if (!in_array($line['id'], $courseforums[$line['courseid']])) {
$courseforums[$line['courseid']][] = $line['id'];
}
$forumcourse[$line['id']] = $line['courseid'];
//add thread to forumthreads list if not already there
if (!in_array($line['tid'], $forumthreads[$line['id']])) {
$forumthreads[$line['id']][] = $line['tid'];
}
$forumname[$line['id']] = Sanitize::stripHtmlTags($line['name']);
$lastpost[$line['tid']] = formatdate($line['lastposttime']);
}
}
$coursenames = array();
$cidlist = implode(',',array_keys($courseforums));
//we know $cidlist is all integers from the database, so this is safe
$stm = $DBH->prepare("SELECT id,name FROM imas_courses WHERE id IN (:cidlist)");
$stm->execute(array(':cidlist'=>$cidlist));
while ($row = $stm->fetch(PDO::FETCH_NUM)) {
$coursenames[$row[0]] = $row[1];
}
asort($coursenames);
if (count($lastpost)>0) {
$threadids = implode(',',array_keys($lastpost));
//we know $threadids is all integers from the database, so this is safe
$query = "SELECT imas_forum_posts.*,imas_users.LastName,imas_users.FirstName FROM imas_forum_posts,imas_users ";
$query .= "WHERE imas_forum_posts.userid=imas_users.id AND imas_forum_posts.id IN (:threadids) ORDER BY imas_forum_posts.postdate DESC";
$stm = $DBH->prepare($query);
$stm->execute(array(':threadids'=>$threadids));
$lastforum = '';
$lastcourse = '';
$forumcontent = array();
while ($line = $stm->fetch(PDO::FETCH_ASSOC)) {
if (!isset($forumcontent[$line['forumid']])) {
$forumcontent[$line['forumid']] = '';
}
$course = Sanitize::courseId($forumcourse[$line['forumid']]);
$url = $imasroot."/forums/posts.php?page=-2&cid=$course&forum=" . Sanitize::onlyInt($line['forumid']) . "&thread=" . Sanitize::onlyInt($line['threadid']);
/*$forumcontent[$line['forumid']] .= "<div style='font-size:100%;'>";
$forumcontent[$line['forumid']] .= "<a style='color: blue;' href='$url' target='_new'>{$line['subject']}</a>";
$forumcontent[$line['forumid']] .= " <span style='color: black;'>".Sanitize::encodeStringForDisplay("{$line['LastName']}, {$line['FirstName']}")."</span>";
$forumcontent[$line['forumid']] .= " <span style='color: gray;'>".Sanitize::encodeStringForDisplay($lastpost[$line['threadid']])."</span></div>";
*/
$forumcontent[$line['forumid']] .= "<tr><td><a style='color: blue;' href='$url' target='_new'>" . Sanitize::encodeStringForDisplay($line['subject']) . "</a></td>";
$forumcontent[$line['forumid']] .= "<td><span style='color: black;'>".Sanitize::encodeStringForDisplay("{$line['LastName']}, {$line['FirstName']}")."</span><br/>";
$forumcontent[$line['forumid']] .= "<span style='color: gray;'>".Sanitize::encodeStringForDisplay($lastpost[$line['threadid']])."</span></td></tr>";
}
echo "<div style='font-size:100%; font-weight: 700; background-color: #ccf; margin-below:5px;'>New Posts</div>";
foreach($coursenames as $id=>$name) {
echo "<div style='font-size:100%; color: #606; font-weight: 700; '>" .Sanitize::encodeStringForDisplay($name)."</div>";
echo "<div style='margin-left: 5px;'>";
asort($courseforums[$id]);
foreach($courseforums[$id] as $fid) {
echo "<div style='font-size:100%; color: green;'>" . Sanitize::encodeStringForDisplay($forumname[$fid]) . "</div>";
echo "<table border=0>";
echo $forumcontent[$fid];
echo '</table></div>';
}
echo '</div>';
}
} else {
echo "<div style='font-size:100%; font-weight: 700; background-color: #ccf; margin-below:5px;'>No New Posts</div>";
}
$query = "SELECT imas_msgs.id,imas_msgs.courseid,imas_msgs.title,imas_msgs.senddate,imas_users.FirstName,imas_users.LastName ";
$query .= "FROM imas_msgs,imas_users WHERE imas_msgs.msgto=:msgto AND imas_msgs.msgfrom=imas_users.id ";
$query .= "AND imas_msgs.viewed=0 AND deleted<2 ORDER BY imas_msgs.senddate DESC";
$stm = $DBH->prepare($query);
$stm->execute(array(':msgto'=>$userid));
if ($stm->rowCount()>0) {
echo "<div style='font-size:100%; font-weight: 700; background-color: #ccf; '>New Messages</div>";
echo '<table border=0 cellspacing=2>';
$intable = true;
} else {
echo "<div style='font-size:100%; font-weight: 700; background-color: #ccf; '>No New Messages</div>";
$intable = false;
}
while ($line = $stm->fetch(PDO::FETCH_ASSOC)) {
$n = 0;
if (trim($line['title'])=='') {
$line['title'] = '[No Subject]';
}
while (strpos($line['title'],'Re: ')===0) {
$line['title'] = substr($line['title'],4);
$n++;
}
$line['title'] = Sanitize::encodeStringForDisplay($line['title']);
if ($n==1) {
$line['title'] = 'Re: '.$line['title'];
} else if ($n>1) {
$line['title'] = "Re<sup>$n</sup>: ".$line['title'];
}
$url = "/msgs/viewmsg.php?cid=0&type=msg&msgid=" . Sanitize::onlyInt($line['id']);
echo "<tr><td>";
echo "<a style='color: blue;' href='$url' target='_new'>" . Sanitize::encodeStringForDisplay($line['title']) . "</a></td>";
echo "<td><span style='color: black;'>".Sanitize::encodeStringForDisplay("{$line['LastName']}, {$line['FirstName']}")."</span><br/>";
echo "<span style='color: gray;'>".Sanitize::encodeStringForDisplay(formatdate($line['senddate']))."</span></td></tr>";
}
if ($intable) {
echo '</table>';
}
?>
</body>
</html>