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