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