Add some bots
[infodrom.org/www.zeitungsliste.de] / lib / info.inc
1 <?php
2
3 function info_searchform()
4 {
5   global $cfg;
6   global $zlist;
7
8   $replace = array('basepath' => $cfg['basepath'],
9                    'keyword' => $zlist['keyword']);
10
11   return load_javascript('searchform.js') . load_template('searchform.html', $replace);
12 }
13
14 function info_new()
15 {
16   global $cfg;
17
18   if ($cfg['path'] == 'reply.html' ||
19       $cfg['path'] == 'topic.html' ||
20       $cfg['path'] == 'new.html' ||
21       $cfg['dir'] == 'zeitung')
22     return;
23
24   $query = "SELECT name,id FROM zeitungen " .
25            "WHERE changed > now() - interval '3 months' AND deleted IS false " .
26            "ORDER BY changed DESC limit 10";
27
28   $sth = db_query($query);
29
30   if (pg_num_rows ($sth) == 0)
31     return;
32
33   $ret = '<h3>Neu aufgenommen</h3>';
34   $ret .= '<p><ul>';
35   for ($n=0; $n < pg_num_rows ($sth); $n++) {
36     $row = pg_fetch_array ($sth, $n);
37     $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a></li>',
38                     $cfg['basepath'], $row['id'], $row['name']);
39   }
40   $ret .= '</ul></p>';
41
42   return $ret;
43 }
44
45 function info_topten()
46 {
47   global $cfg;
48
49   $query = "SELECT zeitung,name,sum(counter) as counter FROM hits " .
50            "INNER JOIN zeitungen ON id = zeitung " .
51            "WHERE deleted IS false " .
52            "GROUP BY zeitung,name ORDER BY counter DESC LIMIT 10";
53
54   $sth = db_query($query) or carp("info_topten");
55
56   if (pg_num_rows ($sth) == 0)
57     return;
58
59   $ret = '<h3>Top 10</h3>';
60   $ret .= '<p><ul>';
61   for ($n=0; $n < pg_num_rows ($sth); $n++) {
62     $row = pg_fetch_array ($sth, $n);
63     $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a></li>',
64                     $cfg['basepath'], $row['zeitung'], $row['name']);
65   }
66   $ret .= '</ul></p>';
67
68   return $ret;
69 }
70
71 function info_hitlist()
72 {
73   global $cfg;
74
75   if (!isset($_SESSION['uid']))
76     return info_topten();
77
78   $query = sprintf("SELECT zeitung,name,counter FROM hits " .
79                    "INNER JOIN zeitungen ON id = zeitung " .
80                    "WHERE deleted IS false AND uid = %d " .
81                    "ORDER BY counter DESC LIMIT 10", $_SESSION['uid']);
82
83   $sth = db_query($query) or carp("info_topten");
84
85   if (pg_num_rows ($sth) == 0)
86     return info_topten();
87
88   $ret = '<h3>Favoriten</h3>';
89   $ret .= '<p><ul>';
90   for ($n=0; $n < pg_num_rows ($sth); $n++) {
91     $row = pg_fetch_array ($sth, $n);
92     $ret .= sprintf('<li><a href="%szeitung/%d.html">%s</a></li>',
93                     $cfg['basepath'], $row['zeitung'], $row['name']);
94   }
95   $ret .= '</ul></p>';
96
97   return $ret;
98 }
99
100 function info_tags()
101 {
102   global $cfg;
103   global $zlist;
104
105   if (!isset($zlist['zid']))
106     return;
107
108   $query = sprintf("SELECT id,tagname,count(uid) AS count " .
109                    "FROM tags JOIN zeitung_tags ON tag = id " .
110                    "WHERE zeitung = %d GROUP BY id,tagname " .
111                    "ORDER BY lower(tagname)",
112                    $zlist['zid']);
113
114   $sth = db_query($query);
115
116   if (pg_num_rows ($sth) == 0)
117     return;
118
119   $ret = '<h3>Tags</h3>';
120   $ret .= '<p class="tagcloud">';
121   for ($n=0; $n < pg_num_rows ($sth); $n++) {
122     $row = pg_fetch_array ($sth, $n);
123
124     $ret .= sprintf('<span class="tag%d"><a href="%stag/%s.html">%s</a></span> ',
125                     tag_class($row['count']), $cfg['basepath'],
126                     urlencode($row['tagname']), $row['tagname']);
127   }
128   $ret .= '</p>';
129
130   return $ret;
131 }
132
133 function info_tagcloud()
134 {
135   global $cfg;
136   global $zlist;
137
138   if (!isset($zlist['zid']))
139     return;
140
141   if (!is_numeric($_SESSION['uid']))
142     return;
143
144   $query = sprintf("SELECT id,tagname FROM tags " .
145                    "JOIN zeitung_tags ON tag = id " .
146                    "WHERE zeitung = %d AND uid = %d " .
147                    "ORDER BY lower(tagname)",
148                    $zlist['zid'], $_SESSION['uid']);
149
150   $sth = db_query($query);
151
152   if (pg_num_rows ($sth) == 0)
153     return info_tags();
154
155   $ret = '<h3>Persönliche Tags</h3>';
156   $ret .= '<p class="tagcloud">';
157   for ($n=0; $n < pg_num_rows ($sth); $n++) {
158     $row = pg_fetch_array ($sth, $n);
159
160     $ret .= sprintf('<span class="tag0"><a href="%stag/%s.html">%s</a></span> ',
161                     $cfg['basepath'],
162                     $row['tagname'], $row['tagname']);
163   }
164   $ret .= '</p>';
165
166   return $ret;
167 }
168
169 function info_archive($zid)
170 {
171   global $cfg;
172
173   $query = sprintf('SELECT count(*) FROM topics '.
174                    'WHERE zeitung = %d AND archived IS true',
175                    $zid);
176
177   $sth = db_query($query);
178
179   $row = pg_fetch_array($sth, 0);
180
181   if ($row[0] > 0)
182     return sprintf('<img src="%sarchive.gif" width="27" height="20" alt="">&nbsp;' .
183                    '<a href="%sarchiv/%d.html"><strong>Archiv</strong></a>',
184                    $cfg['basepath'], $cfg['basepath'], $zid);
185
186     return false;
187 }
188
189 function info_actions()
190 {
191   global $cfg;
192   global $zlist;
193
194   $title = array('edit' => 'Stammdaten dieser Zeitung bearbeiten',
195                  'tags' => 'Persönliche Tags zur Zeitung hinzufügen oder löschen',
196                  'add' => 'Neue Zeitung oder Magazin aufnehmen',
197                  'newtopic' => 'Neue Diskussion zu dieser Zeitung beginnen',
198                  'bookmark' => 'Lesezeichen zu dieser Zeitung setzen',
199                  'bookmarks' => 'Persönliche Lesezeichen verwalten',
200                  );
201
202   $actions = array();
203   $ret = '';
204
205   if (isset($_SESSION['uid']) && $cfg['path'] == 'options.html')
206     $actions[] = sprintf('<img src="%spasswd.gif" width="27" height="23" alt="">&nbsp;' .
207                          '<a href="%spasswd.html"><strong>Passwort ändern</strong></a>',
208                          $cfg['basepath'], $cfg['basepath']);
209
210   if (($cfg['dir'] == 'zeitung' || $cfg['dir'] == 'archiv' || $cfg['dir'] == 'topic' ) &&
211       isset($zlist['zid'])) {
212     if (isset($_SESSION['uid']))
213       $actions[] = sprintf('<img src="%stag.gif" width="27" height="25" alt="">&nbsp;' .
214                            '<a href="%stags.html?zeitung=%d" title="%s"><strong>Tags verwalten</strong></a>',
215                            $cfg['basepath'], $cfg['basepath'], $zlist['zid'],
216                            $title['tags']);
217     else
218       $actions[] = sprintf('<img src="%stag.gif" width="27" height="25" alt="">&nbsp;' .
219                            '<a href="%slogin.html?from=tags" title="%s"><strong>Tags verwalten</strong></a>',
220                            $cfg['basepath'], $cfg['basepath'], $title['tags']);
221   }
222
223   if ($cfg['dir'] == 'zeitung' && isset($_SESSION['uid'])) {
224     if (javascript_ok()) {
225       
226       $ret .= load_javascript('bookmarks.js');
227       $link = sprintf('href="%sbookmark.html" onclick="return bookmark_action(\'add\',%d);"',
228                       $cfg['basepath'], $zlist['zid']);
229     } else
230       $link = sprintf('href="%sbookmark.html?zeitung=%d"', $cfg['basepath'], $zlist['zid']);
231
232     $actions[] = sprintf('<img src="%sadd.gif" width="27" height="24" alt="">&nbsp;' .
233                          '<a %s title="%s"><strong>Lesezeichen setzen</strong></a>',
234                          $cfg['basepath'], $link,
235                          $title['bookmark']);
236   }
237
238   if ($cfg['path'] == 'options.html' && isset($_SESSION['uid']))
239     $actions[] = sprintf('<img src="%sbookmarks.gif" width="27" height="21" alt="">&nbsp;' .
240                          '<a href="%sbookmark.html" title="%s"><strong>Lesezeichen verwalten</strong></a>',
241                          $cfg['basepath'], $cfg['basepath'],
242                          $title['bookmarks']);
243
244   if ($cfg['dir'] == 'zeitung' && isset($zlist['zid'])) {
245     if (isset($_SESSION['uid']))
246       $actions[] = sprintf('<img src="%sedit.gif" width="27" height="26" alt="">&nbsp;' .
247                            '<a href="%sedit.html?zeitung=%d" title="%s"><strong>Bearbeiten</strong></a>',
248                            $cfg['basepath'], $cfg['basepath'], $zlist['zid'],
249                            $title['edit']);
250     else
251       $actions[] = sprintf('<img src="%sedit.gif" width="27" height="26" alt="">&nbsp;' .
252                            '<a href="%slogin.html?from=edit" title="%s"><strong>Bearbeiten</strong></a>',
253                            $cfg['basepath'], $cfg['basepath'],
254                            $title['edit']);
255   }
256
257   if ($zlist['page'] == 'index' ||
258       $cfg['dir'] == 'zeitung' ||
259       $cfg['dir'] == 'tag') {
260     if (isset($_SESSION['uid']))
261       $actions[] = sprintf('<img src="%snewspaper.gif" width="27" height="25" alt="">&nbsp;' .
262                            '<a href="%snew.html" title="%s"><strong>Neue Zeitung</strong></a>',
263                            $cfg['basepath'], $cfg['basepath'], $title['add']);
264     else
265       $actions[] = sprintf('<img src="%snewspaper.gif" width="27" height="25" alt="">&nbsp;' .
266                            '<a href="%slogin.html?from=new" title="%s"><strong>Neue Zeitung</strong></a>',
267                            $cfg['basepath'], $cfg['basepath'], $title['add']);
268   }
269
270   if ($zlist['notopic'] === true)
271     $text = 'Diskussion';
272   else
273     $text = 'Neues Thema';
274
275   if (($cfg['dir'] == 'zeitung' || $cfg['dir'] == 'archiv') && isset($zlist['zid'])) {
276     if (isset($_SESSION['uid']))
277       $actions[] = sprintf('<img src="%snewtopic.gif" width="27" height="20" alt="">&nbsp;' .
278                            '<a href="%stopic.html?zeitung=%d" title="%s"><strong>%s</strong></a>',
279                            $cfg['basepath'], $cfg['basepath'], $zlist['zid'],
280                            $title['newtopic'], $text);
281     else
282       $actions[] = sprintf('<img src="%snewtopic.gif" width="27" height="20" alt="">&nbsp;' .
283                            '<a href="%slogin.html?from=zeitung" title="%s"><strong>%s</strong></a>',
284                            $cfg['basepath'], $cfg['basepath'], $title['newtopic'], $text);
285   }
286
287   if ($cfg['dir'] == 'zeitung' && isset($zlist['zid'])) {
288     $try = info_archive($zlist['zid']);
289     if ($try !== false)
290       $actions[] = $try;
291   }
292
293   if ($actions) {
294     $ret .= '<p><ul class="action">';
295     foreach ($actions as $row)
296       $ret .= '<li>' . $row . '</li>';
297     $ret .= '</ul></p>';
298   }
299
300   return $ret;
301 }
302
303 function info_bookmarks()
304 {
305   global $cfg;
306
307   if (!isset($_SESSION['uid']))
308     return;
309
310   if ($cfg['path'] == 'bookmark.html')
311     return;
312
313   $try = format_info_bookmarks();
314
315   if ($try === false)
316     return;
317
318   $ret = '<h3>Lesezeichen</h3>';
319   $ret .= '<div id="bookmarks">';
320   $ret .= $try;
321   $ret .= '</div>';
322
323   return $ret;
324 }