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