]> git.wh0rd.org - tt-rss.git/blame - functions.php
mobile: css tweaks
[tt-rss.git] / functions.php
CommitLineData
1d3a17c7 1<?php
f1a80dae 2
894ebcf5 3/* if ($_GET["debug"]) {
cce28758
AD
4 define('DEFAULT_ERROR_LEVEL', E_ALL);
5 } else {
6 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
894ebcf5 7 } */
cce28758 8
40d13c28 9 require_once 'config.php';
cc17c205 10
f8c612d4 11 function get_translations() {
6a214f92 12 $tr = array(
019bd5a9 13 "auto" => "Detect automatically",
6a214f92
AD
14 "en_US" => "English",
15 "fr_FR" => "Français",
16 "ru_RU" => "Русский",
9a063469 17 "pt_BR" => "Portuguese/Brazil",
6a214f92 18 "zh_CN" => "Simplified Chinese");
f8c612d4
AD
19
20 return $tr;
21 }
22
8d039718 23 if (ENABLE_TRANSLATIONS == true) {
865220a4
AD
24 require_once "accept-to-gettext.php";
25 require_once "gettext/gettext.inc";
aba609e0 26
8d039718
AD
27 function startup_gettext() {
28
29 # Get locale from Accept-Language header
6a214f92 30 $lang = al2gt(array_keys(get_translations()), "text/html");
89cb787e
AD
31
32 if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
33 $lang = _TRANSLATION_OVERRIDE_DEFAULT;
34 }
35
672f3f3c 36 if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") {
68659d98
AD
37 $lang = $_COOKIE["ttrss_lang"];
38 }
39
8d039718
AD
40 if ($lang) {
41 _setlocale(LC_MESSAGES, $lang);
42 _bindtextdomain("messages", "locale");
43 _textdomain("messages");
44 _bind_textdomain_codeset("messages", "UTF-8");
45 }
aba609e0 46 }
aba609e0 47
cc17c205 48 startup_gettext();
865220a4
AD
49
50 } else {
51 function __($msg) {
52 return $msg;
53 }
54 function startup_gettext() {
55 // no-op
56 return true;
57 }
cc17c205
AD
58 }
59
b619ff15 60 require_once 'db-prefs.php';
5bc0bd27 61 require_once 'compat.php';
af106b0e 62 require_once 'errors.php';
8911ac8b 63 require_once 'version.php';
40d13c28 64
a8931123
AD
65 require_once 'phpmailer/class.phpmailer.php';
66
49f9c923 67 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38
AD
68 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
69
9fdf7824
AD
70 if (ENABLE_SIMPLEPIE) {
71 require_once "simplepie/simplepie.inc";
72 } else {
73 require_once "magpierss/rss_fetch.inc";
74 require_once 'magpierss/rss_utils.inc';
75 }
49f9c923 76
6f9e33e4
AD
77 function _debug($msg) {
78 $ts = strftime("%H:%M:%S", time());
79 print "[$ts] $msg\n";
80 }
81
ad507f85
AD
82 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
83
07d0efe9
AD
84 if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id);
85
ad507f85 86 $rows = -1;
4c193675 87
07d0efe9
AD
88 $result = db_query($link,
89 "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
90
91 $owner_uid = false;
92
93 if (db_num_rows($result) == 1) {
94 $owner_uid = db_fetch_result($result, 0, "owner_uid");
95 }
96
97 if (!$owner_uid) return;
98
99 $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES",
100 $owner_uid, false);
101
102 if (!$purge_unread) $query_limit = " unread = false AND ";
103
fefa6ca3 104 if (DB_TYPE == "pgsql") {
44e241cb 105/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 106 marked = false AND feed_id = '$feed_id' AND
35d8cf43 107 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
108 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
109
6e7f8d26
AD
110 $pg_version = get_pgsql_version($link);
111
112 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
113
114 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
115 ttrss_entries.id = ref_id AND
116 marked = false AND
117 feed_id = '$feed_id' AND
07d0efe9 118 $query_limit
1e59ae35
AD
119 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
120
121 } else {
122
123 $result = db_query($link, "DELETE FROM ttrss_user_entries
124 USING ttrss_entries
125 WHERE ttrss_entries.id = ref_id AND
126 marked = false AND
127 feed_id = '$feed_id' AND
07d0efe9 128 $query_limit
fc774155 129 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 130 }
ad507f85
AD
131
132 $rows = pg_affected_rows($result);
133
fefa6ca3 134 } else {
1e59ae35 135
30f1746f 136/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 137 marked = false AND feed_id = '$feed_id' AND
35d8cf43 138 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
139 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
140
141 $result = db_query($link, "DELETE FROM ttrss_user_entries
142 USING ttrss_user_entries, ttrss_entries
143 WHERE ttrss_entries.id = ref_id AND
144 marked = false AND
145 feed_id = '$feed_id' AND
07d0efe9 146 $query_limit
30f1746f
AD
147 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
148
ad507f85
AD
149 $rows = mysql_affected_rows($link);
150
151 }
152
153 if ($debug) {
6f9e33e4 154 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3
AD
155 }
156 }
157
44e241cb
AD
158 function global_purge_old_posts($link, $do_output = false, $limit = false) {
159
894ebcf5 160 $random_qpart = sql_random_function();
fefa6ca3 161
44e241cb
AD
162 if ($limit) {
163 $limit_qpart = "LIMIT $limit";
164 } else {
165 $limit_qpart = "";
166 }
167
fefa6ca3 168 $result = db_query($link,
44e241cb
AD
169 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
170 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
171
172 while ($line = db_fetch_assoc($result)) {
173
174 $feed_id = $line["id"];
175 $purge_interval = $line["purge_interval"];
176 $owner_uid = $line["owner_uid"];
177
178 if ($purge_interval == 0) {
179
180 $tmp_result = db_query($link,
181 "SELECT value FROM ttrss_user_prefs WHERE
182 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
183
184 if (db_num_rows($tmp_result) != 0) {
185 $purge_interval = db_fetch_result($tmp_result, 0, "value");
186 }
187 }
188
189 if ($do_output) {
ad507f85 190// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
191 }
192
193 if ($purge_interval > 0) {
ad507f85 194 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
195 }
196 }
197
71604ca4 198 // purge orphaned posts in main content table
dab52d7b 199 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
71604ca4
AD
200 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
201
dab52d7b
AD
202 if ($do_output) {
203 $rows = db_affected_rows($link, $result);
204 _debug("Purged $rows orphaned posts.");
205 }
206
fefa6ca3
AD
207 }
208
07d0efe9
AD
209 function feed_purge_interval($link, $feed_id) {
210
211 $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds
212 WHERE id = '$feed_id'");
213
214 if (db_num_rows($result) == 1) {
215 $purge_interval = db_fetch_result($result, 0, "purge_interval");
216 $owner_uid = db_fetch_result($result, 0, "owner_uid");
217
218 if ($purge_interval == 0) $purge_interval = get_pref($link,
219 'PURGE_OLD_DAYS', $user_id);
220
221 return $purge_interval;
222
223 } else {
224 return -1;
225 }
226 }
227
b6eefba5 228 function purge_old_posts($link) {
5d73494a 229
f1a80dae
AD
230 $user_id = $_SESSION["uid"];
231
232 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
233 WHERE owner_uid = '$user_id'");
5d73494a
AD
234
235 while ($line = db_fetch_assoc($result)) {
236
237 $feed_id = $line["id"];
238 $purge_interval = $line["purge_interval"];
239
b619ff15 240 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 241
140aae81 242 if ($purge_interval > 0) {
fefa6ca3 243 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
244 }
245 }
71604ca4
AD
246
247 // purge orphaned posts in main content table
248 db_query($link, "DELETE FROM ttrss_entries WHERE
249 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
250 }
251
c7d57b66
AD
252 function get_feed_update_interval($link, $feed_id) {
253 $result = db_query($link, "SELECT owner_uid, update_interval FROM
254 ttrss_feeds WHERE id = '$feed_id'");
255
256 if (db_num_rows($result) == 1) {
257 $update_interval = db_fetch_result($result, 0, "update_interval");
258 $owner_uid = db_fetch_result($result, 0, "owner_uid");
259
260 if ($update_interval != 0) {
261 return $update_interval;
262 } else {
263 return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
264 }
265
266 } else {
267 return -1;
268 }
269 }
270
1f2b01ed 271 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 272
4769ddaf 273 if (WEB_DEMO_MODE) return;
b0b4abcf 274
a2770077
AD
275 if (!$user_id) {
276 $user_id = $_SESSION["uid"];
277 purge_old_posts($link);
278 }
279
25af8dad 280// db_query($link, "BEGIN");
b82af8c3 281
cbd8650d
AD
282 if (MAX_UPDATE_TIME > 0) {
283 if (DB_TYPE == "mysql") {
284 $q_order = "RAND()";
285 } else {
286 $q_order = "RANDOM()";
287 }
288 } else {
289 $q_order = "last_updated DESC";
290 }
291
d148926e 292 $result = db_query($link, "SELECT feed_url,id,
798f722b 293 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 294 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
295 ORDER BY $q_order");
296
297 $upd_start = time();
40d13c28 298
b6eefba5 299 while ($line = db_fetch_assoc($result)) {
d148926e
AD
300 $upd_intl = $line["update_interval"];
301
b619ff15 302 if (!$upd_intl || $upd_intl == 0) {
e289ca71 303 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
b619ff15 304 }
d148926e 305
c1e202b7
AD
306 if ($upd_intl < 0) {
307 // Updates for this feed are disabled
308 continue;
309 }
310
93d40f50
AD
311 if ($fetch || (!$line["last_updated"] ||
312 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 313
cbd8650d
AD
314// print "<!-- feed: ".$line["feed_url"]." -->";
315
1f2b01ed 316 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
317
318 $upd_elapsed = time() - $upd_start;
319
320 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
321 return;
322 }
d148926e 323 }
40d13c28
AD
324 }
325
25af8dad 326// db_query($link, "COMMIT");
b82af8c3 327
40d13c28
AD
328 }
329
4065b60b
AD
330 function fetch_file_contents($url) {
331 if (USE_CURL_FOR_ICONS) {
332 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
333
334 $ch = curl_init($url);
335 $fp = fopen($tmpfile, "w");
336
337 if ($fp) {
338 curl_setopt($ch, CURLOPT_FILE, $fp);
dd966fed
AD
339 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
340 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
4065b60b
AD
341 curl_exec($ch);
342 curl_close($ch);
343 fclose($fp);
344 }
345
346 $contents = file_get_contents($tmpfile);
347 unlink($tmpfile);
348
349 return $contents;
350
351 } else {
352 return file_get_contents($url);
353 }
354
355 }
78800912 356
4065b60b
AD
357 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
358 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
78800912 359
4065b60b 360 function get_favicon_url($url) {
99331724 361
4065b60b 362 if ($html = @fetch_file_contents($url)) {
78800912 363
4065b60b
AD
364 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
365 // Attempt to grab a favicon link from their webpage url
366 $linkUrl = html_entity_decode($matches[1]);
c798704b 367
4065b60b
AD
368 if (substr($linkUrl, 0, 1) == '/') {
369 $urlParts = parse_url($url);
370 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
371 } else if (substr($linkUrl, 0, 7) == 'http://') {
372 $faviconURL = $linkUrl;
373 } else if (substr($url, -1, 1) == '/') {
374 $faviconURL = $url.$linkUrl;
375 } else {
376 $faviconURL = $url.'/'.$linkUrl;
e695fdc8 377 }
717f5e64 378
c798704b 379 } else {
4065b60b
AD
380 // If unsuccessful, attempt to "guess" the favicon location
381 $urlParts = parse_url($url);
382 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
383 }
384 }
c798704b 385
4065b60b
AD
386 // Run a test to see if what we have attempted to get actually exists.
387 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
388 return $faviconURL;
389 } else {
390 return false;
391 }
392 }
393
394 function url_validate($link) {
395
396 $url_parts = @parse_url($link);
397
398 if ( empty( $url_parts["host"] ) )
399 return false;
400
401 if ( !empty( $url_parts["path"] ) ) {
402 $documentpath = $url_parts["path"];
403 } else {
404 $documentpath = "/";
405 }
406
407 if ( !empty( $url_parts["query"] ) )
408 $documentpath .= "?" . $url_parts["query"];
409
410 $host = $url_parts["host"];
411 $port = $url_parts["port"];
412
413 if ( empty($port) )
414 $port = "80";
415
416 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
417
418 if ( !$socket )
419 return false;
c798704b 420
4065b60b
AD
421 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
422
423 $http_response = fgets( $socket, 22 );
424
425 $responses = "/(200 OK)|(30[0-9] Moved)/";
426 if ( preg_match($responses, $http_response) ) {
427 fclose($socket);
428 return true;
429 } else {
430 return false;
431 }
432
433 }
434
435 function check_feed_favicon($site_url, $feed, $link) {
436 $favicon_url = get_favicon_url($site_url);
437
438# print "FAVICON [$site_url]: $favicon_url\n";
439
440 error_reporting(0);
441
442 $icon_file = ICONS_DIR . "/$feed.ico";
443
444 if ($favicon_url && !file_exists($icon_file)) {
445 $contents = fetch_file_contents($favicon_url);
446
447 $fp = fopen($icon_file, "w");
78800912 448
4065b60b
AD
449 if ($fp) {
450 fwrite($fp, $contents);
451 fclose($fp);
452 chmod($icon_file, 0644);
453 }
78800912 454 }
4065b60b
AD
455
456 error_reporting(DEFAULT_ERROR_LEVEL);
457
78800912
AD
458 }
459
ddb68b81 460 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 461
17c0eeba 462 if (!$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
463 return;
464 }
465
4bc64807 466 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 467 _debug("update_rss_feed: start");
219bd8fc
AD
468 }
469
5370d37f 470 $result = db_query($link, "SELECT id,update_interval,auth_login,auth_pass,cache_images
a88c1f36
AD
471 FROM ttrss_feeds WHERE id = '$feed'");
472
5370d37f
AD
473 if (db_num_rows($result) == 0) {
474 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
475 _debug("update_rss_feed: feed $feed [$feed_url] NOT FOUND");
476 }
477 return;
478 }
479
464bd61e
AD
480 $auth_login = db_fetch_result($result, 0, "auth_login");
481 $auth_pass = db_fetch_result($result, 0, "auth_pass");
482
483 if (!ENABLE_SIMPLEPIE) {
484 $auth_login = urlencode($auth_login);
485 $auth_pass = urlencode($auth_pass);
486 }
47c6c988 487
a88c1f36 488 $update_interval = db_fetch_result($result, 0, "update_interval");
bc0f0785 489 $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
a88c1f36
AD
490
491 if ($update_interval < 0) { return; }
492
ab3d0b99
AD
493 $feed = db_escape_string($feed);
494
47c6c988
AD
495 $fetch_url = $feed_url;
496
497 if ($auth_login && $auth_pass) {
498 $url_parts = array();
499 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
500
501 if ($url_parts[1] && $url_parts[2]) {
502 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
503 }
504
505 }
ab3d0b99 506
4bc64807 507 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
ca872b9d 508 _debug("update_rss_feed: fetching [$fetch_url]...");
219bd8fc
AD
509 }
510
9fdf7824 511 if (!defined('DAEMON_EXTENDED_DEBUG') && !$_GET['xdebug']) {
219bd8fc
AD
512 error_reporting(0);
513 }
514
9fdf7824
AD
515 if (!ENABLE_SIMPLEPIE) {
516 $rss = fetch_rss($fetch_url);
517 } else {
c7d57b66
AD
518 if (!is_dir(SIMPLEPIE_CACHE_DIR)) {
519 mkdir(SIMPLEPIE_CACHE_DIR);
520 }
521
ca872b9d
AD
522 $rss = new SimplePie();
523 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
4148e809 524// $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
ca872b9d
AD
525 $rss->set_feed_url($fetch_url);
526 $rss->set_output_encoding('UTF-8');
c7d57b66 527
bc0f0785
AD
528 if (SIMPLEPIE_CACHE_IMAGES && $cache_images) {
529 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
530 _debug("enabling image cache");
531 }
532
dab52d7b 533 $rss->set_image_handler('./image.php', 'i');
bc0f0785 534 }
dab52d7b 535
c7d57b66
AD
536 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
537 _debug("feed update interval (sec): " .
538 get_feed_update_interval($link, $feed)*60);
539 }
540
541 if (is_dir(SIMPLEPIE_CACHE_DIR)) {
542 $rss->set_cache_location(SIMPLEPIE_CACHE_DIR);
543 $rss->set_cache_duration(get_feed_update_interval($link, $feed) * 60);
544 }
545
9fdf7824 546 $rss->init();
9fdf7824
AD
547 }
548
549// print_r($rss);
219bd8fc 550
4bc64807 551 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 552 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
553 } else {
554 error_reporting (DEFAULT_ERROR_LEVEL);
555 }
556
b6eefba5 557 $feed = db_escape_string($feed);
dcee8f61 558
ca872b9d
AD
559 if (ENABLE_SIMPLEPIE) {
560 $fetch_ok = !$rss->error();
561 } else {
562 $fetch_ok = !!$rss;
563 }
564
565 if ($fetch_ok) {
50b62214 566
4bc64807 567 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
568 _debug("update_rss_feed: processing feed data...");
569 }
570
44e241cb 571// db_query($link, "BEGIN");
dd8c76a9 572
a88c1f36 573 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 574 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 575
b6eefba5
AD
576 $registered_title = db_fetch_result($result, 0, "title");
577 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 578 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 579
7fed1940
AD
580 $owner_uid = db_fetch_result($result, 0, "owner_uid");
581
9fdf7824
AD
582 if (ENABLE_SIMPLEPIE) {
583 $site_url = $rss->get_link();
584 } else {
585 $site_url = $rss->channel["link"];
586 }
587
8d0ec6fd 588 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
4bc64807 589 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
590 _debug("update_rss_feed: checking favicon...");
591 }
9fdf7824
AD
592
593 check_feed_favicon($site_url, $feed, $link);
a2770077
AD
594 }
595
746b249f 596 if (!$registered_title || $registered_title == "[Unknown]") {
4bc64807 597
fb486a33 598 if (ENABLE_SIMPLEPIE) {
c2f8aac4 599 $feed_title = db_escape_string($rss->get_title());
fb486a33
AD
600 } else {
601 $feed_title = db_escape_string($rss->channel["title"]);
602 }
4bc64807
AD
603
604 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
605 _debug("update_rss_feed: registering title: $feed_title");
606 }
7c5a308d 607
f324892e
AD
608 db_query($link, "UPDATE ttrss_feeds SET
609 title = '$feed_title' WHERE id = '$feed'");
610 }
611
49f9c923 612 // weird, weird Magpie
9fdf7824
AD
613 if (!ENABLE_SIMPLEPIE) {
614 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
615 }
147f7691
AD
616
617 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
618 db_query($link, "UPDATE ttrss_feeds SET
619 site_url = '$site_url' WHERE id = '$feed'");
331900c6 620 }
40d13c28 621
b7f4bda2
AD
622// print "I: " . $rss->channel["image"]["url"];
623
9fdf7824
AD
624 if (!ENABLE_SIMPLEPIE) {
625 $icon_url = $rss->image["url"];
626 } else {
627 $icon_url = $rss->get_image_url();
628 }
b7f4bda2 629
147f7691 630 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
631 $icon_url = db_escape_string($icon_url);
632 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
633 }
634
51e456d6 635 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
636 _debug("update_rss_feed: loading filters...");
637 }
e6155a06
AD
638
639 $filters = array();
640
4b3dff6e 641 $result = db_query($link, "SELECT reg_exp,
db42b934 642 ttrss_filter_types.name AS name,
073ca0e6 643 ttrss_filter_actions.name AS action,
c2d9322b 644 inverse,
073ca0e6 645 action_param
db42b934 646 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
e8b79d16 647 enabled = true AND
db42b934
AD
648 owner_uid = $owner_uid AND
649 ttrss_filter_types.id = filter_type AND
650 ttrss_filter_actions.id = action_id AND
f8382011 651 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
e6155a06 652
b6eefba5 653 while ($line = db_fetch_assoc($result)) {
e6155a06 654 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
655
656 $filter["reg_exp"] = $line["reg_exp"];
657 $filter["action"] = $line["action"];
073ca0e6 658 $filter["action_param"] = $line["action_param"];
c2d9322b 659 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
073ca0e6 660
19c9cb11 661 array_push($filters[$line["name"]], $filter);
e6155a06
AD
662 }
663
9fdf7824
AD
664 if (ENABLE_SIMPLEPIE) {
665 $iterator = $rss->get_items();
666 } else {
667 $iterator = $rss->items;
668 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
669 if (!$iterator || !is_array($iterator)) $iterator = $rss;
670 }
c22789da
AD
671
672 if (!is_array($iterator)) {
39541e74 673 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 674 SET last_error = 'Parse error: can\'t find any articles.'
77f0a2a7
AD
675 WHERE id = '$feed'"); */
676
677 // clear any errors and mark feed as updated if fetched okay
678 // even if it's blank
679
51e456d6 680 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
844012bc
AD
681 _debug("update_rss_feed: entry iterator is not an array, no articles?");
682 }
683
77f0a2a7
AD
684 db_query($link, "UPDATE ttrss_feeds
685 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
686
687 return; // no articles
c22789da 688 }
ddb68b81 689
51e456d6 690 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
50b62214
AD
691 _debug("update_rss_feed: processing articles...");
692 }
693
ddb68b81 694 foreach ($iterator as $item) {
7c5a308d 695
40ce98f4
AD
696 if ($_GET['xdebug']) {
697 print_r($item);
ea322415 698
40ce98f4 699 }
71bd29f6 700
9fdf7824
AD
701 if (ENABLE_SIMPLEPIE) {
702 $entry_guid = $item->get_id();
703 if (!$entry_guid) $entry_guid = $item->get_link();
704 if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
705
706 } else {
707
708 $entry_guid = $item["id"];
34e420fb 709
9fdf7824
AD
710 if (!$entry_guid) $entry_guid = $item["guid"];
711 if (!$entry_guid) $entry_guid = $item["link"];
712 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
713 }
be832a1a 714
9fdf7824 715 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
716 _debug("update_rss_feed: guid $entry_guid");
717 }
718
be832a1a
AD
719 if (!$entry_guid) continue;
720
721 $entry_timestamp = "";
722
9fdf7824
AD
723 if (ENABLE_SIMPLEPIE) {
724 $entry_timestamp = strtotime($item->get_date());
725 } else {
726 $rss_2_date = $item['pubdate'];
727 $rss_1_date = $item['dc']['date'];
728 $atom_date = $item['issued'];
729 if (!$atom_date) $atom_date = $item['updated'];
be832a1a 730
9fdf7824
AD
731 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
732 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
733 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
734 }
9bb36aa0
AD
735
736 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
737 _debug("update_rss_feed: date $entry_timestamp");
738 }
739
2e930846 740 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
741 $entry_timestamp = time();
742 $no_orig_date = 'true';
743 } else {
744 $no_orig_date = 'false';
745 }
746
747 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
748
9fdf7824
AD
749 if (ENABLE_SIMPLEPIE) {
750 $entry_title = $item->get_title();
751 } else {
752 $entry_title = trim(strip_tags($item["title"]));
753 }
be832a1a 754
9fdf7824
AD
755 if (ENABLE_SIMPLEPIE) {
756 $entry_link = $item->get_link();
757 } else {
758 // strange Magpie workaround
759 $entry_link = $item["link_"];
760 if (!$entry_link) $entry_link = $item["link"];
761 }
be832a1a 762
9bb36aa0
AD
763 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
764 _debug("update_rss_feed: title $entry_title");
765 }
766
767 if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
7d3ab0dd 768
be832a1a 769 $entry_link = strip_tags($entry_link);
7d3ab0dd 770
9fdf7824
AD
771 if (ENABLE_SIMPLEPIE) {
772 $entry_content = $item->get_description();
773 } else {
774 $entry_content = $item["content:escaped"];
775
776 if (!$entry_content) $entry_content = $item["content:encoded"];
777 if (!$entry_content) $entry_content = $item["content"];
ea322415
AD
778
779 // Magpie bugs are getting ridiculous
780 if (trim($entry_content) == "Array") $entry_content = false;
781
9fdf7824
AD
782 if (!$entry_content) $entry_content = $item["atom_content"];
783 if (!$entry_content) $entry_content = $item["summary"];
784 if (!$entry_content) $entry_content = $item["description"];
785
786 // WTF
787 if (is_array($entry_content)) {
788 $entry_content = $entry_content["encoded"];
789 if (!$entry_content) $entry_content = $entry_content["escaped"];
ea322415 790 }
be832a1a
AD
791 }
792
ea322415
AD
793 if ($_GET["xdebug"]) {
794 print "update_rss_feed: content: ";
795 print_r(htmlspecialchars($entry_content));
796 }
be832a1a
AD
797
798 $entry_content_unescaped = $entry_content;
be832a1a 799
9fdf7824
AD
800 if (ENABLE_SIMPLEPIE) {
801 $entry_comments = strip_tags($item->data["comments"]);
30cf38dd 802 if ($item->get_author()) {
e1d600f0
AD
803 $entry_author_item = $item->get_author();
804 $entry_author = $entry_author_item->get_name();
30cf38dd 805 }
9fdf7824
AD
806 } else {
807 $entry_comments = strip_tags($item["comments"]);
808
809 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
4d6e9157 810
9fdf7824
AD
811 if ($item['author']) {
812
813 if (is_array($item['author'])) {
814
815 if (!$entry_author) {
816 $entry_author = db_escape_string(strip_tags($item['author']['name']));
817 }
818
819 if (!$entry_author) {
820 $entry_author = db_escape_string(strip_tags($item['author']['email']));
821 }
4d6e9157 822 }
9fdf7824 823
4d6e9157 824 if (!$entry_author) {
9fdf7824 825 $entry_author = db_escape_string(strip_tags($item['author']));
4d6e9157 826 }
83f114c8 827 }
be832a1a
AD
828 }
829
83f114c8
AD
830 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
831
be832a1a
AD
832 $entry_guid = db_escape_string(strip_tags($entry_guid));
833
834 $result = db_query($link, "SELECT id FROM ttrss_entries
835 WHERE guid = '$entry_guid'");
836
837 $entry_content = db_escape_string($entry_content);
7e43ad58
AD
838
839 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
840
be832a1a
AD
841 $entry_title = db_escape_string($entry_title);
842 $entry_link = db_escape_string($entry_link);
2544f36b
AD
843 $entry_comments = mb_substr(db_escape_string($entry_comments), 0, 250);
844 $entry_author = mb_substr($entry_author, 0, 250);
be832a1a 845
9fdf7824
AD
846 if (ENABLE_SIMPLEPIE) {
847 $num_comments = 0; #FIXME#
848 } else {
849 $num_comments = db_escape_string($item["slash"]["comments"]);
850 }
be832a1a
AD
851
852 if (!$num_comments) $num_comments = 0;
853
fefef828 854 // parse <category> entries into tags
be832a1a 855
9fdf7824 856 if (ENABLE_SIMPLEPIE) {
be832a1a 857
fefef828 858 $additional_tags = array();
9fdf7824
AD
859 $additional_tags_src = $item->get_categories();
860
a702e931
AD
861 if (is_array($additional_tags_src)) {
862 foreach ($additional_tags_src as $tobj) {
863 array_push($additional_tags, $tobj->get_term());
864 }
fefef828 865 }
fefef828 866
6af621c7
AD
867 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
868 _debug("update_rss_feed: category tags:");
869 print_r($additional_tags);
870 }
871
9fdf7824 872 } else {
fefef828 873
9fdf7824 874 $t_ctr = $item['category#'];
fefef828 875
9fdf7824
AD
876 $additional_tags = false;
877
878 if ($t_ctr == 0) {
879 $additional_tags = false;
71bd29f6 880 } else if ($t_ctr > 0) {
9fdf7824 881 $additional_tags = array($item['category']);
71bd29f6
AD
882
883 if ($item['category@term']) {
884 array_push($additional_tags, $item['category@term']);
885 }
886
9fdf7824
AD
887 for ($i = 0; $i <= $t_ctr; $i++ ) {
888 if ($item["category#$i"]) {
889 array_push($additional_tags, $item["category#$i"]);
890 }
71bd29f6
AD
891
892 if ($item["category#$i@term"]) {
893 array_push($additional_tags, $item["category#$i@term"]);
894 }
9fdf7824
AD
895 }
896 }
897
898 // parse <dc:subject> elements
899
900 $t_ctr = $item['dc']['subject#'];
901
71bd29f6 902 if ($t_ctr > 0) {
9fdf7824 903 $additional_tags = array($item['dc']['subject']);
71bd29f6 904
9fdf7824
AD
905 for ($i = 0; $i <= $t_ctr; $i++ ) {
906 if ($item['dc']["subject#$i"]) {
907 array_push($additional_tags, $item['dc']["subject#$i"]);
908 }
fefef828
AD
909 }
910 }
911 }
8add756a 912
ce53e200
AD
913 // enclosures
914
915 $enclosures = array();
916
917 if (ENABLE_SIMPLEPIE) {
918 $encs = $item->get_enclosures();
919
920 foreach ($encs as $e) {
921 $e_item = array(
922 $e->link, $e->type, $e->length);
923
924 array_push($enclosures, $e_item);
925 }
926
927 } else {
928 $e_ctr = $item['enclosure#'];
929
930 if ($e_ctr > 0) {
931 $e_item = array($item['enclosure@url'],
932 $item['enclosure@type'],
933 $item['enclosure@length']);
934
935 array_push($enclosures, $e_item);
936
71bd29f6
AD
937 for ($i = 0; $i <= $e_ctr; $i++ ) {
938
939 if ($item["enclosure#$i@url"]) {
940 $e_item = array($item["enclosure#$i@url"],
941 $item["enclosure#$i@type"],
942 $item["enclosure#$i@length"]);
943 array_push($enclosures, $e_item);
944 }
ce53e200
AD
945 }
946 }
947
948 }
949
d48d160c 950 # sanitize content
183ad07b 951
007a38d4 952// $entry_content = sanitize_rss($entry_content);
d48d160c 953
9fdf7824 954 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
955 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
956 }
957
44e241cb
AD
958 db_query($link, "BEGIN");
959
4c193675
AD
960 if (db_num_rows($result) == 0) {
961
9fdf7824 962 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
963 _debug("update_rss_feed: base guid not found");
964 }
965
4c193675
AD
966 // base post entry does not exist, create it
967
4c193675
AD
968 $result = db_query($link,
969 "INSERT INTO ttrss_entries
970 (title,
971 guid,
972 link,
973 updated,
974 content,
975 content_hash,
976 no_orig_date,
977 date_entered,
11b0dce2 978 comments,
b6104dee
AD
979 num_comments,
980 author)
4c193675
AD
981 VALUES
982 ('$entry_title',
983 '$entry_guid',
984 '$entry_link',
985 '$entry_timestamp_fmt',
986 '$entry_content',
987 '$content_hash',
988 $no_orig_date,
989 NOW(),
11b0dce2 990 '$entry_comments',
b6104dee
AD
991 '$num_comments',
992 '$entry_author')");
8926aab8
AD
993 } else {
994 // we keep encountering the entry in feeds, so we need to
995 // update date_entered column so that we don't get horrible
996 // dupes when the entry gets purged and reinserted again e.g.
997 // in the case of SLOW SLOW OMG SLOW updating feeds
998
999 $base_entry_id = db_fetch_result($result, 0, "id");
1000
1001 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
1002 WHERE id = '$base_entry_id'");
4c193675
AD
1003 }
1004
1005 // now it should exist, if not - bad luck then
1006
6385315d
AD
1007 $result = db_query($link, "SELECT
1008 id,content_hash,no_orig_date,title,
8926aab8 1009 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
1010 substring(updated,1,19) as updated,
1011 num_comments
6385315d
AD
1012 FROM
1013 ttrss_entries
1014 WHERE guid = '$entry_guid'");
4c193675 1015
ce53e200
AD
1016 $entry_ref_id = 0;
1017 $entry_int_id = 0;
1018
4c193675
AD
1019 if (db_num_rows($result) == 1) {
1020
9fdf7824 1021 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3 1022 _debug("update_rss_feed: base guid found, checking for user record");
34e420fb
AD
1023 }
1024
11b0dce2
AD
1025 // this will be used below in update handler
1026 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
1027 $orig_title = db_fetch_result($result, 0, "title");
1028 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
1029 $orig_date_entered = strtotime(db_fetch_result($result,
1030 0, "date_entered"));
6385315d 1031
11b0dce2 1032 $ref_id = db_fetch_result($result, 0, "id");
ce53e200 1033 $entry_ref_id = $ref_id;
4c193675 1034
11b0dce2 1035 // check for user post link to main table
4c193675 1036
11b0dce2 1037 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 1038 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
1039 $dupcheck_qpart = "AND feed_id = '$feed'";
1040 } else {
1041 $dupcheck_qpart = "";
1042 }
71604ca4 1043
11b0dce2 1044// error_reporting(0);
19c9cb11 1045
f8382011
AD
1046 $article_filters = get_article_filters($filters, $entry_title,
1047 $entry_content, $entry_link);
19c9cb11 1048
9fdf7824 1049 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
1050 _debug("update_rss_feed: article filters: ");
1051 if (count($article_filters) != 0) {
1052 print_r($article_filters);
1053 }
1054 }
1055
f8382011 1056 if (find_article_filter($article_filters, "filter")) {
11b0dce2
AD
1057 continue;
1058 }
19c9cb11 1059
11b0dce2 1060// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 1061
11b0dce2 1062 $result = db_query($link,
ce53e200 1063 "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
11b0dce2
AD
1064 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
1065 $dupcheck_qpart");
7ca91eb3 1066
11b0dce2
AD
1067 // okay it doesn't exist - create user entry
1068 if (db_num_rows($result) == 0) {
1069
9fdf7824 1070 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
7ca91eb3
AD
1071 _debug("update_rss_feed: user record not found, creating...");
1072 }
1073
f8382011 1074 if (!find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
1075 $unread = 'true';
1076 $last_read_qpart = 'NULL';
1077 } else {
1078 $unread = 'false';
1079 $last_read_qpart = 'NOW()';
1080 }
dd7d3187 1081
f8382011 1082 if (find_article_filter($article_filters, 'mark')) {
dd7d3187
AD
1083 $marked = 'true';
1084 } else {
1085 $marked = 'false';
1086 }
a36c0dfe
AD
1087
1088 if (find_article_filter($article_filters, 'publish')) {
1089 $published = 'true';
1090 } else {
1091 $published = 'false';
1092 }
1093
11b0dce2
AD
1094 $result = db_query($link,
1095 "INSERT INTO ttrss_user_entries
a36c0dfe 1096 (ref_id, owner_uid, feed_id, unread, last_read, marked, published)
11b0dce2 1097 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
a36c0dfe 1098 $last_read_qpart, $marked, $published)");
ce53e200
AD
1099
1100 $result = db_query($link,
1101 "SELECT int_id FROM ttrss_user_entries WHERE
1102 ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
1103 feed_id = '$feed' LIMIT 1");
1104
1105 if (db_num_rows($result) == 1) {
1106 $entry_int_id = db_fetch_result($result, 0, "int_id");
1107 }
1108 } else {
1109 $entry_ref_id = db_fetch_result($result, 0, "ref_id");
1110 $entry_int_id = db_fetch_result($result, 0, "int_id");
11b0dce2 1111 }
ce53e200
AD
1112
1113 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1114 _debug("update_rss_feed: RID: $entry_ref_id, IID: $entry_int_id");
1115 }
1116
6385315d
AD
1117 $post_needs_update = false;
1118
8d0ec6fd 1119 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d 1120 ($content_hash != $orig_content_hash)) {
7e43ad58 1121// print "<!-- [$entry_title] $content_hash vs $orig_content_hash -->";
6385315d
AD
1122 $post_needs_update = true;
1123 }
1124
7e43ad58 1125 if (db_escape_string($orig_title) != $entry_title) {
6385315d
AD
1126 $post_needs_update = true;
1127 }
1128
11b0dce2
AD
1129 if ($orig_num_comments != $num_comments) {
1130 $post_needs_update = true;
1131 }
1132
6385315d
AD
1133// this doesn't seem to be very reliable
1134//
1135// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
1136// $post_needs_update = true;
1137// }
1138
1139 // if post needs update, update it and mark all user entries
1c73bc0c 1140 // linking to this post as updated
6385315d
AD
1141 if ($post_needs_update) {
1142
7ca91eb3
AD
1143 if (defined('DAEMON_EXTENDED_DEBUG')) {
1144 _debug("update_rss_feed: post $entry_guid needs update...");
1145 }
1146
6385315d
AD
1147// print "<!-- post $orig_title needs update : $post_needs_update -->";
1148
6385315d 1149 db_query($link, "UPDATE ttrss_entries
11b0dce2 1150 SET title = '$entry_title', content = '$entry_content',
7e43ad58 1151 content_hash = '$content_hash',
11b0dce2 1152 num_comments = '$num_comments'
6385315d
AD
1153 WHERE id = '$ref_id'");
1154
8d0ec6fd 1155 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
1156 db_query($link, "UPDATE ttrss_user_entries
1157 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
1158 } else {
1159 db_query($link, "UPDATE ttrss_user_entries
1160 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
1161 }
6385315d
AD
1162
1163 }
4c193675
AD
1164 }
1165
44e241cb
AD
1166 db_query($link, "COMMIT");
1167
ce53e200
AD
1168 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1169 _debug("update_rss_feed: looking for enclosures...");
1170 }
1171
1172 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1173 print_r($enclosures);
1174 }
1175
1176 db_query($link, "BEGIN");
1177
1178 foreach ($enclosures as $enc) {
1179 $enc_url = db_escape_string($enc[0]);
1180 $enc_type = db_escape_string($enc[1]);
1181 $enc_dur = db_escape_string($enc[2]);
1182
1183 $result = db_query($link, "SELECT id FROM ttrss_enclosures
1184 WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
1185
1186 if (db_num_rows($result) == 0) {
1187 db_query($link, "INSERT INTO ttrss_enclosures
1188 (content_url, content_type, title, duration, post_id) VALUES
1189 ('$enc_url', '$enc_type', '', '$enc_dur', '$entry_ref_id')");
1190 }
1191 }
1192
1193 db_query($link, "COMMIT");
1194
9fdf7824 1195 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb
AD
1196 _debug("update_rss_feed: looking for tags...");
1197 }
1198
eb36b4eb 1199 /* taaaags */
40e1a95b 1200 // <a href="..." rel="tag">Xorg</a>, //
eb36b4eb 1201
05732aa0 1202 $entry_tags = null;
eb36b4eb 1203
40e1a95b 1204 preg_match_all("/<a.*?rel=['\"]tag['\"].*?>([^<]+)<\/a>/i",
ee2c3050
AD
1205 $entry_content_unescaped, $entry_tags);
1206
fefef828
AD
1207/* print "<p><br/>$entry_title : $entry_content_unescaped<br>";
1208 print_r($entry_tags);
1209 print "<br/></p>"; */
eb36b4eb
AD
1210
1211 $entry_tags = $entry_tags[1];
1212
073ca0e6
AD
1213 # check for manual tags
1214
f8382011
AD
1215 $tag_filter = find_article_filter($article_filters, "tag");
1216
1217 if ($tag_filter) {
073ca0e6 1218
f8382011 1219 $manual_tags = trim_array(split(",", $tag_filter[1]));
073ca0e6 1220
f8382011 1221 foreach ($manual_tags as $tag) {
be832a1a
AD
1222 if (tag_is_valid($tag)) {
1223 array_push($entry_tags, $tag);
1224 }
1225 }
1226 }
1227
11c9ea1f
AD
1228 $boring_tags = trim_array(split(",", mb_strtolower(get_pref($link,
1229 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
8fc70781 1230
fefef828
AD
1231 if ($additional_tags && is_array($additional_tags)) {
1232 foreach ($additional_tags as $tag) {
156a785d
AD
1233 if (tag_is_valid($tag) &&
1234 array_search($tag, $boring_tags) === FALSE) {
073ca0e6
AD
1235 array_push($entry_tags, $tag);
1236 }
1237 }
fefef828
AD
1238 }
1239
fcc95e24 1240// print "<p>TAGS: "; print_r($entry_tags); print "</p>";
073ca0e6 1241
9fdf7824
AD
1242 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1243 print_r($entry_tags);
1244 }
1245
eb36b4eb
AD
1246 if (count($entry_tags) > 0) {
1247
44e241cb
AD
1248 db_query($link, "BEGIN");
1249
fe99ab12 1250 foreach ($entry_tags as $tag) {
fefef828 1251
14b6c54b 1252 $tag = sanitize_tag($tag);
fefef828 1253 $tag = db_escape_string($tag);
31483fc1 1254
ef063748
AD
1255 if (!tag_is_valid($tag)) continue;
1256
fe99ab12
AD
1257 $result = db_query($link, "SELECT id FROM ttrss_tags
1258 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
1259 owner_uid = '$owner_uid' LIMIT 1");
1260
1261 // print db_fetch_result($result, 0, "id");
1262
1263 if ($result && db_num_rows($result) == 0) {
1264
fe99ab12
AD
1265 db_query($link, "INSERT INTO ttrss_tags
1266 (owner_uid,tag_name,post_int_id)
1267 VALUES ('$owner_uid','$tag', '$entry_int_id')");
1268 }
1269 }
ce53e200 1270
44e241cb 1271 db_query($link, "COMMIT");
05732aa0 1272 }
9fdf7824
AD
1273
1274 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1275 _debug("update_rss_feed: article processed");
1276 }
4c193675 1277 }
40d13c28 1278
ab3d0b99
AD
1279 db_query($link, "UPDATE ttrss_feeds
1280 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 1281
44e241cb 1282// db_query($link, "COMMIT");
dd8c76a9 1283
ab3d0b99 1284 } else {
ca872b9d 1285
9fdf7824 1286 if (ENABLE_SIMPLEPIE) {
ca872b9d 1287 $error_msg = mb_substr($rss->error(), 0, 250);
9fdf7824 1288 } else {
ca872b9d 1289 $error_msg = mb_substr(magpie_error(), 0, 250);
9fdf7824 1290 }
ca872b9d
AD
1291
1292 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
1293 _debug("update_rss_feed: error fetching feed: $error_msg");
1294 }
1295
1296 $error_msg = db_escape_string($error_msg);
1297
ab3d0b99 1298 db_query($link,
aa5f9f5f
AD
1299 "UPDATE ttrss_feeds SET last_error = '$error_msg',
1300 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
1301 }
1302
ac6ebdb3
AD
1303 if (ENABLE_SIMPLEPIE) {
1304 unset($rss);
1305 }
1306
9fdf7824 1307 if (defined('DAEMON_EXTENDED_DEBUG') || $_GET['xdebug']) {
34e420fb 1308 _debug("update_rss_feed: done");
219bd8fc
AD
1309 }
1310
40d13c28
AD
1311 }
1312
f175937c 1313 function print_select($id, $default, $values, $attributes = "") {
79f3553b 1314 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
1315 foreach ($values as $v) {
1316 if ($v == $default)
1317 $sel = " selected";
1318 else
1319 $sel = "";
1320
1321 print "<option$sel>$v</option>";
1322 }
1323 print "</select>";
1324 }
40d13c28 1325
79f3553b
AD
1326 function print_select_hash($id, $default, $values, $attributes = "") {
1327 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
1328 foreach (array_keys($values) as $v) {
1329 if ($v == $default)
1330 $sel = "selected";
1331 else
1332 $sel = "";
1333
1334 print "<option $sel value=\"$v\">".$values[$v]."</option>";
1335 }
1336
1337 print "</select>";
1338 }
1339
f8382011 1340 function get_article_filters($filters, $title, $content, $link) {
240054f1 1341 $matches = array();
c2d9322b 1342
240054f1
AD
1343 if ($filters["title"]) {
1344 foreach ($filters["title"] as $filter) {
c2d9322b
AD
1345 $reg_exp = $filter["reg_exp"];
1346 $inverse = $filter["inverse"];
1347 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
1348 ($inverse && !preg_match("/$reg_exp/i", $title))) {
1349
240054f1
AD
1350 array_push($matches, array($filter["action"], $filter["action_param"]));
1351 }
1352 }
1353 }
1354
1355 if ($filters["content"]) {
1356 foreach ($filters["content"] as $filter) {
c2d9322b
AD
1357 $reg_exp = $filter["reg_exp"];
1358 $inverse = $filter["inverse"];
1359
1360 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
1361 ($inverse && !preg_match("/$reg_exp/i", $content))) {
1362
240054f1
AD
1363 array_push($matches, array($filter["action"], $filter["action_param"]));
1364 }
1365 }
1366 }
1367
1368 if ($filters["both"]) {
1369 foreach ($filters["both"] as $filter) {
1370 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1371 $inverse = $filter["inverse"];
1372
1373 if ($inverse) {
1374 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
1375 array_push($matches, array($filter["action"], $filter["action_param"]));
1376 }
1377 } else {
1378 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
1379 array_push($matches, array($filter["action"], $filter["action_param"]));
1380 }
240054f1
AD
1381 }
1382 }
1383 }
1384
1385 if ($filters["link"]) {
1386 $reg_exp = $filter["reg_exp"];
1387 foreach ($filters["link"] as $filter) {
1388 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
1389 $inverse = $filter["inverse"];
1390
1391 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
1392 ($inverse && !preg_match("/$reg_exp/i", $link))) {
1393
240054f1
AD
1394 array_push($matches, array($filter["action"], $filter["action_param"]));
1395 }
1396 }
1397 }
1398
1399 return $matches;
1400 }
1401
f8382011
AD
1402 function find_article_filter($filters, $filter_name) {
1403 foreach ($filters as $f) {
1404 if ($f[0] == $filter_name) {
1405 return $f;
1406 };
1407 }
1408 return false;
1409 }
1410
9323147e 1411 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 1412 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
1413
1414 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 1415 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 1416 } else {
023fe037 1417 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
1418 }
1419
9323147e
AD
1420 if ($rtl_content) {
1421 $rtl_tag = "dir=\"rtl\"";
1422 } else {
1423 $rtl_tag = "dir=\"ltr\"";
1424 }
1425
78d5212c
AD
1426 $error_notify_msg = "";
1427
fb1fb4ab
AD
1428 if ($last_error) {
1429 $link_title = "Error: $last_error ($last_updated)";
78d5212c 1430 $error_notify_msg = "(Error)";
ad780e9c 1431 } else if ($last_updated) {
fb1fb4ab
AD
1432 $link_title = "Updated: $last_updated";
1433 }
1434
7210613a 1435 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1436 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
254e0e4b
AD
1437
1438 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 1439 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
1440 print "$feed_icon";
1441 }
1442
9323147e 1443 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
1444
1445 if ($unread != 0) {
1446 $fctr_class = "";
1447 } else {
1448 $fctr_class = "class=\"invisible\"";
1449 }
1450
9323147e 1451 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 1452 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
1453
1454 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1455 print "<div class=\"feedExtInfo\">
1456 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1457 }
1458
254e0e4b
AD
1459 print "</li>";
1460
1461 }
1462
406d9489
AD
1463 function getmicrotime() {
1464 list($usec, $sec) = explode(" ",microtime());
1465 return ((float)$usec + (float)$sec);
1466 }
1467
f541eb78 1468 function print_radio($id, $default, $true_is, $values, $attributes = "") {
77e96719
AD
1469 foreach ($values as $v) {
1470
1471 if ($v == $default)
5da169d9 1472 $sel = "checked";
77e96719 1473 else
5da169d9
AD
1474 $sel = "";
1475
f541eb78 1476 if ($v == $true_is) {
5da169d9
AD
1477 $sel .= " value=\"1\"";
1478 } else {
1479 $sel .= " value=\"0\"";
1480 }
77e96719 1481
69654950
AD
1482 print "<input class=\"noborder\"
1483 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1484
1485 }
1486 }
1487
ff485f1d
AD
1488 function initialize_user_prefs($link, $uid) {
1489
1490 $uid = db_escape_string($uid);
1491
1492 db_query($link, "BEGIN");
1493
1494 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1495
1496 $u_result = db_query($link, "SELECT pref_name
1497 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1498
1499 $active_prefs = array();
1500
1501 while ($line = db_fetch_assoc($u_result)) {
1502 array_push($active_prefs, $line["pref_name"]);
1503 }
1504
1505 while ($line = db_fetch_assoc($result)) {
1506 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1507// print "adding " . $line["pref_name"] . "<br>";
1508
1509 db_query($link, "INSERT INTO ttrss_user_prefs
1510 (owner_uid,pref_name,value) VALUES
1511 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1512
1513 }
1514 }
1515
1516 db_query($link, "COMMIT");
1517
1518 }
956c7629
AD
1519
1520 function lookup_user_id($link, $user) {
1521
1522 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1523 login = '$login'");
1524
1525 if (db_num_rows($result) == 1) {
1526 return db_fetch_result($result, 0, "id");
1527 } else {
1528 return false;
1529 }
1530 }
1531
18664970
AD
1532 function http_authenticate_user($link) {
1533
4bc64807
AD
1534 error_log("http_authenticate_user: ".$_SERVER["PHP_AUTH_USER"]."\n", 3, '/tmp/tt-rss.log');
1535
18664970
AD
1536 if (!$_SERVER["PHP_AUTH_USER"]) {
1537
1538 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1539 header('HTTP/1.0 401 Unauthorized');
1540 exit;
1541
1542 } else {
1543 $auth_result = authenticate_user($link,
1544 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1545
1546 if (!$auth_result) {
1547 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1548 header('HTTP/1.0 401 Unauthorized');
1549 exit;
1550 }
1551 }
1552
1553 return true;
1554 }
1555
461766f3 1556 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1557
131b01b3 1558 if (!SINGLE_USER_MODE) {
c8437f35 1559
1a9f4d3c
AD
1560 $pwd_hash1 = encrypt_password($password);
1561 $pwd_hash2 = encrypt_password($password, $login);
461766f3 1562
66917e70
AD
1563 if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH
1564 && $_SERVER["REMOTE_USER"]) {
1565
1566 $login = db_escape_string($_SERVER["REMOTE_USER"]);
1567
461766f3
AD
1568 $query = "SELECT id,login,access_level
1569 FROM ttrss_users WHERE
66917e70
AD
1570 login = '$login'";
1571
461766f3 1572 } else {
1a9f4d3c 1573 $query = "SELECT id,login,access_level,pwd_hash
461766f3 1574 FROM ttrss_users WHERE
1a9f4d3c
AD
1575 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
1576 pwd_hash = '$pwd_hash2')";
461766f3
AD
1577 }
1578
1579 $result = db_query($link, $query);
131b01b3
AD
1580
1581 if (db_num_rows($result) == 1) {
1582 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1583 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1584 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1585
1586 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1587 $_SESSION["uid"]);
1588
1589 $user_theme = get_user_theme_path($link);
1590
1591 $_SESSION["theme"] = $user_theme;
1592 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1a9f4d3c 1593 $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
131b01b3
AD
1594
1595 initialize_user_prefs($link, $_SESSION["uid"]);
1596
1597 return true;
1598 }
1599
1600 return false;
503eb349 1601
131b01b3 1602 } else {
503eb349 1603
131b01b3
AD
1604 $_SESSION["uid"] = 1;
1605 $_SESSION["name"] = "admin";
f557cd78 1606
0bbba72d
AD
1607 $user_theme = get_user_theme_path($link);
1608
1609 $_SESSION["theme"] = $user_theme;
1610 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1611
1612 initialize_user_prefs($link, $_SESSION["uid"]);
1613
c8437f35
AD
1614 return true;
1615 }
c8437f35
AD
1616 }
1617
e6cb77a0
AD
1618 function make_password($length = 8) {
1619
1620 $password = "";
798f722b
AD
1621 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1622
1623 $i = 0;
e6cb77a0
AD
1624
1625 while ($i < $length) {
1626 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1627
1628 if (!strstr($password, $char)) {
1629 $password .= $char;
1630 $i++;
1631 }
1632 }
1633 return $password;
1634 }
1635
1636 // this is called after user is created to initialize default feeds, labels
1637 // or whatever else
1638
1639 // user preferences are checked on every login, not here
1640
1641 function initialize_user($link, $uid) {
1642
1643 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1644 values ('$uid','unread = true', 'Unread articles')");
1645
1646 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1647 values ('$uid','last_read is null and unread = false', 'Updated articles')");
e603a0fa 1648
e6cb77a0 1649 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1650 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1651 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b 1652
cd2cd415
AD
1653 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1654 values ('$uid', 'Tiny Tiny RSS: Forum',
1655 'http://tt-rss.spb.ru/forum/rss.php')");
3b0feb9b 1656 }
e6cb77a0 1657
b8aa49bc 1658 function logout_user() {
5ccc1cf5
AD
1659 session_destroy();
1660 if (isset($_COOKIE[session_name()])) {
1661 setcookie(session_name(), '', time()-42000, '/');
1662 }
b8aa49bc
AD
1663 }
1664
75836f33 1665 function get_script_urlpath() {
87a79fa4 1666 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1667 }
1668
916f788a 1669 function validate_session($link) {
741edab2
AD
1670 if (SINGLE_USER_MODE) {
1671 return true;
1672 }
1673
a2e9b457 1674 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1675 if ($_SESSION["ip_address"]) {
1676 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
7f0acba7 1677 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
916f788a
AD
1678 return false;
1679 }
1680 }
1681 }
d620cfe7 1682
e6684130
AD
1683 if ($_SESSION["uid"]) {
1684
1685 $result = db_query($link,
1686 "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'");
1687
1688 $pwd_hash = db_fetch_result($result, 0, "pwd_hash");
1689
1690 if ($pwd_hash != $_SESSION["pwd_hash"]) {
1691 return false;
1692 }
1693 }
1694
a885f0ec 1695/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1696
8e849206 1697 //print_r($_SESSION);
d620cfe7
AD
1698
1699 if (time() > $_SESSION["cookie_lifetime"]) {
1700 return false;
1701 }
a885f0ec
AD
1702 } */
1703
916f788a
AD
1704 return true;
1705 }
1706
793185a9 1707 function login_sequence($link, $mobile = false) {
b8aa49bc 1708 if (!SINGLE_USER_MODE) {
75836f33 1709
461766f3
AD
1710 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1711 $swu = db_escape_string($_REQUEST["swu"]);
1712 if ($swu) {
1713 $_SESSION["prefs_cache"] = false;
1714 return authenticate_user($link, $swu, null, true);
1715 }
1716 }
1717
7f0acba7 1718 $login_action = $_POST["login_action"];
a885f0ec 1719
01a87dff 1720 # try to authenticate user if called from login form
7f0acba7 1721 if ($login_action == "do_login") {
01a87dff
AD
1722 $login = $_POST["login"];
1723 $password = $_POST["password"];
d620cfe7 1724 $remember_me = $_POST["remember_me"];
f557cd78 1725
01a87dff
AD
1726 if (authenticate_user($link, $login, $password)) {
1727 $_POST["password"] = "";
d620cfe7 1728
f8c612d4
AD
1729 $_SESSION["language"] = $_POST["language"];
1730
d620cfe7
AD
1731 header("Location: " . $_SERVER["REQUEST_URI"]);
1732 exit;
1733
01a87dff 1734 return;
7f0acba7
AD
1735 } else {
1736 $_SESSION["login_error_msg"] = "Incorrect username or password";
01a87dff
AD
1737 }
1738 }
1739
1df0f48b
AD
1740// print session_id();
1741// print_r($_SESSION);
7f0acba7
AD
1742
1743 if (!$_SESSION["uid"] || !validate_session($link)) {
793185a9 1744 render_login_form($link, $mobile);
01a87dff 1745 exit;
d3687e7a
AD
1746 } else {
1747 /* bump login timestamp */
1748 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1749 $_SESSION["uid"]);
019bd5a9 1750
d54780bc 1751 if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) {
019bd5a9
AD
1752 setcookie("ttrss_lang", $_SESSION["language"],
1753 time() + SESSION_COOKIE_LIFETIME);
1754 }
b8aa49bc 1755 }
d620cfe7 1756
b8aa49bc 1757 } else {
0bbba72d 1758 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1759 }
1760 }
3547842a
AD
1761
1762 function truncate_string($str, $max_len) {
12db369c 1763 if (mb_strlen($str, "utf-8") > $max_len - 3) {
66a251f9 1764 return mb_substr($str, 0, $max_len, "utf-8") . "&hellip;";
3547842a
AD
1765 } else {
1766 return $str;
1767 }
1768 }
54a60e1a
AD
1769
1770 function get_user_theme_path($link) {
798f722b
AD
1771 $result = db_query($link, "SELECT theme_path
1772 FROM
1773 ttrss_themes,ttrss_users
1774 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1775 if (db_num_rows($result) != 0) {
1776 return db_fetch_result($result, 0, "theme_path");
1777 } else {
1778 return null;
1779 }
1780 }
be773442
AD
1781
1782 function smart_date_time($timestamp) {
1783 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1784 return date("G:i", $timestamp);
f26450f1 1785 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1786 return date("M d, G:i", $timestamp);
1787 } else {
7d7e0509 1788 return date("Y/m/d, G:i", $timestamp);
be773442
AD
1789 }
1790 }
1791
1792 function smart_date($timestamp) {
1793 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1794 return "Today";
f26450f1 1795 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1796 return date("D m", $timestamp);
1797 } else {
b02111c2 1798 return date("Y/m/d", $timestamp);
be773442
AD
1799 }
1800 }
a654a595
AD
1801
1802 function sql_bool_to_string($s) {
1803 if ($s == "t" || $s == "1") {
1804 return "true";
1805 } else {
1806 return "false";
1807 }
1808 }
e3c99f3b
AD
1809
1810 function sql_bool_to_bool($s) {
1811 if ($s == "t" || $s == "1") {
1812 return true;
1813 } else {
1814 return false;
1815 }
1816 }
0ea4fb50 1817
e3c99f3b 1818
0ea4fb50
AD
1819 function toggleEvenOdd($a) {
1820 if ($a == "even")
1821 return "odd";
1822 else
1823 return "even";
1824 }
6043fb7e
AD
1825
1826 function sanity_check($link) {
9cbca41f 1827
aec3ce39
AD
1828 error_reporting(0);
1829
6043fb7e
AD
1830 $error_code = 0;
1831 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1832 $schema_version = db_fetch_result($result, 0, "schema_version");
1833
1834 if ($schema_version != SCHEMA_VERSION) {
1835 $error_code = 5;
1836 }
1837
aec3ce39
AD
1838 if (DB_TYPE == "mysql") {
1839 $result = db_query($link, "SELECT true", false);
1840 if (db_num_rows($result) != 1) {
1841 $error_code = 10;
1842 }
1843 }
1844
1845 error_reporting (DEFAULT_ERROR_LEVEL);
1846
6043fb7e 1847 if ($error_code != 0) {
aec3ce39 1848 print_error_xml($error_code);
6043fb7e
AD
1849 return false;
1850 } else {
1851 return true;
4220d6b0 1852 }
6043fb7e
AD
1853 }
1854
27981ca3 1855 function file_is_locked($filename) {
31a6d42d
AD
1856 if (function_exists('flock')) {
1857 error_reporting(0);
1858 $fp = fopen($filename, "r");
1859 error_reporting(DEFAULT_ERROR_LEVEL);
1860 if ($fp) {
1861 if (flock($fp, LOCK_EX | LOCK_NB)) {
1862 flock($fp, LOCK_UN);
1863 fclose($fp);
1864 return false;
1865 }
27981ca3 1866 fclose($fp);
31a6d42d 1867 return true;
27981ca3 1868 }
27981ca3
AD
1869 }
1870 return false;
1871 }
1872
fcb4c0c9
AD
1873 function make_lockfile($filename) {
1874 $fp = fopen($filename, "w");
1875
1876 if (flock($fp, LOCK_EX | LOCK_NB)) {
1877 return $fp;
1878 } else {
1879 return false;
1880 }
1881 }
1882
bf7fcde8
AD
1883 function make_stampfile($filename) {
1884 $fp = fopen($filename, "w");
1885
8e00ae9b 1886 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 1887 fwrite($fp, time() . "\n");
8e00ae9b 1888 flock($fp, LOCK_UN);
bf7fcde8
AD
1889 fclose($fp);
1890 return true;
1891 } else {
1892 return false;
1893 }
1894 }
1895
8e00ae9b
AD
1896 function read_stampfile($filename) {
1897
1898 error_reporting(0);
1899 $fp = fopen($filename, "r");
1900 error_reporting (DEFAULT_ERROR_LEVEL);
1901
1902 if (flock($fp, LOCK_EX)) {
1903 $stamp = fgets($fp);
1904 flock($fp, LOCK_UN);
1905 fclose($fp);
1906 return $stamp;
1907 } else {
1908 return false;
1909 }
1910 }
bf7fcde8 1911
894ebcf5
AD
1912 function sql_random_function() {
1913 if (DB_TYPE == "mysql") {
1914 return "RAND()";
1915 } else {
1916 return "RANDOM()";
1917 }
1918 }
1919
23aa0d16 1920 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1921
1922 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1923
1924 if ($cat_view) {
1925
1926 if ($feed > 0) {
1927 $cat_qpart = "cat_id = '$feed'";
1928 } else {
1929 $cat_qpart = "cat_id IS NULL";
1930 }
1931
1932 $tmp_result = db_query($link, "SELECT id
1933 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1934 $_SESSION["uid"]);
1935
1936 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1937
1938 $tmp_feed = $tmp_line["id"];
1939
1940 db_query($link, "UPDATE ttrss_user_entries
1941 SET unread = false,last_read = NOW()
1942 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1943 }
1944
1945 } else if ($feed > 0) {
1946
1947 $tmp_result = db_query($link, "SELECT id
1948 FROM ttrss_feeds WHERE parent_feed = '$feed'
1949 ORDER BY cat_id,title");
1950
1951 $parent_ids = array();
1952
1953 if (db_num_rows($tmp_result) > 0) {
1954 while ($p = db_fetch_assoc($tmp_result)) {
1955 array_push($parent_ids, "feed_id = " . $p["id"]);
1956 }
1957
1958 $children_qpart = implode(" OR ", $parent_ids);
1959
1960 db_query($link, "UPDATE ttrss_user_entries
1961 SET unread = false,last_read = NOW()
1962 WHERE (feed_id = '$feed' OR $children_qpart)
1963 AND owner_uid = " . $_SESSION["uid"]);
1964
1965 } else {
1966 db_query($link, "UPDATE ttrss_user_entries
1967 SET unread = false,last_read = NOW()
1968 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1969 }
1970
1971 } else if ($feed < 0 && $feed > -10) { // special, like starred
1972
1973 if ($feed == -1) {
1974 db_query($link, "UPDATE ttrss_user_entries
1975 SET unread = false,last_read = NOW()
1976 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1977 }
e4f4b46f
AD
1978
1979 if ($feed == -2) {
1980 db_query($link, "UPDATE ttrss_user_entries
1981 SET unread = false,last_read = NOW()
1982 WHERE published = true AND owner_uid = ".$_SESSION["uid"]);
1983 }
1984
2d24f032
AD
1985 if ($feed == -3) {
1986
c1d7e6c3
AD
1987 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
1988
2d24f032 1989 if (DB_TYPE == "pgsql") {
1f3335dc 1990 $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 1991 } else {
1f3335dc 1992 $match_part = "date_entered > DATE_SUB(NOW(),
c1d7e6c3 1993 INTERVAL $intl HOUR) ";
2d24f032
AD
1994 }
1995
1f3335dc
AD
1996 $result = db_query($link, "SELECT id FROM ttrss_entries,
1997 ttrss_user_entries WHERE $match_part AND
1998 unread = true AND
1999 ttrss_user_entries.ref_id = ttrss_entries.id AND
2000 owner_uid = ".$_SESSION["uid"]);
2001
2002 $affected_ids = array();
2003
2004 while ($line = db_fetch_assoc($result)) {
2005 array_push($affected_ids, $line["id"]);
2006 }
2007
2008 catchupArticlesById($link, $affected_ids, 0);
2d24f032
AD
2009 }
2010
23aa0d16
AD
2011 } else if ($feed < -10) { // label
2012
2013 // TODO make this more efficient
2014
2015 $label_id = -$feed - 11;
2016
2017 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2018 WHERE id = '$label_id'");
2019
2020 if ($tmp_result) {
2021 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
2022
2023 db_query($link, "BEGIN");
2024
2025 $tmp2_result = db_query($link,
2026 "SELECT
2027 int_id
2028 FROM
88040f57 2029 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 2030 WHERE
88040f57
AD
2031 ref_id = ttrss_entries.id AND
2032 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 2033 $sql_exp AND
88040f57 2034 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
2035
2036 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
2037 db_query($link, "UPDATE
2038 ttrss_user_entries
2039 SET
2040 unread = false, last_read = NOW()
2041 WHERE
2042 int_id = " . $tmp_line["int_id"]);
2043 }
2044
2045 db_query($link, "COMMIT");
2046
2047/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
2048 SET unread = false,last_read = NOW()
2049 WHERE $sql_exp
2050 AND ref_id = id
2051 AND owner_uid = ".$_SESSION["uid"]); */
2052 }
2053 }
2054 } else { // tag
2055 db_query($link, "BEGIN");
2056
2057 $tag_name = db_escape_string($feed);
2058
2059 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
2060 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
2061
2062 while ($line = db_fetch_assoc($result)) {
2063 db_query($link, "UPDATE ttrss_user_entries SET
2064 unread = false, last_read = NOW()
2065 WHERE int_id = " . $line["post_int_id"]);
2066 }
2067 db_query($link, "COMMIT");
2068 }
2069 }
2070
35bf080c 2071 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
23aa0d16
AD
2072 if ($cat_view) {
2073
2074 if ($feed > 0) {
2075 $cat_qpart = "cat_id = '$feed'";
2076 } else {
2077 $cat_qpart = "cat_id IS NULL";
2078 }
2079
571dad82 2080 $tmp_result = db_query($link, "SELECT id,feed_url FROM ttrss_feeds
23aa0d16
AD
2081 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2082
2083 while ($tmp_line = db_fetch_assoc($tmp_result)) {
2084 $feed_url = $tmp_line["feed_url"];
571dad82 2085 $feed_id = $tmp_line["id"];
35bf080c 2086 update_rss_feed($link, $feed_url, $feed_id, $force_update);
23aa0d16
AD
2087 }
2088
2089 } else {
2090 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
2091 WHERE id = '$feed'");
2092 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
35bf080c 2093 update_rss_feed($link, $feed_url, $feed, $force_update);
23aa0d16
AD
2094 }
2095 }
a9cb1f83 2096
4ffa126e 2097 function getAllCounters($link, $omode = "flc", $active_feed = false) {
cf4d339c 2098/* getLabelCounters($link);
a9cb1f83
AD
2099 getFeedCounters($link);
2100 getTagCounters($link);
2101 getGlobalCounters($link);
2102 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2103 getCategoryCounters($link);
cf4d339c
AD
2104 } */
2105
2bc2147f 2106 if (!$omode) $omode = "flc";
cf4d339c
AD
2107
2108 getGlobalCounters($link);
2109
2110 if (strchr($omode, "l")) getLabelCounters($link);
4ffa126e 2111 if (strchr($omode, "f")) getFeedCounters($link, SMART_RPC_COUNTERS, $active_feed);
cf4d339c
AD
2112 if (strchr($omode, "t")) getTagCounters($link);
2113 if (strchr($omode, "c")) {
2114 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2115 getCategoryCounters($link);
2116 }
a9cb1f83
AD
2117 }
2118 }
2119
2120 function getCategoryCounters($link) {
bba7c4bf
AD
2121 # two special categories are -1 and -2 (all virtuals; all labels)
2122
2123 $ctr = getCategoryUnread($link, -1);
2124
2125 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>";
2126
2127 $ctr = getCategoryUnread($link, -2);
2128
2129 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2130
14073c0a
AD
2131 $age_qpart = getMaxAgeSubquery();
2132
a9cb1f83 2133 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
14073c0a
AD
2134 FROM ttrss_user_entries, ttrss_entries WHERE feed_id = ttrss_feeds.id
2135 AND id = ref_id AND $age_qpart
a9cb1f83
AD
2136 AND unread = true)) AS unread FROM ttrss_feeds
2137 WHERE
cfb02131 2138 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
2139
2140 while ($line = db_fetch_assoc($result)) {
2141 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
2142 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2143 $line["unread"]."\"/>";
2144 }
2145 }
2146
f295c368
AD
2147 function getCategoryUnread($link, $cat) {
2148
bba7c4bf 2149 if ($cat >= 0) {
18664970 2150
bba7c4bf
AD
2151 if ($cat != 0) {
2152 $cat_query = "cat_id = '$cat'";
2153 } else {
2154 $cat_query = "cat_id IS NULL";
2155 }
14073c0a
AD
2156
2157 $age_qpart = getMaxAgeSubquery();
2158
bba7c4bf
AD
2159 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
2160 AND hidden = false
2161 AND owner_uid = " . $_SESSION["uid"]);
2162
2163 $cat_feeds = array();
2164 while ($line = db_fetch_assoc($result)) {
2165 array_push($cat_feeds, "feed_id = " . $line["id"]);
2166 }
2167
2168 if (count($cat_feeds) == 0) return 0;
2169
2170 $match_part = implode(" OR ", $cat_feeds);
2171
2172 $result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a
AD
2173 FROM ttrss_user_entries,ttrss_entries
2174 WHERE unread = true AND ($match_part) AND id = ref_id
2175 AND $age_qpart AND owner_uid = " . $_SESSION["uid"]);
bba7c4bf
AD
2176
2177 $unread = 0;
2178
2179 # this needs to be rewritten
2180 while ($line = db_fetch_assoc($result)) {
2181 $unread += $line["unread"];
2182 }
2183
2184 return $unread;
2185 } else if ($cat == -1) {
2d24f032 2186 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3);
bba7c4bf 2187 } else if ($cat == -2) {
f295c368 2188
bba7c4bf
AD
2189 $rv = getLabelCounters($link, false, true);
2190 $ctr = 0;
f295c368 2191
bba7c4bf
AD
2192 foreach (array_keys($rv) as $k) {
2193 if ($k < -10) {
2194 $ctr += $rv[$k]["counter"];
2195 }
2196 }
f295c368 2197
bba7c4bf 2198 return $ctr;
f295c368 2199 }
f295c368
AD
2200 }
2201
14073c0a
AD
2202 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
2203 if (DB_TYPE == "pgsql") {
2204 return "ttrss_entries.date_entered >
2205 NOW() - INTERVAL '$days days'";
2206 } else {
2207 return "ttrss_entries.date_entered >
2208 DATE_SUB(NOW(), INTERVAL $days DAY)";
2209 }
2210 }
2211
f295c368 2212 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 2213 $n_feed = sprintf("%d", $feed);
f295c368 2214
14073c0a
AD
2215 $age_qpart = getMaxAgeSubquery();
2216
f295c368 2217 if ($is_cat) {
831ff047 2218 return getCategoryUnread($link, $n_feed);
f295c368 2219 } else if ($n_feed == -1) {
a9cb1f83 2220 $match_part = "marked = true";
e4f4b46f
AD
2221 } else if ($n_feed == -2) {
2222 $match_part = "published = true";
2d24f032
AD
2223 } else if ($n_feed == -3) {
2224 $match_part = "unread = true";
2225
c1d7e6c3
AD
2226 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2227
2d24f032 2228 if (DB_TYPE == "pgsql") {
c1d7e6c3 2229 $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 2230 } else {
c1d7e6c3 2231 $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
2232 }
2233
4919fb42 2234 } else if ($n_feed > 0) {
831ff047 2235
e8b8485f
AD
2236 $result = db_query($link, "SELECT id FROM ttrss_feeds
2237 WHERE parent_feed = '$n_feed'
318260cc 2238 AND hidden = false
4919fb42 2239 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
2240
2241 if (db_num_rows($result) > 0) {
4919fb42 2242
831ff047
AD
2243 $linked_feeds = array();
2244 while ($line = db_fetch_assoc($result)) {
2245 array_push($linked_feeds, "feed_id = " . $line["id"]);
2246 }
e8b8485f
AD
2247
2248 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
2249
2250 $match_part = implode(" OR ", $linked_feeds);
2251
4919fb42 2252 $result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a
AD
2253 FROM ttrss_user_entries,ttrss_entries
2254 WHERE unread = true AND
2255 ttrss_user_entries.ref_id = ttrss_entries.id AND
2256 $age_qpart AND
2257 ($match_part) AND
2258 owner_uid = " . $_SESSION["uid"]);
4919fb42
AD
2259
2260 $unread = 0;
2261
2262 # this needs to be rewritten
2263 while ($line = db_fetch_assoc($result)) {
2264 $unread += $line["unread"];
2265 }
2266
2267 return $unread;
2268
831ff047
AD
2269 } else {
2270 $match_part = "feed_id = '$n_feed'";
2271 }
a9cb1f83 2272 } else if ($feed < -10) {
318260cc 2273
a9cb1f83
AD
2274 $label_id = -$feed - 11;
2275
2276 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
2277 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
2278
2279 $match_part = db_fetch_result($result, 0, "sql_exp");
2280 }
2281
2282 if ($match_part) {
2283
2284 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
2285 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
2286 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2287 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 2288 ttrss_feeds.hidden = false AND
14073c0a 2289 $age_qpart AND
88040f57 2290 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
2291
2292 } else {
2293
2294 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
14073c0a 2295 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
828f22b7 2296 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
14073c0a 2297 AND unread = true AND $age_qpart AND
a9cb1f83
AD
2298 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
2299 }
2300
2301 $unread = db_fetch_result($result, 0, "unread");
cfb02131 2302
a9cb1f83
AD
2303 return $unread;
2304 }
2305
2306 /* FIXME this needs reworking */
2307
f3acc32e
AD
2308 function getGlobalUnread($link, $user_id = false) {
2309
2310 if (!$user_id) {
2311 $user_id = $_SESSION["uid"];
2312 }
2313
14073c0a
AD
2314 $age_qpart = getMaxAgeSubquery();
2315
3831db41 2316 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 2317 WHERE unread = true AND
3831db41 2318 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2319 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 2320 hidden = false AND
14073c0a 2321 $age_qpart AND
f3acc32e 2322 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
2323 $c_id = db_fetch_result($result, 0, "c_id");
2324 return $c_id;
2325 }
2326
2327 function getGlobalCounters($link, $global_unread = -1) {
2328 if ($global_unread == -1) {
2329 $global_unread = getGlobalUnread($link);
2330 }
7bf7e4d3
AD
2331 print "<counter type=\"global\" id='global-unread'
2332 counter='$global_unread'/>";
2333
2334 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2335 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2336
2337 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2338
2339 print "<counter type=\"global\" id='subscribed-feeds'
2340 counter='$subscribed_feeds'/>";
2341
a9cb1f83
AD
2342 }
2343
2344 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
2345
2346 if ($smart_mode) {
2347 if (!$_SESSION["tctr_last_value"]) {
2348 $_SESSION["tctr_last_value"] = array();
2349 }
2350 }
2351
2352 $old_counters = $_SESSION["tctr_last_value"];
2353
2354 $tctrs_modified = false;
2355
2356/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
2357 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
2358 ttrss_user_entries.ref_id = ttrss_entries.id AND
2359 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
2360 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
2361 UNION
2362 select tag_name,0 as count FROM ttrss_tags
2363 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
2364
14073c0a
AD
2365 $age_qpart = getMaxAgeSubquery();
2366
a9cb1f83 2367 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
2368 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2369 AND ref_id = id AND $age_qpart
a9cb1f83 2370 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
2371 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2372 ORDER BY count DESC LIMIT 55");
a9cb1f83
AD
2373
2374 $tags = array();
2375
2376 while ($line = db_fetch_assoc($result)) {
2377 $tags[$line["tag_name"]] += $line["count"];
2378 }
2379
2380 foreach (array_keys($tags) as $tag) {
2381 $unread = $tags[$tag];
2382
2383 $tag = htmlspecialchars($tag);
2384
2385 if (!$smart_mode || $old_counters[$tag] != $unread) {
2386 $old_counters[$tag] = $unread;
2387 $tctrs_modified = true;
2388 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
2389 }
2390
2391 }
2392
2393 if ($smart_mode && $tctrs_modified) {
2394 $_SESSION["tctr_last_value"] = $old_counters;
2395 }
2396
2397 }
2398
ef393de7 2399 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83 2400
14073c0a
AD
2401 $age_qpart = getMaxAgeSubquery();
2402
a9cb1f83
AD
2403 if ($smart_mode) {
2404 if (!$_SESSION["lctr_last_value"]) {
2405 $_SESSION["lctr_last_value"] = array();
2406 }
2407 }
2408
ef393de7
AD
2409 $ret_arr = array();
2410
a9cb1f83
AD
2411 $old_counters = $_SESSION["lctr_last_value"];
2412 $lctrs_modified = false;
2413
2d24f032 2414 $count = getFeedUnread($link, -1);
a9cb1f83 2415
ef393de7
AD
2416 if (!$ret_mode) {
2417 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
2418 } else {
2419 $ret_arr["-1"]["counter"] = $count;
bba7c4bf 2420 $ret_arr["-1"]["description"] = __("Starred articles");
ef393de7 2421 }
a9cb1f83 2422
2d24f032 2423 $count = getFeedUnread($link, -2);
e4f4b46f
AD
2424
2425 if (!$ret_mode) {
2426 print "<counter type=\"label\" id=\"-2\" counter=\"$count\"/>";
2427 } else {
2428 $ret_arr["-2"]["counter"] = $count;
bba7c4bf 2429 $ret_arr["-2"]["description"] = __("Published articles");
e4f4b46f
AD
2430 }
2431
2d24f032
AD
2432 $count = getFeedUnread($link, -3);
2433
2434 if (!$ret_mode) {
2435 print "<counter type=\"label\" id=\"-3\" counter=\"$count\"/>";
2436 } else {
2437 $ret_arr["-3"]["counter"] = $count;
2438 $ret_arr["-3"]["description"] = __("Fresh articles");
2439 }
2440
e4f4b46f 2441
a9cb1f83
AD
2442 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
2443 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
2444
2445 while ($line = db_fetch_assoc($result)) {
2446
2447 $id = -$line["id"] - 11;
2448
ef393de7
AD
2449 $label_name = $line["description"];
2450
a9cb1f83
AD
2451 error_reporting (0);
2452
88040f57 2453 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 2454 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 2455 ttrss_feeds.hidden = false AND
14073c0a 2456 $age_qpart AND
88040f57 2457 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 2458 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 2459 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
2460
2461 $count = db_fetch_result($tmp_result, 0, "count");
2462
2463 if (!$smart_mode || $old_counters[$id] != $count) {
2464 $old_counters[$id] = $count;
2465 $lctrs_modified = true;
ef393de7
AD
2466 if (!$ret_mode) {
2467 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
2468 } else {
2469 $ret_arr[$id]["counter"] = $count;
2470 $ret_arr[$id]["description"] = $label_name;
2471 }
a9cb1f83
AD
2472 }
2473
2474 error_reporting (DEFAULT_ERROR_LEVEL);
2475 }
2476
2477 if ($smart_mode && $lctrs_modified) {
2478 $_SESSION["lctr_last_value"] = $old_counters;
2479 }
ef393de7
AD
2480
2481 return $ret_arr;
a9cb1f83
AD
2482 }
2483
2484/* function getFeedCounter($link, $id) {
2485
2486 $result = db_query($link, "SELECT
2487 count(id) as count,last_error
2488 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
2489 WHERE feed_id = '$id' AND unread = true
2490 AND ttrss_user_entries.feed_id = ttrss_feeds.id
2491 AND ttrss_user_entries.ref_id = ttrss_entries.id");
2492
2493 $count = db_fetch_result($result, 0, "count");
2494 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
2495
2496 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
2497 } */
2498
4ffa126e 2499 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS, $active_feed = false) {
a9cb1f83 2500
14073c0a
AD
2501 $age_qpart = getMaxAgeSubquery();
2502
a9cb1f83
AD
2503 if ($smart_mode) {
2504 if (!$_SESSION["fctr_last_value"]) {
2505 $_SESSION["fctr_last_value"] = array();
2506 }
2507 }
2508
2509 $old_counters = $_SESSION["fctr_last_value"];
2510
1b1b8a7b 2511/* $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 2512 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
2513 (SELECT count(id)
2514 FROM ttrss_entries,ttrss_user_entries
2515 WHERE feed_id = ttrss_feeds.id AND
2516 ttrss_user_entries.ref_id = ttrss_entries.id
2517 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
2518 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1b1b8a7b
AD
2519 AND parent_feed IS NULL"); */
2520
14073c0a
AD
2521 $query = "SELECT ttrss_feeds.id,
2522 ttrss_feeds.title,
1b1b8a7b
AD
2523 SUBSTRING(ttrss_feeds.last_updated,1,19) AS last_updated,
2524 last_error,
bc60fda3 2525 COUNT(ttrss_entries.id) AS count
1b1b8a7b
AD
2526 FROM ttrss_feeds
2527 LEFT JOIN ttrss_user_entries ON (ttrss_user_entries.feed_id = ttrss_feeds.id
2528 AND ttrss_user_entries.owner_uid = ttrss_feeds.owner_uid
2529 AND ttrss_user_entries.unread = true)
14073c0a
AD
2530 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id AND
2531 $age_qpart)
2532 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
1b1b8a7b 2533 AND parent_feed IS NULL
14073c0a 2534 GROUP BY ttrss_feeds.id, ttrss_feeds.title, ttrss_feeds.last_updated, last_error";
a9cb1f83 2535
14073c0a 2536 $result = db_query($link, $query);
a9cb1f83
AD
2537 $fctrs_modified = false;
2538
fb1fb4ab
AD
2539 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2540
a9cb1f83
AD
2541 while ($line = db_fetch_assoc($result)) {
2542
2543 $id = $line["id"];
2544 $count = $line["count"];
2545 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
2546
2547 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2548 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2549 } else {
2550 $last_updated = date($short_date, strtotime($line["last_updated"]));
2551 }
2552
588fb13b
AD
2553 $last_updated = htmlspecialchars($last_updated);
2554
a9cb1f83
AD
2555 $has_img = is_file(ICONS_DIR . "/$id.ico");
2556
2557 $tmp_result = db_query($link,
14073c0a 2558 "SELECT ttrss_feeds.id,COUNT(unread) AS unread
a9cb1f83
AD
2559 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
2560 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
14073c0a
AD
2561 LEFT JOIN ttrss_entries ON (ttrss_user_entries.ref_id = ttrss_entries.id)
2562 WHERE parent_feed = '$id' AND $age_qpart AND unread = true GROUP BY ttrss_feeds.id");
a9cb1f83
AD
2563
2564 if (db_num_rows($tmp_result) > 0) {
2565 while ($l = db_fetch_assoc($tmp_result)) {
2566 $count += $l["unread"];
2567 }
2568 }
2569
2570 if (!$smart_mode || $old_counters[$id] != $count) {
2571 $old_counters[$id] = $count;
2572 $fctrs_modified = true;
2573
2574 if ($last_error) {
2575 $error_part = "error=\"$last_error\"";
2576 } else {
2577 $error_part = "";
2578 }
2579
2580 if ($has_img) {
2581 $has_img_part = "hi=\"$has_img\"";
2582 } else {
2583 $has_img_part = "";
2584 }
2585
4ffa126e
AD
2586 if ($active_feed && $id == $active_feed) {
2587 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2588 } else {
2589 $has_title_part = "";
2590 }
2591
2592 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $has_title_part/>";
a9cb1f83
AD
2593 }
2594 }
2595
2596 if ($smart_mode && $fctrs_modified) {
2597 $_SESSION["fctr_last_value"] = $old_counters;
2598 }
2599 }
2600
1b758780 2601 function get_script_dt_add() {
34e420fb 2602 if (strpos(VERSION, ".99") === false) {
1b758780
AD
2603 return VERSION;
2604 } else {
2605 return time();
2606 }
2607 }
2608
6e7f8d26
AD
2609 function get_pgsql_version($link) {
2610 $result = db_query($link, "SELECT version() AS version");
2611 $version = split(" ", db_fetch_result($result, 0, "version"));
2612 return $version[1];
2613 }
2614
af106b0e
AD
2615 function print_error_xml($code, $add_msg = "") {
2616 global $ERRORS;
2617
2618 $error_msg = $ERRORS[$code];
2619
2620 if ($add_msg) {
2621 $error_msg = "$error_msg; $add_msg";
2622 }
2623
4c2abbc1 2624 print "<rpc-reply>";
af106b0e 2625 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2626 print "</rpc-reply>";
af106b0e 2627 }
956c7629 2628
f27de515
AD
2629 function subscribe_to_feed($link, $feed_link, $cat_id = 0,
2630 $auth_login = '', $auth_pass = '') {
bb0f29a4 2631
b3dfe8ba 2632 # check for feed:http://url
c91c2249
AD
2633 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2634
b3dfe8ba 2635 # check for feed://URL
e2d84cdb 2636 if (strpos($feed_link, "//") === 0) {
b3dfe8ba
AD
2637 $feed_link = "http:$feed_link";
2638 }
2639
c91c2249 2640 if ($feed_link == "") return;
bb0f29a4 2641
956c7629
AD
2642 if ($cat_id == "0" || !$cat_id) {
2643 $cat_qpart = "NULL";
2644 } else {
2645 $cat_qpart = "'$cat_id'";
2646 }
2647
2648 $result = db_query($link,
2649 "SELECT id FROM ttrss_feeds
2650 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2651
2652 if (db_num_rows($result) == 0) {
2653
2654 $result = db_query($link,
f27de515
AD
2655 "INSERT INTO ttrss_feeds
2656 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
956c7629 2657 VALUES ('".$_SESSION["uid"]."', '$feed_link',
f27de515 2658 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
956c7629
AD
2659
2660 $result = db_query($link,
2661 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
f27de515 2662 AND owner_uid = " . $_SESSION["uid"]);
956c7629
AD
2663
2664 $feed_id = db_fetch_result($result, 0, "id");
2665
2666 if ($feed_id) {
2667 update_rss_feed($link, $feed_link, $feed_id, true);
2668 }
2669
2670 return true;
2671 } else {
2672 return false;
2673 }
2674 }
2675
673d54ca
AD
2676 function print_feed_select($link, $id, $default_id = "",
2677 $attributes = "", $include_all_feeds = true) {
2678
79f3553b 2679 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2680 if ($include_all_feeds) {
89cb787e 2681 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca
AD
2682 }
2683
2684 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2685 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2686
2687 if (db_num_rows($result) > 0 && $include_all_feeds) {
2688 print "<option disabled>--------</option>";
2689 }
2690
2691 while ($line = db_fetch_assoc($result)) {
2692 if ($line["id"] == $default_id) {
2693 $is_selected = "selected";
2694 } else {
2695 $is_selected = "";
2696 }
79f3553b 2697 printf("<option $is_selected value='%d'>%s</option>",
47439031 2698 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2699 }
2700
2701 print "</select>";
2702 }
2703
2704 function print_feed_cat_select($link, $id, $default_id = "",
2705 $attributes = "", $include_all_cats = true) {
2706
79f3553b 2707 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
2708
2709 if ($include_all_cats) {
d1db26aa 2710 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2711 }
2712
2713 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2714 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2715
2716 if (db_num_rows($result) > 0 && $include_all_cats) {
2717 print "<option disabled>--------</option>";
2718 }
2719
2720 while ($line = db_fetch_assoc($result)) {
2721 if ($line["id"] == $default_id) {
2722 $is_selected = "selected";
2723 } else {
2724 $is_selected = "";
2725 }
14f69488 2726 printf("<option $is_selected value='%d'>%s</option>",
47439031 2727 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
2728 }
2729
2730 print "</select>";
2731 }
2732
14f69488
AD
2733 function checkbox_to_sql_bool($val) {
2734 return ($val == "on") ? "true" : "false";
2735 }
86b682ce
AD
2736
2737 function getFeedCatTitle($link, $id) {
2738 if ($id == -1) {
d1db26aa 2739 return __("Special");
86b682ce 2740 } else if ($id < -10) {
d1db26aa 2741 return __("Labels");
86b682ce
AD
2742 } else if ($id > 0) {
2743 $result = db_query($link, "SELECT ttrss_feed_categories.title
2744 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2745 cat_id = ttrss_feed_categories.id");
2746 if (db_num_rows($result) == 1) {
2747 return db_fetch_result($result, 0, "title");
2748 } else {
d1db26aa 2749 return __("Uncategorized");
86b682ce
AD
2750 }
2751 } else {
2752 return "getFeedCatTitle($id) failed";
2753 }
2754
2755 }
2756
2757 function getFeedTitle($link, $id) {
2758 if ($id == -1) {
d1db26aa 2759 return __("Starred articles");
945c243e
AD
2760 } else if ($id == -2) {
2761 return __("Published articles");
2d24f032
AD
2762 } else if ($id == -3) {
2763 return __("Fresh articles");
86b682ce
AD
2764 } else if ($id < -10) {
2765 $label_id = -10 - $id;
2766 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2767 if (db_num_rows($result) == 1) {
2768 return db_fetch_result($result, 0, "description");
2769 } else {
2770 return "Unknown label ($label_id)";
2771 }
2772
2773 } else if ($id > 0) {
2774 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2775 if (db_num_rows($result) == 1) {
2776 return db_fetch_result($result, 0, "title");
2777 } else {
2778 return "Unknown feed ($id)";
2779 }
2780 } else {
2781 return "getFeedTitle($id) failed";
2782 }
2783
2784 }
3dd46f19
AD
2785
2786 function get_session_cookie_name() {
2787 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2788 }
3ac2b520
AD
2789
2790 function print_init_params($link) {
2791 print "<init-params>";
2792 if ($_SESSION["stored-params"]) {
2793 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
2794 if ($key) {
2795 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2796 print "<param key=\"$key\" value=\"$value\"/>";
2797 }
3ac2b520
AD
2798 }
2799 }
2800
20361063 2801 print "<param key=\"theme\" value=\"".$_SESSION["theme"]."\"/>";
3ac2b520
AD
2802 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2803 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
17c0eeba 2804 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
3ac2b520
AD
2805
2806 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2807 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2808
e8bd0da9 2809 print "<param key=\"hide_read_feeds\" value=\"" .
465ff90b 2810 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
e8bd0da9 2811
c9268ed5 2812 print "<param key=\"feeds_sort_by_unread\" value=\"" .
465ff90b 2813 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
c9268ed5 2814
f6d6e22f 2815 print "<param key=\"confirm_feed_catchup\" value=\"" .
465ff90b 2816 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
f6d6e22f 2817
ac7bcd71 2818 print "<param key=\"cdm_auto_catchup\" value=\"" .
465ff90b 2819 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
ac7bcd71 2820
8e9c121b
AD
2821 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
2822
be0801a1
AD
2823 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
2824
40496720
AD
2825 print "<param key=\"default_view_mode\" value=\"" .
2826 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
2827
2828 print "<param key=\"default_view_limit\" value=\"" .
465ff90b 2829 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
40496720 2830
fe8d2059
AD
2831 print "<param key=\"prefs_active_tab\" value=\"" .
2832 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
2833
465ff90b
AD
2834 print "<param key=\"infobox_disable_overlay\" value=\"" .
2835 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
2836
527c3bf0
AD
2837 print "<param key=\"icons_location\" value=\"" .
2838 ICONS_URL . "\"/>";
2839
3ac2b520
AD
2840 print "</init-params>";
2841 }
f54f515f
AD
2842
2843 function print_runtime_info($link) {
2844 print "<runtime-info>";
20361063 2845
71ad883b
AD
2846 if (ENABLE_UPDATE_DAEMON) {
2847 print "<param key=\"daemon_is_running\" value=\"".
2848 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
8e00ae9b 2849
9041f58b 2850 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b
AD
2851
2852 $stamp = (int)read_stampfile("update_daemon.stamp");
2853
fbae93d8
AD
2854// print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
2855
8e00ae9b 2856 if ($stamp) {
9041f58b
AD
2857 $stamp_delta = time() - $stamp;
2858
2859 if ($stamp_delta > 1800) {
f6854e44 2860 $stamp_check = 0;
8e00ae9b 2861 } else {
f6854e44
AD
2862 $stamp_check = 1;
2863 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
2864 }
2865
f6854e44
AD
2866 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
2867
8e00ae9b
AD
2868 $stamp_fmt = date("Y.m.d, G:i", $stamp);
2869
2870 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
2871 }
8e00ae9b 2872 }
71ad883b 2873 }
8e00ae9b 2874
d9fa39f1
AD
2875 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2876
c04206e5 2877 if ($_SESSION["last_version_check"] + 7200 < time()) {
d9fa39f1
AD
2878 $new_version_details = check_for_update($link);
2879
2880 print "<param key=\"new_version_available\" value=\"".
2881 sprintf("%d", $new_version_details != ""). "\"/>";
2882
2883 $_SESSION["last_version_check"] = time();
2884 }
2885 }
2886
1c9df66e 2887// print "<param key=\"new_version_available\" value=\"1\"/>";
b4507bc2 2888
f54f515f
AD
2889 print "</runtime-info>";
2890 }
ef393de7 2891
88040f57 2892 function getSearchSql($search, $match_on) {
ef393de7 2893
88040f57 2894 $search_query_part = "";
e20c9d88 2895
88040f57
AD
2896 $keywords = split(" ", $search);
2897 $query_keywords = array();
e20c9d88 2898
88040f57 2899 if ($match_on == "both") {
e20c9d88 2900
88040f57
AD
2901 foreach ($keywords as $k) {
2902 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2903 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2904 }
e20c9d88 2905
88040f57 2906 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2907
88040f57 2908 } else if ($match_on == "title") {
e20c9d88 2909
88040f57
AD
2910 foreach ($keywords as $k) {
2911 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2912 }
e20c9d88 2913
88040f57 2914 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2915
88040f57
AD
2916 } else if ($match_on == "content") {
2917
2918 foreach ($keywords as $k) {
2919 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2920 }
2921 }
2922
2923 $search_query_part = implode("AND", $query_keywords);
2924
2925 return $search_query_part;
2926 }
2927
c36bf4d5
AD
2928 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
2929
2930 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 2931
88040f57
AD
2932 if ($search) {
2933
2934 $search_query_part = getSearchSql($search, $match_on);
2935 $search_query_part .= " AND ";
e20c9d88 2936
ef393de7
AD
2937 } else {
2938 $search_query_part = "";
2939 }
2940
2941 $view_query_part = "";
2942
2943 if ($view_mode == "adaptive") {
2944 if ($search) {
2945 $view_query_part = " ";
2946 } else if ($feed != -1) {
f295c368 2947 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2948 if ($unread > 0) {
2949 $view_query_part = " unread = true AND ";
2950 }
2951 }
2952 }
2953
2954 if ($view_mode == "marked") {
2955 $view_query_part = " marked = true AND ";
2956 }
2957
2958 if ($view_mode == "unread") {
2959 $view_query_part = " unread = true AND ";
2960 }
2961
2962 if ($limit > 0) {
2963 $limit_query_part = "LIMIT " . $limit;
2964 }
2965
2966 $vfeed_query_part = "";
2967
2968 // override query strategy and enable feed display when searching globally
2969 if ($search && $search_mode == "all_feeds") {
2970 $query_strategy_part = "ttrss_entries.id > 0";
2971 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2972 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2973 $query_strategy_part = "ttrss_entries.id > 0";
2974 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2975 id = feed_id) as feed_title,";
2976 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2977
2978 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2979
2980 $tmp_result = false;
2981
2982 if ($cat_view) {
2983 $tmp_result = db_query($link, "SELECT id
2984 FROM ttrss_feeds WHERE cat_id = '$feed'");
2985 } else {
2986 $tmp_result = db_query($link, "SELECT id
2987 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2988 WHERE id = '$feed') AND id != '$feed'");
2989 }
ef393de7
AD
2990
2991 $cat_siblings = array();
2992
2993 if (db_num_rows($tmp_result) > 0) {
2994 while ($p = db_fetch_assoc($tmp_result)) {
2995 array_push($cat_siblings, "feed_id = " . $p["id"]);
2996 }
2997
2998 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2999 $feed, implode(" OR ", $cat_siblings));
3000
3001 } else {
3002 $query_strategy_part = "ttrss_entries.id > 0";
3003 }
3004
3005 } else if ($feed >= 0) {
3006
3007 if ($cat_view) {
5c365f60 3008
ef393de7
AD
3009 if ($feed > 0) {
3010 $query_strategy_part = "cat_id = '$feed'";
3011 } else {
3012 $query_strategy_part = "cat_id IS NULL";
3013 }
3014
3015 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 3016
ef393de7
AD
3017 } else {
3018 $tmp_result = db_query($link, "SELECT id
3019 FROM ttrss_feeds WHERE parent_feed = '$feed'
3020 ORDER BY cat_id,title");
3021
3022 $parent_ids = array();
3023
3024 if (db_num_rows($tmp_result) > 0) {
3025 while ($p = db_fetch_assoc($tmp_result)) {
3026 array_push($parent_ids, "feed_id = " . $p["id"]);
3027 }
3028
3029 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3030 $feed, implode(" OR ", $parent_ids));
3031
3032 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3033 } else {
3034 $query_strategy_part = "feed_id = '$feed'";
3035 }
3036 }
3037 } else if ($feed == -1) { // starred virtual feed
3038 $query_strategy_part = "marked = true";
3039 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e4f4b46f
AD
3040 } else if ($feed == -2) { // published virtual feed
3041 $query_strategy_part = "published = true";
2d24f032
AD
3042 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3043 } else if ($feed == -3) { // fresh virtual feed
3044 $query_strategy_part = "unread = true";
3045
c1d7e6c3
AD
3046 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
3047
2d24f032 3048 if (DB_TYPE == "pgsql") {
c1d7e6c3 3049 $query_strategy_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
2d24f032 3050 } else {
c1d7e6c3 3051 $query_strategy_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
3052 }
3053
e4f4b46f 3054 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
3055 } else if ($feed <= -10) { // labels
3056 $label_id = -$feed - 11;
3057
3058 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
3059 WHERE id = '$label_id'");
3060
3061 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
3de0261a
AD
3062
3063 if (!$query_strategy_part) {
3064 return false;
3065 }
3066
ef393de7
AD
3067 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3068 } else {
3069 $query_strategy_part = "id > 0"; // dumb
3070 }
d6e5706d
AD
3071
3072 if (get_pref($link, 'REVERSE_HEADLINES')) {
3073 $order_by = "updated";
3074 } else {
3075 $order_by = "updated DESC";
3076 }
e939722a
AD
3077
3078 if ($override_order) {
3079 $order_by = $override_order;
3080 }
ef393de7
AD
3081
3082 $feed_title = "";
3083
3084 if ($search && $search_mode == "all_feeds") {
b36e002f 3085 $feed_title = __("Search results")." ($search)";
ef393de7 3086 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
b36e002f 3087 $feed_title = __("Search results")." ($search, $feed)";
ef393de7
AD
3088 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
3089 $feed_title = $feed;
3090 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
3091
3092 if ($cat_view) {
5c365f60 3093
ef393de7
AD
3094 if ($feed != 0) {
3095 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
c36bf4d5 3096 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
3097 $feed_title = db_fetch_result($result, 0, "title");
3098 } else {
d1db26aa 3099 $feed_title = __("Uncategorized");
ef393de7 3100 }
e1eb2147
AD
3101
3102 if ($search) {
b36e002f 3103 $feed_title = __("Searched for")." $search ($feed_title)";
e1eb2147
AD
3104 }
3105
ef393de7
AD
3106 } else {
3107
3108 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
c36bf4d5 3109 WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7
AD
3110
3111 $feed_title = db_fetch_result($result, 0, "title");
3112 $feed_site_url = db_fetch_result($result, 0, "site_url");
3113 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
3114
3115 if ($search) {
b36e002f 3116 $feed_title = __("Searched for") . " $search ($feed_title)";
e1eb2147 3117 }
ef393de7
AD
3118 }
3119
3120 } else if ($feed == -1) {
d1db26aa 3121 $feed_title = __("Starred articles");
e4f4b46f
AD
3122 } else if ($feed == -2) {
3123 $feed_title = __("Published articles");
2d24f032
AD
3124 } else if ($feed == -3) {
3125 $feed_title = __("Fresh articles");
ef393de7
AD
3126 } else if ($feed < -10) {
3127 $label_id = -$feed - 11;
3128 $result = db_query($link, "SELECT description FROM ttrss_labels
3129 WHERE id = '$label_id'");
3130 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
3131
3132 if ($search) {
b36e002f 3133 $feed_title = __("Searched for") . " $search ($feed_title)";
88040f57 3134 }
ef393de7
AD
3135 } else {
3136 $feed_title = "?";
3137 }
3138
ef393de7
AD
3139 if ($feed < -10) error_reporting (0);
3140
62129e67
AD
3141 $content_query_part = "content as content_preview,";
3142
ef393de7
AD
3143 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3144
3145 if ($feed >= 0) {
3146 $feed_kind = "Feeds";
3147 } else {
3148 $feed_kind = "Labels";
3149 }
3150
95a82c08
AD
3151 if ($limit_query_part) {
3152 $offset_query_part = "OFFSET $offset";
3153 }
3154
ef393de7 3155 $query = "SELECT
1f64b1be 3156 guid,
ef393de7 3157 ttrss_entries.id,ttrss_entries.title,
46921916 3158 updated,
e4f4b46f 3159 unread,feed_id,marked,published,link,last_read,
ef393de7
AD
3160 SUBSTRING(last_read,1,19) as last_read_noms,
3161 $vfeed_query_part
3162 $content_query_part
d4b4b9de
AD
3163 SUBSTRING(updated,1,19) as updated_noms,
3164 author
ef393de7
AD
3165 FROM
3166 ttrss_entries,ttrss_user_entries,ttrss_feeds
3167 WHERE
cfb02131 3168 ttrss_feeds.hidden = false AND
ef393de7
AD
3169 ttrss_user_entries.feed_id = ttrss_feeds.id AND
3170 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 3171 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3172 $search_query_part
3173 $view_query_part
3174 $query_strategy_part ORDER BY $order_by
95a82c08 3175 $limit_query_part $offset_query_part";
ef393de7
AD
3176
3177 $result = db_query($link, $query);
3178
3179 if ($_GET["debug"]) print $query;
3180
3181 } else {
3182 // browsing by tag
3183
3184 $feed_kind = "Tags";
3185
3186 $result = db_query($link, "SELECT
1f64b1be 3187 guid,
ef393de7 3188 ttrss_entries.id as id,title,
46921916 3189 updated,
ef393de7
AD
3190 unread,feed_id,
3191 marked,link,last_read,
3192 SUBSTRING(last_read,1,19) as last_read_noms,
3193 $vfeed_query_part
3194 $content_query_part
3195 SUBSTRING(updated,1,19) as updated_noms
3196 FROM
3197 ttrss_entries,ttrss_user_entries,ttrss_tags
3198 WHERE
3199 ref_id = ttrss_entries.id AND
c36bf4d5 3200 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3201 post_int_id = int_id AND tag_name = '$feed' AND
3202 $view_query_part
3203 $search_query_part
3204 $query_strategy_part ORDER BY $order_by
3205 $limit_query_part");
3206 }
3207
c7188969 3208 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
3209
3210 }
3211
c36bf4d5 3212 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
3baeeeca 3213 $search, $search_mode, $match_on) {
18664970
AD
3214
3215 $qfh_ret = queryFeedHeadlines($link, $feed,
c36bf4d5
AD
3216 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
3217 $owner_uid);
18664970
AD
3218
3219 $result = $qfh_ret[0];
59e2aab4 3220 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
3221 $feed_site_url = $qfh_ret[2];
3222 $last_error = $qfh_ret[3];
3223
a5472764 3224// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 3225
a5472764
AD
3226 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3227 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3228 <rss version=\"2.0\">
3baeeeca
AD
3229 <channel>
3230 <title>$feed_title</title>
4bc64807
AD
3231 <link>$feed_site_url</link>
3232 <description>Feed generated by Tiny Tiny RSS</description>";
3baeeeca
AD
3233
3234 while ($line = db_fetch_assoc($result)) {
3235 print "<item>";
4bc64807 3236 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 3237 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 3238
bc976a8c 3239 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
3240
3241 foreach ($tags as $tag) {
3242 print "<category>" . htmlspecialchars($tag) . "</category>";
3243 }
3244
3baeeeca
AD
3245 $rfc822_date = date('r', strtotime($line["updated"]));
3246
3247 print "<pubDate>$rfc822_date</pubDate>";
3248
3249 print "<title>" .
3250 htmlspecialchars($line["title"]) . "</title>";
3251
0c3d1c68
AD
3252 print "<description><![CDATA[" .
3253 $line["content_preview"] . "]]></description>";
3baeeeca
AD
3254
3255 print "</item>";
3256 }
3257
3258 print "</channel></rss>";
18664970
AD
3259
3260 }
3261
0a6c4846
AD
3262 function getCategoryTitle($link, $cat_id) {
3263
bba7c4bf
AD
3264 if ($cat_id == -1) {
3265 return __("Special");
3266 } else if ($cat_id == -2) {
3267 return __("Labels");
0a6c4846 3268 } else {
bba7c4bf
AD
3269
3270 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3271 id = '$cat_id'");
3272
3273 if (db_num_rows($result) == 1) {
3274 return db_fetch_result($result, 0, "title");
3275 } else {
3276 return "Uncategorized";
3277 }
0a6c4846
AD
3278 }
3279 }
3280
f738aef1
AD
3281 // http://ru2.php.net/strip-tags
3282
3283 function strip_tags_long($textstring, $allowed){
3284 while($textstring != strip_tags($textstring, $allowed))
3285 {
3286 while (strlen($textstring) != 0)
3287 {
3288 if (strlen($textstring) > 1024) {
3289 $otherlen = 1024;
3290 } else {
3291 $otherlen = strlen($textstring);
3292 }
3293 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3294 $safetext .= $temptext;
3295 $textstring = substr_replace($textstring,'',0,$otherlen);
3296 }
3297 $textstring = $safetext;
3298 }
3299 return $textstring;
3300 }
3301
3302
1ac0baf4 3303 function sanitize_rss($link, $str, $force_strip_tags = false) {
60452879 3304 $res = $str;
183ad07b 3305
1ac0baf4 3306 if (get_pref($link, "STRIP_UNSAFE_TAGS") || $force_strip_tags) {
f738aef1 3307
7e1265ed
AD
3308 $res = strip_tags_long($res,
3309 "<p><a><i><em><b><strong><blockquote><br><img><div><span><ul><ol><li>");
f738aef1
AD
3310
3311// $res = preg_replace("/\r\n|\n|\r/", "", $res);
3312// $res = strip_tags_long($res, "<p><a><i><em><b><strong><blockquote><br><img><div><span>");
f826eee1
AD
3313 }
3314
183ad07b
AD
3315 return $res;
3316 }
b72c3ef8 3317
9cd7c995
AD
3318 function send_headlines_digests($link, $limit = 100) {
3319
1ddba275
AD
3320 if (!DIGEST_ENABLE) return false;
3321
9cd7c995 3322 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 3323 $days = 1;
9cd7c995
AD
3324
3325 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3326
3327 if (DB_TYPE == "pgsql") {
3328 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3329 } else if (DB_TYPE == "mysql") {
3330 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3331 }
3332
3333 $result = db_query($link, "SELECT id,email FROM ttrss_users
3334 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3335
3336 while ($line = db_fetch_assoc($result)) {
dc85be2b 3337
9cd7c995
AD
3338 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3339 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3340
dc85be2b
AD
3341 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3342
9cd7c995
AD
3343 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3344 $digest = $tuple[0];
3345 $headlines_count = $tuple[1];
dc85be2b 3346 $affected_ids = $tuple[2];
c62a2c21 3347 $digest_text = $tuple[3];
9cd7c995
AD
3348
3349 if ($headlines_count > 0) {
a8931123 3350
c62a2c21 3351 $mail = new PHPMailer();
a8931123 3352
c62a2c21
AD
3353 $mail->PluginDir = "phpmailer/";
3354 $mail->SetLanguage("en", "phpmailer/language/");
a8931123 3355
c62a2c21 3356 $mail->CharSet = "UTF-8";
a8931123 3357
c62a2c21
AD
3358 $mail->From = DIGEST_FROM_ADDRESS;
3359 $mail->FromName = DIGEST_FROM_NAME;
3360 $mail->AddAddress($line["email"], $line["login"]);
c7ddac5c 3361
c62a2c21 3362 if (DIGEST_SMTP_HOST) {
a8931123
AD
3363 $mail->Host = DIGEST_SMTP_HOST;
3364 $mail->Mailer = "smtp";
a8931123
AD
3365 $mail->Username = DIGEST_SMTP_LOGIN;
3366 $mail->Password = DIGEST_SMTP_PASSWORD;
c62a2c21 3367 }
a8931123 3368
c62a2c21 3369 $mail->IsHTML(true);
163a295e 3370 $mail->Subject = DIGEST_SUBJECT;
c62a2c21
AD
3371 $mail->Body = $digest;
3372 $mail->AltBody = $digest_text;
a8931123 3373
c62a2c21 3374 $rc = $mail->Send();
a8931123 3375
c62a2c21 3376 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
a8931123 3377
9cd7c995 3378 print "RC=$rc\n";
a8931123 3379
c62a2c21 3380 if ($rc && $do_catchup) {
dc85be2b 3381 print "Marking affected articles as read...\n";
9968d46f 3382 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
dc85be2b
AD
3383 }
3384
9cd7c995
AD
3385 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3386 WHERE id = " . $line["id"]);
3387 } else {
3388 print "No headlines\n";
3389 }
3390 }
3391 }
3392
cedd3e89
AD
3393 print "All done.\n";
3394
9cd7c995
AD
3395 }
3396
7e3634d9 3397 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
c62a2c21
AD
3398
3399 require_once "MiniTemplator.class.php";
3400
3401 $tpl = new MiniTemplator;
3402 $tpl_t = new MiniTemplator;
3403
3404 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3405 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3406
3407 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3408 $tpl->setVariable('CUR_TIME', date('G:i'));
3409
3410 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3411 $tpl_t->setVariable('CUR_TIME', date('G:i'));
7e3634d9 3412
dc85be2b
AD
3413 $affected_ids = array();
3414
7e3634d9 3415 if (DB_TYPE == "pgsql") {
9cd7c995 3416 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
3417 } else if (DB_TYPE == "mysql") {
3418 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3419 }
3420
3421 $result = db_query($link, "SELECT ttrss_entries.title,
3422 ttrss_feeds.title AS feed_title,
3423 date_entered,
dc85be2b 3424 ttrss_user_entries.ref_id,
7e3634d9 3425 link,
c62a2c21 3426 SUBSTRING(content, 1, 120) AS excerpt,
7e3634d9
AD
3427 SUBSTRING(last_updated,1,19) AS last_updated
3428 FROM
3429 ttrss_user_entries,ttrss_entries,ttrss_feeds
3430 WHERE
3431 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 3432 AND include_in_digest = true
7e3634d9 3433 AND $interval_query
a8931123 3434 AND hidden = false
448b0abd 3435 AND ttrss_user_entries.owner_uid = $user_id
c62a2c21
AD
3436 AND unread = true
3437 ORDER BY ttrss_feeds.title, date_entered DESC
7e3634d9
AD
3438 LIMIT $limit");
3439
3440 $cur_feed_title = "";
3441
9cd7c995
AD
3442 $headlines_count = db_num_rows($result);
3443
c62a2c21
AD
3444 $headlines = array();
3445
7e3634d9 3446 while ($line = db_fetch_assoc($result)) {
c62a2c21
AD
3447 array_push($headlines, $line);
3448 }
3449
3450 for ($i = 0; $i < sizeof($headlines); $i++) {
3451
3452 $line = $headlines[$i];
dc85be2b
AD
3453
3454 array_push($affected_ids, $line["ref_id"]);
3455
7e3634d9 3456 $updated = smart_date_time(strtotime($line["last_updated"]));
7e3634d9 3457
c62a2c21
AD
3458 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3459 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3460 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3461 $tpl->setVariable('ARTICLE_UPDATED', $updated);
163a295e
AD
3462 $tpl->setVariable('ARTICLE_EXCERPT',
3463 truncate_string(strip_tags($line["excerpt"]), 100));
7e3634d9 3464
c62a2c21
AD
3465 $tpl->addBlock('article');
3466
3467 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3468 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3469 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3470 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3471// $tpl_t->setVariable('ARTICLE_EXCERPT',
3472// truncate_string(strip_tags($line["excerpt"]), 100));
3473
3474 $tpl_t->addBlock('article');
3475
3476 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
3477 $tpl->addBlock('feed');
3478 $tpl_t->addBlock('feed');
7e3634d9
AD
3479 }
3480
7e3634d9
AD
3481 }
3482
c62a2c21
AD
3483 $tpl->addBlock('digest');
3484 $tpl->generateOutputToString($tmp);
3485
3486 $tpl_t->addBlock('digest');
3487 $tpl_t->generateOutputToString($tmp_t);
7e3634d9 3488
c62a2c21 3489 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
7e3634d9
AD
3490 }
3491
d9fa39f1 3492 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
3493 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
3494
3495 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
3496 return;
3497 }
3498
3499 error_reporting(0);
f67d9754
AD
3500 if (ENABLE_SIMPLEPIE) {
3501 $rss = new SimplePie();
3502 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
4148e809 3503// $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
f67d9754
AD
3504 $rss->set_feed_url($fetch_url);
3505 $rss->set_output_encoding('UTF-8');
3506 $rss->init();
3507 } else {
3508 $rss = fetch_rss($releases_feed);
3509 }
b72c3ef8
AD
3510 error_reporting (DEFAULT_ERROR_LEVEL);
3511
3512 if ($rss) {
3513
f67d9754
AD
3514 if (ENABLE_SIMPLEPIE) {
3515 $items = $rss->get_items();
3516 } else {
3517 $items = $rss->items;
b72c3ef8 3518
f67d9754
AD
3519 if (!$items || !is_array($items)) $items = $rss->entries;
3520 if (!$items || !is_array($items)) $items = $rss;
3521 }
b72c3ef8 3522
da412ad3 3523 if (!is_array($items) || count($items) == 0) {
b72c3ef8 3524 return;
da412ad3 3525 }
b72c3ef8 3526
a41d2c65 3527 $latest_item = $items[0];
b72c3ef8 3528
f67d9754
AD
3529 if (ENABLE_SIMPLEPIE) {
3530 $last_title = $latest_item->get_title();
3531 } else {
3532 $last_title = $latest_item["title"];
3533 }
b72c3ef8 3534
f67d9754
AD
3535 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3536
3537 if (ENABLE_SIMPLEPIE) {
dcf7fd08
AD
3538 $release_url = sanitize_rss($link, $latest_item->get_link());
3539 $content = sanitize_rss($link, $latest_item->get_description());
f67d9754
AD
3540 } else {
3541 $release_url = sanitize_rss($link, $latest_item["link"]);
3542 $content = sanitize_rss($link, $latest_item["description"]);
3543 }
48e1a342 3544
a41d2c65 3545 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 3546 if ($brief_fmt) {
0d32b41e 3547 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 3548 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 3549 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 3550 } else {
92625568
AD
3551 return "New version of Tiny-Tiny RSS ($latest_version) is available:
3552 <div class='milestoneDetails'>$content</div>
3553 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
3554 download and update information.";
d9fa39f1
AD
3555 }
3556
da412ad3 3557 }
b72c3ef8
AD
3558 }
3559 }
472782e8 3560
18eddb2c
AD
3561 function markArticlesById($link, $ids, $cmode) {
3562
3563 $tmp_ids = array();
3564
3565 foreach ($ids as $id) {
3566 array_push($tmp_ids, "ref_id = '$id'");
3567 }
3568
3569 $ids_qpart = join(" OR ", $tmp_ids);
3570
3571 if ($cmode == 0) {
3572 db_query($link, "UPDATE ttrss_user_entries SET
3573 marked = false,last_read = NOW()
3574 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3575 } else if ($cmode == 1) {
3576 db_query($link, "UPDATE ttrss_user_entries SET
3577 marked = true
3578 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3579 } else {
3580 db_query($link, "UPDATE ttrss_user_entries SET
3581 marked = NOT marked,last_read = NOW()
3582 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3583 }
3584 }
3585
e4f4b46f
AD
3586 function publishArticlesById($link, $ids, $cmode) {
3587
3588 $tmp_ids = array();
3589
3590 foreach ($ids as $id) {
3591 array_push($tmp_ids, "ref_id = '$id'");
3592 }
3593
3594 $ids_qpart = join(" OR ", $tmp_ids);
3595
3596 if ($cmode == 0) {
3597 db_query($link, "UPDATE ttrss_user_entries SET
3598 published = false,last_read = NOW()
3599 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3600 } else if ($cmode == 1) {
3601 db_query($link, "UPDATE ttrss_user_entries SET
3602 published = true
3603 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3604 } else {
3605 db_query($link, "UPDATE ttrss_user_entries SET
3606 published = NOT published,last_read = NOW()
3607 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
3608 }
3609 }
3610
9968d46f
AD
3611 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
3612
3613 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
472782e8
AD
3614
3615 $tmp_ids = array();
3616
3617 foreach ($ids as $id) {
3618 array_push($tmp_ids, "ref_id = '$id'");
3619 }
3620
3621 $ids_qpart = join(" OR ", $tmp_ids);
3622
3623 if ($cmode == 0) {
3624 db_query($link, "UPDATE ttrss_user_entries SET
3625 unread = false,last_read = NOW()
9968d46f 3626 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3627 } else if ($cmode == 1) {
3628 db_query($link, "UPDATE ttrss_user_entries SET
3629 unread = true
9968d46f 3630 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3631 } else {
3632 db_query($link, "UPDATE ttrss_user_entries SET
3633 unread = NOT unread,last_read = NOW()
9968d46f 3634 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
3635 }
3636 }
3637
e097e8be
AD
3638 function catchupArticleById($link, $id, $cmode) {
3639
3640 if ($cmode == 0) {
3641 db_query($link, "UPDATE ttrss_user_entries SET
3642 unread = false,last_read = NOW()
3643 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3644 } else if ($cmode == 1) {
3645 db_query($link, "UPDATE ttrss_user_entries SET
3646 unread = true
3647 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3648 } else {
3649 db_query($link, "UPDATE ttrss_user_entries SET
3650 unread = NOT unread,last_read = NOW()
3651 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
3652 }
3653 }
3654
1f64b1be
AD
3655 function make_guid_from_title($title) {
3656 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 3657 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
3658 }
3659
11befbb2
AD
3660 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
3661 $bottom = false, $rtl_content = false, $feed_id = 0,
3662 $is_cat = false, $search = false, $match_on = false,
95a82c08 3663 $search_mode = false, $offset = 0, $limit = 0) {
11befbb2 3664
e6c115b2
AD
3665 $user_page_offset = $offset + 1;
3666
11befbb2
AD
3667 if (!$bottom) {
3668 $class = "headlinesSubToolbar";
3669 $tid = "headlineActionsTop";
3670 } else {
3671 $class = "headlinesSubToolbar";
3672 $tid = "headlineActionsBottom";
3673 }
3674
3675 print "<table class=\"$class\" id=\"$tid\"
3676 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
3677
3678 if ($rtl_content) {
3679 $rtl_cpart = "RTL";
3680 } else {
3681 $rtl_cpart = "";
3682 }
3683
e6c115b2
AD
3684 $page_prev_link = "javascript:viewFeedGoPage(-1)";
3685 $page_next_link = "javascript:viewFeedGoPage(1)";
3686 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 3687
eb28b131
AD
3688 $catchup_page_link = "javascript:catchupPage()";
3689 $catchup_feed_link = "javascript:catchupCurrentFeed()";
a5ae125a 3690 $catchup_sel_link = "javascript:catchupSelection()";
c6008b62 3691
11befbb2
AD
3692 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
3693
c6008b62
AD
3694 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
3695 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
3696 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
11befbb2 3697
c6008b62
AD
3698 $tog_unread_link = "javascript:selectionToggleUnread()";
3699 $tog_marked_link = "javascript:selectionToggleMarked()";
e4f4b46f 3700 $tog_published_link = "javascript:selectionTogglePublished()";
11befbb2 3701
c6008b62 3702 } else {
11befbb2 3703
c6008b62
AD
3704 $sel_all_link = "javascript:cdmSelectArticles('all')";
3705 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
3706 $sel_none_link = "javascript:cdmSelectArticles('none')";
3707
3708 $tog_unread_link = "javascript:selectionToggleUnread(true)";
3709 $tog_marked_link = "javascript:selectionToggleMarked(true)";
e4f4b46f 3710 $tog_published_link = "javascript:selectionTogglePublished(true)";
c6008b62
AD
3711
3712 }
3713
d420f2ee 3714 if (strpos($_SESSION["client.userAgent"], "MSIE") === false) {
c6008b62 3715
2dd2c13b
AD
3716 print "<td class=\"headlineActions$rtl_cpart\">
3717 <ul class=\"headlineDropdownMenu\">
3718 <li class=\"top2\">
3692e98f 3719 ".__('Select:')."
1025ad87
AD
3720 <a href=\"$sel_all_link\">".__('All')."</a>,
3721 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3722 <a href=\"$sel_none_link\">".__('None')."</a></li>
2dd2c13b 3723 <li class=\"vsep\">&nbsp;</li>
89cb787e 3724 <li class=\"top\">".__('Toggle')."<ul>
9cc600d1 3725 <li onclick=\"$tog_unread_link\">".__('Unread')."</li>
e4f4b46f
AD
3726 <li onclick=\"$tog_marked_link\">".__('Starred')."</li>
3727 <li onclick=\"$tog_published_link\">".__('Published')."</li>
3728 </ul></li>
2dd2c13b 3729 <li class=\"vsep\">&nbsp;</li>
77590c49 3730 <li class=\"top\">".__('Mark as read')."<ul>
a5ae125a 3731 <li onclick=\"$catchup_sel_link\">".__('Selection')."</li>
8be83f42
AD
3732 <!-- <li onclick=\"$catchup_page_link\">".__('This page')."</li> -->
3733 <li><span class=\"insensitive\">--------</span></li>
3734 <li onclick=\"catchupRelativeToArticle(0)\">".__("Above active article")."</li>
3735 <li onclick=\"catchupRelativeToArticle(1)\">".__("Below active article")."</li>
3736 <li><span class=\"insensitive\">--------</span></li>
1025ad87 3737 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
237ec2ad 3738 ";
95a82c08 3739
237ec2ad
AD
3740 $enable_pagination = get_pref($link, "_PREFS_ENABLE_PAGINATION");
3741
3742 if ($limit != 0 && !$search && $enable_pagination) {
95a82c08 3743 print "
237ec2ad 3744 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
3745 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
3746 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
3747 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
95a82c08 3748 </ul>";
d420f2ee 3749 }
95a82c08 3750
d420f2ee 3751 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
237ec2ad
AD
3752 print "
3753 <li class=\"vsep\">&nbsp;</li>
3754 <li class=\"top3\">
d420f2ee
AD
3755 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3756 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3757 ".__('Convert to label')."</a></td>";
d420f2ee 3758 }
95a82c08 3759 print "
2dd2c13b
AD
3760 </td>";
3761
3762 } else {
e6c115b2
AD
3763 // old style subtoolbar:
3764
2dd2c13b 3765 print "<td class=\"headlineActions$rtl_cpart\">".
d1db26aa 3766 __('Select:')."
1025ad87
AD
3767 <a href=\"$sel_all_link\">".__('All')."</a>,
3768 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
3769 <a href=\"$sel_none_link\">".__('None')."</a>
2dd2c13b 3770 &nbsp;&nbsp;".
1025ad87
AD
3771 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
3772 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2dd2c13b 3773 &nbsp;&nbsp;".
d1db26aa 3774 __('Mark as read:')."
1025ad87
AD
3775 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
3776 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
d420f2ee
AD
3777
3778 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
3779
3780 print "&nbsp;&nbsp;
3781 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3782 '$match_on', '$feed_id', '$is_cat');\">
b36e002f 3783 ".__('Convert to label')."</a>";
d420f2ee
AD
3784 }
3785
2dd2c13b
AD
3786 print "</td>";
3787
3788 }
c6008b62 3789
d420f2ee 3790/* if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
c6008b62
AD
3791 print "<td class=\"headlineActions$rtl_cpart\">
3792 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
3793 '$match_on', '$feed_id', '$is_cat');\">
d1db26aa 3794 ".__('Convert to Label')."</a></td>";
d420f2ee 3795} */
11befbb2
AD
3796
3797 print "<td class=\"headlineTitle$rtl_cpart\">";
11befbb2 3798
b27967ac 3799 print "<span class=\"headlineInnerTitle\">";
11befbb2 3800
b27967ac
AD
3801 if ($feed_site_url) {
3802 if (!$bottom) {
3803 $target = "target=\"_new\"";
20361063 3804 }
b27967ac
AD
3805 print "<a $target href=\"$feed_site_url\">".
3806 truncate_string($feed_title,30)."</a>";
3807 } else {
3808 print $feed_title;
3809 }
3810
3811 if ($search) {
3812 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
e6c115b2
AD
3813 }
3814
b27967ac
AD
3815 if ($user_page_offset > 1) {
3816 print " [$user_page_offset] ";
3817 }
3818
3819 print "</span>";
3820
11befbb2 3821 if (!$bottom) {
e6c115b2 3822 print "
11befbb2
AD
3823 <a target=\"_new\"
3824 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
3825 <img class=\"noborder\"
1025ad87 3826 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2
AD
3827 </a>";
3828 }
3829
3830 print "</td>";
3831 print "</tr></table>";
3832
3833 }
3834
bba7c4bf
AD
3835 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
3836
3837 $tmp_category = getCategoryTitle($link, $cat_id);
3838 $cat_unread = getCategoryUnread($link, $cat_id);
3839
3840 if ($hidden) {
3841 $holder_style = "display:none;";
66a251f9 3842 $ellipsis = "…";
bba7c4bf
AD
3843 } else {
3844 $holder_style = "";
3845 $ellipsis = "";
3846 }
3847
3848 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3849
3850 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
3851 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>";
3852
3853 if ($can_browse) {
3854 print "<a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">";
3855 } else {
3856 print "<span id=\"FCAP-$cat_id\">";
3857 }
3858
dda1396f 3859 print " <span id=\"FCATCTR-$cat_id\"
bba7c4bf
AD
3860 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
3861
3862 if ($can_browse) {
3863 print "</a>";
3864 } else {
3865 print "</span>";
3866 }
3867
3868 print "</li>";
3869
3870 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
3871 }
3872
f407c086
AD
3873 function outputFeedList($link, $tags = false) {
3874
3bd9a780 3875 print "<ul class=\"feedList\" id=\"feedList\">";
f407c086
AD
3876
3877 $owner_uid = $_SESSION["uid"];
3878
cf4d339c 3879 /* virtual feeds */
f407c086 3880
cf4d339c 3881 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3b086176
AD
3882
3883 if ($_COOKIE["ttrss_vf_vclps"] == 1) {
bba7c4bf 3884 $cat_hidden = true;
3b086176 3885 } else {
bba7c4bf 3886 $cat_hidden = false;
3b086176
AD
3887 }
3888
3889# print "<li class=\"feedCat\">".__('Special')."</li>";
3890# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\">";
bba7c4bf
AD
3891# print "<li class=\"feedCat\">".
3892# "<a id=\"FCATN--1\" href=\"javascript:toggleCollapseCat(-1)\">".
3893# __('Special')."</a> <span id='FCAP--1'>$ellipsis</span></li>";
3894#
3895# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\">
3896# <ul class=\"feedCatList\" id='FCATLIST--1' style='$holder_style'>";
3b086176 3897
bba7c4bf
AD
3898# $cat_unread = getCategoryUnread($link, -1);
3899# $tmp_category = __("Special");
3900# $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3b086176 3901
bba7c4bf 3902 printCategoryHeader($link, -1, $cat_hidden, false);
cf4d339c 3903 }
f407c086 3904
cf4d339c 3905 $num_starred = getFeedUnread($link, -1);
e4f4b46f 3906 $num_published = getFeedUnread($link, -2);
2d24f032
AD
3907 $num_fresh = getFeedUnread($link, -3);
3908
3909 $class = "virt";
3910
3911 if ($num_fresh > 0) $class .= "Unread";
3912
3913 printFeedEntry(-3, $class, __("Fresh articles"), $num_fresh,
3914 "images/fresh.png", $link);
f407c086 3915
cf4d339c 3916 $class = "virt";
f407c086 3917
cf4d339c 3918 if ($num_starred > 0) $class .= "Unread";
f407c086 3919
abd8a516
AD
3920 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
3921
3922 if ($is_ie) {
3923 $mark_img_ext = "gif";
3924 } else {
3925 $mark_img_ext = "png";
3926 }
3927
d1db26aa 3928 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
abd8a516 3929 "images/mark_set.$mark_img_ext", $link);
f407c086 3930
e4f4b46f
AD
3931 $class = "virt";
3932
3933 if ($num_published > 0) $class .= "Unread";
3934
3935 printFeedEntry(-2, $class, __("Published articles"), $num_published,
f5e0338d 3936 "images/pub_set.gif", $link);
e4f4b46f 3937
cf4d339c 3938 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 3939 print "</ul>";
cf4d339c 3940 }
f407c086 3941
cf4d339c 3942 if (!$tags) {
f407c086
AD
3943
3944 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
3945
3946 $result = db_query($link, "SELECT id,sql_exp,description FROM
3947 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
3948
3949 if (db_num_rows($result) > 0) {
3950 if (get_pref($link, 'ENABLE_FEED_CATS')) {
bd64489f
AD
3951
3952 if ($_COOKIE["ttrss_vf_lclps"] == 1) {
bba7c4bf 3953 $cat_hidden = true;
bd64489f 3954 } else {
bba7c4bf 3955 $cat_hidden = false;
bd64489f
AD
3956 }
3957
bba7c4bf 3958 printCategoryHeader($link, -2, $cat_hidden, false);
bd64489f 3959
bba7c4bf
AD
3960# print "<li class=\"feedCat\">".
3961# "<a id=\"FCATN--2\" href=\"javascript:toggleCollapseCat(-2)\">".
3962# __('Labels')."</a> <span id='FCAP--2'>$ellipsis</span></li>";
3963#
3964# print "<li id=\"feedCatHolder\" class=\"feedCatHolder\"><ul class=\"feedCatList\" id='FCATLIST--2' style='$holder_style'>";
f407c086 3965 } else {
3bd9a780 3966 print "<li><hr></li>";
f407c086
AD
3967 }
3968 }
3969
3970 while ($line = db_fetch_assoc($result)) {
3971
3972 error_reporting (0);
3973
3974 $label_id = -$line['id'] - 11;
3975 $count = getFeedUnread($link, $label_id);
3976
3977 $class = "label";
3978
3979 if ($count > 0) {
3980 $class .= "Unread";
3981 }
3982
3983 error_reporting (DEFAULT_ERROR_LEVEL);
3984
3985 printFeedEntry($label_id,
47439031 3986 $class, $line["description"],
f407c086
AD
3987 $count, "images/label.png", $link);
3988
3989 }
3990
3991 if (db_num_rows($result) > 0) {
3992 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3993 print "</ul>";
3994 }
3995 }
3996
3997 }
3998
3999 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4000 print "<li><hr></li>";
f407c086
AD
4001 }
4002
4003 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4004 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4005 $order_by_qpart = "category,unread DESC,title";
4006 } else {
4007 $order_by_qpart = "category,title";
4008 }
4009 } else {
4010 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4011 $order_by_qpart = "unread DESC,title";
4012 } else {
4013 $order_by_qpart = "title";
4014 }
4015 }
4016
14073c0a
AD
4017 $age_qpart = getMaxAgeSubquery();
4018
f407c086
AD
4019 $result = db_query($link, "SELECT ttrss_feeds.*,
4020 SUBSTRING(last_updated,1,19) AS last_updated_noms,
4021 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
4022 WHERE feed_id = ttrss_feeds.id AND unread = true
14073c0a 4023 AND $age_qpart
f407c086
AD
4024 AND ttrss_user_entries.ref_id = ttrss_entries.id
4025 AND owner_uid = '$owner_uid') as unread,
4026 cat_id,last_error,
4027 ttrss_feed_categories.title AS category,
4028 ttrss_feed_categories.collapsed
4029 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4030 ON (ttrss_feed_categories.id = cat_id)
4031 WHERE
4032 ttrss_feeds.hidden = false AND
4033 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
4034 ORDER BY $order_by_qpart");
4035
4036 $actid = $_GET["actid"];
4037
4038 /* real feeds */
4039
4040 $lnum = 0;
4041
4042 $total_unread = 0;
4043
4044 $category = "";
4045
4046 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4047
4048 while ($line = db_fetch_assoc($result)) {
4049
47439031 4050 $feed = trim($line["title"]);
0f39ae20
AD
4051
4052 if (!$feed) $feed = "[Untitled]";
4053
f407c086
AD
4054 $feed_id = $line["id"];
4055
4056 $subop = $_GET["subop"];
4057
4058 $unread = $line["unread"];
4059
4060 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4061 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4062 } else {
4063 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4064 }
4065
4066 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4067
4068 if ($rtl_content) {
4069 $rtl_tag = "dir=\"RTL\"";
4070 } else {
4071 $rtl_tag = "";
4072 }
4073
4074 $tmp_result = db_query($link,
4075 "SELECT id,COUNT(unread) AS unread
4076 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
4077 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
4078 WHERE parent_feed = '$feed_id' AND unread = true
4079 GROUP BY ttrss_feeds.id");
4080
4081 if (db_num_rows($tmp_result) > 0) {
4082 while ($l = db_fetch_assoc($tmp_result)) {
4083 $unread += $l["unread"];
4084 }
4085 }
4086
4087 $cat_id = $line["cat_id"];
4088
4089 $tmp_category = $line["category"];
4090
4091 if (!$tmp_category) {
d1db26aa 4092 $tmp_category = __("Uncategorized");
f407c086
AD
4093 }
4094
4095 // $class = ($lnum % 2) ? "even" : "odd";
4096
4097 if ($line["last_error"]) {
4098 $class = "error";
4099 } else {
4100 $class = "feed";
4101 }
4102
4103 if ($unread > 0) $class .= "Unread";
4104
4105 if ($actid == $feed_id) {
4106 $class .= "Selected";
4107 }
4108
4109 $total_unread += $unread;
4110
4111 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4112
4113 if ($category) {
4114 print "</ul></li>";
4115 }
4116
4117 $category = $tmp_category;
4118
4119 $collapsed = $line["collapsed"];
4120
4121 // workaround for NULL category
d1db26aa 4122 if ($category == __("Uncategorized")) {
f407c086
AD
4123 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
4124 $collapsed = "t";
4125 }
4126 }
4127
4128 if ($collapsed == "t" || $collapsed == "1") {
225ec0d4
AD
4129 $holder_class = "feedCatHolder";
4130 $holder_style = "display:none;";
66a251f9 4131 $ellipsis = "…";
f407c086 4132 } else {
be5b75da 4133 $holder_class = "feedCatHolder";
225ec0d4 4134 $holder_style = "";
f407c086
AD
4135 $ellipsis = "";
4136 }
4137
4138 $cat_id = sprintf("%d", $cat_id);
4139
4140 $cat_unread = getCategoryUnread($link, $cat_id);
67dabe1a
AD
4141
4142 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
4143
3bd9a780 4144 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
439bbe63 4145 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
7210613a 4146 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
dda1396f 4147 <span id=\"FCATCTR-$cat_id\"
67dabe1a 4148 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
3bd9a780 4149 </a></li>";
f407c086 4150
225ec0d4 4151 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
f407c086
AD
4152 }
4153
4154 printFeedEntry($feed_id, $class, $feed, $unread,
99e37e09 4155 ICONS_DIR."/$feed_id.ico", $link, $rtl_content,
f407c086
AD
4156 $last_updated, $line["last_error"]);
4157
4158 ++$lnum;
4159 }
4160
4161 if (db_num_rows($result) == 0) {
3bd9a780 4162 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
4163 }
4164
4165 } else {
4166
4167 // tags
4168
4169/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4170 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4171 post_int_id = ttrss_user_entries.int_id AND
4172 unread = true AND ref_id = ttrss_entries.id
4173 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4174 UNION
4175 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4176 ORDER BY tag_name"); */
4177
4178 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 4179 print "<li class=\"feedCat\">".__('Tags')."</li>";
f407c086
AD
4180 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
4181 }
4182
14073c0a
AD
4183 $age_qpart = getMaxAgeSubquery();
4184
f407c086 4185 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
4186 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4187 AND ref_id = id AND $age_qpart
f407c086 4188 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
4189 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4190 ORDER BY count DESC LIMIT 50");
f407c086
AD
4191
4192 $tags = array();
4193
4194 while ($line = db_fetch_assoc($result)) {
4195 $tags[$line["tag_name"]] += $line["count"];
4196 }
4197
4198 foreach (array_keys($tags) as $tag) {
4199
4200 $unread = $tags[$tag];
4201
4202 $class = "tag";
4203
4204 if ($unread > 0) {
4205 $class .= "Unread";
4206 }
4207
4208 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4209
4210 }
4211
4212 if (db_num_rows($result) == 0) {
4213 print "<li>No tags to display.</li>";
4214 }
4215
4216 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4217 print "</ul>";
f407c086
AD
4218 }
4219
4220 }
4221
4222 print "</ul>";
4223
4224 }
4225
bc976a8c 4226 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2
AD
4227
4228 $a_id = db_escape_string($id);
4229
bc976a8c
AD
4230 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4231
0c3d1c68
AD
4232 $tmp_result = db_query($link, "SELECT DISTINCT tag_name,
4233 owner_uid as owner FROM
0b126ac2 4234 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bc976a8c 4235 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
4236
4237 $tags = array();
4238
4239 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4240 array_push($tags, $tmp_line["tag_name"]);
4241 }
4242
4243 return $tags;
4244 }
4245
d62a3b63
AD
4246 function trim_value(&$value) {
4247 $value = trim($value);
4248 }
4249
4250 function trim_array($array) {
4251 $tmp = $array;
4252 array_walk($tmp, 'trim_value');
4253 return $tmp;
4254 }
4255
be832a1a 4256 function tag_is_valid($tag) {
ef063748
AD
4257 if ($tag == '') return false;
4258 if (preg_match("/^[0-9]*$/", $tag)) return false;
4259
4260 $tag = iconv("utf-8", "utf-8", $tag);
4261 if (!$tag) return false;
4262
4263 return true;
be832a1a
AD
4264 }
4265
793185a9
AD
4266 function render_login_form($link, $mobile = false) {
4267 if (!$mobile) {
4268 require_once "login_form.php";
4269 } else {
4270 require_once "mobile/login_form.php";
4271 }
01a87dff
AD
4272 }
4273
dc56b3b7
AD
4274 // from http://developer.apple.com/internet/safari/faq.html
4275 function no_cache_incantation() {
4276 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4277 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4278 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4279 header("Cache-Control: post-check=0, pre-check=0", false);
4280 header("Pragma: no-cache"); // HTTP/1.0
4281 }
4282
42395d28
AD
4283 function format_warning($msg, $id = "") {
4284 return "<div class=\"warning\" id=\"$id\">
e780d1d2 4285 <img src=\"images/sign_excl.gif\">$msg</div>";
0d32b41e
AD
4286 }
4287
4288 function format_notice($msg) {
4289 return "<div class=\"notice\">
e780d1d2 4290 <img src=\"images/sign_info.gif\">$msg</div>";
0d32b41e
AD
4291 }
4292
68d2f95e
AD
4293 function format_error($msg) {
4294 return "<div class=\"error\">
e780d1d2 4295 <img src=\"images/sign_excl.gif\">$msg</div>";
68d2f95e
AD
4296 }
4297
4dccf1ed
AD
4298 function print_notice($msg) {
4299 return print format_notice($msg);
4300 }
4301
4302 function print_warning($msg) {
4303 return print format_warning($msg);
4304 }
4305
68d2f95e
AD
4306 function print_error($msg) {
4307 return print format_error($msg);
4308 }
4309
4310
4dccf1ed
AD
4311 function T_sprintf() {
4312 $args = func_get_args();
4313 return vsprintf(__(array_shift($args)), $args);
4314 }
4315
3de0261a
AD
4316 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true) {
4317
10eb9da8
AD
4318 /* we can figure out feed_id from article id anyway, why do we
4319 * pass feed_id here? */
4320
4321 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4322 WHERE ref_id = '$id'");
4323
4324 $feed_id = db_fetch_result($result, 0, "feed_id");
4325
3de0261a
AD
4326 print "<article id='$id'><![CDATA[";
4327
4328 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4329 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4330
4331 if (db_num_rows($result) == 1) {
4332 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4333 } else {
4334 $rtl_content = false;
4335 }
4336
4337 if ($rtl_content) {
4338 $rtl_tag = "dir=\"RTL\"";
4339 $rtl_class = "RTL";
4340 } else {
4341 $rtl_tag = "";
4342 $rtl_class = "";
4343 }
4344
4345 if ($mark_as_read) {
4346 $result = db_query($link, "UPDATE ttrss_user_entries
4347 SET unread = false,last_read = NOW()
4348 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4349 }
4350
4351 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
4352 SUBSTRING(updated,1,16) as updated,
4353 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4354 num_comments,
4355 author
4356 FROM ttrss_entries,ttrss_user_entries
4357 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4358
4359 if ($result) {
4360
4361 $link_target = "";
4362
4363 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4364 $link_target = "target=\"_new\"";
4365 }
4366
4367 $line = db_fetch_assoc($result);
4368
4369 if ($line["icon_url"]) {
4370 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4371 } else {
4372 $feed_icon = "&nbsp;";
4373 }
4374
4375/* if ($line["comments"] && $line["link"] != $line["comments"]) {
4376 $entry_comments = "(<a href=\"".$line["comments"]."\">Comments</a>)";
4377 } else {
4378 $entry_comments = "";
4379 } */
4380
4381 $num_comments = $line["num_comments"];
4382 $entry_comments = "";
4383
4384 if ($num_comments > 0) {
4385 if ($line["comments"]) {
4386 $comments_url = $line["comments"];
4387 } else {
4388 $comments_url = $line["link"];
4389 }
4390 $entry_comments = "<a $link_target href=\"$comments_url\">$num_comments comments</a>";
4391 } else {
4392 if ($line["comments"] && $line["link"] != $line["comments"]) {
4393 $entry_comments = "<a $link_target href=\"".$line["comments"]."\">comments</a>";
4394 }
4395 }
4396
4397 print "<div class=\"postReply\">";
4398
4399 print "<div class=\"postHeader\">";
4400
4401 $entry_author = $line["author"];
4402
4403 if ($entry_author) {
4404 $entry_author = __(" - by ") . $entry_author;
4405 }
4406
4407 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4408 strtotime($line["updated"]));
4409
4410 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4411
4412 if ($line["link"]) {
4413 print "<div clear='both'><a $link_target href=\"" . $line["link"] . "\">" .
06202d88 4414 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
3de0261a
AD
4415 } else {
4416 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4417 }
4418
9cfd409d 4419/* $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3de0261a 4420 ttrss_tags WHERE post_int_id = " . $line["int_id"] . "
9cfd409d
AD
4421 ORDER BY tag_name"); */
4422
4423 $tags = get_article_tags($link, $id);
3de0261a
AD
4424
4425 $tags_str = "";
4426 $f_tags_str = "";
4427
4428 $num_tags = 0;
4429
20361063
AD
4430 if ($_SESSION["theme"] == "3pane") {
4431 $tag_limit = 3;
4432 } else {
4433 $tag_limit = 6;
4434 }
4435
9cfd409d 4436 foreach ($tags as $tag) {
3de0261a 4437 $num_tags++;
14b6c54b
AD
4438 $tag_escaped = str_replace("'", "\\'", $tag);
4439
4440 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>, ";
3de0261a 4441
20361063 4442 if ($num_tags == $tag_limit) {
66a251f9 4443 $tags_str .= "&hellip;";
e7544143 4444
20361063 4445 } else if ($num_tags < $tag_limit) {
3de0261a
AD
4446 $tags_str .= $tag_str;
4447 }
4448 $f_tags_str .= $tag_str;
4449 }
4450
4451 $tags_str = preg_replace("/, $/", "", $tags_str);
4452 $f_tags_str = preg_replace("/, $/", "", $f_tags_str);
4453
66a251f9 4454 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $f_tags_str</div></span>";
e7544143
AD
4455 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4456
3de0261a
AD
4457 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4458
4459 if (!$tags_str) $tags_str = '<span class="tagList">'.__('no tags').'</span>';
4460
5f014cf1
AD
4461 print "<div style='float : right'>
4462 <img src='images/tag.png' class='tagsPic' alt='Tags' title='Tags'>
4463 $tags_str
3de0261a
AD
4464 <a title=\"Edit tags for this article\"
4465 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a></div>
4466 <div clear='both'>$entry_comments</div>";
4467
4468 print "</div>";
4469
4470 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
4471 print "<div class=\"postContent\">";
4472
e7544143 4473 #print "<div id=\"allEntryTags\">".__('Tags:')." $f_tags_str</div>";
3de0261a 4474
c41890b0
AD
4475 $line["content"] = sanitize_rss($link, $line["content"]);
4476
3de0261a
AD
4477 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4478 $line["content"] = preg_replace("/href=/i", "target=\"_new\" href=", $line["content"]);
4479 }
4480
ce53e200
AD
4481 print $line["content"];
4482
4483 $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4484 post_id = '$id'");
4485
4486 if (db_num_rows($result) > 0) {
4487 print "<div class=\"postEnclosures\">";
4488
4489 if (db_num_rows($result) == 1) {
4490 print __("Attachment:") . " ";
4491 } else {
4492 print __("Attachments:") . " ";
4493 }
4494
4495 $entries = array();
4496
4497 while ($line = db_fetch_assoc($result)) {
4498
4499 $url = $line["content_url"];
4500
4501 $filename = substr($url, strrpos($url, "/")+1);
4502
46f73352 4503 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
ce53e200
AD
4504 $filename . " (" . $line["content_type"] . ")" . "</a>";
4505
4506 array_push($entries, $entry);
4507 }
4508
4509 print join(", ", $entries);
4510
4511 print "</div>";
4512 }
4513
4514 print "</div>";
3de0261a
AD
4515
4516 print "</div>";
4517
4518 }
4519
4520 print "]]></article>";
4521
4522 }
4523
4524 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
4525 $next_unread_feed, $offset) {
4526
52d7e7da
AD
4527 $disable_cache = false;
4528
46921916
AD
4529 $timing_info = getmicrotime();
4530
961f4c73
AD
4531 $topmost_article_ids = array();
4532
ac541432
AD
4533 if (!$offset) {
4534 $offset = 0;
4535 }
3de0261a
AD
4536
4537 if ($subop == "undefined") $subop = "";
4538
4539 if ($subop == "CatchupSelected") {
4540 $ids = split(",", db_escape_string($_GET["ids"]));
4541 $cmode = sprintf("%d", $_GET["cmode"]);
4542
4543 catchupArticlesById($link, $ids, $cmode);
4544 }
4545
4546 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
35bf080c 4547 update_generic_feed($link, $feed, $cat_view, true);
3de0261a
AD
4548 }
4549
4550 if ($subop == "MarkAllRead") {
4551 catchup_feed($link, $feed, $cat_view);
4552
4553 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
4554 if ($next_unread_feed) {
4555 $feed = $next_unread_feed;
4556 }
4557 }
4558 }
4559
4560 if ($feed_id > 0) {
4561 $result = db_query($link,
4562 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
4563
4564 if (db_num_rows($result) == 0) {
4565 print "<div align='center'>".__('Feed not found.')."</div>";
4566 return;
4567 }
4568 }
4569
4570 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 4571
3de0261a
AD
4572 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
4573 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
4574
4575 if (db_num_rows($result) == 1) {
4576 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
4577 } else {
4578 $rtl_content = false;
4579 }
4580
4581 if ($rtl_content) {
4582 $rtl_tag = "dir=\"RTL\"";
4583 } else {
4584 $rtl_tag = "";
4585 }
4586 } else {
4587 $rtl_tag = "";
4588 $rtl_content = false;
4589 }
4590
4591 $script_dt_add = get_script_dt_add();
4592
4593 /// START /////////////////////////////////////////////////////////////////////////////////
4594
4595 $search = db_escape_string($_GET["query"]);
52d7e7da
AD
4596
4597 if ($search) {
4598 $disable_cache = true;
4599 }
4600
3de0261a
AD
4601 $search_mode = db_escape_string($_GET["search_mode"]);
4602 $match_on = db_escape_string($_GET["match_on"]);
4603
4604 if (!$match_on) {
4605 $match_on = "both";
4606 }
4607
4608 $real_offset = $offset * $limit;
4609
46921916
AD
4610 if ($_GET["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
4611
3de0261a
AD
4612 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
4613 $search, $search_mode, $match_on, false, $real_offset);
4614
46921916
AD
4615 if ($_GET["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
4616
3de0261a
AD
4617 $result = $qfh_ret[0];
4618 $feed_title = $qfh_ret[1];
4619 $feed_site_url = $qfh_ret[2];
4620 $last_error = $qfh_ret[3];
f56e3080
AD
4621
4622 if ($feed == -2) {
4623 $feed_site_url = article_publish_url($link);
4624 }
4625
3de0261a
AD
4626 /// STOP //////////////////////////////////////////////////////////////////////////////////
4627
ac541432
AD
4628 if (!$offset) {
4629 print "<div id=\"headlinesContainer\" $rtl_tag>";
3de0261a 4630
ac541432
AD
4631 if (!$result) {
4632 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
4633 return;
4634 }
3de0261a 4635
ac541432
AD
4636 print_headline_subtoolbar($link, $feed_site_url, $feed_title, false,
4637 $rtl_content, $feed, $cat_view, $search, $match_on, $search_mode,
4638 $offset, $limit);
3de0261a 4639
ac541432
AD
4640 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
4641 }
3de0261a 4642
29dfb258
AD
4643 $headlines_count = db_num_rows($result);
4644
3de0261a
AD
4645 if (db_num_rows($result) > 0) {
4646
4647# print "\{$offset}";
4648
ac541432 4649 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4650 print "<table class=\"headlinesList\" id=\"headlinesList\"
4651 cellspacing=\"0\">";
4652 }
4653
4ab4d364
AD
4654 $lnum = $limit*$offset;
4655
3de0261a
AD
4656 error_reporting (DEFAULT_ERROR_LEVEL);
4657
4658 $num_unread = 0;
4659
4660 while ($line = db_fetch_assoc($result)) {
4661
4662 $class = ($lnum % 2) ? "even" : "odd";
4663
4664 $id = $line["id"];
4665 $feed_id = $line["feed_id"];
961f4c73
AD
4666
4667 if (count($topmost_article_ids) < 5) {
4668 array_push($topmost_article_ids, $id);
4669 }
4670
3de0261a
AD
4671 if ($line["last_read"] == "" &&
4672 ($line["unread"] != "t" && $line["unread"] != "1")) {
4673
4674 $update_pic = "<img id='FUPDPIC-$id' src=\"images/updated.png\"
4675 alt=\"Updated\">";
4676 } else {
4677 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
4678 alt=\"Updated\">";
4679 }
4680
4681 if ($line["unread"] == "t" || $line["unread"] == "1") {
4682 $class .= "Unread";
4683 ++$num_unread;
4684 $is_unread = true;
4685 } else {
4686 $is_unread = false;
4687 }
abd8a516
AD
4688
4689 $is_ie = (strpos($_SESSION["client.userAgent"], "MSIE") !== false);
4690
4691 if ($is_ie) {
4692 $mark_img_ext = "gif";
4693 } else {
4694 $mark_img_ext = "png";
4695 }
4696
3de0261a 4697 if ($line["marked"] == "t" || $line["marked"] == "1") {
abd8a516 4698 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_set.$mark_img_ext\"
3de0261a 4699 class=\"markedPic\"
f5e0338d 4700 alt=\"Unstar article\" onclick='javascript:tMark($id)'>";
3de0261a 4701 } else {
abd8a516 4702 $marked_pic = "<img id=\"FMPIC-$id\" src=\"images/mark_unset.$mark_img_ext\"
3de0261a 4703 class=\"markedPic\"
f5e0338d 4704 alt=\"Star article\" onclick='javascript:tMark($id)'>";
3de0261a
AD
4705 }
4706
e4f4b46f 4707 if ($line["published"] == "t" || $line["published"] == "1") {
f5e0338d 4708 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_set.gif\"
e4f4b46f 4709 class=\"markedPic\"
f5e0338d 4710 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
e4f4b46f 4711 } else {
f5e0338d 4712 $published_pic = "<img id=\"FPPIC-$id\" src=\"images/pub_unset.gif\"
e4f4b46f 4713 class=\"markedPic\"
f5e0338d 4714 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
e4f4b46f
AD
4715 }
4716
3de0261a
AD
4717# $content_link = "<a target=\"_new\" href=\"".$line["link"]."\">" .
4718# $line["title"] . "</a>";
4719
4720 $content_link = "<a href=\"javascript:view($id,$feed_id);\">" .
4721 $line["title"] . "</a>";
4722
4723# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
4724# $line["title"] . "</a>";
4725
4726 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
46921916 4727 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3de0261a
AD
4728 } else {
4729 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
46921916 4730 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3de0261a
AD
4731 }
4732
4733 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4734 $content_preview = truncate_string(strip_tags($line["content_preview"]),
4735 100);
4736 }
4737
4738 $entry_author = $line["author"];
4739
4740 if ($entry_author) {
4741 $entry_author = " - by $entry_author";
4742 }
4743
4744 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4745
4746 print "<tr class='$class' id='RROW-$id'>";
4747
67343d9f 4748 print "<td class='hlUpdPic'>$update_pic</td>";
3de0261a
AD
4749
4750 print "<td class='hlSelectRow'>
67343d9f
AD
4751 <input type=\"checkbox\" onclick=\"tSR(this)\"
4752 id=\"RCHK-$id\">
3de0261a
AD
4753 </td>";
4754
4755 print "<td class='hlMarkedPic'>$marked_pic</td>";
e4f4b46f 4756 print "<td class='hlMarkedPic'>$published_pic</td>";
3de0261a 4757
df456bb0
AD
4758# if ($line["feed_title"]) {
4759# print "<td class='hlContent'>$content_link</td>";
4760# print "<td class='hlFeed'>
4761# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4762# truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
4763# } else {
4764
4765 print "<td class='hlContent' valign='middle'>";
4766
4767 print "<a href=\"javascript:view($id,$feed_id);\">" .
4768 $line["title"];
4769
4770 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
4771 if ($content_preview) {
4772 print "<span class=\"contentPreview\"> - $content_preview</span>";
3de0261a 4773 }
3de0261a 4774 }
df456bb0
AD
4775
4776 print "</a>";
4777
4778# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
4779# $line["feed_title"]."</a>
4780
4781 if ($line["feed_title"]) {
4782 print "<span class=\"hlFeed\">
4783 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
4784 $line["feed_title"]."</a>)
4785 </span>";
4786 }
4787
4788
4789 print "</td>";
4790
4791# }
3de0261a
AD
4792
4793 print "<td class=\"hlUpdated\"><nobr>$updated_fmt&nbsp;</nobr></td>";
4794
4795 print "</tr>";
4796
4797 } else {
4798
4799 if ($is_unread) {
4800 $add_class = "Unread";
4801 } else {
4802 $add_class = "";
4803 }
0cacc891
AD
4804
4805 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
4806
4807 if ($expand_cdm) {
4808 $cdm_cstyle = "";
4809 } else {
4810 $cdm_cstyle = "style=\"display : none\"";
4811 }
4812
e4914b62
AD
4813 print "<div class=\"cdmArticle$add_class\"
4814 id=\"RROW-$id\" onmouseover='cdmMouseIn(this)'
4815 onmouseout='cdmMouseOut(this)'>";
3de0261a
AD
4816
4817 print "<div class=\"cdmHeader\">";
4818
4819 print "<div class=\"articleUpdated\">$updated_fmt</div>";
4820
4821 print "<a class=\"title\"
4822 onclick=\"javascript:toggleUnread($id, 0)\"
3fc2eed5 4823 target=\"_new\" href=\"".$line["link"]."\">".$line["title"]."</a>";
3de0261a
AD
4824
4825 print $entry_author;
4826
0cacc891
AD
4827 if (!$expand_cdm) {
4828 print "&nbsp;<a id=\"CICH-$id\"
4829 href=\"javascript:cdmExpandArticle($id)\">
4830 (".__('Show article').")</a>";
4831 }
4832
4833
3de0261a
AD
4834 if ($line["feed_title"]) {
4835 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
4836 }
4837
4838 print "</div>";
4839
3fc2eed5
AD
4840 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW')) {
4841 $line["content_preview"] = preg_replace("/href=/i",
4842 "target=\"_new\" href=", $line["content_preview"]);
4843 }
4844
0cacc891 4845 print "<div class=\"cdmContent\" id=\"CICD-$id\" $cdm_cstyle>";
a04c8e8d 4846
0cacc891 4847// print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
a04c8e8d 4848 print $line["content_preview"];
3c66b582
AD
4849
4850 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4851 post_id = '$id'");
4852
4853 if (db_num_rows($e_result) > 0) {
4854 print "<div class=\"cdmEnclosures\">";
4855
4856 if (db_num_rows($e_result) == 1) {
4857 print __("Attachment:") . " ";
4858 } else {
4859 print __("Attachments:") . " ";
4860 }
4861
4862 $entries = array();
4863
4864 while ($e_line = db_fetch_assoc($e_result)) {
4865
4866 $url = $e_line["content_url"];
4867
4868 $filename = substr($url, strrpos($url, "/")+1);
4869
46f73352 4870 $entry = "<a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
3c66b582
AD
4871 $filename . " (" . $e_line["content_type"] . ")" . "</a>";
4872
4873 array_push($entries, $entry);
4874 }
4875
4876 print join(", ", $entries);
4877
4878 print "</div>";
4879 }
4880
0cacc891
AD
4881 print "<br clear='both'>";
4882// print "</div>";
a04c8e8d 4883
0cacc891 4884/* if (!$expand_cdm) {
12f5d8fe
AD
4885 print "<a id=\"CICH-$id\"
4886 href=\"javascript:cdmExpandArticle($id)\">
4887 Show article</a>";
0cacc891 4888 } */
a04c8e8d 4889
0cacc891 4890 print "</div>";
3de0261a 4891
e2ccbfab 4892 print "<div class=\"cdmFooter\"><span class='s0'>";
3de0261a 4893
e2ccbfab 4894 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
3de0261a 4895
e2ccbfab
AD
4896 print __("Select:").
4897 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3de0261a
AD
4898 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
4899
e2ccbfab 4900 print "</span><span class='s1'>$marked_pic</span> ";
e4f4b46f 4901 print "<span class='s1'>$published_pic</span> ";
e2ccbfab 4902
3de0261a
AD
4903 $tags = get_article_tags($link, $id);
4904
4905 $tags_str = "";
22d1f3db 4906 $full_tags_str = "";
d735ebd2 4907 $num_tags = 0;
3de0261a
AD
4908
4909 foreach ($tags as $tag) {
4910 $num_tags++;
22d1f3db
AD
4911 $full_tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4912 if ($num_tags < 5) {
4913 $tags_str .= "<a href=\"javascript:viewfeed('$tag')\">$tag</a>, ";
4914 } else if ($num_tags == 5) {
66a251f9 4915 $tags_str .= "&hellip;";
22d1f3db 4916 }
3de0261a
AD
4917 }
4918
4919 $tags_str = preg_replace("/, $/", "", $tags_str);
22d1f3db
AD
4920 $full_tags_str = preg_replace("/, $/", "", $full_tags_str);
4921
66a251f9 4922 $all_tags_div = "<span class='cdmAllTagsCtr'>&hellip;<div class='cdmAllTags'>All Tags: $full_tags_str</div></span>";
22d1f3db
AD
4923
4924 $tags_str = preg_replace("/\.\.\.$/", "$all_tags_div", $tags_str);
4925
3de0261a
AD
4926
4927 if ($tags_str == "") $tags_str = "no tags";
e2ccbfab
AD
4928
4929// print "<img src='images/tag.png' class='markedPic'>";
4930
5f014cf1
AD
4931 print "<span class='s1'>
4932 <img class='tagsPic' src='images/tag.png' alt='Tags'
4933 title='Tags'> $tags_str <a title=\"Edit tags for this article\"
3de0261a
AD
4934 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
4935
e2ccbfab 4936 print "</span>";
3de0261a 4937
e2ccbfab
AD
4938 print "<span class='s2'>Toggle: <a class=\"cdmToggleLink\"
4939 href=\"javascript:toggleUnread($id)\">
4940 Unread</a></span>";
3de0261a 4941
e2ccbfab 4942 print "</div>";
3de0261a
AD
4943 print "</div>";
4944
4945 }
4946
4947 ++$lnum;
4948 }
4949
ac541432 4950 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
4951 print "</table>";
4952 }
4953
4954// print_headline_subtoolbar($link,
4955// "javascript:catchupPage()", "Mark page as read", true, $rtl_content);
4956
4957
4958 } else {
ac541432 4959 if (!$offset) print "<div class='whiteBox'>".__('No articles found.')."</div>";
3de0261a
AD
4960 }
4961
ac541432
AD
4962 if (!$offset) {
4963 print "</div>";
4964 print "</div>";
4965 }
3de0261a 4966
52d7e7da 4967 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache);
3de0261a 4968 }
0979b696
AD
4969
4970// from here: http://www.roscripts.com/Create_tag_cloud-71.html
4971
4972 function printTagCloud($link) {
35a03bdd
AD
4973
4974 /* get first ref_id to count from */
4975
dcac082b
AD
4976 /*
4977
35a03bdd
AD
4978 $query = "";
4979
4980 if (DB_TYPE == "pgsql") {
4981 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4982 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4983 AND date_entered > NOW() - INTERVAL '30 days'";
4984 } else {
4985 $query = "SELECT MIN(id) AS id FROM ttrss_user_entries, ttrss_entries
4986 WHERE int_id = id AND owner_uid = ".$_SESSION["uid"]."
4987 AND date_entered > DATE_SUB(NOW(), INTERVAL 30 DAY)";
4988 }
4989
4990 $result = db_query($link, $query);
dcac082b 4991 $first_id = db_fetch_result($result, 0, "id"); */
35a03bdd 4992
dcac082b 4993 //AND post_int_id >= '$first_id'
0979b696
AD
4994 $query = "SELECT tag_name, COUNT(post_int_id) AS count
4995 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 4996 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
4997
4998 $result = db_query($link, $query);
4999
5000 $tags = array();
5001
5002 while ($line = db_fetch_assoc($result)) {
5003 $tags[$line["tag_name"]] = $line["count"];
5004 }
5005
5006 ksort($tags);
5007
5008 $max_size = 32; // max font size in pixels
4548a580 5009 $min_size = 11; // min font size in pixels
0979b696
AD
5010
5011 // largest and smallest array values
5012 $max_qty = max(array_values($tags));
5013 $min_qty = min(array_values($tags));
5014
5015 // find the range of values
5016 $spread = $max_qty - $min_qty;
5017 if ($spread == 0) { // we don't want to divide by zero
5018 $spread = 1;
5019 }
5020
5021 // set the font-size increment
5022 $step = ($max_size - $min_size) / ($spread);
5023
5024 // loop through the tag array
5025 foreach ($tags as $key => $value) {
5026 // calculate font-size
5027 // find the $value in excess of $min_qty
5028 // multiply by the font-size increment ($size)
5029 // and add the $min_size set above
5030 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
5031
5032 $key_escaped = str_replace("'", "\\'", $key);
5033
5034 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
0979b696
AD
5035 $size . "px\" title=\"$value articles tagged with " .
5036 $key . '">' . $key . '</a> ';
5037 }
5038 }
46921916
AD
5039
5040 function print_checkpoint($n, $s) {
5041 $ts = getmicrotime();
5042 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5043 return $ts;
5044 }
14b6c54b
AD
5045
5046 function sanitize_tag($tag) {
5047 $tag = trim($tag);
5048
5049 $tag = mb_strtolower($tag, 'utf-8');
5050
0c3d1c68
AD
5051 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5052
5053// $tag = str_replace('"', "", $tag);
5054// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
5055 $tag = str_replace("technorati tag: ", "", $tag);
5056
5057 return $tag;
5058 }
e4f4b46f
AD
5059
5060 function generate_publish_key() {
5061 return sha1(uniqid(rand(), true));
5062 }
5063
f56e3080
AD
5064 function article_publish_url($link) {
5065
17a756d1 5066 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
f56e3080
AD
5067
5068 $url_path .= "?op=publish&key=" . get_pref($link, "_PREFS_PUBLISH_KEY");
5069
5070 return $url_path;
5071 }
5072
d1f0c584
AD
5073 function clear_feed_articles($link, $id) {
5074 $result = db_query($link, "DELETE FROM ttrss_user_entries
a8ae1b9a 5075 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
d1f0c584
AD
5076
5077 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5078 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
5079 }
5080
755a43ee
AD
5081 function add_feed_url() {
5082 $url_path = 'http://' . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
5083 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5084 return $url_path;
5085 }
5086
1a9f4d3c
AD
5087 function encrypt_password($pass, $login = '') {
5088 if ($login) {
5089 return "SHA1X:" . sha1("$login:$pass");
5090 } else {
5091 return "SHA1:" . sha1($pass);
5092 }
5093 }
5094
40d13c28 5095?>