-
Notifications
You must be signed in to change notification settings - Fork 7
/
functions-display.php
executable file
·283 lines (283 loc) · 9.97 KB
/
functions-display.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
<?php
/*
GrinnellPlans - Displayfunctions
What this is: old parts of functions.php separated - in this page, those delaying with display to user controls.
*/
require_once ('Plans.php');
function Redirect($url) {
Header("Location: $url");
}
/**
* All interface objects for displaying pages must implement this (OO) interface.
* @package Interfaces
*/
interface DisplayInterface {
/**
* Displays a page of Plans
*
* Prints the page of Plans as displayed by this interface to stdout
* @param PlansPage $page the page to be displayed
*/
public function display_page(PlansPage $page);
}
function interface_disp_page(PlansPage $page) {
$user = User::get();
if (!$user) { // for guests, use default interface
$interface_path = "interfaces/default/defaultinterface.php";
} else {
$interface_path = $user->interface;
};
require_once($interface_path);
$interface = interface_construct();
if ($interface instanceof DisplayInterface) {
$interface->display_page($page);
}
}
/**
* Populate a Plans page with all the usual stuff
*
* Fills up the PlansPage object with all the elements that are found on every page:
* the main panel (which holds the links, autoreads, finger form, etc), legal footer,
* and so on
*
* @param PlansPage $page The PlansPage object
* @param resource $dbh The database connection
* @param int $idcookie The user's id
*/
function populate_page(PlansPage $page, $dbh, $idcookie) {
$user = User::get();
// get the global stylesheet
$page->stylesheets[] = 'styles/global.css';
// get user stylesheet
$page->stylesheets[] = $user->stylesheet;
$myprivl = get_myprivl();
$page->autoreadpriority = $myprivl;
$mp = new MainPanel();
$page->mainpanel = $mp;
$mp->fingerbox = get_fingerbox();
$mp->linkhome = get_linkhome();
$mp->links = new WidgetList('linkslist', false);
foreach(get_all_user_links($idcookie) as $link) {
$mp->links->append($link);
}
$mp->autoreads = new WidgetList('autoread', true);
foreach ($user->getAutofinger() as $p => $autoreadlist) {
$ar = new AutoRead($p, "setpriv.php?myprivl=$p");
foreach ($autoreadlist as $autoreadlink) {
$ar->append(new PlanLink($autoreadlink));
};
$mp->autoreads->append($ar);
};
$footer = new Footer();
$footer->doyouread = get_just_updated();
$footer->powered_by = get_powered_by();
$footer->legal = new RegularText(get_disclaimer());
$page->footer = $footer;
}
/**
* Populates a Plans page for a guest
*
* Like {@link populate_page()}, but for guest display
*
* @param PlansPage $page The PlansPage object
*/
function populate_guest_page(PlansPage $page) {
//$css=get_item($dbh,"stylesheet","stylesheet","userid", $idcookie);
//if ($css) {$page->stylesheet=$css;}
$css = "styles/default/default.css"; //TODO hardcoding this is ugly
$page->stylesheets = array($css);
$mp = new MainPanel();
$page->mainpanel = $mp;
$mp->fingerbox = get_fingerbox();
$mp->linkhome = get_linkhome();
$mp->links = new WidgetList('linkslist', false);
foreach(get_guest_links() as $link) {
$mp->links->append($link);
}
$mp->autoreads = NULL;
$footer = new Footer();
$footer->doyouread = NULL;
$footer->powered_by = get_powered_by();
$footer->legal = new RegularText(get_disclaimer(), NULL);
$page->footer = $footer;
}
/**
* Get all links
*
* These are all the links that show up for a logged-in user
*
* @return array An array of Hyperlink objects
*/
function get_all_user_links($idcookie) {
$newarr = array();
$newarr[] = new Hyperlink('mainlink_edit', true, 'edit.php', 'Edit Plan');
$newarr[] = new Hyperlink('mainlink_search', true, 'search.php', 'Search Plans');
$newarr[] = new Hyperlink('mainlink_prefs', true, 'customize.php', 'Preferences');
$newarr = array_merge($newarr, get_opt_links($idcookie));
$newarr[] = new Hyperlink('mainlink_logout', true, 'index.php?logout=1', 'Log Out');
return $newarr;
}
/**
* Get the required links for a guest
*
* @return array An array of Hyperlink objects
*/
function get_guest_links() {
$newarr = array();
$newarr[] = new Hyperlink('mainlink_search', true, 'search.php', 'Search Plans');
$newarr[] = new Hyperlink('mainlink_listusers', true, 'listusers.php', 'List Users');
$newarr[] = new Hyperlink('mainlink_logout', true, 'index.php?logout=1', 'Log Out');
return $newarr;
}
/**
* Get the optional links for a user
*
* These are the links that a user may enable or disable.
* Gets the links that the given user has enabled.
*
* @param int $idcookie The user's id
* @return array An array of Hyperlink objects
*/
function get_opt_links($idcookie) {
global $dbh;
if (!isset($dbh)) $dbh = db_connect();
$linkarray = mysqli_query($dbh,"Select avail_links.linkname, avail_links.html_code as html_code, static
From avail_links, opt_links where
opt_links.userid = '$idcookie' and opt_links.linknum = avail_links.linknum");
$newarr = array();
while ($new_row = mysqli_fetch_row($linkarray)) {
if ($new_row[2] == 'yes') {
preg_match("/href=\"([^\"]+)\"/", $new_row[1], $foo);
$href = $foo[1]; // TODO this is silly, let's just store href in the db
$thislink = new Hyperlink('opt_link', false, $href, $new_row[0]);
} else if ($new_row[0] == 'Secrets') {
$count = count_unread_secrets($idcookie);
$thislink = new Hyperlink('mainlink_secrets', true, 'anonymous.php', "Secrets ($count)");
} else if ($new_row[0] == 'Jumble') {
$url = $_SERVER['REQUEST_URI'];
if ((isset($_GET['jumbled']) && $_GET['jumbled'] == 'yes')
|| ((isset($_COOKIE['jumbled']) && $_COOKIE['jumbled'] == 'yes')
&& (isset($_GET['jumbled']) && $_GET['jumbled'] != 'no'))) {
$url = add_param($url, 'jumbled', 'no');
$linktext = 'unjumble';
} else {
$url = add_param($url, 'jumbled', 'yes');
$linktext = 'jumble';
}
$thislink = new Hyperlink('mainlink_jumble', true, $url, $linktext);
} else {
// the forum link needs this, really we just need a better system
preg_match("/href=\"([^\"]+)\"/", $new_row[1], $foo);
$href = $foo[1];
$thislink = new Hyperlink('opt_link', false, $href, $new_row[0]);
}
$newarr[] = $thislink;
}
return $newarr;
}
/**
* Gets the finger form
*
* This is the form that users may use to read a plan by typing a username
*
* @return Form
*/
function get_fingerbox() {
$f = new Form('finger');
$f->action = 'read.php';
$f->method = 'GET';
$item = new TextInput('searchname', NULL);
$f->append($item);
$item = new SubmitInput('Read');
$f->append($item);
return $f;
}
/**
* Get a link to the Plans homepage
* @return Hyperlink
*/
function get_linkhome() {
$l = new Hyperlink('home', true, 'index.php', '');
return $l;
}
/**
* Get a link to the most recently updated plan.
* @return Hyperlink
*/
function get_just_updated() {
// Get the most recently updated plan
$q = Doctrine_Query::create()
->select("userid, username")
->from("Accounts")
->where("username != 'test'")
->andWhereNotIn("userid",
Block::allUserIdsWithBlockingRelationships(User::id()))
->orderBy("changed DESC")
->limit(1);
//return the results of the query
$new_plans = $q->fetchOne();
// Get the appropriate URI for a plan link
$temp = new PlanLink($new_plans["username"]);
// But we want a generic Hyperlink, so it can be styled separately.
return new Hyperlink('justupdatedlink', true, $temp->href,
$new_plans["username"]);
}
function get_powered_by() {
$text = 'Powered by <a href="' . ProjectInformation::projectUrl() . '">GrinnellPlans</a> ' . ProjectInformation::version() . ', an opensource project. File a <a href="' . ProjectInformation::bugReportUrl() . '">bug report</a> after taking a look at the <a href="' . ProjectInformation::recentBugsUrl() . '">recent bugs</a> to avoid duplicates.';
$msg = new RegularText($text);
$msg->identifier = 'poweredby';
return $msg;
}
function wants_secrets($idcookie) {
$wants_secrets = mysqli_query($GLOBALS['dbh'],"Select avail_links.linknum, avail_links.html_code
From avail_links, opt_links where avail_links.linknum = 11 and
opt_links.userid = $idcookie and opt_links.linknum = avail_links.linknum");
if ($row = mysqli_fetch_row($wants_secrets)) {
return 1;
} else {
return 0;
}
}
function count_unread_secrets($idcookie) {
$last_viewed = mysqli_query($GLOBALS['dbh'],"select date from viewed_secrets where userid = $idcookie");
if ($date_row = mysqli_fetch_array($last_viewed)) {
$last = $date_row['date'];
} else {
$last = "000-00-00 00:00:00";
}
$sql = "select count(*) as n from secrets where display = 'yes' and secrets.date_approved > '$last'";
$count = mysqli_query($GLOBALS['dbh'],$sql);
$count_row = mysqli_fetch_array($count);
$count = $count_row['n'];
return $count;
}
function jumble_word($word) {
$l = strlen($word);
if ($l < 4) {
return $word;
}
return $word[0] . (str_shuffle(substr($word, 1, $l - 2))) . $word[$l - 1];
}
function jumble($text) {
ob_start();
preg_match_all('/(<[^>]*>)|([^<>]*)/', $text, $matches);
$c = count($matches[0]);
for ($i = 0;$i < $c;$i++) {
$chunk = $matches[0][$i];
if (preg_match('/</', $chunk)) {
echo $chunk;
} else {
preg_match_all('/(\S+)/', $chunk, $words);
$word_count = count($words[0]);
//echo $word_count;
//echo "\n";
for ($j = 0;$j < $word_count;$j++) {
//echo ($words[0][$j]);
echo (jumble_word($words[0][$j]));
echo "\n";
}
}
}
return ob_get_clean();
}
?>