Trust the database (i.e. don't encode special characters twice)
[infodrom.org/www.zeitungsliste.de] / lib / layout.inc
1 <?php
2
3 include_once('extern/rfc822.php');
4
5 function warning($text)
6 {
7   global $cfg;
8   $ret = '<p class="warn"><img src="'.$cfg['basepath'].
9     'achtung.gif" width="33" height="32" alt="Achtung!">&nbsp;&nbsp;';
10   $ret .= $text .'</p>';
11   return $ret;
12 }
13
14 function information($text)
15 {
16   global $cfg;
17   $ret = '<p class="info"><img src="'.$cfg['basepath'].
18     'information.gif" width="31" height="31" alt="Info">&nbsp;&nbsp;';
19   $ret .= $text .'</p>';
20   return $ret;
21 }
22
23 function pageheader()
24 {
25   global $cfg;
26   global $zlist;
27   global $_POST;
28   global $_SERVER;
29
30   $ret .= '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
31   $ret .= '<html lang="de">';
32   $ret .= '<head>';
33   if (isset($zlist['newspaper']))
34     $ret .= sprintf('<title>Zeitungsliste: %s</title>', $zlist['newspaper']);
35   elseif (isset($zlist['tag']))
36     $ret .= sprintf('<title>Zeitungsliste: Tag %s</title>', $zlist['tag']);
37   else
38     $ret .= '<title>Zeitungsliste</title>';
39   $ret .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
40   $ret .= '<meta name="Author" content="joey@infodrom.org (Joey Schulze)">';
41   $ret .= '<meta name="Language" content="de">';
42   
43   if ($zlist['page'] == 'index') {
44     $ret .= '<meta name="Keywords" content="Zeitung, Zeitungen, Liste, Zeitungsliste, Online-Zeitungen, Zeitungen im Web, Forum, Community, '.
45       'Liste aller Zeitungen">';
46     $ret .= sprintf('<link rel="alternate" type="application/rss+xml" title="Zeitungen" href="%szeitungen.rdf">', $cfg['basepath']);
47   }
48
49   if (is_spider())
50     $ret .= '<meta name="Robots" content="index,follow">';
51   $ret .= '<meta name="Copyright" content="(c) 2008 Information & Consulting">';
52   $ret .= sprintf('<link href="%smain.css" rel="stylesheet" type="text/css">', $cfg['basepath']);
53   $ret .= sprintf('<link rel="icon" href="%sfavicon.ico" type="image/x-icon">', $cfg['basepath']);
54   $ret .= sprintf('<link rel="shortcut icon" href="%sfavicon.ico" type="image/x-icon">', $cfg['basepath']);
55
56   if (isset($zlist['redirect']))
57     $ret .= sprintf('<meta http-equiv="refresh" content="7; URL=%s%s">',
58                     $cfg['home'], $zlist['redirect']);
59
60   $ret .= '</head>';
61
62   if (javascript_ok()) {
63     $ret .= sprintf('<script type="text/javascript" src="%szlist.js"></script>', $cfg['basepath']);
64     $ret .= sprintf('<script type="text/javascript">var zlist_urlbase = "%s";</script>', $cfg['basepath']);
65   }
66
67   $ret .= '<body>';
68   $ret .= '<div class="banner"><h1 class="none"><a href="http://www.debian.org/">Zeitungen online im Netz</a></h1></div>';
69
70   $ret .= '<div class="status">';
71
72   $ret .= '<div class="homestatus"><a href="'.$cfg['home'].'">Home</a>';
73   if ($cfg['path'] != 'contact.html')
74     $ret .= sprintf('&nbsp;&nbsp;&nbsp;<a href="%scontact.html">Kontakt</a>', $cfg['basepath']);
75   if (is_spider())
76     $ret .= sprintf('&nbsp;&nbsp;&nbsp;<a href="%ssitemap.html">Sitemap</a>', $cfg['basepath']);
77   $ret .= '</div>';
78
79   if (logged_in()) {
80     if ($cfg['path'] != "logout.html") {
81       $ret .= '<div class="logstatus">';
82       if ($cfg['path'] != "options.html")
83         $ret .= sprintf('<a href="%soptions.html">Einstellungen</a>&nbsp;&nbsp;&nbsp;',
84                         $cfg['basepath']);
85       $ret .= sprintf('<a href="%slogout.html">Abmelden</a>',
86                       $cfg['basepath']);
87       $ret .= '</div>';
88     }
89   } else {
90     if (($cfg['path'] != "login.html" && $cfg['path'] != "activate.html") ||
91         ($cfg['path'] == 'login.html' && isset($_POST['sendnew'])))
92       $ret .= sprintf('<div class="logstatus"><a href="%slogin.html">Anmelden</a></div>',
93                       $cfg['basepath']);
94   }
95   $ret .= sprintf('<div class="userstatus">%s</div>', userstatus());
96   $ret .= '</div>';
97
98   $ret .= '<div class="page">';
99   if ($_SERVER['SERVER_NAME'] == 'www.zeitungsliste.de' && javascript_ok())
100     $ret .= "\n" . load_template('addsense.js');
101
102   return $ret;
103 }
104
105 function layout_page($body)
106 {
107   global $zlist;
108
109   $ret = '<div class="info">';
110   if (is_array($zlist['info'])) {
111     include_once('info.inc');
112     foreach ($zlist['info'] as $func)
113       $ret .= $func();
114   }
115
116   $ret .= '</div>';
117
118   $ret .= '<div class="main">';
119   $ret .= $body;
120   $ret .= '</div>';
121   return $ret;
122 }
123
124 function layout_bookmarks($rel, $title)
125 {
126   global $cfg;
127
128   // Mehr auf http://www.bikeshops.de/Bikeshops/BikeGuide/BikeGuideWertgarantie.asp
129   $bookmarks = array(
130                      array('text' => 'Mister Wong',
131                            'icon' => 'link_misterwong.gif',
132                            'link' => 'http://www.mister-wong.de/index.php?action=addurl&amp;bm_url=@url@&amp;bm_description=@title@'),
133                      array('text' => 'Delicious',
134                            'icon' => 'link_delicious.gif',
135                            'link' => 'http://del.icio.us/post?v=4&amp;noui&amp;url=@url@&amp;title=@title@'),
136                      array('text' => 'Google Bookmarks',
137                            'icon' => 'link_google.gif',
138                            'link' => 'http://www.google.com/bookmarks/mark?op=add&amp;hl=de&amp;bkmk=@url@&amp;title=@title@'),
139                      array('text' => 'Yahoo Web',
140                            'icon' => 'link_yahoo.gif',
141                            'link' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=@url@&amp;t=@title@'),
142                      array('text' => 'Technorati',
143                            'icon' => 'link_technorati.gif',
144                            'link' => 'http://technorati.com/faves?add=@url@'),
145                      array('text' => 'Digg it',
146                            'icon' => 'link_digg.gif',
147                            'link' => 'http://digg.com/submit?phase=2&amp;url=@url@&amp;title=@title@'),
148                      array('text' => 'Yigg',
149                            'icon' => 'link_yigg.gif',
150                            'link' => 'http://yigg.de/neu?action=addurl&amp;exturl=@url@&amp;exttitle=@title@'),
151                      array('text' => 'Taggle',
152                            'icon' => 'link_taggle.gif',
153                            'link' => 'http://taggle.de/addLinkDetails?mAddress=@url@&amp;title=B@title@&amp;submitted=Weiter'),
154                      array('text' => 'Linkarena',
155                            'icon' => 'link_linkarena.gif',
156                            'link' => 'http://www.linkarena.com/linkadd.php?linkName=@title@&amp;linkURL=@url@'),
157                      array('text' => 'OneView',
158                            'icon' => 'link_oneview.gif',
159                            'link' => 'http://www.oneview.de/quickadd/neu/addBookmark.jsf?URL=@url@&amp;title=@title@'),
160                      array('text' => 'folk it',
161                            'icon' => 'link_folkd.gif',
162                            'link' => 'http://www.folkd.com/submit/@url@'),
163                      array('text' => 'Facebook',
164                            'icon' => 'link_facebook.gif',
165                            'link' => 'http://www.facebook.com/sharer.php?u=@url@&amp;title=@title@'),
166                      array('text' => 'Blinklist',
167                            'icon' => 'link_blinklist.gif',
168                            'link' => 'http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=@url@&Description=@title@'),
169                      array('text' => 'Furl',
170                            'icon' => 'link_furl.gif',
171                            'link' => 'http://www.furl.net/storeIt.jsp?t=@title@&u=@url@'),
172                      array('text' => 'Reddit',
173                            'icon' => 'link_reddit.gif',
174                            'link' => 'http://reddit.com/submit?url=@url@&title=@title@'),
175                      array('text' => 'Spurl',
176                            'icon' => 'link_spurl.gif',
177                            'link' => 'http://www.spurl.net/spurl.php?url=@url@&title=@title@'),
178                      array('text' => 'Shadows',
179                            'icon' => 'link_shadows.gif',
180                            'link' => 'http://www.shadows.com/features/tcr.htm?url=@url@&title=@title@'),
181                      array('text' => 'newsvine',
182                            'icon' => 'link_newsvine.gif',
183                            'link' => 'http://www.newsvine.com/_wine/save?popoff=1&u=@url@&tags=&blurb=@title@'),
184                      array('text' => 'CXWeb',
185                            'icon' => 'link_cxweb.gif',
186                            'link' => 'http://www.cxweb.de/login/?action=add&amp;address=@url@&amp;title=@title@'),
187                      array('text' => 'Simpy',
188                            'icon' => 'link_simpy.gif',
189                            'link' => 'http://www.simpy.com/simpy/LinkAdd.do?title=@title@&amp;href=@url@'),
190                      array('text' => 'Ma.Gnolia',
191                            'icon' => 'link_magnolia.gif',
192                            'link' => 'http://ma.gnolia.com/bookmarklet/add?url=@url@&amp;title=@title@&amp;description=&amp;tags='),
193                      array('text' => 'StumbleUpon',
194                            'icon' => 'link_stumbleupon.gif',
195                            'link' => 'http://www.stumbleupon.com/submit?url=@url@&amp;title=@title@'),
196                      array('text' => 'linksilo.de',
197                            'icon' => 'link_linksilo.gif',
198                            'link' => 'http://www.linksilo.de/index.php?area=bookmarks&amp;func=bookmark_new&amp;addurl=@url@&amp;addtitle=@title@'),
199                      array('text' => 'bookmarks.cc',
200                            'icon' => 'link_bookmarkscc.gif',
201                            'link' => 'http://www.bookmarks.cc/bookmarken.php?action=neu&amp;url=@url@&amp;title=@title@'),
202                      );
203
204   $url = $cfg['home'] . $rel;
205
206   $ret = '<div class="social_bookmarks">Lesezeichen: ';
207
208   foreach ($bookmarks as $bm) {
209     $ret .= sprintf('<a href="%s" title="%s"><img src="%sbookmarks/%s" width="16" height="16" alt="%s" name="%s" border="0"></a>',
210                     str_replace(array('@url@', '@title@'),
211                                 array(htmlspecialchars($url), htmlspecialchars($title)),
212                                 $bm['link']),
213                     $bm['text'],
214                     $cfg['basepath'], $bm['icon'], $bm['text'], $bm['text']);
215   }
216
217   $ret .= '</div>';
218
219   return $ret;
220 }
221
222 /*
223  * Add links to the main page for web spider so that they get the entire database
224  */
225 function layout_sitemap()
226 {
227   global $_SESSION;
228   global $_SERVER;
229   global $cfg;
230
231   if (!is_spider())
232     header('Location: ' . $cfg['home']);
233
234   $ret = '<h1>Sitemap der Zeitungsliste</h1>';
235
236   $query = 'SELECT tagname FROM tags ORDER BY lower(tagname)';
237
238   $sth = db_query($query);
239
240   if ($sth !== false && pg_num_rows($sth) > 0) {
241     $ret .= '<h3>User-Tags (Merkmale)</h3>';
242     $ret .= '<ul class="gold">';
243
244     for ($i=0; $i < pg_num_rows($sth); $i++) {
245       $row = pg_fetch_array($sth, $i);
246
247       $ret .= sprintf('<li><a href="%stag/%s.html">%s</a></li>',
248                       $cfg['basepath'], urlencode($row['tagname']),
249                       $row['tagname']);
250     }
251
252     $ret .= '</ul>';
253   }
254
255   $query = 'SELECT id,name,city FROM zeitungen WHERE deleted IS false ORDER BY name';
256
257   $sth = db_query($query);
258
259   if ($sth !== false && pg_num_rows($sth) > 0) {
260     $ret .= '<h3>Zeitungen und Magazine</h3>';
261     $ret .= '<ul class="gold">';
262     for ($i=0; $i < pg_num_rows($sth); $i++) {
263       $row = pg_fetch_array($sth, $i);
264
265       $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a>, %s</li>',
266                       $cfg['basepath'], $row['id'], $row['name'], $row['city']);
267     }
268     $ret .= '</ul>';
269   }
270
271   $query = 'SELECT id,topic FROM topics ORDER BY created';
272
273   $sth = db_query($query);
274
275   if ($sth !== false && pg_num_rows($sth) > 0) {
276     $ret .= '<h3>Diskussionen</h3>';
277     $ret .= '<ul class="gold">';
278     for ($i=0; $i < pg_num_rows($sth); $i++) {
279       $row = pg_fetch_array($sth, $i);
280
281       $ret .= sprintf('<li><a href="%stopic/%d.html">%s</a></li>',
282                       $cfg['basepath'], $row['id'], $row['topic']);
283     }
284     $ret .= '</ul>';
285   }
286
287   return $ret;
288 }
289
290 function layout_showpaper($archived=false)
291 {
292   global $_GET;
293   global $zlist;
294
295   if (!isset($_GET['zeitung']) || !is_numeric($_GET['zeitung']))
296     $ret = warning('Keine Zeitung oder Magazin gefunden.');
297   else {
298     $zid = $_GET['zeitung'];
299     $details = format_newspaper($zid, true);
300     if ($details === false)
301       $ret .= warning('Keine Zeitung oder Magazin gefunden.');
302     else {
303       $zlist['zid'] = $zid;
304       $ret .= $details;
305       if (!$archived) {
306         hits_inc ($zid);
307         $ret .= format_board($zid);
308       } else {
309         $ret .= format_board($zid, $archived);
310       }
311       $ret .= layout_bookmarks(sprintf('zeitung/%d.html', $zid), $zlist['newspaper']);
312     }
313   }
314
315   return $ret;
316 }
317
318 function layout_archive()
319 {
320   global $zlist;
321   global $_GET;
322   global $cfg;
323
324   if (!isset($_GET['archiv']) || !is_numeric($_GET['archiv']))
325     $ret = warning('Keine Zeitung oder Magazin gefunden.');
326   else {
327     $zid = $_GET['archiv'];
328     $details = format_newspaper($zid, true);
329     if ($details === false)
330       $ret .= warning('Keine Zeitung oder Magazin gefunden.');
331     else {
332       $zlist['zid'] = $zid;
333       $ret .= $details;
334
335       $query = sprintf('SELECT id,topic FROM topics ' .
336                        'WHERE zeitung = %d ORDER BY created DESC',
337                        $_GET['archiv']);
338
339       $sth = db_query($query);
340
341       if (!$sth || pg_num_rows ($sth) == 0)
342         $ret .= warning('Kein Archiv gefunden.');
343       else {
344         $ret .= '<p><ul class="gold">';
345         for ($i=0; $i < pg_num_rows ($sth); $i++) {
346           $row = pg_fetch_array($sth, $i);
347           $ret .= sprintf('<li><a href="%stopic/%d.html">%s</a></li>',
348                           $cfg['basepath'], $row['id'], $row['topic']);
349         }
350         $ret .= '</ul></p>';
351       }
352     }
353   }
354
355   return $ret;
356 }
357
358 function layout_showtag()
359 {
360   global $cfg;
361   global $zlist;
362   global $_GET;
363
364   if (!isset($_GET['tag']))
365     $ret = warning('Keine derartiges Merkmal gefunden.');
366   else {
367     $tagname = $_GET['tag'];
368     $ret .= '<h3>Merkmal <span class="tag">' . $tagname . '</span></h3>';
369
370     $query = sprintf("SELECT zeitungen.id,name,count(uid) AS count FROM zeitungen " .
371                      "JOIN zeitung_tags ON zeitung = id " .
372                      "JOIN tags ON tag = tags.id " .
373                      "WHERE tagname = '%s' " .
374                      "GROUP BY zeitungen.id,name ORDER BY count DESC",
375                      pg_escape_string($tagname));
376
377     $sth = db_query($query);
378
379     $zlist['tag'] = htmlspecialchars($tagname);
380     if (!$sth || pg_num_rows ($sth) == 0) {
381       $ret .= warning('Keine Zeitungen oder Magazine mit diesem Merkmal gefunden.');
382       unset($zlist['newspaper']);
383     } else {
384       $headlines = array('Mäßige','Mittelmäßige','Gute','Häufige','Häufigste');
385       $oldtag = -1;
386
387       for ($i=0; $i < pg_num_rows ($sth); $i++) {
388         $row = pg_fetch_array ($sth, $i);
389         $tag = tag_class($row['count']);
390
391         if ($oldtag != $tag) {
392           if ($oldtag != -1)
393             $ret .= '</ul>';
394           $ret .= sprintf('<h3 class="tag%d">%s Bewertungen</h3>', $tag, $headlines[$tag]);
395           $ret .= '<ul class="gold">';
396           $oldtag = $tag;       
397         }
398
399         $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a></li>',
400                         $cfg['basepath'], $row['id'], $row['name'], $row['city']);
401       }
402       $ret .= '</ul>';
403
404       $ret .= layout_bookmarks(sprintf('tag/%s.html', htmlspecialchars($tagname)), 'Merkmal '.$tagname);
405     }
406   }
407
408   return $ret;
409 }
410
411 function layout_topic()
412 {
413   global $zlist;
414   global $_GET;
415
416   if  (!isset($_GET['topic']) || !is_numeric($_GET['topic']))
417     $ret = warning('Keine passende Diskussion gefunden.');
418   else {
419     $discussion = format_topic($_GET['topic']);
420
421     if (isset($zlist['zid'])) {
422       $ret .= format_newspaper($zlist['zid'], true);
423
424       if ($zlist['archived'])
425         $ret .= '<h3>Abgeschlossene Diskussion</h3>';
426       else
427         $ret .= '<h3>Diskussion</h3>';
428     }
429     $ret .= $discussion;
430     if (isset($zlist['zid']))
431       $ret .= layout_bookmarks(sprintf('topic/%d.html', $zlist['zid']), $zlist['topic']);
432   }
433
434   return $ret;
435 }
436
437 function notfound()
438 {
439   header("HTTP/1.0 404 Not Found");
440
441   return warning('Diese Seite existiert nicht!');
442 }
443
444 function contact_send($topic, $email, $body)
445 {
446   global $cfg;
447   global $_SESSION;
448   global $_SERVER;
449
450   $header = array();
451   if (!empty($email)) {
452     if (isset($_SESSION['uid']))
453       $header[] = sprintf('Reply-To: %s <%s>',
454                           strlen($_SESSION['realname'])?$_SESSION['realname']:$_SESSION['nickname'],
455                           $email);
456     else
457       $header[] = 'Reply-To: ' . $email;
458   }
459
460   $body .= "\n\n"
461     ."HTTP_USER_AGENT: " . $_SERVER['HTTP_USER_AGENT'] . "\n"
462     ."REMOTE_ADDR    : " . $_SERVER['REMOTE_ADDR'] . "\n";
463
464   if (isset($_SESSION['uid']))
465     $body .= "Nickname       : " . $_SESSION['nickname'] . "\n";
466
467   $subject = str_replace(array('<', '>', '(', ')', '$', '/', '%', '\\', '#', ';', '&'), '', $topic);
468
469   sendmail($cfg['mailto'], '', $subject, $body, $header);
470 }
471
472 function process_contact()
473 {
474   global $_SERVER;
475   global $_POST;
476
477   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
478     if (empty($_POST['topic']))
479       $ret = warning('Sie haben keinen Titel für die Nachricht angegeben!');
480     elseif (empty($_POST['body']))
481       $ret = warning('Sie haben keinen Text der Nachricht angegeben!');
482     elseif (!empty($_POST['email']) && !is_valid_email_address($_POST['email']))
483       $ret = warning('Die angegebene Mail-Adresse ist ungültig.');
484     else {
485       contact_send($_POST['topic'], $_POST['email'], $_POST['body']);
486       $ret = information('Die Nachricht wurde an den Chef geschickt.');
487       $ret .= '<p>Die Bearbeitung kann eine Weile dauern.  Wenn Sie eine '.
488         'Antwort erwarten, sollte diese in den nächsten Tagen an die von '.
489         'Ihnen angegebene Adresse geschickt werden.</p>';
490       return $ret;
491     }
492     $replace = array('topic' => $_POST['topic'],
493                      'email' => $_POST['email'],
494                      'body' => $_POST['body']);
495   } elseif ($_SERVER['REQUEST_METHOD'] == 'GET') {
496     if (isset($_SESSION['email']))
497       $replace = array('email' => $_SESSION['email']);
498     else
499       $replace = array();
500   }
501
502   return load_javascript('contact.js') . load_template('contact.html', $replace);
503 }
504
505 ?>