Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 895 lines (819 sloc) 26.27 kB
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
1 <?php
2
3 define('COOKIE_SESSID','ureddit_sessid');
a52cf81 @anastasds added custom email header denoting internal message id
anastasds authored
4 define('PREFIX','');
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
5 define('USE_MARKDOWN','true');
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
6 define('SRVDOMAIN','http' . (isset($_SERVER['HTTPS']) && strlen($_SERVER['HTTPS']) > 0 ? "s" : "") . '://uofreddit.com');
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
7
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
8 function latest_blog_post($dbpdo)
9 {
5030058 @anastasds integrating postfix and wordpress based on config constant
anastasds authored
10 if(config::wordpress)
11 {
12 $res = $dbpdo->query("SELECT `post_title`, `post_name`, `post_date` FROM `wp_posts` WHERE `post_status`='publish' AND `post_type`='post' ORDER BY `ID` DESC LIMIT 1", array());
13 $year = date("Y", strtotime($res[0]['post_date']));
14 $month = date("m", strtotime($res[0]['post_date']));
15 $day = date("d", strtotime($res[0]['post_date']));
16 return array('title' => $res[0]['post_title'], 'url' => '/blog/' . $year . '/' . $month . '/' . $day . '/' . $res[0]['post_name']);
17 }
18 else
19 return array('title' => "Install WordPress or remove this box.", 'url' => "http://wordpress.com");
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
20 }
21
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
22 function latest_commit($dbpdo)
f967762 @anastasds github integration
anastasds authored
23 {
2391527 @anastasds removed bloat code for accessing config settings
anastasds authored
24 if(config::use_memcache)
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
25 {
26 if(!($val = $dbpdo->memcache->get('latest_commit')))
27 {
28 $fdata = new SimpleXMLElement(stripslashes(file_get_contents("https://github.com/ureddit/ureddit/commits/master.atom")), true);
29 //$fdata = new SimpleXMLElement(file_get_contents("github.txt"), true);
30 $val = array('title' => '' . $fdata->entry[0]->title, 'url' => '' . $fdata->entry[0]->link[0]['href']);
31 $dbpdo->memcache_set('latest_commit',$val);
32 }
33 }
34 else
35 {
36 $fdata = new SimpleXMLElement(stripslashes(file_get_contents("https://github.com/ureddit/ureddit/commits/master.atom")), true);
37 //$fdata = new SimpleXMLElement(file_get_contents("github.txt"), true);
38 $val = array('title' => $fdata->entry[0]->title, 'url' => $fdata->entry[0]->link[0]['href']);
39 }
40
41 return $val;
f967762 @anastasds github integration
anastasds authored
42 }
43
18dfdbe @anastasds added redirection from old class URLs to new ones
anastasds authored
44 function translate_class_id($dbpdo,$old_id)
45 {
46 $translation = $dbpdo->query("SELECT new_id FROM class_id_translation WHERE old_id = ?", array($old_id));
47 if(count($translation) > 0)
48 return $translation[0]['new_id'];
afd11c3 @anastasds redirecting old class pages now only works if the new class still exists
anastasds authored
49 return false;
18dfdbe @anastasds added redirection from old class URLs to new ones
anastasds authored
50 }
51
df71705 @anastasds implemented voting
anastasds authored
52 function votebox($class, $user = false)
53 {
54 $score = $class->calculate_score();
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
55
df71705 @anastasds implemented voting
anastasds authored
56 if($user !== false)
57 {
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
58 if(isset($user->votes['downvoted']) && in_array($class->id, $user->votes['downvoted']))
df71705 @anastasds implemented voting
anastasds authored
59 {
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
60 echo '<img src="' . SRVDOMAIN . PREFIX . '/img/down-filled.png" alt="-1\'d" class="downvoted" onclick="$.post(\'' . PREFIX . '/vote.php\', {action: \'remove\', id: \'' . $class->id . '\'}, function(response) {$(\'#class' . $class->id . ' > .content > .voting\').html(response);})">';
df71705 @anastasds implemented voting
anastasds authored
61 }
62 else
63 {
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
64 echo '<img src="' . SRVDOMAIN . PREFIX . '/img/down.png" alt="-1" class="downvote" onclick="$.post(\'' . PREFIX . '/vote.php\', {action: \'downvote\', id: \'' . $class->id . '\'}, function(response) {$(\'#class' . $class->id . ' > .content > .voting\').html(response);})">';
df71705 @anastasds implemented voting
anastasds authored
65 }
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
66 if(isset($user->votes['upvoted']) && in_array($class->id, $user->votes['upvoted']))
df71705 @anastasds implemented voting
anastasds authored
67 {
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
68 echo '<img src="' . SRVDOMAIN . PREFIX . '/img/up-filled.png" alt="+1\'d" class="upvoted" onclick="$.post(\'' . PREFIX . '/vote.php\', {action: \'remove\', id: \'' . $class->id . '\'}, function(response) {$(\'#class' . $class->id . ' > .content > .voting\').html(response);})">';
df71705 @anastasds implemented voting
anastasds authored
69 }
70 else
71 {
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
72 echo '<img src="' . SRVDOMAIN . PREFIX . '/img/up.png" alt="+1" class="upvote" onclick="$.post(\'' . PREFIX . '/vote.php\', {action: \'upvote\', id: \'' . $class->id . '\'}, function(response) {$(\'#class' . $class->id . ' > .content > .voting\').html(response);})">';
df71705 @anastasds implemented voting
anastasds authored
73 }
74 }
75 else
76 {
015c8fe @anastasds changed class page, URL, teacher links to buttons; optimized as per G…
anastasds authored
77 echo '<a href="' . PREFIX . '/login"><img src="' . SRVDOMAIN . PREFIX . '/img/down.png" alt="-1" class="downvote"></a>';
78 echo '<a href="' . PREFIX . '/login"><img src="' . SRVDOMAIN . PREFIX . '/img/up.png" alt="+1" class="upvote"></a>';
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
79
df71705 @anastasds implemented voting
anastasds authored
80 }
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
81 echo $score . '&nbsp;';
df71705 @anastasds implemented voting
anastasds authored
82 }
83
33c789c @anastasds fixed functionality-breaking bugs, optimized for page load time
anastasds authored
84 function signup_button($user, $class_id)
c3e32ac @anastasds added signup button
anastasds authored
85 {
86 /*
87 class statuses:
88
89 0 cancelled
90 1 has not begun, open for signups
91 2 has not begun, closed to signups
92 3 running, open for signups
93 4 running, closed to signups
94 5 finished
95 */
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
96 if($user === false)
97 $class = new course($user, $class_id);
98 else
99 $class = new course($user->dbpdo, $class_id);
5a06750 @ureddit debugging
authored
100 $status = $class->get_attribute_value('status');
c3e32ac @anastasds added signup button
anastasds authored
101 echo "<div id=\"button" . $class->id . "\">\n";
102
4581030 @anastasds memecached support for object::get_parents and object::get_children
anastasds authored
103 if(!logged_in())
c3e32ac @anastasds added signup button
anastasds authored
104 {
105 $text = array("0" => "canceled", "1" => "+add", "2" => "closed", "3" => "+add", "4" => "closed", "5" => "finished");
106 ?>
107 <div class="signup-button">
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
108 <a href="<?=PREFIX ?>/login"><button class="button-add"><?=$text[$status] ?></button></a>
c3e32ac @anastasds added signup button
anastasds authored
109 </div></div>
110 <?php
111 return;
112 }
113
33c789c @anastasds fixed functionality-breaking bugs, optimized for page load time
anastasds authored
114 if(!$user->is_taking_class($class->id)) // if student is not in class
c3e32ac @anastasds added signup button
anastasds authored
115 {
116 if(!$user->is_teaching_class($class->id))
117 {
5a06750 @ureddit debugging
authored
118 if($status == "1" || $status == "3")
c3e32ac @anastasds added signup button
anastasds authored
119 {
120 ?>
121 <div class="signup-button">
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
122 <a onclick="$.get('<?=PREFIX ?>/enroll.php',{id: '<?=$class->id ?>'}, function(data) { $('#button<?=$class->id ?>').html(data) });">
123 <button class="button-add">
c3e32ac @anastasds added signup button
anastasds authored
124 +add
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
125 </button>
c3e32ac @anastasds added signup button
anastasds authored
126 </a>
127 </div>
128 <?php
5a06750 @ureddit debugging
authored
129 } elseif($status == "5") {
c3e32ac @anastasds added signup button
anastasds authored
130 ?>
131 <div class="signup-button">
132 <a class="link-signup-button">
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
133 <button class="button-add">
c3e32ac @anastasds added signup button
anastasds authored
134 finished
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
135 </button>
c3e32ac @anastasds added signup button
anastasds authored
136 </a>
137 </div>
138 <?php
b461b71 @anastasds fixed canceled classes being marked as closed
anastasds authored
139 } elseif($status == "2" || $status == "4") {
c3e32ac @anastasds added signup button
anastasds authored
140 ?>
141 <div class="signup-button">
142 <a class="link-signup-button">
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
143 <button class="button-add">
c3e32ac @anastasds added signup button
anastasds authored
144 closed
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
145 </button>
c3e32ac @anastasds added signup button
anastasds authored
146 </a>
147 </div>
148 <?php
b461b71 @anastasds fixed canceled classes being marked as closed
anastasds authored
149 } elseif($status == "0") {
150 ?>
151 <div class="signup-button">
152 <button class="button-add">
153 canceled
154 </button>
155 </div>
156 <?php
c3e32ac @anastasds added signup button
anastasds authored
157 }
158 }
159 else
160 {
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
161 ?>
162 <div class="teacher-button">
c3e32ac @anastasds added signup button
anastasds authored
163 <a href="<?=PREFIX ?>/teachers/" class="link-signup-button">
164 teacher
165 </a>
166 </div>
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
167 <?php
c3e32ac @anastasds added signup button
anastasds authored
168 }
169 }
170 else
171 {
172 ?>
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
173 <div class="signup-button">
c3e32ac @anastasds added signup button
anastasds authored
174 <a onclick="$.get('<?=PREFIX ?>/drop_class.php',{id: '<?=$class->id ?>'}, function(data) { $('#button<?=$class->id ?>').html(data) });" class="link-signup-button">
223e48d @anastasds redesigned add/drop/etc buttons
anastasds authored
175 <button class="button-added">
176 enrolled
177 </button>
c3e32ac @anastasds added signup button
anastasds authored
178 </a>
179 </div>
180 <?
181 }
182 echo "</div>\n";
183 }
184
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
185 function get_feed($user)
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
186 {
187 $items = array();
186cfb2 @anastasds fixed activity logging bug
anastasds authored
188 $actions = $user->dbpdo->query("SELECT * FROM `activity` WHERE `parent_id` = ? ORDER BY `datetime` DESC LIMIT 25", array($user->id));
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
189 foreach($actions as $action)
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
190 {
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
191 if(is_null($action['child_id']))
1acd8fe @anastasds minor tweaks
anastasds authored
192 $items[] = '<li><strong>' . $user->value . '</strong> ' . $action['action'] . '<br><small><em>' . $action['datetime'] . '</em></small></li>';
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
193 else
194 {
195 $object = new object($user->dbpdo, $action['child_id']);
196 $items[] = '<li><strong>' . $user->value . '</strong> ' . $action['action'] . ' <strong>' . $object->value . '</strong><br><small><em>' . $action['datetime'] . '</em></small></li>';
197 }
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
198 }
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
199 if(empty($items))
200 $items[] = "<em>no user activity found</em>";
201 return $items;
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
202 }
203
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
204 function display_schedule($user)
205 {
206 $user->get_schedule();
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
207
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
208 $categories = array();
02a06a9 @anastasds user page now displays categories in alphabetical order
anastasds authored
209 $sorted_categories = array();
4581030 @anastasds memecached support for object::get_parents and object::get_children
anastasds authored
210
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
211 foreach($user->schedule as $class_id)
212 {
213 $class = new course($user->dbpdo, $class_id);
d310130 @anastasds user schedules no longer display empty categories (and are free of ot…
anastasds authored
214 if($class->get_attribute_value('status') == '0')
dbdb5af @anastasds user schedules no longer list empty categories
anastasds authored
215 continue;
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
216 $class->get_categories();
217 foreach($class->categories as $category_id)
218 $categories[$category_id][] = $class;
219 }
02a06a9 @anastasds user page now displays categories in alphabetical order
anastasds authored
220 foreach($categories as $category_id => &$classes)
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
221 {
222 $category = new category($user->dbpdo, $category_id);
02a06a9 @anastasds user page now displays categories in alphabetical order
anastasds authored
223 $category_objects[$category_id] = $category;
224 $sorted_categories[$category_id] = $category->value;
225 }
226
227 asort($sorted_categories);
228
229 foreach($sorted_categories as $category_id => $category_value)
230 {
4581030 @anastasds memecached support for object::get_parents and object::get_children
anastasds authored
231 ?>
27d6587 @anastasds implemented activity feeds and activity logging; finished porting reg…
anastasds authored
232 <div class="category">
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
233 <div class="content">
234 <?php
235 echo $category_value;
236 foreach($categories[$category_id] as $class)
dbdb5af @anastasds user schedules no longer list empty categories
anastasds authored
237 $class->display_with_container();
744760a @anastasds class page is tentatively done, social bar finished with the addition…
anastasds authored
238 ?>
239 </div>
24f6678 @anastasds supporting function for displaying user schedules
anastasds authored
240 </div>
241 <?php
242 }
243 }
244
0860188 @ureddit teacher index page is complete
authored
245 function list_teacher_classes($user)
246 {
247 $user->get_taught_classes();
248 $categories = array();
249 foreach($user->teaching as $class_id)
250 {
251 $class = new course($user->dbpdo, $class_id);
252 $class->get_categories();
253 foreach($class->categories as $category_id)
254 $categories[$category_id][] = $class;
255 }
256 foreach($categories as $category_id => $classes)
257 {
258 $category = new category($user->dbpdo, $category_id);
259 ?>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
260 <div id="category<?=$category->id ?>" class="category">
261 <div class="content">
0860188 @ureddit teacher index page is complete
authored
262 <?=$category->value ?>
263 <?php
264 foreach($classes as $class)
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
265 $class->display_with_container(true);
0860188 @ureddit teacher index page is complete
authored
266 ?>
267 </div>
268 </div>
269 <?php
270 }
271 }
272
278bbc2 @ureddit debugged inbox / outbox / orangered envelope, PM system appears to be…
authored
273 function object_type_value_to_id($dbpdo, $type, $value)
274 {
275 return $dbpdo->query("SELECT `id` FROM `objects` WHERE `type` = ? AND `value` = ?",
276 array(
277 $type,
278 $value
279 ));
280 }
281
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
282 function latest_reddit_post($dbpdo)
db02d13 @anastasds new skeleton for pages based on html5boilerplate and part of the soci…
anastasds authored
283 {
2391527 @anastasds removed bloat code for accessing config settings
anastasds authored
284 if(config::use_memcache)
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
285 {
286 if(!($val = $dbpdo->memcache_get('latest_reddit_post')))
287 {
fbda438 @anastasds fixed "latest reddit post" bug
anastasds authored
288 $json = json_decode(file_get_contents('/srv/http/ureddit.com/public_html/reddit.json'), true);
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
289 $val = array('url' => 'http://reddit.com' . $json['data']['children'][0]['data']['permalink'], 'title' => $json['data']['children'][0]['data']['title']);
290 $dbpdo->memcache_set('latest_reddit_post', $val);
291 }
292 }
293 else
294 {
fbda438 @anastasds fixed "latest reddit post" bug
anastasds authored
295 $json = json_decode(file_get_contents('/srv/http/ureddit.com/public_html/reddit.json'), true);
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
296 $val = array('url' => 'http://reddit.com' . $json['data']['children'][0]['data']['permalink'], 'title' => $json['data']['children'][0]['data']['title']);
297 }
298
299 return $val;
db02d13 @anastasds new skeleton for pages based on html5boilerplate and part of the soci…
anastasds authored
300 }
301
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
302 function latest_tweet($dbpdo)
db02d13 @anastasds new skeleton for pages based on html5boilerplate and part of the soci…
anastasds authored
303 {
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
304 $config = $dbpdo->config;
2391527 @anastasds removed bloat code for accessing config settings
anastasds authored
305 if(config::use_memcache)
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
306 {
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
307 if(!($val = $dbpdo->memcache_get('latest_tweet')))
308 {
309 try
310 {
311 $t = new Twitter($config::twitterConsumerKey, $config::twitterConsumerSecret, $config::twitterAccessToken, $config::twitterAccessTokenSecret);
312 $latest = $t->load(Twitter::ME,1);
313 $val = array('text' => Twitter::clickable($latest->status->text), 'url' => 'http://twitter.com/uofreddit/status/' . $latest->status->id);
314 $dbpdo->memcache_set('latest_tweet',$val,60);
315 }
316 catch (TwitterException $e)
317 {
318 return array('text' => 'Error fetching tweets. Click to go to the @uofreddit Twitter feed.', 'url' => 'http://twitter.com/uofreddit');
319 }
320 }
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
321 }
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
322 else
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
323 {
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
324 try
325 {
326 $t = new Twitter($config::twitterConsumerKey, $config::twitterConsumerSecret, $config::twitterAccessToken, $config::twitterAccessTokenSecret);
327 $latest = $t->load(Twitter::ME,1);
328 $val = array('text' => Twitter::clickable($latest->status->text), 'url' => 'http://twitter.com/uofreddit/status/' . $latest->status->id);
329 memcache_set('latest_tweet',$val,300);
330 }
331 catch (TwitterException $e)
332 {
333 return array('text' => 'Error fetching tweets. Click to go to the @uofreddit Twitter feed.', 'url' => 'http://twitter.com/uofreddit');
334 }
26c6b38 @anastasds added archive section and ported password recovery page (plus various…
anastasds authored
335 }
4ee4a5b @anastasds added logging data for class stats (and some performance tweaks)
anastasds authored
336 return $val;
db02d13 @anastasds new skeleton for pages based on html5boilerplate and part of the soci…
anastasds authored
337 }
338
8531573 @anastasds added class creation page and fixed a nasty bug where object::save wo…
anastasds authored
339 function tweet($config,$status)
340 {
3c4e596 @anastasds edit class page stopped working when class object::define_attribute w…
anastasds authored
341 $t = new Twitter($config::twitterConsumerKey, $config::twitterConsumerSecret, $config::twitterAccessToken, $config::twitterAccessTokenSecret);
8531573 @anastasds added class creation page and fixed a nasty bug where object::save wo…
anastasds authored
342 $t->send($status);
343 }
344
345 function category_dropdown($dbpdo, $name, $selected_val = "")
346 {
347 $categories = $dbpdo->query("SELECT * FROM `objects` WHERE `type` = ? ORDER BY `value` ASC", array('category'));
348 ?><select name="<?=$name ?>" class="teach"><?php
349 foreach($categories as $cat)
350 {
351 ?><option <?=($cat['id'] == $selected_val ? "SELECTED" : "") ?> value="<?=$cat['id'] ?>"><?=$cat['value'] ?></option><?php
352 }
353 ?></select><?
354 }
355
bde8535 @anastasds fixed inbox/outbox; had to add LIMIT clause to object::get_parents an…
anastasds authored
356 function num_sent_messages($user)
357 {
358 $sent = $user->dbpdo->query("SELECT COUNT(*) FROM associations WHERE type = ? AND parent_id = ?", array('message','$user->id'));
359 return $sent[0]['COUNT(*)'];
360 }
361
362 function num_messages($user)
363 {
364 $num = $user->dbpdo->query("SELECT COUNT(*) FROM associations WHERE type = ? AND child_id = ?", array('message','$user->id'));
365 return $num[0]['COUNT(*)'];
366 }
f3519c0 @anastasds fixed login, issue with user::__construct preventing its config objec…
anastasds authored
367
368 // this function is not original, it was found online
369 // I have lost my record of who the author was; if found,
370 // I will give credit
371 function encrypt($toEncrypt,$privatekey)
372 {
373 $priv = openssl_pkey_get_private ($privatekey);
374
375 $toEncrypt = unpack('H*', $toEncrypt);
376 $toEncrypt = $toEncrypt[1];
377
378 $result = "";
379
380 while(strlen($toEncrypt)%16 != 0){
381 $toEncrypt .= "00";
382 }
383
384 $iv = "1234567812345678";
385 for($i = 0; $i < strlen($toEncrypt); $i+=16){
386 $p = substr($toEncrypt, $i, 16);
387 $x = $p ^ $iv;
388
389 if(!openssl_private_encrypt($x, $e, $priv, OPENSSL_NO_PADDING)){
390 throw new Exception(openssl_error_string());
391 }
392
393 $iv = $e ^ $p;
394
395 $result .= $e;
396 }
397
398 $result = unpack('H*', $result);
399 return $result[1];
400 }
401
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
402 function post($name, $default = "")
403 {
404 if(!empty($_POST) && isset($_POST[$name]))
405 return htmlspecialchars(stripslashes($_POST[$name]));
406 return htmlspecialchars(stripslashes($default));
407 }
408
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
409 function display_messages($user, $offset = 0, $limit=15)
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
410 {
411 $found = 0;
412 $unread = array();
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
413 $user->get_inbox($offset, $limit*2);
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
414
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
415 for($i = 0; $i < count($user->inbox)/2; $i++)
416 {
835b409 @anastasds fixed mass messaging, additional info when displaying mass messages, …
anastasds authored
417 if($user->get_object_type($user->inbox[2*$i]['parent_id']) == 'class')
418 {
419 $sender = new user($user->dbpdo, $user->inbox[2*$i]['parent_id']);
420 if($sender->type == 'class')
421 {
422 $author = $sender->dbpdo->query("SELECT value FROM association_attributes WHERE association_id = ? AND type = ?",
423 array(
424 $user->inbox[2*$i]['association_id'],
425 'author'
426 ));
427 $author = new user($sender->dbpdo, $author[0]['value']);
428 }
429
430 $found = 1;
431 }
432 else
433 {
434 $sender = new user($user->dbpdo, $user->inbox[2*$i]['parent_id']);
435 $found = 1;
436 }
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
437 ?>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
438 <div class="message">
439 <div class="content">
440 <div class="subject"><?=$user->inbox[2*$i]['value'] ?></div>
441 <div class="body"><?=$user->process_text($user->inbox[2*$i+1]['value']) ?></div>
442 <div class="signature">from <strong><?=($sender->type == 'class' ? $author->value . '</strong> (regarding class <strong><a href="' . PREFIX . "/class/" . $sender->id . '">' . $sender->value . '</a></strong>)' : $sender->value) . '</strong>'?> at <?=$user->inbox[2*$i]['creation'] ?> [<a href="<?=PREFIX ?>/user/<?=($sender->type == 'class' ? $author->value : $sender->value) ?>" class="link-class-desc">reply</a>]</div>
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
443 </div>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
444 </div>
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
445 <?php
446 }
447
448 if($found == 0)
449 {
450 ?>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
451 <div class="message">
452 <p><em>you have no new messages</em></p>
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
453 </div>
454 <?php
455 }
67f66a9 @anastasds fixed bug preventing old messages from being marked as read
anastasds authored
456 $user->dbpdo->query("UPDATE `associations` SET `type` = ? WHERE type = ? AND child_id = ?",
457 array(
458 'read_mass_message',
459 'unread_mass_message',
460 $user->id
461 ));
462
463 $user->dbpdo->query("UPDATE `associations` SET `type` = ? WHERE type = ? AND child_id = ?",
464 array(
465 'read_message',
466 'unread_message',
467 $user->id
468 ));
469
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
470 }
471
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
472 function display_sent_messages($user, $offset = 0, $limit=15)
473 {
474 $found = 0;
475 $unread = array();
476 $user->get_outbox($offset, $limit*2);
477 for($i = 0; $i < count($user->outbox)/2; $i++)
478 {
d65d8d9 @anastasds outbox now displays receipient's name rather than sender's
anastasds authored
479 $receipient = new user($user->dbpdo, $user->outbox[2*$i]['child_id']);
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
480 $found = 1;
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
481 ?>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
482 <div class="message">
483 <div class="content">
484 <div class="subject"><?=$user->outbox[2*$i]['value'] ?></div>
485 <div class="body"><?=$user->process_text($user->outbox[2*$i+1]['value']) ?></div>
486 <div class="signature">to <strong><?=$receipient->value ?></strong> at <?=$user->outbox[$i]['creation'] ?> [<a href="<?=PREFIX ?>/user/<?=$recepient->value ?>" class="link-class-desc">reply</a>]</div>
487 </div>
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
488 </div>
489 <?php
363d3d8 @anastasds reworked messaging system, increased/fixed memcache integration, bugf…
anastasds authored
490 }
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
491
492 if($found == 0)
493 {
494 ?>
18dc0b1 @anastasds v3 frontend done? time to add features, it seems
anastasds authored
495 <div class="message">
496 <p><em>you have no new messages</em></p>
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
497 </div>
498 <?php
499 }
500 }
501
502 function generate_random_password()
503 {
504 $pass = "";
505 for($i = 0; $i < 16; $i++)
506 $pass .= chr((rand(0,5) < 5 ? (int) rand(65,90) : (int)rand(97,122)));
507 return $pass;
508 }
509
7d0cf8e @anastasds HTTP 301 redirect from old class pages to new
anastasds authored
510 function send_user_to($place,$domain="ureddit.com",$http_code = NULL)
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
511 {
512 $s = isset($_SERVER['HTTPS']) && strlen($_SERVER['HTTPS']) > 0 ? "s" : "";
7d0cf8e @anastasds HTTP 301 redirect from old class pages to new
anastasds authored
513 if($http_code !== NULL)
514 header("HTTP/1.1 $http_code");
f7f77f9 @anastasds fixed redirection bug
anastasds authored
515 header("Location: http$s://" . str_replace('universityofreddit','ureddit',$_SERVER['SERVER_NAME']) . PREFIX . $place);
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
516 die();
517 }
518
519 function process($text)
520 {
521 if(USE_MARKDOWN == "true")
522 return Markdown(htmlspecialchars(stripslashes($text)));
523 return nl2br(htmlspecialchars(stripslashes($text)));
524 }
525
526 function logged_in()
527 {
528 if(!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != "true")
529 return false;
530 return true;
531 }
532
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
533 function logout($dbpdo)
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
534 {
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
535 $dbpdo->query("DELETE FROM `sessions` WHERE `object_id` = ?", array($dbpdo->session('user_id')));
222ab53 @anastasds fixed cookies to make logins persistent
anastasds authored
536 setcookie(COOKIE_SESSID,"",time()-60*60*24);
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
537
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
538 $_SESSION = array();
bd88092 @anastasds optimized user pages (and fixed some bugs)
anastasds authored
539 session_destroy();
540 send_user_to("/");
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
541 }
542
543 function cookie_exists()
544 {
545 if(isset($_COOKIE[COOKIE_SESSID]))
546 return true;
547 return false;
548 }
549
550 function login($user)
551 {
552 $_SESSION['logged_in'] = "true";
553 $_SESSION['user_id'] = $user->id;
554 $_SESSION['username'] = $user->value;
222ab53 @anastasds fixed cookies to make logins persistent
anastasds authored
555 setcookie(COOKIE_SESSID,session_id(),time()-60*60*24);
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
556 }
557
a52cf81 @anastasds added custom email header denoting internal message id
anastasds authored
558 function send_email($from, $to, $subject, $message, $internalid = false)
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
559 {
560
561 $fHeaders = "To: " . $to . "\n";
562 $fHeaders .= "From: " . $from . "\n";
563
564 $fHeaders .= "Subject: " . encode_header ($subject) . "\n";
565 $fHeaders .= "MIME-Version: 1.0\n";
a52cf81 @anastasds added custom email header denoting internal message id
anastasds authored
566 if($internalid !== false)
567 $fHeaders .= "Association: $internalid\r\n";
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
568 //$fHeaders .= "Content-Type: text/plain; charset=utf-8\n";
569 //$fHeaders .= "Content-Transfer-Encoding: 8bit\n";
570 $fHeaders .= "Content-Type: text/html; charset=utf-8\r\n";
571
572 $fHeaders .= "<html><body>\n";
573 $fHeaders .= $message;
574 $fHeaders .= "</body></html>\n";
575
576 $errno = "0";
577 $errstr = "0";
578 $fh = @fsockopen ('localhost', '25', $errno, $errstr, '30');
579 if ($fh)
580 {
581 $res = smtp_get_response($fh);
582 fputs ($fh, "EHLO localhost\r\n");
583 $res = smtp_get_response($fh);
584 fputs ($fh, "MAIL FROM:<$from>\r\n");
585 $res = smtp_get_response($fh);
586 fputs ($fh, "RCPT TO:<$to>\r\n");
587 $res = smtp_get_response($fh);
588 fputs ($fh, "DATA\r\n");
589 $res = smtp_get_response($fh);
590 fputs ($fh, "$fHeaders\r\n.\r\n");
591 $res = smtp_get_response($fh);
592 fputs ($fh, "QUIT\r\n");
593 $res = smtp_get_response($fh);
594 fclose ($fh);
595 return true;
596 }
597 else
598 return false;
599 }
600
601 function encode_header($string, $default_charset = "utf-8")
602 {
603 if (strtolower ($default_charset) == 'iso-8859-1')
604 {
605 $string = str_replace ("\240",' ',$string);
606 }
607
608 $j = strlen ($string);
609 $max_l = 75 - strlen ($default_charset) - 7;
610 $aRet = array ();
611 $ret = '';
612 $iEncStart = $enc_init = false;
613 $cur_l = $iOffset = 0;
614
615 for ($i = 0; $i < $j; ++$i)
616 {
617 switch ($string{$i})
618 {
619 case '=':
620 case '<':
621 case '>':
622 case ',':
623 case '?':
624 case '_':
625 if ($iEncStart === false)
626 {
627 $iEncStart = $i;
628 }
629 $cur_l+=3;
630 if ($cur_l > ($max_l-2))
631 {
632 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
633 $aRet[] = "=?$default_charset?Q?$ret?=";
634 $iOffset = $i;
635 $cur_l = 0;
636 $ret = '';
637 $iEncStart = false;
638 }
639 else
640 {
641 $ret .= sprintf ("=%02X",ord($string{$i}));
642 }
643 break;
644 case '(':
645 case ')':
646 if ($iEncStart !== false)
647 {
648 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
649 $aRet[] = "=?$default_charset?Q?$ret?=";
650 $iOffset = $i;
651 $cur_l = 0;
652 $ret = '';
653 $iEncStart = false;
654 }
655 break;
656 case ' ':
657 if ($iEncStart !== false)
658 {
659 $cur_l++;
660 if ($cur_l > $max_l)
661 {
662 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
663 $aRet[] = "=?$default_charset?Q?$ret?=";
664 $iOffset = $i;
665 $cur_l = 0;
666 $ret = '';
667 $iEncStart = false;
668 }
669 else
670 {
671 $ret .= '_';
672 }
673 }
674 break;
675 default:
676 $k = ord ($string{$i});
677 if ($k > 126)
678 {
679 if ($iEncStart === false)
680 {
681 // do not start encoding in the middle of a string, also take the rest of the word.
682 $sLeadString = substr ($string,0,$i);
683 $aLeadString = explode (' ',$sLeadString);
684 $sToBeEncoded = array_pop ($aLeadString);
685 $iEncStart = $i - strlen ($sToBeEncoded);
686 $ret .= $sToBeEncoded;
687 $cur_l += strlen ($sToBeEncoded);
688 }
689 $cur_l += 3;
690 // first we add the encoded string that reached it's max size
691 if ($cur_l > ($max_l-2))
692 {
693 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
694 $aRet[] = "=?$default_charset?Q?$ret?= ";
695 $cur_l = 3;
696 $ret = '';
697 $iOffset = $i;
698 $iEncStart = $i;
699 }
700 $enc_init = true;
701 $ret .= sprintf ("=%02X", $k);
702 }
703 else
704 {
705 if ($iEncStart !== false)
706 {
707 $cur_l++;
708 if ($cur_l > $max_l)
709 {
710 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
711 $aRet[] = "=?$default_charset?Q?$ret?=";
712 $iEncStart = false;
713 $iOffset = $i;
714 $cur_l = 0;
715 $ret = '';
716 }
717 else
718 {
719 $ret .= $string{$i};
720 }
721 }
722 }
723 break;
724 }
725 }
726 if ($enc_init)
727 {
728 if ($iEncStart !== false)
729 {
730 $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
731 $aRet[] = "=?$default_charset?Q?$ret?=";
732 }
733 else
734 {
735 $aRet[] = substr ($string,$iOffset);
736 }
737 $string = implode ('',$aRet);
738 }
739 return $string;
740 }
741
742 function pacrypt ($pw, $pw_db="")
743 {
744 $pw = stripslashes($pw);
745 $password = "";
746 $salt = "uofr!336";
747
748 $split_salt = preg_split ('/\$/', $pw_db);
749 if (isset ($split_salt[2])) {
750 $salt = $split_salt[2];
751 }
752 $password = md5crypt ($pw, $salt);
753 $password = escape_string ($password);
754 return $password;
755 }
756
757 function escape_string ($string)
758 {
3c4e596 @anastasds edit class page stopped working when class object::define_attribute w…
anastasds authored
759
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
760 // if the string is actually an array, do a recursive cleaning.
761 // Note, the array keys are not cleaned.
762 if(is_array($string)) {
763 $clean = array();
764 foreach(array_keys($string) as $row) {
765 $clean[$row] = escape_string($string[$row]);
766 }
767 return $clean;
768 }
769 if (get_magic_quotes_gpc ())
770 {
771 $string = stripslashes($string);
772 }
773 if (!is_numeric($string))
774 {
775 $escaped_string = ($string);
776 }
777 else
778 {
779 $escaped_string = $string;
780 }
781 return $escaped_string;
782 }
783
784 function md5crypt ($pw, $salt="", $magic="")
785 {
786 $MAGIC = "$1$";
787
788 if ($magic == "") $magic = $MAGIC;
789 $slist = explode ("$", $salt);
790 if ($slist[0] == "1") $salt = $slist[1];
791
792 $salt = substr ($salt, 0, 8);
793 $ctx = $pw . $magic . $salt;
794 $final = hex2bin (md5 ($pw . $salt . $pw));
795
796 for ($i=strlen ($pw); $i>0; $i-=16)
797 {
798 if ($i > 16)
799 {
800 $ctx .= substr ($final,0,16);
801 }
802 else
803 {
804 $ctx .= substr ($final,0,$i);
805 }
806 }
807 $i = strlen ($pw);
808
809 while ($i > 0)
810 {
811 if ($i & 1) $ctx .= chr (0);
812 else $ctx .= $pw[0];
813 $i = $i >> 1;
814 }
815 $final = hex2bin (md5 ($ctx));
816
817 for ($i=0;$i<1000;$i++)
818 {
819 $ctx1 = "";
820 if ($i & 1)
821 {
822 $ctx1 .= $pw;
823 }
824 else
825 {
826 $ctx1 .= substr ($final,0,16);
827 }
828 if ($i % 3) $ctx1 .= $salt;
829 if ($i % 7) $ctx1 .= $pw;
830 if ($i & 1)
831 {
832 $ctx1 .= substr ($final,0,16);
833 }
834 else
835 {
836 $ctx1 .= $pw;
837 }
838 $final = hex2bin (md5 ($ctx1));
839 }
840 $passwd = "";
841 $passwd .= to64 (((ord ($final[0]) << 16) | (ord ($final[6]) << 8) | (ord ($final[12]))), 4);
842 $passwd .= to64 (((ord ($final[1]) << 16) | (ord ($final[7]) << 8) | (ord ($final[13]))), 4);
843 $passwd .= to64 (((ord ($final[2]) << 16) | (ord ($final[8]) << 8) | (ord ($final[14]))), 4);
844 $passwd .= to64 (((ord ($final[3]) << 16) | (ord ($final[9]) << 8) | (ord ($final[15]))), 4);
845 $passwd .= to64 (((ord ($final[4]) << 16) | (ord ($final[10]) << 8) | (ord ($final[5]))), 4);
846 $passwd .= to64 (ord ($final[11]), 2);
847 return "$magic$salt\$$passwd";
848 }
849
1fe143c @anastasds hex2bin was added in PHP 5.4.0 so the manual definition is obsolete
anastasds authored
850 /*
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
851 function hex2bin ($str)
852 {
853 $len = strlen ($str);
854 $nstr = "";
855 for ($i=0;$i<$len;$i+=2)
856 {
857 $num = sscanf (substr ($str,$i,2), "%x");
858 $nstr.=chr ($num[0]);
859 }
860 return $nstr;
861 }
1fe143c @anastasds hex2bin was added in PHP 5.4.0 so the manual definition is obsolete
anastasds authored
862 */
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
863
864 function to64 ($v, $n)
865 {
866 $ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
867 $ret = "";
868 while (($n - 1) >= 0)
869 {
870 $n--;
871 $ret .= $ITOA64[$v & 0x3f];
872 $v = $v >> 6;
873 }
874 return $ret;
875 }
876
877 function has_new_messages($dbpdo, $user_id)
878 {
835b409 @anastasds fixed mass messaging, additional info when displaying mass messages, …
anastasds authored
879 $unread = $dbpdo->query("SELECT COUNT(*) FROM associations WHERE child_id = ? AND (type = ? OR type = ?)",array($user_id, 'unread_message', 'unread_mass_message'));
880 return !($unread[0]['COUNT(*)'] == '0');
e3313d6 @anastasds broke my commit tree somehow, recommitting for a fresh start
anastasds authored
881 }
882
883 function smtp_get_response ($fh)
884 {
885 $res ='';
886 do
887 {
888 $line = fgets($fh, 256);
889 $res .= $line;
890 }
891 while (preg_match("/^\d\d\d\-/", $line));
892 return $res;
893 }
894
895 ?>
Something went wrong with that request. Please try again.