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