Drop obsolete bookmarking services
[infodrom.org/www.zeitungsliste.de] / lib / bookmarks.inc
1 <?php
2
3 function bookmark_add($zid, $uid)
4 {
5   $query = sprintf("INSERT INTO bookmarks (uid,zeitung) VALUES (%d,%d)",
6                    $uid, $zid);
7
8   # Ignore potentially existing bookmark
9   db_query($query);
10 }
11
12 function bookmark_delete($zid, $uid)
13 {
14   $query = sprintf("DELETE FROM bookmarks " .
15                    "WHERE uid = %d AND zeitung = %d",
16                    $uid, $zid);
17
18   # Ignore potentially non-existing bookmark
19   db_query($query);
20 }
21
22 function bookmark_up($zid, $uid)
23 {
24   $query = sprintf("UPDATE bookmarks SET priority = priority - 1 " .
25                    "WHERE uid = %d AND zeitung = %d",
26                    $uid, $zid);
27
28   # Ignore potentially non-existing bookmark
29   db_query($query);
30 }
31
32 function bookmark_down($zid, $uid)
33 {
34   $query = sprintf("UPDATE bookmarks SET priority = priority + 1 " .
35                    "WHERE uid = %d AND zeitung = %d",
36                    $uid, $zid);
37
38   # Ignore potentially non-existing bookmark
39   db_query($query);
40 }
41
42 function format_bookmarks()
43 {
44   if (!isset($_SESSION['uid']))
45     return;
46
47   $query = sprintf("SELECT zeitung,name,priority FROM bookmarks " .
48                    "JOIN zeitungen ON zeitungen.id = zeitung " .
49                    "WHERE uid = %d " .
50                    "ORDER BY priority,name LIMIT 20",
51                    $_SESSION['uid']);
52
53   $sth = db_query($query);
54
55   if ($sth === false || pg_num_rows ($sth) == 0)
56     return '&nbsp;';
57
58 $rowf = '<tr>
59 <td width="85">
60 <a %s><img src="up.png" width="16" height="16" alt="up" title="Nach oben schieben" border="0"></a>
61 <strong>%d</strong>
62 <a %s><img src="down.png" width="16" height="16" alt="up" title="Nach unten schieben" border="0"></a>
63 <a %s><img src="delete.gif" width="16" height="16" alt="delete" title="Lesezeichen löschen" border="0"></a>
64 </td>
65 <td>
66 <a href="zeitung/%d.html">%s</a></td>
67 </tr>';
68
69   $ret .= '<table class="bookmarks" width="100%" alt="">';
70   for ($n=0; $n < pg_num_rows ($sth); $n++) {
71     $row = pg_fetch_array ($sth, $n);
72
73     if (javascript_ok()) {
74       $link_up = sprintf('href="bookmark.html" onclick="return bookmark_action(\'up\',%d);"', $row['zeitung']);
75       $link_down = sprintf('href="bookmark.html" onclick="return bookmark_action(\'down\',%d);"', $row['zeitung']);
76       $link_del = sprintf('href="bookmark.html" onclick="return bookmark_action(\'del\',%d);"', $row['zeitung']);
77     } else {
78       $link_up = sprintf('href="bookmark.html?zeitung=%d&action=up"', $row['zeitung']);
79       $link_down = sprintf('href="bookmark.html?zeitung=%d&action=down"', $row['zeitung']);
80       $link_del = sprintf('href="bookmark.html?zeitung=%d&action=delete"', $row['zeitung']);
81     }
82
83     $ret .= sprintf($rowf,
84                     $link_up, $row['priority'], $link_down, $link_del,
85                     $row['zeitung'], $row['name']);
86   }
87   $ret .= '</table>';
88
89
90   return $ret;
91 }
92
93 function bookmarks_manage()
94 {
95   if (!isset($_SESSION['uid']))
96     return notfound();
97
98   $ret = '<h3>Verwaltung der persönlichen Lesezeichen</h3>';
99
100   $ret .= '<div id="bookmarks">';
101   $ret .= load_javascript('bookmarks.js');
102   $ret .= format_bookmarks();
103   $ret .= '</div>';
104
105   return $ret;
106 }
107
108 function process_bookmark()
109 {
110   global $zlist;
111   global $cfg;
112
113   if (!isset($_SESSION['uid']))
114     return notfound();
115
116   if (empty($_SERVER['QUERY_STRING'])) {
117     return bookmarks_manage();
118   } elseif (!isset($_GET['zeitung']) || !is_numeric($_GET['zeitung'])) {
119     if (isset($_SERVER['HTTP_REFERER']))
120       $zlist['redirect'] = substr($_SERVER['HTTP_REFERER'], strlen($zlist['home']));
121     return warning('Keine Zeitung oder Magazin gefunden.');
122   } elseif (isset($_GET['action'])) {
123     if ($_GET['action'] == 'up')
124       bookmark_up($_GET['zeitung'], $_SESSION['uid']);
125     elseif ($_GET['action'] == 'down')
126       bookmark_down($_GET['zeitung'], $_SESSION['uid']);
127     elseif ($_GET['action'] == 'delete')
128       bookmark_delete($_GET['zeitung'], $_SESSION['uid']);
129     else
130       return notfound();
131     header('Location: ' . $cfg['home'] . 'bookmark.html');
132     exit;
133   } else {
134     bookmark_add($_GET['zeitung'], $_SESSION['uid']);
135     if (isset($_SERVER['HTTP_REFERER'])) {
136       header('Location: ' . $_SERVER['HTTP_REFERER']);
137       exit;
138     } else
139       return information('Ihr Lesezeichen wurde hinzugefügt');
140   }
141 }
142
143 function ajax_bookmark_check()
144 {
145   if (!isset($_POST['zeitung']) || !is_numeric($_POST['zeitung']) ||
146       !isset($_SESSION['uid']))
147     return false;
148
149   return true;
150 }
151
152 function ajax_bookmark_up()
153 {
154   if (!ajax_bookmark_check())
155     return false;
156
157   bookmark_up($_POST['zeitung'], $_SESSION['uid']);
158
159   return format_bookmarks();
160 }
161
162 function ajax_bookmark_down()
163 {
164   if (!ajax_bookmark_check())
165     return false;
166
167   bookmark_down($_POST['zeitung'], $_SESSION['uid']);
168
169   return format_bookmarks();
170 }
171
172 function ajax_bookmark_del()
173 {
174   if (!ajax_bookmark_check())
175     return false;
176
177   bookmark_delete($_POST['zeitung'], $_SESSION['uid']);
178
179   return format_bookmarks();
180 }
181
182 function ajax_bookmark_add()
183 {
184   if (!ajax_bookmark_check())
185     return false;
186
187   bookmark_add($_POST['zeitung'], $_SESSION['uid']);
188
189   return format_info_bookmarks();
190 }