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