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