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