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