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