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