]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
f1a80dae | 2 | |
894ebcf5 | 3 | /* if ($_GET["debug"]) { |
cce28758 AD |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
894ebcf5 | 7 | } */ |
cce28758 | 8 | |
40d13c28 | 9 | require_once 'config.php'; |
b619ff15 | 10 | require_once 'db-prefs.php'; |
5bc0bd27 | 11 | require_once 'compat.php'; |
af106b0e | 12 | require_once 'errors.php'; |
8911ac8b | 13 | require_once 'version.php'; |
40d13c28 | 14 | |
7c5a308d | 15 | if (RSS_BACKEND_TYPE == "magpie") { |
d5f74779 | 16 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); |
ed891a51 | 17 | require_once "magpierss/rss_fetch.inc"; |
7c5a308d AD |
18 | require_once 'magpierss/rss_utils.inc'; |
19 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
20 | require_once 'simplepie/simplepie.inc'; | |
21 | } | |
387234f3 | 22 | |
a3ee2a38 AD |
23 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
24 | ||
ad507f85 AD |
25 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { |
26 | ||
27 | $rows = -1; | |
4c193675 | 28 | |
fefa6ca3 | 29 | if (DB_TYPE == "pgsql") { |
44e241cb | 30 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 31 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 32 | (SELECT date_entered FROM ttrss_entries WHERE |
44e241cb AD |
33 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ |
34 | ||
6e7f8d26 AD |
35 | $pg_version = get_pgsql_version($link); |
36 | ||
37 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 AD |
38 | |
39 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
40 | ttrss_entries.id = ref_id AND | |
41 | marked = false AND | |
42 | feed_id = '$feed_id' AND | |
43 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
44 | ||
45 | } else { | |
46 | ||
47 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
48 | USING ttrss_entries | |
49 | WHERE ttrss_entries.id = ref_id AND | |
50 | marked = false AND | |
51 | feed_id = '$feed_id' AND | |
fc774155 | 52 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 53 | } |
ad507f85 AD |
54 | |
55 | $rows = pg_affected_rows($result); | |
56 | ||
fefa6ca3 | 57 | } else { |
1e59ae35 | 58 | |
30f1746f | 59 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 60 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 | 61 | (SELECT date_entered FROM ttrss_entries WHERE |
30f1746f AD |
62 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
63 | ||
64 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
65 | USING ttrss_user_entries, ttrss_entries | |
66 | WHERE ttrss_entries.id = ref_id AND | |
67 | marked = false AND | |
68 | feed_id = '$feed_id' AND | |
69 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
70 | ||
ad507f85 AD |
71 | $rows = mysql_affected_rows($link); |
72 | ||
73 | } | |
74 | ||
75 | if ($debug) { | |
76 | print "Purged feed $feed_id ($purge_interval): deleted $rows articles\n"; | |
fefa6ca3 AD |
77 | } |
78 | } | |
79 | ||
44e241cb AD |
80 | function global_purge_old_posts($link, $do_output = false, $limit = false) { |
81 | ||
894ebcf5 | 82 | $random_qpart = sql_random_function(); |
fefa6ca3 | 83 | |
44e241cb AD |
84 | if ($limit) { |
85 | $limit_qpart = "LIMIT $limit"; | |
86 | } else { | |
87 | $limit_qpart = ""; | |
88 | } | |
89 | ||
fefa6ca3 | 90 | $result = db_query($link, |
44e241cb AD |
91 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds |
92 | ORDER BY $random_qpart $limit_qpart"); | |
fefa6ca3 AD |
93 | |
94 | while ($line = db_fetch_assoc($result)) { | |
95 | ||
96 | $feed_id = $line["id"]; | |
97 | $purge_interval = $line["purge_interval"]; | |
98 | $owner_uid = $line["owner_uid"]; | |
99 | ||
100 | if ($purge_interval == 0) { | |
101 | ||
102 | $tmp_result = db_query($link, | |
103 | "SELECT value FROM ttrss_user_prefs WHERE | |
104 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
105 | ||
106 | if (db_num_rows($tmp_result) != 0) { | |
107 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
108 | } | |
109 | } | |
110 | ||
111 | if ($do_output) { | |
ad507f85 | 112 | // print "Feed $feed_id: purge interval = $purge_interval\n"; |
fefa6ca3 AD |
113 | } |
114 | ||
115 | if ($purge_interval > 0) { | |
ad507f85 | 116 | purge_feed($link, $feed_id, $purge_interval, $do_output); |
fefa6ca3 AD |
117 | } |
118 | } | |
119 | ||
71604ca4 AD |
120 | // purge orphaned posts in main content table |
121 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
122 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
123 | ||
fefa6ca3 AD |
124 | } |
125 | ||
b6eefba5 | 126 | function purge_old_posts($link) { |
5d73494a | 127 | |
f1a80dae AD |
128 | $user_id = $_SESSION["uid"]; |
129 | ||
130 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
131 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
132 | |
133 | while ($line = db_fetch_assoc($result)) { | |
134 | ||
135 | $feed_id = $line["id"]; | |
136 | $purge_interval = $line["purge_interval"]; | |
137 | ||
b619ff15 | 138 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 139 | |
140aae81 | 140 | if ($purge_interval > 0) { |
fefa6ca3 | 141 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
142 | } |
143 | } | |
71604ca4 AD |
144 | |
145 | // purge orphaned posts in main content table | |
146 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
147 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
148 | } |
149 | ||
1f2b01ed | 150 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 151 | |
4769ddaf | 152 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 153 | |
a2770077 AD |
154 | if (!$user_id) { |
155 | $user_id = $_SESSION["uid"]; | |
156 | purge_old_posts($link); | |
157 | } | |
158 | ||
25af8dad | 159 | // db_query($link, "BEGIN"); |
b82af8c3 | 160 | |
cbd8650d AD |
161 | if (MAX_UPDATE_TIME > 0) { |
162 | if (DB_TYPE == "mysql") { | |
163 | $q_order = "RAND()"; | |
164 | } else { | |
165 | $q_order = "RANDOM()"; | |
166 | } | |
167 | } else { | |
168 | $q_order = "last_updated DESC"; | |
169 | } | |
170 | ||
d148926e | 171 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 172 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 173 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
174 | ORDER BY $q_order"); |
175 | ||
176 | $upd_start = time(); | |
40d13c28 | 177 | |
b6eefba5 | 178 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
179 | $upd_intl = $line["update_interval"]; |
180 | ||
b619ff15 | 181 | if (!$upd_intl || $upd_intl == 0) { |
e289ca71 | 182 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false); |
b619ff15 | 183 | } |
d148926e | 184 | |
c1e202b7 AD |
185 | if ($upd_intl < 0) { |
186 | // Updates for this feed are disabled | |
187 | continue; | |
188 | } | |
189 | ||
93d40f50 AD |
190 | if ($fetch || (!$line["last_updated"] || |
191 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 192 | |
cbd8650d AD |
193 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
194 | ||
1f2b01ed | 195 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
196 | |
197 | $upd_elapsed = time() - $upd_start; | |
198 | ||
199 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
200 | return; | |
201 | } | |
d148926e | 202 | } |
40d13c28 AD |
203 | } |
204 | ||
25af8dad | 205 | // db_query($link, "COMMIT"); |
b82af8c3 | 206 | |
40d13c28 AD |
207 | } |
208 | ||
4065b60b AD |
209 | function fetch_file_contents($url) { |
210 | if (USE_CURL_FOR_ICONS) { | |
211 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
212 | ||
213 | $ch = curl_init($url); | |
214 | $fp = fopen($tmpfile, "w"); | |
215 | ||
216 | if ($fp) { | |
217 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
218 | curl_exec($ch); | |
219 | curl_close($ch); | |
220 | fclose($fp); | |
221 | } | |
222 | ||
223 | $contents = file_get_contents($tmpfile); | |
224 | unlink($tmpfile); | |
225 | ||
226 | return $contents; | |
227 | ||
228 | } else { | |
229 | return file_get_contents($url); | |
230 | } | |
231 | ||
232 | } | |
78800912 | 233 | |
4065b60b AD |
234 | // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) |
235 | // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
78800912 | 236 | |
4065b60b | 237 | function get_favicon_url($url) { |
99331724 | 238 | |
4065b60b | 239 | if ($html = @fetch_file_contents($url)) { |
78800912 | 240 | |
4065b60b AD |
241 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { |
242 | // Attempt to grab a favicon link from their webpage url | |
243 | $linkUrl = html_entity_decode($matches[1]); | |
c798704b | 244 | |
4065b60b AD |
245 | if (substr($linkUrl, 0, 1) == '/') { |
246 | $urlParts = parse_url($url); | |
247 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
248 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
249 | $faviconURL = $linkUrl; | |
250 | } else if (substr($url, -1, 1) == '/') { | |
251 | $faviconURL = $url.$linkUrl; | |
252 | } else { | |
253 | $faviconURL = $url.'/'.$linkUrl; | |
e695fdc8 | 254 | } |
717f5e64 | 255 | |
c798704b | 256 | } else { |
4065b60b AD |
257 | // If unsuccessful, attempt to "guess" the favicon location |
258 | $urlParts = parse_url($url); | |
259 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
260 | } | |
261 | } | |
c798704b | 262 | |
4065b60b AD |
263 | // Run a test to see if what we have attempted to get actually exists. |
264 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
265 | return $faviconURL; | |
266 | } else { | |
267 | return false; | |
268 | } | |
269 | } | |
270 | ||
271 | function url_validate($link) { | |
272 | ||
273 | $url_parts = @parse_url($link); | |
274 | ||
275 | if ( empty( $url_parts["host"] ) ) | |
276 | return false; | |
277 | ||
278 | if ( !empty( $url_parts["path"] ) ) { | |
279 | $documentpath = $url_parts["path"]; | |
280 | } else { | |
281 | $documentpath = "/"; | |
282 | } | |
283 | ||
284 | if ( !empty( $url_parts["query"] ) ) | |
285 | $documentpath .= "?" . $url_parts["query"]; | |
286 | ||
287 | $host = $url_parts["host"]; | |
288 | $port = $url_parts["port"]; | |
289 | ||
290 | if ( empty($port) ) | |
291 | $port = "80"; | |
292 | ||
293 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
294 | ||
295 | if ( !$socket ) | |
296 | return false; | |
c798704b | 297 | |
4065b60b AD |
298 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); |
299 | ||
300 | $http_response = fgets( $socket, 22 ); | |
301 | ||
302 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
303 | if ( preg_match($responses, $http_response) ) { | |
304 | fclose($socket); | |
305 | return true; | |
306 | } else { | |
307 | return false; | |
308 | } | |
309 | ||
310 | } | |
311 | ||
312 | function check_feed_favicon($site_url, $feed, $link) { | |
313 | $favicon_url = get_favicon_url($site_url); | |
314 | ||
315 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
316 | ||
317 | error_reporting(0); | |
318 | ||
319 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
320 | ||
321 | if ($favicon_url && !file_exists($icon_file)) { | |
322 | $contents = fetch_file_contents($favicon_url); | |
323 | ||
324 | $fp = fopen($icon_file, "w"); | |
78800912 | 325 | |
4065b60b AD |
326 | if ($fp) { |
327 | fwrite($fp, $contents); | |
328 | fclose($fp); | |
329 | chmod($icon_file, 0644); | |
330 | } | |
78800912 | 331 | } |
4065b60b AD |
332 | |
333 | error_reporting(DEFAULT_ERROR_LEVEL); | |
334 | ||
78800912 AD |
335 | } |
336 | ||
ddb68b81 | 337 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 338 | |
4769ddaf | 339 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 340 | |
ddb68b81 | 341 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
342 | return; |
343 | } | |
344 | ||
47c6c988 | 345 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
346 | FROM ttrss_feeds WHERE id = '$feed'"); |
347 | ||
ff25e639 AD |
348 | $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login")); |
349 | $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass")); | |
47c6c988 | 350 | |
a88c1f36 AD |
351 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
352 | ||
353 | if ($update_interval < 0) { return; } | |
354 | ||
ab3d0b99 AD |
355 | $feed = db_escape_string($feed); |
356 | ||
47c6c988 AD |
357 | $fetch_url = $feed_url; |
358 | ||
359 | if ($auth_login && $auth_pass) { | |
360 | $url_parts = array(); | |
361 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
362 | ||
363 | if ($url_parts[1] && $url_parts[2]) { | |
364 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
365 | } | |
366 | ||
367 | } | |
ab3d0b99 | 368 | |
7c5a308d AD |
369 | if (RSS_BACKEND_TYPE == "magpie") { |
370 | error_reporting(0); | |
371 | $rss = fetch_rss($fetch_url); | |
372 | error_reporting (DEFAULT_ERROR_LEVEL); | |
373 | } else if (RSS_BACKEND_TYPE == "simplepie") { | |
374 | ||
375 | if (!file_exists(SIMPLEPIE_CACHE_DIR)) { | |
376 | mkdir(SIMPLEPIE_CACHE_DIR); | |
377 | } | |
76798ff3 | 378 | |
7c5a308d AD |
379 | $rss = new SimplePie(); |
380 | $rss->feed_url($fetch_url); | |
381 | $rss->cache_location(SIMPLEPIE_CACHE_DIR); | |
382 | $rss->init(); | |
383 | } | |
384 | ||
b6eefba5 | 385 | $feed = db_escape_string($feed); |
dcee8f61 | 386 | |
7c5a308d | 387 | $rss_check = $rss; |
b82af8c3 | 388 | |
7c5a308d AD |
389 | if (RSS_BACKEND_TYPE == "simplepie") { |
390 | $rss_check = $rss->data; | |
391 | } | |
392 | ||
393 | if ($rss_check) { | |
394 | ||
44e241cb | 395 | // db_query($link, "BEGIN"); |
dd8c76a9 | 396 | |
a88c1f36 | 397 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 398 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 399 | |
b6eefba5 AD |
400 | $registered_title = db_fetch_result($result, 0, "title"); |
401 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 402 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 403 | |
7fed1940 AD |
404 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
405 | ||
8d0ec6fd | 406 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { |
4065b60b | 407 | check_feed_favicon($rss->channel["link"], $feed, $link); |
a2770077 AD |
408 | } |
409 | ||
746b249f | 410 | if (!$registered_title || $registered_title == "[Unknown]") { |
7c5a308d AD |
411 | |
412 | if (RSS_BACKEND_TYPE == "magpie") { | |
413 | $feed_title = db_escape_string($rss->channel["title"]); | |
414 | } else { | |
415 | $feed_title = $rss->get_feed_title(); | |
416 | } | |
417 | ||
f324892e AD |
418 | db_query($link, "UPDATE ttrss_feeds SET |
419 | title = '$feed_title' WHERE id = '$feed'"); | |
420 | } | |
421 | ||
7c5a308d AD |
422 | if (RSS_BACKEND_TYPE == "magpie") { |
423 | $site_url = $rss->channel["link"]; | |
424 | // weird, weird Magpie | |
425 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
426 | } else { | |
427 | $site_url = $rss->get_feed_link(); | |
428 | } | |
147f7691 AD |
429 | |
430 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
431 | db_query($link, "UPDATE ttrss_feeds SET |
432 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 433 | } |
40d13c28 | 434 | |
b7f4bda2 AD |
435 | // print "I: " . $rss->channel["image"]["url"]; |
436 | ||
7c5a308d AD |
437 | if (RSS_BACKEND_TYPE == "magpie") { |
438 | $icon_url = $rss->image["url"]; | |
439 | } else { | |
440 | $icon_url = $rss->get_image_url(); # FIXME | |
441 | } | |
b7f4bda2 | 442 | |
147f7691 | 443 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
444 | $icon_url = db_escape_string($icon_url); |
445 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
446 | } |
447 | ||
e6155a06 AD |
448 | |
449 | $filters = array(); | |
450 | ||
4b3dff6e | 451 | $result = db_query($link, "SELECT reg_exp, |
db42b934 | 452 | ttrss_filter_types.name AS name, |
073ca0e6 AD |
453 | ttrss_filter_actions.name AS action, |
454 | action_param | |
db42b934 | 455 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE |
e8b79d16 | 456 | enabled = true AND |
db42b934 AD |
457 | owner_uid = $owner_uid AND |
458 | ttrss_filter_types.id = filter_type AND | |
459 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 460 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 461 | |
b6eefba5 | 462 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 463 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
464 | |
465 | $filter["reg_exp"] = $line["reg_exp"]; | |
466 | $filter["action"] = $line["action"]; | |
073ca0e6 AD |
467 | $filter["action_param"] = $line["action_param"]; |
468 | ||
19c9cb11 | 469 | array_push($filters[$line["name"]], $filter); |
e6155a06 AD |
470 | } |
471 | ||
7c5a308d AD |
472 | if (RSS_BACKEND_TYPE == "magpie") { |
473 | $iterator = $rss->items; | |
ddb68b81 | 474 | |
7c5a308d AD |
475 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; |
476 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
477 | ||
478 | } else { | |
479 | $iterator = $rss->get_items(); | |
480 | } | |
c22789da AD |
481 | |
482 | if (!is_array($iterator)) { | |
39541e74 | 483 | /* db_query($link, "UPDATE ttrss_feeds |
75bd0669 | 484 | SET last_error = 'Parse error: can\'t find any articles.' |
39541e74 | 485 | WHERE id = '$feed'"); */ |
c22789da AD |
486 | return; // WTF? |
487 | } | |
ddb68b81 AD |
488 | |
489 | foreach ($iterator as $item) { | |
7c5a308d AD |
490 | |
491 | if (RSS_BACKEND_TYPE == "magpie") { | |
492 | ||
493 | $entry_guid = $item["id"]; | |
494 | ||
495 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
496 | if (!$entry_guid) $entry_guid = $item["link"]; | |
1f64b1be AD |
497 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); |
498 | ||
7c5a308d | 499 | if (!$entry_guid) continue; |
40d13c28 | 500 | |
7c5a308d AD |
501 | $entry_timestamp = ""; |
502 | ||
503 | $rss_2_date = $item['pubdate']; | |
504 | $rss_1_date = $item['dc']['date']; | |
505 | $atom_date = $item['issued']; | |
506 | if (!$atom_date) $atom_date = $item['updated']; | |
b82af8c3 | 507 | |
7c5a308d AD |
508 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
509 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
510 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
511 | ||
512 | if ($entry_timestamp == "") { | |
513 | $entry_timestamp = time(); | |
514 | $no_orig_date = 'true'; | |
515 | } else { | |
516 | $no_orig_date = 'false'; | |
517 | } | |
518 | ||
519 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
520 | ||
7d3ab0dd | 521 | $entry_title = trim(strip_tags($item["title"])); |
7c5a308d AD |
522 | |
523 | // strange Magpie workaround | |
524 | $entry_link = $item["link_"]; | |
525 | if (!$entry_link) $entry_link = $item["link"]; | |
526 | ||
527 | if (!$entry_title) continue; | |
1f64b1be | 528 | # if (!$entry_link) continue; |
7d3ab0dd AD |
529 | |
530 | $entry_link = strip_tags($entry_link); | |
531 | ||
7c5a308d AD |
532 | $entry_content = $item["content:escaped"]; |
533 | ||
534 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
535 | if (!$entry_content) $entry_content = $item["content"]; | |
79b5d2d2 | 536 | if (!$entry_content) $entry_content = $item["atom_content"]; |
7c5a308d AD |
537 | if (!$entry_content) $entry_content = $item["summary"]; |
538 | if (!$entry_content) $entry_content = $item["description"]; | |
539 | ||
540 | // if (!$entry_content) continue; | |
541 | ||
542 | // WTF | |
543 | if (is_array($entry_content)) { | |
544 | $entry_content = $entry_content["encoded"]; | |
545 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
546 | } | |
547 | ||
548 | // print_r($item); | |
549 | // print_r(htmlspecialchars($entry_content)); | |
550 | // print "<br>"; | |
551 | ||
552 | $entry_content_unescaped = $entry_content; | |
553 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
554 | ||
7d3ab0dd | 555 | $entry_comments = strip_tags($item["comments"]); |
bbb416e5 | 556 | |
7d3ab0dd | 557 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); |
bbb416e5 AD |
558 | |
559 | if (!$entry_author) { | |
560 | $entry_author = db_escape_string(strip_tags($item['author'])); | |
561 | } | |
562 | ||
7d3ab0dd | 563 | $entry_guid = db_escape_string(strip_tags($entry_guid)); |
7c5a308d AD |
564 | |
565 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
566 | WHERE guid = '$entry_guid'"); | |
567 | ||
568 | $entry_content = db_escape_string($entry_content); | |
569 | $entry_title = db_escape_string($entry_title); | |
570 | $entry_link = db_escape_string($entry_link); | |
571 | $entry_comments = db_escape_string($entry_comments); | |
572 | ||
573 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
574 | ||
575 | if (!$num_comments) $num_comments = 0; | |
8add756a AD |
576 | } |
577 | ||
d48d160c | 578 | # sanitize content |
183ad07b AD |
579 | |
580 | $entry_content = sanitize_rss($entry_content); | |
d48d160c | 581 | |
44e241cb AD |
582 | db_query($link, "BEGIN"); |
583 | ||
4c193675 AD |
584 | if (db_num_rows($result) == 0) { |
585 | ||
586 | // base post entry does not exist, create it | |
587 | ||
4c193675 AD |
588 | $result = db_query($link, |
589 | "INSERT INTO ttrss_entries | |
590 | (title, | |
591 | guid, | |
592 | link, | |
593 | updated, | |
594 | content, | |
595 | content_hash, | |
596 | no_orig_date, | |
597 | date_entered, | |
11b0dce2 | 598 | comments, |
b6104dee AD |
599 | num_comments, |
600 | author) | |
4c193675 AD |
601 | VALUES |
602 | ('$entry_title', | |
603 | '$entry_guid', | |
604 | '$entry_link', | |
605 | '$entry_timestamp_fmt', | |
606 | '$entry_content', | |
607 | '$content_hash', | |
608 | $no_orig_date, | |
609 | NOW(), | |
11b0dce2 | 610 | '$entry_comments', |
b6104dee AD |
611 | '$num_comments', |
612 | '$entry_author')"); | |
8926aab8 AD |
613 | } else { |
614 | // we keep encountering the entry in feeds, so we need to | |
615 | // update date_entered column so that we don't get horrible | |
616 | // dupes when the entry gets purged and reinserted again e.g. | |
617 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
618 | ||
619 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
620 | ||
621 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
622 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
623 | } |
624 | ||
625 | // now it should exist, if not - bad luck then | |
626 | ||
6385315d AD |
627 | $result = db_query($link, "SELECT |
628 | id,content_hash,no_orig_date,title, | |
8926aab8 | 629 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
630 | substring(updated,1,19) as updated, |
631 | num_comments | |
6385315d AD |
632 | FROM |
633 | ttrss_entries | |
634 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
635 | |
636 | if (db_num_rows($result) == 1) { | |
637 | ||
11b0dce2 AD |
638 | // this will be used below in update handler |
639 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
640 | $orig_title = db_fetch_result($result, 0, "title"); | |
641 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
642 | $orig_date_entered = strtotime(db_fetch_result($result, |
643 | 0, "date_entered")); | |
6385315d | 644 | |
11b0dce2 | 645 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 646 | |
11b0dce2 | 647 | // check for user post link to main table |
4c193675 | 648 | |
11b0dce2 | 649 | // do we allow duplicate posts with same GUID in different feeds? |
8d0ec6fd | 650 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { |
11b0dce2 AD |
651 | $dupcheck_qpart = "AND feed_id = '$feed'"; |
652 | } else { | |
653 | $dupcheck_qpart = ""; | |
654 | } | |
71604ca4 | 655 | |
11b0dce2 | 656 | // error_reporting(0); |
19c9cb11 | 657 | |
073ca0e6 | 658 | $tuple = get_filter_name($entry_title, $entry_content, |
11b0dce2 | 659 | $entry_link, $filters); |
19c9cb11 | 660 | |
073ca0e6 AD |
661 | $filter_name = $tuple[0]; |
662 | $filter_param = $tuple[1]; | |
663 | ||
11b0dce2 AD |
664 | if ($filter_name == "filter") { |
665 | continue; | |
666 | } | |
19c9cb11 | 667 | |
11b0dce2 | 668 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 669 | |
11b0dce2 AD |
670 | $result = db_query($link, |
671 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
672 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
673 | $dupcheck_qpart"); | |
674 | ||
675 | // okay it doesn't exist - create user entry | |
676 | if (db_num_rows($result) == 0) { | |
677 | ||
678 | if ($filter_name != 'catchup') { | |
679 | $unread = 'true'; | |
680 | $last_read_qpart = 'NULL'; | |
681 | } else { | |
682 | $unread = 'false'; | |
683 | $last_read_qpart = 'NOW()'; | |
684 | } | |
dd7d3187 AD |
685 | |
686 | if ($filter_name == 'mark') { | |
687 | $marked = 'true'; | |
688 | } else { | |
689 | $marked = 'false'; | |
690 | } | |
19c9cb11 | 691 | |
11b0dce2 AD |
692 | $result = db_query($link, |
693 | "INSERT INTO ttrss_user_entries | |
dd7d3187 | 694 | (ref_id, owner_uid, feed_id, unread, last_read, marked) |
11b0dce2 | 695 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, |
dd7d3187 | 696 | $last_read_qpart, $marked)"); |
11b0dce2 AD |
697 | } |
698 | ||
6385315d AD |
699 | $post_needs_update = false; |
700 | ||
8d0ec6fd | 701 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && |
6385315d AD |
702 | ($content_hash != $orig_content_hash)) { |
703 | $post_needs_update = true; | |
704 | } | |
705 | ||
706 | if ($orig_title != $entry_title) { | |
707 | $post_needs_update = true; | |
708 | } | |
709 | ||
11b0dce2 AD |
710 | if ($orig_num_comments != $num_comments) { |
711 | $post_needs_update = true; | |
712 | } | |
713 | ||
6385315d AD |
714 | // this doesn't seem to be very reliable |
715 | // | |
716 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
717 | // $post_needs_update = true; | |
718 | // } | |
719 | ||
720 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 721 | // linking to this post as updated |
6385315d AD |
722 | if ($post_needs_update) { |
723 | ||
724 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
725 | ||
6385315d | 726 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
727 | SET title = '$entry_title', content = '$entry_content', |
728 | num_comments = '$num_comments' | |
6385315d AD |
729 | WHERE id = '$ref_id'"); |
730 | ||
8d0ec6fd | 731 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { |
4919fb42 AD |
732 | db_query($link, "UPDATE ttrss_user_entries |
733 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
734 | } else { | |
735 | db_query($link, "UPDATE ttrss_user_entries | |
736 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
737 | } | |
6385315d AD |
738 | |
739 | } | |
4c193675 AD |
740 | } |
741 | ||
44e241cb AD |
742 | db_query($link, "COMMIT"); |
743 | ||
eb36b4eb AD |
744 | /* taaaags */ |
745 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
746 | ||
05732aa0 | 747 | $entry_tags = null; |
eb36b4eb | 748 | |
372ced8b | 749 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
750 | $entry_content_unescaped, $entry_tags); |
751 | ||
752 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
753 | // print_r($entry_tags); | |
372ced8b | 754 | // print "<br>"; |
eb36b4eb AD |
755 | |
756 | $entry_tags = $entry_tags[1]; | |
757 | ||
073ca0e6 AD |
758 | # check for manual tags |
759 | ||
760 | if ($filter_name == "tag") { | |
761 | ||
762 | $manual_tags = trim_array(split(",", $filter_param)); | |
763 | ||
764 | foreach ($manual_tags as $tag) { | |
df74052f | 765 | if (!preg_match("/^[0-9]*$/", $tag) && $tag != '') { |
073ca0e6 AD |
766 | array_push($entry_tags, $tag); |
767 | } | |
768 | } | |
769 | } | |
770 | ||
eb36b4eb AD |
771 | if (count($entry_tags) > 0) { |
772 | ||
44e241cb AD |
773 | db_query($link, "BEGIN"); |
774 | ||
05732aa0 AD |
775 | $result = db_query($link, "SELECT id,int_id |
776 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 777 | WHERE guid = '$entry_guid' |
05732aa0 | 778 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 779 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 780 | |
fe99ab12 | 781 | if (db_num_rows($result) == 1) { |
eb36b4eb | 782 | |
fe99ab12 AD |
783 | $entry_id = db_fetch_result($result, 0, "id"); |
784 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
785 | ||
786 | foreach ($entry_tags as $tag) { | |
787 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
788 | |
789 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
790 | $tag = str_replace("technorati tag: ", "", $tag); |
791 | ||
792 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
793 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
794 | owner_uid = '$owner_uid' LIMIT 1"); | |
795 | ||
796 | // print db_fetch_result($result, 0, "id"); | |
797 | ||
798 | if ($result && db_num_rows($result) == 0) { | |
799 | ||
800 | // print "tagging $entry_id as $tag<br>"; | |
801 | ||
802 | db_query($link, "INSERT INTO ttrss_tags | |
803 | (owner_uid,tag_name,post_int_id) | |
804 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
805 | } | |
806 | } | |
eb36b4eb | 807 | } |
44e241cb | 808 | db_query($link, "COMMIT"); |
05732aa0 | 809 | } |
4c193675 | 810 | } |
40d13c28 | 811 | |
ab3d0b99 AD |
812 | db_query($link, "UPDATE ttrss_feeds |
813 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 814 | |
44e241cb | 815 | // db_query($link, "COMMIT"); |
dd8c76a9 | 816 | |
ab3d0b99 AD |
817 | } else { |
818 | $error_msg = db_escape_string(magpie_error()); | |
819 | db_query($link, | |
aa5f9f5f AD |
820 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
821 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
822 | } |
823 | ||
824 | } | |
825 | ||
f175937c | 826 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 827 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
828 | foreach ($values as $v) { |
829 | if ($v == $default) | |
830 | $sel = " selected"; | |
831 | else | |
832 | $sel = ""; | |
833 | ||
834 | print "<option$sel>$v</option>"; | |
835 | } | |
836 | print "</select>"; | |
837 | } | |
40d13c28 | 838 | |
79f3553b AD |
839 | function print_select_hash($id, $default, $values, $attributes = "") { |
840 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
841 | foreach (array_keys($values) as $v) { |
842 | if ($v == $default) | |
843 | $sel = "selected"; | |
844 | else | |
845 | $sel = ""; | |
846 | ||
847 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
848 | } | |
849 | ||
850 | print "</select>"; | |
851 | } | |
852 | ||
19c9cb11 | 853 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
854 | |
855 | if ($filters["title"]) { | |
19c9cb11 AD |
856 | foreach ($filters["title"] as $filter) { |
857 | $reg_exp = $filter["reg_exp"]; | |
858 | if (preg_match("/$reg_exp/i", $title)) { | |
073ca0e6 | 859 | return array($filter["action"], $filter["action_param"]); |
19c9cb11 | 860 | } |
e6155a06 AD |
861 | } |
862 | } | |
863 | ||
864 | if ($filters["content"]) { | |
19c9cb11 AD |
865 | foreach ($filters["content"] as $filter) { |
866 | $reg_exp = $filter["reg_exp"]; | |
867 | if (preg_match("/$reg_exp/i", $content)) { | |
073ca0e6 | 868 | return array($filter["action"], $filter["action_param"]); |
19c9cb11 | 869 | } |
e6155a06 AD |
870 | } |
871 | } | |
872 | ||
873 | if ($filters["both"]) { | |
874 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
875 | $reg_exp = $filter["reg_exp"]; |
876 | if (preg_match("/$reg_exp/i", $title) || | |
877 | preg_match("/$reg_exp/i", $content)) { | |
073ca0e6 | 878 | return array($filter["action"], $filter["action_param"]); |
19c9cb11 | 879 | } |
e6155a06 AD |
880 | } |
881 | } | |
882 | ||
3a933f22 | 883 | if ($filters["link"]) { |
19c9cb11 AD |
884 | $reg_exp = $filter["reg_exp"]; |
885 | foreach ($filters["link"] as $filter) { | |
886 | $reg_exp = $filter["reg_exp"]; | |
887 | if (preg_match("/$reg_exp/i", $link)) { | |
073ca0e6 | 888 | return array($filter["action"], $filter["action_param"]); |
19c9cb11 | 889 | } |
3a933f22 AD |
890 | } |
891 | } | |
892 | ||
e6155a06 AD |
893 | return false; |
894 | } | |
895 | ||
240054f1 AD |
896 | function get_filter_matches($title, $content, $link, $filters) { |
897 | ||
898 | $matches = array(); | |
899 | ||
900 | if ($filters["title"]) { | |
901 | foreach ($filters["title"] as $filter) { | |
902 | $reg_exp = $filter["reg_exp"]; | |
903 | if (preg_match("/$reg_exp/i", $title)) { | |
904 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
905 | } | |
906 | } | |
907 | } | |
908 | ||
909 | if ($filters["content"]) { | |
910 | foreach ($filters["content"] as $filter) { | |
911 | $reg_exp = $filter["reg_exp"]; | |
912 | if (preg_match("/$reg_exp/i", $content)) { | |
913 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
914 | } | |
915 | } | |
916 | } | |
917 | ||
918 | if ($filters["both"]) { | |
919 | foreach ($filters["both"] as $filter) { | |
920 | $reg_exp = $filter["reg_exp"]; | |
921 | if (preg_match("/$reg_exp/i", $title) || | |
922 | preg_match("/$reg_exp/i", $content)) { | |
923 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
924 | } | |
925 | } | |
926 | } | |
927 | ||
928 | if ($filters["link"]) { | |
929 | $reg_exp = $filter["reg_exp"]; | |
930 | foreach ($filters["link"] as $filter) { | |
931 | $reg_exp = $filter["reg_exp"]; | |
932 | if (preg_match("/$reg_exp/i", $link)) { | |
933 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
934 | } | |
935 | } | |
936 | } | |
937 | ||
938 | return $matches; | |
939 | } | |
940 | ||
9323147e | 941 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, |
fb1fb4ab | 942 | $rtl_content = false, $last_updated = false, $last_error = false) { |
254e0e4b AD |
943 | |
944 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 945 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 946 | } else { |
023fe037 | 947 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
948 | } |
949 | ||
9323147e AD |
950 | if ($rtl_content) { |
951 | $rtl_tag = "dir=\"rtl\""; | |
952 | } else { | |
953 | $rtl_tag = "dir=\"ltr\""; | |
954 | } | |
955 | ||
78d5212c AD |
956 | $error_notify_msg = ""; |
957 | ||
fb1fb4ab AD |
958 | if ($last_error) { |
959 | $link_title = "Error: $last_error ($last_updated)"; | |
78d5212c | 960 | $error_notify_msg = "(Error)"; |
ad780e9c | 961 | } else if ($last_updated) { |
fb1fb4ab AD |
962 | $link_title = "Updated: $last_updated"; |
963 | } | |
964 | ||
7210613a AD |
965 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" |
966 | href=\"#\" onclick=\"javascript:viewfeed('$feed_id', '', false);\">$feed_title</a>"; | |
254e0e4b AD |
967 | |
968 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 969 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
970 | print "$feed_icon"; |
971 | } | |
972 | ||
9323147e | 973 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; |
254e0e4b AD |
974 | |
975 | if ($unread != 0) { | |
976 | $fctr_class = ""; | |
977 | } else { | |
978 | $fctr_class = "class=\"invisible\""; | |
979 | } | |
980 | ||
9323147e | 981 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> |
254e0e4b | 982 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; |
78d5212c AD |
983 | |
984 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
985 | print "<div class=\"feedExtInfo\"> | |
986 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
987 | } | |
988 | ||
254e0e4b AD |
989 | print "</li>"; |
990 | ||
991 | } | |
992 | ||
406d9489 AD |
993 | function getmicrotime() { |
994 | list($usec, $sec) = explode(" ",microtime()); | |
995 | return ((float)$usec + (float)$sec); | |
996 | } | |
997 | ||
77e96719 AD |
998 | function print_radio($id, $default, $values, $attributes = "") { |
999 | foreach ($values as $v) { | |
1000 | ||
1001 | if ($v == $default) | |
5da169d9 | 1002 | $sel = "checked"; |
77e96719 | 1003 | else |
5da169d9 AD |
1004 | $sel = ""; |
1005 | ||
1006 | if ($v == "Yes") { | |
1007 | $sel .= " value=\"1\""; | |
1008 | } else { | |
1009 | $sel .= " value=\"0\""; | |
1010 | } | |
77e96719 | 1011 | |
69654950 AD |
1012 | print "<input class=\"noborder\" |
1013 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
77e96719 AD |
1014 | |
1015 | } | |
1016 | } | |
1017 | ||
ff485f1d AD |
1018 | function initialize_user_prefs($link, $uid) { |
1019 | ||
1020 | $uid = db_escape_string($uid); | |
1021 | ||
1022 | db_query($link, "BEGIN"); | |
1023 | ||
1024 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
1025 | ||
1026 | $u_result = db_query($link, "SELECT pref_name | |
1027 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
1028 | ||
1029 | $active_prefs = array(); | |
1030 | ||
1031 | while ($line = db_fetch_assoc($u_result)) { | |
1032 | array_push($active_prefs, $line["pref_name"]); | |
1033 | } | |
1034 | ||
1035 | while ($line = db_fetch_assoc($result)) { | |
1036 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
1037 | // print "adding " . $line["pref_name"] . "<br>"; | |
1038 | ||
1039 | db_query($link, "INSERT INTO ttrss_user_prefs | |
1040 | (owner_uid,pref_name,value) VALUES | |
1041 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
1042 | ||
1043 | } | |
1044 | } | |
1045 | ||
1046 | db_query($link, "COMMIT"); | |
1047 | ||
1048 | } | |
956c7629 AD |
1049 | |
1050 | function lookup_user_id($link, $user) { | |
1051 | ||
1052 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
1053 | login = '$login'"); | |
1054 | ||
1055 | if (db_num_rows($result) == 1) { | |
1056 | return db_fetch_result($result, 0, "id"); | |
1057 | } else { | |
1058 | return false; | |
1059 | } | |
1060 | } | |
1061 | ||
18664970 AD |
1062 | function http_authenticate_user($link) { |
1063 | ||
1064 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1065 | ||
1066 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1067 | header('HTTP/1.0 401 Unauthorized'); | |
1068 | exit; | |
1069 | ||
1070 | } else { | |
1071 | $auth_result = authenticate_user($link, | |
1072 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1073 | ||
1074 | if (!$auth_result) { | |
1075 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1076 | header('HTTP/1.0 401 Unauthorized'); | |
1077 | exit; | |
1078 | } | |
1079 | } | |
1080 | ||
1081 | return true; | |
1082 | } | |
1083 | ||
461766f3 | 1084 | function authenticate_user($link, $login, $password, $force_auth = false) { |
c8437f35 | 1085 | |
131b01b3 | 1086 | if (!SINGLE_USER_MODE) { |
c8437f35 | 1087 | |
131b01b3 | 1088 | $pwd_hash = 'SHA1:' . sha1($password); |
461766f3 AD |
1089 | |
1090 | if ($force_auth && defined('_DEBUG_USER_SWITCH')) { | |
1091 | $query = "SELECT id,login,access_level | |
1092 | FROM ttrss_users WHERE | |
1093 | login = '$login'"; | |
1094 | } else { | |
1095 | $query = "SELECT id,login,access_level | |
1096 | FROM ttrss_users WHERE | |
1097 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
1098 | } | |
1099 | ||
1100 | $result = db_query($link, $query); | |
131b01b3 AD |
1101 | |
1102 | if (db_num_rows($result) == 1) { | |
1103 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1104 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1105 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1106 | ||
1107 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1108 | $_SESSION["uid"]); | |
1109 | ||
1110 | $user_theme = get_user_theme_path($link); | |
1111 | ||
1112 | $_SESSION["theme"] = $user_theme; | |
1113 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1114 | ||
1115 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1116 | ||
1117 | return true; | |
1118 | } | |
1119 | ||
1120 | return false; | |
503eb349 | 1121 | |
131b01b3 | 1122 | } else { |
503eb349 | 1123 | |
131b01b3 AD |
1124 | $_SESSION["uid"] = 1; |
1125 | $_SESSION["name"] = "admin"; | |
f557cd78 | 1126 | |
0bbba72d AD |
1127 | $user_theme = get_user_theme_path($link); |
1128 | ||
1129 | $_SESSION["theme"] = $user_theme; | |
1130 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1131 | ||
1132 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1133 | ||
c8437f35 AD |
1134 | return true; |
1135 | } | |
c8437f35 AD |
1136 | } |
1137 | ||
e6cb77a0 AD |
1138 | function make_password($length = 8) { |
1139 | ||
1140 | $password = ""; | |
798f722b AD |
1141 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
1142 | ||
1143 | $i = 0; | |
e6cb77a0 AD |
1144 | |
1145 | while ($i < $length) { | |
1146 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1147 | ||
1148 | if (!strstr($password, $char)) { | |
1149 | $password .= $char; | |
1150 | $i++; | |
1151 | } | |
1152 | } | |
1153 | return $password; | |
1154 | } | |
1155 | ||
1156 | // this is called after user is created to initialize default feeds, labels | |
1157 | // or whatever else | |
1158 | ||
1159 | // user preferences are checked on every login, not here | |
1160 | ||
1161 | function initialize_user($link, $uid) { | |
1162 | ||
1163 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1164 | values ('$uid','unread = true', 'Unread articles')"); | |
1165 | ||
1166 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1167 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1168 | ||
1169 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 1170 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 1171 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
1172 | |
1173 | } | |
e6cb77a0 | 1174 | |
b8aa49bc | 1175 | function logout_user() { |
5ccc1cf5 AD |
1176 | session_destroy(); |
1177 | if (isset($_COOKIE[session_name()])) { | |
1178 | setcookie(session_name(), '', time()-42000, '/'); | |
1179 | } | |
b8aa49bc AD |
1180 | } |
1181 | ||
75836f33 | 1182 | function get_script_urlpath() { |
87a79fa4 | 1183 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
1184 | } |
1185 | ||
1186 | function get_login_redirect() { | |
1187 | $server = $_SERVER["SERVER_NAME"]; | |
1188 | ||
1189 | if (ENABLE_LOGIN_SSL) { | |
1190 | $protocol = "https"; | |
1191 | } else { | |
1192 | $protocol = "http"; | |
1193 | } | |
1194 | ||
1195 | $url_path = get_script_urlpath(); | |
1196 | ||
1197 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
1198 | ||
1199 | return $redirect_uri; | |
1200 | } | |
1201 | ||
916f788a | 1202 | function validate_session($link) { |
a2e9b457 | 1203 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { |
916f788a AD |
1204 | if ($_SESSION["ip_address"]) { |
1205 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1206 | return false; | |
1207 | } | |
1208 | } | |
1209 | } | |
1210 | return true; | |
1211 | } | |
1212 | ||
7ae65adf AD |
1213 | function basic_nosid_redirect_check() { |
1214 | if (!SINGLE_USER_MODE) { | |
3dd46f19 | 1215 | if (!$_COOKIE[get_session_cookie_name()]) { |
7ae65adf AD |
1216 | $redirect_uri = get_login_redirect(); |
1217 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1218 | header("Location: $redirect_uri?rt=$return_to"); | |
1219 | exit; | |
1220 | } | |
1221 | } | |
1222 | } | |
1223 | ||
b8aa49bc AD |
1224 | function login_sequence($link) { |
1225 | if (!SINGLE_USER_MODE) { | |
75836f33 | 1226 | |
461766f3 AD |
1227 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { |
1228 | $swu = db_escape_string($_REQUEST["swu"]); | |
1229 | if ($swu) { | |
1230 | $_SESSION["prefs_cache"] = false; | |
1231 | return authenticate_user($link, $swu, null, true); | |
1232 | } | |
1233 | } | |
1234 | ||
916f788a AD |
1235 | if (!validate_session($link)) { |
1236 | logout_user(); | |
1237 | $redirect_uri = get_login_redirect(); | |
1238 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); | |
1239 | header("Location: $redirect_uri?rt=$return_to"); | |
1240 | exit; | |
1241 | } | |
1242 | ||
b8aa49bc AD |
1243 | if (!USE_HTTP_AUTH) { |
1244 | if (!$_SESSION["uid"]) { | |
75836f33 | 1245 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
1246 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
1247 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
1248 | exit; |
1249 | } | |
1250 | } else { | |
f557cd78 AD |
1251 | if (!$_SESSION["uid"]) { |
1252 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1253 | ||
1254 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1255 | header('HTTP/1.0 401 Unauthorized'); | |
1256 | exit; | |
1257 | ||
1258 | } else { | |
1259 | $auth_result = authenticate_user($link, | |
1260 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1261 | ||
1262 | if (!$auth_result) { | |
1263 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
1264 | header('HTTP/1.0 401 Unauthorized'); | |
1265 | exit; | |
1266 | } | |
1267 | } | |
1268 | } | |
b8aa49bc AD |
1269 | } |
1270 | } else { | |
0bbba72d | 1271 | return authenticate_user($link, "admin", null); |
b8aa49bc AD |
1272 | } |
1273 | } | |
3547842a AD |
1274 | |
1275 | function truncate_string($str, $max_len) { | |
12db369c AD |
1276 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
1277 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
1278 | } else { |
1279 | return $str; | |
1280 | } | |
1281 | } | |
54a60e1a AD |
1282 | |
1283 | function get_user_theme_path($link) { | |
798f722b AD |
1284 | $result = db_query($link, "SELECT theme_path |
1285 | FROM | |
1286 | ttrss_themes,ttrss_users | |
1287 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
1288 | if (db_num_rows($result) != 0) { |
1289 | return db_fetch_result($result, 0, "theme_path"); | |
1290 | } else { | |
1291 | return null; | |
1292 | } | |
1293 | } | |
be773442 AD |
1294 | |
1295 | function smart_date_time($timestamp) { | |
1296 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1297 | return date("G:i", $timestamp); | |
f26450f1 | 1298 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1299 | return date("M d, G:i", $timestamp); |
1300 | } else { | |
b02111c2 | 1301 | return date("Y/m/d G:i", $timestamp); |
be773442 AD |
1302 | } |
1303 | } | |
1304 | ||
1305 | function smart_date($timestamp) { | |
1306 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1307 | return "Today"; | |
f26450f1 | 1308 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
1309 | return date("D m", $timestamp); |
1310 | } else { | |
b02111c2 | 1311 | return date("Y/m/d", $timestamp); |
be773442 AD |
1312 | } |
1313 | } | |
a654a595 AD |
1314 | |
1315 | function sql_bool_to_string($s) { | |
1316 | if ($s == "t" || $s == "1") { | |
1317 | return "true"; | |
1318 | } else { | |
1319 | return "false"; | |
1320 | } | |
1321 | } | |
e3c99f3b AD |
1322 | |
1323 | function sql_bool_to_bool($s) { | |
1324 | if ($s == "t" || $s == "1") { | |
1325 | return true; | |
1326 | } else { | |
1327 | return false; | |
1328 | } | |
1329 | } | |
0ea4fb50 | 1330 | |
e3c99f3b | 1331 | |
0ea4fb50 AD |
1332 | function toggleEvenOdd($a) { |
1333 | if ($a == "even") | |
1334 | return "odd"; | |
1335 | else | |
1336 | return "even"; | |
1337 | } | |
6043fb7e AD |
1338 | |
1339 | function sanity_check($link) { | |
9cbca41f | 1340 | |
aec3ce39 AD |
1341 | error_reporting(0); |
1342 | ||
6043fb7e AD |
1343 | $error_code = 0; |
1344 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1345 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1346 | ||
1347 | if ($schema_version != SCHEMA_VERSION) { | |
1348 | $error_code = 5; | |
1349 | } | |
1350 | ||
aec3ce39 AD |
1351 | if (DB_TYPE == "mysql") { |
1352 | $result = db_query($link, "SELECT true", false); | |
1353 | if (db_num_rows($result) != 1) { | |
1354 | $error_code = 10; | |
1355 | } | |
1356 | } | |
1357 | ||
1358 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1359 | ||
6043fb7e | 1360 | if ($error_code != 0) { |
aec3ce39 | 1361 | print_error_xml($error_code); |
6043fb7e AD |
1362 | return false; |
1363 | } else { | |
1364 | return true; | |
9cbca41f | 1365 | } |
6043fb7e AD |
1366 | } |
1367 | ||
27981ca3 AD |
1368 | function file_is_locked($filename) { |
1369 | error_reporting(0); | |
1370 | $fp = fopen($filename, "r"); | |
1371 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1372 | if ($fp) { | |
1373 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1374 | flock($fp, LOCK_UN); | |
1375 | fclose($fp); | |
1376 | return false; | |
1377 | } | |
1378 | fclose($fp); | |
1379 | return true; | |
1380 | } | |
1381 | return false; | |
1382 | } | |
1383 | ||
fcb4c0c9 AD |
1384 | function make_lockfile($filename) { |
1385 | $fp = fopen($filename, "w"); | |
1386 | ||
1387 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1388 | return $fp; | |
1389 | } else { | |
1390 | return false; | |
1391 | } | |
1392 | } | |
1393 | ||
894ebcf5 AD |
1394 | function sql_random_function() { |
1395 | if (DB_TYPE == "mysql") { | |
1396 | return "RAND()"; | |
1397 | } else { | |
1398 | return "RANDOM()"; | |
1399 | } | |
1400 | } | |
1401 | ||
23aa0d16 | 1402 | function catchup_feed($link, $feed, $cat_view) { |
88040f57 AD |
1403 | |
1404 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
23aa0d16 AD |
1405 | |
1406 | if ($cat_view) { | |
1407 | ||
1408 | if ($feed > 0) { | |
1409 | $cat_qpart = "cat_id = '$feed'"; | |
1410 | } else { | |
1411 | $cat_qpart = "cat_id IS NULL"; | |
1412 | } | |
1413 | ||
1414 | $tmp_result = db_query($link, "SELECT id | |
1415 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1416 | $_SESSION["uid"]); | |
1417 | ||
1418 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1419 | ||
1420 | $tmp_feed = $tmp_line["id"]; | |
1421 | ||
1422 | db_query($link, "UPDATE ttrss_user_entries | |
1423 | SET unread = false,last_read = NOW() | |
1424 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1425 | } | |
1426 | ||
1427 | } else if ($feed > 0) { | |
1428 | ||
1429 | $tmp_result = db_query($link, "SELECT id | |
1430 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1431 | ORDER BY cat_id,title"); | |
1432 | ||
1433 | $parent_ids = array(); | |
1434 | ||
1435 | if (db_num_rows($tmp_result) > 0) { | |
1436 | while ($p = db_fetch_assoc($tmp_result)) { | |
1437 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1438 | } | |
1439 | ||
1440 | $children_qpart = implode(" OR ", $parent_ids); | |
1441 | ||
1442 | db_query($link, "UPDATE ttrss_user_entries | |
1443 | SET unread = false,last_read = NOW() | |
1444 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1445 | AND owner_uid = " . $_SESSION["uid"]); | |
1446 | ||
1447 | } else { | |
1448 | db_query($link, "UPDATE ttrss_user_entries | |
1449 | SET unread = false,last_read = NOW() | |
1450 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1451 | } | |
1452 | ||
1453 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1454 | ||
1455 | if ($feed == -1) { | |
1456 | db_query($link, "UPDATE ttrss_user_entries | |
1457 | SET unread = false,last_read = NOW() | |
1458 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1459 | } | |
1460 | ||
1461 | } else if ($feed < -10) { // label | |
1462 | ||
1463 | // TODO make this more efficient | |
1464 | ||
1465 | $label_id = -$feed - 11; | |
1466 | ||
1467 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1468 | WHERE id = '$label_id'"); | |
1469 | ||
1470 | if ($tmp_result) { | |
1471 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1472 | ||
1473 | db_query($link, "BEGIN"); | |
1474 | ||
1475 | $tmp2_result = db_query($link, | |
1476 | "SELECT | |
1477 | int_id | |
1478 | FROM | |
88040f57 | 1479 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
23aa0d16 | 1480 | WHERE |
88040f57 AD |
1481 | ref_id = ttrss_entries.id AND |
1482 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
23aa0d16 | 1483 | $sql_exp AND |
88040f57 | 1484 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
23aa0d16 AD |
1485 | |
1486 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1487 | db_query($link, "UPDATE | |
1488 | ttrss_user_entries | |
1489 | SET | |
1490 | unread = false, last_read = NOW() | |
1491 | WHERE | |
1492 | int_id = " . $tmp_line["int_id"]); | |
1493 | } | |
1494 | ||
1495 | db_query($link, "COMMIT"); | |
1496 | ||
1497 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1498 | SET unread = false,last_read = NOW() | |
1499 | WHERE $sql_exp | |
1500 | AND ref_id = id | |
1501 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1502 | } | |
1503 | } | |
1504 | } else { // tag | |
1505 | db_query($link, "BEGIN"); | |
1506 | ||
1507 | $tag_name = db_escape_string($feed); | |
1508 | ||
1509 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1510 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1511 | ||
1512 | while ($line = db_fetch_assoc($result)) { | |
1513 | db_query($link, "UPDATE ttrss_user_entries SET | |
1514 | unread = false, last_read = NOW() | |
1515 | WHERE int_id = " . $line["post_int_id"]); | |
1516 | } | |
1517 | db_query($link, "COMMIT"); | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | function update_generic_feed($link, $feed, $cat_view) { | |
1522 | if ($cat_view) { | |
1523 | ||
1524 | if ($feed > 0) { | |
1525 | $cat_qpart = "cat_id = '$feed'"; | |
1526 | } else { | |
1527 | $cat_qpart = "cat_id IS NULL"; | |
1528 | } | |
1529 | ||
1530 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1531 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1532 | ||
1533 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1534 | $feed_url = $tmp_line["feed_url"]; | |
1535 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1536 | } | |
1537 | ||
1538 | } else { | |
1539 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1540 | WHERE id = '$feed'"); | |
1541 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1542 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1543 | } | |
1544 | } | |
a9cb1f83 | 1545 | |
cf4d339c AD |
1546 | function getAllCounters($link, $omode = "tflc") { |
1547 | /* getLabelCounters($link); | |
a9cb1f83 AD |
1548 | getFeedCounters($link); |
1549 | getTagCounters($link); | |
1550 | getGlobalCounters($link); | |
1551 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1552 | getCategoryCounters($link); | |
cf4d339c AD |
1553 | } */ |
1554 | ||
1555 | if (!$omode) $omode = "tflc"; | |
1556 | ||
1557 | getGlobalCounters($link); | |
1558 | ||
1559 | if (strchr($omode, "l")) getLabelCounters($link); | |
1560 | if (strchr($omode, "f")) getFeedCounters($link); | |
1561 | if (strchr($omode, "t")) getTagCounters($link); | |
1562 | if (strchr($omode, "c")) { | |
1563 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1564 | getCategoryCounters($link); | |
1565 | } | |
a9cb1f83 AD |
1566 | } |
1567 | } | |
1568 | ||
1569 | function getCategoryCounters($link) { | |
1570 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1571 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1572 | AND unread = true)) AS unread FROM ttrss_feeds | |
1573 | WHERE | |
cfb02131 | 1574 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); |
a9cb1f83 AD |
1575 | |
1576 | while ($line = db_fetch_assoc($result)) { | |
1577 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1578 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1579 | $line["unread"]."\"/>"; | |
1580 | } | |
1581 | } | |
1582 | ||
f295c368 AD |
1583 | function getCategoryUnread($link, $cat) { |
1584 | ||
18664970 AD |
1585 | if ($cat != 0) { |
1586 | $cat_query = "cat_id = '$cat'"; | |
1587 | } else { | |
1588 | $cat_query = "cat_id IS NULL"; | |
1589 | } | |
1590 | ||
1591 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
cfb02131 | 1592 | AND hidden = false |
f295c368 AD |
1593 | AND owner_uid = " . $_SESSION["uid"]); |
1594 | ||
1595 | $cat_feeds = array(); | |
1596 | while ($line = db_fetch_assoc($result)) { | |
1597 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1598 | } | |
1599 | ||
18664970 AD |
1600 | if (count($cat_feeds) == 0) return 0; |
1601 | ||
f295c368 AD |
1602 | $match_part = implode(" OR ", $cat_feeds); |
1603 | ||
1604 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1605 | FROM ttrss_user_entries | |
4919fb42 | 1606 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); |
f295c368 AD |
1607 | |
1608 | $unread = 0; | |
1609 | ||
1610 | # this needs to be rewritten | |
1611 | while ($line = db_fetch_assoc($result)) { | |
1612 | $unread += $line["unread"]; | |
1613 | } | |
1614 | ||
1615 | return $unread; | |
1616 | ||
1617 | } | |
1618 | ||
1619 | function getFeedUnread($link, $feed, $is_cat = false) { | |
a9cb1f83 | 1620 | $n_feed = sprintf("%d", $feed); |
f295c368 AD |
1621 | |
1622 | if ($is_cat) { | |
831ff047 | 1623 | return getCategoryUnread($link, $n_feed); |
f295c368 | 1624 | } else if ($n_feed == -1) { |
a9cb1f83 | 1625 | $match_part = "marked = true"; |
4919fb42 | 1626 | } else if ($n_feed > 0) { |
831ff047 | 1627 | |
e8b8485f AD |
1628 | $result = db_query($link, "SELECT id FROM ttrss_feeds |
1629 | WHERE parent_feed = '$n_feed' | |
318260cc | 1630 | AND hidden = false |
4919fb42 | 1631 | AND owner_uid = " . $_SESSION["uid"]); |
831ff047 AD |
1632 | |
1633 | if (db_num_rows($result) > 0) { | |
4919fb42 | 1634 | |
831ff047 AD |
1635 | $linked_feeds = array(); |
1636 | while ($line = db_fetch_assoc($result)) { | |
1637 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
1638 | } | |
e8b8485f AD |
1639 | |
1640 | array_push($linked_feeds, "feed_id = $n_feed"); | |
831ff047 AD |
1641 | |
1642 | $match_part = implode(" OR ", $linked_feeds); | |
1643 | ||
4919fb42 | 1644 | $result = db_query($link, "SELECT COUNT(int_id) AS unread |
318260cc | 1645 | FROM ttrss_user_entries |
e8b8485f AD |
1646 | WHERE unread = true AND ($match_part) |
1647 | AND owner_uid = " . $_SESSION["uid"]); | |
4919fb42 AD |
1648 | |
1649 | $unread = 0; | |
1650 | ||
1651 | # this needs to be rewritten | |
1652 | while ($line = db_fetch_assoc($result)) { | |
1653 | $unread += $line["unread"]; | |
1654 | } | |
1655 | ||
1656 | return $unread; | |
1657 | ||
831ff047 AD |
1658 | } else { |
1659 | $match_part = "feed_id = '$n_feed'"; | |
1660 | } | |
a9cb1f83 | 1661 | } else if ($feed < -10) { |
318260cc | 1662 | |
a9cb1f83 AD |
1663 | $label_id = -$feed - 11; |
1664 | ||
1665 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1666 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1667 | ||
1668 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1669 | } | |
1670 | ||
1671 | if ($match_part) { | |
1672 | ||
1673 | $result = db_query($link, "SELECT count(int_id) AS unread | |
88040f57 AD |
1674 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE |
1675 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1676 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
cfb02131 | 1677 | ttrss_feeds.hidden = false AND |
88040f57 | 1678 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); |
a9cb1f83 AD |
1679 | |
1680 | } else { | |
1681 | ||
1682 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1683 | FROM ttrss_tags,ttrss_user_entries | |
1684 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1685 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1686 | } | |
1687 | ||
1688 | $unread = db_fetch_result($result, 0, "unread"); | |
cfb02131 | 1689 | |
a9cb1f83 AD |
1690 | return $unread; |
1691 | } | |
1692 | ||
1693 | /* FIXME this needs reworking */ | |
1694 | ||
f3acc32e AD |
1695 | function getGlobalUnread($link, $user_id = false) { |
1696 | ||
1697 | if (!$user_id) { | |
1698 | $user_id = $_SESSION["uid"]; | |
1699 | } | |
1700 | ||
3831db41 | 1701 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1702 | WHERE unread = true AND |
3831db41 | 1703 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1704 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
3831db41 | 1705 | hidden = false AND |
f3acc32e | 1706 | ttrss_user_entries.owner_uid = '$user_id'"); |
a9cb1f83 AD |
1707 | $c_id = db_fetch_result($result, 0, "c_id"); |
1708 | return $c_id; | |
1709 | } | |
1710 | ||
1711 | function getGlobalCounters($link, $global_unread = -1) { | |
1712 | if ($global_unread == -1) { | |
1713 | $global_unread = getGlobalUnread($link); | |
1714 | } | |
7bf7e4d3 AD |
1715 | print "<counter type=\"global\" id='global-unread' |
1716 | counter='$global_unread'/>"; | |
1717 | ||
1718 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1719 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1720 | ||
1721 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1722 | ||
1723 | print "<counter type=\"global\" id='subscribed-feeds' | |
1724 | counter='$subscribed_feeds'/>"; | |
1725 | ||
a9cb1f83 AD |
1726 | } |
1727 | ||
1728 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1729 | ||
1730 | if ($smart_mode) { | |
1731 | if (!$_SESSION["tctr_last_value"]) { | |
1732 | $_SESSION["tctr_last_value"] = array(); | |
1733 | } | |
1734 | } | |
1735 | ||
1736 | $old_counters = $_SESSION["tctr_last_value"]; | |
1737 | ||
1738 | $tctrs_modified = false; | |
1739 | ||
1740 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1741 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1742 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1743 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1744 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1745 | UNION | |
1746 | select tag_name,0 as count FROM ttrss_tags | |
1747 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1748 | ||
1749 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1750 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1751 | AND unread = true)) AS count FROM ttrss_tags | |
1752 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
1753 | ||
1754 | $tags = array(); | |
1755 | ||
1756 | while ($line = db_fetch_assoc($result)) { | |
1757 | $tags[$line["tag_name"]] += $line["count"]; | |
1758 | } | |
1759 | ||
1760 | foreach (array_keys($tags) as $tag) { | |
1761 | $unread = $tags[$tag]; | |
1762 | ||
1763 | $tag = htmlspecialchars($tag); | |
1764 | ||
1765 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1766 | $old_counters[$tag] = $unread; | |
1767 | $tctrs_modified = true; | |
1768 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1769 | } | |
1770 | ||
1771 | } | |
1772 | ||
1773 | if ($smart_mode && $tctrs_modified) { | |
1774 | $_SESSION["tctr_last_value"] = $old_counters; | |
1775 | } | |
1776 | ||
1777 | } | |
1778 | ||
ef393de7 | 1779 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { |
a9cb1f83 AD |
1780 | |
1781 | if ($smart_mode) { | |
1782 | if (!$_SESSION["lctr_last_value"]) { | |
1783 | $_SESSION["lctr_last_value"] = array(); | |
1784 | } | |
1785 | } | |
1786 | ||
ef393de7 AD |
1787 | $ret_arr = array(); |
1788 | ||
a9cb1f83 AD |
1789 | $old_counters = $_SESSION["lctr_last_value"]; |
1790 | $lctrs_modified = false; | |
1791 | ||
88040f57 | 1792 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds |
a9cb1f83 | 1793 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 AD |
1794 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
1795 | unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
a9cb1f83 AD |
1796 | |
1797 | $count = db_fetch_result($result, 0, "count"); | |
1798 | ||
ef393de7 AD |
1799 | if (!$ret_mode) { |
1800 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1801 | } else { | |
1802 | $ret_arr["-1"]["counter"] = $count; | |
1803 | $ret_arr["-1"]["description"] = "Starred"; | |
1804 | } | |
a9cb1f83 AD |
1805 | |
1806 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1807 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1808 | ||
1809 | while ($line = db_fetch_assoc($result)) { | |
1810 | ||
1811 | $id = -$line["id"] - 11; | |
1812 | ||
ef393de7 AD |
1813 | $label_name = $line["description"]; |
1814 | ||
a9cb1f83 AD |
1815 | error_reporting (0); |
1816 | ||
88040f57 | 1817 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds |
a9cb1f83 | 1818 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND |
cfb02131 | 1819 | ttrss_feeds.hidden = false AND |
88040f57 | 1820 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
a9cb1f83 | 1821 | ttrss_user_entries.ref_id = ttrss_entries.id AND |
88040f57 | 1822 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); |
a9cb1f83 AD |
1823 | |
1824 | $count = db_fetch_result($tmp_result, 0, "count"); | |
1825 | ||
1826 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1827 | $old_counters[$id] = $count; | |
1828 | $lctrs_modified = true; | |
ef393de7 AD |
1829 | if (!$ret_mode) { |
1830 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
1831 | } else { | |
1832 | $ret_arr[$id]["counter"] = $count; | |
1833 | $ret_arr[$id]["description"] = $label_name; | |
1834 | } | |
a9cb1f83 AD |
1835 | } |
1836 | ||
1837 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1838 | } | |
1839 | ||
1840 | if ($smart_mode && $lctrs_modified) { | |
1841 | $_SESSION["lctr_last_value"] = $old_counters; | |
1842 | } | |
ef393de7 AD |
1843 | |
1844 | return $ret_arr; | |
a9cb1f83 AD |
1845 | } |
1846 | ||
1847 | /* function getFeedCounter($link, $id) { | |
1848 | ||
1849 | $result = db_query($link, "SELECT | |
1850 | count(id) as count,last_error | |
1851 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1852 | WHERE feed_id = '$id' AND unread = true | |
1853 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
1854 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
1855 | ||
1856 | $count = db_fetch_result($result, 0, "count"); | |
1857 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
1858 | ||
1859 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
1860 | } */ | |
1861 | ||
1862 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1863 | ||
1864 | if ($smart_mode) { | |
1865 | if (!$_SESSION["fctr_last_value"]) { | |
1866 | $_SESSION["fctr_last_value"] = array(); | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | $old_counters = $_SESSION["fctr_last_value"]; | |
1871 | ||
1872 | $result = db_query($link, "SELECT id,last_error,parent_feed, | |
fb1fb4ab | 1873 | SUBSTRING(last_updated,1,19) AS last_updated, |
a9cb1f83 AD |
1874 | (SELECT count(id) |
1875 | FROM ttrss_entries,ttrss_user_entries | |
1876 | WHERE feed_id = ttrss_feeds.id AND | |
1877 | ttrss_user_entries.ref_id = ttrss_entries.id | |
1878 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
1879 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
1880 | AND parent_feed IS NULL"); | |
1881 | ||
1882 | $fctrs_modified = false; | |
1883 | ||
fb1fb4ab AD |
1884 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); |
1885 | ||
a9cb1f83 AD |
1886 | while ($line = db_fetch_assoc($result)) { |
1887 | ||
1888 | $id = $line["id"]; | |
1889 | $count = $line["count"]; | |
1890 | $last_error = htmlspecialchars($line["last_error"]); | |
fb1fb4ab AD |
1891 | |
1892 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
1893 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
1894 | } else { | |
1895 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
1896 | } | |
1897 | ||
a9cb1f83 AD |
1898 | $has_img = is_file(ICONS_DIR . "/$id.ico"); |
1899 | ||
1900 | $tmp_result = db_query($link, | |
1901 | "SELECT id,COUNT(unread) AS unread | |
1902 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
1903 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
1904 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
1905 | ||
1906 | if (db_num_rows($tmp_result) > 0) { | |
1907 | while ($l = db_fetch_assoc($tmp_result)) { | |
1908 | $count += $l["unread"]; | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | if (!$smart_mode || $old_counters[$id] != $count) { | |
1913 | $old_counters[$id] = $count; | |
1914 | $fctrs_modified = true; | |
1915 | ||
1916 | if ($last_error) { | |
1917 | $error_part = "error=\"$last_error\""; | |
1918 | } else { | |
1919 | $error_part = ""; | |
1920 | } | |
1921 | ||
1922 | if ($has_img) { | |
1923 | $has_img_part = "hi=\"$has_img\""; | |
1924 | } else { | |
1925 | $has_img_part = ""; | |
1926 | } | |
1927 | ||
fb1fb4ab | 1928 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; |
a9cb1f83 AD |
1929 | } |
1930 | } | |
1931 | ||
1932 | if ($smart_mode && $fctrs_modified) { | |
1933 | $_SESSION["fctr_last_value"] = $old_counters; | |
1934 | } | |
1935 | } | |
1936 | ||
1b758780 AD |
1937 | function get_script_dt_add() { |
1938 | if (strpos(VERSION, "99") === false) { | |
1939 | return VERSION; | |
1940 | } else { | |
1941 | return time(); | |
1942 | } | |
1943 | } | |
1944 | ||
6e7f8d26 AD |
1945 | function get_pgsql_version($link) { |
1946 | $result = db_query($link, "SELECT version() AS version"); | |
1947 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
1948 | return $version[1]; | |
1949 | } | |
1950 | ||
af106b0e AD |
1951 | function print_error_xml($code, $add_msg = "") { |
1952 | global $ERRORS; | |
1953 | ||
1954 | $error_msg = $ERRORS[$code]; | |
1955 | ||
1956 | if ($add_msg) { | |
1957 | $error_msg = "$error_msg; $add_msg"; | |
1958 | } | |
1959 | ||
4c2abbc1 | 1960 | print "<rpc-reply>"; |
af106b0e | 1961 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; |
4c2abbc1 | 1962 | print "</rpc-reply>"; |
af106b0e | 1963 | } |
956c7629 AD |
1964 | |
1965 | function subscribe_to_feed($link, $feed_link, $cat_id = 0) { | |
bb0f29a4 | 1966 | |
c91c2249 AD |
1967 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); |
1968 | ||
1969 | if ($feed_link == "") return; | |
bb0f29a4 | 1970 | |
956c7629 AD |
1971 | if ($cat_id == "0" || !$cat_id) { |
1972 | $cat_qpart = "NULL"; | |
1973 | } else { | |
1974 | $cat_qpart = "'$cat_id'"; | |
1975 | } | |
1976 | ||
1977 | $result = db_query($link, | |
1978 | "SELECT id FROM ttrss_feeds | |
1979 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
1980 | ||
1981 | if (db_num_rows($result) == 0) { | |
1982 | ||
1983 | $result = db_query($link, | |
1984 | "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id) | |
1985 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
1986 | '[Unknown]', $cat_qpart)"); | |
1987 | ||
1988 | $result = db_query($link, | |
1989 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
1990 | AND owner_uid = " . $_SESSION["uid"]); | |
1991 | ||
1992 | $feed_id = db_fetch_result($result, 0, "id"); | |
1993 | ||
1994 | if ($feed_id) { | |
1995 | update_rss_feed($link, $feed_link, $feed_id, true); | |
1996 | } | |
1997 | ||
1998 | return true; | |
1999 | } else { | |
2000 | return false; | |
2001 | } | |
2002 | } | |
2003 | ||
673d54ca AD |
2004 | function print_feed_select($link, $id, $default_id = "", |
2005 | $attributes = "", $include_all_feeds = true) { | |
2006 | ||
79f3553b | 2007 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca | 2008 | if ($include_all_feeds) { |
79f3553b | 2009 | print "<option value=\"0\">All feeds</option>"; |
673d54ca AD |
2010 | } |
2011 | ||
2012 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
2013 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2014 | ||
2015 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
2016 | print "<option disabled>--------</option>"; | |
2017 | } | |
2018 | ||
2019 | while ($line = db_fetch_assoc($result)) { | |
2020 | if ($line["id"] == $default_id) { | |
2021 | $is_selected = "selected"; | |
2022 | } else { | |
2023 | $is_selected = ""; | |
2024 | } | |
79f3553b | 2025 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 2026 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
2027 | } |
2028 | ||
2029 | print "</select>"; | |
2030 | } | |
2031 | ||
2032 | function print_feed_cat_select($link, $id, $default_id = "", | |
2033 | $attributes = "", $include_all_cats = true) { | |
2034 | ||
79f3553b | 2035 | print "<select id=\"$id\" name=\"$id\" $attributes>"; |
673d54ca AD |
2036 | |
2037 | if ($include_all_cats) { | |
14f69488 | 2038 | print "<option value=\"0\">Uncategorized</option>"; |
673d54ca AD |
2039 | } |
2040 | ||
2041 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2042 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2043 | ||
2044 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2045 | print "<option disabled>--------</option>"; | |
2046 | } | |
2047 | ||
2048 | while ($line = db_fetch_assoc($result)) { | |
2049 | if ($line["id"] == $default_id) { | |
2050 | $is_selected = "selected"; | |
2051 | } else { | |
2052 | $is_selected = ""; | |
2053 | } | |
14f69488 | 2054 | printf("<option $is_selected value='%d'>%s</option>", |
07164479 | 2055 | $line["id"], htmlspecialchars(db_unescape_string($line["title"]))); |
673d54ca AD |
2056 | } |
2057 | ||
2058 | print "</select>"; | |
2059 | } | |
2060 | ||
14f69488 AD |
2061 | function checkbox_to_sql_bool($val) { |
2062 | return ($val == "on") ? "true" : "false"; | |
2063 | } | |
86b682ce AD |
2064 | |
2065 | function getFeedCatTitle($link, $id) { | |
2066 | if ($id == -1) { | |
2067 | return "Special"; | |
2068 | } else if ($id < -10) { | |
2069 | return "Labels"; | |
2070 | } else if ($id > 0) { | |
2071 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2072 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2073 | cat_id = ttrss_feed_categories.id"); | |
2074 | if (db_num_rows($result) == 1) { | |
2075 | return db_fetch_result($result, 0, "title"); | |
2076 | } else { | |
eb4311d0 | 2077 | return "Uncategorized"; |
86b682ce AD |
2078 | } |
2079 | } else { | |
2080 | return "getFeedCatTitle($id) failed"; | |
2081 | } | |
2082 | ||
2083 | } | |
2084 | ||
2085 | function getFeedTitle($link, $id) { | |
2086 | if ($id == -1) { | |
2087 | return "Starred articles"; | |
2088 | } else if ($id < -10) { | |
2089 | $label_id = -10 - $id; | |
2090 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
2091 | if (db_num_rows($result) == 1) { | |
2092 | return db_fetch_result($result, 0, "description"); | |
2093 | } else { | |
2094 | return "Unknown label ($label_id)"; | |
2095 | } | |
2096 | ||
2097 | } else if ($id > 0) { | |
2098 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2099 | if (db_num_rows($result) == 1) { | |
2100 | return db_fetch_result($result, 0, "title"); | |
2101 | } else { | |
2102 | return "Unknown feed ($id)"; | |
2103 | } | |
2104 | } else { | |
2105 | return "getFeedTitle($id) failed"; | |
2106 | } | |
2107 | ||
2108 | } | |
3dd46f19 AD |
2109 | |
2110 | function get_session_cookie_name() { | |
2111 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2112 | } | |
3ac2b520 AD |
2113 | |
2114 | function print_init_params($link) { | |
2115 | print "<init-params>"; | |
2116 | if ($_SESSION["stored-params"]) { | |
2117 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
5f57b06d AD |
2118 | if ($key) { |
2119 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2120 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2121 | } | |
3ac2b520 AD |
2122 | } |
2123 | } | |
2124 | ||
2125 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2126 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
0d51e25d | 2127 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; |
3ac2b520 AD |
2128 | |
2129 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2130 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2131 | ||
e8bd0da9 AD |
2132 | print "<param key=\"hide_read_feeds\" value=\"" . |
2133 | sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>"; | |
2134 | ||
c9268ed5 AD |
2135 | print "<param key=\"feeds_sort_by_unread\" value=\"" . |
2136 | sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>"; | |
2137 | ||
f6d6e22f AD |
2138 | print "<param key=\"confirm_feed_catchup\" value=\"" . |
2139 | sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>"; | |
2140 | ||
3ac2b520 AD |
2141 | print "</init-params>"; |
2142 | } | |
f54f515f AD |
2143 | |
2144 | function print_runtime_info($link) { | |
2145 | print "<runtime-info>"; | |
71ad883b AD |
2146 | if (ENABLE_UPDATE_DAEMON) { |
2147 | print "<param key=\"daemon_is_running\" value=\"". | |
2148 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2149 | } | |
f54f515f AD |
2150 | print "</runtime-info>"; |
2151 | } | |
ef393de7 | 2152 | |
88040f57 | 2153 | function getSearchSql($search, $match_on) { |
ef393de7 | 2154 | |
88040f57 | 2155 | $search_query_part = ""; |
e20c9d88 | 2156 | |
88040f57 AD |
2157 | $keywords = split(" ", $search); |
2158 | $query_keywords = array(); | |
e20c9d88 | 2159 | |
88040f57 | 2160 | if ($match_on == "both") { |
e20c9d88 | 2161 | |
88040f57 AD |
2162 | foreach ($keywords as $k) { |
2163 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2164 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2165 | } | |
e20c9d88 | 2166 | |
88040f57 | 2167 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2168 | |
88040f57 | 2169 | } else if ($match_on == "title") { |
e20c9d88 | 2170 | |
88040f57 AD |
2171 | foreach ($keywords as $k) { |
2172 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2173 | } | |
e20c9d88 | 2174 | |
88040f57 | 2175 | $search_query_part = implode("AND", $query_keywords) . " AND "; |
e20c9d88 | 2176 | |
88040f57 AD |
2177 | } else if ($match_on == "content") { |
2178 | ||
2179 | foreach ($keywords as $k) { | |
2180 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | $search_query_part = implode("AND", $query_keywords); | |
2185 | ||
2186 | return $search_query_part; | |
2187 | } | |
2188 | ||
2189 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false) { | |
88040f57 AD |
2190 | if ($search) { |
2191 | ||
2192 | $search_query_part = getSearchSql($search, $match_on); | |
2193 | $search_query_part .= " AND "; | |
e20c9d88 | 2194 | |
ef393de7 AD |
2195 | } else { |
2196 | $search_query_part = ""; | |
2197 | } | |
2198 | ||
2199 | $view_query_part = ""; | |
2200 | ||
2201 | if ($view_mode == "adaptive") { | |
2202 | if ($search) { | |
2203 | $view_query_part = " "; | |
2204 | } else if ($feed != -1) { | |
f295c368 | 2205 | $unread = getFeedUnread($link, $feed, $cat_view); |
ef393de7 AD |
2206 | if ($unread > 0) { |
2207 | $view_query_part = " unread = true AND "; | |
2208 | } | |
2209 | } | |
2210 | } | |
2211 | ||
2212 | if ($view_mode == "marked") { | |
2213 | $view_query_part = " marked = true AND "; | |
2214 | } | |
2215 | ||
2216 | if ($view_mode == "unread") { | |
2217 | $view_query_part = " unread = true AND "; | |
2218 | } | |
2219 | ||
2220 | if ($limit > 0) { | |
2221 | $limit_query_part = "LIMIT " . $limit; | |
2222 | } | |
2223 | ||
2224 | $vfeed_query_part = ""; | |
2225 | ||
2226 | // override query strategy and enable feed display when searching globally | |
2227 | if ($search && $search_mode == "all_feeds") { | |
2228 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2229 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2230 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2231 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2232 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2233 | id = feed_id) as feed_title,"; | |
2234 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2235 | ||
2236 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
0a6c4846 AD |
2237 | |
2238 | $tmp_result = false; | |
2239 | ||
2240 | if ($cat_view) { | |
2241 | $tmp_result = db_query($link, "SELECT id | |
2242 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2243 | } else { | |
2244 | $tmp_result = db_query($link, "SELECT id | |
2245 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2246 | WHERE id = '$feed') AND id != '$feed'"); | |
2247 | } | |
ef393de7 AD |
2248 | |
2249 | $cat_siblings = array(); | |
2250 | ||
2251 | if (db_num_rows($tmp_result) > 0) { | |
2252 | while ($p = db_fetch_assoc($tmp_result)) { | |
2253 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2254 | } | |
2255 | ||
2256 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2257 | $feed, implode(" OR ", $cat_siblings)); | |
2258 | ||
2259 | } else { | |
2260 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2261 | } | |
2262 | ||
2263 | } else if ($feed >= 0) { | |
2264 | ||
2265 | if ($cat_view) { | |
5c365f60 | 2266 | |
ef393de7 AD |
2267 | if ($feed > 0) { |
2268 | $query_strategy_part = "cat_id = '$feed'"; | |
2269 | } else { | |
2270 | $query_strategy_part = "cat_id IS NULL"; | |
2271 | } | |
2272 | ||
2273 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
5c365f60 | 2274 | |
ef393de7 AD |
2275 | } else { |
2276 | $tmp_result = db_query($link, "SELECT id | |
2277 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2278 | ORDER BY cat_id,title"); | |
2279 | ||
2280 | $parent_ids = array(); | |
2281 | ||
2282 | if (db_num_rows($tmp_result) > 0) { | |
2283 | while ($p = db_fetch_assoc($tmp_result)) { | |
2284 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2285 | } | |
2286 | ||
2287 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2288 | $feed, implode(" OR ", $parent_ids)); | |
2289 | ||
2290 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2291 | } else { | |
2292 | $query_strategy_part = "feed_id = '$feed'"; | |
2293 | } | |
2294 | } | |
2295 | } else if ($feed == -1) { // starred virtual feed | |
2296 | $query_strategy_part = "marked = true"; | |
2297 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2298 | } else if ($feed <= -10) { // labels | |
2299 | $label_id = -$feed - 11; | |
2300 | ||
2301 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2302 | WHERE id = '$label_id'"); | |
2303 | ||
2304 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2305 | ||
2306 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2307 | } else { | |
2308 | $query_strategy_part = "id > 0"; // dumb | |
2309 | } | |
d6e5706d AD |
2310 | |
2311 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2312 | $order_by = "updated"; | |
2313 | } else { | |
2314 | $order_by = "updated DESC"; | |
2315 | } | |
e939722a AD |
2316 | |
2317 | if ($override_order) { | |
2318 | $order_by = $override_order; | |
2319 | } | |
ef393de7 AD |
2320 | |
2321 | $feed_title = ""; | |
2322 | ||
2323 | if ($search && $search_mode == "all_feeds") { | |
2324 | $feed_title = "Global search results ($search)"; | |
2325 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
e1eb2147 | 2326 | $feed_title = "Tag search results ($search, $feed)"; |
ef393de7 AD |
2327 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { |
2328 | $feed_title = $feed; | |
2329 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2330 | ||
2331 | if ($cat_view) { | |
5c365f60 | 2332 | |
ef393de7 AD |
2333 | if ($feed != 0) { |
2334 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
2335 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2336 | $feed_title = db_fetch_result($result, 0, "title"); | |
2337 | } else { | |
2338 | $feed_title = "Uncategorized"; | |
2339 | } | |
e1eb2147 AD |
2340 | |
2341 | if ($search) { | |
2342 | $feed_title = "Category search results ($search, $feed_title)"; | |
2343 | } | |
2344 | ||
ef393de7 AD |
2345 | } else { |
2346 | ||
2347 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
2348 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
2349 | ||
2350 | $feed_title = db_fetch_result($result, 0, "title"); | |
2351 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2352 | $last_error = db_fetch_result($result, 0, "last_error"); | |
e1eb2147 AD |
2353 | |
2354 | if ($search) { | |
2355 | $feed_title = "Feed search results ($search, $feed_title)"; | |
2356 | } | |
ef393de7 AD |
2357 | } |
2358 | ||
2359 | } else if ($feed == -1) { | |
2360 | $feed_title = "Starred articles"; | |
2361 | } else if ($feed < -10) { | |
2362 | $label_id = -$feed - 11; | |
2363 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2364 | WHERE id = '$label_id'"); | |
2365 | $feed_title = db_fetch_result($result, 0, "description"); | |
88040f57 AD |
2366 | |
2367 | if ($search) { | |
2368 | $feed_title = "Label search results ($search, $feed_title)"; | |
2369 | } | |
ef393de7 AD |
2370 | } else { |
2371 | $feed_title = "?"; | |
2372 | } | |
2373 | ||
2374 | $feed_title = db_unescape_string($feed_title); | |
2375 | ||
2376 | if ($feed < -10) error_reporting (0); | |
2377 | ||
2378 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2379 | ||
2380 | if ($feed >= 0) { | |
2381 | $feed_kind = "Feeds"; | |
2382 | } else { | |
2383 | $feed_kind = "Labels"; | |
2384 | } | |
2385 | ||
2386 | $content_query_part = "content as content_preview,"; | |
2387 | ||
2388 | $query = "SELECT | |
1f64b1be | 2389 | guid, |
ef393de7 AD |
2390 | ttrss_entries.id,ttrss_entries.title, |
2391 | SUBSTRING(updated,1,16) as updated, | |
2392 | unread,feed_id,marked,link,last_read, | |
2393 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2394 | $vfeed_query_part | |
2395 | $content_query_part | |
d4b4b9de AD |
2396 | SUBSTRING(updated,1,19) as updated_noms, |
2397 | author | |
ef393de7 AD |
2398 | FROM |
2399 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2400 | WHERE | |
cfb02131 | 2401 | ttrss_feeds.hidden = false AND |
ef393de7 AD |
2402 | ttrss_user_entries.feed_id = ttrss_feeds.id AND |
2403 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2404 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2405 | $search_query_part | |
2406 | $view_query_part | |
2407 | $query_strategy_part ORDER BY $order_by | |
2408 | $limit_query_part"; | |
2409 | ||
2410 | $result = db_query($link, $query); | |
2411 | ||
2412 | if ($_GET["debug"]) print $query; | |
2413 | ||
2414 | } else { | |
2415 | // browsing by tag | |
2416 | ||
2417 | $feed_kind = "Tags"; | |
2418 | ||
2419 | $result = db_query($link, "SELECT | |
1f64b1be | 2420 | guid, |
ef393de7 AD |
2421 | ttrss_entries.id as id,title, |
2422 | SUBSTRING(updated,1,16) as updated, | |
2423 | unread,feed_id, | |
2424 | marked,link,last_read, | |
2425 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2426 | $vfeed_query_part | |
2427 | $content_query_part | |
2428 | SUBSTRING(updated,1,19) as updated_noms | |
2429 | FROM | |
2430 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
2431 | WHERE | |
2432 | ref_id = ttrss_entries.id AND | |
2433 | ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND | |
2434 | post_int_id = int_id AND tag_name = '$feed' AND | |
2435 | $view_query_part | |
2436 | $search_query_part | |
2437 | $query_strategy_part ORDER BY $order_by | |
2438 | $limit_query_part"); | |
2439 | } | |
2440 | ||
c7188969 | 2441 | return array($result, $feed_title, $feed_site_url, $last_error); |
ef393de7 AD |
2442 | |
2443 | } | |
2444 | ||
e1eb2147 | 2445 | function generate_syndicated_feed($link, $feed, $is_cat, |
3baeeeca | 2446 | $search, $search_mode, $match_on) { |
18664970 AD |
2447 | |
2448 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
e939722a | 2449 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC"); |
18664970 AD |
2450 | |
2451 | $result = $qfh_ret[0]; | |
59e2aab4 | 2452 | $feed_title = htmlspecialchars($qfh_ret[1]); |
18664970 AD |
2453 | $feed_site_url = $qfh_ret[2]; |
2454 | $last_error = $qfh_ret[3]; | |
2455 | ||
3baeeeca AD |
2456 | print "<rss version=\"2.0\"> |
2457 | <channel> | |
2458 | <title>$feed_title</title> | |
2459 | <link>$feed_site_url</link> | |
2460 | <generator>Tiny Tiny RSS v".VERSION."</generator>"; | |
2461 | ||
2462 | while ($line = db_fetch_assoc($result)) { | |
2463 | print "<item>"; | |
2464 | print "<id>" . htmlspecialchars($line["guid"]) . "</id>"; | |
2465 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
2466 | ||
2467 | $rfc822_date = date('r', strtotime($line["updated"])); | |
2468 | ||
2469 | print "<pubDate>$rfc822_date</pubDate>"; | |
2470 | ||
2471 | print "<title>" . | |
2472 | htmlspecialchars($line["title"]) . "</title>"; | |
2473 | ||
2474 | print "<description>" . | |
2475 | htmlspecialchars($line["content_preview"]) . "</description>"; | |
2476 | ||
2477 | print "</item>"; | |
2478 | } | |
2479 | ||
2480 | print "</channel></rss>"; | |
18664970 AD |
2481 | |
2482 | } | |
2483 | ||
0a6c4846 AD |
2484 | function getCategoryTitle($link, $cat_id) { |
2485 | ||
2486 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
2487 | id = '$cat_id'"); | |
2488 | ||
2489 | if (db_num_rows($result) == 1) { | |
2490 | return db_fetch_result($result, 0, "title"); | |
2491 | } else { | |
2492 | return "Uncategorized"; | |
2493 | } | |
2494 | } | |
2495 | ||
183ad07b | 2496 | function sanitize_rss($str) { |
60452879 | 2497 | $res = $str; |
183ad07b AD |
2498 | |
2499 | $res = preg_replace('/<script.*?>/i', | |
60452879 | 2500 | "<p class=\"scriptWarn\">Disabled script: ", $res); |
183ad07b | 2501 | |
60452879 AD |
2502 | $res = preg_replace('/<\/script.*?>/i', "</p>", $res); |
2503 | ||
8511f62f | 2504 | /* $res = preg_replace('/<embed.*?>/i', "", $res); |
183ad07b | 2505 | |
a262b161 AD |
2506 | $res = preg_replace('/<object.*?>.*?<\/object>/i', |
2507 | "<p class=\"objectWarn\">(Disabled html object | |
8511f62f | 2508 | - flash or other embedded content)</p>", $res); */ |
a262b161 | 2509 | |
183ad07b AD |
2510 | return $res; |
2511 | } | |
b72c3ef8 | 2512 | |
9cd7c995 AD |
2513 | function send_headlines_digests($link, $limit = 100) { |
2514 | ||
1ddba275 AD |
2515 | if (!DIGEST_ENABLE) return false; |
2516 | ||
9cd7c995 | 2517 | $user_limit = DIGEST_EMAIL_LIMIT; |
5430c959 | 2518 | $days = 1; |
9cd7c995 AD |
2519 | |
2520 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
2521 | ||
2522 | if (DB_TYPE == "pgsql") { | |
2523 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
2524 | } else if (DB_TYPE == "mysql") { | |
2525 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2526 | } | |
2527 | ||
2528 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
2529 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
2530 | ||
2531 | while ($line = db_fetch_assoc($result)) { | |
2532 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
2533 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
2534 | ||
2535 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); | |
2536 | $digest = $tuple[0]; | |
2537 | $headlines_count = $tuple[1]; | |
2538 | ||
2539 | if ($headlines_count > 0) { | |
2540 | $rc = mail($line["login"] . " <" . $line["email"] . ">", | |
2541 | "[tt-rss] New headlines for last 24 hours", $digest, | |
3ab3c1f0 AD |
2542 | "From: " . MAIL_FROM . "\n". |
2543 | "Content-Type: text/plain; charset=\"utf-8\"\n". | |
2544 | "Content-Transfer-Encoding: 8bit\n"); | |
9cd7c995 AD |
2545 | print "RC=$rc\n"; |
2546 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
2547 | WHERE id = " . $line["id"]); | |
2548 | } else { | |
2549 | print "No headlines\n"; | |
2550 | } | |
2551 | } | |
2552 | } | |
2553 | ||
2554 | // $digest = prepare_headlines_digest($link, $user_id, $days, $limit); | |
2555 | ||
2556 | } | |
2557 | ||
7e3634d9 AD |
2558 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { |
2559 | $tmp = "New headlines for last 24 hours, as of " . date("Y/m/d H:m") . "\n"; | |
2560 | $tmp .= "=======================================================\n\n"; | |
2561 | ||
2562 | if (DB_TYPE == "pgsql") { | |
9cd7c995 | 2563 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; |
7e3634d9 AD |
2564 | } else if (DB_TYPE == "mysql") { |
2565 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2566 | } | |
2567 | ||
2568 | $result = db_query($link, "SELECT ttrss_entries.title, | |
2569 | ttrss_feeds.title AS feed_title, | |
2570 | date_entered, | |
2571 | link, | |
2572 | SUBSTRING(last_updated,1,19) AS last_updated | |
2573 | FROM | |
2574 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2575 | WHERE | |
2576 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
3dd9183c | 2577 | AND include_in_digest = true |
7e3634d9 | 2578 | AND $interval_query |
448b0abd | 2579 | AND ttrss_user_entries.owner_uid = $user_id |
7e3634d9 AD |
2580 | AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC |
2581 | LIMIT $limit"); | |
2582 | ||
2583 | $cur_feed_title = ""; | |
2584 | ||
9cd7c995 AD |
2585 | $headlines_count = db_num_rows($result); |
2586 | ||
7e3634d9 AD |
2587 | while ($line = db_fetch_assoc($result)) { |
2588 | $updated = smart_date_time(strtotime($line["last_updated"])); | |
2589 | $feed_title = $line["feed_title"]; | |
2590 | ||
2591 | if ($cur_feed_title != $feed_title) { | |
2592 | $cur_feed_title = $feed_title; | |
2593 | ||
2594 | $tmp .= "$feed_title\n\n"; | |
2595 | } | |
2596 | ||
2597 | $tmp .= " * " . trim($line["title"]) . " - $updated\n"; | |
2598 | $tmp .= " " . trim($line["link"]) . "\n"; | |
2599 | $tmp .= "\n"; | |
2600 | } | |
2601 | ||
2602 | $tmp .= "--- \n"; | |
dfe6f833 AD |
2603 | $tmp .= "You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at " . |
2604 | DIGEST_HOSTNAME . "\n". | |
2605 | "To unsubscribe, visit your configuration options or contact instance owner.\n"; | |
7e3634d9 AD |
2606 | |
2607 | ||
9cd7c995 | 2608 | return array($tmp, $headlines_count); |
7e3634d9 AD |
2609 | } |
2610 | ||
b72c3ef8 AD |
2611 | function check_for_update($link) { |
2612 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
2613 | ||
2614 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
2615 | return; | |
2616 | } | |
2617 | ||
2618 | error_reporting(0); | |
2619 | $rss = fetch_rss($releases_feed); | |
2620 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2621 | ||
2622 | if ($rss) { | |
2623 | ||
2624 | $items = $rss->items; | |
2625 | ||
2626 | if (!$items || !is_array($items)) $items = $rss->entries; | |
2627 | if (!$items || !is_array($items)) $items = $rss; | |
2628 | ||
da412ad3 | 2629 | if (!is_array($items) || count($items) == 0) { |
b72c3ef8 | 2630 | return; |
da412ad3 | 2631 | } |
b72c3ef8 | 2632 | |
a41d2c65 | 2633 | $latest_item = $items[0]; |
b72c3ef8 | 2634 | |
a41d2c65 | 2635 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"])); |
b72c3ef8 | 2636 | |
48e1a342 AD |
2637 | $release_url = sanitize_rss($latest_item["link"]); |
2638 | $content = sanitize_rss($latest_item["description"]); | |
2639 | ||
a41d2c65 | 2640 | if (version_compare(VERSION, $latest_version) == -1) { |
8a797376 | 2641 | return "<div class=\"notice\"><a href=\"javascript:showBlockElement('milestoneDetails')\"> |
a41d2c65 | 2642 | New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a> |
8a797376 | 2643 | <div id=\"milestoneDetails\">$content</div></div>"; |
da412ad3 | 2644 | } |
b72c3ef8 AD |
2645 | } |
2646 | } | |
472782e8 | 2647 | |
18eddb2c AD |
2648 | function markArticlesById($link, $ids, $cmode) { |
2649 | ||
2650 | $tmp_ids = array(); | |
2651 | ||
2652 | foreach ($ids as $id) { | |
2653 | array_push($tmp_ids, "ref_id = '$id'"); | |
2654 | } | |
2655 | ||
2656 | $ids_qpart = join(" OR ", $tmp_ids); | |
2657 | ||
2658 | if ($cmode == 0) { | |
2659 | db_query($link, "UPDATE ttrss_user_entries SET | |
2660 | marked = false,last_read = NOW() | |
2661 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2662 | } else if ($cmode == 1) { | |
2663 | db_query($link, "UPDATE ttrss_user_entries SET | |
2664 | marked = true | |
2665 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2666 | } else { | |
2667 | db_query($link, "UPDATE ttrss_user_entries SET | |
2668 | marked = NOT marked,last_read = NOW() | |
2669 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2670 | } | |
2671 | } | |
2672 | ||
472782e8 AD |
2673 | function catchupArticlesById($link, $ids, $cmode) { |
2674 | ||
2675 | $tmp_ids = array(); | |
2676 | ||
2677 | foreach ($ids as $id) { | |
2678 | array_push($tmp_ids, "ref_id = '$id'"); | |
2679 | } | |
2680 | ||
2681 | $ids_qpart = join(" OR ", $tmp_ids); | |
2682 | ||
2683 | if ($cmode == 0) { | |
2684 | db_query($link, "UPDATE ttrss_user_entries SET | |
2685 | unread = false,last_read = NOW() | |
2686 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2687 | } else if ($cmode == 1) { | |
2688 | db_query($link, "UPDATE ttrss_user_entries SET | |
2689 | unread = true | |
2690 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2691 | } else { | |
2692 | db_query($link, "UPDATE ttrss_user_entries SET | |
2693 | unread = NOT unread,last_read = NOW() | |
2694 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2695 | } | |
2696 | } | |
2697 | ||
a262b161 AD |
2698 | function escape_for_form($s) { |
2699 | return htmlspecialchars(db_unescape_string($s)); | |
2700 | } | |
2701 | ||
1f64b1be AD |
2702 | function make_guid_from_title($title) { |
2703 | return preg_replace("/[ \"\',.:;]/", "-", | |
2704 | mb_strtolower(strip_tags($title))); | |
2705 | } | |
2706 | ||
11befbb2 AD |
2707 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, |
2708 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
2709 | $is_cat = false, $search = false, $match_on = false, | |
2710 | $search_mode = false) { | |
2711 | ||
2712 | if (!$bottom) { | |
2713 | $class = "headlinesSubToolbar"; | |
2714 | $tid = "headlineActionsTop"; | |
2715 | } else { | |
2716 | $class = "headlinesSubToolbar"; | |
2717 | $tid = "headlineActionsBottom"; | |
2718 | } | |
2719 | ||
2720 | print "<table class=\"$class\" id=\"$tid\" | |
2721 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; | |
2722 | ||
2723 | if ($rtl_content) { | |
2724 | $rtl_cpart = "RTL"; | |
2725 | } else { | |
2726 | $rtl_cpart = ""; | |
2727 | } | |
2728 | ||
2729 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
2730 | ||
2731 | print "<td class=\"headlineActions$rtl_cpart\"> | |
2732 | Select: | |
2733 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)\">All</a>, | |
2734 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)\">Unread</a>, | |
2735 | <a href=\"javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)\">None</a> | |
2736 | | |
2737 | Toggle: <a href=\"javascript:selectionToggleUnread()\">Unread</a>, | |
2738 | <a href=\"javascript:selectionToggleMarked()\">Starred</a> | |
2739 | | |
2740 | Mark as read: | |
2741 | <a href=\"#\" onclick=\"catchupPage()\">Page</a>, | |
2742 | <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>"; | |
2743 | print "</td>"; | |
2744 | ||
f2650917 | 2745 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { |
11befbb2 AD |
2746 | print "<td class=\"headlineActions$rtl_cpart\"> |
2747 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
2748 | '$match_on', '$feed_id', '$is_cat');\"> | |
2749 | Convert this search to label</a></td>"; | |
2750 | } | |
2751 | ||
2752 | } else { | |
2753 | ||
2754 | print "<td class=\"headlineActions$rtl_cpart\"> | |
2755 | Select: | |
2756 | <a href=\"javascript:cdmSelectArticles('all')\">All</a>, | |
2757 | <a href=\"javascript:cdmSelectArticles('unread')\">Unread</a>, | |
2758 | <a href=\"javascript:cdmSelectArticles('none')\">None</a> | |
2759 | | |
2760 | Toggle: <a href=\"javascript:selectionToggleUnread(true)\">Unread</a>, | |
b2128f01 AD |
2761 | <a href=\"javascript:selectionToggleMarked(true)\">Starred</a> |
2762 | | |
2763 | Mark as read: | |
2764 | <a href=\"#\" onclick=\"catchupPage()\">Page</a>, | |
2765 | <a href=\"#\" onclick=\"catchupCurrentFeed()\">Feed</a>"; | |
11befbb2 AD |
2766 | |
2767 | print "</td>"; | |
2768 | ||
2769 | } | |
2770 | ||
2771 | print "<td class=\"headlineTitle$rtl_cpart\">"; | |
2772 | ||
2773 | if ($feed_site_url) { | |
2774 | if (!$bottom) { | |
2775 | $target = "target=\"_blank\""; | |
2776 | } | |
2777 | print "<a $target href=\"$feed_site_url\">$feed_title</a>"; | |
2778 | } else { | |
2779 | print $feed_title; | |
2780 | } | |
2781 | ||
2782 | if ($search) { | |
2783 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
2784 | } | |
2785 | ||
2786 | if (!$bottom) { | |
2787 | print " | |
2788 | <a target=\"_new\" | |
2789 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> | |
2790 | <img class=\"noborder\" | |
2791 | alt=\"Generated feed\" src=\"images/feed-icon-12x12.png\"> | |
2792 | </a>"; | |
2793 | } | |
2794 | ||
2795 | print "</td>"; | |
2796 | print "</tr></table>"; | |
2797 | ||
2798 | } | |
2799 | ||
f407c086 AD |
2800 | function outputFeedList($link, $tags = false) { |
2801 | ||
2802 | print "<ul class=\"feedList\" id=\"feedList\">\n"; | |
2803 | ||
2804 | $owner_uid = $_SESSION["uid"]; | |
2805 | ||
cf4d339c | 2806 | /* virtual feeds */ |
f407c086 | 2807 | |
cf4d339c AD |
2808 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
2809 | print "<li class=\"feedCat\">Special</li>"; | |
2810 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
2811 | } | |
f407c086 | 2812 | |
cf4d339c | 2813 | $num_starred = getFeedUnread($link, -1); |
f407c086 | 2814 | |
cf4d339c | 2815 | $class = "virt"; |
f407c086 | 2816 | |
cf4d339c | 2817 | if ($num_starred > 0) $class .= "Unread"; |
f407c086 | 2818 | |
cf4d339c AD |
2819 | printFeedEntry(-1, $class, "Starred articles", $num_starred, |
2820 | "images/mark_set.png", $link); | |
f407c086 | 2821 | |
cf4d339c AD |
2822 | if (get_pref($link, 'ENABLE_FEED_CATS')) { |
2823 | print "</ul>\n"; | |
2824 | } | |
f407c086 | 2825 | |
cf4d339c | 2826 | if (!$tags) { |
f407c086 AD |
2827 | |
2828 | if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) { | |
2829 | ||
2830 | $result = db_query($link, "SELECT id,sql_exp,description FROM | |
2831 | ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description"); | |
2832 | ||
2833 | if (db_num_rows($result) > 0) { | |
2834 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2835 | print "<li class=\"feedCat\">Labels</li>"; | |
2836 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
2837 | } else { | |
2838 | print "<li><hr></li>"; | |
2839 | } | |
2840 | } | |
2841 | ||
2842 | while ($line = db_fetch_assoc($result)) { | |
2843 | ||
2844 | error_reporting (0); | |
2845 | ||
2846 | $label_id = -$line['id'] - 11; | |
2847 | $count = getFeedUnread($link, $label_id); | |
2848 | ||
2849 | $class = "label"; | |
2850 | ||
2851 | if ($count > 0) { | |
2852 | $class .= "Unread"; | |
2853 | } | |
2854 | ||
2855 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2856 | ||
2857 | printFeedEntry($label_id, | |
2858 | $class, db_unescape_string($line["description"]), | |
2859 | $count, "images/label.png", $link); | |
2860 | ||
2861 | } | |
2862 | ||
2863 | if (db_num_rows($result) > 0) { | |
2864 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2865 | print "</ul>"; | |
2866 | } | |
2867 | } | |
2868 | ||
2869 | } | |
2870 | ||
2871 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
2872 | print "<li><hr></li>"; | |
2873 | } | |
2874 | ||
2875 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
2876 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
2877 | $order_by_qpart = "category,unread DESC,title"; | |
2878 | } else { | |
2879 | $order_by_qpart = "category,title"; | |
2880 | } | |
2881 | } else { | |
2882 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
2883 | $order_by_qpart = "unread DESC,title"; | |
2884 | } else { | |
2885 | $order_by_qpart = "title"; | |
2886 | } | |
2887 | } | |
2888 | ||
2889 | $result = db_query($link, "SELECT ttrss_feeds.*, | |
2890 | SUBSTRING(last_updated,1,19) AS last_updated_noms, | |
2891 | (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries | |
2892 | WHERE feed_id = ttrss_feeds.id AND unread = true | |
2893 | AND ttrss_user_entries.ref_id = ttrss_entries.id | |
2894 | AND owner_uid = '$owner_uid') as unread, | |
2895 | cat_id,last_error, | |
2896 | ttrss_feed_categories.title AS category, | |
2897 | ttrss_feed_categories.collapsed | |
2898 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
2899 | ON (ttrss_feed_categories.id = cat_id) | |
2900 | WHERE | |
2901 | ttrss_feeds.hidden = false AND | |
2902 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
2903 | ORDER BY $order_by_qpart"); | |
2904 | ||
2905 | $actid = $_GET["actid"]; | |
2906 | ||
2907 | /* real feeds */ | |
2908 | ||
2909 | $lnum = 0; | |
2910 | ||
2911 | $total_unread = 0; | |
2912 | ||
2913 | $category = ""; | |
2914 | ||
2915 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
2916 | ||
2917 | while ($line = db_fetch_assoc($result)) { | |
2918 | ||
2919 | $feed = db_unescape_string($line["title"]); | |
2920 | $feed_id = $line["id"]; | |
2921 | ||
2922 | $subop = $_GET["subop"]; | |
2923 | ||
2924 | $unread = $line["unread"]; | |
2925 | ||
2926 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2927 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
2928 | } else { | |
2929 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
2930 | } | |
2931 | ||
2932 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
2933 | ||
2934 | if ($rtl_content) { | |
2935 | $rtl_tag = "dir=\"RTL\""; | |
2936 | } else { | |
2937 | $rtl_tag = ""; | |
2938 | } | |
2939 | ||
2940 | $tmp_result = db_query($link, | |
2941 | "SELECT id,COUNT(unread) AS unread | |
2942 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
2943 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
2944 | WHERE parent_feed = '$feed_id' AND unread = true | |
2945 | GROUP BY ttrss_feeds.id"); | |
2946 | ||
2947 | if (db_num_rows($tmp_result) > 0) { | |
2948 | while ($l = db_fetch_assoc($tmp_result)) { | |
2949 | $unread += $l["unread"]; | |
2950 | } | |
2951 | } | |
2952 | ||
2953 | $cat_id = $line["cat_id"]; | |
2954 | ||
2955 | $tmp_category = $line["category"]; | |
2956 | ||
2957 | if (!$tmp_category) { | |
2958 | $tmp_category = "Uncategorized"; | |
2959 | } | |
2960 | ||
2961 | // $class = ($lnum % 2) ? "even" : "odd"; | |
2962 | ||
2963 | if ($line["last_error"]) { | |
2964 | $class = "error"; | |
2965 | } else { | |
2966 | $class = "feed"; | |
2967 | } | |
2968 | ||
2969 | if ($unread > 0) $class .= "Unread"; | |
2970 | ||
2971 | if ($actid == $feed_id) { | |
2972 | $class .= "Selected"; | |
2973 | } | |
2974 | ||
2975 | $total_unread += $unread; | |
2976 | ||
2977 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
2978 | ||
2979 | if ($category) { | |
2980 | print "</ul></li>"; | |
2981 | } | |
2982 | ||
2983 | $category = $tmp_category; | |
2984 | ||
2985 | $collapsed = $line["collapsed"]; | |
2986 | ||
2987 | // workaround for NULL category | |
2988 | if ($category == "Uncategorized") { | |
2989 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { | |
2990 | $collapsed = "t"; | |
2991 | } | |
2992 | } | |
2993 | ||
2994 | if ($collapsed == "t" || $collapsed == "1") { | |
2995 | $holder_class = "invisible"; | |
2996 | $ellipsis = "..."; | |
2997 | } else { | |
2998 | $holder_class = ""; | |
2999 | $ellipsis = ""; | |
3000 | } | |
3001 | ||
3002 | $cat_id = sprintf("%d", $cat_id); | |
3003 | ||
3004 | $cat_unread = getCategoryUnread($link, $cat_id); | |
67dabe1a AD |
3005 | |
3006 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3007 | ||
f407c086 | 3008 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> |
67dabe1a | 3009 | <a id=\"FCATN-$cat_id\" href=\"#\"onclick=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a> |
7210613a | 3010 | <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\"> |
67dabe1a AD |
3011 | <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" |
3012 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis | |
f407c086 AD |
3013 | </a></li>"; |
3014 | ||
3015 | // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function | |
3016 | // -> keyboard navigation, etc. | |
3017 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">"; | |
3018 | } | |
3019 | ||
3020 | printFeedEntry($feed_id, $class, $feed, $unread, | |
3021 | "icons/$feed_id.ico", $link, $rtl_content, | |
3022 | $last_updated, $line["last_error"]); | |
3023 | ||
3024 | ++$lnum; | |
3025 | } | |
3026 | ||
3027 | if (db_num_rows($result) == 0) { | |
3028 | print "<li>No feeds to display.</li>"; | |
3029 | } | |
3030 | ||
3031 | } else { | |
3032 | ||
3033 | // tags | |
3034 | ||
3035 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
3036 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
3037 | post_int_id = ttrss_user_entries.int_id AND | |
3038 | unread = true AND ref_id = ttrss_entries.id | |
3039 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
3040 | UNION | |
3041 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
3042 | ORDER BY tag_name"); */ | |
3043 | ||
3044 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3045 | print "<li class=\"feedCat\">Tags</li>"; | |
3046 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
3047 | } | |
3048 | ||
3049 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
3050 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
3051 | AND unread = true)) AS count FROM ttrss_tags | |
3052 | WHERE owner_uid = 2 GROUP BY tag_name ORDER BY tag_name"); | |
3053 | ||
3054 | $tags = array(); | |
3055 | ||
3056 | while ($line = db_fetch_assoc($result)) { | |
3057 | $tags[$line["tag_name"]] += $line["count"]; | |
3058 | } | |
3059 | ||
3060 | foreach (array_keys($tags) as $tag) { | |
3061 | ||
3062 | $unread = $tags[$tag]; | |
3063 | ||
3064 | $class = "tag"; | |
3065 | ||
3066 | if ($unread > 0) { | |
3067 | $class .= "Unread"; | |
3068 | } | |
3069 | ||
3070 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
3071 | ||
3072 | } | |
3073 | ||
3074 | if (db_num_rows($result) == 0) { | |
3075 | print "<li>No tags to display.</li>"; | |
3076 | } | |
3077 | ||
3078 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3079 | print "</ul>\n"; | |
3080 | } | |
3081 | ||
3082 | } | |
3083 | ||
3084 | print "</ul>"; | |
3085 | ||
3086 | } | |
3087 | ||
0b126ac2 AD |
3088 | function get_article_tags($link, $id) { |
3089 | ||
3090 | $a_id = db_escape_string($id); | |
3091 | ||
3092 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM | |
3093 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
3094 | ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."') ORDER BY tag_name"); | |
3095 | ||
3096 | $tags = array(); | |
3097 | ||
3098 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
3099 | array_push($tags, $tmp_line["tag_name"]); | |
3100 | } | |
3101 | ||
3102 | return $tags; | |
3103 | } | |
3104 | ||
d62a3b63 AD |
3105 | function trim_value(&$value) { |
3106 | $value = trim($value); | |
3107 | } | |
3108 | ||
3109 | function trim_array($array) { | |
3110 | $tmp = $array; | |
3111 | array_walk($tmp, 'trim_value'); | |
3112 | return $tmp; | |
3113 | } | |
3114 | ||
40d13c28 | 3115 | ?> |