]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | /* if ($_GET["debug"]) { | |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); | |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
7 | } */ | |
8 | ||
9 | require_once 'config.php'; | |
10 | ||
11 | if (ENABLE_TRANSLATIONS == true) { | |
12 | require_once "accept-to-gettext.php"; | |
13 | require_once "gettext/gettext.inc"; | |
14 | ||
15 | function startup_gettext() { | |
16 | ||
17 | # Get locale from Accept-Language header | |
18 | $lang = al2gt(array("en_US", "ru_RU"), "text/html"); | |
19 | ||
20 | if ($lang) { | |
21 | _setlocale(LC_MESSAGES, $lang); | |
22 | _bindtextdomain("messages", "locale"); | |
23 | _textdomain("messages"); | |
24 | _bind_textdomain_codeset("messages", "UTF-8"); | |
25 | } | |
26 | } | |
27 | ||
28 | startup_gettext(); | |
29 | ||
30 | } else { | |
31 | function __($msg) { | |
32 | return $msg; | |
33 | } | |
34 | function startup_gettext() { | |
35 | // no-op | |
36 | return true; | |
37 | } | |
38 | } | |
39 | ||
40 | require_once 'db-prefs.php'; | |
41 | require_once 'compat.php'; | |
42 | require_once 'errors.php'; | |
43 | require_once 'version.php'; | |
44 | ||
45 | define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')'); | |
46 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); | |
47 | ||
48 | require_once "magpierss/rss_fetch.inc"; | |
49 | require_once 'magpierss/rss_utils.inc'; | |
50 | ||
51 | include_once "tw/tw-config.php"; | |
52 | include_once "tw/tw.php"; | |
53 | include_once TW_SETUP . "paranoya.php"; | |
54 | ||
55 | $tw_parser = new twParser(); | |
56 | ||
57 | function _debug($msg) { | |
58 | $ts = strftime("%H:%M:%S", time()); | |
59 | print "[$ts] $msg\n"; | |
60 | } | |
61 | ||
62 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { | |
63 | ||
64 | $rows = -1; | |
65 | ||
66 | if (DB_TYPE == "pgsql") { | |
67 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
68 | marked = false AND feed_id = '$feed_id' AND | |
69 | (SELECT date_entered FROM ttrss_entries WHERE | |
70 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */ | |
71 | ||
72 | $pg_version = get_pgsql_version($link); | |
73 | ||
74 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
75 | ||
76 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
77 | ttrss_entries.id = ref_id AND | |
78 | marked = false AND | |
79 | feed_id = '$feed_id' AND | |
80 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
81 | ||
82 | } else { | |
83 | ||
84 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
85 | USING ttrss_entries | |
86 | WHERE ttrss_entries.id = ref_id AND | |
87 | marked = false AND | |
88 | feed_id = '$feed_id' AND | |
89 | ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
90 | } | |
91 | ||
92 | $rows = pg_affected_rows($result); | |
93 | ||
94 | } else { | |
95 | ||
96 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
97 | marked = false AND feed_id = '$feed_id' AND | |
98 | (SELECT date_entered FROM ttrss_entries WHERE | |
99 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ | |
100 | ||
101 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
102 | USING ttrss_user_entries, ttrss_entries | |
103 | WHERE ttrss_entries.id = ref_id AND | |
104 | marked = false AND | |
105 | feed_id = '$feed_id' AND | |
106 | ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
107 | ||
108 | $rows = mysql_affected_rows($link); | |
109 | ||
110 | } | |
111 | ||
112 | if ($debug) { | |
113 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); | |
114 | } | |
115 | } | |
116 | ||
117 | function global_purge_old_posts($link, $do_output = false, $limit = false) { | |
118 | ||
119 | $random_qpart = sql_random_function(); | |
120 | ||
121 | if ($limit) { | |
122 | $limit_qpart = "LIMIT $limit"; | |
123 | } else { | |
124 | $limit_qpart = ""; | |
125 | } | |
126 | ||
127 | $result = db_query($link, | |
128 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds | |
129 | ORDER BY $random_qpart $limit_qpart"); | |
130 | ||
131 | while ($line = db_fetch_assoc($result)) { | |
132 | ||
133 | $feed_id = $line["id"]; | |
134 | $purge_interval = $line["purge_interval"]; | |
135 | $owner_uid = $line["owner_uid"]; | |
136 | ||
137 | if ($purge_interval == 0) { | |
138 | ||
139 | $tmp_result = db_query($link, | |
140 | "SELECT value FROM ttrss_user_prefs WHERE | |
141 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
142 | ||
143 | if (db_num_rows($tmp_result) != 0) { | |
144 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
145 | } | |
146 | } | |
147 | ||
148 | if ($do_output) { | |
149 | // print "Feed $feed_id: purge interval = $purge_interval\n"; | |
150 | } | |
151 | ||
152 | if ($purge_interval > 0) { | |
153 | purge_feed($link, $feed_id, $purge_interval, $do_output); | |
154 | } | |
155 | } | |
156 | ||
157 | // purge orphaned posts in main content table | |
158 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
159 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
160 | ||
161 | } | |
162 | ||
163 | function purge_old_posts($link) { | |
164 | ||
165 | $user_id = $_SESSION["uid"]; | |
166 | ||
167 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
168 | WHERE owner_uid = '$user_id'"); | |
169 | ||
170 | while ($line = db_fetch_assoc($result)) { | |
171 | ||
172 | $feed_id = $line["id"]; | |
173 | $purge_interval = $line["purge_interval"]; | |
174 | ||
175 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); | |
176 | ||
177 | if ($purge_interval > 0) { | |
178 | purge_feed($link, $feed_id, $purge_interval); | |
179 | } | |
180 | } | |
181 | ||
182 | // purge orphaned posts in main content table | |
183 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
184 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
185 | } | |
186 | ||
187 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { | |
188 | ||
189 | if (WEB_DEMO_MODE) return; | |
190 | ||
191 | if (!$user_id) { | |
192 | $user_id = $_SESSION["uid"]; | |
193 | purge_old_posts($link); | |
194 | } | |
195 | ||
196 | // db_query($link, "BEGIN"); | |
197 | ||
198 | if (MAX_UPDATE_TIME > 0) { | |
199 | if (DB_TYPE == "mysql") { | |
200 | $q_order = "RAND()"; | |
201 | } else { | |
202 | $q_order = "RANDOM()"; | |
203 | } | |
204 | } else { | |
205 | $q_order = "last_updated DESC"; | |
206 | } | |
207 | ||
208 | $result = db_query($link, "SELECT feed_url,id, | |
209 | SUBSTRING(last_updated,1,19) AS last_updated, | |
210 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' | |
211 | ORDER BY $q_order"); | |
212 | ||
213 | $upd_start = time(); | |
214 | ||
215 | while ($line = db_fetch_assoc($result)) { | |
216 | $upd_intl = $line["update_interval"]; | |
217 | ||
218 | if (!$upd_intl || $upd_intl == 0) { | |
219 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false); | |
220 | } | |
221 | ||
222 | if ($upd_intl < 0) { | |
223 | // Updates for this feed are disabled | |
224 | continue; | |
225 | } | |
226 | ||
227 | if ($fetch || (!$line["last_updated"] || | |
228 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
229 | ||
230 | // print "<!-- feed: ".$line["feed_url"]." -->"; | |
231 | ||
232 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); | |
233 | ||
234 | $upd_elapsed = time() - $upd_start; | |
235 | ||
236 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
237 | return; | |
238 | } | |
239 | } | |
240 | } | |
241 | ||
242 | // db_query($link, "COMMIT"); | |
243 | ||
244 | } | |
245 | ||
246 | function fetch_file_contents($url) { | |
247 | if (USE_CURL_FOR_ICONS) { | |
248 | $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp"); | |
249 | ||
250 | $ch = curl_init($url); | |
251 | $fp = fopen($tmpfile, "w"); | |
252 | ||
253 | if ($fp) { | |
254 | curl_setopt($ch, CURLOPT_FILE, $fp); | |
255 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); | |
256 | curl_setopt($ch, CURLOPT_TIMEOUT, 45); | |
257 | curl_exec($ch); | |
258 | curl_close($ch); | |
259 | fclose($fp); | |
260 | } | |
261 | ||
262 | $contents = file_get_contents($tmpfile); | |
263 | unlink($tmpfile); | |
264 | ||
265 | return $contents; | |
266 | ||
267 | } else { | |
268 | return file_get_contents($url); | |
269 | } | |
270 | ||
271 | } | |
272 | ||
273 | // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
274 | // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
275 | ||
276 | function get_favicon_url($url) { | |
277 | ||
278 | if ($html = @fetch_file_contents($url)) { | |
279 | ||
280 | if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) { | |
281 | // Attempt to grab a favicon link from their webpage url | |
282 | $linkUrl = html_entity_decode($matches[1]); | |
283 | ||
284 | if (substr($linkUrl, 0, 1) == '/') { | |
285 | $urlParts = parse_url($url); | |
286 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl; | |
287 | } else if (substr($linkUrl, 0, 7) == 'http://') { | |
288 | $faviconURL = $linkUrl; | |
289 | } else if (substr($url, -1, 1) == '/') { | |
290 | $faviconURL = $url.$linkUrl; | |
291 | } else { | |
292 | $faviconURL = $url.'/'.$linkUrl; | |
293 | } | |
294 | ||
295 | } else { | |
296 | // If unsuccessful, attempt to "guess" the favicon location | |
297 | $urlParts = parse_url($url); | |
298 | $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico'; | |
299 | } | |
300 | } | |
301 | ||
302 | // Run a test to see if what we have attempted to get actually exists. | |
303 | if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) { | |
304 | return $faviconURL; | |
305 | } else { | |
306 | return false; | |
307 | } | |
308 | } | |
309 | ||
310 | function url_validate($link) { | |
311 | ||
312 | $url_parts = @parse_url($link); | |
313 | ||
314 | if ( empty( $url_parts["host"] ) ) | |
315 | return false; | |
316 | ||
317 | if ( !empty( $url_parts["path"] ) ) { | |
318 | $documentpath = $url_parts["path"]; | |
319 | } else { | |
320 | $documentpath = "/"; | |
321 | } | |
322 | ||
323 | if ( !empty( $url_parts["query"] ) ) | |
324 | $documentpath .= "?" . $url_parts["query"]; | |
325 | ||
326 | $host = $url_parts["host"]; | |
327 | $port = $url_parts["port"]; | |
328 | ||
329 | if ( empty($port) ) | |
330 | $port = "80"; | |
331 | ||
332 | $socket = @fsockopen( $host, $port, $errno, $errstr, 30 ); | |
333 | ||
334 | if ( !$socket ) | |
335 | return false; | |
336 | ||
337 | fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n"); | |
338 | ||
339 | $http_response = fgets( $socket, 22 ); | |
340 | ||
341 | $responses = "/(200 OK)|(30[0-9] Moved)/"; | |
342 | if ( preg_match($responses, $http_response) ) { | |
343 | fclose($socket); | |
344 | return true; | |
345 | } else { | |
346 | return false; | |
347 | } | |
348 | ||
349 | } | |
350 | ||
351 | function check_feed_favicon($site_url, $feed, $link) { | |
352 | $favicon_url = get_favicon_url($site_url); | |
353 | ||
354 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
355 | ||
356 | error_reporting(0); | |
357 | ||
358 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
359 | ||
360 | if ($favicon_url && !file_exists($icon_file)) { | |
361 | $contents = fetch_file_contents($favicon_url); | |
362 | ||
363 | $fp = fopen($icon_file, "w"); | |
364 | ||
365 | if ($fp) { | |
366 | fwrite($fp, $contents); | |
367 | fclose($fp); | |
368 | chmod($icon_file, 0644); | |
369 | } | |
370 | } | |
371 | ||
372 | error_reporting(DEFAULT_ERROR_LEVEL); | |
373 | ||
374 | } | |
375 | ||
376 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { | |
377 | ||
378 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { | |
379 | return; | |
380 | } | |
381 | ||
382 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
383 | _debug("update_rss_feed: start"); | |
384 | } | |
385 | ||
386 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass | |
387 | FROM ttrss_feeds WHERE id = '$feed'"); | |
388 | ||
389 | $auth_login = db_fetch_result($result, 0, "auth_login"); | |
390 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
391 | ||
392 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
393 | ||
394 | if ($update_interval < 0) { return; } | |
395 | ||
396 | $feed = db_escape_string($feed); | |
397 | ||
398 | $fetch_url = $feed_url; | |
399 | ||
400 | if ($auth_login && $auth_pass) { | |
401 | $url_parts = array(); | |
402 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
403 | ||
404 | if ($url_parts[1] && $url_parts[2]) { | |
405 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
406 | } | |
407 | ||
408 | } | |
409 | ||
410 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
411 | _debug("update_rss_feed: fetching..."); | |
412 | } | |
413 | ||
414 | if (!defined('DAEMON_EXTENDED_DEBUG')) { | |
415 | error_reporting(0); | |
416 | } | |
417 | ||
418 | $rss = fetch_rss($fetch_url); | |
419 | ||
420 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
421 | _debug("update_rss_feed: fetch done, parsing..."); | |
422 | } else { | |
423 | error_reporting (DEFAULT_ERROR_LEVEL); | |
424 | } | |
425 | ||
426 | $feed = db_escape_string($feed); | |
427 | ||
428 | if ($rss) { | |
429 | ||
430 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
431 | _debug("update_rss_feed: processing feed data..."); | |
432 | } | |
433 | ||
434 | // db_query($link, "BEGIN"); | |
435 | ||
436 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid | |
437 | FROM ttrss_feeds WHERE id = '$feed'"); | |
438 | ||
439 | $registered_title = db_fetch_result($result, 0, "title"); | |
440 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
441 | $orig_site_url = db_fetch_result($result, 0, "site_url"); | |
442 | ||
443 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
444 | ||
445 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) { | |
446 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
447 | _debug("update_rss_feed: checking favicon..."); | |
448 | } | |
449 | check_feed_favicon($rss->channel["link"], $feed, $link); | |
450 | } | |
451 | ||
452 | if (!$registered_title || $registered_title == "[Unknown]") { | |
453 | ||
454 | $feed_title = db_escape_string($rss->channel["title"]); | |
455 | ||
456 | if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) { | |
457 | _debug("update_rss_feed: registering title: $feed_title"); | |
458 | } | |
459 | ||
460 | db_query($link, "UPDATE ttrss_feeds SET | |
461 | title = '$feed_title' WHERE id = '$feed'"); | |
462 | } | |
463 | ||
464 | $site_url = $rss->channel["link"]; | |
465 | // weird, weird Magpie | |
466 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
467 | ||
468 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
469 | db_query($link, "UPDATE ttrss_feeds SET | |
470 | site_url = '$site_url' WHERE id = '$feed'"); | |
471 | } | |
472 | ||
473 | // print "I: " . $rss->channel["image"]["url"]; | |
474 | ||
475 | $icon_url = $rss->image["url"]; | |
476 | ||
477 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { | |
478 | $icon_url = db_escape_string($icon_url); | |
479 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
480 | } | |
481 | ||
482 | if (defined('DAEMON_EXTENDED_DEBUG' || $_GET['xdebug'])) { | |
483 | _debug("update_rss_feed: loading filters..."); | |
484 | } | |
485 | ||
486 | $filters = array(); | |
487 | ||
488 | $result = db_query($link, "SELECT reg_exp, | |
489 | ttrss_filter_types.name AS name, | |
490 | ttrss_filter_actions.name AS action, | |
491 | inverse, | |
492 | action_param | |
493 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
494 | enabled = true AND | |
495 | owner_uid = $owner_uid AND | |
496 | ttrss_filter_types.id = filter_type AND | |
497 | ttrss_filter_actions.id = action_id AND | |
498 | (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp"); | |
499 | ||
500 | while ($line = db_fetch_assoc($result)) { | |
501 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); | |
502 | ||
503 | $filter["reg_exp"] = $line["reg_exp"]; | |
504 | $filter["action"] = $line["action"]; | |
505 | $filter["action_param"] = $line["action_param"]; | |
506 | $filter["inverse"] = sql_bool_to_bool($line["inverse"]); | |
507 | ||
508 | array_push($filters[$line["name"]], $filter); | |
509 | } | |
510 | ||
511 | $iterator = $rss->items; | |
512 | ||
513 | if (!$iterator || !is_array($iterator)) $iterator = $rss->entries; | |
514 | if (!$iterator || !is_array($iterator)) $iterator = $rss; | |
515 | ||
516 | if (!is_array($iterator)) { | |
517 | /* db_query($link, "UPDATE ttrss_feeds | |
518 | SET last_error = 'Parse error: can\'t find any articles.' | |
519 | WHERE id = '$feed'"); */ | |
520 | ||
521 | // clear any errors and mark feed as updated if fetched okay | |
522 | // even if it's blank | |
523 | ||
524 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
525 | _debug("update_rss_feed: entry iterator is not an array, no articles?"); | |
526 | } | |
527 | ||
528 | db_query($link, "UPDATE ttrss_feeds | |
529 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
530 | ||
531 | return; // no articles | |
532 | } | |
533 | ||
534 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
535 | _debug("update_rss_feed: processing articles..."); | |
536 | } | |
537 | ||
538 | foreach ($iterator as $item) { | |
539 | ||
540 | $entry_guid = $item["id"]; | |
541 | ||
542 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
543 | if (!$entry_guid) $entry_guid = $item["link"]; | |
544 | if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]); | |
545 | ||
546 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
547 | _debug("update_rss_feed: guid $entry_guid"); | |
548 | } | |
549 | ||
550 | if (!$entry_guid) continue; | |
551 | ||
552 | $entry_timestamp = ""; | |
553 | ||
554 | $rss_2_date = $item['pubdate']; | |
555 | $rss_1_date = $item['dc']['date']; | |
556 | $atom_date = $item['issued']; | |
557 | if (!$atom_date) $atom_date = $item['updated']; | |
558 | ||
559 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); | |
560 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
561 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
562 | ||
563 | if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) { | |
564 | $entry_timestamp = time(); | |
565 | $no_orig_date = 'true'; | |
566 | } else { | |
567 | $no_orig_date = 'false'; | |
568 | } | |
569 | ||
570 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); | |
571 | ||
572 | $entry_title = trim(strip_tags($item["title"])); | |
573 | ||
574 | // strange Magpie workaround | |
575 | $entry_link = $item["link_"]; | |
576 | if (!$entry_link) $entry_link = $item["link"]; | |
577 | ||
578 | if (!$entry_title) continue; | |
579 | # if (!$entry_link) continue; | |
580 | ||
581 | $entry_link = strip_tags($entry_link); | |
582 | ||
583 | $entry_content = $item["content:escaped"]; | |
584 | ||
585 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
586 | if (!$entry_content) $entry_content = $item["content"]; | |
587 | if (!$entry_content) $entry_content = $item["atom_content"]; | |
588 | if (!$entry_content) $entry_content = $item["summary"]; | |
589 | if (!$entry_content) $entry_content = $item["description"]; | |
590 | ||
591 | // if (!$entry_content) continue; | |
592 | ||
593 | // WTF | |
594 | if (is_array($entry_content)) { | |
595 | $entry_content = $entry_content["encoded"]; | |
596 | if (!$entry_content) $entry_content = $entry_content["escaped"]; | |
597 | } | |
598 | ||
599 | // print_r($item); | |
600 | // print_r(htmlspecialchars($entry_content)); | |
601 | // print "<br>"; | |
602 | ||
603 | $entry_content_unescaped = $entry_content; | |
604 | ||
605 | $entry_comments = strip_tags($item["comments"]); | |
606 | ||
607 | $entry_author = db_escape_string(strip_tags($item['dc']['creator'])); | |
608 | ||
609 | if ($item['author']) { | |
610 | ||
611 | if (is_array($item['author'])) { | |
612 | ||
613 | if (!$entry_author) { | |
614 | $entry_author = db_escape_string(strip_tags($item['author']['name'])); | |
615 | } | |
616 | ||
617 | if (!$entry_author) { | |
618 | $entry_author = db_escape_string(strip_tags($item['author']['email'])); | |
619 | } | |
620 | } | |
621 | ||
622 | if (!$entry_author) { | |
623 | $entry_author = db_escape_string(strip_tags($item['author'])); | |
624 | } | |
625 | } | |
626 | ||
627 | if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = ''; | |
628 | ||
629 | $entry_guid = db_escape_string(strip_tags($entry_guid)); | |
630 | ||
631 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
632 | WHERE guid = '$entry_guid'"); | |
633 | ||
634 | $entry_content = db_escape_string($entry_content); | |
635 | ||
636 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); | |
637 | ||
638 | $entry_title = db_escape_string($entry_title); | |
639 | $entry_link = db_escape_string($entry_link); | |
640 | $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250); | |
641 | $entry_author = mb_substr($entry_author, 0, 250); | |
642 | ||
643 | $num_comments = db_escape_string($item["slash"]["comments"]); | |
644 | ||
645 | if (!$num_comments) $num_comments = 0; | |
646 | ||
647 | // parse <category> entries into tags | |
648 | ||
649 | $t_ctr = $item['category#']; | |
650 | ||
651 | $additional_tags = false; | |
652 | ||
653 | if ($t_ctr == 0) { | |
654 | $additional_tags = false; | |
655 | } else if ($t_ctr == 1) { | |
656 | $additional_tags = array($item['category']); | |
657 | } else { | |
658 | $additional_tags = array(); | |
659 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
660 | if ($item["category#$i"]) { | |
661 | array_push($additional_tags, $item["category#$i"]); | |
662 | } | |
663 | } | |
664 | } | |
665 | ||
666 | // parse <dc:subject> elements | |
667 | ||
668 | $t_ctr = $item['dc']['subject#']; | |
669 | ||
670 | if ($t_ctr == 1) { | |
671 | $additional_tags = array($item['dc']['subject']); | |
672 | } else if ($t_ctr > 1) { | |
673 | $additional_tags = array(); | |
674 | for ($i = 0; $i <= $t_ctr; $i++ ) { | |
675 | if ($item['dc']["subject#$i"]) { | |
676 | array_push($additional_tags, $item['dc']["subject#$i"]); | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
681 | # sanitize content | |
682 | ||
683 | // $entry_content = sanitize_rss($entry_content); | |
684 | ||
685 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
686 | _debug("update_rss_feed: done collecting data [TITLE:$entry_title]"); | |
687 | } | |
688 | ||
689 | db_query($link, "BEGIN"); | |
690 | ||
691 | if (db_num_rows($result) == 0) { | |
692 | ||
693 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
694 | _debug("update_rss_feed: base guid not found"); | |
695 | } | |
696 | ||
697 | // base post entry does not exist, create it | |
698 | ||
699 | $result = db_query($link, | |
700 | "INSERT INTO ttrss_entries | |
701 | (title, | |
702 | guid, | |
703 | link, | |
704 | updated, | |
705 | content, | |
706 | content_hash, | |
707 | no_orig_date, | |
708 | date_entered, | |
709 | comments, | |
710 | num_comments, | |
711 | author) | |
712 | VALUES | |
713 | ('$entry_title', | |
714 | '$entry_guid', | |
715 | '$entry_link', | |
716 | '$entry_timestamp_fmt', | |
717 | '$entry_content', | |
718 | '$content_hash', | |
719 | $no_orig_date, | |
720 | NOW(), | |
721 | '$entry_comments', | |
722 | '$num_comments', | |
723 | '$entry_author')"); | |
724 | } else { | |
725 | // we keep encountering the entry in feeds, so we need to | |
726 | // update date_entered column so that we don't get horrible | |
727 | // dupes when the entry gets purged and reinserted again e.g. | |
728 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
729 | ||
730 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
731 | ||
732 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
733 | WHERE id = '$base_entry_id'"); | |
734 | } | |
735 | ||
736 | // now it should exist, if not - bad luck then | |
737 | ||
738 | $result = db_query($link, "SELECT | |
739 | id,content_hash,no_orig_date,title, | |
740 | substring(date_entered,1,19) as date_entered, | |
741 | substring(updated,1,19) as updated, | |
742 | num_comments | |
743 | FROM | |
744 | ttrss_entries | |
745 | WHERE guid = '$entry_guid'"); | |
746 | ||
747 | if (db_num_rows($result) == 1) { | |
748 | ||
749 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
750 | _debug("update_rss_feed: base guid found, checking for user record"); | |
751 | } | |
752 | ||
753 | // this will be used below in update handler | |
754 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
755 | $orig_title = db_fetch_result($result, 0, "title"); | |
756 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
757 | $orig_date_entered = strtotime(db_fetch_result($result, | |
758 | 0, "date_entered")); | |
759 | ||
760 | $ref_id = db_fetch_result($result, 0, "id"); | |
761 | ||
762 | // check for user post link to main table | |
763 | ||
764 | // do we allow duplicate posts with same GUID in different feeds? | |
765 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) { | |
766 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
767 | } else { | |
768 | $dupcheck_qpart = ""; | |
769 | } | |
770 | ||
771 | // error_reporting(0); | |
772 | ||
773 | $article_filters = get_article_filters($filters, $entry_title, | |
774 | $entry_content, $entry_link); | |
775 | ||
776 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
777 | _debug("update_rss_feed: article filters: "); | |
778 | if (count($article_filters) != 0) { | |
779 | print_r($article_filters); | |
780 | } | |
781 | } | |
782 | ||
783 | if (find_article_filter($article_filters, "filter")) { | |
784 | continue; | |
785 | } | |
786 | ||
787 | // error_reporting (DEFAULT_ERROR_LEVEL); | |
788 | ||
789 | $result = db_query($link, | |
790 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
791 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
792 | $dupcheck_qpart"); | |
793 | ||
794 | // okay it doesn't exist - create user entry | |
795 | if (db_num_rows($result) == 0) { | |
796 | ||
797 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
798 | _debug("update_rss_feed: user record not found, creating..."); | |
799 | } | |
800 | ||
801 | if (!find_article_filter($article_filters, 'catchup')) { | |
802 | $unread = 'true'; | |
803 | $last_read_qpart = 'NULL'; | |
804 | } else { | |
805 | $unread = 'false'; | |
806 | $last_read_qpart = 'NOW()'; | |
807 | } | |
808 | ||
809 | if (find_article_filter($article_filters, 'mark')) { | |
810 | $marked = 'true'; | |
811 | } else { | |
812 | $marked = 'false'; | |
813 | } | |
814 | ||
815 | if (find_article_filter($article_filters, 'publish')) { | |
816 | $published = 'true'; | |
817 | } else { | |
818 | $published = 'false'; | |
819 | } | |
820 | ||
821 | $result = db_query($link, | |
822 | "INSERT INTO ttrss_user_entries | |
823 | (ref_id, owner_uid, feed_id, unread, last_read, marked, published) | |
824 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
825 | $last_read_qpart, $marked, $published)"); | |
826 | } | |
827 | ||
828 | $post_needs_update = false; | |
829 | ||
830 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) && | |
831 | ($content_hash != $orig_content_hash)) { | |
832 | // print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->"; | |
833 | $post_needs_update = true; | |
834 | } | |
835 | ||
836 | if (db_escape_string($orig_title) != $entry_title) { | |
837 | $post_needs_update = true; | |
838 | } | |
839 | ||
840 | if ($orig_num_comments != $num_comments) { | |
841 | $post_needs_update = true; | |
842 | } | |
843 | ||
844 | // this doesn't seem to be very reliable | |
845 | // | |
846 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
847 | // $post_needs_update = true; | |
848 | // } | |
849 | ||
850 | // if post needs update, update it and mark all user entries | |
851 | // linking to this post as updated | |
852 | if ($post_needs_update) { | |
853 | ||
854 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
855 | _debug("update_rss_feed: post $entry_guid needs update..."); | |
856 | } | |
857 | ||
858 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
859 | ||
860 | db_query($link, "UPDATE ttrss_entries | |
861 | SET title = '$entry_title', content = '$entry_content', | |
862 | content_hash = '$content_hash', | |
863 | num_comments = '$num_comments' | |
864 | WHERE id = '$ref_id'"); | |
865 | ||
866 | if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) { | |
867 | db_query($link, "UPDATE ttrss_user_entries | |
868 | SET last_read = null, unread = true WHERE ref_id = '$ref_id'"); | |
869 | } else { | |
870 | db_query($link, "UPDATE ttrss_user_entries | |
871 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
872 | } | |
873 | ||
874 | } | |
875 | } | |
876 | ||
877 | db_query($link, "COMMIT"); | |
878 | ||
879 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
880 | _debug("update_rss_feed: looking for tags..."); | |
881 | } | |
882 | ||
883 | /* taaaags */ | |
884 | // <a href="..." rel="tag">Xorg</a>, // | |
885 | ||
886 | $entry_tags = null; | |
887 | ||
888 | preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i", | |
889 | $entry_content_unescaped, $entry_tags); | |
890 | ||
891 | /* print "<p><br/>$entry_title : $entry_content_unescaped<br>"; | |
892 | print_r($entry_tags); | |
893 | print "<br/></p>"; */ | |
894 | ||
895 | $entry_tags = $entry_tags[1]; | |
896 | ||
897 | # check for manual tags | |
898 | ||
899 | $tag_filter = find_article_filter($article_filters, "tag"); | |
900 | ||
901 | if ($tag_filter) { | |
902 | ||
903 | $manual_tags = trim_array(split(",", $tag_filter[1])); | |
904 | ||
905 | foreach ($manual_tags as $tag) { | |
906 | if (tag_is_valid($tag)) { | |
907 | array_push($entry_tags, $tag); | |
908 | } | |
909 | } | |
910 | } | |
911 | ||
912 | $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link, | |
913 | 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8'))); | |
914 | ||
915 | if ($additional_tags && is_array($additional_tags)) { | |
916 | foreach ($additional_tags as $tag) { | |
917 | if (tag_is_valid($tag) && | |
918 | array_search($tag, $boring_tags) === FALSE) { | |
919 | array_push($entry_tags, $tag); | |
920 | } | |
921 | } | |
922 | } | |
923 | ||
924 | // print "<p>TAGS: "; print_r($entry_tags); print "</p>"; | |
925 | ||
926 | if (count($entry_tags) > 0) { | |
927 | ||
928 | db_query($link, "BEGIN"); | |
929 | ||
930 | $result = db_query($link, "SELECT id,int_id | |
931 | FROM ttrss_entries,ttrss_user_entries | |
932 | WHERE guid = '$entry_guid' | |
933 | AND feed_id = '$feed' AND ref_id = id | |
934 | AND owner_uid = '$owner_uid'"); | |
935 | ||
936 | if (db_num_rows($result) == 1) { | |
937 | ||
938 | $entry_id = db_fetch_result($result, 0, "id"); | |
939 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
940 | ||
941 | foreach ($entry_tags as $tag) { | |
942 | ||
943 | $tag = sanitize_tag($tag); | |
944 | $tag = db_escape_string($tag); | |
945 | ||
946 | if (!tag_is_valid($tag)) continue; | |
947 | ||
948 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
949 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
950 | owner_uid = '$owner_uid' LIMIT 1"); | |
951 | ||
952 | // print db_fetch_result($result, 0, "id"); | |
953 | ||
954 | if ($result && db_num_rows($result) == 0) { | |
955 | ||
956 | db_query($link, "INSERT INTO ttrss_tags | |
957 | (owner_uid,tag_name,post_int_id) | |
958 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
959 | } | |
960 | } | |
961 | } | |
962 | db_query($link, "COMMIT"); | |
963 | } | |
964 | } | |
965 | ||
966 | db_query($link, "UPDATE ttrss_feeds | |
967 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
968 | ||
969 | // db_query($link, "COMMIT"); | |
970 | ||
971 | } else { | |
972 | $error_msg = db_escape_string(magpie_error()); | |
973 | db_query($link, | |
974 | "UPDATE ttrss_feeds SET last_error = '$error_msg', | |
975 | last_updated = NOW() WHERE id = '$feed'"); | |
976 | } | |
977 | ||
978 | if (defined('DAEMON_EXTENDED_DEBUG')) { | |
979 | _debug("update_rss_feed: done"); | |
980 | } | |
981 | ||
982 | } | |
983 | ||
984 | function print_select($id, $default, $values, $attributes = "") { | |
985 | print "<select name=\"$id\" id=\"$id\" $attributes>"; | |
986 | foreach ($values as $v) { | |
987 | if ($v == $default) | |
988 | $sel = " selected"; | |
989 | else | |
990 | $sel = ""; | |
991 | ||
992 | print "<option$sel>$v</option>"; | |
993 | } | |
994 | print "</select>"; | |
995 | } | |
996 | ||
997 | function print_select_hash($id, $default, $values, $attributes = "") { | |
998 | print "<select name=\"$id\" id='$id' $attributes>"; | |
999 | foreach (array_keys($values) as $v) { | |
1000 | if ($v == $default) | |
1001 | $sel = "selected"; | |
1002 | else | |
1003 | $sel = ""; | |
1004 | ||
1005 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
1006 | } | |
1007 | ||
1008 | print "</select>"; | |
1009 | } | |
1010 | ||
1011 | function get_article_filters($filters, $title, $content, $link) { | |
1012 | $matches = array(); | |
1013 | ||
1014 | if ($filters["title"]) { | |
1015 | foreach ($filters["title"] as $filter) { | |
1016 | $reg_exp = $filter["reg_exp"]; | |
1017 | $inverse = $filter["inverse"]; | |
1018 | if ((!$inverse && preg_match("/$reg_exp/i", $title)) || | |
1019 | ($inverse && !preg_match("/$reg_exp/i", $title))) { | |
1020 | ||
1021 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | if ($filters["content"]) { | |
1027 | foreach ($filters["content"] as $filter) { | |
1028 | $reg_exp = $filter["reg_exp"]; | |
1029 | $inverse = $filter["inverse"]; | |
1030 | ||
1031 | if ((!$inverse && preg_match("/$reg_exp/i", $content)) || | |
1032 | ($inverse && !preg_match("/$reg_exp/i", $content))) { | |
1033 | ||
1034 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | if ($filters["both"]) { | |
1040 | foreach ($filters["both"] as $filter) { | |
1041 | $reg_exp = $filter["reg_exp"]; | |
1042 | $inverse = $filter["inverse"]; | |
1043 | ||
1044 | if ($inverse) { | |
1045 | if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) { | |
1046 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1047 | } | |
1048 | } else { | |
1049 | if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) { | |
1050 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1051 | } | |
1052 | } | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | if ($filters["link"]) { | |
1057 | $reg_exp = $filter["reg_exp"]; | |
1058 | foreach ($filters["link"] as $filter) { | |
1059 | $reg_exp = $filter["reg_exp"]; | |
1060 | $inverse = $filter["inverse"]; | |
1061 | ||
1062 | if ((!$inverse && preg_match("/$reg_exp/i", $link)) || | |
1063 | ($inverse && !preg_match("/$reg_exp/i", $link))) { | |
1064 | ||
1065 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | return $matches; | |
1071 | } | |
1072 | ||
1073 | function find_article_filter($filters, $filter_name) { | |
1074 | foreach ($filters as $f) { | |
1075 | if ($f[0] == $filter_name) { | |
1076 | return $f; | |
1077 | }; | |
1078 | } | |
1079 | return false; | |
1080 | } | |
1081 | ||
1082 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link, | |
1083 | $rtl_content = false, $last_updated = false, $last_error = false) { | |
1084 | ||
1085 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
1086 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; | |
1087 | } else { | |
1088 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; | |
1089 | } | |
1090 | ||
1091 | if ($rtl_content) { | |
1092 | $rtl_tag = "dir=\"rtl\""; | |
1093 | } else { | |
1094 | $rtl_tag = "dir=\"ltr\""; | |
1095 | } | |
1096 | ||
1097 | $error_notify_msg = ""; | |
1098 | ||
1099 | if ($last_error) { | |
1100 | $link_title = "Error: $last_error ($last_updated)"; | |
1101 | $error_notify_msg = "(Error)"; | |
1102 | } else if ($last_updated) { | |
1103 | $link_title = "Updated: $last_updated"; | |
1104 | } | |
1105 | ||
1106 | $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\" | |
1107 | href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>"; | |
1108 | ||
1109 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
1110 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { | |
1111 | print "$feed_icon"; | |
1112 | } | |
1113 | ||
1114 | print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>"; | |
1115 | ||
1116 | if ($unread != 0) { | |
1117 | $fctr_class = ""; | |
1118 | } else { | |
1119 | $fctr_class = "class=\"invisible\""; | |
1120 | } | |
1121 | ||
1122 | print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\"> | |
1123 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
1124 | ||
1125 | if (get_pref($link, "EXTENDED_FEEDLIST")) { | |
1126 | print "<div class=\"feedExtInfo\"> | |
1127 | <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>"; | |
1128 | } | |
1129 | ||
1130 | print "</li>"; | |
1131 | ||
1132 | } | |
1133 | ||
1134 | function getmicrotime() { | |
1135 | list($usec, $sec) = explode(" ",microtime()); | |
1136 | return ((float)$usec + (float)$sec); | |
1137 | } | |
1138 | ||
1139 | function print_radio($id, $default, $values, $attributes = "") { | |
1140 | foreach ($values as $v) { | |
1141 | ||
1142 | if ($v == $default) | |
1143 | $sel = "checked"; | |
1144 | else | |
1145 | $sel = ""; | |
1146 | ||
1147 | if ($v == "Yes") { | |
1148 | $sel .= " value=\"1\""; | |
1149 | } else { | |
1150 | $sel .= " value=\"0\""; | |
1151 | } | |
1152 | ||
1153 | print "<input class=\"noborder\" | |
1154 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
1155 | ||
1156 | } | |
1157 | } | |
1158 | ||
1159 | function initialize_user_prefs($link, $uid) { | |
1160 | ||
1161 | $uid = db_escape_string($uid); | |
1162 | ||
1163 | db_query($link, "BEGIN"); | |
1164 | ||
1165 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
1166 | ||
1167 | $u_result = db_query($link, "SELECT pref_name | |
1168 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
1169 | ||
1170 | $active_prefs = array(); | |
1171 | ||
1172 | while ($line = db_fetch_assoc($u_result)) { | |
1173 | array_push($active_prefs, $line["pref_name"]); | |
1174 | } | |
1175 | ||
1176 | while ($line = db_fetch_assoc($result)) { | |
1177 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
1178 | // print "adding " . $line["pref_name"] . "<br>"; | |
1179 | ||
1180 | db_query($link, "INSERT INTO ttrss_user_prefs | |
1181 | (owner_uid,pref_name,value) VALUES | |
1182 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
1183 | ||
1184 | } | |
1185 | } | |
1186 | ||
1187 | db_query($link, "COMMIT"); | |
1188 | ||
1189 | } | |
1190 | ||
1191 | function lookup_user_id($link, $user) { | |
1192 | ||
1193 | $result = db_query($link, "SELECT id FROM ttrss_users WHERE | |
1194 | login = '$login'"); | |
1195 | ||
1196 | if (db_num_rows($result) == 1) { | |
1197 | return db_fetch_result($result, 0, "id"); | |
1198 | } else { | |
1199 | return false; | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | function http_authenticate_user($link) { | |
1204 | ||
1205 | error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log'); | |
1206 | ||
1207 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
1208 | ||
1209 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1210 | header('HTTP/1.0 401 Unauthorized'); | |
1211 | exit; | |
1212 | ||
1213 | } else { | |
1214 | $auth_result = authenticate_user($link, | |
1215 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
1216 | ||
1217 | if (!$auth_result) { | |
1218 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"'); | |
1219 | header('HTTP/1.0 401 Unauthorized'); | |
1220 | exit; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | return true; | |
1225 | } | |
1226 | ||
1227 | function authenticate_user($link, $login, $password, $force_auth = false) { | |
1228 | ||
1229 | if (!SINGLE_USER_MODE) { | |
1230 | ||
1231 | $pwd_hash = 'SHA1:' . sha1($password); | |
1232 | ||
1233 | if ($force_auth && defined('_DEBUG_USER_SWITCH')) { | |
1234 | $query = "SELECT id,login,access_level | |
1235 | FROM ttrss_users WHERE | |
1236 | login = '$login'"; | |
1237 | } else { | |
1238 | $query = "SELECT id,login,access_level | |
1239 | FROM ttrss_users WHERE | |
1240 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
1241 | } | |
1242 | ||
1243 | $result = db_query($link, $query); | |
1244 | ||
1245 | if (db_num_rows($result) == 1) { | |
1246 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
1247 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
1248 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
1249 | ||
1250 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1251 | $_SESSION["uid"]); | |
1252 | ||
1253 | $user_theme = get_user_theme_path($link); | |
1254 | ||
1255 | $_SESSION["theme"] = $user_theme; | |
1256 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1257 | ||
1258 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1259 | ||
1260 | return true; | |
1261 | } | |
1262 | ||
1263 | return false; | |
1264 | ||
1265 | } else { | |
1266 | ||
1267 | $_SESSION["uid"] = 1; | |
1268 | $_SESSION["name"] = "admin"; | |
1269 | ||
1270 | $user_theme = get_user_theme_path($link); | |
1271 | ||
1272 | $_SESSION["theme"] = $user_theme; | |
1273 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
1274 | ||
1275 | initialize_user_prefs($link, $_SESSION["uid"]); | |
1276 | ||
1277 | return true; | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | function make_password($length = 8) { | |
1282 | ||
1283 | $password = ""; | |
1284 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; | |
1285 | ||
1286 | $i = 0; | |
1287 | ||
1288 | while ($i < $length) { | |
1289 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
1290 | ||
1291 | if (!strstr($password, $char)) { | |
1292 | $password .= $char; | |
1293 | $i++; | |
1294 | } | |
1295 | } | |
1296 | return $password; | |
1297 | } | |
1298 | ||
1299 | // this is called after user is created to initialize default feeds, labels | |
1300 | // or whatever else | |
1301 | ||
1302 | // user preferences are checked on every login, not here | |
1303 | ||
1304 | function initialize_user($link, $uid) { | |
1305 | ||
1306 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1307 | values ('$uid','unread = true', 'Unread articles')"); | |
1308 | ||
1309 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
1310 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
1311 | ||
1312 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
1313 | values ('$uid', 'Tiny Tiny RSS: New Releases', | |
1314 | 'http://tt-rss.spb.ru/releases.rss')"); | |
1315 | ||
1316 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
1317 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
1318 | 'http://tt-rss.spb.ru/forum/rss.php')"); | |
1319 | } | |
1320 | ||
1321 | function logout_user() { | |
1322 | session_destroy(); | |
1323 | if (isset($_COOKIE[session_name()])) { | |
1324 | setcookie(session_name(), '', time()-42000, '/'); | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | function get_script_urlpath() { | |
1329 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); | |
1330 | } | |
1331 | ||
1332 | function validate_session($link) { | |
1333 | if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) { | |
1334 | if ($_SESSION["ip_address"]) { | |
1335 | if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) { | |
1336 | $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)"; | |
1337 | return false; | |
1338 | } | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) { | |
1343 | ||
1344 | //print_r($_SESSION); | |
1345 | ||
1346 | if (time() > $_SESSION["cookie_lifetime"]) { | |
1347 | return false; | |
1348 | } | |
1349 | } */ | |
1350 | ||
1351 | return true; | |
1352 | } | |
1353 | ||
1354 | function login_sequence($link, $mobile = false) { | |
1355 | if (!SINGLE_USER_MODE) { | |
1356 | ||
1357 | if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) { | |
1358 | $swu = db_escape_string($_REQUEST["swu"]); | |
1359 | if ($swu) { | |
1360 | $_SESSION["prefs_cache"] = false; | |
1361 | return authenticate_user($link, $swu, null, true); | |
1362 | } | |
1363 | } | |
1364 | ||
1365 | $login_action = $_POST["login_action"]; | |
1366 | ||
1367 | # try to authenticate user if called from login form | |
1368 | if ($login_action == "do_login") { | |
1369 | $login = $_POST["login"]; | |
1370 | $password = $_POST["password"]; | |
1371 | $remember_me = $_POST["remember_me"]; | |
1372 | ||
1373 | if (authenticate_user($link, $login, $password)) { | |
1374 | $_POST["password"] = ""; | |
1375 | ||
1376 | header("Location: " . $_SERVER["REQUEST_URI"]); | |
1377 | exit; | |
1378 | ||
1379 | return; | |
1380 | } else { | |
1381 | $_SESSION["login_error_msg"] = "Incorrect username or password"; | |
1382 | } | |
1383 | } | |
1384 | ||
1385 | // print session_id(); | |
1386 | // print_r($_SESSION); | |
1387 | ||
1388 | if (!$_SESSION["uid"] || !validate_session($link)) { | |
1389 | render_login_form($link, $mobile); | |
1390 | exit; | |
1391 | } else { | |
1392 | /* bump login timestamp */ | |
1393 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1394 | $_SESSION["uid"]); | |
1395 | } | |
1396 | ||
1397 | } else { | |
1398 | return authenticate_user($link, "admin", null); | |
1399 | } | |
1400 | } | |
1401 | ||
1402 | function truncate_string($str, $max_len) { | |
1403 | if (mb_strlen($str, "utf-8") > $max_len - 3) { | |
1404 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
1405 | } else { | |
1406 | return $str; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | function get_user_theme_path($link) { | |
1411 | $result = db_query($link, "SELECT theme_path | |
1412 | FROM | |
1413 | ttrss_themes,ttrss_users | |
1414 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
1415 | if (db_num_rows($result) != 0) { | |
1416 | return db_fetch_result($result, 0, "theme_path"); | |
1417 | } else { | |
1418 | return null; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | function smart_date_time($timestamp) { | |
1423 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1424 | return date("G:i", $timestamp); | |
1425 | } else if (date("Y", $timestamp) == date("Y")) { | |
1426 | return date("M d, G:i", $timestamp); | |
1427 | } else { | |
1428 | return date("Y/m/d, G:i", $timestamp); | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | function smart_date($timestamp) { | |
1433 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
1434 | return "Today"; | |
1435 | } else if (date("Y", $timestamp) == date("Y")) { | |
1436 | return date("D m", $timestamp); | |
1437 | } else { | |
1438 | return date("Y/m/d", $timestamp); | |
1439 | } | |
1440 | } | |
1441 | ||
1442 | function sql_bool_to_string($s) { | |
1443 | if ($s == "t" || $s == "1") { | |
1444 | return "true"; | |
1445 | } else { | |
1446 | return "false"; | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | function sql_bool_to_bool($s) { | |
1451 | if ($s == "t" || $s == "1") { | |
1452 | return true; | |
1453 | } else { | |
1454 | return false; | |
1455 | } | |
1456 | } | |
1457 | ||
1458 | ||
1459 | function toggleEvenOdd($a) { | |
1460 | if ($a == "even") | |
1461 | return "odd"; | |
1462 | else | |
1463 | return "even"; | |
1464 | } | |
1465 | ||
1466 | function sanity_check($link) { | |
1467 | ||
1468 | error_reporting(0); | |
1469 | ||
1470 | $error_code = 0; | |
1471 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1472 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
1473 | ||
1474 | if ($schema_version != SCHEMA_VERSION) { | |
1475 | $error_code = 5; | |
1476 | } | |
1477 | ||
1478 | if (DB_TYPE == "mysql") { | |
1479 | $result = db_query($link, "SELECT true", false); | |
1480 | if (db_num_rows($result) != 1) { | |
1481 | $error_code = 10; | |
1482 | } | |
1483 | } | |
1484 | ||
1485 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1486 | ||
1487 | if ($error_code != 0) { | |
1488 | print_error_xml($error_code); | |
1489 | return false; | |
1490 | } else { | |
1491 | return true; | |
1492 | } | |
1493 | } | |
1494 | ||
1495 | function file_is_locked($filename) { | |
1496 | error_reporting(0); | |
1497 | $fp = fopen($filename, "r"); | |
1498 | error_reporting(DEFAULT_ERROR_LEVEL); | |
1499 | if ($fp) { | |
1500 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1501 | flock($fp, LOCK_UN); | |
1502 | fclose($fp); | |
1503 | return false; | |
1504 | } | |
1505 | fclose($fp); | |
1506 | return true; | |
1507 | } | |
1508 | return false; | |
1509 | } | |
1510 | ||
1511 | function make_lockfile($filename) { | |
1512 | $fp = fopen($filename, "w"); | |
1513 | ||
1514 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1515 | return $fp; | |
1516 | } else { | |
1517 | return false; | |
1518 | } | |
1519 | } | |
1520 | ||
1521 | function make_stampfile($filename) { | |
1522 | $fp = fopen($filename, "w"); | |
1523 | ||
1524 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1525 | fwrite($fp, time() . "\n"); | |
1526 | flock($fp, LOCK_UN); | |
1527 | fclose($fp); | |
1528 | return true; | |
1529 | } else { | |
1530 | return false; | |
1531 | } | |
1532 | } | |
1533 | ||
1534 | function read_stampfile($filename) { | |
1535 | ||
1536 | error_reporting(0); | |
1537 | $fp = fopen($filename, "r"); | |
1538 | error_reporting (DEFAULT_ERROR_LEVEL); | |
1539 | ||
1540 | if (flock($fp, LOCK_EX)) { | |
1541 | $stamp = fgets($fp); | |
1542 | flock($fp, LOCK_UN); | |
1543 | fclose($fp); | |
1544 | return $stamp; | |
1545 | } else { | |
1546 | return false; | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | function sql_random_function() { | |
1551 | if (DB_TYPE == "mysql") { | |
1552 | return "RAND()"; | |
1553 | } else { | |
1554 | return "RANDOM()"; | |
1555 | } | |
1556 | } | |
1557 | ||
1558 | function catchup_feed($link, $feed, $cat_view) { | |
1559 | ||
1560 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
1561 | ||
1562 | if ($cat_view) { | |
1563 | ||
1564 | if ($feed > 0) { | |
1565 | $cat_qpart = "cat_id = '$feed'"; | |
1566 | } else { | |
1567 | $cat_qpart = "cat_id IS NULL"; | |
1568 | } | |
1569 | ||
1570 | $tmp_result = db_query($link, "SELECT id | |
1571 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " . | |
1572 | $_SESSION["uid"]); | |
1573 | ||
1574 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1575 | ||
1576 | $tmp_feed = $tmp_line["id"]; | |
1577 | ||
1578 | db_query($link, "UPDATE ttrss_user_entries | |
1579 | SET unread = false,last_read = NOW() | |
1580 | WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]); | |
1581 | } | |
1582 | ||
1583 | } else if ($feed > 0) { | |
1584 | ||
1585 | $tmp_result = db_query($link, "SELECT id | |
1586 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
1587 | ORDER BY cat_id,title"); | |
1588 | ||
1589 | $parent_ids = array(); | |
1590 | ||
1591 | if (db_num_rows($tmp_result) > 0) { | |
1592 | while ($p = db_fetch_assoc($tmp_result)) { | |
1593 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
1594 | } | |
1595 | ||
1596 | $children_qpart = implode(" OR ", $parent_ids); | |
1597 | ||
1598 | db_query($link, "UPDATE ttrss_user_entries | |
1599 | SET unread = false,last_read = NOW() | |
1600 | WHERE (feed_id = '$feed' OR $children_qpart) | |
1601 | AND owner_uid = " . $_SESSION["uid"]); | |
1602 | ||
1603 | } else { | |
1604 | db_query($link, "UPDATE ttrss_user_entries | |
1605 | SET unread = false,last_read = NOW() | |
1606 | WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
1607 | } | |
1608 | ||
1609 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1610 | ||
1611 | if ($feed == -1) { | |
1612 | db_query($link, "UPDATE ttrss_user_entries | |
1613 | SET unread = false,last_read = NOW() | |
1614 | WHERE marked = true AND owner_uid = ".$_SESSION["uid"]); | |
1615 | } | |
1616 | ||
1617 | if ($feed == -2) { | |
1618 | db_query($link, "UPDATE ttrss_user_entries | |
1619 | SET unread = false,last_read = NOW() | |
1620 | WHERE published = true AND owner_uid = ".$_SESSION["uid"]); | |
1621 | } | |
1622 | ||
1623 | } else if ($feed < -10) { // label | |
1624 | ||
1625 | // TODO make this more efficient | |
1626 | ||
1627 | $label_id = -$feed - 11; | |
1628 | ||
1629 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
1630 | WHERE id = '$label_id'"); | |
1631 | ||
1632 | if ($tmp_result) { | |
1633 | $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp"); | |
1634 | ||
1635 | db_query($link, "BEGIN"); | |
1636 | ||
1637 | $tmp2_result = db_query($link, | |
1638 | "SELECT | |
1639 | int_id | |
1640 | FROM | |
1641 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
1642 | WHERE | |
1643 | ref_id = ttrss_entries.id AND | |
1644 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1645 | $sql_exp AND | |
1646 | ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); | |
1647 | ||
1648 | while ($tmp_line = db_fetch_assoc($tmp2_result)) { | |
1649 | db_query($link, "UPDATE | |
1650 | ttrss_user_entries | |
1651 | SET | |
1652 | unread = false, last_read = NOW() | |
1653 | WHERE | |
1654 | int_id = " . $tmp_line["int_id"]); | |
1655 | } | |
1656 | ||
1657 | db_query($link, "COMMIT"); | |
1658 | ||
1659 | /* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries | |
1660 | SET unread = false,last_read = NOW() | |
1661 | WHERE $sql_exp | |
1662 | AND ref_id = id | |
1663 | AND owner_uid = ".$_SESSION["uid"]); */ | |
1664 | } | |
1665 | } | |
1666 | } else { // tag | |
1667 | db_query($link, "BEGIN"); | |
1668 | ||
1669 | $tag_name = db_escape_string($feed); | |
1670 | ||
1671 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1672 | WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]); | |
1673 | ||
1674 | while ($line = db_fetch_assoc($result)) { | |
1675 | db_query($link, "UPDATE ttrss_user_entries SET | |
1676 | unread = false, last_read = NOW() | |
1677 | WHERE int_id = " . $line["post_int_id"]); | |
1678 | } | |
1679 | db_query($link, "COMMIT"); | |
1680 | } | |
1681 | } | |
1682 | ||
1683 | function update_generic_feed($link, $feed, $cat_view) { | |
1684 | if ($cat_view) { | |
1685 | ||
1686 | if ($feed > 0) { | |
1687 | $cat_qpart = "cat_id = '$feed'"; | |
1688 | } else { | |
1689 | $cat_qpart = "cat_id IS NULL"; | |
1690 | } | |
1691 | ||
1692 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1693 | WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]); | |
1694 | ||
1695 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1696 | $feed_url = $tmp_line["feed_url"]; | |
1697 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1698 | } | |
1699 | ||
1700 | } else { | |
1701 | $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds | |
1702 | WHERE id = '$feed'"); | |
1703 | $feed_url = db_fetch_result($tmp_result, 0, "feed_url"); | |
1704 | update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON); | |
1705 | } | |
1706 | } | |
1707 | ||
1708 | function getAllCounters($link, $omode = "flc") { | |
1709 | /* getLabelCounters($link); | |
1710 | getFeedCounters($link); | |
1711 | getTagCounters($link); | |
1712 | getGlobalCounters($link); | |
1713 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1714 | getCategoryCounters($link); | |
1715 | } */ | |
1716 | ||
1717 | if (!$omode) $omode = "flc"; | |
1718 | ||
1719 | getGlobalCounters($link); | |
1720 | ||
1721 | if (strchr($omode, "l")) getLabelCounters($link); | |
1722 | if (strchr($omode, "f")) getFeedCounters($link); | |
1723 | if (strchr($omode, "t")) getTagCounters($link); | |
1724 | if (strchr($omode, "c")) { | |
1725 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
1726 | getCategoryCounters($link); | |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | ||
1731 | function getCategoryCounters($link) { | |
1732 | $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id) | |
1733 | FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id | |
1734 | AND unread = true)) AS unread FROM ttrss_feeds | |
1735 | WHERE | |
1736 | hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id"); | |
1737 | ||
1738 | while ($line = db_fetch_assoc($result)) { | |
1739 | $line["cat_id"] = sprintf("%d", $line["cat_id"]); | |
1740 | print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"". | |
1741 | $line["unread"]."\"/>"; | |
1742 | } | |
1743 | } | |
1744 | ||
1745 | function getCategoryUnread($link, $cat) { | |
1746 | ||
1747 | if ($cat != 0) { | |
1748 | $cat_query = "cat_id = '$cat'"; | |
1749 | } else { | |
1750 | $cat_query = "cat_id IS NULL"; | |
1751 | } | |
1752 | ||
1753 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
1754 | AND hidden = false | |
1755 | AND owner_uid = " . $_SESSION["uid"]); | |
1756 | ||
1757 | $cat_feeds = array(); | |
1758 | while ($line = db_fetch_assoc($result)) { | |
1759 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1760 | } | |
1761 | ||
1762 | if (count($cat_feeds) == 0) return 0; | |
1763 | ||
1764 | $match_part = implode(" OR ", $cat_feeds); | |
1765 | ||
1766 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1767 | FROM ttrss_user_entries | |
1768 | WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]); | |
1769 | ||
1770 | $unread = 0; | |
1771 | ||
1772 | # this needs to be rewritten | |
1773 | while ($line = db_fetch_assoc($result)) { | |
1774 | $unread += $line["unread"]; | |
1775 | } | |
1776 | ||
1777 | return $unread; | |
1778 | ||
1779 | } | |
1780 | ||
1781 | function getFeedUnread($link, $feed, $is_cat = false) { | |
1782 | $n_feed = sprintf("%d", $feed); | |
1783 | ||
1784 | if ($is_cat) { | |
1785 | return getCategoryUnread($link, $n_feed); | |
1786 | } else if ($n_feed == -1) { | |
1787 | $match_part = "marked = true"; | |
1788 | } else if ($n_feed == -2) { | |
1789 | $match_part = "published = true"; | |
1790 | } else if ($n_feed > 0) { | |
1791 | ||
1792 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
1793 | WHERE parent_feed = '$n_feed' | |
1794 | AND hidden = false | |
1795 | AND owner_uid = " . $_SESSION["uid"]); | |
1796 | ||
1797 | if (db_num_rows($result) > 0) { | |
1798 | ||
1799 | $linked_feeds = array(); | |
1800 | while ($line = db_fetch_assoc($result)) { | |
1801 | array_push($linked_feeds, "feed_id = " . $line["id"]); | |
1802 | } | |
1803 | ||
1804 | array_push($linked_feeds, "feed_id = $n_feed"); | |
1805 | ||
1806 | $match_part = implode(" OR ", $linked_feeds); | |
1807 | ||
1808 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1809 | FROM ttrss_user_entries | |
1810 | WHERE unread = true AND ($match_part) | |
1811 | AND owner_uid = " . $_SESSION["uid"]); | |
1812 | ||
1813 | $unread = 0; | |
1814 | ||
1815 | # this needs to be rewritten | |
1816 | while ($line = db_fetch_assoc($result)) { | |
1817 | $unread += $line["unread"]; | |
1818 | } | |
1819 | ||
1820 | return $unread; | |
1821 | ||
1822 | } else { | |
1823 | $match_part = "feed_id = '$n_feed'"; | |
1824 | } | |
1825 | } else if ($feed < -10) { | |
1826 | ||
1827 | $label_id = -$feed - 11; | |
1828 | ||
1829 | $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE | |
1830 | id = '$label_id' AND owner_uid = " . $_SESSION["uid"]); | |
1831 | ||
1832 | $match_part = db_fetch_result($result, 0, "sql_exp"); | |
1833 | } | |
1834 | ||
1835 | if ($match_part) { | |
1836 | ||
1837 | $result = db_query($link, "SELECT count(int_id) AS unread | |
1838 | FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE | |
1839 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1840 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1841 | ttrss_feeds.hidden = false AND | |
1842 | unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]); | |
1843 | ||
1844 | } else { | |
1845 | ||
1846 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1847 | FROM ttrss_tags,ttrss_user_entries | |
1848 | WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND | |
1849 | ttrss_tags.owner_uid = " . $_SESSION["uid"]); | |
1850 | } | |
1851 | ||
1852 | $unread = db_fetch_result($result, 0, "unread"); | |
1853 | ||
1854 | return $unread; | |
1855 | } | |
1856 | ||
1857 | /* FIXME this needs reworking */ | |
1858 | ||
1859 | function getGlobalUnread($link, $user_id = false) { | |
1860 | ||
1861 | if (!$user_id) { | |
1862 | $user_id = $_SESSION["uid"]; | |
1863 | } | |
1864 | ||
1865 | $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1866 | WHERE unread = true AND | |
1867 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1868 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1869 | hidden = false AND | |
1870 | ttrss_user_entries.owner_uid = '$user_id'"); | |
1871 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1872 | return $c_id; | |
1873 | } | |
1874 | ||
1875 | function getGlobalCounters($link, $global_unread = -1) { | |
1876 | if ($global_unread == -1) { | |
1877 | $global_unread = getGlobalUnread($link); | |
1878 | } | |
1879 | print "<counter type=\"global\" id='global-unread' | |
1880 | counter='$global_unread'/>"; | |
1881 | ||
1882 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1883 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1884 | ||
1885 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1886 | ||
1887 | print "<counter type=\"global\" id='subscribed-feeds' | |
1888 | counter='$subscribed_feeds'/>"; | |
1889 | ||
1890 | } | |
1891 | ||
1892 | function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
1893 | ||
1894 | if ($smart_mode) { | |
1895 | if (!$_SESSION["tctr_last_value"]) { | |
1896 | $_SESSION["tctr_last_value"] = array(); | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | $old_counters = $_SESSION["tctr_last_value"]; | |
1901 | ||
1902 | $tctrs_modified = false; | |
1903 | ||
1904 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
1905 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
1906 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1907 | ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND | |
1908 | post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name | |
1909 | UNION | |
1910 | select tag_name,0 as count FROM ttrss_tags | |
1911 | WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */ | |
1912 | ||
1913 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1914 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
1915 | AND unread = true)) AS count FROM ttrss_tags | |
1916 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name | |
1917 | ORDER BY count DESC LIMIT 55"); | |
1918 | ||
1919 | $tags = array(); | |
1920 | ||
1921 | while ($line = db_fetch_assoc($result)) { | |
1922 | $tags[$line["tag_name"]] += $line["count"]; | |
1923 | } | |
1924 | ||
1925 | foreach (array_keys($tags) as $tag) { | |
1926 | $unread = $tags[$tag]; | |
1927 | ||
1928 | $tag = htmlspecialchars($tag); | |
1929 | ||
1930 | if (!$smart_mode || $old_counters[$tag] != $unread) { | |
1931 | $old_counters[$tag] = $unread; | |
1932 | $tctrs_modified = true; | |
1933 | print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>"; | |
1934 | } | |
1935 | ||
1936 | } | |
1937 | ||
1938 | if ($smart_mode && $tctrs_modified) { | |
1939 | $_SESSION["tctr_last_value"] = $old_counters; | |
1940 | } | |
1941 | ||
1942 | } | |
1943 | ||
1944 | function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) { | |
1945 | ||
1946 | if ($smart_mode) { | |
1947 | if (!$_SESSION["lctr_last_value"]) { | |
1948 | $_SESSION["lctr_last_value"] = array(); | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | $ret_arr = array(); | |
1953 | ||
1954 | $old_counters = $_SESSION["lctr_last_value"]; | |
1955 | $lctrs_modified = false; | |
1956 | ||
1957 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1958 | WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1959 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1960 | hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
1961 | ||
1962 | $count = db_fetch_result($result, 0, "count"); | |
1963 | ||
1964 | if (!$ret_mode) { | |
1965 | print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>"; | |
1966 | } else { | |
1967 | $ret_arr["-1"]["counter"] = $count; | |
1968 | $ret_arr["-1"]["description"] = "Starred"; | |
1969 | } | |
1970 | ||
1971 | $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
1972 | WHERE published = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1973 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
1974 | hidden = false AND unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
1975 | ||
1976 | $count = db_fetch_result($result, 0, "count"); | |
1977 | ||
1978 | if (!$ret_mode) { | |
1979 | print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>"; | |
1980 | } else { | |
1981 | $ret_arr["-2"]["counter"] = $count; | |
1982 | $ret_arr["-2"]["description"] = "Published"; | |
1983 | } | |
1984 | ||
1985 | ||
1986 | $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM | |
1987 | ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description"); | |
1988 | ||
1989 | while ($line = db_fetch_assoc($result)) { | |
1990 | ||
1991 | $id = -$line["id"] - 11; | |
1992 | ||
1993 | $label_name = $line["description"]; | |
1994 | ||
1995 | error_reporting (0); | |
1996 | ||
1997 | $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds | |
1998 | WHERE (" . $line["sql_exp"] . ") AND unread = true AND | |
1999 | ttrss_feeds.hidden = false AND | |
2000 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2001 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2002 | ttrss_user_entries.owner_uid = ".$_SESSION["uid"]); | |
2003 | ||
2004 | $count = db_fetch_result($tmp_result, 0, "count"); | |
2005 | ||
2006 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2007 | $old_counters[$id] = $count; | |
2008 | $lctrs_modified = true; | |
2009 | if (!$ret_mode) { | |
2010 | print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>"; | |
2011 | } else { | |
2012 | $ret_arr[$id]["counter"] = $count; | |
2013 | $ret_arr[$id]["description"] = $label_name; | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2018 | } | |
2019 | ||
2020 | if ($smart_mode && $lctrs_modified) { | |
2021 | $_SESSION["lctr_last_value"] = $old_counters; | |
2022 | } | |
2023 | ||
2024 | return $ret_arr; | |
2025 | } | |
2026 | ||
2027 | /* function getFeedCounter($link, $id) { | |
2028 | ||
2029 | $result = db_query($link, "SELECT | |
2030 | count(id) as count,last_error | |
2031 | FROM ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2032 | WHERE feed_id = '$id' AND unread = true | |
2033 | AND ttrss_user_entries.feed_id = ttrss_feeds.id | |
2034 | AND ttrss_user_entries.ref_id = ttrss_entries.id"); | |
2035 | ||
2036 | $count = db_fetch_result($result, 0, "count"); | |
2037 | $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error")); | |
2038 | ||
2039 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>"; | |
2040 | } */ | |
2041 | ||
2042 | function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) { | |
2043 | ||
2044 | if ($smart_mode) { | |
2045 | if (!$_SESSION["fctr_last_value"]) { | |
2046 | $_SESSION["fctr_last_value"] = array(); | |
2047 | } | |
2048 | } | |
2049 | ||
2050 | $old_counters = $_SESSION["fctr_last_value"]; | |
2051 | ||
2052 | /* $result = db_query($link, "SELECT id,last_error,parent_feed, | |
2053 | SUBSTRING(last_updated,1,19) AS last_updated, | |
2054 | (SELECT count(id) | |
2055 | FROM ttrss_entries,ttrss_user_entries | |
2056 | WHERE feed_id = ttrss_feeds.id AND | |
2057 | ttrss_user_entries.ref_id = ttrss_entries.id | |
2058 | AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count | |
2059 | FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . " | |
2060 | AND parent_feed IS NULL"); */ | |
2061 | ||
2062 | $result = db_query($link, "SELECT ttrss_feeds.id, | |
2063 | SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated, | |
2064 | last_error, | |
2065 | COUNT(ttrss_entries.id) AS count | |
2066 | FROM ttrss_feeds | |
2067 | LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id | |
2068 | AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid | |
2069 | AND ttrss_user_entries.unread = true) | |
2070 | LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id) | |
2071 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
2072 | AND parent_feed IS NULL | |
2073 | GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error"); | |
2074 | ||
2075 | $fctrs_modified = false; | |
2076 | ||
2077 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
2078 | ||
2079 | while ($line = db_fetch_assoc($result)) { | |
2080 | ||
2081 | $id = $line["id"]; | |
2082 | $count = $line["count"]; | |
2083 | $last_error = htmlspecialchars($line["last_error"]); | |
2084 | ||
2085 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
2086 | $last_updated = smart_date_time(strtotime($line["last_updated"])); | |
2087 | } else { | |
2088 | $last_updated = date($short_date, strtotime($line["last_updated"])); | |
2089 | } | |
2090 | ||
2091 | $has_img = is_file(ICONS_DIR . "/$id.ico"); | |
2092 | ||
2093 | $tmp_result = db_query($link, | |
2094 | "SELECT id,COUNT(unread) AS unread | |
2095 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
2096 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
2097 | WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id"); | |
2098 | ||
2099 | if (db_num_rows($tmp_result) > 0) { | |
2100 | while ($l = db_fetch_assoc($tmp_result)) { | |
2101 | $count += $l["unread"]; | |
2102 | } | |
2103 | } | |
2104 | ||
2105 | if (!$smart_mode || $old_counters[$id] != $count) { | |
2106 | $old_counters[$id] = $count; | |
2107 | $fctrs_modified = true; | |
2108 | ||
2109 | if ($last_error) { | |
2110 | $error_part = "error=\"$last_error\""; | |
2111 | } else { | |
2112 | $error_part = ""; | |
2113 | } | |
2114 | ||
2115 | if ($has_img) { | |
2116 | $has_img_part = "hi=\"$has_img\""; | |
2117 | } else { | |
2118 | $has_img_part = ""; | |
2119 | } | |
2120 | ||
2121 | print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>"; | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | if ($smart_mode && $fctrs_modified) { | |
2126 | $_SESSION["fctr_last_value"] = $old_counters; | |
2127 | } | |
2128 | } | |
2129 | ||
2130 | function get_script_dt_add() { | |
2131 | if (strpos(VERSION, ".99") === false) { | |
2132 | return VERSION; | |
2133 | } else { | |
2134 | return time(); | |
2135 | } | |
2136 | } | |
2137 | ||
2138 | function get_pgsql_version($link) { | |
2139 | $result = db_query($link, "SELECT version() AS version"); | |
2140 | $version = split(" ", db_fetch_result($result, 0, "version")); | |
2141 | return $version[1]; | |
2142 | } | |
2143 | ||
2144 | function print_error_xml($code, $add_msg = "") { | |
2145 | global $ERRORS; | |
2146 | ||
2147 | $error_msg = $ERRORS[$code]; | |
2148 | ||
2149 | if ($add_msg) { | |
2150 | $error_msg = "$error_msg; $add_msg"; | |
2151 | } | |
2152 | ||
2153 | print "<rpc-reply>"; | |
2154 | print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>"; | |
2155 | print "</rpc-reply>"; | |
2156 | } | |
2157 | ||
2158 | function subscribe_to_feed($link, $feed_link, $cat_id = 0, | |
2159 | $auth_login = '', $auth_pass = '') { | |
2160 | ||
2161 | # check for feed:http://url | |
2162 | $feed_link = trim(preg_replace("/^feed:/", "", $feed_link)); | |
2163 | ||
2164 | # check for feed://URL | |
2165 | if (strpos($feed_link, "//") === 0) { | |
2166 | $feed_link = "http:$feed_link"; | |
2167 | } | |
2168 | ||
2169 | if ($feed_link == "") return; | |
2170 | ||
2171 | if ($cat_id == "0" || !$cat_id) { | |
2172 | $cat_qpart = "NULL"; | |
2173 | } else { | |
2174 | $cat_qpart = "'$cat_id'"; | |
2175 | } | |
2176 | ||
2177 | $result = db_query($link, | |
2178 | "SELECT id FROM ttrss_feeds | |
2179 | WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]); | |
2180 | ||
2181 | if (db_num_rows($result) == 0) { | |
2182 | ||
2183 | $result = db_query($link, | |
2184 | "INSERT INTO ttrss_feeds | |
2185 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass) | |
2186 | VALUES ('".$_SESSION["uid"]."', '$feed_link', | |
2187 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')"); | |
2188 | ||
2189 | $result = db_query($link, | |
2190 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link' | |
2191 | AND owner_uid = " . $_SESSION["uid"]); | |
2192 | ||
2193 | $feed_id = db_fetch_result($result, 0, "id"); | |
2194 | ||
2195 | if ($feed_id) { | |
2196 | update_rss_feed($link, $feed_link, $feed_id, true); | |
2197 | } | |
2198 | ||
2199 | return true; | |
2200 | } else { | |
2201 | return false; | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | function print_feed_select($link, $id, $default_id = "", | |
2206 | $attributes = "", $include_all_feeds = true) { | |
2207 | ||
2208 | print "<select id=\"$id\" name=\"$id\" $attributes>"; | |
2209 | if ($include_all_feeds) { | |
2210 | print "<option value=\"0\">All feeds</option>"; | |
2211 | } | |
2212 | ||
2213 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
2214 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2215 | ||
2216 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
2217 | print "<option disabled>--------</option>"; | |
2218 | } | |
2219 | ||
2220 | while ($line = db_fetch_assoc($result)) { | |
2221 | if ($line["id"] == $default_id) { | |
2222 | $is_selected = "selected"; | |
2223 | } else { | |
2224 | $is_selected = ""; | |
2225 | } | |
2226 | printf("<option $is_selected value='%d'>%s</option>", | |
2227 | $line["id"], htmlspecialchars($line["title"])); | |
2228 | } | |
2229 | ||
2230 | print "</select>"; | |
2231 | } | |
2232 | ||
2233 | function print_feed_cat_select($link, $id, $default_id = "", | |
2234 | $attributes = "", $include_all_cats = true) { | |
2235 | ||
2236 | print "<select id=\"$id\" name=\"$id\" $attributes>"; | |
2237 | ||
2238 | if ($include_all_cats) { | |
2239 | print "<option value=\"0\">".__('Uncategorized')."</option>"; | |
2240 | } | |
2241 | ||
2242 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2243 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2244 | ||
2245 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2246 | print "<option disabled>--------</option>"; | |
2247 | } | |
2248 | ||
2249 | while ($line = db_fetch_assoc($result)) { | |
2250 | if ($line["id"] == $default_id) { | |
2251 | $is_selected = "selected"; | |
2252 | } else { | |
2253 | $is_selected = ""; | |
2254 | } | |
2255 | printf("<option $is_selected value='%d'>%s</option>", | |
2256 | $line["id"], htmlspecialchars($line["title"])); | |
2257 | } | |
2258 | ||
2259 | print "</select>"; | |
2260 | } | |
2261 | ||
2262 | function checkbox_to_sql_bool($val) { | |
2263 | return ($val == "on") ? "true" : "false"; | |
2264 | } | |
2265 | ||
2266 | function getFeedCatTitle($link, $id) { | |
2267 | if ($id == -1) { | |
2268 | return __("Special"); | |
2269 | } else if ($id < -10) { | |
2270 | return __("Labels"); | |
2271 | } else if ($id > 0) { | |
2272 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2273 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2274 | cat_id = ttrss_feed_categories.id"); | |
2275 | if (db_num_rows($result) == 1) { | |
2276 | return db_fetch_result($result, 0, "title"); | |
2277 | } else { | |
2278 | return __("Uncategorized"); | |
2279 | } | |
2280 | } else { | |
2281 | return "getFeedCatTitle($id) failed"; | |
2282 | } | |
2283 | ||
2284 | } | |
2285 | ||
2286 | function getFeedTitle($link, $id) { | |
2287 | if ($id == -1) { | |
2288 | return __("Starred articles"); | |
2289 | } else if ($id == -2) { | |
2290 | return __("Published articles"); | |
2291 | } else if ($id < -10) { | |
2292 | $label_id = -10 - $id; | |
2293 | $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'"); | |
2294 | if (db_num_rows($result) == 1) { | |
2295 | return db_fetch_result($result, 0, "description"); | |
2296 | } else { | |
2297 | return "Unknown label ($label_id)"; | |
2298 | } | |
2299 | ||
2300 | } else if ($id > 0) { | |
2301 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2302 | if (db_num_rows($result) == 1) { | |
2303 | return db_fetch_result($result, 0, "title"); | |
2304 | } else { | |
2305 | return "Unknown feed ($id)"; | |
2306 | } | |
2307 | } else { | |
2308 | return "getFeedTitle($id) failed"; | |
2309 | } | |
2310 | ||
2311 | } | |
2312 | ||
2313 | function get_session_cookie_name() { | |
2314 | return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME); | |
2315 | } | |
2316 | ||
2317 | function print_init_params($link) { | |
2318 | print "<init-params>"; | |
2319 | if ($_SESSION["stored-params"]) { | |
2320 | foreach (array_keys($_SESSION["stored-params"]) as $key) { | |
2321 | if ($key) { | |
2322 | $value = htmlspecialchars($_SESSION["stored-params"][$key]); | |
2323 | print "<param key=\"$key\" value=\"$value\"/>"; | |
2324 | } | |
2325 | } | |
2326 | } | |
2327 | ||
2328 | print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>"; | |
2329 | print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>"; | |
2330 | print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>"; | |
2331 | ||
2332 | print "<param key=\"on_catchup_show_next_feed\" value=\"" . | |
2333 | get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>"; | |
2334 | ||
2335 | print "<param key=\"hide_read_feeds\" value=\"" . | |
2336 | (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>"; | |
2337 | ||
2338 | print "<param key=\"feeds_sort_by_unread\" value=\"" . | |
2339 | (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>"; | |
2340 | ||
2341 | print "<param key=\"confirm_feed_catchup\" value=\"" . | |
2342 | (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>"; | |
2343 | ||
2344 | print "<param key=\"cdm_auto_catchup\" value=\"" . | |
2345 | (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>"; | |
2346 | ||
2347 | print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>"; | |
2348 | ||
2349 | print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>"; | |
2350 | ||
2351 | print "<param key=\"default_view_mode\" value=\"" . | |
2352 | get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>"; | |
2353 | ||
2354 | print "<param key=\"default_view_limit\" value=\"" . | |
2355 | (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>"; | |
2356 | ||
2357 | print "<param key=\"prefs_active_tab\" value=\"" . | |
2358 | get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>"; | |
2359 | ||
2360 | print "<param key=\"infobox_disable_overlay\" value=\"" . | |
2361 | get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>"; | |
2362 | ||
2363 | print "</init-params>"; | |
2364 | } | |
2365 | ||
2366 | function print_runtime_info($link) { | |
2367 | print "<runtime-info>"; | |
2368 | if (ENABLE_UPDATE_DAEMON) { | |
2369 | print "<param key=\"daemon_is_running\" value=\"". | |
2370 | sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>"; | |
2371 | ||
2372 | if ($_SESSION["daemon_stamp_check"] + 600 < time()) { | |
2373 | ||
2374 | $stamp = (int)read_stampfile("update_daemon.stamp"); | |
2375 | ||
2376 | if ($stamp) { | |
2377 | if ($stamp + 86400*3 < time()) { | |
2378 | print "<param key=\"daemon_stamp_ok\" value=\"0\"/>"; | |
2379 | } else { | |
2380 | print "<param key=\"daemon_stamp_ok\" value=\"1\"/>"; | |
2381 | } | |
2382 | ||
2383 | $stamp_fmt = date("Y.m.d, G:i", $stamp); | |
2384 | ||
2385 | print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>"; | |
2386 | } | |
2387 | ||
2388 | $_SESSION["daemon_stamp_check"] = time(); | |
2389 | } | |
2390 | } | |
2391 | ||
2392 | if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) { | |
2393 | ||
2394 | if ($_SESSION["last_version_check"] + 600 < time()) { | |
2395 | $new_version_details = check_for_update($link); | |
2396 | ||
2397 | print "<param key=\"new_version_available\" value=\"". | |
2398 | sprintf("%d", $new_version_details != ""). "\"/>"; | |
2399 | ||
2400 | $_SESSION["last_version_check"] = time(); | |
2401 | } | |
2402 | } | |
2403 | ||
2404 | print "</runtime-info>"; | |
2405 | } | |
2406 | ||
2407 | function getSearchSql($search, $match_on) { | |
2408 | ||
2409 | $search_query_part = ""; | |
2410 | ||
2411 | $keywords = split(" ", $search); | |
2412 | $query_keywords = array(); | |
2413 | ||
2414 | if ($match_on == "both") { | |
2415 | ||
2416 | foreach ($keywords as $k) { | |
2417 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%') | |
2418 | OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2419 | } | |
2420 | ||
2421 | $search_query_part = implode("AND", $query_keywords) . " AND "; | |
2422 | ||
2423 | } else if ($match_on == "title") { | |
2424 | ||
2425 | foreach ($keywords as $k) { | |
2426 | array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))"); | |
2427 | } | |
2428 | ||
2429 | $search_query_part = implode("AND", $query_keywords) . " AND "; | |
2430 | ||
2431 | } else if ($match_on == "content") { | |
2432 | ||
2433 | foreach ($keywords as $k) { | |
2434 | array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))"); | |
2435 | } | |
2436 | } | |
2437 | ||
2438 | $search_query_part = implode("AND", $query_keywords); | |
2439 | ||
2440 | return $search_query_part; | |
2441 | } | |
2442 | ||
2443 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) { | |
2444 | ||
2445 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
2446 | ||
2447 | if ($search) { | |
2448 | ||
2449 | $search_query_part = getSearchSql($search, $match_on); | |
2450 | $search_query_part .= " AND "; | |
2451 | ||
2452 | } else { | |
2453 | $search_query_part = ""; | |
2454 | } | |
2455 | ||
2456 | $view_query_part = ""; | |
2457 | ||
2458 | if ($view_mode == "adaptive") { | |
2459 | if ($search) { | |
2460 | $view_query_part = " "; | |
2461 | } else if ($feed != -1) { | |
2462 | $unread = getFeedUnread($link, $feed, $cat_view); | |
2463 | if ($unread > 0) { | |
2464 | $view_query_part = " unread = true AND "; | |
2465 | } | |
2466 | } | |
2467 | } | |
2468 | ||
2469 | if ($view_mode == "marked") { | |
2470 | $view_query_part = " marked = true AND "; | |
2471 | } | |
2472 | ||
2473 | if ($view_mode == "unread") { | |
2474 | $view_query_part = " unread = true AND "; | |
2475 | } | |
2476 | ||
2477 | if ($limit > 0) { | |
2478 | $limit_query_part = "LIMIT " . $limit; | |
2479 | } | |
2480 | ||
2481 | $vfeed_query_part = ""; | |
2482 | ||
2483 | // override query strategy and enable feed display when searching globally | |
2484 | if ($search && $search_mode == "all_feeds") { | |
2485 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2486 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2487 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2488 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2489 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2490 | id = feed_id) as feed_title,"; | |
2491 | } else if ($feed >= 0 && $search && $search_mode == "this_cat") { | |
2492 | ||
2493 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2494 | ||
2495 | $tmp_result = false; | |
2496 | ||
2497 | if ($cat_view) { | |
2498 | $tmp_result = db_query($link, "SELECT id | |
2499 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2500 | } else { | |
2501 | $tmp_result = db_query($link, "SELECT id | |
2502 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2503 | WHERE id = '$feed') AND id != '$feed'"); | |
2504 | } | |
2505 | ||
2506 | $cat_siblings = array(); | |
2507 | ||
2508 | if (db_num_rows($tmp_result) > 0) { | |
2509 | while ($p = db_fetch_assoc($tmp_result)) { | |
2510 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2511 | } | |
2512 | ||
2513 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2514 | $feed, implode(" OR ", $cat_siblings)); | |
2515 | ||
2516 | } else { | |
2517 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2518 | } | |
2519 | ||
2520 | } else if ($feed >= 0) { | |
2521 | ||
2522 | if ($cat_view) { | |
2523 | ||
2524 | if ($feed > 0) { | |
2525 | $query_strategy_part = "cat_id = '$feed'"; | |
2526 | } else { | |
2527 | $query_strategy_part = "cat_id IS NULL"; | |
2528 | } | |
2529 | ||
2530 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2531 | ||
2532 | } else { | |
2533 | $tmp_result = db_query($link, "SELECT id | |
2534 | FROM ttrss_feeds WHERE parent_feed = '$feed' | |
2535 | ORDER BY cat_id,title"); | |
2536 | ||
2537 | $parent_ids = array(); | |
2538 | ||
2539 | if (db_num_rows($tmp_result) > 0) { | |
2540 | while ($p = db_fetch_assoc($tmp_result)) { | |
2541 | array_push($parent_ids, "feed_id = " . $p["id"]); | |
2542 | } | |
2543 | ||
2544 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2545 | $feed, implode(" OR ", $parent_ids)); | |
2546 | ||
2547 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2548 | } else { | |
2549 | $query_strategy_part = "feed_id = '$feed'"; | |
2550 | } | |
2551 | } | |
2552 | } else if ($feed == -1) { // starred virtual feed | |
2553 | $query_strategy_part = "marked = true"; | |
2554 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2555 | } else if ($feed == -2) { // published virtual feed | |
2556 | $query_strategy_part = "published = true"; | |
2557 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2558 | } else if ($feed <= -10) { // labels | |
2559 | $label_id = -$feed - 11; | |
2560 | ||
2561 | $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels | |
2562 | WHERE id = '$label_id'"); | |
2563 | ||
2564 | $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp"); | |
2565 | ||
2566 | if (!$query_strategy_part) { | |
2567 | return false; | |
2568 | } | |
2569 | ||
2570 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2571 | } else { | |
2572 | $query_strategy_part = "id > 0"; // dumb | |
2573 | } | |
2574 | ||
2575 | if (get_pref($link, 'REVERSE_HEADLINES')) { | |
2576 | $order_by = "updated"; | |
2577 | } else { | |
2578 | $order_by = "updated DESC"; | |
2579 | } | |
2580 | ||
2581 | if ($override_order) { | |
2582 | $order_by = $override_order; | |
2583 | } | |
2584 | ||
2585 | $feed_title = ""; | |
2586 | ||
2587 | if ($search && $search_mode == "all_feeds") { | |
2588 | $feed_title = __("Search results")." ($search)"; | |
2589 | } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
2590 | $feed_title = __("Search results")." ($search, $feed)"; | |
2591 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) { | |
2592 | $feed_title = $feed; | |
2593 | } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) { | |
2594 | ||
2595 | if ($cat_view) { | |
2596 | ||
2597 | if ($feed != 0) { | |
2598 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories | |
2599 | WHERE id = '$feed' AND owner_uid = $owner_uid"); | |
2600 | $feed_title = db_fetch_result($result, 0, "title"); | |
2601 | } else { | |
2602 | $feed_title = __("Uncategorized"); | |
2603 | } | |
2604 | ||
2605 | if ($search) { | |
2606 | $feed_title = __("Searched for")." $search ($feed_title)"; | |
2607 | } | |
2608 | ||
2609 | } else { | |
2610 | ||
2611 | $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds | |
2612 | WHERE id = '$feed' AND owner_uid = $owner_uid"); | |
2613 | ||
2614 | $feed_title = db_fetch_result($result, 0, "title"); | |
2615 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2616 | $last_error = db_fetch_result($result, 0, "last_error"); | |
2617 | ||
2618 | if ($search) { | |
2619 | $feed_title = __("Searched for") . " $search ($feed_title)"; | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | } else if ($feed == -1) { | |
2624 | $feed_title = __("Starred articles"); | |
2625 | } else if ($feed == -2) { | |
2626 | $feed_title = __("Published articles"); | |
2627 | } else if ($feed < -10) { | |
2628 | $label_id = -$feed - 11; | |
2629 | $result = db_query($link, "SELECT description FROM ttrss_labels | |
2630 | WHERE id = '$label_id'"); | |
2631 | $feed_title = db_fetch_result($result, 0, "description"); | |
2632 | ||
2633 | if ($search) { | |
2634 | $feed_title = __("Searched for") . " $search ($feed_title)"; | |
2635 | } | |
2636 | } else { | |
2637 | $feed_title = "?"; | |
2638 | } | |
2639 | ||
2640 | if ($feed < -10) error_reporting (0); | |
2641 | ||
2642 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2643 | ||
2644 | if ($feed >= 0) { | |
2645 | $feed_kind = "Feeds"; | |
2646 | } else { | |
2647 | $feed_kind = "Labels"; | |
2648 | } | |
2649 | ||
2650 | $content_query_part = "content as content_preview,"; | |
2651 | ||
2652 | if ($limit_query_part) { | |
2653 | $offset_query_part = "OFFSET $offset"; | |
2654 | } | |
2655 | ||
2656 | $query = "SELECT | |
2657 | guid, | |
2658 | ttrss_entries.id,ttrss_entries.title, | |
2659 | updated, | |
2660 | unread,feed_id,marked,published,link,last_read, | |
2661 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2662 | $vfeed_query_part | |
2663 | $content_query_part | |
2664 | SUBSTRING(updated,1,19) as updated_noms, | |
2665 | author | |
2666 | FROM | |
2667 | ttrss_entries,ttrss_user_entries,ttrss_feeds | |
2668 | WHERE | |
2669 | ttrss_feeds.hidden = false AND | |
2670 | ttrss_user_entries.feed_id = ttrss_feeds.id AND | |
2671 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2672 | ttrss_user_entries.owner_uid = '$owner_uid' AND | |
2673 | $search_query_part | |
2674 | $view_query_part | |
2675 | $query_strategy_part ORDER BY $order_by | |
2676 | $limit_query_part $offset_query_part"; | |
2677 | ||
2678 | $result = db_query($link, $query); | |
2679 | ||
2680 | if ($_GET["debug"]) print $query; | |
2681 | ||
2682 | } else { | |
2683 | // browsing by tag | |
2684 | ||
2685 | $feed_kind = "Tags"; | |
2686 | ||
2687 | $result = db_query($link, "SELECT | |
2688 | guid, | |
2689 | ttrss_entries.id as id,title, | |
2690 | updated, | |
2691 | unread,feed_id, | |
2692 | marked,link,last_read, | |
2693 | SUBSTRING(last_read,1,19) as last_read_noms, | |
2694 | $vfeed_query_part | |
2695 | $content_query_part | |
2696 | SUBSTRING(updated,1,19) as updated_noms | |
2697 | FROM | |
2698 | ttrss_entries,ttrss_user_entries,ttrss_tags | |
2699 | WHERE | |
2700 | ref_id = ttrss_entries.id AND | |
2701 | ttrss_user_entries.owner_uid = '$owner_uid' AND | |
2702 | post_int_id = int_id AND tag_name = '$feed' AND | |
2703 | $view_query_part | |
2704 | $search_query_part | |
2705 | $query_strategy_part ORDER BY $order_by | |
2706 | $limit_query_part"); | |
2707 | } | |
2708 | ||
2709 | return array($result, $feed_title, $feed_site_url, $last_error); | |
2710 | ||
2711 | } | |
2712 | ||
2713 | function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat, | |
2714 | $search, $search_mode, $match_on) { | |
2715 | ||
2716 | $qfh_ret = queryFeedHeadlines($link, $feed, | |
2717 | 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0, | |
2718 | $owner_uid); | |
2719 | ||
2720 | $result = $qfh_ret[0]; | |
2721 | $feed_title = htmlspecialchars($qfh_ret[1]); | |
2722 | $feed_site_url = $qfh_ret[2]; | |
2723 | $last_error = $qfh_ret[3]; | |
2724 | ||
2725 | # if (!$feed_site_url) $feed_site_url = "http://localhost/"; | |
2726 | ||
2727 | print "<rss version=\"2.0\"> | |
2728 | <channel> | |
2729 | <title>$feed_title</title> | |
2730 | <link>$feed_site_url</link> | |
2731 | <description>Feed generated by Tiny Tiny RSS</description>"; | |
2732 | ||
2733 | while ($line = db_fetch_assoc($result)) { | |
2734 | print "<item>"; | |
2735 | print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>"; | |
2736 | print "<link>" . htmlspecialchars($line["link"]) . "</link>"; | |
2737 | ||
2738 | $tags = get_article_tags($link, $line["id"], $owner_uid); | |
2739 | ||
2740 | foreach ($tags as $tag) { | |
2741 | print "<category>" . htmlspecialchars($tag) . "</category>"; | |
2742 | } | |
2743 | ||
2744 | $rfc822_date = date('r', strtotime($line["updated"])); | |
2745 | ||
2746 | print "<pubDate>$rfc822_date</pubDate>"; | |
2747 | ||
2748 | print "<title>" . | |
2749 | htmlspecialchars($line["title"]) . "</title>"; | |
2750 | ||
2751 | print "<description><![CDATA[" . | |
2752 | $line["content_preview"] . "]]></description>"; | |
2753 | ||
2754 | print "</item>"; | |
2755 | } | |
2756 | ||
2757 | print "</channel></rss>"; | |
2758 | ||
2759 | } | |
2760 | ||
2761 | function getCategoryTitle($link, $cat_id) { | |
2762 | ||
2763 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
2764 | id = '$cat_id'"); | |
2765 | ||
2766 | if (db_num_rows($result) == 1) { | |
2767 | return db_fetch_result($result, 0, "title"); | |
2768 | } else { | |
2769 | return "Uncategorized"; | |
2770 | } | |
2771 | } | |
2772 | ||
2773 | // http://ru2.php.net/strip-tags | |
2774 | ||
2775 | function strip_tags_long($textstring, $allowed){ | |
2776 | while($textstring != strip_tags($textstring, $allowed)) | |
2777 | { | |
2778 | while (strlen($textstring) != 0) | |
2779 | { | |
2780 | if (strlen($textstring) > 1024) { | |
2781 | $otherlen = 1024; | |
2782 | } else { | |
2783 | $otherlen = strlen($textstring); | |
2784 | } | |
2785 | $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed); | |
2786 | $safetext .= $temptext; | |
2787 | $textstring = substr_replace($textstring,'',0,$otherlen); | |
2788 | } | |
2789 | $textstring = $safetext; | |
2790 | } | |
2791 | return $textstring; | |
2792 | } | |
2793 | ||
2794 | ||
2795 | function sanitize_rss($link, $str, $force_strip_tags = false) { | |
2796 | $res = $str; | |
2797 | ||
2798 | if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) { | |
2799 | global $tw_parser; | |
2800 | global $tw_paranoya_setup; | |
2801 | ||
2802 | $res = $tw_parser->strip_tags($res, $tw_paranoya_setup); | |
2803 | ||
2804 | // $res = preg_replace("/\r\n|\n|\r/", "", $res); | |
2805 | // $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>"); | |
2806 | } | |
2807 | ||
2808 | return $res; | |
2809 | } | |
2810 | ||
2811 | function send_headlines_digests($link, $limit = 100) { | |
2812 | ||
2813 | if (!DIGEST_ENABLE) return false; | |
2814 | ||
2815 | $user_limit = DIGEST_EMAIL_LIMIT; | |
2816 | $days = 1; | |
2817 | ||
2818 | print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n"; | |
2819 | ||
2820 | if (DB_TYPE == "pgsql") { | |
2821 | $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'"; | |
2822 | } else if (DB_TYPE == "mysql") { | |
2823 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2824 | } | |
2825 | ||
2826 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
2827 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
2828 | ||
2829 | while ($line = db_fetch_assoc($result)) { | |
2830 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
2831 | print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
2832 | ||
2833 | $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit); | |
2834 | $digest = $tuple[0]; | |
2835 | $headlines_count = $tuple[1]; | |
2836 | ||
2837 | if ($headlines_count > 0) { | |
2838 | $rc = mail($line["login"] . " <" . $line["email"] . ">", | |
2839 | "[tt-rss] New headlines for last 24 hours", $digest, | |
2840 | "From: " . MAIL_FROM . "\n". | |
2841 | "Content-Type: text/plain; charset=\"utf-8\"\n". | |
2842 | "Content-Transfer-Encoding: 8bit\n"); | |
2843 | print "RC=$rc\n"; | |
2844 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
2845 | WHERE id = " . $line["id"]); | |
2846 | } else { | |
2847 | print "No headlines\n"; | |
2848 | } | |
2849 | } | |
2850 | } | |
2851 | ||
2852 | // $digest = prepare_headlines_digest($link, $user_id, $days, $limit); | |
2853 | ||
2854 | } | |
2855 | ||
2856 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) { | |
2857 | $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n"; | |
2858 | $tmp .= "=======================================================\n\n"; | |
2859 | ||
2860 | if (DB_TYPE == "pgsql") { | |
2861 | $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'"; | |
2862 | } else if (DB_TYPE == "mysql") { | |
2863 | $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2864 | } | |
2865 | ||
2866 | $result = db_query($link, "SELECT ttrss_entries.title, | |
2867 | ttrss_feeds.title AS feed_title, | |
2868 | date_entered, | |
2869 | link, | |
2870 | SUBSTRING(last_updated,1,19) AS last_updated | |
2871 | FROM | |
2872 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2873 | WHERE | |
2874 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
2875 | AND include_in_digest = true | |
2876 | AND $interval_query | |
2877 | AND ttrss_user_entries.owner_uid = $user_id | |
2878 | AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC | |
2879 | LIMIT $limit"); | |
2880 | ||
2881 | $cur_feed_title = ""; | |
2882 | ||
2883 | $headlines_count = db_num_rows($result); | |
2884 | ||
2885 | while ($line = db_fetch_assoc($result)) { | |
2886 | $updated = smart_date_time(strtotime($line["last_updated"])); | |
2887 | $feed_title = $line["feed_title"]; | |
2888 | ||
2889 | if ($cur_feed_title != $feed_title) { | |
2890 | $cur_feed_title = $feed_title; | |
2891 | ||
2892 | $tmp .= "$feed_title\n\n"; | |
2893 | } | |
2894 | ||
2895 | $tmp .= " * " . trim($line["title"]) . " - $updated\n"; | |
2896 | $tmp .= " " . trim($line["link"]) . "\n"; | |
2897 | $tmp .= "\n"; | |
2898 | } | |
2899 | ||
2900 | $tmp .= "--- \n"; | |
2901 | $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") . | |
2902 | DIGEST_HOSTNAME . "\n". | |
2903 | __("To unsubscribe, visit your configuration options or contact instance owner.\n"); | |
2904 | ||
2905 | ||
2906 | return array($tmp, $headlines_count); | |
2907 | } | |
2908 | ||
2909 | function check_for_update($link, $brief_fmt = true) { | |
2910 | $releases_feed = "http://tt-rss.spb.ru/releases.rss"; | |
2911 | ||
2912 | if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) { | |
2913 | return; | |
2914 | } | |
2915 | ||
2916 | error_reporting(0); | |
2917 | $rss = fetch_rss($releases_feed); | |
2918 | error_reporting (DEFAULT_ERROR_LEVEL); | |
2919 | ||
2920 | if ($rss) { | |
2921 | ||
2922 | $items = $rss->items; | |
2923 | ||
2924 | if (!$items || !is_array($items)) $items = $rss->entries; | |
2925 | if (!$items || !is_array($items)) $items = $rss; | |
2926 | ||
2927 | if (!is_array($items) || count($items) == 0) { | |
2928 | return; | |
2929 | } | |
2930 | ||
2931 | $latest_item = $items[0]; | |
2932 | ||
2933 | $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"])); | |
2934 | ||
2935 | $release_url = sanitize_rss($link, $latest_item["link"]); | |
2936 | $content = sanitize_rss($link, $latest_item["description"]); | |
2937 | ||
2938 | if (version_compare(VERSION, $latest_version) == -1) { | |
2939 | if ($brief_fmt) { | |
2940 | return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\"> | |
2941 | New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a> | |
2942 | <div id=\"milestoneDetails\">$content</div>"); | |
2943 | } else { | |
2944 | return "New version of Tiny-Tiny RSS ($latest_version) is available: | |
2945 | <div class='milestoneDetails'>$content</div> | |
2946 | Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for | |
2947 | download and update information."; | |
2948 | } | |
2949 | ||
2950 | } | |
2951 | } | |
2952 | } | |
2953 | ||
2954 | function markArticlesById($link, $ids, $cmode) { | |
2955 | ||
2956 | $tmp_ids = array(); | |
2957 | ||
2958 | foreach ($ids as $id) { | |
2959 | array_push($tmp_ids, "ref_id = '$id'"); | |
2960 | } | |
2961 | ||
2962 | $ids_qpart = join(" OR ", $tmp_ids); | |
2963 | ||
2964 | if ($cmode == 0) { | |
2965 | db_query($link, "UPDATE ttrss_user_entries SET | |
2966 | marked = false,last_read = NOW() | |
2967 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2968 | } else if ($cmode == 1) { | |
2969 | db_query($link, "UPDATE ttrss_user_entries SET | |
2970 | marked = true | |
2971 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2972 | } else { | |
2973 | db_query($link, "UPDATE ttrss_user_entries SET | |
2974 | marked = NOT marked,last_read = NOW() | |
2975 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2976 | } | |
2977 | } | |
2978 | ||
2979 | function publishArticlesById($link, $ids, $cmode) { | |
2980 | ||
2981 | $tmp_ids = array(); | |
2982 | ||
2983 | foreach ($ids as $id) { | |
2984 | array_push($tmp_ids, "ref_id = '$id'"); | |
2985 | } | |
2986 | ||
2987 | $ids_qpart = join(" OR ", $tmp_ids); | |
2988 | ||
2989 | if ($cmode == 0) { | |
2990 | db_query($link, "UPDATE ttrss_user_entries SET | |
2991 | published = false,last_read = NOW() | |
2992 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2993 | } else if ($cmode == 1) { | |
2994 | db_query($link, "UPDATE ttrss_user_entries SET | |
2995 | published = true | |
2996 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2997 | } else { | |
2998 | db_query($link, "UPDATE ttrss_user_entries SET | |
2999 | published = NOT published,last_read = NOW() | |
3000 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3001 | } | |
3002 | } | |
3003 | ||
3004 | function catchupArticlesById($link, $ids, $cmode) { | |
3005 | ||
3006 | $tmp_ids = array(); | |
3007 | ||
3008 | foreach ($ids as $id) { | |
3009 | array_push($tmp_ids, "ref_id = '$id'"); | |
3010 | } | |
3011 | ||
3012 | $ids_qpart = join(" OR ", $tmp_ids); | |
3013 | ||
3014 | if ($cmode == 0) { | |
3015 | db_query($link, "UPDATE ttrss_user_entries SET | |
3016 | unread = false,last_read = NOW() | |
3017 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3018 | } else if ($cmode == 1) { | |
3019 | db_query($link, "UPDATE ttrss_user_entries SET | |
3020 | unread = true | |
3021 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3022 | } else { | |
3023 | db_query($link, "UPDATE ttrss_user_entries SET | |
3024 | unread = NOT unread,last_read = NOW() | |
3025 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3026 | } | |
3027 | } | |
3028 | ||
3029 | function catchupArticleById($link, $id, $cmode) { | |
3030 | ||
3031 | if ($cmode == 0) { | |
3032 | db_query($link, "UPDATE ttrss_user_entries SET | |
3033 | unread = false,last_read = NOW() | |
3034 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3035 | } else if ($cmode == 1) { | |
3036 | db_query($link, "UPDATE ttrss_user_entries SET | |
3037 | unread = true | |
3038 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3039 | } else { | |
3040 | db_query($link, "UPDATE ttrss_user_entries SET | |
3041 | unread = NOT unread,last_read = NOW() | |
3042 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3043 | } | |
3044 | } | |
3045 | ||
3046 | function make_guid_from_title($title) { | |
3047 | return preg_replace("/[ \"\',.:;]/", "-", | |
3048 | mb_strtolower(strip_tags($title), 'utf-8')); | |
3049 | } | |
3050 | ||
3051 | function print_headline_subtoolbar($link, $feed_site_url, $feed_title, | |
3052 | $bottom = false, $rtl_content = false, $feed_id = 0, | |
3053 | $is_cat = false, $search = false, $match_on = false, | |
3054 | $search_mode = false, $offset = 0, $limit = 0) { | |
3055 | ||
3056 | $user_page_offset = $offset + 1; | |
3057 | ||
3058 | if (!$bottom) { | |
3059 | $class = "headlinesSubToolbar"; | |
3060 | $tid = "headlineActionsTop"; | |
3061 | } else { | |
3062 | $class = "headlinesSubToolbar"; | |
3063 | $tid = "headlineActionsBottom"; | |
3064 | } | |
3065 | ||
3066 | print "<table class=\"$class\" id=\"$tid\" | |
3067 | width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>"; | |
3068 | ||
3069 | if ($rtl_content) { | |
3070 | $rtl_cpart = "RTL"; | |
3071 | } else { | |
3072 | $rtl_cpart = ""; | |
3073 | } | |
3074 | ||
3075 | $page_prev_link = "javascript:viewFeedGoPage(-1)"; | |
3076 | $page_next_link = "javascript:viewFeedGoPage(1)"; | |
3077 | $page_first_link = "javascript:viewFeedGoPage(0)"; | |
3078 | ||
3079 | $catchup_page_link = "javascript:catchupPage()"; | |
3080 | $catchup_feed_link = "javascript:catchupCurrentFeed()"; | |
3081 | ||
3082 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
3083 | ||
3084 | $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)"; | |
3085 | $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)"; | |
3086 | $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)"; | |
3087 | ||
3088 | $tog_unread_link = "javascript:selectionToggleUnread()"; | |
3089 | $tog_marked_link = "javascript:selectionToggleMarked()"; | |
3090 | $tog_published_link = "javascript:selectionTogglePublished()"; | |
3091 | ||
3092 | } else { | |
3093 | ||
3094 | $sel_all_link = "javascript:cdmSelectArticles('all')"; | |
3095 | $sel_unread_link = "javascript:cdmSelectArticles('unread')"; | |
3096 | $sel_none_link = "javascript:cdmSelectArticles('none')"; | |
3097 | ||
3098 | $tog_unread_link = "javascript:selectionToggleUnread(true)"; | |
3099 | $tog_marked_link = "javascript:selectionToggleMarked(true)"; | |
3100 | $tog_published_link = "javascript:selectionTogglePublished(true)"; | |
3101 | ||
3102 | } | |
3103 | ||
3104 | if (strpos($_SESSION["client.userAgent"], "MSIE") === false) { | |
3105 | ||
3106 | print "<td class=\"headlineActions$rtl_cpart\"> | |
3107 | <ul class=\"headlineDropdownMenu\"> | |
3108 | <li class=\"top2\"> | |
3109 | ".__('Select:')." | |
3110 | <a href=\"$sel_all_link\">".__('All')."</a>, | |
3111 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3112 | <a href=\"$sel_none_link\">".__('None')."</a></li> | |
3113 | <li class=\"vsep\"> </li> | |
3114 | <li class=\"top\">Toggle<ul> | |
3115 | <li onclick=\"$tog_unread_link\">".__('Unread')."</li> | |
3116 | <li onclick=\"$tog_marked_link\">".__('Starred')."</li> | |
3117 | <li onclick=\"$tog_published_link\">".__('Published')."</li> | |
3118 | </ul></li> | |
3119 | <li class=\"vsep\"> </li> | |
3120 | <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul> | |
3121 | <li onclick=\"$catchup_page_link\">".__('This page')."</li> | |
3122 | <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li> | |
3123 | "; | |
3124 | ||
3125 | $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION"); | |
3126 | ||
3127 | if ($limit != 0 && !$search && $enable_pagination) { | |
3128 | print " | |
3129 | <li class=\"vsep\"> </li> | |
3130 | <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul> | |
3131 | <li onclick=\"$page_prev_link\">".__('Previous page')."</li> | |
3132 | <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li> | |
3133 | </ul>"; | |
3134 | } | |
3135 | ||
3136 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3137 | print " | |
3138 | <li class=\"vsep\"> </li> | |
3139 | <li class=\"top3\"> | |
3140 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3141 | '$match_on', '$feed_id', '$is_cat');\"> | |
3142 | ".__('Convert to label')."</a></td>"; | |
3143 | } | |
3144 | print " | |
3145 | </td>"; | |
3146 | ||
3147 | } else { | |
3148 | // old style subtoolbar: | |
3149 | ||
3150 | print "<td class=\"headlineActions$rtl_cpart\">". | |
3151 | __('Select:')." | |
3152 | <a href=\"$sel_all_link\">".__('All')."</a>, | |
3153 | <a href=\"$sel_unread_link\">".__('Unread')."</a>, | |
3154 | <a href=\"$sel_none_link\">".__('None')."</a> | |
3155 | ". | |
3156 | __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>, | |
3157 | <a href=\"$tog_marked_link\">".__('Starred')."</a> | |
3158 | ". | |
3159 | __('Mark as read:')." | |
3160 | <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>, | |
3161 | <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>"; | |
3162 | ||
3163 | if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3164 | ||
3165 | print " | |
3166 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3167 | '$match_on', '$feed_id', '$is_cat');\"> | |
3168 | ".__('Convert to label')."</a>"; | |
3169 | } | |
3170 | ||
3171 | print "</td>"; | |
3172 | ||
3173 | } | |
3174 | ||
3175 | /* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) { | |
3176 | print "<td class=\"headlineActions$rtl_cpart\"> | |
3177 | <a href=\"javascript:labelFromSearch('$search', '$search_mode', | |
3178 | '$match_on', '$feed_id', '$is_cat');\"> | |
3179 | ".__('Convert to Label')."</a></td>"; | |
3180 | } */ | |
3181 | ||
3182 | print "<td class=\"headlineTitle$rtl_cpart\">"; | |
3183 | ||
3184 | if ($feed_site_url) { | |
3185 | if (!$bottom) { | |
3186 | $target = "target=\"_new\""; | |
3187 | } | |
3188 | print "<a $target href=\"$feed_site_url\">$feed_title</a>"; | |
3189 | } else { | |
3190 | print $feed_title; | |
3191 | } | |
3192 | ||
3193 | if ($search) { | |
3194 | $search_q = "&q=$search&m=$match_on&smode=$search_mode"; | |
3195 | } | |
3196 | ||
3197 | if ($user_page_offset > 1) { | |
3198 | print " [$user_page_offset] "; | |
3199 | } | |
3200 | ||
3201 | if (!$bottom) { | |
3202 | print " | |
3203 | <a target=\"_new\" | |
3204 | href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\"> | |
3205 | <img class=\"noborder\" | |
3206 | alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\"> | |
3207 | </a>"; | |
3208 | } | |
3209 | ||
3210 | print "</td>"; | |
3211 | print "</tr></table>"; | |
3212 | ||
3213 | } | |
3214 | ||
3215 | function outputFeedList($link, $tags = false) { | |
3216 | ||
3217 | print "<ul class=\"feedList\" id=\"feedList\">"; | |
3218 | ||
3219 | $owner_uid = $_SESSION["uid"]; | |
3220 | ||
3221 | /* virtual feeds */ | |
3222 | ||
3223 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3224 | print "<li class=\"feedCat\">".__('Special')."</li>"; | |
3225 | print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
3226 | } | |
3227 | ||
3228 | $num_starred = getFeedUnread($link, -1); | |
3229 | $num_published = getFeedUnread($link, -2); | |
3230 | ||
3231 | $class = "virt"; | |
3232 | ||
3233 | if ($num_starred > 0) $class .= "Unread"; | |
3234 | ||
3235 | printFeedEntry(-1, $class, __("Starred articles"), $num_starred, | |
3236 | "images/mark_set.png", $link); | |
3237 | ||
3238 | $class = "virt"; | |
3239 | ||
3240 | if ($num_published > 0) $class .= "Unread"; | |
3241 | ||
3242 | printFeedEntry(-2, $class, __("Published articles"), $num_published, | |
3243 | "images/pub_set.png", $link); | |
3244 | ||
3245 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3246 | print "</ul>"; | |
3247 | } | |
3248 | ||
3249 | if (!$tags) { | |
3250 | ||
3251 | if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) { | |
3252 | ||
3253 | $result = db_query($link, "SELECT id,sql_exp,description FROM | |
3254 | ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description"); | |
3255 | ||
3256 | if (db_num_rows($result) > 0) { | |
3257 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3258 | print "<li class=\"feedCat\">".__('Labels')."</li>"; | |
3259 | print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
3260 | } else { | |
3261 | print "<li><hr></li>"; | |
3262 | } | |
3263 | } | |
3264 | ||
3265 | while ($line = db_fetch_assoc($result)) { | |
3266 | ||
3267 | error_reporting (0); | |
3268 | ||
3269 | $label_id = -$line['id'] - 11; | |
3270 | $count = getFeedUnread($link, $label_id); | |
3271 | ||
3272 | $class = "label"; | |
3273 | ||
3274 | if ($count > 0) { | |
3275 | $class .= "Unread"; | |
3276 | } | |
3277 | ||
3278 | error_reporting (DEFAULT_ERROR_LEVEL); | |
3279 | ||
3280 | printFeedEntry($label_id, | |
3281 | $class, $line["description"], | |
3282 | $count, "images/label.png", $link); | |
3283 | ||
3284 | } | |
3285 | ||
3286 | if (db_num_rows($result) > 0) { | |
3287 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3288 | print "</ul>"; | |
3289 | } | |
3290 | } | |
3291 | ||
3292 | } | |
3293 | ||
3294 | if (!get_pref($link, 'ENABLE_FEED_CATS')) { | |
3295 | print "<li><hr></li>"; | |
3296 | } | |
3297 | ||
3298 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3299 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
3300 | $order_by_qpart = "category,unread DESC,title"; | |
3301 | } else { | |
3302 | $order_by_qpart = "category,title"; | |
3303 | } | |
3304 | } else { | |
3305 | if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) { | |
3306 | $order_by_qpart = "unread DESC,title"; | |
3307 | } else { | |
3308 | $order_by_qpart = "title"; | |
3309 | } | |
3310 | } | |
3311 | ||
3312 | $result = db_query($link, "SELECT ttrss_feeds.*, | |
3313 | SUBSTRING(last_updated,1,19) AS last_updated_noms, | |
3314 | (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries | |
3315 | WHERE feed_id = ttrss_feeds.id AND unread = true | |
3316 | AND ttrss_user_entries.ref_id = ttrss_entries.id | |
3317 | AND owner_uid = '$owner_uid') as unread, | |
3318 | cat_id,last_error, | |
3319 | ttrss_feed_categories.title AS category, | |
3320 | ttrss_feed_categories.collapsed | |
3321 | FROM ttrss_feeds LEFT JOIN ttrss_feed_categories | |
3322 | ON (ttrss_feed_categories.id = cat_id) | |
3323 | WHERE | |
3324 | ttrss_feeds.hidden = false AND | |
3325 | ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL | |
3326 | ORDER BY $order_by_qpart"); | |
3327 | ||
3328 | $actid = $_GET["actid"]; | |
3329 | ||
3330 | /* real feeds */ | |
3331 | ||
3332 | $lnum = 0; | |
3333 | ||
3334 | $total_unread = 0; | |
3335 | ||
3336 | $category = ""; | |
3337 | ||
3338 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
3339 | ||
3340 | while ($line = db_fetch_assoc($result)) { | |
3341 | ||
3342 | $feed = trim($line["title"]); | |
3343 | ||
3344 | if (!$feed) $feed = "[Untitled]"; | |
3345 | ||
3346 | $feed_id = $line["id"]; | |
3347 | ||
3348 | $subop = $_GET["subop"]; | |
3349 | ||
3350 | $unread = $line["unread"]; | |
3351 | ||
3352 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
3353 | $last_updated = smart_date_time(strtotime($line["last_updated_noms"])); | |
3354 | } else { | |
3355 | $last_updated = date($short_date, strtotime($line["last_updated_noms"])); | |
3356 | } | |
3357 | ||
3358 | $rtl_content = sql_bool_to_bool($line["rtl_content"]); | |
3359 | ||
3360 | if ($rtl_content) { | |
3361 | $rtl_tag = "dir=\"RTL\""; | |
3362 | } else { | |
3363 | $rtl_tag = ""; | |
3364 | } | |
3365 | ||
3366 | $tmp_result = db_query($link, | |
3367 | "SELECT id,COUNT(unread) AS unread | |
3368 | FROM ttrss_feeds LEFT JOIN ttrss_user_entries | |
3369 | ON (ttrss_feeds.id = ttrss_user_entries.feed_id) | |
3370 | WHERE parent_feed = '$feed_id' AND unread = true | |
3371 | GROUP BY ttrss_feeds.id"); | |
3372 | ||
3373 | if (db_num_rows($tmp_result) > 0) { | |
3374 | while ($l = db_fetch_assoc($tmp_result)) { | |
3375 | $unread += $l["unread"]; | |
3376 | } | |
3377 | } | |
3378 | ||
3379 | $cat_id = $line["cat_id"]; | |
3380 | ||
3381 | $tmp_category = $line["category"]; | |
3382 | ||
3383 | if (!$tmp_category) { | |
3384 | $tmp_category = __("Uncategorized"); | |
3385 | } | |
3386 | ||
3387 | // $class = ($lnum % 2) ? "even" : "odd"; | |
3388 | ||
3389 | if ($line["last_error"]) { | |
3390 | $class = "error"; | |
3391 | } else { | |
3392 | $class = "feed"; | |
3393 | } | |
3394 | ||
3395 | if ($unread > 0) $class .= "Unread"; | |
3396 | ||
3397 | if ($actid == $feed_id) { | |
3398 | $class .= "Selected"; | |
3399 | } | |
3400 | ||
3401 | $total_unread += $unread; | |
3402 | ||
3403 | if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) { | |
3404 | ||
3405 | if ($category) { | |
3406 | print "</ul></li>"; | |
3407 | } | |
3408 | ||
3409 | $category = $tmp_category; | |
3410 | ||
3411 | $collapsed = $line["collapsed"]; | |
3412 | ||
3413 | // workaround for NULL category | |
3414 | if ($category == __("Uncategorized")) { | |
3415 | if ($_COOKIE["ttrss_vf_uclps"] == 1) { | |
3416 | $collapsed = "t"; | |
3417 | } | |
3418 | } | |
3419 | ||
3420 | if ($collapsed == "t" || $collapsed == "1") { | |
3421 | $holder_class = "invisible"; | |
3422 | $ellipsis = "..."; | |
3423 | } else { | |
3424 | $holder_class = "feedCatHolder"; | |
3425 | $ellipsis = ""; | |
3426 | } | |
3427 | ||
3428 | $cat_id = sprintf("%d", $cat_id); | |
3429 | ||
3430 | $cat_unread = getCategoryUnread($link, $cat_id); | |
3431 | ||
3432 | $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread"; | |
3433 | ||
3434 | print "<li class=\"feedCat\" id=\"FCAT-$cat_id\"> | |
3435 | <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a> | |
3436 | <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\"> | |
3437 | <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\" | |
3438 | class=\"$catctr_class\">($cat_unread)</span> $ellipsis | |
3439 | </a></li>"; | |
3440 | ||
3441 | // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function | |
3442 | // -> keyboard navigation, etc. | |
3443 | print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">"; | |
3444 | } | |
3445 | ||
3446 | printFeedEntry($feed_id, $class, $feed, $unread, | |
3447 | ICONS_DIR."/$feed_id.ico", $link, $rtl_content, | |
3448 | $last_updated, $line["last_error"]); | |
3449 | ||
3450 | ++$lnum; | |
3451 | } | |
3452 | ||
3453 | if (db_num_rows($result) == 0) { | |
3454 | print "<li>".__('No feeds to display.')."</li>"; | |
3455 | } | |
3456 | ||
3457 | } else { | |
3458 | ||
3459 | // tags | |
3460 | ||
3461 | /* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count | |
3462 | FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE | |
3463 | post_int_id = ttrss_user_entries.int_id AND | |
3464 | unread = true AND ref_id = ttrss_entries.id | |
3465 | AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name | |
3466 | UNION | |
3467 | select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid' | |
3468 | ORDER BY tag_name"); */ | |
3469 | ||
3470 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3471 | print "<li class=\"feedCat\">".__('Tags')."</li>"; | |
3472 | print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">"; | |
3473 | } | |
3474 | ||
3475 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
3476 | FROM ttrss_user_entries WHERE int_id = post_int_id | |
3477 | AND unread = true)) AS count FROM ttrss_tags | |
3478 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name | |
3479 | ORDER BY count DESC LIMIT 50"); | |
3480 | ||
3481 | $tags = array(); | |
3482 | ||
3483 | while ($line = db_fetch_assoc($result)) { | |
3484 | $tags[$line["tag_name"]] += $line["count"]; | |
3485 | } | |
3486 | ||
3487 | foreach (array_keys($tags) as $tag) { | |
3488 | ||
3489 | $unread = $tags[$tag]; | |
3490 | ||
3491 | $class = "tag"; | |
3492 | ||
3493 | if ($unread > 0) { | |
3494 | $class .= "Unread"; | |
3495 | } | |
3496 | ||
3497 | printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link); | |
3498 | ||
3499 | } | |
3500 | ||
3501 | if (db_num_rows($result) == 0) { | |
3502 | print "<li>No tags to display.</li>"; | |
3503 | } | |
3504 | ||
3505 | if (get_pref($link, 'ENABLE_FEED_CATS')) { | |
3506 | print "</ul>"; | |
3507 | } | |
3508 | ||
3509 | } | |
3510 | ||
3511 | print "</ul>"; | |
3512 | ||
3513 | } | |
3514 | ||
3515 | function get_article_tags($link, $id, $owner_uid = 0) { | |
3516 | ||
3517 | $a_id = db_escape_string($id); | |
3518 | ||
3519 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
3520 | ||
3521 | $tmp_result = db_query($link, "SELECT DISTINCT tag_name, | |
3522 | owner_uid as owner FROM | |
3523 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
3524 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"); | |
3525 | ||
3526 | $tags = array(); | |
3527 | ||
3528 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
3529 | array_push($tags, $tmp_line["tag_name"]); | |
3530 | } | |
3531 | ||
3532 | return $tags; | |
3533 | } | |
3534 | ||
3535 | function trim_value(&$value) { | |
3536 | $value = trim($value); | |
3537 | } | |
3538 | ||
3539 | function trim_array($array) { | |
3540 | $tmp = $array; | |
3541 | array_walk($tmp, 'trim_value'); | |
3542 | return $tmp; | |
3543 | } | |
3544 | ||
3545 | function tag_is_valid($tag) { | |
3546 | if ($tag == '') return false; | |
3547 | if (preg_match("/^[0-9]*$/", $tag)) return false; | |
3548 | ||
3549 | $tag = iconv("utf-8", "utf-8", $tag); | |
3550 | if (!$tag) return false; | |
3551 | ||
3552 | return true; | |
3553 | } | |
3554 | ||
3555 | function render_login_form($link, $mobile = false) { | |
3556 | if (!$mobile) { | |
3557 | require_once "login_form.php"; | |
3558 | } else { | |
3559 | require_once "mobile/login_form.php"; | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | // from http://developer.apple.com/internet/safari/faq.html | |
3564 | function no_cache_incantation() { | |
3565 | header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :) | |
3566 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified | |
3567 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1 | |
3568 | header("Cache-Control: post-check=0, pre-check=0", false); | |
3569 | header("Pragma: no-cache"); // HTTP/1.0 | |
3570 | } | |
3571 | ||
3572 | function format_warning($msg, $id = "") { | |
3573 | return "<div class=\"warning\" id=\"$id\"> | |
3574 | <img src=\"images/sign_excl.png\">$msg</div>"; | |
3575 | } | |
3576 | ||
3577 | function format_notice($msg) { | |
3578 | return "<div class=\"notice\"> | |
3579 | <img src=\"images/sign_info.png\">$msg</div>"; | |
3580 | } | |
3581 | ||
3582 | function format_error($msg) { | |
3583 | return "<div class=\"error\"> | |
3584 | <img src=\"images/sign_excl.png\">$msg</div>"; | |
3585 | } | |
3586 | ||
3587 | function print_notice($msg) { | |
3588 | return print format_notice($msg); | |
3589 | } | |
3590 | ||
3591 | function print_warning($msg) { | |
3592 | return print format_warning($msg); | |
3593 | } | |
3594 | ||
3595 | function print_error($msg) { | |
3596 | return print format_error($msg); | |
3597 | } | |
3598 | ||
3599 | ||
3600 | function T_sprintf() { | |
3601 | $args = func_get_args(); | |
3602 | return vsprintf(__(array_shift($args)), $args); | |
3603 | } | |
3604 | ||
3605 | function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) { | |
3606 | ||
3607 | /* we can figure out feed_id from article id anyway, why do we | |
3608 | * pass feed_id here? */ | |
3609 | ||
3610 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
3611 | WHERE ref_id = '$id'"); | |
3612 | ||
3613 | $feed_id = db_fetch_result($result, 0, "feed_id"); | |
3614 | ||
3615 | print "<article id='$id'><![CDATA["; | |
3616 | ||
3617 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
3618 | WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]); | |
3619 | ||
3620 | if (db_num_rows($result) == 1) { | |
3621 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
3622 | } else { | |
3623 | $rtl_content = false; | |
3624 | } | |
3625 | ||
3626 | if ($rtl_content) { | |
3627 | $rtl_tag = "dir=\"RTL\""; | |
3628 | $rtl_class = "RTL"; | |
3629 | } else { | |
3630 | $rtl_tag = ""; | |
3631 | $rtl_class = ""; | |
3632 | } | |
3633 | ||
3634 | if ($mark_as_read) { | |
3635 | $result = db_query($link, "UPDATE ttrss_user_entries | |
3636 | SET unread = false,last_read = NOW() | |
3637 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3638 | } | |
3639 | ||
3640 | $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id, | |
3641 | SUBSTRING(updated,1,16) as updated, | |
3642 | (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url, | |
3643 | num_comments, | |
3644 | author | |
3645 | FROM ttrss_entries,ttrss_user_entries | |
3646 | WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]); | |
3647 | ||
3648 | if ($result) { | |
3649 | ||
3650 | $link_target = ""; | |
3651 | ||
3652 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
3653 | $link_target = "target=\"_new\""; | |
3654 | } | |
3655 | ||
3656 | $line = db_fetch_assoc($result); | |
3657 | ||
3658 | if ($line["icon_url"]) { | |
3659 | $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">"; | |
3660 | } else { | |
3661 | $feed_icon = " "; | |
3662 | } | |
3663 | ||
3664 | /* if ($line["comments"] && $line["link"] != $line["comments"]) { | |
3665 | $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)"; | |
3666 | } else { | |
3667 | $entry_comments = ""; | |
3668 | } */ | |
3669 | ||
3670 | $num_comments = $line["num_comments"]; | |
3671 | $entry_comments = ""; | |
3672 | ||
3673 | if ($num_comments > 0) { | |
3674 | if ($line["comments"]) { | |
3675 | $comments_url = $line["comments"]; | |
3676 | } else { | |
3677 | $comments_url = $line["link"]; | |
3678 | } | |
3679 | $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>"; | |
3680 | } else { | |
3681 | if ($line["comments"] && $line["link"] != $line["comments"]) { | |
3682 | $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>"; | |
3683 | } | |
3684 | } | |
3685 | ||
3686 | print "<div class=\"postReply\">"; | |
3687 | ||
3688 | print "<div class=\"postHeader\">"; | |
3689 | ||
3690 | $entry_author = $line["author"]; | |
3691 | ||
3692 | if ($entry_author) { | |
3693 | $entry_author = __(" - by ") . $entry_author; | |
3694 | } | |
3695 | ||
3696 | $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'), | |
3697 | strtotime($line["updated"])); | |
3698 | ||
3699 | print "<div class=\"postDate$rtl_class\">$parsed_updated</div>"; | |
3700 | ||
3701 | if ($line["link"]) { | |
3702 | print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" . | |
3703 | $line["title"] . "</a>$entry_author</div>"; | |
3704 | } else { | |
3705 | print "<div clear='both'>" . $line["title"] . "$entry_author</div>"; | |
3706 | } | |
3707 | ||
3708 | /* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM | |
3709 | ttrss_tags WHERE post_int_id = " . $line["int_id"] . " | |
3710 | ORDER BY tag_name"); */ | |
3711 | ||
3712 | $tags = get_article_tags($link, $id); | |
3713 | ||
3714 | $tags_str = ""; | |
3715 | $f_tags_str = ""; | |
3716 | ||
3717 | $num_tags = 0; | |
3718 | ||
3719 | foreach ($tags as $tag) { | |
3720 | $num_tags++; | |
3721 | $tag_escaped = str_replace("'", "\\'", $tag); | |
3722 | ||
3723 | $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, "; | |
3724 | ||
3725 | if ($num_tags == 6) { | |
3726 | $tags_str .= "<a href=\"javascript:showBlockElement('allEntryTags')\">...</a>"; | |
3727 | } else if ($num_tags < 6) { | |
3728 | $tags_str .= $tag_str; | |
3729 | } | |
3730 | $f_tags_str .= $tag_str; | |
3731 | } | |
3732 | ||
3733 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
3734 | $f_tags_str = preg_replace("/, $/", "", $f_tags_str); | |
3735 | ||
3736 | if (!$entry_comments) $entry_comments = " "; # placeholder | |
3737 | ||
3738 | if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>'; | |
3739 | ||
3740 | print "<div style='float : right'>$tags_str | |
3741 | <a title=\"Edit tags for this article\" | |
3742 | href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div> | |
3743 | <div clear='both'>$entry_comments</div>"; | |
3744 | ||
3745 | print "</div>"; | |
3746 | ||
3747 | print "<div class=\"postIcon\">" . $feed_icon . "</div>"; | |
3748 | print "<div class=\"postContent\">"; | |
3749 | ||
3750 | print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>"; | |
3751 | ||
3752 | $line["content"] = sanitize_rss($link, $line["content"]); | |
3753 | ||
3754 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
3755 | $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]); | |
3756 | } | |
3757 | ||
3758 | print $line["content"] . "</div>"; | |
3759 | ||
3760 | print "</div>"; | |
3761 | ||
3762 | } | |
3763 | ||
3764 | print "]]></article>"; | |
3765 | ||
3766 | } | |
3767 | ||
3768 | function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view, | |
3769 | $next_unread_feed, $offset) { | |
3770 | ||
3771 | $timing_info = getmicrotime(); | |
3772 | ||
3773 | $topmost_article_ids = array(); | |
3774 | ||
3775 | if (!$offset) { | |
3776 | $offset = 0; | |
3777 | } | |
3778 | ||
3779 | if ($subop == "undefined") $subop = ""; | |
3780 | ||
3781 | if ($subop == "CatchupSelected") { | |
3782 | $ids = split(",", db_escape_string($_GET["ids"])); | |
3783 | $cmode = sprintf("%d", $_GET["cmode"]); | |
3784 | ||
3785 | catchupArticlesById($link, $ids, $cmode); | |
3786 | } | |
3787 | ||
3788 | if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) { | |
3789 | update_generic_feed($link, $feed, $cat_view); | |
3790 | } | |
3791 | ||
3792 | if ($subop == "MarkAllRead") { | |
3793 | catchup_feed($link, $feed, $cat_view); | |
3794 | ||
3795 | if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) { | |
3796 | if ($next_unread_feed) { | |
3797 | $feed = $next_unread_feed; | |
3798 | } | |
3799 | } | |
3800 | } | |
3801 | ||
3802 | if ($feed_id > 0) { | |
3803 | $result = db_query($link, | |
3804 | "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1"); | |
3805 | ||
3806 | if (db_num_rows($result) == 0) { | |
3807 | print "<div align='center'>".__('Feed not found.')."</div>"; | |
3808 | return; | |
3809 | } | |
3810 | } | |
3811 | ||
3812 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
3813 | ||
3814 | $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds | |
3815 | WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]); | |
3816 | ||
3817 | if (db_num_rows($result) == 1) { | |
3818 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
3819 | } else { | |
3820 | $rtl_content = false; | |
3821 | } | |
3822 | ||
3823 | if ($rtl_content) { | |
3824 | $rtl_tag = "dir=\"RTL\""; | |
3825 | } else { | |
3826 | $rtl_tag = ""; | |
3827 | } | |
3828 | } else { | |
3829 | $rtl_tag = ""; | |
3830 | $rtl_content = false; | |
3831 | } | |
3832 | ||
3833 | $script_dt_add = get_script_dt_add(); | |
3834 | ||
3835 | /// START ///////////////////////////////////////////////////////////////////////////////// | |
3836 | ||
3837 | $search = db_escape_string($_GET["query"]); | |
3838 | $search_mode = db_escape_string($_GET["search_mode"]); | |
3839 | $match_on = db_escape_string($_GET["match_on"]); | |
3840 | ||
3841 | if (!$match_on) { | |
3842 | $match_on = "both"; | |
3843 | } | |
3844 | ||
3845 | $real_offset = $offset * $limit; | |
3846 | ||
3847 | if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info); | |
3848 | ||
3849 | $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, | |
3850 | $search, $search_mode, $match_on, false, $real_offset); | |
3851 | ||
3852 | if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info); | |
3853 | ||
3854 | $result = $qfh_ret[0]; | |
3855 | $feed_title = $qfh_ret[1]; | |
3856 | $feed_site_url = $qfh_ret[2]; | |
3857 | $last_error = $qfh_ret[3]; | |
3858 | ||
3859 | /// STOP ////////////////////////////////////////////////////////////////////////////////// | |
3860 | ||
3861 | if (!$offset) { | |
3862 | print "<div id=\"headlinesContainer\" $rtl_tag>"; | |
3863 | ||
3864 | if (!$result) { | |
3865 | print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>"; | |
3866 | return; | |
3867 | } | |
3868 | ||
3869 | print_headline_subtoolbar($link, $feed_site_url, $feed_title, false, | |
3870 | $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode, | |
3871 | $offset, $limit); | |
3872 | ||
3873 | print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">"; | |
3874 | } | |
3875 | ||
3876 | if (db_num_rows($result) > 0) { | |
3877 | ||
3878 | # print "\{$offset}"; | |
3879 | ||
3880 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { | |
3881 | print "<table class=\"headlinesList\" id=\"headlinesList\" | |
3882 | cellspacing=\"0\">"; | |
3883 | } | |
3884 | ||
3885 | $lnum = 0; | |
3886 | ||
3887 | error_reporting (DEFAULT_ERROR_LEVEL); | |
3888 | ||
3889 | $num_unread = 0; | |
3890 | ||
3891 | while ($line = db_fetch_assoc($result)) { | |
3892 | ||
3893 | $class = ($lnum % 2) ? "even" : "odd"; | |
3894 | ||
3895 | $id = $line["id"]; | |
3896 | $feed_id = $line["feed_id"]; | |
3897 | ||
3898 | if (count($topmost_article_ids) < 5) { | |
3899 | array_push($topmost_article_ids, $id); | |
3900 | } | |
3901 | ||
3902 | if ($line["last_read"] == "" && | |
3903 | ($line["unread"] != "t" && $line["unread"] != "1")) { | |
3904 | ||
3905 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\" | |
3906 | alt=\"Updated\">"; | |
3907 | } else { | |
3908 | $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\" | |
3909 | alt=\"Updated\">"; | |
3910 | } | |
3911 | ||
3912 | if ($line["unread"] == "t" || $line["unread"] == "1") { | |
3913 | $class .= "Unread"; | |
3914 | ++$num_unread; | |
3915 | $is_unread = true; | |
3916 | } else { | |
3917 | $is_unread = false; | |
3918 | } | |
3919 | ||
3920 | if ($line["marked"] == "t" || $line["marked"] == "1") { | |
3921 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.png\" | |
3922 | class=\"markedPic\" | |
3923 | alt=\"Reset mark\" onclick='javascript:tMark($id)'>"; | |
3924 | } else { | |
3925 | $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.png\" | |
3926 | class=\"markedPic\" | |
3927 | alt=\"Set mark\" onclick='javascript:tMark($id)'>"; | |
3928 | } | |
3929 | ||
3930 | if ($line["published"] == "t" || $line["published"] == "1") { | |
3931 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.png\" | |
3932 | class=\"markedPic\" | |
3933 | alt=\"Unpublish\" onclick='javascript:tPub($id)'>"; | |
3934 | } else { | |
3935 | $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.png\" | |
3936 | class=\"markedPic\" | |
3937 | alt=\"Publish\" onclick='javascript:tPub($id)'>"; | |
3938 | } | |
3939 | ||
3940 | # $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" . | |
3941 | # $line["title"] . "</a>"; | |
3942 | ||
3943 | $content_link = "<a href=\"javascript:view($id,$feed_id);\">" . | |
3944 | $line["title"] . "</a>"; | |
3945 | ||
3946 | # $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" . | |
3947 | # $line["title"] . "</a>"; | |
3948 | ||
3949 | if (get_pref($link, 'HEADLINES_SMART_DATE')) { | |
3950 | $updated_fmt = smart_date_time(strtotime($line["updated_noms"])); | |
3951 | } else { | |
3952 | $short_date = get_pref($link, 'SHORT_DATE_FORMAT'); | |
3953 | $updated_fmt = date($short_date, strtotime($line["updated_noms"])); | |
3954 | } | |
3955 | ||
3956 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
3957 | $content_preview = truncate_string(strip_tags($line["content_preview"]), | |
3958 | 100); | |
3959 | } | |
3960 | ||
3961 | $entry_author = $line["author"]; | |
3962 | ||
3963 | if ($entry_author) { | |
3964 | $entry_author = " - by $entry_author"; | |
3965 | } | |
3966 | ||
3967 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) { | |
3968 | ||
3969 | print "<tr class='$class' id='RROW-$id'>"; | |
3970 | ||
3971 | print "<td class='hlUpdPic'>$update_pic</td>"; | |
3972 | ||
3973 | print "<td class='hlSelectRow'> | |
3974 | <input type=\"checkbox\" onclick=\"tSR(this)\" | |
3975 | id=\"RCHK-$id\"> | |
3976 | </td>"; | |
3977 | ||
3978 | print "<td class='hlMarkedPic'>$marked_pic</td>"; | |
3979 | print "<td class='hlMarkedPic'>$published_pic</td>"; | |
3980 | ||
3981 | if ($line["feed_title"]) { | |
3982 | print "<td class='hlContent'>$content_link</td>"; | |
3983 | print "<td class='hlFeed'> | |
3984 | <a href=\"javascript:viewfeed($feed_id, '', false)\">". | |
3985 | $line["feed_title"]."</a> </td>"; | |
3986 | } else { | |
3987 | print "<td class='hlContent' valign='middle'>"; | |
3988 | ||
3989 | print "<a href=\"javascript:view($id,$feed_id);\">" . | |
3990 | $line["title"]; | |
3991 | ||
3992 | if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) { | |
3993 | if ($content_preview) { | |
3994 | print "<span class=\"contentPreview\"> - $content_preview</span>"; | |
3995 | } | |
3996 | } | |
3997 | ||
3998 | print "</a>"; | |
3999 | print "</td>"; | |
4000 | } | |
4001 | ||
4002 | print "<td class=\"hlUpdated\"><nobr>$updated_fmt </nobr></td>"; | |
4003 | ||
4004 | print "</tr>"; | |
4005 | ||
4006 | } else { | |
4007 | ||
4008 | if ($is_unread) { | |
4009 | $add_class = "Unread"; | |
4010 | } else { | |
4011 | $add_class = ""; | |
4012 | } | |
4013 | ||
4014 | print "<div class=\"cdmArticle$add_class\" | |
4015 | id=\"RROW-$id\" onmouseover='cdmMouseIn(this)' | |
4016 | onmouseout='cdmMouseOut(this)'>"; | |
4017 | ||
4018 | print "<div class=\"cdmHeader\">"; | |
4019 | ||
4020 | print "<div class=\"articleUpdated\">$updated_fmt</div>"; | |
4021 | ||
4022 | print "<a class=\"title\" | |
4023 | onclick=\"javascript:toggleUnread($id, 0)\" | |
4024 | target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>"; | |
4025 | ||
4026 | print $entry_author; | |
4027 | ||
4028 | if ($line["feed_title"]) { | |
4029 | print " (<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)"; | |
4030 | } | |
4031 | ||
4032 | print "</div>"; | |
4033 | ||
4034 | if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) { | |
4035 | $line["content_preview"] = preg_replace("/href=/i", | |
4036 | "target=\"_new\" href=", $line["content_preview"]); | |
4037 | } | |
4038 | ||
4039 | print "<div class=\"cdmContent\">" . $line["content_preview"] . "</div><br clear=\"all\">"; | |
4040 | ||
4041 | print "<div class=\"cdmFooter\"><span class='s0'>"; | |
4042 | ||
4043 | /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */ | |
4044 | ||
4045 | print __("Select:"). | |
4046 | " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this, | |
4047 | 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">"; | |
4048 | ||
4049 | print "</span><span class='s1'>$marked_pic</span> "; | |
4050 | print "<span class='s1'>$published_pic</span> "; | |
4051 | ||
4052 | $tags = get_article_tags($link, $id); | |
4053 | ||
4054 | $tags_str = ""; | |
4055 | $full_tags_str = ""; | |
4056 | $num_tags = 0; | |
4057 | ||
4058 | foreach ($tags as $tag) { | |
4059 | $num_tags++; | |
4060 | $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
4061 | if ($num_tags < 5) { | |
4062 | $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, "; | |
4063 | } else if ($num_tags == 5) { | |
4064 | $tags_str .= "..."; | |
4065 | } | |
4066 | } | |
4067 | ||
4068 | $tags_str = preg_replace("/, $/", "", $tags_str); | |
4069 | $full_tags_str = preg_replace("/, $/", "", $full_tags_str); | |
4070 | ||
4071 | $all_tags_div = "<span class='cdmAllTagsCtr'>...<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>"; | |
4072 | ||
4073 | $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str); | |
4074 | ||
4075 | ||
4076 | if ($tags_str == "") $tags_str = "no tags"; | |
4077 | ||
4078 | // print "<img src='images/tag.png' class='markedPic'>"; | |
4079 | ||
4080 | print "<span class='s1'>Tags: $tags_str <a title=\"Edit tags for this article\" | |
4081 | href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>"; | |
4082 | ||
4083 | print "</span>"; | |
4084 | ||
4085 | print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\" | |
4086 | href=\"javascript:toggleUnread($id)\"> | |
4087 | Unread</a></span>"; | |
4088 | ||
4089 | print "</div>"; | |
4090 | print "</div>"; | |
4091 | ||
4092 | } | |
4093 | ||
4094 | ++$lnum; | |
4095 | } | |
4096 | ||
4097 | if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) { | |
4098 | print "</table>"; | |
4099 | } | |
4100 | ||
4101 | // print_headline_subtoolbar($link, | |
4102 | // "javascript:catchupPage()", "Mark page as read", true, $rtl_content); | |
4103 | ||
4104 | ||
4105 | } else { | |
4106 | if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>"; | |
4107 | } | |
4108 | ||
4109 | if (!$offset) { | |
4110 | print "</div>"; | |
4111 | print "</div>"; | |
4112 | } | |
4113 | ||
4114 | return $topmost_article_ids; | |
4115 | } | |
4116 | ||
4117 | // from here: http://www.roscripts.com/Create_tag_cloud-71.html | |
4118 | ||
4119 | function printTagCloud($link) { | |
4120 | ||
4121 | /* get first ref_id to count from */ | |
4122 | ||
4123 | /* | |
4124 | ||
4125 | $query = ""; | |
4126 | ||
4127 | if (DB_TYPE == "pgsql") { | |
4128 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4129 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4130 | AND date_entered > NOW() - INTERVAL '30 days'"; | |
4131 | } else { | |
4132 | $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries | |
4133 | WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]." | |
4134 | AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)"; | |
4135 | } | |
4136 | ||
4137 | $result = db_query($link, $query); | |
4138 | $first_id = db_fetch_result($result, 0, "id"); */ | |
4139 | ||
4140 | //AND post_int_id >= '$first_id' | |
4141 | $query = "SELECT tag_name, COUNT(post_int_id) AS count | |
4142 | FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]." | |
4143 | GROUP BY tag_name ORDER BY count DESC LIMIT 50"; | |
4144 | ||
4145 | $result = db_query($link, $query); | |
4146 | ||
4147 | $tags = array(); | |
4148 | ||
4149 | while ($line = db_fetch_assoc($result)) { | |
4150 | $tags[$line["tag_name"]] = $line["count"]; | |
4151 | } | |
4152 | ||
4153 | ksort($tags); | |
4154 | ||
4155 | $max_size = 32; // max font size in pixels | |
4156 | $min_size = 11; // min font size in pixels | |
4157 | ||
4158 | // largest and smallest array values | |
4159 | $max_qty = max(array_values($tags)); | |
4160 | $min_qty = min(array_values($tags)); | |
4161 | ||
4162 | // find the range of values | |
4163 | $spread = $max_qty - $min_qty; | |
4164 | if ($spread == 0) { // we don't want to divide by zero | |
4165 | $spread = 1; | |
4166 | } | |
4167 | ||
4168 | // set the font-size increment | |
4169 | $step = ($max_size - $min_size) / ($spread); | |
4170 | ||
4171 | // loop through the tag array | |
4172 | foreach ($tags as $key => $value) { | |
4173 | // calculate font-size | |
4174 | // find the $value in excess of $min_qty | |
4175 | // multiply by the font-size increment ($size) | |
4176 | // and add the $min_size set above | |
4177 | $size = round($min_size + (($value - $min_qty) * $step)); | |
4178 | ||
4179 | $key_escaped = str_replace("'", "\\'", $key); | |
4180 | ||
4181 | echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " . | |
4182 | $size . "px\" title=\"$value articles tagged with " . | |
4183 | $key . '">' . $key . '</a> '; | |
4184 | } | |
4185 | } | |
4186 | ||
4187 | function print_checkpoint($n, $s) { | |
4188 | $ts = getmicrotime(); | |
4189 | echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s); | |
4190 | return $ts; | |
4191 | } | |
4192 | ||
4193 | function sanitize_tag($tag) { | |
4194 | $tag = trim($tag); | |
4195 | ||
4196 | $tag = mb_strtolower($tag, 'utf-8'); | |
4197 | ||
4198 | $tag = preg_replace('/[\"\+\>\<]/', "", $tag); | |
4199 | ||
4200 | // $tag = str_replace('"', "", $tag); | |
4201 | // $tag = str_replace("+", " ", $tag); | |
4202 | $tag = str_replace("technorati tag: ", "", $tag); | |
4203 | ||
4204 | return $tag; | |
4205 | } | |
4206 | ||
4207 | function generate_publish_key() { | |
4208 | return sha1(uniqid(rand(), true)); | |
4209 | } | |
4210 | ||
4211 | ?> |