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