]> git.wh0rd.org - tt-rss.git/blame - functions.php
better image attachment detection for inlining
[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
7210613a 1660 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1661 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
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
AD
2237
2238 if (flock($fp, LOCK_EX | LOCK_NB)) {
2239 return $fp;
2240 } else {
2241 return false;
2242 }
2243 }
2244
bf7fcde8 2245 function make_stampfile($filename) {
cfa43e02 2246 $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
bf7fcde8 2247
8e00ae9b 2248 if (flock($fp, LOCK_EX | LOCK_NB)) {
bf7fcde8 2249 fwrite($fp, time() . "\n");
8e00ae9b 2250 flock($fp, LOCK_UN);
bf7fcde8
AD
2251 fclose($fp);
2252 return true;
2253 } else {
2254 return false;
2255 }
2256 }
2257
8e00ae9b
AD
2258 function read_stampfile($filename) {
2259
2260 error_reporting(0);
cfa43e02 2261 $fp = fopen(LOCK_DIRECTORY . "/$filename", "r");
8e00ae9b
AD
2262 error_reporting (DEFAULT_ERROR_LEVEL);
2263
e89aed7b
AD
2264 if ($fp) {
2265 if (flock($fp, LOCK_EX)) {
2266 $stamp = fgets($fp);
2267 flock($fp, LOCK_UN);
2268 fclose($fp);
2269 return $stamp;
2270 } else {
2271 return false;
2272 }
8e00ae9b
AD
2273 } else {
2274 return false;
2275 }
2276 }
bf7fcde8 2277
894ebcf5
AD
2278 function sql_random_function() {
2279 if (DB_TYPE == "mysql") {
2280 return "RAND()";
2281 } else {
2282 return "RANDOM()";
2283 }
2284 }
2285
c7e51de1
AD
2286 function catchup_feed($link, $feed, $cat_view, $owner_uid) {
2287
2288 if (!$owner_uid) $owner_uid = $_SESSION['uid'];
88040f57
AD
2289
2290 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
22fdebff 2291
23aa0d16
AD
2292 if ($cat_view) {
2293
72a2f4f5 2294 if ($feed >= 0) {
f9fca8cb
AD
2295
2296 if ($feed > 0) {
2297 $cat_qpart = "cat_id = '$feed'";
2298 } else {
2299 $cat_qpart = "cat_id IS NULL";
2300 }
23aa0d16 2301
f9fca8cb 2302 $tmp_result = db_query($link, "SELECT id
c7e51de1 2303 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = $owner_uid");
f9fca8cb
AD
2304
2305 while ($tmp_line = db_fetch_assoc($tmp_result)) {
23aa0d16 2306
f9fca8cb 2307 $tmp_feed = $tmp_line["id"];
23aa0d16 2308
f9fca8cb
AD
2309 db_query($link, "UPDATE ttrss_user_entries
2310 SET unread = false,last_read = NOW()
c7e51de1 2311 WHERE feed_id = '$tmp_feed' AND owner_uid = $owner_uid");
f9fca8cb
AD
2312 }
2313 } else if ($feed == -2) {
23aa0d16 2314
6f69764c
AD
2315
2316 db_query($link, "UPDATE ttrss_user_entries
2317 SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
2318 FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0
c7e51de1 2319 AND unread = true AND owner_uid = $owner_uid");
23aa0d16
AD
2320 }
2321
2322 } else if ($feed > 0) {
2323
2324 $tmp_result = db_query($link, "SELECT id
2325 FROM ttrss_feeds WHERE parent_feed = '$feed'
2326 ORDER BY cat_id,title");
2327
2328 $parent_ids = array();
2329
2330 if (db_num_rows($tmp_result) > 0) {
2331 while ($p = db_fetch_assoc($tmp_result)) {
2332 array_push($parent_ids, "feed_id = " . $p["id"]);
2333 }
2334
2335 $children_qpart = implode(" OR ", $parent_ids);
2336
2337 db_query($link, "UPDATE ttrss_user_entries
2338 SET unread = false,last_read = NOW()
2339 WHERE (feed_id = '$feed' OR $children_qpart)
c7e51de1 2340 AND owner_uid = $owner_uid");
23aa0d16
AD
2341
2342 } else {
2343 db_query($link, "UPDATE ttrss_user_entries
2344 SET unread = false,last_read = NOW()
c7e51de1 2345 WHERE feed_id = '$feed' AND owner_uid = $owner_uid");
23aa0d16
AD
2346 }
2347
2348 } else if ($feed < 0 && $feed > -10) { // special, like starred
2349
2350 if ($feed == -1) {
2351 db_query($link, "UPDATE ttrss_user_entries
2352 SET unread = false,last_read = NOW()
c7e51de1 2353 WHERE marked = true AND owner_uid = $owner_uid");
23aa0d16 2354 }
e4f4b46f
AD
2355
2356 if ($feed == -2) {
2357 db_query($link, "UPDATE ttrss_user_entries
2358 SET unread = false,last_read = NOW()
c7e51de1 2359 WHERE published = true AND owner_uid = $owner_uid");
e4f4b46f
AD
2360 }
2361
2d24f032
AD
2362 if ($feed == -3) {
2363
c1d7e6c3
AD
2364 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE");
2365
2d24f032 2366 if (DB_TYPE == "pgsql") {
7608b38a 2367 $match_part = "updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2368 } else {
7608b38a 2369 $match_part = "updated > DATE_SUB(NOW(),
c1d7e6c3 2370 INTERVAL $intl HOUR) ";
2d24f032
AD
2371 }
2372
1f3335dc
AD
2373 $result = db_query($link, "SELECT id FROM ttrss_entries,
2374 ttrss_user_entries WHERE $match_part AND
2375 unread = true AND
2376 ttrss_user_entries.ref_id = ttrss_entries.id AND
c7e51de1 2377 owner_uid = $owner_uid");
1f3335dc
AD
2378
2379 $affected_ids = array();
2380
2381 while ($line = db_fetch_assoc($result)) {
2382 array_push($affected_ids, $line["id"]);
2383 }
2384
2385 catchupArticlesById($link, $affected_ids, 0);
2d24f032
AD
2386 }
2387
3584cb11
AD
2388 if ($feed == -4) {
2389 db_query($link, "UPDATE ttrss_user_entries
2390 SET unread = false,last_read = NOW()
c7e51de1 2391 WHERE owner_uid = $owner_uid");
3584cb11
AD
2392 }
2393
23aa0d16
AD
2394 } else if ($feed < -10) { // label
2395
23aa0d16
AD
2396 $label_id = -$feed - 11;
2397
933ba4ee 2398 db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2
338c238d
AD
2399 SET unread = false, last_read = NOW()
2400 WHERE label_id = '$label_id' AND unread = true
c7e51de1 2401 AND owner_uid = '$owner_uid' AND ref_id = article_id");
23aa0d16 2402
23aa0d16 2403 }
ad0056a8 2404
c7e51de1 2405 ccache_update($link, $feed, $owner_uid, $cat_view);
ad0056a8 2406
23aa0d16
AD
2407 } else { // tag
2408 db_query($link, "BEGIN");
2409
2410 $tag_name = db_escape_string($feed);
2411
2412 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
c7e51de1 2413 WHERE tag_name = '$tag_name' AND owner_uid = $owner_uid");
23aa0d16
AD
2414
2415 while ($line = db_fetch_assoc($result)) {
2416 db_query($link, "UPDATE ttrss_user_entries SET
2417 unread = false, last_read = NOW()
2418 WHERE int_id = " . $line["post_int_id"]);
2419 }
2420 db_query($link, "COMMIT");
2421 }
2422 }
2423
35bf080c 2424 function update_generic_feed($link, $feed, $cat_view, $force_update = false) {
23aa0d16
AD
2425 if ($cat_view) {
2426
2427 if ($feed > 0) {
2428 $cat_qpart = "cat_id = '$feed'";
2429 } else {
2430 $cat_qpart = "cat_id IS NULL";
2431 }
2432
c633e370 2433 $tmp_result = db_query($link, "SELECT id FROM ttrss_feeds
23aa0d16
AD
2434 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
2435
2436 while ($tmp_line = db_fetch_assoc($tmp_result)) {
571dad82 2437 $feed_id = $tmp_line["id"];
c633e370 2438 update_rss_feed($link, $feed_id, $force_update);
23aa0d16
AD
2439 }
2440
2441 } else {
c633e370 2442 update_rss_feed($link, $feed, $force_update);
23aa0d16
AD
2443 }
2444 }
a9cb1f83 2445
4ffa126e 2446 function getAllCounters($link, $omode = "flc", $active_feed = false) {
cf4d339c 2447
e33fe293 2448 if (!$omode) $omode = "flc";
d9e4bba0 2449
e33fe293 2450 getGlobalCounters($link);
0a6e5382
AD
2451 getVirtCounters($link);
2452
e33fe293 2453 if (strchr($omode, "l")) getLabelCounters($link);
3809b278 2454 if (strchr($omode, "f")) getFeedCounters($link, $active_feed);
e33fe293
AD
2455 if (strchr($omode, "t")) getTagCounters($link);
2456 if (strchr($omode, "c")) {
2457 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2458 getCategoryCounters($link);
cf4d339c 2459 }
e33fe293 2460 }
a9cb1f83
AD
2461 }
2462
2463 function getCategoryCounters($link) {
bba7c4bf
AD
2464 # two special categories are -1 and -2 (all virtuals; all labels)
2465
b2531a28 2466/* $ctr = getCategoryUnread($link, -1);
bba7c4bf 2467
b2531a28 2468 print "<counter type=\"category\" id=\"-1\" counter=\"$ctr\"/>"; */
bba7c4bf 2469
b2531a28 2470 $ctr = getCategoryUnread($link, -2);
bba7c4bf
AD
2471
2472 print "<counter type=\"category\" id=\"-2\" counter=\"$ctr\"/>";
2473
14073c0a
AD
2474 $age_qpart = getMaxAgeSubquery();
2475
31375163
AD
2476 $result = db_query($link, "SELECT id AS cat_id, value AS unread
2477 FROM ttrss_feed_categories, ttrss_cat_counters_cache
2478 WHERE ttrss_cat_counters_cache.feed_id = id AND
2479 ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
2480
2481 while ($line = db_fetch_assoc($result)) {
22fdebff 2482 $line["cat_id"] = (int) $line["cat_id"];
8a4c759e 2483
a9cb1f83
AD
2484 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
2485 $line["unread"]."\"/>";
2486 }
d232a40f
AD
2487
2488 /* Special case: NULL category doesn't actually exist in the DB */
2489
2490 print "<counter type=\"category\" id=\"0\" counter=\"".
2491 ccache_find($link, 0, $_SESSION["uid"], true)."\"/>";
2492
a9cb1f83
AD
2493 }
2494
b6d486a3
AD
2495 function getCategoryUnread($link, $cat, $owner_uid = false) {
2496
2497 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
f295c368 2498
bba7c4bf 2499 if ($cat >= 0) {
18664970 2500
bba7c4bf
AD
2501 if ($cat != 0) {
2502 $cat_query = "cat_id = '$cat'";
2503 } else {
2504 $cat_query = "cat_id IS NULL";
2505 }
14073c0a
AD
2506
2507 $age_qpart = getMaxAgeSubquery();
2508
bba7c4bf 2509 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
b6d486a3 2510 AND owner_uid = " . $owner_uid);
bba7c4bf
AD
2511
2512 $cat_feeds = array();
2513 while ($line = db_fetch_assoc($result)) {
2514 array_push($cat_feeds, "feed_id = " . $line["id"]);
2515 }
2516
2517 if (count($cat_feeds) == 0) return 0;
2518
2519 $match_part = implode(" OR ", $cat_feeds);
2520
2521 $result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a
AD
2522 FROM ttrss_user_entries,ttrss_entries
2523 WHERE unread = true AND ($match_part) AND id = ref_id
b6d486a3 2524 AND $age_qpart AND owner_uid = " . $owner_uid);
bba7c4bf
AD
2525
2526 $unread = 0;
2527
2528 # this needs to be rewritten
2529 while ($line = db_fetch_assoc($result)) {
2530 $unread += $line["unread"];
2531 }
2532
2533 return $unread;
2534 } else if ($cat == -1) {
59e15af4 2535 return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0);
bba7c4bf 2536 } else if ($cat == -2) {
f295c368 2537
b2531a28
AD
2538 $result = db_query($link, "
2539 SELECT COUNT(unread) AS unread FROM
2540 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2541 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
2542 ttrss_labels2.owner_uid = '$owner_uid'
117335bf 2543 AND unread = true AND feed_id = ttrss_feeds.id
b2531a28 2544 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4 2545
b2531a28 2546 $unread = db_fetch_result($result, 0, "unread");
f295c368 2547
b2531a28 2548 return $unread;
f295c368 2549
ceb30ba4 2550 }
f295c368
AD
2551 }
2552
14073c0a
AD
2553 function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) {
2554 if (DB_TYPE == "pgsql") {
2555 return "ttrss_entries.date_entered >
2556 NOW() - INTERVAL '$days days'";
2557 } else {
2558 return "ttrss_entries.date_entered >
2559 DATE_SUB(NOW(), INTERVAL $days DAY)";
2560 }
2561 }
2562
f295c368 2563 function getFeedUnread($link, $feed, $is_cat = false) {
2627f2d0 2564 return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]);
bdb7369b
AD
2565 }
2566
ceb30ba4
AD
2567 function getLabelUnread($link, $label_id, $owner_uid = false) {
2568 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2569
2570 $result = db_query($link, "
0112162d 2571 SELECT COUNT(unread) AS unread FROM
b0f24af1
AD
2572 ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds
2573 WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND
2574 ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id'
117335bf 2575 AND unread = true AND feed_id = ttrss_feeds.id
933ba4ee 2576 AND ttrss_user_entries.owner_uid = '$owner_uid'");
ceb30ba4
AD
2577
2578 if (db_num_rows($result) != 0) {
2579 return db_fetch_result($result, 0, "unread");
2580 } else {
2581 return 0;
2582 }
2583 }
2584
2627f2d0
AD
2585 function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false,
2586 $owner_uid = false) {
2587
22fdebff 2588 $n_feed = (int) $feed;
f295c368 2589
2627f2d0
AD
2590 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
2591
bdb7369b
AD
2592 if ($unread_only) {
2593 $unread_qpart = "unread = true";
2594 } else {
2595 $unread_qpart = "true";
2596 }
2597
14073c0a
AD
2598 $age_qpart = getMaxAgeSubquery();
2599
f295c368 2600 if ($is_cat) {
b6d486a3 2601 return getCategoryUnread($link, $n_feed, $owner_uid);
326469fc
AD
2602 } if ($feed != "0" && $n_feed == 0) {
2603
2604 $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id)
2605 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2606 AND ref_id = id AND $age_qpart
2607 AND $unread_qpart)) AS count FROM ttrss_tags
2608 WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
2609 return db_fetch_result($result, 0, "count");
2610
f295c368 2611 } else if ($n_feed == -1) {
a9cb1f83 2612 $match_part = "marked = true";
e4f4b46f
AD
2613 } else if ($n_feed == -2) {
2614 $match_part = "published = true";
2d24f032
AD
2615 } else if ($n_feed == -3) {
2616 $match_part = "unread = true";
2617
b71e188e 2618 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 2619
2d24f032 2620 if (DB_TYPE == "pgsql") {
7608b38a 2621 $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 2622 } else {
7608b38a 2623 $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032 2624 }
b2531a28
AD
2625 } else if ($n_feed == -4) {
2626 $match_part = "true";
e04c18a2 2627 } else if ($n_feed >= 0) {
831ff047 2628
e8b8485f
AD
2629 $result = db_query($link, "SELECT id FROM ttrss_feeds
2630 WHERE parent_feed = '$n_feed'
2627f2d0 2631 AND owner_uid = " . $owner_uid);
831ff047
AD
2632
2633 if (db_num_rows($result) > 0) {
4919fb42 2634
831ff047
AD
2635 $linked_feeds = array();
2636 while ($line = db_fetch_assoc($result)) {
2637 array_push($linked_feeds, "feed_id = " . $line["id"]);
2638 }
e8b8485f
AD
2639
2640 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
2641
2642 $match_part = implode(" OR ", $linked_feeds);
2643
dbfc4365 2644 $tmp_result = db_query($link, "SELECT COUNT(int_id) AS unread
14073c0a 2645 FROM ttrss_user_entries,ttrss_entries
bdb7369b 2646 WHERE $unread_qpart AND
14073c0a
AD
2647 ttrss_user_entries.ref_id = ttrss_entries.id AND
2648 $age_qpart AND
2649 ($match_part) AND
2627f2d0 2650 owner_uid = " . $owner_uid);
4919fb42
AD
2651
2652 $unread = 0;
2653
2654 # this needs to be rewritten
dbfc4365 2655 while ($line = db_fetch_assoc($tmp_result)) {
4919fb42
AD
2656 $unread += $line["unread"];
2657 }
2658
2659 return $unread;
2660
831ff047 2661 } else {
e04c18a2
AD
2662 if ($n_feed != 0) {
2663 $match_part = "feed_id = '$n_feed'";
2664 } else {
2665 $match_part = "feed_id IS NULL";
2666 }
831ff047 2667 }
a9cb1f83 2668 } else if ($feed < -10) {
318260cc 2669
a9cb1f83
AD
2670 $label_id = -$feed - 11;
2671
ceb30ba4 2672 return getLabelUnread($link, $label_id, $owner_uid);
a9cb1f83 2673
a9cb1f83
AD
2674 }
2675
2676 if ($match_part) {
e04c18a2
AD
2677
2678 if ($n_feed != 0) {
2679 $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries";
117335bf 2680 $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
2681 } else {
2682 $from_qpart = "ttrss_user_entries,ttrss_entries";
2683 }
2684
dbfc4365 2685 $query = "SELECT count(int_id) AS unread
e04c18a2 2686 FROM $from_qpart WHERE
88040f57 2687 ttrss_user_entries.ref_id = ttrss_entries.id AND
14073c0a 2688 $age_qpart AND
dbfc4365
AD
2689 $feeds_qpart
2690 $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
2691
2692 $result = db_query($link, $query);
a9cb1f83
AD
2693
2694 } else {
2695
2696 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
14073c0a 2697 FROM ttrss_tags,ttrss_user_entries,ttrss_entries
828f22b7 2698 WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
bdb7369b 2699 AND $unread_qpart AND $age_qpart AND
2627f2d0 2700 ttrss_tags.owner_uid = " . $owner_uid);
a9cb1f83
AD
2701 }
2702
2703 $unread = db_fetch_result($result, 0, "unread");
cfb02131 2704
a9cb1f83
AD
2705 return $unread;
2706 }
2707
f3acc32e
AD
2708 function getGlobalUnread($link, $user_id = false) {
2709
2710 if (!$user_id) {
2711 $user_id = $_SESSION["uid"];
2712 }
2713
8a4c759e
AD
2714 $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache
2715 WHERE owner_uid = '$user_id' AND feed_id > 0");
2716
2717 $c_id = db_fetch_result($result, 0, "c_id");
2718
a9cb1f83
AD
2719 return $c_id;
2720 }
2721
2722 function getGlobalCounters($link, $global_unread = -1) {
2723 if ($global_unread == -1) {
2724 $global_unread = getGlobalUnread($link);
2725 }
7bf7e4d3
AD
2726 print "<counter type=\"global\" id='global-unread'
2727 counter='$global_unread'/>";
2728
2729 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2730 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2731
2732 $subscribed_feeds = db_fetch_result($result, 0, "fn");
2733
2734 print "<counter type=\"global\" id='subscribed-feeds'
2735 counter='$subscribed_feeds'/>";
2736
a9cb1f83
AD
2737 }
2738
95004daf
AD
2739 function getSubscribedFeeds($link) {
2740 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
2741 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
2742
2743 return db_fetch_result($result, 0, "fn");
2744 }
2745
3809b278 2746 function getTagCounters($link) {
a9cb1f83 2747
14073c0a
AD
2748 $age_qpart = getMaxAgeSubquery();
2749
a9cb1f83 2750 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
2751 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
2752 AND ref_id = id AND $age_qpart
a9cb1f83 2753 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
2754 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
2755 ORDER BY count DESC LIMIT 55");
a9cb1f83
AD
2756
2757 $tags = array();
2758
2759 while ($line = db_fetch_assoc($result)) {
2760 $tags[$line["tag_name"]] += $line["count"];
2761 }
2762
2763 foreach (array_keys($tags) as $tag) {
2764 $unread = $tags[$tag];
a9cb1f83 2765 $tag = htmlspecialchars($tag);
3809b278 2766 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
a9cb1f83 2767 }
a9cb1f83
AD
2768 }
2769
3809b278 2770 function getVirtCounters($link, $ret_mode = false) {
a9cb1f83 2771
ef393de7 2772 $ret_arr = array();
bdb7369b 2773
e04c18a2 2774 for ($i = 0; $i >= -4; $i--) {
bdb7369b 2775
ceb30ba4
AD
2776 $count = getFeedUnread($link, $i);
2777
2778 if (!$ret_mode) {
2779
2780 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2781 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $i) . " total)\"";
2782 } else {
2783 $xmsg_part = "";
2784 }
bdb7369b 2785
ceb30ba4 2786 print "<counter type=\"label\" id=\"$i\" counter=\"$count\" $xmsg_part/>";
bdb7369b 2787 } else {
ceb30ba4
AD
2788 $ret_arr[$i]["counter"] = $count;
2789 $ret_arr[$i]["description"] = getFeedTitle($link, $i);
bdb7369b 2790 }
0a6e5382
AD
2791 }
2792
2793 return $ret_arr;
2794 }
2795
3809b278 2796 function getLabelCounters($link, $ret_mode = false) {
0a6e5382
AD
2797
2798 $age_qpart = getMaxAgeSubquery();
2799
3809b278 2800 $owner_uid = $_SESSION["uid"];
bdb7369b 2801
3809b278
AD
2802 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2
2803 WHERE owner_uid = '$owner_uid'");
2804
2805 while ($line = db_fetch_assoc($result)) {
2d24f032 2806
3809b278 2807 $id = -$line["id"] - 11;
e4f4b46f 2808
3809b278
AD
2809 $label_name = $line["caption"];
2810 $count = getFeedUnread($link, $id);
abd9b165 2811
3809b278
AD
2812 if (!$ret_mode) {
2813
2814 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2815 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
2816 } else {
2817 $xmsg_part = "";
ef393de7 2818 }
a9cb1f83 2819
3809b278 2820 print "<counter type=\"label\" id=\"$id\" counter=\"$count\" $xmsg_part/>";
ef393de7 2821
3809b278
AD
2822 } else {
2823 $ret_arr[$id]["counter"] = $count;
2824 $ret_arr[$id]["description"] = $label_name;
2825 }
2826 }
2827
ef393de7 2828 return $ret_arr;
a9cb1f83
AD
2829 }
2830
3809b278 2831 function getFeedCounters($link, $active_feed = false) {
a9cb1f83 2832
14073c0a
AD
2833 $age_qpart = getMaxAgeSubquery();
2834
8a4c759e
AD
2835 $query = "SELECT ttrss_feeds.id,
2836 ttrss_feeds.title,
2837 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
de0a2122
AD
2838 last_error, value AS count
2839 FROM ttrss_feeds, ttrss_counters_cache
8a4c759e
AD
2840 WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
2841 AND parent_feed IS NULL
55e01d7e 2842 AND ttrss_counters_cache.feed_id = id";
a9cb1f83 2843
14073c0a 2844 $result = db_query($link, $query);
a9cb1f83
AD
2845 $fctrs_modified = false;
2846
fb1fb4ab
AD
2847 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
2848
a9cb1f83
AD
2849 while ($line = db_fetch_assoc($result)) {
2850
2851 $id = $line["id"];
de0a2122 2852 $count = $line["count"];
a9cb1f83 2853 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
2854
2855 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
2856 $last_updated = smart_date_time(strtotime($line["last_updated"]));
2857 } else {
2858 $last_updated = date($short_date, strtotime($line["last_updated"]));
2859 }
2860
588fb13b
AD
2861 $last_updated = htmlspecialchars($last_updated);
2862
7defa089 2863 $has_img = feed_has_icon($id);
a9cb1f83 2864
de0a2122
AD
2865 $tmp_result = db_query($link,
2866 "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache
2867 WHERE parent_feed = '$id' AND feed_id = id");
2868
2869 $count += db_fetch_result($tmp_result, 0, "unread");
a9cb1f83 2870
3809b278
AD
2871 if ($last_error) {
2872 $error_part = "error=\"$last_error\"";
2873 } else {
2874 $error_part = "";
2875 }
a9cb1f83 2876
3809b278
AD
2877 if ($has_img) {
2878 $has_img_part = "hi=\"$has_img\"";
2879 } else {
2880 $has_img_part = "";
2881 }
4ffa126e 2882
3809b278
AD
2883 if ($active_feed && $id == $active_feed) {
2884 $has_title_part = "title=\"" . htmlspecialchars($line["title"]) . "\"";
2885 } else {
2886 $has_title_part = "";
2887 }
bdb7369b 2888
3809b278
AD
2889 if (get_pref($link, 'EXTENDED_FEEDLIST')) {
2890 $xmsg_part = "xmsg=\"(" . getFeedArticles($link, $id) . " total)\"";
a9cb1f83 2891 }
a9cb1f83 2892
3809b278 2893 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\" $xmsg_part $has_title_part/>";
a9cb1f83
AD
2894 }
2895 }
2896
1b758780 2897 function get_script_dt_add() {
3ac60b76 2898/* if (strpos(VERSION, ".99") === false) {
1b758780
AD
2899 return VERSION;
2900 } else {
2901 return time();
3ac60b76
AD
2902 } */
2903 return time();
1b758780
AD
2904 }
2905
6e7f8d26
AD
2906 function get_pgsql_version($link) {
2907 $result = db_query($link, "SELECT version() AS version");
2908 $version = split(" ", db_fetch_result($result, 0, "version"));
2909 return $version[1];
2910 }
2911
af106b0e
AD
2912 function print_error_xml($code, $add_msg = "") {
2913 global $ERRORS;
2914
2915 $error_msg = $ERRORS[$code];
2916
2917 if ($add_msg) {
2918 $error_msg = "$error_msg; $add_msg";
2919 }
2920
4c2abbc1 2921 print "<rpc-reply>";
af106b0e 2922 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2923 print "</rpc-reply>";
af106b0e 2924 }
956c7629 2925
a5819bb3 2926 function subscribe_to_feed($link, $url, $cat_id = 0,
f27de515 2927 $auth_login = '', $auth_pass = '') {
bb0f29a4 2928
a5819bb3 2929 $parts = parse_url($url);
c91c2249 2930
a5819bb3
AD
2931 if (!validate_feed_url($url)) return 2;
2932
2933 if ($parts['scheme'] == 'feed') $parts['scheme'] = 'http';
b3dfe8ba 2934
a5819bb3 2935 $url = make_url_from_parts($parts);
bb0f29a4 2936
956c7629
AD
2937 if ($cat_id == "0" || !$cat_id) {
2938 $cat_qpart = "NULL";
2939 } else {
2940 $cat_qpart = "'$cat_id'";
2941 }
2942
2943 $result = db_query($link,
2944 "SELECT id FROM ttrss_feeds
a5819bb3 2945 WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
956c7629
AD
2946
2947 if (db_num_rows($result) == 0) {
2948
2949 $result = db_query($link,
f27de515
AD
2950 "INSERT INTO ttrss_feeds
2951 (owner_uid,feed_url,title,cat_id, auth_login,auth_pass)
a5819bb3 2952 VALUES ('".$_SESSION["uid"]."', '$url',
f27de515 2953 '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass')");
956c7629
AD
2954
2955 $result = db_query($link,
a5819bb3 2956 "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
f27de515 2957 AND owner_uid = " . $_SESSION["uid"]);
956c7629
AD
2958
2959 $feed_id = db_fetch_result($result, 0, "id");
2960
2961 if ($feed_id) {
c633e370 2962 update_rss_feed($link, $feed_id, true);
956c7629
AD
2963 }
2964
a5819bb3 2965 return 1;
956c7629 2966 } else {
a5819bb3 2967 return 0;
956c7629
AD
2968 }
2969 }
2970
673d54ca
AD
2971 function print_feed_select($link, $id, $default_id = "",
2972 $attributes = "", $include_all_feeds = true) {
2973
79f3553b 2974 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2975 if ($include_all_feeds) {
89cb787e 2976 print "<option value=\"0\">".__('All feeds')."</option>";
673d54ca
AD
2977 }
2978
2979 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2980 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2981
2982 if (db_num_rows($result) > 0 && $include_all_feeds) {
2983 print "<option disabled>--------</option>";
2984 }
2985
2986 while ($line = db_fetch_assoc($result)) {
2987 if ($line["id"] == $default_id) {
2988 $is_selected = "selected";
2989 } else {
2990 $is_selected = "";
2991 }
b1710666
AD
2992
2993 $title = truncate_string(htmlspecialchars($line["title"]), 40);
2994
79f3553b 2995 printf("<option $is_selected value='%d'>%s</option>",
b1710666 2996 $line["id"], $title);
673d54ca
AD
2997 }
2998
2999 print "</select>";
3000 }
3001
3002 function print_feed_cat_select($link, $id, $default_id = "",
3003 $attributes = "", $include_all_cats = true) {
3004
79f3553b 3005 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
3006
3007 if ($include_all_cats) {
d1db26aa 3008 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
3009 }
3010
3011 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
3012 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
3013
3014 if (db_num_rows($result) > 0 && $include_all_cats) {
3015 print "<option disabled>--------</option>";
3016 }
3017
3018 while ($line = db_fetch_assoc($result)) {
3019 if ($line["id"] == $default_id) {
3020 $is_selected = "selected";
3021 } else {
3022 $is_selected = "";
3023 }
14f69488 3024 printf("<option $is_selected value='%d'>%s</option>",
47439031 3025 $line["id"], htmlspecialchars($line["title"]));
673d54ca
AD
3026 }
3027
3028 print "</select>";
3029 }
3030
14f69488
AD
3031 function checkbox_to_sql_bool($val) {
3032 return ($val == "on") ? "true" : "false";
3033 }
86b682ce
AD
3034
3035 function getFeedCatTitle($link, $id) {
3036 if ($id == -1) {
d1db26aa 3037 return __("Special");
86b682ce 3038 } else if ($id < -10) {
d1db26aa 3039 return __("Labels");
86b682ce
AD
3040 } else if ($id > 0) {
3041 $result = db_query($link, "SELECT ttrss_feed_categories.title
3042 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
3043 cat_id = ttrss_feed_categories.id");
3044 if (db_num_rows($result) == 1) {
3045 return db_fetch_result($result, 0, "title");
3046 } else {
d1db26aa 3047 return __("Uncategorized");
86b682ce
AD
3048 }
3049 } else {
3050 return "getFeedCatTitle($id) failed";
3051 }
3052
3053 }
3054
af88c48a
AD
3055 function getFeedIcon($id) {
3056 switch ($id) {
4bee8b5f
AD
3057 case 0:
3058 return "images/archive.png";
3059 break;
af88c48a 3060 case -1:
f65ffc2d 3061 return "images/mark_set.png";
af88c48a
AD
3062 break;
3063 case -2:
b97e6e02 3064 return "images/pub_set.png";
af88c48a
AD
3065 break;
3066 case -3:
3067 return "images/fresh.png";
3068 break;
3069 case -4:
3070 return "images/tag.png";
3071 break;
3072 default:
4bee8b5f
AD
3073 if ($id < -10) {
3074 return "images/label.png";
3075 } else {
3076 return ICONS_URL . "/$id.ico";
3077 }
af88c48a
AD
3078 break;
3079 }
3080 }
3081
86b682ce
AD
3082 function getFeedTitle($link, $id) {
3083 if ($id == -1) {
d1db26aa 3084 return __("Starred articles");
945c243e
AD
3085 } else if ($id == -2) {
3086 return __("Published articles");
2d24f032
AD
3087 } else if ($id == -3) {
3088 return __("Fresh articles");
b2531a28
AD
3089 } else if ($id == -4) {
3090 return __("All articles");
80db1113 3091 } else if ($id === 0 || $id === "0") {
e04c18a2 3092 return __("Archived articles");
86b682ce 3093 } else if ($id < -10) {
76626c72 3094 $label_id = -$id - 11;
ceb30ba4 3095 $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
86b682ce 3096 if (db_num_rows($result) == 1) {
ceb30ba4 3097 return db_fetch_result($result, 0, "caption");
86b682ce
AD
3098 } else {
3099 return "Unknown label ($label_id)";
3100 }
3101
3102 } else if ($id > 0) {
3103 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
3104 if (db_num_rows($result) == 1) {
3105 return db_fetch_result($result, 0, "title");
3106 } else {
3107 return "Unknown feed ($id)";
3108 }
3109 } else {
22fdebff 3110 return $id;
86b682ce 3111 }
86b682ce 3112 }
3dd46f19
AD
3113
3114 function get_session_cookie_name() {
3115 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
3116 }
3ac2b520
AD
3117
3118 function print_init_params($link) {
3119 print "<init-params>";
3120 if ($_SESSION["stored-params"]) {
3121 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
3122 if ($key) {
3123 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
3124 print "<param key=\"$key\" value=\"$value\"/>";
3125 }
3ac2b520
AD
3126 }
3127 }
3128
dce46cad 3129 print "<param key=\"theme\" value=\"".get_user_theme($link)."\"/>";
e71f2610 3130 print "<param key=\"theme_options\" value=\"".get_user_theme_options($link)."\"/>";
3ac2b520
AD
3131 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
3132 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
17c0eeba 3133 print "<param key=\"daemon_refresh_only\" value=\"true\"/>";
3ac2b520 3134
883fee8d
AD
3135 print "<param key=\"sign_progress\" value=\"".
3136 theme_image($link, "images/indicator_white.gif")."\"/>";
3137
ba569828
AD
3138 print "<param key=\"sign_progress_tiny\" value=\"".
3139 theme_image($link, "images/indicator_tiny.gif")."\"/>";
3140
883fee8d
AD
3141 print "<param key=\"sign_excl\" value=\"".
3142 theme_image($link, "images/sign_excl.png")."\"/>";
3143
3144 print "<param key=\"sign_info\" value=\"".
3145 theme_image($link, "images/sign_info.png")."\"/>";
3146
3ac2b520
AD
3147 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
3148 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
3149
e8bd0da9 3150 print "<param key=\"hide_read_feeds\" value=\"" .
465ff90b 3151 (int) get_pref($link, "HIDE_READ_FEEDS") . "\"/>";
e8bd0da9 3152
1c980681
AD
3153 print "<param key=\"enable_feed_cats\" value=\"" .
3154 (int) get_pref($link, "ENABLE_FEED_CATS") . "\"/>";
3155
c9268ed5 3156 print "<param key=\"feeds_sort_by_unread\" value=\"" .
465ff90b 3157 (int) get_pref($link, "FEEDS_SORT_BY_UNREAD") . "\"/>";
c9268ed5 3158
f6d6e22f 3159 print "<param key=\"confirm_feed_catchup\" value=\"" .
465ff90b 3160 (int) get_pref($link, "CONFIRM_FEED_CATCHUP") . "\"/>";
f6d6e22f 3161
ac7bcd71 3162 print "<param key=\"cdm_auto_catchup\" value=\"" .
465ff90b 3163 (int) get_pref($link, "CDM_AUTO_CATCHUP") . "\"/>";
ac7bcd71 3164
8e9c121b
AD
3165 print "<param key=\"icons_url\" value=\"" . ICONS_URL . "\"/>";
3166
be0801a1
AD
3167 print "<param key=\"cookie_lifetime\" value=\"" . SESSION_COOKIE_LIFETIME . "\"/>";
3168
40496720
AD
3169 print "<param key=\"default_view_mode\" value=\"" .
3170 get_pref($link, "_DEFAULT_VIEW_MODE") . "\"/>";
3171
3172 print "<param key=\"default_view_limit\" value=\"" .
465ff90b 3173 (int) get_pref($link, "_DEFAULT_VIEW_LIMIT") . "\"/>";
40496720 3174
7b4d02a8
AD
3175 print "<param key=\"default_view_order_by\" value=\"" .
3176 get_pref($link, "_DEFAULT_VIEW_ORDER_BY") . "\"/>";
3177
fe8d2059
AD
3178 print "<param key=\"prefs_active_tab\" value=\"" .
3179 get_pref($link, "_PREFS_ACTIVE_TAB") . "\"/>";
3180
465ff90b
AD
3181 print "<param key=\"infobox_disable_overlay\" value=\"" .
3182 get_pref($link, "_INFOBOX_DISABLE_OVERLAY") . "\"/>";
3183
527c3bf0
AD
3184 print "<param key=\"icons_location\" value=\"" .
3185 ICONS_URL . "\"/>";
3186
22f3e356
AD
3187 print "<param key=\"hide_read_shows_special\" value=\"" .
3188 (int) get_pref($link, "HIDE_READ_SHOWS_SPECIAL") . "\"/>";
3189
fca93350
AD
3190 print "<param key=\"hide_feedlist\" value=\"" .
3191 (int) get_pref($link, "HIDE_FEEDLIST") . "\"/>";
24c1e1c1 3192
a598370d
AD
3193 print "<param key=\"bw_limit\" value=\"".
3194 (int) $_SESSION["bw_limit"]."\"/>";
3195
bd090ab4
AD
3196// print "<param key=\"sync_counters\" value=\"" .
3197// (int) get_pref($link, "SYNC_COUNTERS") . "\"/>";
3198
3199 print "<param key=\"sync_counters\" value=\"1\"/>";
d234a2e3 3200
5de926d8
AD
3201 print "<param key=\"offline_enabled\" value=\"".
3202 (int) get_pref($link, "ENABLE_OFFLINE_READING") . "\"/>";
59b223d7 3203
9b7ecc0a
AD
3204 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
3205 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
3206
3207 $num_feeds = db_fetch_result($result, 0, "cf");
3208
3209 print "<param key=\"num_feeds\" value=\"".
3210 (int)$num_feeds. "\"/>";
3211
57937c42
AD
3212 print "<param key=\"collapsed_feedlist\" value=\"" .
3213 (int) get_pref($link, "_COLLAPSED_FEEDLIST") . "\"/>";
3214
3ac2b520
AD
3215 print "</init-params>";
3216 }
f54f515f
AD
3217
3218 function print_runtime_info($link) {
3219 print "<runtime-info>";
20361063 3220
9b7ecc0a
AD
3221 $result = db_query($link, "SELECT COUNT(*) AS cf FROM
3222 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
3223
3224 $num_feeds = db_fetch_result($result, 0, "cf");
3225
3226 print "<param key=\"num_feeds\" value=\"".
3227 (int)$num_feeds. "\"/>";
3228
71ad883b
AD
3229 if (ENABLE_UPDATE_DAEMON) {
3230 print "<param key=\"daemon_is_running\" value=\"".
22fdebff 3231 (int) file_is_locked("update_daemon.lock") . "\"/>";
8e00ae9b 3232
9041f58b 3233 if (time() - $_SESSION["daemon_stamp_check"] > 30) {
8e00ae9b
AD
3234
3235 $stamp = (int)read_stampfile("update_daemon.stamp");
3236
fbae93d8
AD
3237// print "<param key=\"daemon_stamp_delta\" value=\"$stamp_delta\"/>";
3238
8e00ae9b 3239 if ($stamp) {
9041f58b
AD
3240 $stamp_delta = time() - $stamp;
3241
3242 if ($stamp_delta > 1800) {
f6854e44 3243 $stamp_check = 0;
8e00ae9b 3244 } else {
f6854e44
AD
3245 $stamp_check = 1;
3246 $_SESSION["daemon_stamp_check"] = time();
8e00ae9b
AD
3247 }
3248
f6854e44
AD
3249 print "<param key=\"daemon_stamp_ok\" value=\"$stamp_check\"/>";
3250
8e00ae9b
AD
3251 $stamp_fmt = date("Y.m.d, G:i", $stamp);
3252
3253 print "<param key=\"daemon_stamp\" value=\"$stamp_fmt\"/>";
3254 }
8e00ae9b 3255 }
71ad883b 3256 }
8e00ae9b 3257
d9fa39f1
AD
3258 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
3259
8742de78 3260 if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) {
d9fa39f1
AD
3261 $new_version_details = check_for_update($link);
3262
3263 print "<param key=\"new_version_available\" value=\"".
3264 sprintf("%d", $new_version_details != ""). "\"/>";
3265
3266 $_SESSION["last_version_check"] = time();
3267 }
3268 }
3269
1c9df66e 3270// print "<param key=\"new_version_available\" value=\"1\"/>";
b4507bc2 3271
f54f515f
AD
3272 print "</runtime-info>";
3273 }
ef393de7 3274
88040f57 3275 function getSearchSql($search, $match_on) {
ef393de7 3276
88040f57 3277 $search_query_part = "";
e20c9d88 3278
88040f57
AD
3279 $keywords = split(" ", $search);
3280 $query_keywords = array();
e20c9d88 3281
88040f57 3282 if ($match_on == "both") {
e20c9d88 3283
88040f57 3284 foreach ($keywords as $k) {
eb6c7f42
AD
3285 if (strpos($k, "-") === 0) {
3286 $k = substr($k, 1);
3287 $not = "NOT";
3288 } else {
3289 $not = "";
3290 }
3291
3292 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
3293 OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
88040f57 3294 }
e20c9d88 3295
88040f57 3296 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 3297
88040f57 3298 } else if ($match_on == "title") {
e20c9d88 3299
88040f57 3300 foreach ($keywords as $k) {
eb6c7f42
AD
3301 if (strpos($k, "-") === 0) {
3302 $k = substr($k, 1);
3303 $not = "NOT";
3304 } else {
3305 $not = "";
3306 }
3307
3308 array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%'))");
88040f57 3309 }
e20c9d88 3310
88040f57 3311 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 3312
88040f57
AD
3313 } else if ($match_on == "content") {
3314
3315 foreach ($keywords as $k) {
eb6c7f42
AD
3316 if (strpos($k, "-") === 0) {
3317 $k = substr($k, 1);
3318 $not = "NOT";
3319 } else {
3320 $not = "";
3321 }
3322
3323 array_push($query_keywords, "(UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
88040f57
AD
3324 }
3325 }
3326
3327 $search_query_part = implode("AND", $query_keywords);
3328
3329 return $search_query_part;
3330 }
3331
c36bf4d5
AD
3332 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0) {
3333
3334 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
c1a0b534 3335
88040f57
AD
3336 if ($search) {
3337
3338 $search_query_part = getSearchSql($search, $match_on);
3339 $search_query_part .= " AND ";
e20c9d88 3340
ef393de7
AD
3341 } else {
3342 $search_query_part = "";
3343 }
3344
3345 $view_query_part = "";
3346
7b4d02a8 3347 if ($view_mode == "adaptive" || $view_query_part == "noscores") {
ef393de7
AD
3348 if ($search) {
3349 $view_query_part = " ";
3350 } else if ($feed != -1) {
f295c368 3351 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7 3352 if ($unread > 0) {
ff863e00 3353 $view_query_part = " unread = true AND ";
ef393de7
AD
3354 }
3355 }
3356 }
3357
3358 if ($view_mode == "marked") {
3359 $view_query_part = " marked = true AND ";
3360 }
3361
3362 if ($view_mode == "unread") {
3363 $view_query_part = " unread = true AND ";
3364 }
8b09eac8
AD
3365
3366 if ($view_mode == "updated") {
3367 $view_query_part = " (last_read is null and unread = false) AND ";
3368 }
3369
ef393de7
AD
3370 if ($limit > 0) {
3371 $limit_query_part = "LIMIT " . $limit;
3372 }
3373
3374 $vfeed_query_part = "";
3375
3376 // override query strategy and enable feed display when searching globally
3377 if ($search && $search_mode == "all_feeds") {
3378 $query_strategy_part = "ttrss_entries.id > 0";
3379 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
22fdebff 3380 /* tags */
ef393de7
AD
3381 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
3382 $query_strategy_part = "ttrss_entries.id > 0";
3383 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
3384 id = feed_id) as feed_title,";
e04c18a2 3385 } else if ($feed > 0 && $search && $search_mode == "this_cat") {
ef393de7
AD
3386
3387 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
3388
3389 $tmp_result = false;
3390
3391 if ($cat_view) {
3392 $tmp_result = db_query($link, "SELECT id
3393 FROM ttrss_feeds WHERE cat_id = '$feed'");
3394 } else {
3395 $tmp_result = db_query($link, "SELECT id
3396 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
3397 WHERE id = '$feed') AND id != '$feed'");
3398 }
ef393de7
AD
3399
3400 $cat_siblings = array();
3401
3402 if (db_num_rows($tmp_result) > 0) {
3403 while ($p = db_fetch_assoc($tmp_result)) {
3404 array_push($cat_siblings, "feed_id = " . $p["id"]);
3405 }
3406
3407 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3408 $feed, implode(" OR ", $cat_siblings));
3409
3410 } else {
3411 $query_strategy_part = "ttrss_entries.id > 0";
3412 }
3413
e04c18a2 3414 } else if ($feed > 0) {
ef393de7
AD
3415
3416 if ($cat_view) {
5c365f60 3417
ef393de7
AD
3418 if ($feed > 0) {
3419 $query_strategy_part = "cat_id = '$feed'";
3420 } else {
3421 $query_strategy_part = "cat_id IS NULL";
3422 }
3423
3424 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 3425
ef393de7
AD
3426 } else {
3427 $tmp_result = db_query($link, "SELECT id
3428 FROM ttrss_feeds WHERE parent_feed = '$feed'
3429 ORDER BY cat_id,title");
3430
3431 $parent_ids = array();
3432
3433 if (db_num_rows($tmp_result) > 0) {
3434 while ($p = db_fetch_assoc($tmp_result)) {
3435 array_push($parent_ids, "feed_id = " . $p["id"]);
3436 }
3437
3438 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
3439 $feed, implode(" OR ", $parent_ids));
3440
3441 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3442 } else {
3443 $query_strategy_part = "feed_id = '$feed'";
3444 }
3445 }
e04c18a2
AD
3446 } else if ($feed == 0) { // starred virtual feed
3447 $query_strategy_part = "feed_id IS NULL";
ef393de7
AD
3448 } else if ($feed == -1) { // starred virtual feed
3449 $query_strategy_part = "marked = true";
3450 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
e6a38cde
AD
3451 } else if ($feed == -2) { // published virtual feed OR labels category
3452
3453 if (!$cat_view) {
3454 $query_strategy_part = "published = true";
3455 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3456 } else {
3457 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3458
3459 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
3460
3461 $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND
3462 ttrss_user_labels2.article_id = ref_id";
3463
3464 }
3465
2d24f032
AD
3466 } else if ($feed == -3) { // fresh virtual feed
3467 $query_strategy_part = "unread = true";
3468
7a22dc2a 3469 $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid);
c1d7e6c3 3470
2d24f032 3471 if (DB_TYPE == "pgsql") {
7608b38a 3472 $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' ";
2d24f032 3473 } else {
7608b38a 3474 $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
2d24f032
AD
3475 }
3476
b2531a28
AD
3477 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
3478 } else if ($feed == -4) { // all articles virtual feed
3479 $query_strategy_part = "true";
e4f4b46f 3480 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ef393de7
AD
3481 } else if ($feed <= -10) { // labels
3482 $label_id = -$feed - 11;
3de0261a 3483
ceb30ba4
AD
3484 $query_strategy_part = "label_id = '$label_id' AND
3485 ttrss_labels2.id = ttrss_user_labels2.label_id AND
3486 ttrss_user_labels2.article_id = ref_id";
3de0261a 3487
ef393de7 3488 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
ceb30ba4
AD
3489 $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2";
3490
ef393de7
AD
3491 } else {
3492 $query_strategy_part = "id > 0"; // dumb
3493 }
d6e5706d 3494
7a22dc2a 3495 if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) {
d6e5706d
AD
3496 $order_by = "updated";
3497 } else {
3498 $order_by = "updated DESC";
3499 }
e939722a 3500
7b4d02a8
AD
3501 if ($view_mode != "noscores") {
3502 $order_by = "score DESC, $order_by";
3503 }
48b0c4ec 3504
e939722a
AD
3505 if ($override_order) {
3506 $order_by = $override_order;
3507 }
ef393de7
AD
3508
3509 $feed_title = "";
3510
22fdebff
AD
3511 if ($search) {
3512 $feed_title = "Search results";
3513 } else {
ef393de7 3514 if ($cat_view) {
22fdebff 3515 $feed_title = getCategoryTitle($link, $feed);
ef393de7 3516 } else {
22fdebff
AD
3517 if ((int)$feed == $feed && $feed > 0) {
3518 $result = db_query($link, "SELECT title,site_url,last_error
3519 FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid");
ef393de7 3520
22fdebff
AD
3521 $feed_title = db_fetch_result($result, 0, "title");
3522 $feed_site_url = db_fetch_result($result, 0, "site_url");
3523 $last_error = db_fetch_result($result, 0, "last_error");
3524 } else {
3525 $feed_title = getFeedTitle($link, $feed);
3526 }
88040f57 3527 }
ef393de7
AD
3528 }
3529
62129e67
AD
3530 $content_query_part = "content as content_preview,";
3531
ef393de7
AD
3532 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
3533
3534 if ($feed >= 0) {
3535 $feed_kind = "Feeds";
3536 } else {
3537 $feed_kind = "Labels";
3538 }
3539
95a82c08
AD
3540 if ($limit_query_part) {
3541 $offset_query_part = "OFFSET $offset";
3542 }
3543
d00f22ac 3544 if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) {
6cfea5c7 3545 if (!$override_order) {
43fc671f
AD
3546 $order_by = "ttrss_feeds.title, $order_by";
3547 }
6cfea5c7
AD
3548 }
3549
e04c18a2
AD
3550 if ($feed != "0") {
3551 $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part";
117335bf 3552 $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND";
e04c18a2
AD
3553
3554 } else {
3555 $from_qpart = "ttrss_entries,ttrss_user_entries$ext_tables_part
3556 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)";
3557 }
3558
3ebd7ca5 3559 $query = "SELECT DISTINCT
1f64b1be 3560 guid,
ef393de7 3561 ttrss_entries.id,ttrss_entries.title,
46921916 3562 updated,
c7e51de1 3563 note,
494a64ea 3564 unread,feed_id,marked,published,link,last_read,orig_feed_id,
fc2b26a6 3565 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3566 $vfeed_query_part
3567 $content_query_part
fc2b26a6 3568 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
ff6e357a 3569 author,score
ef393de7 3570 FROM
e04c18a2 3571 $from_qpart
ef393de7 3572 WHERE
43fc671f 3573 $group_limit_part
e04c18a2 3574 $feed_check_qpart
ef393de7 3575 ttrss_user_entries.ref_id = ttrss_entries.id AND
c36bf4d5 3576 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3577 $search_query_part
3578 $view_query_part
3579 $query_strategy_part ORDER BY $order_by
95a82c08 3580 $limit_query_part $offset_query_part";
4bc311fc 3581
b4e75b2a 3582 if ($_REQUEST["debug"]) print $query;
4bc311fc
AD
3583
3584 $result = db_query($link, $query);
ef393de7
AD
3585
3586 } else {
3587 // browsing by tag
3588
3589 $feed_kind = "Tags";
3590
3591 $result = db_query($link, "SELECT
1f64b1be 3592 guid,
c7e51de1 3593 note,
ef393de7 3594 ttrss_entries.id as id,title,
46921916 3595 updated,
494a64ea 3596 unread,feed_id,orig_feed_id,
ef393de7 3597 marked,link,last_read,
fc2b26a6 3598 ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms,
ef393de7
AD
3599 $vfeed_query_part
3600 $content_query_part
4d0b3607
AD
3601 ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms,
3602 score
ef393de7
AD
3603 FROM
3604 ttrss_entries,ttrss_user_entries,ttrss_tags
3605 WHERE
546499a9 3606 ref_id = ttrss_entries.id AND
c36bf4d5 3607 ttrss_user_entries.owner_uid = '$owner_uid' AND
ef393de7
AD
3608 post_int_id = int_id AND tag_name = '$feed' AND
3609 $view_query_part
3610 $search_query_part
3611 $query_strategy_part ORDER BY $order_by
3612 $limit_query_part");
3613 }
3614
c7188969 3615 return array($result, $feed_title, $feed_site_url, $last_error);
22fdebff 3616
ef393de7
AD
3617 }
3618
c36bf4d5 3619 function generate_syndicated_feed($link, $owner_uid, $feed, $is_cat,
ead2715d
AD
3620 $limit, $search, $search_mode, $match_on) {
3621
db54143e 3622 $note_style = "float : right; background-color : #fff7d5; border-width : 1px; ".
c7e51de1 3623 "padding : 5px; border-style : dashed; border-color : #e7d796;".
db54143e 3624 "margin-bottom : 1em; color : #9a8c59;";
c7e51de1 3625
ead2715d 3626 if (!$limit) $limit = 30;
18664970
AD
3627
3628 $qfh_ret = queryFeedHeadlines($link, $feed,
ead2715d 3629 $limit, false, $is_cat, $search, $search_mode, $match_on, "updated DESC", 0,
c36bf4d5 3630 $owner_uid);
18664970
AD
3631
3632 $result = $qfh_ret[0];
59e2aab4 3633 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
3634 $feed_site_url = $qfh_ret[2];
3635 $last_error = $qfh_ret[3];
3636
a5472764 3637// if (!$feed_site_url) $feed_site_url = "http://localhost/";
4bc64807 3638
a5472764
AD
3639 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>
3640 <?xml-stylesheet type=\"text/xsl\" href=\"rss.xsl\"?>
3641 <rss version=\"2.0\">
3baeeeca
AD
3642 <channel>
3643 <title>$feed_title</title>
4bc64807
AD
3644 <link>$feed_site_url</link>
3645 <description>Feed generated by Tiny Tiny RSS</description>";
3baeeeca
AD
3646
3647 while ($line = db_fetch_assoc($result)) {
3648 print "<item>";
4bc64807 3649 print "<guid>" . htmlspecialchars($line["guid"]) . "</guid>";
3baeeeca 3650 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
0c3d1c68 3651
bc976a8c 3652 $tags = get_article_tags($link, $line["id"], $owner_uid);
0c3d1c68
AD
3653
3654 foreach ($tags as $tag) {
3655 print "<category>" . htmlspecialchars($tag) . "</category>";
3656 }
3657
3baeeeca
AD
3658 $rfc822_date = date('r', strtotime($line["updated"]));
3659
3660 print "<pubDate>$rfc822_date</pubDate>";
3661
3662 print "<title>" .
3663 htmlspecialchars($line["title"]) . "</title>";
3664
db54143e
AD
3665 print "<description><![CDATA[";
3666
c7e51de1
AD
3667 if ($line["note"]) {
3668 print "<div style='$note_style'>";
3669 print $line["note"];
3670 print "</div>";
3671 }
db54143e 3672
ceb0cab5 3673 print sanitize_rss($link, $line["content_preview"], false, $owner_uid);
c7e51de1 3674 print "]]></description>";
3baeeeca
AD
3675
3676 print "</item>";
3677 }
3678
3679 print "</channel></rss>";
18664970
AD
3680
3681 }
3682
0a6c4846
AD
3683 function getCategoryTitle($link, $cat_id) {
3684
bba7c4bf
AD
3685 if ($cat_id == -1) {
3686 return __("Special");
3687 } else if ($cat_id == -2) {
3688 return __("Labels");
0a6c4846 3689 } else {
bba7c4bf
AD
3690
3691 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
3692 id = '$cat_id'");
3693
3694 if (db_num_rows($result) == 1) {
3695 return db_fetch_result($result, 0, "title");
3696 } else {
3697 return "Uncategorized";
3698 }
0a6c4846
AD
3699 }
3700 }
3701
f45a286b
AD
3702 function strip_tags_long($string, $allowed) {
3703
3704 $config = HTMLPurifier_Config::createDefault();
3705
3706 $config->set('HTML', 'Allowed', $allowed);
3707 $purifier = new HTMLPurifier($config);
3708
3709 return $purifier->purify($string);
3710
3711 }
3712
f738aef1
AD
3713 // http://ru2.php.net/strip-tags
3714
f45a286b 3715/* function strip_tags_long($textstring, $allowed){
f738aef1
AD
3716 while($textstring != strip_tags($textstring, $allowed))
3717 {
3718 while (strlen($textstring) != 0)
3719 {
3720 if (strlen($textstring) > 1024) {
3721 $otherlen = 1024;
3722 } else {
3723 $otherlen = strlen($textstring);
3724 }
3725 $temptext = strip_tags(substr($textstring,0,$otherlen), $allowed);
3726 $safetext .= $temptext;
3727 $textstring = substr_replace($textstring,'',0,$otherlen);
3728 }
3729 $textstring = $safetext;
3730 }
3731 return $textstring;
f45a286b 3732} */
f738aef1
AD
3733
3734
ceb0cab5 3735 function sanitize_rss($link, $str, $force_strip_tags = false, $owner = false) {
60452879 3736 $res = $str;
183ad07b 3737
ceb0cab5
AD
3738 if (!$owner) $owner = $_SESSION["uid"];
3739
3740 if (get_pref($link, "STRIP_UNSAFE_TAGS", $owner) || $force_strip_tags) {
f738aef1 3741
f45a286b
AD
3742// $res = strip_tags_long($res,
3743// "<p><a><i><em><b><strong><code><pre><blockquote><br><img><ul><ol><li>");
3744
7e1265ed 3745 $res = strip_tags_long($res,
f45a286b 3746 "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title],ul,ol,li,h1,h2,h3,h4");
f738aef1 3747
f826eee1
AD
3748 }
3749
ceb0cab5 3750 if (get_pref($link, "STRIP_IMAGES", $owner)) {
8dccabed 3751 $res = preg_replace('/<img[^>]+>/is', '', $res);
7514749d 3752 }
8dccabed 3753
a522a767 3754 if (get_pref($link, 'OPEN_LINKS_IN_NEW_WINDOW', $owner)) {
7514749d 3755 $res = preg_replace("/href=/i", "target=\"_blank\" href=", $res);
8dccabed
AD
3756 }
3757
183ad07b
AD
3758 return $res;
3759 }
b72c3ef8 3760
45004d43
AD
3761 /**
3762 * Send by mail a digest of last articles.
3763 *
3764 * @param mixed $link The database connection.
3765 * @param integer $limit The maximum number of articles by digest.
3766 * @return boolean Return false if digests are not enabled.
3767 */
9cd7c995
AD
3768 function send_headlines_digests($link, $limit = 100) {
3769
1ddba275
AD
3770 if (!DIGEST_ENABLE) return false;
3771
9cd7c995 3772 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 3773 $days = 1;
9cd7c995
AD
3774
3775 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
3776
3777 if (DB_TYPE == "pgsql") {
3778 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
3779 } else if (DB_TYPE == "mysql") {
3780 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
3781 }
3782
3783 $result = db_query($link, "SELECT id,email FROM ttrss_users
3784 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
3785
3786 while ($line = db_fetch_assoc($result)) {
dc85be2b 3787
9cd7c995
AD
3788 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
3789 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
3790
dc85be2b
AD
3791 $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false);
3792
9cd7c995
AD
3793 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
3794 $digest = $tuple[0];
3795 $headlines_count = $tuple[1];
dc85be2b 3796 $affected_ids = $tuple[2];
c62a2c21 3797 $digest_text = $tuple[3];
9cd7c995
AD
3798
3799 if ($headlines_count > 0) {
a8931123 3800
c62a2c21 3801 $mail = new PHPMailer();
a8931123 3802
d134e3a3
AD
3803 $mail->PluginDir = "lib/phpmailer/";
3804 $mail->SetLanguage("en", "lib/phpmailer/language/");
a8931123 3805
c62a2c21 3806 $mail->CharSet = "UTF-8";
a8931123 3807
c62a2c21
AD
3808 $mail->From = DIGEST_FROM_ADDRESS;
3809 $mail->FromName = DIGEST_FROM_NAME;
3810 $mail->AddAddress($line["email"], $line["login"]);
c7ddac5c 3811
c62a2c21 3812 if (DIGEST_SMTP_HOST) {
a8931123
AD
3813 $mail->Host = DIGEST_SMTP_HOST;
3814 $mail->Mailer = "smtp";
19a1da0d 3815 $mail->SMTPAuth = DIGEST_SMTP_LOGIN != '';
a8931123
AD
3816 $mail->Username = DIGEST_SMTP_LOGIN;
3817 $mail->Password = DIGEST_SMTP_PASSWORD;
c62a2c21 3818 }
a8931123 3819
c62a2c21 3820 $mail->IsHTML(true);
163a295e 3821 $mail->Subject = DIGEST_SUBJECT;
c62a2c21
AD
3822 $mail->Body = $digest;
3823 $mail->AltBody = $digest_text;
a8931123 3824
c62a2c21 3825 $rc = $mail->Send();
a8931123 3826
c62a2c21 3827 if (!$rc) print "ERROR: " . $mail->ErrorInfo;
a8931123 3828
9cd7c995 3829 print "RC=$rc\n";
a8931123 3830
c62a2c21 3831 if ($rc && $do_catchup) {
dc85be2b 3832 print "Marking affected articles as read...\n";
9968d46f 3833 catchupArticlesById($link, $affected_ids, 0, $line["id"]);
dc85be2b
AD
3834 }
3835
9cd7c995
AD
3836 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
3837 WHERE id = " . $line["id"]);
3838 } else {
3839 print "No headlines\n";
3840 }
3841 }
3842 }
3843
cedd3e89
AD
3844 print "All done.\n";
3845
9cd7c995
AD
3846 }
3847
7e3634d9 3848 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
c62a2c21 3849
fe7537b5 3850 require_once "lib/MiniTemplator.class.php";
c62a2c21
AD
3851
3852 $tpl = new MiniTemplator;
3853 $tpl_t = new MiniTemplator;
3854
3855 $tpl->readTemplateFromFile("templates/digest_template_html.txt");
3856 $tpl_t->readTemplateFromFile("templates/digest_template.txt");
3857
3858 $tpl->setVariable('CUR_DATE', date('Y/m/d'));
3859 $tpl->setVariable('CUR_TIME', date('G:i'));
3860
3861 $tpl_t->setVariable('CUR_DATE', date('Y/m/d'));
3862 $tpl_t->setVariable('CUR_TIME', date('G:i'));
7e3634d9 3863
dc85be2b
AD
3864 $affected_ids = array();
3865
7e3634d9 3866 if (DB_TYPE == "pgsql") {
9cd7c995 3867 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
3868 } else if (DB_TYPE == "mysql") {
3869 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
3870 }
3871
3872 $result = db_query($link, "SELECT ttrss_entries.title,
3873 ttrss_feeds.title AS feed_title,
3874 date_entered,
dc85be2b 3875 ttrss_user_entries.ref_id,
7e3634d9 3876 link,
c62a2c21 3877 SUBSTRING(content, 1, 120) AS excerpt,
fc2b26a6 3878 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated
7e3634d9
AD
3879 FROM
3880 ttrss_user_entries,ttrss_entries,ttrss_feeds
3881 WHERE
3882 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 3883 AND include_in_digest = true
7e3634d9 3884 AND $interval_query
448b0abd 3885 AND ttrss_user_entries.owner_uid = $user_id
c62a2c21
AD
3886 AND unread = true
3887 ORDER BY ttrss_feeds.title, date_entered DESC
7e3634d9
AD
3888 LIMIT $limit");
3889
3890 $cur_feed_title = "";
3891
9cd7c995
AD
3892 $headlines_count = db_num_rows($result);
3893
c62a2c21
AD
3894 $headlines = array();
3895
7e3634d9 3896 while ($line = db_fetch_assoc($result)) {
c62a2c21
AD
3897 array_push($headlines, $line);
3898 }
3899
3900 for ($i = 0; $i < sizeof($headlines); $i++) {
3901
3902 $line = $headlines[$i];
dc85be2b
AD
3903
3904 array_push($affected_ids, $line["ref_id"]);
3905
7e3634d9 3906 $updated = smart_date_time(strtotime($line["last_updated"]));
7e3634d9 3907
c62a2c21
AD
3908 $tpl->setVariable('FEED_TITLE', $line["feed_title"]);
3909 $tpl->setVariable('ARTICLE_TITLE', $line["title"]);
3910 $tpl->setVariable('ARTICLE_LINK', $line["link"]);
3911 $tpl->setVariable('ARTICLE_UPDATED', $updated);
163a295e
AD
3912 $tpl->setVariable('ARTICLE_EXCERPT',
3913 truncate_string(strip_tags($line["excerpt"]), 100));
7e3634d9 3914
c62a2c21
AD
3915 $tpl->addBlock('article');
3916
3917 $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]);
3918 $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]);
3919 $tpl_t->setVariable('ARTICLE_LINK', $line["link"]);
3920 $tpl_t->setVariable('ARTICLE_UPDATED', $updated);
3921// $tpl_t->setVariable('ARTICLE_EXCERPT',
3922// truncate_string(strip_tags($line["excerpt"]), 100));
3923
3924 $tpl_t->addBlock('article');
3925
3926 if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) {
3927 $tpl->addBlock('feed');
3928 $tpl_t->addBlock('feed');
7e3634d9
AD
3929 }
3930
7e3634d9
AD
3931 }
3932
c62a2c21
AD
3933 $tpl->addBlock('digest');
3934 $tpl->generateOutputToString($tmp);
3935
3936 $tpl_t->addBlock('digest');
3937 $tpl_t->generateOutputToString($tmp_t);
7e3634d9 3938
c62a2c21 3939 return array($tmp, $headlines_count, $affected_ids, $tmp_t);
7e3634d9
AD
3940 }
3941
73495fd1 3942 function check_for_update($link) {
b6d486a3 3943 $releases_feed = "http://tt-rss.org/releases.rss";
b72c3ef8
AD
3944
3945 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
3946 return;
3947 }
3948
3949 error_reporting(0);
f67d9754
AD
3950 if (ENABLE_SIMPLEPIE) {
3951 $rss = new SimplePie();
3952 $rss->set_useragent(SIMPLEPIE_USERAGENT . MAGPIE_USER_AGENT_EXT);
4148e809 3953// $rss->set_timeout(MAGPIE_FETCH_TIME_OUT);
f67d9754
AD
3954 $rss->set_feed_url($fetch_url);
3955 $rss->set_output_encoding('UTF-8');
3956 $rss->init();
3957 } else {
3958 $rss = fetch_rss($releases_feed);
3959 }
b72c3ef8
AD
3960 error_reporting (DEFAULT_ERROR_LEVEL);
3961
3962 if ($rss) {
3963
f67d9754
AD
3964 if (ENABLE_SIMPLEPIE) {
3965 $items = $rss->get_items();
3966 } else {
3967 $items = $rss->items;
b72c3ef8 3968
f67d9754
AD
3969 if (!$items || !is_array($items)) $items = $rss->entries;
3970 if (!$items || !is_array($items)) $items = $rss;
3971 }
b72c3ef8 3972
da412ad3 3973 if (!is_array($items) || count($items) == 0) {
b72c3ef8 3974 return;
da412ad3 3975 }
b72c3ef8 3976
a41d2c65 3977 $latest_item = $items[0];
b72c3ef8 3978
f67d9754
AD
3979 if (ENABLE_SIMPLEPIE) {
3980 $last_title = $latest_item->get_title();
3981 } else {
3982 $last_title = $latest_item["title"];
3983 }
b72c3ef8 3984
f67d9754
AD
3985 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $last_title));
3986
3987 if (ENABLE_SIMPLEPIE) {
dcf7fd08
AD
3988 $release_url = sanitize_rss($link, $latest_item->get_link());
3989 $content = sanitize_rss($link, $latest_item->get_description());
f67d9754
AD
3990 } else {
3991 $release_url = sanitize_rss($link, $latest_item["link"]);
3992 $content = sanitize_rss($link, $latest_item["description"]);
3993 }
48e1a342 3994
a41d2c65 3995 if (version_compare(VERSION, $latest_version) == -1) {
73495fd1
AD
3996 return sprintf("New version of Tiny-Tiny RSS (%s) is available:",
3997 $latest_version)."<div class='milestoneDetails'>$content</div>";
3998 } else {
3999 return false;
4000 }
b72c3ef8
AD
4001 }
4002 }
472782e8 4003
18eddb2c
AD
4004 function markArticlesById($link, $ids, $cmode) {
4005
4006 $tmp_ids = array();
4007
4008 foreach ($ids as $id) {
4009 array_push($tmp_ids, "ref_id = '$id'");
4010 }
4011
4012 $ids_qpart = join(" OR ", $tmp_ids);
4013
4014 if ($cmode == 0) {
4015 db_query($link, "UPDATE ttrss_user_entries SET
4016 marked = false,last_read = NOW()
4017 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4018 } else if ($cmode == 1) {
4019 db_query($link, "UPDATE ttrss_user_entries SET
4020 marked = true
4021 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4022 } else {
4023 db_query($link, "UPDATE ttrss_user_entries SET
4024 marked = NOT marked,last_read = NOW()
4025 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4026 }
4027 }
4028
e4f4b46f
AD
4029 function publishArticlesById($link, $ids, $cmode) {
4030
4031 $tmp_ids = array();
4032
4033 foreach ($ids as $id) {
4034 array_push($tmp_ids, "ref_id = '$id'");
4035 }
4036
4037 $ids_qpart = join(" OR ", $tmp_ids);
4038
4039 if ($cmode == 0) {
4040 db_query($link, "UPDATE ttrss_user_entries SET
4041 published = false,last_read = NOW()
4042 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4043 } else if ($cmode == 1) {
4044 db_query($link, "UPDATE ttrss_user_entries SET
4045 published = true
4046 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4047 } else {
4048 db_query($link, "UPDATE ttrss_user_entries SET
4049 published = NOT published,last_read = NOW()
4050 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
4051 }
4052 }
4053
9968d46f
AD
4054 function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) {
4055
4056 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
472782e8
AD
4057
4058 $tmp_ids = array();
4059
4060 foreach ($ids as $id) {
4061 array_push($tmp_ids, "ref_id = '$id'");
4062 }
4063
4064 $ids_qpart = join(" OR ", $tmp_ids);
4065
4066 if ($cmode == 0) {
4067 db_query($link, "UPDATE ttrss_user_entries SET
4068 unread = false,last_read = NOW()
9968d46f 4069 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
4070 } else if ($cmode == 1) {
4071 db_query($link, "UPDATE ttrss_user_entries SET
4072 unread = true
9968d46f 4073 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8
AD
4074 } else {
4075 db_query($link, "UPDATE ttrss_user_entries SET
4076 unread = NOT unread,last_read = NOW()
9968d46f 4077 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
472782e8 4078 }
0737b95a
AD
4079
4080 /* update ccache */
4081
4082 $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries
4083 WHERE ($ids_qpart) AND owner_uid = $owner_uid");
4084
4085 while ($line = db_fetch_assoc($result)) {
4086 ccache_update($link, $line["feed_id"], $owner_uid);
4087 }
472782e8
AD
4088 }
4089
e097e8be
AD
4090 function catchupArticleById($link, $id, $cmode) {
4091
4092 if ($cmode == 0) {
4093 db_query($link, "UPDATE ttrss_user_entries SET
4094 unread = false,last_read = NOW()
4095 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4096 } else if ($cmode == 1) {
4097 db_query($link, "UPDATE ttrss_user_entries SET
4098 unread = true
4099 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4100 } else {
4101 db_query($link, "UPDATE ttrss_user_entries SET
4102 unread = NOT unread,last_read = NOW()
4103 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
4104 }
ab197ae1
AD
4105
4106 $feed_id = getArticleFeed($link, $id);
4107 ccache_update($link, $feed_id, $_SESSION["uid"]);
e097e8be
AD
4108 }
4109
1f64b1be
AD
4110 function make_guid_from_title($title) {
4111 return preg_replace("/[ \"\',.:;]/", "-",
fefef828 4112 mb_strtolower(strip_tags($title), 'utf-8'));
1f64b1be
AD
4113 }
4114
11befbb2 4115 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
0ef68e6f
AD
4116 $feed_id, $is_cat, $search, $match_on,
4117 $search_mode) {
e6c115b2 4118
0ef68e6f 4119 print "<div class=\"headlinesSubToolbar\">";
11befbb2 4120
e6c115b2
AD
4121 $page_prev_link = "javascript:viewFeedGoPage(-1)";
4122 $page_next_link = "javascript:viewFeedGoPage(1)";
4123 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 4124
eb28b131
AD
4125 $catchup_page_link = "javascript:catchupPage()";
4126 $catchup_feed_link = "javascript:catchupCurrentFeed()";
a5ae125a 4127 $catchup_sel_link = "javascript:catchupSelection()";
c6008b62 4128
e04c18a2
AD
4129 $archive_sel_link = "javascript:archiveSelection()";
4130 $delete_sel_link = "javascript:deleteSelection()";
4131
11befbb2
AD
4132 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
4133
c6008b62
AD
4134 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
4135 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
4136 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
e75d70b5 4137 $sel_inv_link = "javascript:invertHeadlineSelection()";
11befbb2 4138
c6008b62
AD
4139 $tog_unread_link = "javascript:selectionToggleUnread()";
4140 $tog_marked_link = "javascript:selectionToggleMarked()";
e4f4b46f 4141 $tog_published_link = "javascript:selectionTogglePublished()";
11befbb2 4142
c6008b62 4143 } else {
11befbb2 4144
c6008b62
AD
4145 $sel_all_link = "javascript:cdmSelectArticles('all')";
4146 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
4147 $sel_none_link = "javascript:cdmSelectArticles('none')";
4148
e75d70b5
AD
4149 $sel_inv_link = "javascript:invertHeadlineSelection()";
4150
c6008b62
AD
4151 $tog_unread_link = "javascript:selectionToggleUnread(true)";
4152 $tog_marked_link = "javascript:selectionToggleMarked(true)";
e4f4b46f 4153 $tog_published_link = "javascript:selectionTogglePublished(true)";
c6008b62
AD
4154
4155 }
4156
0ef68e6f
AD
4157 print "<div id=\"subtoolbar_ftitle\">";
4158
4159 if ($feed_site_url) {
4160 if (!$bottom) {
4161 $target = "target=\"_blank\"";
4162 }
4163 print "<a $target href=\"$feed_site_url\">".
4164 truncate_string($feed_title,30)."</a>";
4165 } else {
5163fc70
AD
4166 if ($feed_id < -10) {
4167 $label_id = -11-$feed_id;
4168
4169 $result = db_query($link, "SELECT fg_color, bg_color
4170 FROM ttrss_labels2 WHERE id = '$label_id' AND owner_uid = " .
4171 $_SESSION["uid"]);
4172
4173 if (db_num_rows($result) != 0) {
4174 $fg_color = db_fetch_result($result, 0, "fg_color");
4175 $bg_color = db_fetch_result($result, 0, "bg_color");
4176
4177 print "<span style='background : $bg_color; color : $fg_color'>";
4178 print $feed_title;
4179 print "</span>";
4180 } else {
4181 print $feed_title;
4182 }
4183
4184 } else {
4185 print $feed_title;
4186 }
0ef68e6f
AD
4187 }
4188
4189 if ($search) {
4190 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
4191 }
4192
4193 print "
4194 <a target=\"_blank\"
4195 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
4196 <img class=\"noborder\"
4197 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
4198 </a>";
b8a637f3 4199
0ef68e6f 4200 print "</div>";
ceb30ba4 4201
d75ed3eb
AD
4202 print __('Select:')."
4203 <a href=\"$sel_all_link\">".__('All')."</a>,
4204 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
4205 <a href=\"$sel_inv_link\">".__('Invert')."</a>,
4206 <a href=\"$sel_none_link\">".__('None')."</a></li>";
bf3c9838 4207
d75ed3eb 4208 print "&nbsp;&nbsp;";
bf3c9838 4209
d75ed3eb
AD
4210 print "<span
4211 onmouseover=\"enable_selection(false)\"
4212 onmouseout=\"enable_selection(true)\"
4213 onclick=\"toggleHeadlineActions()\" id=\"headlineActionsDrop\">".
7d939be7
AD
4214 __("Actions...") . "&nbsp;&nbsp;<img width='11' height'7'
4215 src=\"images/down_arrow.png\">
d75ed3eb 4216 </span>";
bf3c9838 4217
d75ed3eb 4218 print "<ul id=\"headlineActionsBody\" style=\"display : none\">";
bf3c9838 4219
d75ed3eb
AD
4220 print "<li class=\"insensitive\">".__('Selection toggle:')."</li>
4221 <li onclick=\"$tog_unread_link\">&nbsp;&nbsp;".__('Unread')."</li>
4222 <li onclick=\"$tog_marked_link\">&nbsp;&nbsp;".__('Starred')."</li>
4223 <li onclick=\"$tog_published_link\">&nbsp;&nbsp;".__('Published')."</li>
938052ba
AD
4224 <li class=\"insensitive\">".__('Selection:')."</li>
4225 <li onclick=\"$catchup_sel_link\">&nbsp;&nbsp;".__('Mark as read')."</li>";
bf3c9838 4226
938052ba
AD
4227// print "<li onclick=\"$catchup_feed_link\">&nbsp;&nbsp;".__('Entire feed').
4228// "</li>";
bf3c9838 4229
e04c18a2 4230 if ($feed_id != "0") {
938052ba 4231 print "<li onclick=\"$archive_sel_link\">&nbsp;&nbsp;".__('Archive')."</li>";
e04c18a2 4232 } else {
938052ba 4233 print "<li onclick=\"$archive_sel_link\">&nbsp;&nbsp;".__('Move back')."</li>";
84d7198a 4234 print "<li onclick=\"$delete_sel_link\">&nbsp;&nbsp;".__('Delete')."</li>";
e04c18a2 4235
84d7198a 4236 }
938052ba 4237
d75ed3eb
AD
4238 //print "<li><span class=\"insensitive\">--------</span></li>";
4239 print "<li class=\"insensitive\">".__('Assign label:')."</li>";
bf3c9838 4240
79c88e11 4241 print_labels_headlines_dropdown($link, $feed_id);
c6008b62 4242
d75ed3eb 4243 print "</ul>";
11befbb2 4244
0ef68e6f 4245 print "</div>";
11befbb2
AD
4246 }
4247
bba7c4bf
AD
4248 function printCategoryHeader($link, $cat_id, $hidden = false, $can_browse = true) {
4249
4250 $tmp_category = getCategoryTitle($link, $cat_id);
cc914918
AD
4251
4252 if ($cat_id > 0) {
4253 $cat_unread = ccache_find($link, $cat_id, $_SESSION["uid"], true);
b2531a28 4254 } else if ($cat_id == 0 || $cat_id == -2) {
cc914918
AD
4255 $cat_unread = getCategoryUnread($link, $cat_id);
4256 }
bba7c4bf
AD
4257
4258 if ($hidden) {
4259 $holder_style = "display:none;";
66a251f9 4260 $ellipsis = "…";
bba7c4bf
AD
4261 } else {
4262 $holder_style = "";
4263 $ellipsis = "";
4264 }
4265
4266 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
4267
bba7c4bf 4268 if ($can_browse) {
4c41e58f
AD
4269 $browse_cat_link = "onclick=\"javascript:viewCategory($cat_id)\"";
4270 $inner_title_class = "catTitle";
bba7c4bf 4271 } else {
4c41e58f
AD
4272 $browse_cat_link = "";
4273 $inner_title_class = "catTitleNL";
bba7c4bf
AD
4274 }
4275
2baedabe 4276 $cat_class = "feedCat";
364e391e
AD
4277
4278 print "<li class=\"$cat_class\" id=\"FCAT-$cat_id\">
98fb6193 4279 <img onclick=\"toggleCollapseCat($cat_id)\" class=\"catCollapse\"
4c41e58f
AD
4280 title=\"".__('Click to collapse category')."\"
4281 src=\"images/cat-collapse.png\"><span class=\"$inner_title_class\"
c6dbeedc 4282 id=\"FCATN-$cat_id\" $browse_cat_link/>$tmp_category</span>";
4c41e58f
AD
4283
4284 print "<span id=\"FCAP-$cat_id\">";
4285
dda1396f 4286 print " <span id=\"FCATCTR-$cat_id\"
bba7c4bf
AD
4287 class=\"$catctr_class\">($cat_unread)</span> $ellipsis";
4288
4c41e58f 4289 print "</span>";
bba7c4bf 4290
782ddd70 4291 //print "</li>";
bba7c4bf 4292
60ea2377 4293 print "<ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\" style='$holder_style'>";
7abee14f 4294
bba7c4bf
AD
4295 }
4296
f407c086
AD
4297 function outputFeedList($link, $tags = false) {
4298
3bd9a780 4299 print "<ul class=\"feedList\" id=\"feedList\">";
f407c086
AD
4300
4301 $owner_uid = $_SESSION["uid"];
4302
cf4d339c 4303 /* virtual feeds */
f407c086 4304
cf4d339c 4305 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3b086176 4306
57937c42 4307 $cat_hidden = get_pref($link, "_COLLAPSED_SPECIAL");
3b086176 4308
bba7c4bf 4309 printCategoryHeader($link, -1, $cat_hidden, false);
cf4d339c 4310 }
f407c086 4311
e1050aec 4312 foreach (array(-4, -3, -1, -2, 0) as $i) {
4bee8b5f
AD
4313 printFeedEntry($i, "virt", false, false,
4314 false, $link);
4315 }
e4f4b46f 4316
cf4d339c 4317 if (get_pref($link, 'ENABLE_FEED_CATS')) {
8b803aa2 4318 print "</ul></li>";
cf4d339c 4319 }
f407c086 4320
cf4d339c 4321 if (!$tags) {
f407c086 4322
ceb30ba4 4323
2eb9c95c 4324 $result = db_query($link, "SELECT * FROM
ceb30ba4 4325 ttrss_labels2 WHERE owner_uid = '$owner_uid' ORDER by caption");
f407c086
AD
4326
4327 if (db_num_rows($result) > 0) {
4328 if (get_pref($link, 'ENABLE_FEED_CATS')) {
bd64489f 4329
57937c42 4330 $cat_hidden = get_pref($link, "_COLLAPSED_LABELS");
bd64489f 4331
e6a38cde 4332 printCategoryHeader($link, -2, $cat_hidden, true);
bd64489f 4333
f407c086 4334 } else {
3bd9a780 4335 print "<li><hr></li>";
f407c086
AD
4336 }
4337 }
4338
4339 while ($line = db_fetch_assoc($result)) {
4340
f407c086
AD
4341 $label_id = -$line['id'] - 11;
4342 $count = getFeedUnread($link, $label_id);
f407c086
AD
4343
4344 printFeedEntry($label_id,
4bee8b5f
AD
4345 "label", $line["caption"],
4346 $count, false, $link,
2eb9c95c
AD
4347 false, false, false,
4348 $line['fg_color'], $line['bg_color']);
f407c086
AD
4349
4350 }
4351
4352 if (db_num_rows($result) > 0) {
4353 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4354 print "</ul>";
4355 }
ceb30ba4 4356 }
f407c086 4357
f407c086
AD
4358
4359 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4360 print "<li><hr></li>";
f407c086
AD
4361 }
4362
4363 if (get_pref($link, 'ENABLE_FEED_CATS')) {
4364 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
9d393c84 4365 $order_by_qpart = "order_id,category,unread DESC,title";
f407c086 4366 } else {
9d393c84 4367 $order_by_qpart = "order_id,category,title";
f407c086
AD
4368 }
4369 } else {
4370 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
4371 $order_by_qpart = "unread DESC,title";
4372 } else {
4373 $order_by_qpart = "title";
4374 }
4375 }
4376
14073c0a
AD
4377 $age_qpart = getMaxAgeSubquery();
4378
99509451 4379 $query = "SELECT ttrss_feeds.*,
fc2b26a6 4380 ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated_noms,
f407c086
AD
4381 cat_id,last_error,
4382 ttrss_feed_categories.title AS category,
d7135e2a
AD
4383 ttrss_feed_categories.collapsed,
4384 value AS unread
4385 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
4386 ON (ttrss_feed_categories.id = cat_id)
4387 LEFT JOIN ttrss_counters_cache
4388 ON
4389 (ttrss_feeds.id = feed_id)
f407c086 4390 WHERE
f407c086 4391 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
99509451
AD
4392 ORDER BY $order_by_qpart";
4393
4394 $result = db_query($link, $query);
f407c086 4395
b4e75b2a 4396 $actid = $_REQUEST["actid"];
f407c086
AD
4397
4398 /* real feeds */
4399
4400 $lnum = 0;
4401
4402 $total_unread = 0;
4403
4404 $category = "";
4405
4406 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
4407
4408 while ($line = db_fetch_assoc($result)) {
4409
70c9b173 4410 $feed = htmlspecialchars(trim($line["title"]));
0f39ae20
AD
4411
4412 if (!$feed) $feed = "[Untitled]";
4413
f407c086 4414 $feed_id = $line["id"];
d7135e2a 4415 $unread = $line["unread"];
f407c086 4416
b4e75b2a 4417 $subop = $_REQUEST["subop"];
f407c086
AD
4418
4419 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
4420 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
4421 } else {
4422 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
4423 }
4424
4425 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
4426
4427 if ($rtl_content) {
4428 $rtl_tag = "dir=\"RTL\"";
4429 } else {
4430 $rtl_tag = "";
4431 }
4432
4433 $tmp_result = db_query($link,
de0a2122
AD
4434 "SELECT SUM(value) AS unread FROM ttrss_feeds, ttrss_counters_cache
4435 WHERE parent_feed = '$feed_id' AND feed_id = id");
f407c086 4436
de0a2122
AD
4437 $unread += db_fetch_result($tmp_result, 0, "unread");
4438
f407c086
AD
4439 $cat_id = $line["cat_id"];
4440
4441 $tmp_category = $line["category"];
4442
4443 if (!$tmp_category) {
d1db26aa 4444 $tmp_category = __("Uncategorized");
f407c086
AD
4445 }
4446
4447 // $class = ($lnum % 2) ? "even" : "odd";
4448
4449 if ($line["last_error"]) {
4450 $class = "error";
4451 } else {
4452 $class = "feed";
4453 }
4454
f407c086
AD
4455 if ($actid == $feed_id) {
4456 $class .= "Selected";
4457 }
4458
4459 $total_unread += $unread;
4460
4461 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
4462
4463 if ($category) {
8b803aa2 4464 print "</ul></li>";
f407c086
AD
4465 }
4466
4467 $category = $tmp_category;
4468
7abee14f 4469 $collapsed = sql_bool_to_bool($line["collapsed"]);
f407c086
AD
4470
4471 // workaround for NULL category
d1db26aa 4472 if ($category == __("Uncategorized")) {
57937c42 4473 $collapsed = get_pref($link, "_COLLAPSED_UNCAT");
f407c086
AD
4474 }
4475
22fdebff 4476 $cat_id = (int) $cat_id;
f407c086 4477
7abee14f
AD
4478 printCategoryHeader($link, $cat_id, $collapsed, true);
4479
f407c086
AD
4480 }
4481
4482 printFeedEntry($feed_id, $class, $feed, $unread,
4bee8b5f 4483 false, $link, $rtl_content,
f407c086
AD
4484 $last_updated, $line["last_error"]);
4485
4486 ++$lnum;
4487 }
4488
4489 if (db_num_rows($result) == 0) {
3bd9a780 4490 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
4491 }
4492
4493 } else {
4494
4495 // tags
4496
4497/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
4498 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
4499 post_int_id = ttrss_user_entries.int_id AND
4500 unread = true AND ref_id = ttrss_entries.id
4501 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
4502 UNION
4503 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
4504 ORDER BY tag_name"); */
4505
4506 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 4507 print "<li class=\"feedCat\">".__('Tags')."</li>";
60ea2377 4508 print "<ul class=\"feedCatList\">";
f407c086
AD
4509 }
4510
14073c0a
AD
4511 $age_qpart = getMaxAgeSubquery();
4512
f407c086 4513 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
14073c0a
AD
4514 FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
4515 AND ref_id = id AND $age_qpart
f407c086 4516 AND unread = true)) AS count FROM ttrss_tags
ef1ac7c7
AD
4517 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name
4518 ORDER BY count DESC LIMIT 50");
f407c086
AD
4519
4520 $tags = array();
4521
4522 while ($line = db_fetch_assoc($result)) {
4523 $tags[$line["tag_name"]] += $line["count"];
4524 }
4525
4526 foreach (array_keys($tags) as $tag) {
4527
4528 $unread = $tags[$tag];
f407c086 4529 $class = "tag";
326469fc 4530
f407c086
AD
4531 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
4532
4533 }
4534
4535 if (db_num_rows($result) == 0) {
4536 print "<li>No tags to display.</li>";
4537 }
4538
4539 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3bd9a780 4540 print "</ul>";
f407c086
AD
4541 }
4542
4543 }
4544
4545 print "</ul>";
4546
4547 }
4548
bc976a8c 4549 function get_article_tags($link, $id, $owner_uid = 0) {
0b126ac2 4550
bd3f2ade
AD
4551 global $memcache;
4552
0b126ac2
AD
4553 $a_id = db_escape_string($id);
4554
bc976a8c
AD
4555 if (!$owner_uid) $owner_uid = $_SESSION["uid"];
4556
bd3f2ade 4557 $query = "SELECT DISTINCT tag_name,
0c3d1c68 4558 owner_uid as owner FROM
0b126ac2 4559 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
bd3f2ade 4560 ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name";
0b126ac2 4561
bd3f2ade 4562 $obj_id = md5("TAGS:$owner_uid:$id");
0b126ac2 4563 $tags = array();
bd3f2ade
AD
4564
4565 if ($memcache && $obj = $memcache->get($obj_id)) {
4566 $tags = $obj;
4567 } else {
4568 $tmp_result = db_query($link, $query);
4569
4570 while ($tmp_line = db_fetch_assoc($tmp_result)) {
4571 array_push($tags, $tmp_line["tag_name"]);
4572 }
4573
4574 if ($memcache) $memcache->add($obj_id, $tags, 0, 3600);
0b126ac2
AD
4575 }
4576
4577 return $tags;
4578 }
4579
d62a3b63
AD
4580 function trim_value(&$value) {
4581 $value = trim($value);
4582 }
4583
4584 function trim_array($array) {
4585 $tmp = $array;
4586 array_walk($tmp, 'trim_value');
4587 return $tmp;
4588 }
4589
be832a1a 4590 function tag_is_valid($tag) {
ef063748
AD
4591 if ($tag == '') return false;
4592 if (preg_match("/^[0-9]*$/", $tag)) return false;
41f7498a 4593 if (mb_strlen($tag) > 250) return false;
ef063748 4594
31365729
AD
4595 if (function_exists('iconv')) {
4596 $tag = iconv("utf-8", "utf-8", $tag);
4597 }
4598
ef063748
AD
4599 if (!$tag) return false;
4600
4601 return true;
be832a1a
AD
4602 }
4603
afb12ed0
AD
4604 function render_login_form($link, $mobile = 0) {
4605 switch ($mobile) {
4606 case 0:
793185a9 4607 require_once "login_form.php";
afb12ed0
AD
4608 break;
4609 case 1:
793185a9 4610 require_once "mobile/login_form.php";
afb12ed0
AD
4611 break;
4612 case 2:
4613 require_once "mobile/classic/login_form.php";
793185a9 4614 }
01a87dff
AD
4615 }
4616
dc56b3b7
AD
4617 // from http://developer.apple.com/internet/safari/faq.html
4618 function no_cache_incantation() {
4619 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
4620 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
4621 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
4622 header("Cache-Control: post-check=0, pre-check=0", false);
4623 header("Pragma: no-cache"); // HTTP/1.0
4624 }
4625
42395d28 4626 function format_warning($msg, $id = "") {
883fee8d 4627 global $link;
42395d28 4628 return "<div class=\"warning\" id=\"$id\">
883fee8d 4629 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
0d32b41e
AD
4630 }
4631
4632 function format_notice($msg) {
883fee8d
AD
4633 global $link;
4634 return "<div class=\"notice\" id=\"$id\">
4635 <img src=\"".theme_image($link, "images/sign_info.png")."\">$msg</div>";
0d32b41e
AD
4636 }
4637
68d2f95e 4638 function format_error($msg) {
883fee8d
AD
4639 global $link;
4640 return "<div class=\"error\" id=\"$id\">
4641 <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>";
68d2f95e
AD
4642 }
4643
4dccf1ed
AD
4644 function print_notice($msg) {
4645 return print format_notice($msg);
4646 }
4647
4648 function print_warning($msg) {
4649 return print format_warning($msg);
4650 }
4651
68d2f95e
AD
4652 function print_error($msg) {
4653 return print format_error($msg);
4654 }
4655
4656
4dccf1ed
AD
4657 function T_sprintf() {
4658 $args = func_get_args();
4659 return vsprintf(__(array_shift($args)), $args);
4660 }
4661
51682b23
AD
4662 function format_inline_player($link, $url, $ctype) {
4663
4664 $entry = "";
4665
4666 if (($ctype == __("audio/mpeg")) && (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
4667
4668 $entry .= "<object type=\"application/x-shockwave-flash\"
4669 data=\"extras/button/musicplayer.swf?song_url=$url\"
4670 width=\"17\" height=\"17\">
4671 <param name=\"movie\" value=\"extras/button/musicplayer.swf?song_url=$url\" /> </object>";
4672 }
4673
4674 /*
4675
4676 if (substr($ctype,0,6)=="audio/" || $ctype=="application/ogg" || $ctype=="application/x-ogg") {
4677 $entry .= "<audio controls=\"controls\"><source src=\"$url\" type=\"$ctype\" />";
4678 if (($ctype == __("audio/mpeg")) &&
4679 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
4680 $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>";
4681 }
4682 $entry .= "</audio> ";
4683 if (($ctype == __("audio/mpeg")) &&
4684 (get_pref($link, "ENABLE_FLASH_PLAYER")) ) {
4685 $entry .= "<a id='switchToFlashLink' href='#' onclick='return switchToFlash(this)'>".__('Switch to Flash Player')."</a>";
4686 $entry .= "<script type='text/javascript'>html5AudioOrFlash('$ctype');</script>";
4687 }
4688 } elseif (substr($ctype,0,6)=="video/") {
4689 $entry .= "<video controls=\"controls\"><source src=\"$url\" type=\"$ctype\" />";
4690 $entry .= "</video>";
4691 } */
4692
4693
4694
4695 return $entry;
4696 }
4697
eedfb635
AD
4698 function outputArticleXML($link, $id, $feed_id, $mark_as_read = true,
4699 $zoom_mode = false) {
3de0261a 4700
10eb9da8 4701 /* we can figure out feed_id from article id anyway, why do we
e04c18a2 4702 * pass feed_id here? let's ignore the argument :( */
10eb9da8
AD
4703
4704 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
4705 WHERE ref_id = '$id'");
4706
e04c18a2 4707 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
10eb9da8 4708
eedfb635 4709 if (!$zoom_mode) { print "<article id='$id'><![CDATA["; };
3de0261a 4710
54e61a68 4711 $result = db_query($link, "SELECT rtl_content, always_display_enclosures FROM ttrss_feeds
3de0261a
AD
4712 WHERE id = '$feed_id' AND owner_uid = " . $_SESSION["uid"]);
4713
4714 if (db_num_rows($result) == 1) {
4715 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
54e61a68 4716 $always_display_enclosures = sql_bool_to_bool(db_fetch_result($result, 0, "always_display_enclosures"));
3de0261a
AD
4717 } else {
4718 $rtl_content = false;
54e61a68 4719 $always_display_enclosures = false;
3de0261a
AD
4720 }
4721
4722 if ($rtl_content) {
4723 $rtl_tag = "dir=\"RTL\"";
4724 $rtl_class = "RTL";
4725 } else {
4726 $rtl_tag = "";
4727 $rtl_class = "";
4728 }
4729
4730 if ($mark_as_read) {
4731 $result = db_query($link, "UPDATE ttrss_user_entries
4732 SET unread = false,last_read = NOW()
4733 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
8a4c759e
AD
4734
4735 ccache_update($link, $feed_id, $_SESSION["uid"]);
3de0261a
AD
4736 }
4737
4738 $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id,
fc2b26a6 4739 ".SUBSTRING_FOR_DATE."(updated,1,16) as updated,
3de0261a
AD
4740 (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url,
4741 num_comments,
c7e51de1 4742 author,
ef83538d 4743 orig_feed_id,
c7e51de1 4744 note
3de0261a
AD
4745 FROM ttrss_entries,ttrss_user_entries
4746 WHERE id = '$id' AND ref_id = id AND owner_uid = " . $_SESSION["uid"]);
4747
4748 if ($result) {
4749
3de0261a
AD
4750 $line = db_fetch_assoc($result);
4751
4752 if ($line["icon_url"]) {
4753 $feed_icon = "<img class=\"feedIcon\" src=\"" . $line["icon_url"] . "\">";
4754 } else {
4755 $feed_icon = "&nbsp;";
4756 }
4757
3de0261a
AD
4758 $num_comments = $line["num_comments"];
4759 $entry_comments = "";
4760
4761 if ($num_comments > 0) {
4762 if ($line["comments"]) {
4763 $comments_url = $line["comments"];
4764 } else {
4765 $comments_url = $line["link"];
4766 }
7514749d 4767 $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>";
3de0261a
AD
4768 } else {
4769 if ($line["comments"] && $line["link"] != $line["comments"]) {
7514749d 4770 $entry_comments = "<a target='_blank' href=\"".$line["comments"]."\">comments</a>";
3de0261a
AD
4771 }
4772 }
4773
eedfb635
AD
4774 if ($zoom_mode) {
4775 header("Content-Type: text/html");
4776 print "<html><head>
5bb0cc8e 4777 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
eedfb635
AD
4778 <title>Tiny Tiny RSS - ".$line["title"]."</title>
4779 <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\">
4780 </head><body>";
4781 }
4782
4783
3de0261a
AD
4784 print "<div class=\"postReply\">";
4785
94047498
AD
4786 print "<div class=\"postHeader\" onmouseover=\"enable_resize(true)\"
4787 onmouseout=\"enable_resize(false)\">";
3de0261a
AD
4788
4789 $entry_author = $line["author"];
4790
4791 if ($entry_author) {
60164936 4792 $entry_author = __(" - ") . $entry_author;
3de0261a
AD
4793 }
4794
4795 $parsed_updated = date(get_pref($link, 'LONG_DATE_FORMAT'),
4796 strtotime($line["updated"]));
4797
4798 print "<div class=\"postDate$rtl_class\">$parsed_updated</div>";
4799
4800 if ($line["link"]) {
7514749d 4801 print "<div clear='both'><a target='_blank' href=\"" . $line["link"] . "\">" .
06202d88 4802 $line["title"] . "</a><span class='author'>$entry_author</span></div>";
3de0261a
AD
4803 } else {
4804 print "<div clear='both'>" . $line["title"] . "$entry_author</div>";
4805 }
4806
307d187c 4807 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
e7544143 4808
3de0261a
AD
4809 if (!$entry_comments) $entry_comments = "&nbsp;"; # placeholder
4810
5f014cf1 4811 print "<div style='float : right'>
e9823609
AD
4812 <img src='".theme_image($link, 'images/tag.png')."'
4813 class='tagsPic' alt='Tags' title='Tags'>&nbsp;";
eedfb635
AD
4814
4815 if (!$zoom_mode) {
307d187c 4816 print "<span id=\"ATSTR-$id\">$tags_str</span>
eedfb635 4817 <a title=\"".__('Edit tags for this article')."\"
4710e3dc
AD
4818 href=\"javascript:editArticleTags($id, $feed_id)\">(+)</a>";
4819
e9823609
AD
4820 print "<img src=\"".theme_image($link, 'images/art-zoom.png')."\"
4821 class='tagsPic' style=\"cursor : pointer\" style=\"cursor : pointer\"
eedfb635
AD
4822 onclick=\"zoomToArticle($id)\"
4823 alt='Zoom' title='".__('Show article summary in new window')."'>";
c7e51de1
AD
4824
4825 $note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
4826
e9823609
AD
4827 print "<img src=\"".theme_image($link, 'images/art-pub-note.png')."\"
4828 class='tagsPic' style=\"cursor : pointer\" style=\"cursor : pointer\"
c7e51de1
AD
4829 onclick=\"publishWithNote($id, '$note_escaped')\"
4830 alt='PubNote' title='".__('Publish article with a note')."'>";
4831
24ecbcae
AD
4832 } else {
4833 $tags_str = strip_tags($tags_str);
4834 print "<span id=\"ATSTR-$id\">$tags_str</span>";
eedfb635
AD
4835 }
4836 print "</div>";
4837 print "<div clear='both'>$entry_comments</div>";
3de0261a 4838
ef83538d
AD
4839 if ($line["orig_feed_id"]) {
4840
4841 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
4842 WHERE id = ".$line["orig_feed_id"]);
4843
4844 if (db_num_rows($tmp_result) != 0) {
4845
4846 print "<div clear='both'>";
4847 print __("Originally from:");
4848
4849 print "&nbsp;";
4850
4851 $tmp_line = db_fetch_assoc($tmp_result);
4852
4853 print "<a target='_blank'
4854 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
4855 $tmp_line['title'] . "</a>";
4856
4857 print "&nbsp;";
4858
4859 print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
4860 print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
4861
4862 print "</div>";
4863 }
4864 }
4865
3de0261a
AD
4866 print "</div>";
4867
4868 print "<div class=\"postIcon\">" . $feed_icon . "</div>";
64ab16ac 4869
3de0261a 4870 print "<div class=\"postContent\">";
64ab16ac 4871
c54526fe 4872 $article_content = sanitize_rss($link, $line["content"]);
c41890b0 4873
c7e51de1
AD
4874 print "<div id=\"POSTNOTE-$id\">";
4875 if ($line['note']) {
4876 print format_article_note($id, $line['note']);
4877 }
4878 print "</div>";
4879
db54143e
AD
4880 print $article_content;
4881
be35798b
AD
4882// $result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4883// post_id = '$id' AND content_url != ''");
ce53e200 4884
be35798b
AD
4885 $result = get_article_enclosures($link, $id);
4886
4887// if (db_num_rows($result) > 0) {
4888
4889 if (count($result) > 0) {
ce53e200 4890
9c5ee7e1 4891 $entries_html = array();
ce53e200
AD
4892 $entries = array();
4893
be35798b
AD
4894 //while ($line = db_fetch_assoc($result)) {
4895 foreach ($result as $line) {
ce53e200
AD
4896
4897 $url = $line["content_url"];
4752b041
AD
4898 $ctype = $line["content_type"];
4899
4900 if (!$ctype) $ctype = __("unknown type");
ce53e200
AD
4901
4902 $filename = substr($url, strrpos($url, "/")+1);
4903
51682b23 4904 $entry = format_inline_player($link, $url, $ctype);
8dccabed 4905
51682b23 4906 $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4752b041 4907 $filename . " (" . $ctype . ")" . "</a>";
ce53e200 4908
9c5ee7e1
AD
4909 array_push($entries_html, $entry);
4910
4911 $entry = array();
4912
4913 $entry["type"] = $ctype;
4914 $entry["filename"] = $filename;
4915 $entry["url"] = $url;
4916
ce53e200
AD
4917 array_push($entries, $entry);
4918 }
4919
9c5ee7e1
AD
4920 print "<div class=\"postEnclosures\">";
4921
fbaca246 4922 if (!get_pref($link, "STRIP_IMAGES")) {
44cfa025
AD
4923 if ($always_display_enclosures ||
4924 !preg_match("/<img/i", $article_content)) {
4925
fbaca246 4926 foreach ($entries as $entry) {
44cfa025
AD
4927
4928 if (preg_match("/image/", $entry["type"]) ||
4929 preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
4930
4931 print "<p><img
fbaca246 4932 alt=\"".htmlspecialchars($entry["filename"])."\"
44cfa025 4933 src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>";
fbaca246 4934 }
9c5ee7e1
AD
4935 }
4936 }
4937 }
4938
9c5ee7e1
AD
4939 if (db_num_rows($result) == 1) {
4940 print __("Attachment:") . " ";
4941 } else {
4942 print __("Attachments:") . " ";
4943 }
4944
4945 print join(", ", $entries_html);
ce53e200
AD
4946
4947 print "</div>";
4948 }
4949
4950 print "</div>";
3de0261a
AD
4951
4952 print "</div>";
4953
4954 }
4955
eedfb635
AD
4956 if (!$zoom_mode) {
4957 print "]]></article>";
4958 } else {
4959 print "
4960 <div style=\"text-align : center\">
2ae69126
AD
4961 <button onclick=\"return window.close()\">".
4962 __("Close this window")."</button></div>";
eedfb635
AD
4963 print "</body></html>";
4964
4965 }
3de0261a
AD
4966
4967 }
4968
4969 function outputHeadlinesList($link, $feed, $subop, $view_mode, $limit, $cat_view,
7b4d02a8
AD
4970 $next_unread_feed, $offset, $vgr_last_feed = false,
4971 $override_order = false) {
3de0261a 4972
52d7e7da
AD
4973 $disable_cache = false;
4974
46921916
AD
4975 $timing_info = getmicrotime();
4976
961f4c73
AD
4977 $topmost_article_ids = array();
4978
ac541432
AD
4979 if (!$offset) {
4980 $offset = 0;
4981 }
3de0261a
AD
4982
4983 if ($subop == "undefined") $subop = "";
4984
a9bcfb8f
AD
4985 $subop_split = split(":", $subop);
4986
3de0261a 4987 if ($subop == "CatchupSelected") {
b4e75b2a
AD
4988 $ids = split(",", db_escape_string($_REQUEST["ids"]));
4989 $cmode = sprintf("%d", $_REQUEST["cmode"]);
3de0261a
AD
4990
4991 catchupArticlesById($link, $ids, $cmode);
4992 }
4993
4994 if ($subop == "ForceUpdate" && sprintf("%d", $feed) > 0) {
35bf080c 4995 update_generic_feed($link, $feed, $cat_view, true);
3de0261a
AD
4996 }
4997
4998 if ($subop == "MarkAllRead") {
4999 catchup_feed($link, $feed, $cat_view);
5000
5001 if (get_pref($link, 'ON_CATCHUP_SHOW_NEXT_FEED')) {
5002 if ($next_unread_feed) {
5003 $feed = $next_unread_feed;
5004 }
5005 }
5006 }
5007
a9bcfb8f
AD
5008 if ($subop_split[0] == "MarkAllReadGR") {
5009 catchup_feed($link, $subop_split[1], false);
5010 }
5011
5012
3de0261a
AD
5013 if ($feed_id > 0) {
5014 $result = db_query($link,
5015 "SELECT id FROM ttrss_feeds WHERE id = '$feed' LIMIT 1");
5016
5017 if (db_num_rows($result) == 0) {
5018 print "<div align='center'>".__('Feed not found.')."</div>";
5019 return;
5020 }
5021 }
5022
5023 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
10eb9da8 5024
3de0261a
AD
5025 $result = db_query($link, "SELECT rtl_content FROM ttrss_feeds
5026 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
5027
5028 if (db_num_rows($result) == 1) {
5029 $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content"));
5030 } else {
5031 $rtl_content = false;
5032 }
5033
5034 if ($rtl_content) {
5035 $rtl_tag = "dir=\"RTL\"";
5036 } else {
5037 $rtl_tag = "";
5038 }
5039 } else {
5040 $rtl_tag = "";
5041 $rtl_content = false;
5042 }
5043
5044 $script_dt_add = get_script_dt_add();
5045
5046 /// START /////////////////////////////////////////////////////////////////////////////////
5047
b4e75b2a 5048 $search = db_escape_string($_REQUEST["query"]);
52d7e7da
AD
5049
5050 if ($search) {
5051 $disable_cache = true;
5052 }
5053
b4e75b2a
AD
5054 $search_mode = db_escape_string($_REQUEST["search_mode"]);
5055 $match_on = db_escape_string($_REQUEST["match_on"]);
3de0261a
AD
5056
5057 if (!$match_on) {
5058 $match_on = "both";
5059 }
5060
5061 $real_offset = $offset * $limit;
5062
b4e75b2a 5063 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H0", $timing_info);
46921916 5064
3de0261a 5065 $qfh_ret = queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view,
7b4d02a8 5066 $search, $search_mode, $match_on, $override_order, $real_offset);
3de0261a 5067
b4e75b2a 5068 if ($_REQUEST["debug"]) $timing_info = print_checkpoint("H1", $timing_info);
46921916 5069
3de0261a
AD
5070 $result = $qfh_ret[0];
5071 $feed_title = $qfh_ret[1];
5072 $feed_site_url = $qfh_ret[2];
5073 $last_error = $qfh_ret[3];
f56e3080 5074
081e527d
AD
5075 $vgroup_last_feed = $vgr_last_feed;
5076
f56e3080
AD
5077 if ($feed == -2) {
5078 $feed_site_url = article_publish_url($link);
5079 }
5080
3de0261a
AD
5081 /// STOP //////////////////////////////////////////////////////////////////////////////////
5082
ac541432
AD
5083 if (!$offset) {
5084 print "<div id=\"headlinesContainer\" $rtl_tag>";
3de0261a 5085
ac541432
AD
5086 if (!$result) {
5087 print "<div align='center'>".__("Could not display feed (query failed). Please check label match syntax or local configuration.")."</div>";
5088 return;
5089 }
3de0261a 5090
0ef68e6f
AD
5091 print_headline_subtoolbar($link, $feed_site_url, $feed_title,
5092 $feed, $cat_view, $search, $match_on, $search_mode);
3de0261a 5093
ac541432
AD
5094 print "<div id=\"headlinesInnerContainer\" onscroll=\"headlines_scroll_handler()\">";
5095 }
3de0261a 5096
29dfb258
AD
5097 $headlines_count = db_num_rows($result);
5098
3de0261a
AD
5099 if (db_num_rows($result) > 0) {
5100
5101# print "\{$offset}";
5102
ac541432 5103 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
5104 print "<table class=\"headlinesList\" id=\"headlinesList\"
5105 cellspacing=\"0\">";
5106 }
5107
4ab4d364
AD
5108 $lnum = $limit*$offset;
5109
3de0261a
AD
5110 error_reporting (DEFAULT_ERROR_LEVEL);
5111
5112 $num_unread = 0;
6cfea5c7
AD
5113 $cur_feed_title = '';
5114
784ac51f
AD
5115 $fresh_intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE") * 60 * 60;
5116
3de0261a
AD
5117 while ($line = db_fetch_assoc($result)) {
5118
5119 $class = ($lnum % 2) ? "even" : "odd";
5120
5121 $id = $line["id"];
5122 $feed_id = $line["feed_id"];
961f4c73 5123
e2549229 5124 $labels = get_article_labels($link, $id);
e2549229 5125
2eb9c95c
AD
5126 $labels_str = "<span id=\"HLLCTR-$id\">";
5127 $labels_str .= format_article_labels($labels, $id);
f9247195 5128 $labels_str .= "</span>";
2eb9c95c 5129
961f4c73
AD
5130 if (count($topmost_article_ids) < 5) {
5131 array_push($topmost_article_ids, $id);
5132 }
5133
784ac51f 5134 if ($line["last_read"] == "" && !sql_bool_to_bool($line["unread"])) {
3de0261a 5135
883fee8d
AD
5136 $update_pic = "<img id='FUPDPIC-$id' src=\"".
5137 theme_image($link, 'images/updated.png')."\"
3de0261a
AD
5138 alt=\"Updated\">";
5139 } else {
5140 $update_pic = "<img id='FUPDPIC-$id' src=\"images/blank_icon.gif\"
5141 alt=\"Updated\">";
5142 }
784ac51f
AD
5143
5144 if (sql_bool_to_bool($line["unread"]) &&
5145 time() - strtotime($line["updated_noms"]) < $fresh_intl) {
5146
e9823609
AD
5147 $update_pic = "<img id='FUPDPIC-$id' src=\"".
5148 theme_image($link, 'images/fresh_sign.png')."\" alt=\"Fresh\">";
784ac51f 5149 }
3de0261a
AD
5150
5151 if ($line["unread"] == "t" || $line["unread"] == "1") {
5152 $class .= "Unread";
5153 ++$num_unread;
5154 $is_unread = true;
5155 } else {
5156 $is_unread = false;
5157 }
abd8a516 5158
3de0261a 5159 if ($line["marked"] == "t" || $line["marked"] == "1") {
e9823609
AD
5160 $marked_pic = "<img id=\"FMPIC-$id\"
5161 src=\"".theme_image($link, 'images/mark_set.png')."\"
5162 class=\"markedPic\" alt=\"Unstar article\"
5163 onclick='javascript:tMark($id)'>";
3de0261a 5164 } else {
e9823609
AD
5165 $marked_pic = "<img id=\"FMPIC-$id\"
5166 src=\"".theme_image($link, 'images/mark_unset.png')."\"
5167 class=\"markedPic\" alt=\"Star article\"
5168 onclick='javascript:tMark($id)'>";
3de0261a
AD
5169 }
5170
e4f4b46f 5171 if ($line["published"] == "t" || $line["published"] == "1") {
e9823609
AD
5172 $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($link,
5173 'images/pub_set.png')."\"
e4f4b46f 5174 class=\"markedPic\"
f5e0338d 5175 alt=\"Unpublish article\" onclick='javascript:tPub($id)'>";
e4f4b46f 5176 } else {
e9823609
AD
5177 $published_pic = "<img id=\"FPPIC-$id\" src=\"".theme_image($link,
5178 'images/pub_unset.png')."\"
e4f4b46f 5179 class=\"markedPic\"
f5e0338d 5180 alt=\"Publish article\" onclick='javascript:tPub($id)'>";
e4f4b46f
AD
5181 }
5182
e944346c 5183# $content_link = "<a target=\"_blank\" href=\"".$line["link"]."\">" .
3de0261a
AD
5184# $line["title"] . "</a>";
5185
f0971fc1
AD
5186# $content_link = "<a
5187# href=\"" . htmlspecialchars($line["link"]) . "\"
5188# onclick=\"view($id,$feed_id);\">" .
5189# $line["title"] . "</a>";
3de0261a
AD
5190
5191# $content_link = "<a href=\"javascript:viewContentUrl('".$line["link"]."');\">" .
5192# $line["title"] . "</a>";
5193
5194 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
46921916 5195 $updated_fmt = smart_date_time(strtotime($line["updated_noms"]));
3de0261a
AD
5196 } else {
5197 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
46921916 5198 $updated_fmt = date($short_date, strtotime($line["updated_noms"]));
3de0261a
AD
5199 }
5200
5201 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5202 $content_preview = truncate_string(strip_tags($line["content_preview"]),
5203 100);
5204 }
5205
ff6e357a
AD
5206 $score = $line["score"];
5207
883fee8d
AD
5208 $score_pic = theme_image($link,
5209 "images/" . get_score_pic($score));
546499a9 5210
9bf3f101
AD
5211/* $score_title = __("(Click to change)");
5212 $score_pic = "<img class='hlScorePic' src=\"images/$score_pic\"
5213 onclick=\"adjustArticleScore($id, $score)\" title=\"$score $score_title\">"; */
546499a9 5214
883fee8d 5215 $score_pic = "<img class='hlScorePic' src=\"$score_pic\"
9bf3f101 5216 title=\"$score\">";
ff6e357a 5217
5daa24f2
AD
5218 if ($score > 500) {
5219 $hlc_suffix = "H";
5220 } else if ($score < -100) {
5221 $hlc_suffix = "L";
5222 } else {
5223 $hlc_suffix = "";
5224 }
5225
3de0261a
AD
5226 $entry_author = $line["author"];
5227
5228 if ($entry_author) {
60164936 5229 $entry_author = " - $entry_author";
3de0261a
AD
5230 }
5231
7defa089 5232 $has_feed_icon = feed_has_icon($feed_id);
bd51294a
AD
5233
5234 if ($has_feed_icon) {
5235 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5236 } else {
5237 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
20be0cf8 5238 $feed_icon_img = "";
bd51294a
AD
5239 }
5240
3de0261a 5241 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
6cfea5c7 5242
d00f22ac 5243 if (get_pref($link, 'VFEED_GROUP_BY_FEED')) {
bb031f91 5244 if ($feed_id != $vgroup_last_feed && $line["feed_title"]) {
a9bcfb8f
AD
5245
5246 $cur_feed_title = $line["feed_title"];
081e527d 5247 $vgroup_last_feed = $feed_id;
a9bcfb8f 5248
962d8ba4 5249 $cur_feed_title = htmlspecialchars($cur_feed_title);
43fc671f 5250
af163b85 5251 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
a9bcfb8f 5252
6cfea5c7 5253 print "<tr class='feedTitle'><td colspan='7'>".
dc803347 5254 "<div style=\"float : right\">$feed_icon_img</div>".
6cfea5c7 5255 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
965fb2af 5256 $line["feed_title"]."</a> $vf_catchup_link</td></tr>";
6cfea5c7
AD
5257 }
5258 }
314fcd2b
AD
5259
5260 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5261 onmouseout='postMouseOut($id)'";
5262
5263 print "<tr class='$class' id='RROW-$id' $mouseover_attrs>";
3de0261a 5264
67343d9f 5265 print "<td class='hlUpdPic'>$update_pic</td>";
3de0261a
AD
5266
5267 print "<td class='hlSelectRow'>
67343d9f
AD
5268 <input type=\"checkbox\" onclick=\"tSR(this)\"
5269 id=\"RCHK-$id\">
3de0261a
AD
5270 </td>";
5271
5272 print "<td class='hlMarkedPic'>$marked_pic</td>";
e4f4b46f 5273 print "<td class='hlMarkedPic'>$published_pic</td>";
3de0261a 5274
df456bb0
AD
5275# if ($line["feed_title"]) {
5276# print "<td class='hlContent'>$content_link</td>";
5277# print "<td class='hlFeed'>
5278# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5279# truncate_string($line["feed_title"],30)."</a>&nbsp;</td>";
5280# } else {
5281
e04c18a2 5282 print "<td onclick='view($id)' class='hlContent$hlc_suffix' valign='middle' id='HLC-$id'>";
df456bb0 5283
f0971fc1
AD
5284 print "<a id=\"RTITLE-$id\"
5285 href=\"" . htmlspecialchars($line["link"]) . "\"
6e35a862 5286 onclick=\"return false\">" .
df456bb0
AD
5287 $line["title"];
5288
5289 if (get_pref($link, 'SHOW_CONTENT_PREVIEW')) {
5290 if ($content_preview) {
5291 print "<span class=\"contentPreview\"> - $content_preview</span>";
3de0261a 5292 }
3de0261a 5293 }
df456bb0
AD
5294
5295 print "</a>";
5296
e2549229
AD
5297 print $labels_str;
5298
df456bb0
AD
5299# <a href=\"javascript:viewfeed($feed_id, '', false)\">".
5300# $line["feed_title"]."</a>
5301
d00f22ac 5302 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
6cfea5c7
AD
5303 if ($line["feed_title"]) {
5304 print "<span class=\"hlFeed\">
5305 (<a href=\"javascript:viewfeed($feed_id, '', false)\">".
5306 $line["feed_title"]."</a>)
5307 </span>";
5308 }
df456bb0 5309 }
6e35a862
AD
5310
5311// print "<img id='HLL-$id' class='hlLoading'
5312// src='images/indicator_tiny.gif' style='display : none'>";
5313
df456bb0 5314 print "</td>";
bd51294a 5315
df456bb0 5316# }
3de0261a 5317
e04c18a2 5318 print "<td class=\"hlUpdated\" onclick='view($id)'><nobr>$updated_fmt&nbsp;</nobr></td>";
546499a9
AD
5319
5320 print "<td class='hlMarkedPic'>$score_pic</td>";
bd51294a
AD
5321
5322 if ($line["feed_title"] && !get_pref($link, 'VFEED_GROUP_BY_FEED')) {
d7e83df7 5323 print "<td onclick=\"viewfeed($feed_id)\" class=\"hlFeedIcon\">$feed_icon_img</td>";
bd51294a
AD
5324 }
5325
3de0261a
AD
5326 print "</tr>";
5327
5328 } else {
6cfea5c7 5329
bb031f91 5330 if (get_pref($link, 'VFEED_GROUP_BY_FEED') && $line["feed_title"]) {
081e527d
AD
5331 if ($feed_id != $vgroup_last_feed) {
5332
5333 $cur_feed_title = $line["feed_title"];
5334 $vgroup_last_feed = $feed_id;
5335
962d8ba4
AD
5336 $cur_feed_title = htmlspecialchars($cur_feed_title);
5337
af163b85 5338 $vf_catchup_link = "(<a onclick='javascript:catchupFeedInGroup($feed_id);' href='#'>".__('mark as read')."</a>)";
081e527d 5339
7defa089 5340 $has_feed_icon = feed_has_icon($feed_id);
dc803347
AD
5341
5342 if ($has_feed_icon) {
5343 $feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"".ICONS_URL."/$feed_id.ico\" alt=\"\">";
5344 } else {
5345 //$feed_icon_img = "<img class=\"tinyFeedIcon\" src=\"images/blank_icon.gif\" alt=\"\">";
5346 }
5347
6cfea5c7 5348 print "<div class='cdmFeedTitle'>".
dc803347 5349 "<div style=\"float : right\">$feed_icon_img</div>".
6cfea5c7 5350 "<a href=\"javascript:viewfeed($feed_id, '', false)\">".
081e527d 5351 $line["feed_title"]."</a> $vf_catchup_link</div>";
6cfea5c7
AD
5352 }
5353 }
5354
3de0261a
AD
5355 if ($is_unread) {
5356 $add_class = "Unread";
5357 } else {
5358 $add_class = "";
5359 }
0cacc891
AD
5360
5361 $expand_cdm = get_pref($link, 'CDM_EXPANDED');
3cd4239a 5362 $show_excerpt = false;
0cacc891 5363
5daa24f2 5364 if ($expand_cdm && $score >= -100) {
0cacc891 5365 $cdm_cstyle = "";
3cd4239a 5366 $show_excerpt = false;
0cacc891
AD
5367 } else {
5368 $cdm_cstyle = "style=\"display : none\"";
3cd4239a 5369 $show_excerpt = true;
0cacc891
AD
5370 }
5371
314fcd2b
AD
5372 $mouseover_attrs = "onmouseover='postMouseIn($id)'
5373 onmouseout='postMouseOut($id)'";
5374
e4914b62 5375 print "<div class=\"cdmArticle$add_class\"
3cd4239a 5376 id=\"RROW-$id\"
314fcd2b 5377 $mouseover_attrs'>";
3de0261a
AD
5378
5379 print "<div class=\"cdmHeader\">";
5380
965fb2af
AD
5381 if (!get_pref($link, "VFEED_GROUP_BY_FEED") || !$line["feed_title"]) {
5382 $cdm_feed_icon = "<span style=\"cursor : pointer\" onclick=\"viewfeed($feed_id)\">$feed_icon_img</span>";
5383 }
5384
5385 print "<div class=\"articleUpdated\">$updated_fmt $score_pic $cdm_feed_icon
b3296d36 5386 </div>";
5daa24f2 5387
9617eb94 5388 print "<span id=\"RTITLE-$id\" class=\"titleWrap$hlc_suffix\"><a class=\"title\"
3de0261a 5389 onclick=\"javascript:toggleUnread($id, 0)\"
5daa24f2
AD
5390 target=\"_blank\" href=\"".$line["link"]."\">".$line["title"]."</a>
5391 ";
3de0261a
AD
5392
5393 print $entry_author;
5394
3cd4239a 5395/* if (!$expand_cdm || $score < -100) {
0cacc891
AD
5396 print "&nbsp;<a id=\"CICH-$id\"
5397 href=\"javascript:cdmExpandArticle($id)\">
5398 (".__('Show article').")</a>";
3cd4239a 5399 } */
0cacc891 5400
39f3c580 5401 print $labels_str;
0cacc891 5402
d00f22ac 5403 if (!get_pref($link, 'VFEED_GROUP_BY_FEED')) {
6cfea5c7
AD
5404 if ($line["feed_title"]) {
5405 print "&nbsp;(<a href='javascript:viewfeed($feed_id)'>".$line["feed_title"]."</a>)";
5406 }
3de0261a
AD
5407 }
5408
5daa24f2 5409 print "</span></div>";
3de0261a 5410
3cd4239a
AD
5411 if ($show_excerpt) {
5412 print "<div class=\"cdmExcerpt\" id=\"CEXC-$id\"
5413 onclick=\"cdmExpandArticle($id)\"
5414 title=\"".__('Click to expand article')."\">";
c9efd838
AD
5415
5416 $content_preview = trim(truncate_string(strip_tags($line["content_preview"]), 100));
5417
5418 if (strlen($content_preview) != 0) {
5419 print $content_preview;
5420 } else {
5421 print __('Click to expand article');
5422 }
3cd4239a
AD
5423 print "</div>";
5424 }
5425
5426 print "<div class=\"cdmContent\"
5427 onclick=\"cdmClicked($id)\"
5428 id=\"CICD-$id\" $cdm_cstyle>";
a04c8e8d 5429
494a64ea
AD
5430 if ($line["orig_feed_id"]) {
5431
5432 $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds
5433 WHERE id = ".$line["orig_feed_id"]);
5434
5435 if (db_num_rows($tmp_result) != 0) {
5436
5437 print "<div clear='both'>";
5438 print __("Originally from:");
5439
5440 print "&nbsp;";
5441
5442 $tmp_line = db_fetch_assoc($tmp_result);
5443
5444 print "<a target='_blank'
5445 href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" .
5446 $tmp_line['title'] . "</a>";
5447
5448 print "&nbsp;";
5449
5450 print "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>";
5451 print "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.gif'></a>";
5452
5453 print "</div>";
5454 }
5455 }
5456
0cacc891 5457// print "<div class=\"cdmInnerContent\" id=\"CICD-$id\" $cdm_cstyle>";
621ffb00 5458
c7e51de1
AD
5459 print "<div id=\"POSTNOTE-$id\">";
5460 if ($line['note']) {
5461 print format_article_note($id, $line['note']);
5462 }
5463 print "</div>";
5464
54e61a68
AD
5465 print sanitize_rss($link, $line["content_preview"]);
5466
c54526fe 5467 $article_content = $line["content_preview"];
3c66b582
AD
5468
5469 $e_result = db_query($link, "SELECT * FROM ttrss_enclosures WHERE
4752b041 5470 post_id = '$id' AND content_url != ''");
3c66b582
AD
5471
5472 if (db_num_rows($e_result) > 0) {
3c66b582 5473
a3eeb471 5474 $entries_html = array();
3c66b582
AD
5475 $entries = array();
5476
5477 while ($e_line = db_fetch_assoc($e_result)) {
5478
5479 $url = $e_line["content_url"];
4752b041
AD
5480 $ctype = $e_line["content_type"];
5481 if (!$ctype) $ctype = __("unknown type");
3c66b582
AD
5482
5483 $filename = substr($url, strrpos($url, "/")+1);
5484
51682b23 5485 $entry = format_inline_player($link, $url, $ctype);
8dccabed 5486
51682b23 5487 $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" .
4752b041 5488 $filename . " (" . $ctype . ")" . "</a>";
3c66b582 5489
a3eeb471
AD
5490 array_push($entries_html, $entry);
5491
5492 $entry = array();
5493
5494 $entry["type"] = $ctype;
5495 $entry["filename"] = $filename;
5496 $entry["url"] = $url;
5497
3c66b582
AD
5498 array_push($entries, $entry);
5499 }
5500
54e61a68
AD
5501 $tmp_result = db_query($link, "SELECT always_display_enclosures FROM
5502 ttrss_feeds WHERE id = ".$line['feed_id']." AND owner_uid = ".$_SESSION["uid"]);
5503
5504 $always_display_enclosures = db_fetch_result($tmp_result, 0, "always_display_enclosures");
5505
fbaca246 5506 if (!get_pref($link, "STRIP_IMAGES")) {
44cfa025
AD
5507 if ($always_display_enclosures ||
5508 !preg_match("/img/i", $article_content)) {
5509
fbaca246 5510 foreach ($entries as $entry) {
44cfa025
AD
5511 if (preg_match("/image/", $entry["type"]) ||
5512 preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) {
fbaca246
AD
5513 print "<p><img
5514 alt=\"".htmlspecialchars($entry["filename"])."\"
5515 src=\"" .htmlspecialchars($entry["url"]) . "\"></p>";
5516 }
a3eeb471
AD
5517 }
5518 }
5519 }
5520
5521 print "<div class=\"cdmEnclosures\">";
5522
5523 if (db_num_rows($e_result) == 1) {
5524 print __("Attachment:") . " ";
5525 } else {
5526 print __("Attachments:") . " ";
5527 }
5528
5529 print join(", ", $entries_html);
3c66b582
AD
5530
5531 print "</div>";
5532 }
5533
a3eeb471 5534
0cacc891
AD
5535 print "<br clear='both'>";
5536// print "</div>";
a04c8e8d 5537
0cacc891 5538/* if (!$expand_cdm) {
12f5d8fe
AD
5539 print "<a id=\"CICH-$id\"
5540 href=\"javascript:cdmExpandArticle($id)\">
5541 Show article</a>";
0cacc891 5542 } */
a04c8e8d 5543
0cacc891 5544 print "</div>";
3de0261a 5545
e2ccbfab 5546 print "<div class=\"cdmFooter\"><span class='s0'>";
3de0261a 5547
e2ccbfab 5548 /* print "<div class=\"markedPic\">Star it: $marked_pic</div>"; */
3de0261a 5549
e2ccbfab
AD
5550 print __("Select:").
5551 " <input type=\"checkbox\" onclick=\"toggleSelectRowById(this,
3de0261a
AD
5552 'RROW-$id')\" class=\"feedCheckBox\" id=\"RCHK-$id\">";
5553
c7e51de1
AD
5554 print "</span><span class='s1'>$marked_pic&nbsp;";
5555 print "$published_pic&nbsp;";
5556 print "<img src=\"images/art-zoom.png\" class='tagsPic'
eedfb635
AD
5557 onclick=\"zoomToArticle($id)\"
5558 style=\"cursor : pointer\"
5559 alt='Zoom'
c7e51de1
AD
5560 title='".__('Show article summary in new window')."'>&nbsp;";
5561
5562 $note_escaped = htmlspecialchars($line['note'], ENT_QUOTES);
5563
5564 print "<img src=\"images/art-pub-note.png\" class='tagsPic'
5565 style=\"cursor : pointer\" style=\"cursor : pointer\"
5566 onclick=\"publishWithNote($id, '$note_escaped')\"
5567 alt='PubNote' title='".__('Publish article with a note')."'>";
5568
5569 print "</span>";
e2ccbfab 5570
307d187c 5571 $tags_str = format_tags_string(get_article_tags($link, $id), $id);
e2ccbfab 5572
5f014cf1 5573 print "<span class='s1'>
e9823609
AD
5574 <img class='tagsPic' src='".theme_image($link,
5575 'images/tag.png')."' alt='Tags' title='Tags'>
307d187c
AD
5576 <span id=\"ATSTR-$id\">$tags_str</span>
5577 <a title=\"".__('Edit tags for this article')."\"
5578 href=\"javascript:editArticleTags($id, $feed_id, true)\">(+)</a>";
3de0261a 5579
e2ccbfab 5580 print "</span>";
3de0261a 5581
c7e51de1 5582 print "<span class='s2'><a class=\"cdmToggleLink\"
e2ccbfab 5583 href=\"javascript:toggleUnread($id)\">
c7e51de1 5584 ".__('toggle unread')."</a></span>";
3de0261a 5585
e2ccbfab 5586 print "</div>";
3de0261a
AD
5587 print "</div>";
5588
5589 }
5590
5591 ++$lnum;
5592 }
5593
ac541432 5594 if (!get_pref($link, 'COMBINED_DISPLAY_MODE') && !$offset) {
3de0261a
AD
5595 print "</table>";
5596 }
5597
3de0261a 5598 } else {
93c841c4
AD
5599 $message = "";
5600
5601 switch ($view_mode) {
5602 case "unread":
5603 $message = __("No unread articles found to display.");
5604 break;
8b09eac8
AD
5605 case "updated":
5606 $message = __("No updated articles found to display.");
5607 break;
93c841c4
AD
5608 case "marked":
5609 $message = __("No starred articles found to display.");
5610 break;
5611 default:
215af892
AD
5612 if ($feed < -10) {
5613 $message = __("No articles found to display. You can assign articles to labels manually (see the Actions menu above) or use a filter.");
5614 } else {
5615 $message = __("No articles found to display.");
5616 }
93c841c4
AD
5617 }
5618
5619 if (!$offset) print "<div class='whiteBox'>$message</div>";
3de0261a
AD
5620 }
5621
ac541432
AD
5622 if (!$offset) {
5623 print "</div>";
5624 print "</div>";
5625 }
3de0261a 5626
081e527d 5627 return array($topmost_article_ids, $headlines_count, $feed, $disable_cache, $vgroup_last_feed);
3de0261a 5628 }
0979b696
AD
5629
5630// from here: http://www.roscripts.com/Create_tag_cloud-71.html
5631
5632 function printTagCloud($link) {
35a03bdd 5633
0979b696
AD
5634 $query = "SELECT tag_name, COUNT(post_int_id) AS count
5635 FROM ttrss_tags WHERE owner_uid = ".$_SESSION["uid"]."
b31af972 5636 GROUP BY tag_name ORDER BY count DESC LIMIT 50";
0979b696
AD
5637
5638 $result = db_query($link, $query);
5639
5640 $tags = array();
5641
5642 while ($line = db_fetch_assoc($result)) {
5643 $tags[$line["tag_name"]] = $line["count"];
5644 }
5645
5646 ksort($tags);
5647
5648 $max_size = 32; // max font size in pixels
4548a580 5649 $min_size = 11; // min font size in pixels
0979b696
AD
5650
5651 // largest and smallest array values
5652 $max_qty = max(array_values($tags));
5653 $min_qty = min(array_values($tags));
5654
5655 // find the range of values
5656 $spread = $max_qty - $min_qty;
5657 if ($spread == 0) { // we don't want to divide by zero
5658 $spread = 1;
5659 }
5660
5661 // set the font-size increment
5662 $step = ($max_size - $min_size) / ($spread);
5663
5664 // loop through the tag array
5665 foreach ($tags as $key => $value) {
5666 // calculate font-size
5667 // find the $value in excess of $min_qty
5668 // multiply by the font-size increment ($size)
5669 // and add the $min_size set above
5670 $size = round($min_size + (($value - $min_qty) * $step));
546ffab4
AD
5671
5672 $key_escaped = str_replace("'", "\\'", $key);
5673
5674 echo "<a href=\"javascript:viewfeed('$key_escaped') \" style=\"font-size: " .
0979b696
AD
5675 $size . "px\" title=\"$value articles tagged with " .
5676 $key . '">' . $key . '</a> ';
5677 }
5678 }
46921916
AD
5679
5680 function print_checkpoint($n, $s) {
5681 $ts = getmicrotime();
5682 echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s);
5683 return $ts;
5684 }
14b6c54b
AD
5685
5686 function sanitize_tag($tag) {
5687 $tag = trim($tag);
5688
5689 $tag = mb_strtolower($tag, 'utf-8');
5690
0c3d1c68
AD
5691 $tag = preg_replace('/[\"\+\>\<]/', "", $tag);
5692
5693// $tag = str_replace('"', "", $tag);
5694// $tag = str_replace("+", " ", $tag);
14b6c54b
AD
5695 $tag = str_replace("technorati tag: ", "", $tag);
5696
5697 return $tag;
5698 }
e4f4b46f
AD
5699
5700 function generate_publish_key() {
5701 return sha1(uniqid(rand(), true));
5702 }
5703
f56e3080
AD
5704 function article_publish_url($link) {
5705
e2749781
AD
5706 $url_path = "";
5707
5708
5709 if ($_SERVER['HTTPS'] != "on") {
5710 $url_path = "http://";
5711 } else {
5712 $url_path = "https://";
5713 }
f56e3080 5714
e2749781 5715 $url_path .= $_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
d9084cf2
AD
5716 $url_path .= "/backend.php?op=publish&key=" .
5717 get_pref($link, "_PREFS_PUBLISH_KEY", $_SESSION["uid"]);
f56e3080
AD
5718
5719 return $url_path;
5720 }
5721
45004d43
AD
5722 /**
5723 * Purge a feed contents, marked articles excepted.
5724 *
5725 * @param mixed $link The database connection.
5726 * @param integer $id The id of the feed to purge.
5727 * @return void
5728 */
d1f0c584 5729 function clear_feed_articles($link, $id) {
e04c18a2
AD
5730
5731 if ($id != 0) {
5732 $result = db_query($link, "DELETE FROM ttrss_user_entries
a8ae1b9a 5733 WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]);
e04c18a2
AD
5734 } else {
5735 $result = db_query($link, "DELETE FROM ttrss_user_entries
5736 WHERE feed_id IS NULL AND marked = false AND owner_uid = " . $_SESSION["uid"]);
5737 }
d1f0c584
AD
5738
5739 $result = db_query($link, "DELETE FROM ttrss_entries WHERE
5740 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
ced46404
AD
5741
5742 ccache_update($link, $id, $_SESSION['uid']);
45004d43
AD
5743 } // function clear_feed_articles
5744
5745 /**
5746 * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI.
5747 *
5748 * @return string The Mozilla Firefox feed adding URL.
5749 */
5750 function add_feed_url() {
d70c5ae4 5751 $url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
755a43ee
AD
5752 $url_path .= "?op=pref-feeds&quiet=1&subop=add&feed_url=%s";
5753 return $url_path;
45004d43
AD
5754 } // function add_feed_url
5755
5756 /**
5757 * Encrypt a password in SHA1.
5758 *
5759 * @param string $pass The password to encrypt.
5760 * @param string $login A optionnal login.
5761 * @return string The encrypted password.
5762 */
1a9f4d3c
AD
5763 function encrypt_password($pass, $login = '') {
5764 if ($login) {
5765 return "SHA1X:" . sha1("$login:$pass");
5766 } else {
5767 return "SHA1:" . sha1($pass);
5768 }
45004d43
AD
5769 } // function encrypt_password
5770
5771 /**
5772 * Update a feed batch.
5773 * Used by daemons to update n feeds by run.
5774 * Only update feed needing a update, and not being processed
5775 * by another process.
5776 *
5777 * @param mixed $link Database link
5778 * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
5779 * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
5780 * @param boolean $debug Set to false to disable debug output. Default to true.
5781 * @return void
5782 */
5783 function update_daemon_common($link, $limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
5784 // Process all other feeds using last_updated and interval parameters
5785
5786 // Test if the user has loggued in recently. If not, it does not update its feeds.
5787 if (DAEMON_UPDATE_LOGIN_LIMIT > 0) {
5788 if (DB_TYPE == "pgsql") {
5789 $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
5790 } else {
5791 $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
5792 }
5793 } else {
5794 $login_thresh_qpart = "";
5795 }
5796
5797 // Test if the feed need a update (update interval exceded).
5798 if (DB_TYPE == "pgsql") {
5799 $update_limit_qpart = "AND ((
5800 ttrss_feeds.update_interval = 0
5801 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
5802 ) OR (
5803 ttrss_feeds.update_interval > 0
5804 AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
da4caf5d 5805 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5806 } else {
5807 $update_limit_qpart = "AND ((
5808 ttrss_feeds.update_interval = 0
5809 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
5810 ) OR (
5811 ttrss_feeds.update_interval > 0
5812 AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
da4caf5d 5813 ) OR ttrss_feeds.last_updated IS NULL)";
45004d43
AD
5814 }
5815
5816 // Test if feed is currently being updated by another process.
5817 if (DB_TYPE == "pgsql") {
5818 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '120 seconds')";
5819 } else {
5820 $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 120 SECOND))";
5821 }
5822
5823 // Test if there is a limit to number of updated feeds
5824 $query_limit = "";
5825 if($limit) $query_limit = sprintf("LIMIT %d", $limit);
5826
51b8c957
AD
5827 $random_qpart = sql_random_function();
5828
45004d43
AD
5829 // We search for feed needing update.
5830 $result = db_query($link, "SELECT ttrss_feeds.feed_url,ttrss_feeds.id, ttrss_feeds.owner_uid,
fc2b26a6 5831 ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
45004d43
AD
5832 ttrss_feeds.update_interval
5833 FROM
5834 ttrss_feeds, ttrss_users, ttrss_user_prefs
5835 WHERE
5836 ttrss_feeds.owner_uid = ttrss_users.id
5837 AND ttrss_users.id = ttrss_user_prefs.owner_uid
5838 AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
5839 $login_thresh_qpart $update_limit_qpart
51b8c957
AD
5840 $updstart_thresh_qpart
5841 ORDER BY $random_qpart $query_limit");
45004d43
AD
5842
5843 $user_prefs_cache = array();
5844
5845 if($debug) _debug(sprintf("Scheduled %d feeds to update...\n", db_num_rows($result)));
5846
5847 // Here is a little cache magic in order to minimize risk of double feed updates.
5848 $feeds_to_update = array();
5849 while ($line = db_fetch_assoc($result)) {
5850 $feeds_to_update[$line['id']] = $line;
5851 }
5852
5853 // We update the feed last update started date before anything else.
5854 // There is no lag due to feed contents downloads
5855 // It prevent an other process to update the same feed.
5856 $feed_ids = array_keys($feeds_to_update);
5857 if($feed_ids) {
5858 db_query($link, sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
5859 WHERE id IN (%s)", implode(',', $feed_ids)));
5860 }
5861
5862 // For each feed, we call the feed update function.
5863 while ($line = array_pop($feeds_to_update)) {
5864
5865 if($debug) _debug("Feed: " . $line["feed_url"] . ", " . $line["last_updated"]);
5866
5867 // We setup a alarm to alert if the feed take more than 300s to update.
5868 // => HANG alarm.
9a91a51e 5869 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(300);
c633e370 5870 update_rss_feed($link, $line["id"], true);
45004d43 5871 // Cancel the alarm (the update went well)
9a91a51e 5872 if(!$from_http && function_exists('pcntl_alarm')) pcntl_alarm(0);
45004d43
AD
5873
5874 sleep(1); // prevent flood (FIXME make this an option?)
5875 }
5876
0e0dd486
AD
5877 // Send feed digests by email if needed.
5878 if (DAEMON_SENDS_DIGESTS) send_headlines_digests($link);
5879
5880 purge_orphans($link);
45004d43
AD
5881
5882 } // function update_daemon_common
1a9f4d3c 5883
621ffb00
AD
5884 function sanitize_article_content($text) {
5885 # we don't support CDATA sections in articles, they break our own escaping
5886 $text = preg_replace("/\[\[CDATA/", "", $text);
5887 $text = preg_replace("/\]\]\>/", "", $text);
5888 return $text;
5889 }
fee840fb
AD
5890
5891 function load_filters($link, $feed, $owner_uid, $action_id = false) {
5892 $filters = array();
5893
b8ffa322 5894 global $memcache;
fee840fb 5895
1f011328
AD
5896 $obj_id = md5("FILTER:$feed:$owner_uid:$action_id");
5897
b8ffa322
AD
5898 if ($memcache && $obj = $memcache->get($obj_id)) {
5899
b8ffa322
AD
5900 return $obj;
5901
5902 } else {
fee840fb 5903
b8ffa322
AD
5904 if ($action_id) $ftype_query_part = "action_id = '$action_id' AND";
5905
5906 $result = db_query($link, "SELECT reg_exp,
5907 ttrss_filter_types.name AS name,
5908 ttrss_filter_actions.name AS action,
5909 inverse,
5910 action_param,
5911 filter_param
5912 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
5913 enabled = true AND
5914 $ftype_query_part
5915 owner_uid = $owner_uid AND
5916 ttrss_filter_types.id = filter_type AND
5917 ttrss_filter_actions.id = action_id AND
5918 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
5919
5920 while ($line = db_fetch_assoc($result)) {
5921 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
5922 $filter["reg_exp"] = $line["reg_exp"];
5923 $filter["action"] = $line["action"];
5924 $filter["action_param"] = $line["action_param"];
5925 $filter["filter_param"] = $line["filter_param"];
5926 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
5927
5928 array_push($filters[$line["name"]], $filter);
5929 }
5930
5931 if ($memcache) $memcache->add($obj_id, $filters, 0, 3600*8);
5932
5933 return $filters;
5934 }
fee840fb 5935 }
1e36af0c
AD
5936
5937 function get_score_pic($score) {
1cce3aca 5938 if ($score > 100) {
1e36af0c 5939 return "score_high.png";
1cce3aca 5940 } else if ($score > 0) {
883fee8d 5941 return "score_half_high.png";
1cce3aca 5942 } else if ($score < -100) {
883fee8d 5943 return "score_low.png";
1cce3aca 5944 } else if ($score < 0) {
883fee8d 5945 return "score_half_low.png";
1e36af0c 5946 } else {
883fee8d 5947 return "score_neutral.png";
1e36af0c
AD
5948 }
5949 }
ec92c9d1 5950
0745839a 5951 function rounded_table_start($classname, $header = "&nbsp;") {
ec92c9d1 5952 print "<table width='100%' class='$classname' cellspacing='0' cellpadding='0'>";
74d5c8fa 5953 print "<tr><td class='c1'>&nbsp;</td><td class='top'>$header</td><td class='c2'>&nbsp;</td></tr>";
ec92c9d1
AD
5954 print "<tr><td class='left'>&nbsp;</td><td class='content'>";
5955 }
5956
0745839a 5957 function rounded_table_end($footer = "&nbsp;") {
ec92c9d1 5958 print "</td><td class='right'>&nbsp;</td></tr>";
74d5c8fa 5959 print "<tr><td class='c4'>&nbsp;</td><td class='bottom'>$footer</td><td class='c3'>&nbsp;</td></tr>";
ec92c9d1
AD
5960 print "</table>";
5961 }
5962
7defa089
AD
5963 function feed_has_icon($id) {
5964 return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0;
5965 }
f29ba148
AD
5966
5967 function init_connection($link) {
5968 if (DB_TYPE == "pgsql") {
6fc720fd 5969 pg_query($link, "set client_encoding = 'UTF-8'");
f29ba148 5970 pg_set_client_encoding("UNICODE");
045d0ab8 5971 pg_query($link, "set datestyle = 'ISO, european'");
f29ba148
AD
5972 } else {
5973 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
5974 db_query($link, "SET NAMES " . MYSQL_CHARSET);
5975 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
5976 }
5977 }
5978 }
5e96ca9d
AD
5979
5980 function update_feedbrowser_cache($link) {
5981
931dcbc1 5982 $result = db_query($link, "SELECT feed_url,title, COUNT(id) AS subscribers
5e96ca9d
AD
5983 FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
5984 WHERE tf.feed_url = ttrss_feeds.feed_url
5985 AND (private IS true OR feed_url LIKE '%:%@%/%'))
d460f7aa 5986 GROUP BY feed_url, title ORDER BY subscribers DESC LIMIT 1000");
5e96ca9d
AD
5987
5988 db_query($link, "BEGIN");
5989
5990 db_query($link, "DELETE FROM ttrss_feedbrowser_cache");
5991
5992 $count = 0;
5993
5994 while ($line = db_fetch_assoc($result)) {
5995 $subscribers = db_escape_string($line["subscribers"]);
5996 $feed_url = db_escape_string($line["feed_url"]);
931dcbc1
AD
5997 $title = db_escape_string($line["title"]);
5998
5999 $tmp_result = db_query($link, "SELECT subscribers FROM
6000 ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
6001
6002 if (db_num_rows($tmp_result) == 0) {
6003
6004 db_query($link, "INSERT INTO ttrss_feedbrowser_cache
6005 (feed_url, title, subscribers) VALUES ('$feed_url',
6006 '$title', '$subscribers')");
6007
6008 ++$count;
6009
6010 }
5e96ca9d 6011
5e96ca9d
AD
6012 }
6013
6014 db_query($link, "COMMIT");
6015
6016 return $count;
6017
6018 }
2627f2d0 6019
8a4c759e 6020 function ccache_zero($link, $feed_id, $owner_uid) {
2627f2d0
AD
6021 db_query($link, "UPDATE ttrss_counters_cache SET
6022 value = 0, updated = NOW() WHERE
6023 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
8a4c759e 6024 }
2627f2d0 6025
ad0056a8
AD
6026 function ccache_zero_all($link, $owner_uid) {
6027 db_query($link, "UPDATE ttrss_counters_cache SET
6028 value = 0 WHERE owner_uid = '$owner_uid'");
6029
6030 db_query($link, "UPDATE ttrss_cat_counters_cache SET
6031 value = 0 WHERE owner_uid = '$owner_uid'");
6032 }
6033
c7e51de1
AD
6034 function ccache_remove($link, $feed_id, $owner_uid, $is_cat = false) {
6035
6036 if (!$is_cat) {
6037 $table = "ttrss_counters_cache";
6038 } else {
6039 $table = "ttrss_cat_counters_cache";
6040 }
6041
6042 db_query($link, "DELETE FROM $table WHERE
6043 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
6044
6045 }
6046
5f4f7adf
AD
6047 function ccache_update_all($link, $owner_uid) {
6048
6049 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) {
6050
6051 $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache
6052 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
6053
6054 while ($line = db_fetch_assoc($result)) {
6055 ccache_update($link, $line["feed_id"], $owner_uid, true);
6056 }
6057
c5ffeb61
AD
6058 /* We have to manually include category 0 */
6059
6060 ccache_update($link, 0, $owner_uid, true);
6061
8a4c759e 6062 } else {
5f4f7adf
AD
6063 $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache
6064 WHERE feed_id > 0 AND owner_uid = '$owner_uid'");
8a4c759e 6065
5f4f7adf
AD
6066 while ($line = db_fetch_assoc($result)) {
6067 print ccache_update($link, $line["feed_id"], $owner_uid);
6068
6069 }
6070
6071 }
6072 }
2627f2d0 6073
6b49a3dd
AD
6074 function ccache_find($link, $feed_id, $owner_uid, $is_cat = false,
6075 $no_update = false) {
8a4c759e 6076
5c432ba4
AD
6077 if (!is_numeric($feed_id)) return;
6078
8a4c759e
AD
6079 if (!$is_cat) {
6080 $table = "ttrss_counters_cache";
32d2181b
AD
6081 if ($feed_id > 0) {
6082 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
6083 WHERE id = '$feed_id'");
6084 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
6085 }
8a4c759e
AD
6086 } else {
6087 $table = "ttrss_cat_counters_cache";
6088 }
6089
6090 if (DB_TYPE == "pgsql") {
6091 $date_qpart = "updated > NOW() - INTERVAL '15 minutes'";
6092 } else if (DB_TYPE == "mysql") {
6093 $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)";
6094 }
6095
6096 $result = db_query($link, "SELECT value FROM $table
6097 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id'
37fb651d 6098 LIMIT 1");
2627f2d0
AD
6099
6100 if (db_num_rows($result) == 1) {
6101 return db_fetch_result($result, 0, "value");
6102 } else {
6b49a3dd
AD
6103 if ($no_update) {
6104 return -1;
6105 } else {
6106 return ccache_update($link, $feed_id, $owner_uid, $is_cat);
6107 }
2627f2d0
AD
6108 }
6109
6110 }
6111
6c2a9b9e 6112 function ccache_update($link, $feed_id, $owner_uid, $is_cat = false,
6b49a3dd
AD
6113 $update_pcat = true) {
6114
5c432ba4
AD
6115 if (!is_numeric($feed_id)) return;
6116
32d2181b 6117 if (!$is_cat && $feed_id > 0) {
2e93b64c
AD
6118 $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds
6119 WHERE id = '$feed_id'");
6120 $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid");
6121 }
6122
43ead405
AD
6123 $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true);
6124
6125 /* When updating a label, all we need to do is recalculate feed counters
6126 * because labels are not cached */
c98e43db
AD
6127
6128 if ($feed_id < 0) {
43ead405
AD
6129 ccache_update_all($link, $owner_uid);
6130 return;
c98e43db
AD
6131 }
6132
8a4c759e
AD
6133 if (!$is_cat) {
6134 $table = "ttrss_counters_cache";
6135 } else {
6136 $table = "ttrss_cat_counters_cache";
6137 }
6138
b6d486a3 6139 if ($is_cat && $feed_id >= 0) {
37fb651d
AD
6140 if ($feed_id != 0) {
6141 $cat_qpart = "cat_id = '$feed_id'";
6142 } else {
6143 $cat_qpart = "cat_id IS NULL";
6144 }
6145
6b49a3dd
AD
6146 /* Recalculate counters for child feeds */
6147
6148 $result = db_query($link, "SELECT id FROM ttrss_feeds
6149 WHERE owner_uid = '$owner_uid' AND $cat_qpart");
6150
6151 while ($line = db_fetch_assoc($result)) {
6152 ccache_update($link, $line["id"], $owner_uid, false, false);
6153 }
6154
37fb651d
AD
6155 $result = db_query($link, "SELECT SUM(value) AS sv
6156 FROM ttrss_counters_cache, ttrss_feeds
6157 WHERE id = feed_id AND $cat_qpart AND
6158 ttrss_feeds.owner_uid = '$owner_uid'");
6159
51e196de 6160 $unread = (int) db_fetch_result($result, 0, "sv");
37fb651d 6161
f55b0b12
AD
6162 } else {
6163 $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid);
37fb651d 6164 }
2627f2d0 6165
c7e51de1
AD
6166 db_query($link, "BEGIN");
6167
8a4c759e 6168 $result = db_query($link, "SELECT feed_id FROM $table
2627f2d0
AD
6169 WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1");
6170
6171 if (db_num_rows($result) == 1) {
8a4c759e 6172 db_query($link, "UPDATE $table SET
2627f2d0
AD
6173 value = '$unread', updated = NOW() WHERE
6174 feed_id = '$feed_id' AND owner_uid = '$owner_uid'");
6175
6176 } else {
8a4c759e 6177 db_query($link, "INSERT INTO $table
2627f2d0
AD
6178 (feed_id, value, owner_uid, updated)
6179 VALUES
6180 ($feed_id, $unread, $owner_uid, NOW())");
2627f2d0
AD
6181 }
6182
c7e51de1
AD
6183 db_query($link, "COMMIT");
6184
6b49a3dd 6185 if ($feed_id > 0 && $prev_unread != $unread) {
8a4c759e 6186
37fb651d 6187 if (!$is_cat) {
8a4c759e 6188
6b49a3dd 6189 /* Update parent category */
8a4c759e 6190
6b49a3dd 6191 if ($update_pcat) {
37fb651d 6192
6b49a3dd
AD
6193 $result = db_query($link, "SELECT cat_id FROM ttrss_feeds
6194 WHERE owner_uid = '$owner_uid' AND id = '$feed_id'");
8a4c759e 6195
6b49a3dd 6196 $cat_id = (int) db_fetch_result($result, 0, "cat_id");
37fb651d 6197
6b49a3dd 6198 ccache_update($link, $cat_id, $owner_uid, true);
6c2a9b9e 6199
6c2a9b9e 6200 }
8a4c759e 6201 }
5f4f7adf
AD
6202 } else if ($feed_id < 0) {
6203 ccache_update_all($link, $owner_uid);
8a4c759e
AD
6204 }
6205
6206 return $unread;
2627f2d0 6207 }
ceb30ba4
AD
6208
6209 function label_find_id($link, $label, $owner_uid) {
6210 $result = db_query($link,
6211 "SELECT id FROM ttrss_labels2 WHERE caption = '$label'
6212 AND owner_uid = '$owner_uid' LIMIT 1");
6213
6214 if (db_num_rows($result) == 1) {
6215 return db_fetch_result($result, 0, "id");
6216 } else {
6217 return 0;
6218 }
6219 }
6220
814bff66 6221 function get_article_labels($link, $id) {
d721a547
AD
6222 global $memcache;
6223
814bff66 6224 $result = db_query($link,
2eb9c95c 6225 "SELECT DISTINCT label_id,caption,fg_color,bg_color
814bff66
AD
6226 FROM ttrss_labels2, ttrss_user_labels2
6227 WHERE id = label_id
6228 AND article_id = '$id'
e2549229
AD
6229 AND owner_uid = ".$_SESSION["uid"] . "
6230 ORDER BY caption");
814bff66 6231
d721a547
AD
6232 $obj_id = md5("LABELS:$id:" . $_SESSION["uid"]);
6233
814bff66
AD
6234 $rv = array();
6235
d721a547
AD
6236 if ($memcache && $obj = $memcache->get($obj_id)) {
6237 return $obj;
6238 } else {
6239 while ($line = db_fetch_assoc($result)) {
6240 $rk = array($line["label_id"], $line["caption"], $line["fg_color"],
6241 $line["bg_color"]);
6242 array_push($rv, $rk);
6243 }
6244 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
814bff66
AD
6245 }
6246
6247 return $rv;
6248 }
6249
6250
b8a637f3
AD
6251 function label_find_caption($link, $label, $owner_uid) {
6252 $result = db_query($link,
6253 "SELECT caption FROM ttrss_labels2 WHERE id = '$label'
6254 AND owner_uid = '$owner_uid' LIMIT 1");
6255
6256 if (db_num_rows($result) == 1) {
6257 return db_fetch_result($result, 0, "caption");
6258 } else {
6259 return "";
6260 }
6261 }
6262
933ba4ee
AD
6263 function label_remove_article($link, $id, $label, $owner_uid) {
6264
6265 $label_id = label_find_id($link, $label, $owner_uid);
6266
6267 if (!$label_id) return;
6268
6269 $result = db_query($link,
6270 "DELETE FROM ttrss_user_labels2
6271 WHERE
6272 label_id = '$label_id' AND
6273 article_id = '$id'");
6274 }
6275
ceb30ba4
AD
6276 function label_add_article($link, $id, $label, $owner_uid) {
6277
d721a547
AD
6278 global $memcache;
6279
6280 if ($memcache) {
6281 $obj_id = md5("LABELS:$id:$owner_uid");
6282 $memcache->delete($obj_id);
6283 }
6284
ceb30ba4
AD
6285 $label_id = label_find_id($link, $label, $owner_uid);
6286
6287 if (!$label_id) return;
6288
6289 $result = db_query($link,
6290 "SELECT
6291 article_id FROM ttrss_labels2, ttrss_user_labels2
6292 WHERE
6293 label_id = id AND
6294 label_id = '$label_id' AND
6295 article_id = '$id' AND owner_uid = '$owner_uid'
6296 LIMIT 1");
6297
6298 if (db_num_rows($result) == 0) {
6299 db_query($link, "INSERT INTO ttrss_user_labels2
6300 (label_id, article_id) VALUES ('$label_id', '$id')");
6301 }
6302 }
1380f8ee
AD
6303
6304 function label_remove($link, $id, $owner_uid) {
d721a547
AD
6305 global $memcache;
6306
6307 if ($memcache) {
6308 $obj_id = md5("LABELS:$id:$owner_uid");
6309 $memcache->delete($obj_id);
6310 }
1380f8ee
AD
6311
6312 db_query($link, "BEGIN");
6313
6314 $result = db_query($link, "SELECT caption FROM ttrss_labels2
6315 WHERE id = '$id'");
6316
6317 $caption = db_fetch_result($result, 0, "caption");
6318
6319 $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id'
6320 AND owner_uid = " . $_SESSION["uid"]);
6321
6322 if (db_affected_rows($link, $result) != 0 && $caption) {
6323
6324 /* Disable filters that reference label being removed */
6325
6326 db_query($link, "UPDATE ttrss_filters SET
6327 enabled = false WHERE action_param = '$caption'
6328 AND action_id = 7
6329 AND owner_uid = " . $_SESSION["uid"]);
6330 }
6331
6332 db_query($link, "COMMIT");
6333 }
79c88e11 6334
6b2ee18d
AD
6335 function label_create($link, $caption) {
6336
6337 db_query($link, "BEGIN");
6338
6339 $result = false;
6340
6341 $result = db_query($link, "SELECT id FROM ttrss_labels2
6342 WHERE caption = '$caption' AND owner_uid = ". $_SESSION["uid"]);
6343
6344 if (db_num_rows($result) == 0) {
6345 $result = db_query($link,
6346 "INSERT INTO ttrss_labels2 (caption,owner_uid)
6347 VALUES ('$caption', '".$_SESSION["uid"]."')");
6348
6349 $result = db_affected_rows($link, $result) != 0;
6350 }
6351
6352 db_query($link, "COMMIT");
6353
6354 return $result;
6355 }
6356
79c88e11
AD
6357 function print_labels_headlines_dropdown($link, $feed_id) {
6358 print "<li onclick=\"javascript:addLabel()\">
6359 &nbsp;&nbsp;".__("Create label...")."</li>";
6360
6361 $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 WHERE
6362 owner_uid = '".$_SESSION["uid"]."' ORDER BY caption");
6363
6364 while ($line = db_fetch_assoc($result)) {
6365
6366 $label_id = $line["id"];
6367 $label_caption = $line["caption"];
6368
6369 if ($feed_id < -10 && $feed_id == -11-$label_id) {
6370 print "<li id=\"LHDL-$id\"
6371 onclick=\"javascript:selectionRemoveLabel($label_id)\">
6372 &nbsp;&nbsp;$label_caption ".__('(remove)')."</li>";
6373 } else {
6374 print "<li id=\"LHDL-$id\"
6375 onclick=\"javascript:selectionAssignLabel($label_id)\">
6376 &nbsp;&nbsp;$label_caption</li>";
6377 }
6378 }
6379 }
307d187c
AD
6380
6381 function format_tags_string($tags, $id) {
6382
6383 $tags_str = "";
6384 $tags_nolinks_str = "";
6385
6386 $num_tags = 0;
6387
dce46cad 6388/* if (get_user_theme($link) == "3pane") {
307d187c
AD
6389 $tag_limit = 3;
6390 } else {
6391 $tag_limit = 6;
d9084cf2
AD
6392 } */
6393
6394 $tag_limit = 6;
307d187c
AD
6395
6396 $formatted_tags = array();
6397
6398 foreach ($tags as $tag) {
6399 $num_tags++;
6400 $tag_escaped = str_replace("'", "\\'", $tag);
6401
275a0af2
AD
6402 if (mb_strlen($tag) > 30) {
6403 $tag = truncate_string($tag, 30);
6404 }
6405
307d187c
AD
6406 $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>";
6407
6408 array_push($formatted_tags, $tag_str);
275a0af2
AD
6409
6410 $tmp_tags_str = implode(", ", $formatted_tags);
307d187c 6411
275a0af2 6412 if ($num_tags == $tag_limit || mb_strlen($tmp_tags_str) > 150) {
307d187c
AD
6413 break;
6414 }
6415 }
6416
6417 $tags_str = implode(", ", $formatted_tags);
6418
6419 if ($num_tags < count($tags)) {
6420 $tags_str .= ", &hellip;";
6421 }
6422
6423 if ($num_tags == 0) {
6424 $tags_str = __("no tags");
6425 }
6426
6427 return $tags_str;
6428
6429 }
2eb9c95c
AD
6430
6431 function format_article_labels($labels, $id) {
6432
6433 $labels_str = "";
6434
6435 foreach ($labels as $l) {
6436 $labels_str .= sprintf("<span class='hlLabelRef'
6437 style='color : %s; background-color : %s'>%s</span>",
6438 $l[2], $l[3], $l[1]);
6439 }
6440
6441 return $labels_str;
6442
6443 }
c7e51de1
AD
6444
6445 function format_article_note($id, $note) {
6446
6447 $note_escaped = htmlspecialchars($note, ENT_QUOTES);
6448
6449 $str = "<div class='articleNote'>";
db54143e 6450 $str .= $note;
c7e51de1
AD
6451 $str .= "<div class='articleNoteOps'>";
6452 $str .= "<a href=\"javascript:publishWithNote($id, '$note_escaped')\">".
6453 __('edit note')."</a>";
6454 $str .= "</div>";
c7e51de1
AD
6455 $str .= "</div>";
6456
6457 return $str;
6458 }
7f969260
AD
6459
6460 function toggle_collapse_cat($link, $cat_id) {
6461 if ($cat_id > 0) {
6462 db_query($link, "UPDATE ttrss_feed_categories SET
6463 collapsed = NOT collapsed WHERE id = '$cat_id' AND owner_uid = " .
6464 $_SESSION["uid"]);
6465 } else {
6466 $pref_name = '';
6467
6468 switch ($cat_id) {
6469 case -1:
6470 $pref_name = '_COLLAPSED_SPECIAL';
6471 break;
6472 case -2:
6473 $pref_name = '_COLLAPSED_LABELS';
6474 break;
6475 case 0:
6476 $pref_name = '_COLLAPSED_UNCAT';
6477 break;
6478 }
6479
6480 if ($pref_name) {
6481 if (get_pref($link, $pref_name)) {
6482 set_pref($link, $pref_name, 'false');
6483 } else {
6484 set_pref($link, $pref_name, 'true');
6485 }
6486 }
6487 }
6488 }
7e329f13
AD
6489
6490 function remove_feed($link, $id, $owner_uid) {
6491
6492 if ($id > 0) {
e04c18a2
AD
6493
6494 /* save starred articles in Archived feed */
6495
6496 db_query($link, "BEGIN");
6497
8056ec50
AD
6498 /* prepare feed if necessary */
6499
6500 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
6501 WHERE id = '$id'");
6502
6503 if (db_num_rows($result) == 0) {
6504 db_query($link, "INSERT INTO ttrss_archived_feeds
6505 (id, owner_uid, title, feed_url, site_url)
6506 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
6507 WHERE id = '$id'");
6508 }
6509
74d22f0c
AD
6510 db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL,
6511 orig_feed_id = '$id' WHERE feed_id = '$id' AND
e04c18a2
AD
6512 marked = true AND owner_uid = $owner_uid");
6513
6514 /* remove the feed */
6515
7e329f13
AD
6516 db_query($link, "DELETE FROM ttrss_feeds
6517 WHERE id = '$id' AND owner_uid = $owner_uid");
6518
e04c18a2
AD
6519 db_query($link, "COMMIT");
6520
69877646 6521/* if (file_exists(ICONS_DIR . "/$id.ico")) {
7e329f13 6522 unlink(ICONS_DIR . "/$id.ico");
69877646 6523 } */
7e329f13
AD
6524
6525 ccache_remove($link, $id, $owner_uid);
6526
6527 } else {
6528 label_remove($link, -11-$id, $owner_uid);
6529 ccache_remove($link, -11-$id, $owner_uid);
6530 }
6531 }
6532
6533 function remove_feed_category($link, $id, $owner_uid) {
6534
6535 db_query($link, "DELETE FROM ttrss_feed_categories
6536 WHERE id = '$id' AND owner_uid = $owner_uid");
6537
6538 ccache_remove($link, $id, $owner_uid, true);
6539 }
6540
16fdac16
AD
6541 function archive_article($link, $id, $owner_uid) {
6542 db_query($link, "BEGIN");
6543
6544 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
6545 WHERE ref_id = '$id' AND owner_uid = $owner_uid");
6546
6547 if (db_num_rows($result) != 0) {
6548
6549 /* prepare the archived table */
6550
6551 $feed_id = (int) db_fetch_result($result, 0, "feed_id");
6552
6553 if ($feed_id) {
6554 $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
6555 WHERE id = '$feed_id'");
6556
6557 if (db_num_rows($result) == 0) {
6558 db_query($link, "INSERT INTO ttrss_archived_feeds
6559 (id, owner_uid, title, feed_url, site_url)
6560 SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
6561 WHERE id = '$feed_id'");
6562 }
6563
6564 db_query($link, "UPDATE ttrss_user_entries
6565 SET orig_feed_id = feed_id, feed_id = NULL
6566 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
6567 }
6568 }
6569
6570 db_query($link, "COMMIT");
6571 }
ab197ae1
AD
6572
6573 function getArticleFeed($link, $id) {
6574 $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries
a545dc31 6575 WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]);
ab197ae1
AD
6576
6577 if (db_num_rows($result) != 0) {
6578 return db_fetch_result($result, 0, "feed_id");
6579 } else {
6580 return 0;
6581 }
6582 }
a5819bb3
AD
6583
6584 function make_url_from_parts($parts) {
6585 $url = $parts['scheme'] . '://' . $parts['host'];
6586
6587 if ($parts['path']) $url .= $parts['path'];
6588 if ($parts['query']) $url .= '?' . $parts['query'];
6589
6590 return $url;
6591 }
6592
6593 function validate_feed_url($url) {
6594 $parts = parse_url($url);
6595
6596 return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https');
6597
6598 }
d9084cf2 6599
be35798b
AD
6600 function get_article_enclosures($link, $id) {
6601
6602 global $memcache;
6603
6604 $query = "SELECT * FROM ttrss_enclosures
6605 WHERE post_id = '$id' AND content_url != ''";
6606
bd3f2ade 6607 $obj_id = md5("ENCLOSURES:$id");
be35798b
AD
6608
6609 $rv = array();
6610
bd3f2ade 6611 if ($memcache && $obj = $memcache->get($obj_id)) {
be35798b
AD
6612 $rv = $obj;
6613 } else {
6614 $result = db_query($link, $query);
6615
6616 if (db_num_rows($result) > 0) {
6617 while ($line = db_fetch_assoc($result)) {
6618 array_push($rv, $line);
6619 }
bd3f2ade 6620 if ($memcache) $memcache->add($obj_id, $rv, 0, 3600);
be35798b
AD
6621 }
6622 }
6623
6624 return $rv;
6625 }
6626
40d13c28 6627?>