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