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