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