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