]> git.wh0rd.org - tt-rss.git/blame - functions.php
force set curl timeouts
[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
1025ad87
AD
9 require_once "accept-to-gettext.php";
10 require_once "gettext/gettext.inc";
11
40d13c28 12 require_once 'config.php';
cc17c205 13
aba609e0
AD
14 function startup_gettext() {
15
16 # Get locale from Accept-Language header
17 $lang = al2gt(array("en_US", "ru_RU"), "text/html");
18
19 if ($lang) {
20 _setlocale(LC_MESSAGES, $lang);
21 _bindtextdomain("messages", "locale");
22 _textdomain("messages");
23 _bind_textdomain_codeset("messages", "UTF-8");
24 }
25 }
26
cc17c205
AD
27 if (ENABLE_TRANSLATIONS == true) {
28 startup_gettext();
29 }
30
b619ff15 31 require_once 'db-prefs.php';
5bc0bd27 32 require_once 'compat.php';
af106b0e 33 require_once 'errors.php';
8911ac8b 34 require_once 'version.php';
40d13c28 35
49f9c923 36 define('MAGPIE_USER_AGENT_EXT', ' (Tiny Tiny RSS/' . VERSION . ')');
a3ee2a38
AD
37 define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
38
49f9c923
AD
39 require_once "magpierss/rss_fetch.inc";
40 require_once 'magpierss/rss_utils.inc';
41
6f9e33e4
AD
42 function _debug($msg) {
43 $ts = strftime("%H:%M:%S", time());
44 print "[$ts] $msg\n";
45 }
46
ad507f85
AD
47 function purge_feed($link, $feed_id, $purge_interval, $debug = false) {
48
49 $rows = -1;
4c193675 50
fefa6ca3 51 if (DB_TYPE == "pgsql") {
44e241cb 52/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 53 marked = false AND feed_id = '$feed_id' AND
35d8cf43 54 (SELECT date_entered FROM ttrss_entries WHERE
44e241cb
AD
55 id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); */
56
6e7f8d26
AD
57 $pg_version = get_pgsql_version($link);
58
59 if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
1e59ae35
AD
60
61 $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
62 ttrss_entries.id = ref_id AND
63 marked = false AND
64 feed_id = '$feed_id' AND
65 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
66
67 } else {
68
69 $result = db_query($link, "DELETE FROM ttrss_user_entries
70 USING ttrss_entries
71 WHERE ttrss_entries.id = ref_id AND
72 marked = false AND
73 feed_id = '$feed_id' AND
fc774155 74 ttrss_entries.date_entered < NOW() - INTERVAL '$purge_interval days'");
1e59ae35 75 }
ad507f85
AD
76
77 $rows = pg_affected_rows($result);
78
fefa6ca3 79 } else {
1e59ae35 80
30f1746f 81/* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE
fefa6ca3 82 marked = false AND feed_id = '$feed_id' AND
35d8cf43 83 (SELECT date_entered FROM ttrss_entries WHERE
30f1746f
AD
84 id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
85
86 $result = db_query($link, "DELETE FROM ttrss_user_entries
87 USING ttrss_user_entries, ttrss_entries
88 WHERE ttrss_entries.id = ref_id AND
89 marked = false AND
90 feed_id = '$feed_id' AND
91 ttrss_entries.date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
92
ad507f85
AD
93 $rows = mysql_affected_rows($link);
94
95 }
96
97 if ($debug) {
6f9e33e4 98 _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
fefa6ca3
AD
99 }
100 }
101
44e241cb
AD
102 function global_purge_old_posts($link, $do_output = false, $limit = false) {
103
894ebcf5 104 $random_qpart = sql_random_function();
fefa6ca3 105
44e241cb
AD
106 if ($limit) {
107 $limit_qpart = "LIMIT $limit";
108 } else {
109 $limit_qpart = "";
110 }
111
fefa6ca3 112 $result = db_query($link,
44e241cb
AD
113 "SELECT id,purge_interval,owner_uid FROM ttrss_feeds
114 ORDER BY $random_qpart $limit_qpart");
fefa6ca3
AD
115
116 while ($line = db_fetch_assoc($result)) {
117
118 $feed_id = $line["id"];
119 $purge_interval = $line["purge_interval"];
120 $owner_uid = $line["owner_uid"];
121
122 if ($purge_interval == 0) {
123
124 $tmp_result = db_query($link,
125 "SELECT value FROM ttrss_user_prefs WHERE
126 pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'");
127
128 if (db_num_rows($tmp_result) != 0) {
129 $purge_interval = db_fetch_result($tmp_result, 0, "value");
130 }
131 }
132
133 if ($do_output) {
ad507f85 134// print "Feed $feed_id: purge interval = $purge_interval\n";
fefa6ca3
AD
135 }
136
137 if ($purge_interval > 0) {
ad507f85 138 purge_feed($link, $feed_id, $purge_interval, $do_output);
fefa6ca3
AD
139 }
140 }
141
71604ca4
AD
142 // purge orphaned posts in main content table
143 db_query($link, "DELETE FROM ttrss_entries WHERE
144 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
145
fefa6ca3
AD
146 }
147
b6eefba5 148 function purge_old_posts($link) {
5d73494a 149
f1a80dae
AD
150 $user_id = $_SESSION["uid"];
151
152 $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds
153 WHERE owner_uid = '$user_id'");
5d73494a
AD
154
155 while ($line = db_fetch_assoc($result)) {
156
157 $feed_id = $line["id"];
158 $purge_interval = $line["purge_interval"];
159
b619ff15 160 if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS');
5d73494a 161
140aae81 162 if ($purge_interval > 0) {
fefa6ca3 163 purge_feed($link, $feed_id, $purge_interval);
5d73494a
AD
164 }
165 }
71604ca4
AD
166
167 // purge orphaned posts in main content table
168 db_query($link, "DELETE FROM ttrss_entries WHERE
169 (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0");
c3a8d71a
AD
170 }
171
1f2b01ed 172 function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) {
40d13c28 173
4769ddaf 174 if (WEB_DEMO_MODE) return;
b0b4abcf 175
a2770077
AD
176 if (!$user_id) {
177 $user_id = $_SESSION["uid"];
178 purge_old_posts($link);
179 }
180
25af8dad 181// db_query($link, "BEGIN");
b82af8c3 182
cbd8650d
AD
183 if (MAX_UPDATE_TIME > 0) {
184 if (DB_TYPE == "mysql") {
185 $q_order = "RAND()";
186 } else {
187 $q_order = "RANDOM()";
188 }
189 } else {
190 $q_order = "last_updated DESC";
191 }
192
d148926e 193 $result = db_query($link, "SELECT feed_url,id,
798f722b 194 SUBSTRING(last_updated,1,19) AS last_updated,
5c563acd 195 update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'
cbd8650d
AD
196 ORDER BY $q_order");
197
198 $upd_start = time();
40d13c28 199
b6eefba5 200 while ($line = db_fetch_assoc($result)) {
d148926e
AD
201 $upd_intl = $line["update_interval"];
202
b619ff15 203 if (!$upd_intl || $upd_intl == 0) {
e289ca71 204 $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id, false);
b619ff15 205 }
d148926e 206
c1e202b7
AD
207 if ($upd_intl < 0) {
208 // Updates for this feed are disabled
209 continue;
210 }
211
93d40f50
AD
212 if ($fetch || (!$line["last_updated"] ||
213 time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) {
c5142cca 214
cbd8650d
AD
215// print "<!-- feed: ".$line["feed_url"]." -->";
216
1f2b01ed 217 update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon);
cbd8650d
AD
218
219 $upd_elapsed = time() - $upd_start;
220
221 if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) {
222 return;
223 }
d148926e 224 }
40d13c28
AD
225 }
226
25af8dad 227// db_query($link, "COMMIT");
b82af8c3 228
40d13c28
AD
229 }
230
4065b60b
AD
231 function fetch_file_contents($url) {
232 if (USE_CURL_FOR_ICONS) {
233 $tmpfile = tempnam(TMP_DIRECTORY, "ttrss-tmp");
234
235 $ch = curl_init($url);
236 $fp = fopen($tmpfile, "w");
237
238 if ($fp) {
239 curl_setopt($ch, CURLOPT_FILE, $fp);
dd966fed
AD
240 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
241 curl_setopt($ch, CURLOPT_TIMEOUT, 45);
4065b60b
AD
242 curl_exec($ch);
243 curl_close($ch);
244 fclose($fp);
245 }
246
247 $contents = file_get_contents($tmpfile);
248 unlink($tmpfile);
249
250 return $contents;
251
252 } else {
253 return file_get_contents($url);
254 }
255
256 }
78800912 257
4065b60b
AD
258 // adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
259 // http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
78800912 260
4065b60b 261 function get_favicon_url($url) {
99331724 262
4065b60b 263 if ($html = @fetch_file_contents($url)) {
78800912 264
4065b60b
AD
265 if ( preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $html, $matches)) {
266 // Attempt to grab a favicon link from their webpage url
267 $linkUrl = html_entity_decode($matches[1]);
c798704b 268
4065b60b
AD
269 if (substr($linkUrl, 0, 1) == '/') {
270 $urlParts = parse_url($url);
271 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].$linkUrl;
272 } else if (substr($linkUrl, 0, 7) == 'http://') {
273 $faviconURL = $linkUrl;
274 } else if (substr($url, -1, 1) == '/') {
275 $faviconURL = $url.$linkUrl;
276 } else {
277 $faviconURL = $url.'/'.$linkUrl;
e695fdc8 278 }
717f5e64 279
c798704b 280 } else {
4065b60b
AD
281 // If unsuccessful, attempt to "guess" the favicon location
282 $urlParts = parse_url($url);
283 $faviconURL = $urlParts['scheme'].'://'.$urlParts['host'].'/favicon.ico';
284 }
285 }
c798704b 286
4065b60b
AD
287 // Run a test to see if what we have attempted to get actually exists.
288 if(USE_CURL_FOR_ICONS || url_validate($faviconURL)) {
289 return $faviconURL;
290 } else {
291 return false;
292 }
293 }
294
295 function url_validate($link) {
296
297 $url_parts = @parse_url($link);
298
299 if ( empty( $url_parts["host"] ) )
300 return false;
301
302 if ( !empty( $url_parts["path"] ) ) {
303 $documentpath = $url_parts["path"];
304 } else {
305 $documentpath = "/";
306 }
307
308 if ( !empty( $url_parts["query"] ) )
309 $documentpath .= "?" . $url_parts["query"];
310
311 $host = $url_parts["host"];
312 $port = $url_parts["port"];
313
314 if ( empty($port) )
315 $port = "80";
316
317 $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
318
319 if ( !$socket )
320 return false;
c798704b 321
4065b60b
AD
322 fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
323
324 $http_response = fgets( $socket, 22 );
325
326 $responses = "/(200 OK)|(30[0-9] Moved)/";
327 if ( preg_match($responses, $http_response) ) {
328 fclose($socket);
329 return true;
330 } else {
331 return false;
332 }
333
334 }
335
336 function check_feed_favicon($site_url, $feed, $link) {
337 $favicon_url = get_favicon_url($site_url);
338
339# print "FAVICON [$site_url]: $favicon_url\n";
340
341 error_reporting(0);
342
343 $icon_file = ICONS_DIR . "/$feed.ico";
344
345 if ($favicon_url && !file_exists($icon_file)) {
346 $contents = fetch_file_contents($favicon_url);
347
348 $fp = fopen($icon_file, "w");
78800912 349
4065b60b
AD
350 if ($fp) {
351 fwrite($fp, $contents);
352 fclose($fp);
353 chmod($icon_file, 0644);
354 }
78800912 355 }
4065b60b
AD
356
357 error_reporting(DEFAULT_ERROR_LEVEL);
358
78800912
AD
359 }
360
ddb68b81 361 function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) {
40d13c28 362
ddb68b81 363 if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) {
21cfcdf2
AD
364 return;
365 }
366
219bd8fc 367 if (defined('DAEMON_EXTENDED_DEBUG')) {
34e420fb 368 _debug("update_rss_feed: start");
219bd8fc
AD
369 }
370
47c6c988 371 $result = db_query($link, "SELECT update_interval,auth_login,auth_pass
a88c1f36
AD
372 FROM ttrss_feeds WHERE id = '$feed'");
373
ff25e639
AD
374 $auth_login = db_unescape_string(db_fetch_result($result, 0, "auth_login"));
375 $auth_pass = db_unescape_string(db_fetch_result($result, 0, "auth_pass"));
47c6c988 376
a88c1f36
AD
377 $update_interval = db_fetch_result($result, 0, "update_interval");
378
379 if ($update_interval < 0) { return; }
380
ab3d0b99
AD
381 $feed = db_escape_string($feed);
382
47c6c988
AD
383 $fetch_url = $feed_url;
384
385 if ($auth_login && $auth_pass) {
386 $url_parts = array();
387 preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts);
388
389 if ($url_parts[1] && $url_parts[2]) {
390 $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2];
391 }
392
393 }
ab3d0b99 394
219bd8fc 395 if (defined('DAEMON_EXTENDED_DEBUG')) {
34e420fb 396 _debug("update_rss_feed: fetching...");
219bd8fc
AD
397 }
398
399 if (!defined('DAEMON_EXTENDED_DEBUG')) {
400 error_reporting(0);
401 }
402
49f9c923 403 $rss = fetch_rss($fetch_url);
219bd8fc
AD
404
405 if (defined('DAEMON_EXTENDED_DEBUG')) {
34e420fb 406 _debug("update_rss_feed: fetch done, parsing...");
219bd8fc
AD
407 } else {
408 error_reporting (DEFAULT_ERROR_LEVEL);
409 }
410
b6eefba5 411 $feed = db_escape_string($feed);
dcee8f61 412
49f9c923 413 if ($rss) {
50b62214
AD
414
415 if (defined('DAEMON_EXTENDED_DEBUG')) {
416 _debug("update_rss_feed: processing feed data...");
417 }
418
44e241cb 419// db_query($link, "BEGIN");
dd8c76a9 420
a88c1f36 421 $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid
f324892e 422 FROM ttrss_feeds WHERE id = '$feed'");
331900c6 423
b6eefba5
AD
424 $registered_title = db_fetch_result($result, 0, "title");
425 $orig_icon_url = db_fetch_result($result, 0, "icon_url");
f324892e 426 $orig_site_url = db_fetch_result($result, 0, "site_url");
331900c6 427
7fed1940
AD
428 $owner_uid = db_fetch_result($result, 0, "owner_uid");
429
8d0ec6fd 430 if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid, false)) {
50b62214
AD
431 if (defined('DAEMON_EXTENDED_DEBUG')) {
432 _debug("update_rss_feed: checking favicon...");
433 }
4065b60b 434 check_feed_favicon($rss->channel["link"], $feed, $link);
a2770077
AD
435 }
436
746b249f 437 if (!$registered_title || $registered_title == "[Unknown]") {
7c5a308d 438
49f9c923 439 $feed_title = db_escape_string($rss->channel["title"]);
7c5a308d 440
f324892e
AD
441 db_query($link, "UPDATE ttrss_feeds SET
442 title = '$feed_title' WHERE id = '$feed'");
443 }
444
49f9c923
AD
445 $site_url = $rss->channel["link"];
446 // weird, weird Magpie
447 if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]);
147f7691
AD
448
449 if ($site_url && $orig_site_url != db_escape_string($site_url)) {
f324892e
AD
450 db_query($link, "UPDATE ttrss_feeds SET
451 site_url = '$site_url' WHERE id = '$feed'");
331900c6 452 }
40d13c28 453
b7f4bda2
AD
454// print "I: " . $rss->channel["image"]["url"];
455
49f9c923 456 $icon_url = $rss->image["url"];
b7f4bda2 457
147f7691 458 if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) {
b6eefba5
AD
459 $icon_url = db_escape_string($icon_url);
460 db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'");
b7f4bda2
AD
461 }
462
50b62214
AD
463 if (defined('DAEMON_EXTENDED_DEBUG')) {
464 _debug("update_rss_feed: loading filters...");
465 }
e6155a06
AD
466
467 $filters = array();
468
4b3dff6e 469 $result = db_query($link, "SELECT reg_exp,
db42b934 470 ttrss_filter_types.name AS name,
073ca0e6 471 ttrss_filter_actions.name AS action,
c2d9322b 472 inverse,
073ca0e6 473 action_param
db42b934 474 FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE
e8b79d16 475 enabled = true AND
db42b934
AD
476 owner_uid = $owner_uid AND
477 ttrss_filter_types.id = filter_type AND
478 ttrss_filter_actions.id = action_id AND
f8382011 479 (feed_id IS NULL OR feed_id = '$feed') ORDER BY reg_exp");
e6155a06 480
b6eefba5 481 while ($line = db_fetch_assoc($result)) {
e6155a06 482 if (!$filters[$line["name"]]) $filters[$line["name"]] = array();
19c9cb11
AD
483
484 $filter["reg_exp"] = $line["reg_exp"];
485 $filter["action"] = $line["action"];
073ca0e6 486 $filter["action_param"] = $line["action_param"];
c2d9322b 487 $filter["inverse"] = sql_bool_to_bool($line["inverse"]);
073ca0e6 488
19c9cb11 489 array_push($filters[$line["name"]], $filter);
e6155a06
AD
490 }
491
49f9c923 492 $iterator = $rss->items;
7c5a308d 493
49f9c923
AD
494 if (!$iterator || !is_array($iterator)) $iterator = $rss->entries;
495 if (!$iterator || !is_array($iterator)) $iterator = $rss;
c22789da
AD
496
497 if (!is_array($iterator)) {
39541e74 498 /* db_query($link, "UPDATE ttrss_feeds
75bd0669 499 SET last_error = 'Parse error: can\'t find any articles.'
39541e74 500 WHERE id = '$feed'"); */
c22789da
AD
501 return; // WTF?
502 }
ddb68b81 503
50b62214
AD
504 if (defined('DAEMON_EXTENDED_DEBUG')) {
505 _debug("update_rss_feed: processing articles...");
506 }
507
ddb68b81 508 foreach ($iterator as $item) {
7c5a308d 509
be832a1a 510 $entry_guid = $item["id"];
34e420fb 511
be832a1a
AD
512 if (!$entry_guid) $entry_guid = $item["guid"];
513 if (!$entry_guid) $entry_guid = $item["link"];
514 if (!$entry_guid) $entry_guid = make_guid_from_title($item["title"]);
515
34e420fb
AD
516 if (defined('DAEMON_EXTENDED_DEBUG')) {
517 _debug("update_rss_feed: guid $entry_guid");
518 }
519
be832a1a
AD
520 if (!$entry_guid) continue;
521
522 $entry_timestamp = "";
523
524 $rss_2_date = $item['pubdate'];
525 $rss_1_date = $item['dc']['date'];
526 $atom_date = $item['issued'];
527 if (!$atom_date) $atom_date = $item['updated'];
528
529 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
530 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
531 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
b82af8c3 532
2e930846 533 if ($entry_timestamp == "" || $entry_timestamp == -1 || !$entry_timestamp) {
be832a1a
AD
534 $entry_timestamp = time();
535 $no_orig_date = 'true';
536 } else {
537 $no_orig_date = 'false';
538 }
539
540 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
541
542 $entry_title = trim(strip_tags($item["title"]));
543
544 // strange Magpie workaround
545 $entry_link = $item["link_"];
546 if (!$entry_link) $entry_link = $item["link"];
547
548 if (!$entry_title) continue;
1f64b1be 549# if (!$entry_link) continue;
7d3ab0dd 550
be832a1a 551 $entry_link = strip_tags($entry_link);
7d3ab0dd 552
be832a1a 553 $entry_content = $item["content:escaped"];
bbb416e5 554
be832a1a
AD
555 if (!$entry_content) $entry_content = $item["content:encoded"];
556 if (!$entry_content) $entry_content = $item["content"];
557 if (!$entry_content) $entry_content = $item["atom_content"];
558 if (!$entry_content) $entry_content = $item["summary"];
559 if (!$entry_content) $entry_content = $item["description"];
bbb416e5 560
be832a1a 561// if (!$entry_content) continue;
bbb416e5 562
be832a1a
AD
563 // WTF
564 if (is_array($entry_content)) {
565 $entry_content = $entry_content["encoded"];
566 if (!$entry_content) $entry_content = $entry_content["escaped"];
567 }
568
569// print_r($item);
570// print_r(htmlspecialchars($entry_content));
571// print "<br>";
572
573 $entry_content_unescaped = $entry_content;
574 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
575
576 $entry_comments = strip_tags($item["comments"]);
577
578 $entry_author = db_escape_string(strip_tags($item['dc']['creator']));
579
83f114c8 580 if ($item['author']) {
83f114c8 581
4d6e9157
AD
582 if (is_array($item['author'])) {
583
584 if (!$entry_author) {
585 $entry_author = db_escape_string(strip_tags($item['author']['name']));
586 }
587
588 if (!$entry_author) {
589 $entry_author = db_escape_string(strip_tags($item['author']['email']));
590 }
83f114c8
AD
591 }
592
593 if (!$entry_author) {
594 $entry_author = db_escape_string(strip_tags($item['author']));
595 }
be832a1a
AD
596 }
597
83f114c8
AD
598 if (preg_match('/^[\t\n\r ]*$/', $entry_author)) $entry_author = '';
599
be832a1a
AD
600 $entry_guid = db_escape_string(strip_tags($entry_guid));
601
602 $result = db_query($link, "SELECT id FROM ttrss_entries
603 WHERE guid = '$entry_guid'");
604
605 $entry_content = db_escape_string($entry_content);
606 $entry_title = db_escape_string($entry_title);
607 $entry_link = db_escape_string($entry_link);
608 $entry_comments = db_escape_string($entry_comments);
609
610 $num_comments = db_escape_string($item["slash"]["comments"]);
611
612 if (!$num_comments) $num_comments = 0;
613
ef063748 614/* $dc_subject = $item['dc']['subject'];
be832a1a
AD
615
616 $subject_tags = false;
617
618 if (is_array($dc_subject)) {
619 $subject_tags = $dc_subject;
620 } else if ($dc_subject) {
621 $subject_tags = array($dc_subject);
ef063748 622 } */
8add756a 623
d48d160c 624 # sanitize content
183ad07b
AD
625
626 $entry_content = sanitize_rss($entry_content);
d48d160c 627
34e420fb
AD
628 if (defined('DAEMON_EXTENDED_DEBUG')) {
629 _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
630 }
631
44e241cb
AD
632 db_query($link, "BEGIN");
633
4c193675
AD
634 if (db_num_rows($result) == 0) {
635
34e420fb
AD
636 if (defined('DAEMON_EXTENDED_DEBUG')) {
637 _debug("update_rss_feed: base guid not found");
638 }
639
4c193675
AD
640 // base post entry does not exist, create it
641
4c193675
AD
642 $result = db_query($link,
643 "INSERT INTO ttrss_entries
644 (title,
645 guid,
646 link,
647 updated,
648 content,
649 content_hash,
650 no_orig_date,
651 date_entered,
11b0dce2 652 comments,
b6104dee
AD
653 num_comments,
654 author)
4c193675
AD
655 VALUES
656 ('$entry_title',
657 '$entry_guid',
658 '$entry_link',
659 '$entry_timestamp_fmt',
660 '$entry_content',
661 '$content_hash',
662 $no_orig_date,
663 NOW(),
11b0dce2 664 '$entry_comments',
b6104dee
AD
665 '$num_comments',
666 '$entry_author')");
8926aab8
AD
667 } else {
668 // we keep encountering the entry in feeds, so we need to
669 // update date_entered column so that we don't get horrible
670 // dupes when the entry gets purged and reinserted again e.g.
671 // in the case of SLOW SLOW OMG SLOW updating feeds
672
673 $base_entry_id = db_fetch_result($result, 0, "id");
674
675 db_query($link, "UPDATE ttrss_entries SET date_entered = NOW()
676 WHERE id = '$base_entry_id'");
4c193675
AD
677 }
678
679 // now it should exist, if not - bad luck then
680
6385315d
AD
681 $result = db_query($link, "SELECT
682 id,content_hash,no_orig_date,title,
8926aab8 683 substring(date_entered,1,19) as date_entered,
11b0dce2
AD
684 substring(updated,1,19) as updated,
685 num_comments
6385315d
AD
686 FROM
687 ttrss_entries
688 WHERE guid = '$entry_guid'");
4c193675
AD
689
690 if (db_num_rows($result) == 1) {
691
34e420fb
AD
692 if (defined('DAEMON_EXTENDED_DEBUG')) {
693 _debug("update_rss_feed: base guid found, creating user ref");
694 }
695
11b0dce2
AD
696 // this will be used below in update handler
697 $orig_content_hash = db_fetch_result($result, 0, "content_hash");
698 $orig_title = db_fetch_result($result, 0, "title");
699 $orig_num_comments = db_fetch_result($result, 0, "num_comments");
8926aab8
AD
700 $orig_date_entered = strtotime(db_fetch_result($result,
701 0, "date_entered"));
6385315d 702
11b0dce2 703 $ref_id = db_fetch_result($result, 0, "id");
4c193675 704
11b0dce2 705 // check for user post link to main table
4c193675 706
11b0dce2 707 // do we allow duplicate posts with same GUID in different feeds?
8d0ec6fd 708 if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
11b0dce2
AD
709 $dupcheck_qpart = "AND feed_id = '$feed'";
710 } else {
711 $dupcheck_qpart = "";
712 }
71604ca4 713
11b0dce2 714// error_reporting(0);
19c9cb11 715
f8382011
AD
716 $article_filters = get_article_filters($filters, $entry_title,
717 $entry_content, $entry_link);
19c9cb11 718
f8382011 719 if (find_article_filter($article_filters, "filter")) {
11b0dce2
AD
720 continue;
721 }
19c9cb11 722
11b0dce2 723// error_reporting (DEFAULT_ERROR_LEVEL);
3a933f22 724
11b0dce2
AD
725 $result = db_query($link,
726 "SELECT ref_id FROM ttrss_user_entries WHERE
727 ref_id = '$ref_id' AND owner_uid = '$owner_uid'
728 $dupcheck_qpart");
729
730 // okay it doesn't exist - create user entry
731 if (db_num_rows($result) == 0) {
732
f8382011 733 if (!find_article_filter($article_filters, 'catchup')) {
11b0dce2
AD
734 $unread = 'true';
735 $last_read_qpart = 'NULL';
736 } else {
737 $unread = 'false';
738 $last_read_qpart = 'NOW()';
739 }
dd7d3187 740
f8382011 741 if (find_article_filter($article_filters, 'mark')) {
dd7d3187
AD
742 $marked = 'true';
743 } else {
744 $marked = 'false';
745 }
19c9cb11 746
11b0dce2
AD
747 $result = db_query($link,
748 "INSERT INTO ttrss_user_entries
dd7d3187 749 (ref_id, owner_uid, feed_id, unread, last_read, marked)
11b0dce2 750 VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
dd7d3187 751 $last_read_qpart, $marked)");
11b0dce2
AD
752 }
753
6385315d
AD
754 $post_needs_update = false;
755
8d0ec6fd 756 if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid, false) &&
6385315d
AD
757 ($content_hash != $orig_content_hash)) {
758 $post_needs_update = true;
759 }
760
761 if ($orig_title != $entry_title) {
762 $post_needs_update = true;
763 }
764
11b0dce2
AD
765 if ($orig_num_comments != $num_comments) {
766 $post_needs_update = true;
767 }
768
6385315d
AD
769// this doesn't seem to be very reliable
770//
771// if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) {
772// $post_needs_update = true;
773// }
774
775 // if post needs update, update it and mark all user entries
1c73bc0c 776 // linking to this post as updated
6385315d
AD
777 if ($post_needs_update) {
778
779// print "<!-- post $orig_title needs update : $post_needs_update -->";
780
6385315d 781 db_query($link, "UPDATE ttrss_entries
11b0dce2
AD
782 SET title = '$entry_title', content = '$entry_content',
783 num_comments = '$num_comments'
6385315d
AD
784 WHERE id = '$ref_id'");
785
8d0ec6fd 786 if (get_pref($link, "MARK_UNREAD_ON_UPDATE", $owner_uid, false)) {
4919fb42
AD
787 db_query($link, "UPDATE ttrss_user_entries
788 SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
789 } else {
790 db_query($link, "UPDATE ttrss_user_entries
791 SET last_read = null WHERE ref_id = '$ref_id' AND unread = false");
792 }
6385315d
AD
793
794 }
4c193675
AD
795 }
796
44e241cb
AD
797 db_query($link, "COMMIT");
798
34e420fb
AD
799 if (defined('DAEMON_EXTENDED_DEBUG')) {
800 _debug("update_rss_feed: looking for tags...");
801 }
802
eb36b4eb
AD
803 /* taaaags */
804 // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, //
805
05732aa0 806 $entry_tags = null;
eb36b4eb 807
372ced8b 808 preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i",
ee2c3050
AD
809 $entry_content_unescaped, $entry_tags);
810
811// print "<br>$entry_title : $entry_content_unescaped<br>";
812// print_r($entry_tags);
372ced8b 813// print "<br>";
eb36b4eb
AD
814
815 $entry_tags = $entry_tags[1];
816
073ca0e6
AD
817 # check for manual tags
818
f8382011
AD
819 $tag_filter = find_article_filter($article_filters, "tag");
820
821 if ($tag_filter) {
073ca0e6 822
f8382011 823 $manual_tags = trim_array(split(",", $tag_filter[1]));
073ca0e6 824
f8382011 825 foreach ($manual_tags as $tag) {
be832a1a
AD
826 if (tag_is_valid($tag)) {
827 array_push($entry_tags, $tag);
828 }
829 }
830 }
831
ef063748 832/* if ($subject_tags) {
be832a1a
AD
833 foreach ($subject_tags as $tag) {
834 if (tag_is_valid($tag)) {
073ca0e6
AD
835 array_push($entry_tags, $tag);
836 }
837 }
ef063748 838 } */
073ca0e6 839
eb36b4eb
AD
840 if (count($entry_tags) > 0) {
841
44e241cb
AD
842 db_query($link, "BEGIN");
843
05732aa0
AD
844 $result = db_query($link, "SELECT id,int_id
845 FROM ttrss_entries,ttrss_user_entries
25da6909 846 WHERE guid = '$entry_guid'
05732aa0 847 AND feed_id = '$feed' AND ref_id = id
7fed1940 848 AND owner_uid = '$owner_uid'");
eb36b4eb 849
fe99ab12 850 if (db_num_rows($result) == 1) {
eb36b4eb 851
fe99ab12
AD
852 $entry_id = db_fetch_result($result, 0, "id");
853 $entry_int_id = db_fetch_result($result, 0, "int_id");
854
855 foreach ($entry_tags as $tag) {
ef063748 856 $tag = db_escape_string(mb_strtolower(strip_tags($tag)));
31483fc1
AD
857
858 $tag = str_replace("+", " ", $tag);
fe99ab12 859 $tag = str_replace("technorati tag: ", "", $tag);
ef063748
AD
860
861 if (!tag_is_valid($tag)) continue;
862
fe99ab12
AD
863 $result = db_query($link, "SELECT id FROM ttrss_tags
864 WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
865 owner_uid = '$owner_uid' LIMIT 1");
866
867 // print db_fetch_result($result, 0, "id");
868
869 if ($result && db_num_rows($result) == 0) {
870
871 // print "tagging $entry_id as $tag<br>";
872
873 db_query($link, "INSERT INTO ttrss_tags
874 (owner_uid,tag_name,post_int_id)
875 VALUES ('$owner_uid','$tag', '$entry_int_id')");
876 }
877 }
eb36b4eb 878 }
44e241cb 879 db_query($link, "COMMIT");
05732aa0 880 }
4c193675 881 }
40d13c28 882
ab3d0b99
AD
883 db_query($link, "UPDATE ttrss_feeds
884 SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
eb36b4eb 885
44e241cb 886// db_query($link, "COMMIT");
dd8c76a9 887
ab3d0b99
AD
888 } else {
889 $error_msg = db_escape_string(magpie_error());
890 db_query($link,
aa5f9f5f
AD
891 "UPDATE ttrss_feeds SET last_error = '$error_msg',
892 last_updated = NOW() WHERE id = '$feed'");
40d13c28
AD
893 }
894
219bd8fc 895 if (defined('DAEMON_EXTENDED_DEBUG')) {
34e420fb 896 _debug("update_rss_feed: done");
219bd8fc
AD
897 }
898
40d13c28
AD
899 }
900
f175937c 901 function print_select($id, $default, $values, $attributes = "") {
79f3553b 902 print "<select name=\"$id\" id=\"$id\" $attributes>";
a0d53889
AD
903 foreach ($values as $v) {
904 if ($v == $default)
905 $sel = " selected";
906 else
907 $sel = "";
908
909 print "<option$sel>$v</option>";
910 }
911 print "</select>";
912 }
40d13c28 913
79f3553b
AD
914 function print_select_hash($id, $default, $values, $attributes = "") {
915 print "<select name=\"$id\" id='$id' $attributes>";
673d54ca
AD
916 foreach (array_keys($values) as $v) {
917 if ($v == $default)
918 $sel = "selected";
919 else
920 $sel = "";
921
922 print "<option $sel value=\"$v\">".$values[$v]."</option>";
923 }
924
925 print "</select>";
926 }
927
f8382011 928 function get_article_filters($filters, $title, $content, $link) {
240054f1 929 $matches = array();
c2d9322b 930
240054f1
AD
931 if ($filters["title"]) {
932 foreach ($filters["title"] as $filter) {
c2d9322b
AD
933 $reg_exp = $filter["reg_exp"];
934 $inverse = $filter["inverse"];
935 if ((!$inverse && preg_match("/$reg_exp/i", $title)) ||
936 ($inverse && !preg_match("/$reg_exp/i", $title))) {
937
240054f1
AD
938 array_push($matches, array($filter["action"], $filter["action_param"]));
939 }
940 }
941 }
942
943 if ($filters["content"]) {
944 foreach ($filters["content"] as $filter) {
c2d9322b
AD
945 $reg_exp = $filter["reg_exp"];
946 $inverse = $filter["inverse"];
947
948 if ((!$inverse && preg_match("/$reg_exp/i", $content)) ||
949 ($inverse && !preg_match("/$reg_exp/i", $content))) {
950
240054f1
AD
951 array_push($matches, array($filter["action"], $filter["action_param"]));
952 }
953 }
954 }
955
956 if ($filters["both"]) {
957 foreach ($filters["both"] as $filter) {
958 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
959 $inverse = $filter["inverse"];
960
961 if ($inverse) {
962 if (!preg_match("/$reg_exp/i", $title) || !preg_match("/$reg_exp/i", $content)) {
963 array_push($matches, array($filter["action"], $filter["action_param"]));
964 }
965 } else {
966 if (preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) {
967 array_push($matches, array($filter["action"], $filter["action_param"]));
968 }
240054f1
AD
969 }
970 }
971 }
972
973 if ($filters["link"]) {
974 $reg_exp = $filter["reg_exp"];
975 foreach ($filters["link"] as $filter) {
976 $reg_exp = $filter["reg_exp"];
c2d9322b
AD
977 $inverse = $filter["inverse"];
978
979 if ((!$inverse && preg_match("/$reg_exp/i", $link)) ||
980 ($inverse && !preg_match("/$reg_exp/i", $link))) {
981
240054f1
AD
982 array_push($matches, array($filter["action"], $filter["action_param"]));
983 }
984 }
985 }
986
987 return $matches;
988 }
989
f8382011
AD
990 function find_article_filter($filters, $filter_name) {
991 foreach ($filters as $f) {
992 if ($f[0] == $filter_name) {
993 return $f;
994 };
995 }
996 return false;
997 }
998
9323147e 999 function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link,
fb1fb4ab 1000 $rtl_content = false, $last_updated = false, $last_error = false) {
254e0e4b
AD
1001
1002 if (file_exists($icon_file) && filesize($icon_file) > 0) {
023fe037 1003 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">";
254e0e4b 1004 } else {
023fe037 1005 $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">";
254e0e4b
AD
1006 }
1007
9323147e
AD
1008 if ($rtl_content) {
1009 $rtl_tag = "dir=\"rtl\"";
1010 } else {
1011 $rtl_tag = "dir=\"ltr\"";
1012 }
1013
78d5212c
AD
1014 $error_notify_msg = "";
1015
fb1fb4ab
AD
1016 if ($last_error) {
1017 $link_title = "Error: $last_error ($last_updated)";
78d5212c 1018 $error_notify_msg = "(Error)";
ad780e9c 1019 } else if ($last_updated) {
fb1fb4ab
AD
1020 $link_title = "Updated: $last_updated";
1021 }
1022
7210613a 1023 $feed = "<a title=\"$link_title\" id=\"FEEDL-$feed_id\"
c50e2b30 1024 href=\"javascript:viewfeed('$feed_id', '', false, '', false, 0);\">$feed_title</a>";
254e0e4b
AD
1025
1026 print "<li id=\"FEEDR-$feed_id\" class=\"$class\">";
b619ff15 1027 if (get_pref($link, 'ENABLE_FEED_ICONS')) {
254e0e4b
AD
1028 print "$feed_icon";
1029 }
1030
9323147e 1031 print "<span $rtl_tag id=\"FEEDN-$feed_id\">$feed</span>";
254e0e4b
AD
1032
1033 if ($unread != 0) {
1034 $fctr_class = "";
1035 } else {
1036 $fctr_class = "class=\"invisible\"";
1037 }
1038
9323147e 1039 print " <span $rtl_tag $fctr_class id=\"FEEDCTR-$feed_id\">
254e0e4b 1040 (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>";
78d5212c
AD
1041
1042 if (get_pref($link, "EXTENDED_FEEDLIST")) {
1043 print "<div class=\"feedExtInfo\">
1044 <span id=\"FLUPD-$feed_id\">$last_updated $error_notify_msg</span></div>";
1045 }
1046
254e0e4b
AD
1047 print "</li>";
1048
1049 }
1050
406d9489
AD
1051 function getmicrotime() {
1052 list($usec, $sec) = explode(" ",microtime());
1053 return ((float)$usec + (float)$sec);
1054 }
1055
77e96719
AD
1056 function print_radio($id, $default, $values, $attributes = "") {
1057 foreach ($values as $v) {
1058
1059 if ($v == $default)
5da169d9 1060 $sel = "checked";
77e96719 1061 else
5da169d9
AD
1062 $sel = "";
1063
1064 if ($v == "Yes") {
1065 $sel .= " value=\"1\"";
1066 } else {
1067 $sel .= " value=\"0\"";
1068 }
77e96719 1069
69654950
AD
1070 print "<input class=\"noborder\"
1071 type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
77e96719
AD
1072
1073 }
1074 }
1075
ff485f1d
AD
1076 function initialize_user_prefs($link, $uid) {
1077
1078 $uid = db_escape_string($uid);
1079
1080 db_query($link, "BEGIN");
1081
1082 $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs");
1083
1084 $u_result = db_query($link, "SELECT pref_name
1085 FROM ttrss_user_prefs WHERE owner_uid = '$uid'");
1086
1087 $active_prefs = array();
1088
1089 while ($line = db_fetch_assoc($u_result)) {
1090 array_push($active_prefs, $line["pref_name"]);
1091 }
1092
1093 while ($line = db_fetch_assoc($result)) {
1094 if (array_search($line["pref_name"], $active_prefs) === FALSE) {
1095// print "adding " . $line["pref_name"] . "<br>";
1096
1097 db_query($link, "INSERT INTO ttrss_user_prefs
1098 (owner_uid,pref_name,value) VALUES
1099 ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
1100
1101 }
1102 }
1103
1104 db_query($link, "COMMIT");
1105
1106 }
956c7629
AD
1107
1108 function lookup_user_id($link, $user) {
1109
1110 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
1111 login = '$login'");
1112
1113 if (db_num_rows($result) == 1) {
1114 return db_fetch_result($result, 0, "id");
1115 } else {
1116 return false;
1117 }
1118 }
1119
18664970
AD
1120 function http_authenticate_user($link) {
1121
1122 if (!$_SERVER["PHP_AUTH_USER"]) {
1123
1124 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1125 header('HTTP/1.0 401 Unauthorized');
1126 exit;
1127
1128 } else {
1129 $auth_result = authenticate_user($link,
1130 $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]);
1131
1132 if (!$auth_result) {
1133 header('WWW-Authenticate: Basic realm="Tiny Tiny RSS RSSGen"');
1134 header('HTTP/1.0 401 Unauthorized');
1135 exit;
1136 }
1137 }
1138
1139 return true;
1140 }
1141
461766f3 1142 function authenticate_user($link, $login, $password, $force_auth = false) {
c8437f35 1143
131b01b3 1144 if (!SINGLE_USER_MODE) {
c8437f35 1145
131b01b3 1146 $pwd_hash = 'SHA1:' . sha1($password);
461766f3
AD
1147
1148 if ($force_auth && defined('_DEBUG_USER_SWITCH')) {
1149 $query = "SELECT id,login,access_level
1150 FROM ttrss_users WHERE
1151 login = '$login'";
1152 } else {
1153 $query = "SELECT id,login,access_level
1154 FROM ttrss_users WHERE
1155 login = '$login' AND pwd_hash = '$pwd_hash'";
1156 }
1157
1158 $result = db_query($link, $query);
131b01b3
AD
1159
1160 if (db_num_rows($result) == 1) {
1161 $_SESSION["uid"] = db_fetch_result($result, 0, "id");
1162 $_SESSION["name"] = db_fetch_result($result, 0, "login");
1163 $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
1164
1165 db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
1166 $_SESSION["uid"]);
1167
1168 $user_theme = get_user_theme_path($link);
1169
1170 $_SESSION["theme"] = $user_theme;
1171 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1172
1173 initialize_user_prefs($link, $_SESSION["uid"]);
1174
1175 return true;
1176 }
1177
1178 return false;
503eb349 1179
131b01b3 1180 } else {
503eb349 1181
131b01b3
AD
1182 $_SESSION["uid"] = 1;
1183 $_SESSION["name"] = "admin";
f557cd78 1184
0bbba72d
AD
1185 $user_theme = get_user_theme_path($link);
1186
1187 $_SESSION["theme"] = $user_theme;
1188 $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
1189
1190 initialize_user_prefs($link, $_SESSION["uid"]);
1191
c8437f35
AD
1192 return true;
1193 }
c8437f35
AD
1194 }
1195
e6cb77a0
AD
1196 function make_password($length = 8) {
1197
1198 $password = "";
798f722b
AD
1199 $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
1200
1201 $i = 0;
e6cb77a0
AD
1202
1203 while ($i < $length) {
1204 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
1205
1206 if (!strstr($password, $char)) {
1207 $password .= $char;
1208 $i++;
1209 }
1210 }
1211 return $password;
1212 }
1213
1214 // this is called after user is created to initialize default feeds, labels
1215 // or whatever else
1216
1217 // user preferences are checked on every login, not here
1218
1219 function initialize_user($link, $uid) {
1220
1221 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1222 values ('$uid','unread = true', 'Unread articles')");
1223
1224 db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description)
1225 values ('$uid','last_read is null and unread = false', 'Updated articles')");
1226
1227 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
74bff337 1228 values ('$uid', 'Tiny Tiny RSS: New Releases',
628fcd2c 1229 'http://tt-rss.spb.ru/releases.rss')");
3b0feb9b 1230
cd2cd415
AD
1231 db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url)
1232 values ('$uid', 'Tiny Tiny RSS: Forum',
1233 'http://tt-rss.spb.ru/forum/rss.php')");
3b0feb9b 1234 }
e6cb77a0 1235
b8aa49bc 1236 function logout_user() {
5ccc1cf5
AD
1237 session_destroy();
1238 if (isset($_COOKIE[session_name()])) {
1239 setcookie(session_name(), '', time()-42000, '/');
1240 }
b8aa49bc
AD
1241 }
1242
75836f33 1243 function get_script_urlpath() {
87a79fa4 1244 return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]);
75836f33
AD
1245 }
1246
916f788a 1247 function validate_session($link) {
a2e9b457 1248 if (SESSION_CHECK_ADDRESS && $_SESSION["uid"]) {
916f788a
AD
1249 if ($_SESSION["ip_address"]) {
1250 if ($_SESSION["ip_address"] != $_SERVER["REMOTE_ADDR"]) {
7f0acba7 1251 $_SESSION["login_error_msg"] = "Session failed to validate (incorrect IP)";
916f788a
AD
1252 return false;
1253 }
1254 }
1255 }
d620cfe7 1256
a885f0ec 1257/* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) {
d620cfe7 1258
8e849206 1259 //print_r($_SESSION);
d620cfe7
AD
1260
1261 if (time() > $_SESSION["cookie_lifetime"]) {
1262 return false;
1263 }
a885f0ec
AD
1264 } */
1265
916f788a
AD
1266 return true;
1267 }
1268
793185a9 1269 function login_sequence($link, $mobile = false) {
b8aa49bc 1270 if (!SINGLE_USER_MODE) {
75836f33 1271
461766f3
AD
1272 if (defined('_DEBUG_USER_SWITCH') && $_SESSION["uid"]) {
1273 $swu = db_escape_string($_REQUEST["swu"]);
1274 if ($swu) {
1275 $_SESSION["prefs_cache"] = false;
1276 return authenticate_user($link, $swu, null, true);
1277 }
1278 }
1279
7f0acba7 1280 $login_action = $_POST["login_action"];
a885f0ec 1281
01a87dff 1282 # try to authenticate user if called from login form
7f0acba7 1283 if ($login_action == "do_login") {
01a87dff
AD
1284 $login = $_POST["login"];
1285 $password = $_POST["password"];
d620cfe7 1286 $remember_me = $_POST["remember_me"];
f557cd78 1287
01a87dff
AD
1288 if (authenticate_user($link, $login, $password)) {
1289 $_POST["password"] = "";
d620cfe7 1290
d620cfe7
AD
1291 header("Location: " . $_SERVER["REQUEST_URI"]);
1292 exit;
1293
01a87dff 1294 return;
7f0acba7
AD
1295 } else {
1296 $_SESSION["login_error_msg"] = "Incorrect username or password";
01a87dff
AD
1297 }
1298 }
1299
1df0f48b
AD
1300// print session_id();
1301// print_r($_SESSION);
7f0acba7
AD
1302
1303 if (!$_SESSION["uid"] || !validate_session($link)) {
793185a9 1304 render_login_form($link, $mobile);
01a87dff 1305 exit;
b8aa49bc 1306 }
d620cfe7 1307
1df0f48b 1308
b8aa49bc 1309 } else {
0bbba72d 1310 return authenticate_user($link, "admin", null);
b8aa49bc
AD
1311 }
1312 }
3547842a
AD
1313
1314 function truncate_string($str, $max_len) {
12db369c
AD
1315 if (mb_strlen($str, "utf-8") > $max_len - 3) {
1316 return mb_substr($str, 0, $max_len, "utf-8") . "...";
3547842a
AD
1317 } else {
1318 return $str;
1319 }
1320 }
54a60e1a
AD
1321
1322 function get_user_theme_path($link) {
798f722b
AD
1323 $result = db_query($link, "SELECT theme_path
1324 FROM
1325 ttrss_themes,ttrss_users
1326 WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]);
54a60e1a
AD
1327 if (db_num_rows($result) != 0) {
1328 return db_fetch_result($result, 0, "theme_path");
1329 } else {
1330 return null;
1331 }
1332 }
be773442
AD
1333
1334 function smart_date_time($timestamp) {
1335 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1336 return date("G:i", $timestamp);
f26450f1 1337 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1338 return date("M d, G:i", $timestamp);
1339 } else {
b02111c2 1340 return date("Y/m/d G:i", $timestamp);
be773442
AD
1341 }
1342 }
1343
1344 function smart_date($timestamp) {
1345 if (date("Y.m.d", $timestamp) == date("Y.m.d")) {
1346 return "Today";
f26450f1 1347 } else if (date("Y", $timestamp) == date("Y")) {
be773442
AD
1348 return date("D m", $timestamp);
1349 } else {
b02111c2 1350 return date("Y/m/d", $timestamp);
be773442
AD
1351 }
1352 }
a654a595
AD
1353
1354 function sql_bool_to_string($s) {
1355 if ($s == "t" || $s == "1") {
1356 return "true";
1357 } else {
1358 return "false";
1359 }
1360 }
e3c99f3b
AD
1361
1362 function sql_bool_to_bool($s) {
1363 if ($s == "t" || $s == "1") {
1364 return true;
1365 } else {
1366 return false;
1367 }
1368 }
0ea4fb50 1369
e3c99f3b 1370
0ea4fb50
AD
1371 function toggleEvenOdd($a) {
1372 if ($a == "even")
1373 return "odd";
1374 else
1375 return "even";
1376 }
6043fb7e
AD
1377
1378 function sanity_check($link) {
9cbca41f 1379
aec3ce39
AD
1380 error_reporting(0);
1381
6043fb7e
AD
1382 $error_code = 0;
1383 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
1384 $schema_version = db_fetch_result($result, 0, "schema_version");
1385
1386 if ($schema_version != SCHEMA_VERSION) {
1387 $error_code = 5;
1388 }
1389
aec3ce39
AD
1390 if (DB_TYPE == "mysql") {
1391 $result = db_query($link, "SELECT true", false);
1392 if (db_num_rows($result) != 1) {
1393 $error_code = 10;
1394 }
1395 }
1396
1397 error_reporting (DEFAULT_ERROR_LEVEL);
1398
6043fb7e 1399 if ($error_code != 0) {
aec3ce39 1400 print_error_xml($error_code);
6043fb7e
AD
1401 return false;
1402 } else {
1403 return true;
4220d6b0 1404 }
6043fb7e
AD
1405 }
1406
27981ca3
AD
1407 function file_is_locked($filename) {
1408 error_reporting(0);
1409 $fp = fopen($filename, "r");
1410 error_reporting(DEFAULT_ERROR_LEVEL);
1411 if ($fp) {
1412 if (flock($fp, LOCK_EX | LOCK_NB)) {
1413 flock($fp, LOCK_UN);
1414 fclose($fp);
1415 return false;
1416 }
1417 fclose($fp);
1418 return true;
1419 }
1420 return false;
1421 }
1422
fcb4c0c9
AD
1423 function make_lockfile($filename) {
1424 $fp = fopen($filename, "w");
1425
1426 if (flock($fp, LOCK_EX | LOCK_NB)) {
1427 return $fp;
1428 } else {
1429 return false;
1430 }
1431 }
1432
894ebcf5
AD
1433 function sql_random_function() {
1434 if (DB_TYPE == "mysql") {
1435 return "RAND()";
1436 } else {
1437 return "RANDOM()";
1438 }
1439 }
1440
23aa0d16 1441 function catchup_feed($link, $feed, $cat_view) {
88040f57
AD
1442
1443 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
23aa0d16
AD
1444
1445 if ($cat_view) {
1446
1447 if ($feed > 0) {
1448 $cat_qpart = "cat_id = '$feed'";
1449 } else {
1450 $cat_qpart = "cat_id IS NULL";
1451 }
1452
1453 $tmp_result = db_query($link, "SELECT id
1454 FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = " .
1455 $_SESSION["uid"]);
1456
1457 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1458
1459 $tmp_feed = $tmp_line["id"];
1460
1461 db_query($link, "UPDATE ttrss_user_entries
1462 SET unread = false,last_read = NOW()
1463 WHERE feed_id = '$tmp_feed' AND owner_uid = " . $_SESSION["uid"]);
1464 }
1465
1466 } else if ($feed > 0) {
1467
1468 $tmp_result = db_query($link, "SELECT id
1469 FROM ttrss_feeds WHERE parent_feed = '$feed'
1470 ORDER BY cat_id,title");
1471
1472 $parent_ids = array();
1473
1474 if (db_num_rows($tmp_result) > 0) {
1475 while ($p = db_fetch_assoc($tmp_result)) {
1476 array_push($parent_ids, "feed_id = " . $p["id"]);
1477 }
1478
1479 $children_qpart = implode(" OR ", $parent_ids);
1480
1481 db_query($link, "UPDATE ttrss_user_entries
1482 SET unread = false,last_read = NOW()
1483 WHERE (feed_id = '$feed' OR $children_qpart)
1484 AND owner_uid = " . $_SESSION["uid"]);
1485
1486 } else {
1487 db_query($link, "UPDATE ttrss_user_entries
1488 SET unread = false,last_read = NOW()
1489 WHERE feed_id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
1490 }
1491
1492 } else if ($feed < 0 && $feed > -10) { // special, like starred
1493
1494 if ($feed == -1) {
1495 db_query($link, "UPDATE ttrss_user_entries
1496 SET unread = false,last_read = NOW()
1497 WHERE marked = true AND owner_uid = ".$_SESSION["uid"]);
1498 }
1499
1500 } else if ($feed < -10) { // label
1501
1502 // TODO make this more efficient
1503
1504 $label_id = -$feed - 11;
1505
1506 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
1507 WHERE id = '$label_id'");
1508
1509 if ($tmp_result) {
1510 $sql_exp = db_fetch_result($tmp_result, 0, "sql_exp");
1511
1512 db_query($link, "BEGIN");
1513
1514 $tmp2_result = db_query($link,
1515 "SELECT
1516 int_id
1517 FROM
88040f57 1518 ttrss_user_entries,ttrss_entries,ttrss_feeds
23aa0d16 1519 WHERE
88040f57
AD
1520 ref_id = ttrss_entries.id AND
1521 ttrss_user_entries.feed_id = ttrss_feeds.id AND
23aa0d16 1522 $sql_exp AND
88040f57 1523 ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
23aa0d16
AD
1524
1525 while ($tmp_line = db_fetch_assoc($tmp2_result)) {
1526 db_query($link, "UPDATE
1527 ttrss_user_entries
1528 SET
1529 unread = false, last_read = NOW()
1530 WHERE
1531 int_id = " . $tmp_line["int_id"]);
1532 }
1533
1534 db_query($link, "COMMIT");
1535
1536/* db_query($link, "UPDATE ttrss_user_entries,ttrss_entries
1537 SET unread = false,last_read = NOW()
1538 WHERE $sql_exp
1539 AND ref_id = id
1540 AND owner_uid = ".$_SESSION["uid"]); */
1541 }
1542 }
1543 } else { // tag
1544 db_query($link, "BEGIN");
1545
1546 $tag_name = db_escape_string($feed);
1547
1548 $result = db_query($link, "SELECT post_int_id FROM ttrss_tags
1549 WHERE tag_name = '$tag_name' AND owner_uid = " . $_SESSION["uid"]);
1550
1551 while ($line = db_fetch_assoc($result)) {
1552 db_query($link, "UPDATE ttrss_user_entries SET
1553 unread = false, last_read = NOW()
1554 WHERE int_id = " . $line["post_int_id"]);
1555 }
1556 db_query($link, "COMMIT");
1557 }
1558 }
1559
1560 function update_generic_feed($link, $feed, $cat_view) {
1561 if ($cat_view) {
1562
1563 if ($feed > 0) {
1564 $cat_qpart = "cat_id = '$feed'";
1565 } else {
1566 $cat_qpart = "cat_id IS NULL";
1567 }
1568
1569 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1570 WHERE $cat_qpart AND owner_uid = " . $_SESSION["uid"]);
1571
1572 while ($tmp_line = db_fetch_assoc($tmp_result)) {
1573 $feed_url = $tmp_line["feed_url"];
1574 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1575 }
1576
1577 } else {
1578 $tmp_result = db_query($link, "SELECT feed_url FROM ttrss_feeds
1579 WHERE id = '$feed'");
1580 $feed_url = db_fetch_result($tmp_result, 0, "feed_url");
1581 update_rss_feed($link, $feed_url, $feed, ENABLE_UPDATE_DAEMON);
1582 }
1583 }
a9cb1f83 1584
cf4d339c
AD
1585 function getAllCounters($link, $omode = "tflc") {
1586/* getLabelCounters($link);
a9cb1f83
AD
1587 getFeedCounters($link);
1588 getTagCounters($link);
1589 getGlobalCounters($link);
1590 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1591 getCategoryCounters($link);
cf4d339c
AD
1592 } */
1593
1594 if (!$omode) $omode = "tflc";
1595
1596 getGlobalCounters($link);
1597
1598 if (strchr($omode, "l")) getLabelCounters($link);
1599 if (strchr($omode, "f")) getFeedCounters($link);
1600 if (strchr($omode, "t")) getTagCounters($link);
1601 if (strchr($omode, "c")) {
1602 if (get_pref($link, 'ENABLE_FEED_CATS')) {
1603 getCategoryCounters($link);
1604 }
a9cb1f83
AD
1605 }
1606 }
1607
1608 function getCategoryCounters($link) {
1609 $result = db_query($link, "SELECT cat_id,SUM((SELECT COUNT(int_id)
1610 FROM ttrss_user_entries WHERE feed_id = ttrss_feeds.id
1611 AND unread = true)) AS unread FROM ttrss_feeds
1612 WHERE
cfb02131 1613 hidden = false AND owner_uid = ".$_SESSION["uid"]." GROUP BY cat_id");
a9cb1f83
AD
1614
1615 while ($line = db_fetch_assoc($result)) {
1616 $line["cat_id"] = sprintf("%d", $line["cat_id"]);
1617 print "<counter type=\"category\" id=\"".$line["cat_id"]."\" counter=\"".
1618 $line["unread"]."\"/>";
1619 }
1620 }
1621
f295c368
AD
1622 function getCategoryUnread($link, $cat) {
1623
18664970
AD
1624 if ($cat != 0) {
1625 $cat_query = "cat_id = '$cat'";
1626 } else {
1627 $cat_query = "cat_id IS NULL";
1628 }
1629
1630 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query
cfb02131 1631 AND hidden = false
f295c368
AD
1632 AND owner_uid = " . $_SESSION["uid"]);
1633
1634 $cat_feeds = array();
1635 while ($line = db_fetch_assoc($result)) {
1636 array_push($cat_feeds, "feed_id = " . $line["id"]);
1637 }
1638
18664970
AD
1639 if (count($cat_feeds) == 0) return 0;
1640
f295c368
AD
1641 $match_part = implode(" OR ", $cat_feeds);
1642
1643 $result = db_query($link, "SELECT COUNT(int_id) AS unread
1644 FROM ttrss_user_entries
4919fb42 1645 WHERE unread = true AND ($match_part) AND owner_uid = " . $_SESSION["uid"]);
f295c368
AD
1646
1647 $unread = 0;
1648
1649 # this needs to be rewritten
1650 while ($line = db_fetch_assoc($result)) {
1651 $unread += $line["unread"];
1652 }
1653
1654 return $unread;
1655
1656 }
1657
1658 function getFeedUnread($link, $feed, $is_cat = false) {
a9cb1f83 1659 $n_feed = sprintf("%d", $feed);
f295c368
AD
1660
1661 if ($is_cat) {
831ff047 1662 return getCategoryUnread($link, $n_feed);
f295c368 1663 } else if ($n_feed == -1) {
a9cb1f83 1664 $match_part = "marked = true";
4919fb42 1665 } else if ($n_feed > 0) {
831ff047 1666
e8b8485f
AD
1667 $result = db_query($link, "SELECT id FROM ttrss_feeds
1668 WHERE parent_feed = '$n_feed'
318260cc 1669 AND hidden = false
4919fb42 1670 AND owner_uid = " . $_SESSION["uid"]);
831ff047
AD
1671
1672 if (db_num_rows($result) > 0) {
4919fb42 1673
831ff047
AD
1674 $linked_feeds = array();
1675 while ($line = db_fetch_assoc($result)) {
1676 array_push($linked_feeds, "feed_id = " . $line["id"]);
1677 }
e8b8485f
AD
1678
1679 array_push($linked_feeds, "feed_id = $n_feed");
831ff047
AD
1680
1681 $match_part = implode(" OR ", $linked_feeds);
1682
4919fb42 1683 $result = db_query($link, "SELECT COUNT(int_id) AS unread
318260cc 1684 FROM ttrss_user_entries
e8b8485f
AD
1685 WHERE unread = true AND ($match_part)
1686 AND owner_uid = " . $_SESSION["uid"]);
4919fb42
AD
1687
1688 $unread = 0;
1689
1690 # this needs to be rewritten
1691 while ($line = db_fetch_assoc($result)) {
1692 $unread += $line["unread"];
1693 }
1694
1695 return $unread;
1696
831ff047
AD
1697 } else {
1698 $match_part = "feed_id = '$n_feed'";
1699 }
a9cb1f83 1700 } else if ($feed < -10) {
318260cc 1701
a9cb1f83
AD
1702 $label_id = -$feed - 11;
1703
1704 $result = db_query($link, "SELECT sql_exp FROM ttrss_labels WHERE
1705 id = '$label_id' AND owner_uid = " . $_SESSION["uid"]);
1706
1707 $match_part = db_fetch_result($result, 0, "sql_exp");
1708 }
1709
1710 if ($match_part) {
1711
1712 $result = db_query($link, "SELECT count(int_id) AS unread
88040f57
AD
1713 FROM ttrss_user_entries,ttrss_feeds,ttrss_entries WHERE
1714 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1715 ttrss_user_entries.ref_id = ttrss_entries.id AND
cfb02131 1716 ttrss_feeds.hidden = false AND
88040f57 1717 unread = true AND ($match_part) AND ttrss_user_entries.owner_uid = " . $_SESSION["uid"]);
a9cb1f83
AD
1718
1719 } else {
1720
1721 $result = db_query($link, "SELECT COUNT(post_int_id) AS unread
1722 FROM ttrss_tags,ttrss_user_entries
1723 WHERE tag_name = '$feed' AND post_int_id = int_id AND unread = true AND
1724 ttrss_tags.owner_uid = " . $_SESSION["uid"]);
1725 }
1726
1727 $unread = db_fetch_result($result, 0, "unread");
cfb02131 1728
a9cb1f83
AD
1729 return $unread;
1730 }
1731
1732 /* FIXME this needs reworking */
1733
f3acc32e
AD
1734 function getGlobalUnread($link, $user_id = false) {
1735
1736 if (!$user_id) {
1737 $user_id = $_SESSION["uid"];
1738 }
1739
3831db41 1740 $result = db_query($link, "SELECT count(ttrss_entries.id) as c_id FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1741 WHERE unread = true AND
3831db41 1742 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1743 ttrss_user_entries.ref_id = ttrss_entries.id AND
3831db41 1744 hidden = false AND
f3acc32e 1745 ttrss_user_entries.owner_uid = '$user_id'");
a9cb1f83
AD
1746 $c_id = db_fetch_result($result, 0, "c_id");
1747 return $c_id;
1748 }
1749
1750 function getGlobalCounters($link, $global_unread = -1) {
1751 if ($global_unread == -1) {
1752 $global_unread = getGlobalUnread($link);
1753 }
7bf7e4d3
AD
1754 print "<counter type=\"global\" id='global-unread'
1755 counter='$global_unread'/>";
1756
1757 $result = db_query($link, "SELECT COUNT(id) AS fn FROM
1758 ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
1759
1760 $subscribed_feeds = db_fetch_result($result, 0, "fn");
1761
1762 print "<counter type=\"global\" id='subscribed-feeds'
1763 counter='$subscribed_feeds'/>";
1764
a9cb1f83
AD
1765 }
1766
1767 function getTagCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1768
1769 if ($smart_mode) {
1770 if (!$_SESSION["tctr_last_value"]) {
1771 $_SESSION["tctr_last_value"] = array();
1772 }
1773 }
1774
1775 $old_counters = $_SESSION["tctr_last_value"];
1776
1777 $tctrs_modified = false;
1778
1779/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
1780 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
1781 ttrss_user_entries.ref_id = ttrss_entries.id AND
1782 ttrss_tags.owner_uid = ".$_SESSION["uid"]." AND
1783 post_int_id = ttrss_user_entries.int_id AND unread = true GROUP BY tag_name
1784 UNION
1785 select tag_name,0 as count FROM ttrss_tags
1786 WHERE ttrss_tags.owner_uid = ".$_SESSION["uid"]); */
1787
1788 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
1789 FROM ttrss_user_entries WHERE int_id = post_int_id
1790 AND unread = true)) AS count FROM ttrss_tags
22e00732 1791 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
a9cb1f83
AD
1792
1793 $tags = array();
1794
1795 while ($line = db_fetch_assoc($result)) {
1796 $tags[$line["tag_name"]] += $line["count"];
1797 }
1798
1799 foreach (array_keys($tags) as $tag) {
1800 $unread = $tags[$tag];
1801
1802 $tag = htmlspecialchars($tag);
1803
1804 if (!$smart_mode || $old_counters[$tag] != $unread) {
1805 $old_counters[$tag] = $unread;
1806 $tctrs_modified = true;
1807 print "<counter type=\"tag\" id=\"$tag\" counter=\"$unread\"/>";
1808 }
1809
1810 }
1811
1812 if ($smart_mode && $tctrs_modified) {
1813 $_SESSION["tctr_last_value"] = $old_counters;
1814 }
1815
1816 }
1817
ef393de7 1818 function getLabelCounters($link, $smart_mode = SMART_RPC_COUNTERS, $ret_mode = false) {
a9cb1f83
AD
1819
1820 if ($smart_mode) {
1821 if (!$_SESSION["lctr_last_value"]) {
1822 $_SESSION["lctr_last_value"] = array();
1823 }
1824 }
1825
ef393de7
AD
1826 $ret_arr = array();
1827
a9cb1f83
AD
1828 $old_counters = $_SESSION["lctr_last_value"];
1829 $lctrs_modified = false;
1830
88040f57 1831 $result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
a9cb1f83 1832 WHERE marked = true AND ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57
AD
1833 ttrss_user_entries.feed_id = ttrss_feeds.id AND
1834 unread = true AND ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1835
1836 $count = db_fetch_result($result, 0, "count");
1837
ef393de7
AD
1838 if (!$ret_mode) {
1839 print "<counter type=\"label\" id=\"-1\" counter=\"$count\"/>";
1840 } else {
1841 $ret_arr["-1"]["counter"] = $count;
1842 $ret_arr["-1"]["description"] = "Starred";
1843 }
a9cb1f83
AD
1844
1845 $result = db_query($link, "SELECT owner_uid,id,sql_exp,description FROM
1846 ttrss_labels WHERE owner_uid = ".$_SESSION["uid"]." ORDER by description");
1847
1848 while ($line = db_fetch_assoc($result)) {
1849
1850 $id = -$line["id"] - 11;
1851
ef393de7
AD
1852 $label_name = $line["description"];
1853
a9cb1f83
AD
1854 error_reporting (0);
1855
88040f57 1856 $tmp_result = db_query($link, "SELECT count(ttrss_entries.id) as count FROM ttrss_user_entries,ttrss_entries,ttrss_feeds
a9cb1f83 1857 WHERE (" . $line["sql_exp"] . ") AND unread = true AND
cfb02131 1858 ttrss_feeds.hidden = false AND
88040f57 1859 ttrss_user_entries.feed_id = ttrss_feeds.id AND
a9cb1f83 1860 ttrss_user_entries.ref_id = ttrss_entries.id AND
88040f57 1861 ttrss_user_entries.owner_uid = ".$_SESSION["uid"]);
a9cb1f83
AD
1862
1863 $count = db_fetch_result($tmp_result, 0, "count");
1864
1865 if (!$smart_mode || $old_counters[$id] != $count) {
1866 $old_counters[$id] = $count;
1867 $lctrs_modified = true;
ef393de7
AD
1868 if (!$ret_mode) {
1869 print "<counter type=\"label\" id=\"$id\" counter=\"$count\"/>";
1870 } else {
1871 $ret_arr[$id]["counter"] = $count;
1872 $ret_arr[$id]["description"] = $label_name;
1873 }
a9cb1f83
AD
1874 }
1875
1876 error_reporting (DEFAULT_ERROR_LEVEL);
1877 }
1878
1879 if ($smart_mode && $lctrs_modified) {
1880 $_SESSION["lctr_last_value"] = $old_counters;
1881 }
ef393de7
AD
1882
1883 return $ret_arr;
a9cb1f83
AD
1884 }
1885
1886/* function getFeedCounter($link, $id) {
1887
1888 $result = db_query($link, "SELECT
1889 count(id) as count,last_error
1890 FROM ttrss_entries,ttrss_user_entries,ttrss_feeds
1891 WHERE feed_id = '$id' AND unread = true
1892 AND ttrss_user_entries.feed_id = ttrss_feeds.id
1893 AND ttrss_user_entries.ref_id = ttrss_entries.id");
1894
1895 $count = db_fetch_result($result, 0, "count");
1896 $last_error = htmlspecialchars(db_fetch_result($result, 0, "last_error"));
1897
1898 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" error=\"$last_error\"/>";
1899 } */
1900
1901 function getFeedCounters($link, $smart_mode = SMART_RPC_COUNTERS) {
1902
1903 if ($smart_mode) {
1904 if (!$_SESSION["fctr_last_value"]) {
1905 $_SESSION["fctr_last_value"] = array();
1906 }
1907 }
1908
1909 $old_counters = $_SESSION["fctr_last_value"];
1910
1911 $result = db_query($link, "SELECT id,last_error,parent_feed,
fb1fb4ab 1912 SUBSTRING(last_updated,1,19) AS last_updated,
a9cb1f83
AD
1913 (SELECT count(id)
1914 FROM ttrss_entries,ttrss_user_entries
1915 WHERE feed_id = ttrss_feeds.id AND
1916 ttrss_user_entries.ref_id = ttrss_entries.id
1917 AND unread = true AND owner_uid = ".$_SESSION["uid"].") as count
1918 FROM ttrss_feeds WHERE owner_uid = ".$_SESSION["uid"] . "
1919 AND parent_feed IS NULL");
1920
1921 $fctrs_modified = false;
1922
fb1fb4ab
AD
1923 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
1924
a9cb1f83
AD
1925 while ($line = db_fetch_assoc($result)) {
1926
1927 $id = $line["id"];
1928 $count = $line["count"];
1929 $last_error = htmlspecialchars($line["last_error"]);
fb1fb4ab
AD
1930
1931 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
1932 $last_updated = smart_date_time(strtotime($line["last_updated"]));
1933 } else {
1934 $last_updated = date($short_date, strtotime($line["last_updated"]));
1935 }
1936
a9cb1f83
AD
1937 $has_img = is_file(ICONS_DIR . "/$id.ico");
1938
1939 $tmp_result = db_query($link,
1940 "SELECT id,COUNT(unread) AS unread
1941 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
1942 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
1943 WHERE parent_feed = '$id' AND unread = true GROUP BY ttrss_feeds.id");
1944
1945 if (db_num_rows($tmp_result) > 0) {
1946 while ($l = db_fetch_assoc($tmp_result)) {
1947 $count += $l["unread"];
1948 }
1949 }
1950
1951 if (!$smart_mode || $old_counters[$id] != $count) {
1952 $old_counters[$id] = $count;
1953 $fctrs_modified = true;
1954
1955 if ($last_error) {
1956 $error_part = "error=\"$last_error\"";
1957 } else {
1958 $error_part = "";
1959 }
1960
1961 if ($has_img) {
1962 $has_img_part = "hi=\"$has_img\"";
1963 } else {
1964 $has_img_part = "";
1965 }
1966
fb1fb4ab 1967 print "<counter type=\"feed\" id=\"$id\" counter=\"$count\" $has_img_part $error_part updated=\"$last_updated\"/>";
a9cb1f83
AD
1968 }
1969 }
1970
1971 if ($smart_mode && $fctrs_modified) {
1972 $_SESSION["fctr_last_value"] = $old_counters;
1973 }
1974 }
1975
1b758780 1976 function get_script_dt_add() {
34e420fb 1977 if (strpos(VERSION, ".99") === false) {
1b758780
AD
1978 return VERSION;
1979 } else {
1980 return time();
1981 }
1982 }
1983
6e7f8d26
AD
1984 function get_pgsql_version($link) {
1985 $result = db_query($link, "SELECT version() AS version");
1986 $version = split(" ", db_fetch_result($result, 0, "version"));
1987 return $version[1];
1988 }
1989
af106b0e
AD
1990 function print_error_xml($code, $add_msg = "") {
1991 global $ERRORS;
1992
1993 $error_msg = $ERRORS[$code];
1994
1995 if ($add_msg) {
1996 $error_msg = "$error_msg; $add_msg";
1997 }
1998
4c2abbc1 1999 print "<rpc-reply>";
af106b0e 2000 print "<error error-code=\"$code\" error-msg=\"$error_msg\"/>";
4c2abbc1 2001 print "</rpc-reply>";
af106b0e 2002 }
956c7629
AD
2003
2004 function subscribe_to_feed($link, $feed_link, $cat_id = 0) {
bb0f29a4 2005
b3dfe8ba 2006 # check for feed:http://url
c91c2249
AD
2007 $feed_link = trim(preg_replace("/^feed:/", "", $feed_link));
2008
b3dfe8ba 2009 # check for feed://URL
e2d84cdb 2010 if (strpos($feed_link, "//") === 0) {
b3dfe8ba
AD
2011 $feed_link = "http:$feed_link";
2012 }
2013
c91c2249 2014 if ($feed_link == "") return;
bb0f29a4 2015
956c7629
AD
2016 if ($cat_id == "0" || !$cat_id) {
2017 $cat_qpart = "NULL";
2018 } else {
2019 $cat_qpart = "'$cat_id'";
2020 }
2021
2022 $result = db_query($link,
2023 "SELECT id FROM ttrss_feeds
2024 WHERE feed_url = '$feed_link' AND owner_uid = ".$_SESSION["uid"]);
2025
2026 if (db_num_rows($result) == 0) {
2027
2028 $result = db_query($link,
2029 "INSERT INTO ttrss_feeds (owner_uid,feed_url,title,cat_id)
2030 VALUES ('".$_SESSION["uid"]."', '$feed_link',
2031 '[Unknown]', $cat_qpart)");
2032
2033 $result = db_query($link,
2034 "SELECT id FROM ttrss_feeds WHERE feed_url = '$feed_link'
2035 AND owner_uid = " . $_SESSION["uid"]);
2036
2037 $feed_id = db_fetch_result($result, 0, "id");
2038
2039 if ($feed_id) {
2040 update_rss_feed($link, $feed_link, $feed_id, true);
2041 }
2042
2043 return true;
2044 } else {
2045 return false;
2046 }
2047 }
2048
673d54ca
AD
2049 function print_feed_select($link, $id, $default_id = "",
2050 $attributes = "", $include_all_feeds = true) {
2051
79f3553b 2052 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca 2053 if ($include_all_feeds) {
79f3553b 2054 print "<option value=\"0\">All feeds</option>";
673d54ca
AD
2055 }
2056
2057 $result = db_query($link, "SELECT id,title FROM ttrss_feeds
2058 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2059
2060 if (db_num_rows($result) > 0 && $include_all_feeds) {
2061 print "<option disabled>--------</option>";
2062 }
2063
2064 while ($line = db_fetch_assoc($result)) {
2065 if ($line["id"] == $default_id) {
2066 $is_selected = "selected";
2067 } else {
2068 $is_selected = "";
2069 }
79f3553b 2070 printf("<option $is_selected value='%d'>%s</option>",
07164479 2071 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
2072 }
2073
2074 print "</select>";
2075 }
2076
2077 function print_feed_cat_select($link, $id, $default_id = "",
2078 $attributes = "", $include_all_cats = true) {
2079
79f3553b 2080 print "<select id=\"$id\" name=\"$id\" $attributes>";
673d54ca
AD
2081
2082 if ($include_all_cats) {
d1db26aa 2083 print "<option value=\"0\">".__('Uncategorized')."</option>";
673d54ca
AD
2084 }
2085
2086 $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories
2087 WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
2088
2089 if (db_num_rows($result) > 0 && $include_all_cats) {
2090 print "<option disabled>--------</option>";
2091 }
2092
2093 while ($line = db_fetch_assoc($result)) {
2094 if ($line["id"] == $default_id) {
2095 $is_selected = "selected";
2096 } else {
2097 $is_selected = "";
2098 }
14f69488 2099 printf("<option $is_selected value='%d'>%s</option>",
07164479 2100 $line["id"], htmlspecialchars(db_unescape_string($line["title"])));
673d54ca
AD
2101 }
2102
2103 print "</select>";
2104 }
2105
14f69488
AD
2106 function checkbox_to_sql_bool($val) {
2107 return ($val == "on") ? "true" : "false";
2108 }
86b682ce
AD
2109
2110 function getFeedCatTitle($link, $id) {
2111 if ($id == -1) {
d1db26aa 2112 return __("Special");
86b682ce 2113 } else if ($id < -10) {
d1db26aa 2114 return __("Labels");
86b682ce
AD
2115 } else if ($id > 0) {
2116 $result = db_query($link, "SELECT ttrss_feed_categories.title
2117 FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
2118 cat_id = ttrss_feed_categories.id");
2119 if (db_num_rows($result) == 1) {
2120 return db_fetch_result($result, 0, "title");
2121 } else {
d1db26aa 2122 return __("Uncategorized");
86b682ce
AD
2123 }
2124 } else {
2125 return "getFeedCatTitle($id) failed";
2126 }
2127
2128 }
2129
2130 function getFeedTitle($link, $id) {
2131 if ($id == -1) {
d1db26aa 2132 return __("Starred articles");
86b682ce
AD
2133 } else if ($id < -10) {
2134 $label_id = -10 - $id;
2135 $result = db_query($link, "SELECT description FROM ttrss_labels WHERE id = '$label_id'");
2136 if (db_num_rows($result) == 1) {
2137 return db_fetch_result($result, 0, "description");
2138 } else {
2139 return "Unknown label ($label_id)";
2140 }
2141
2142 } else if ($id > 0) {
2143 $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'");
2144 if (db_num_rows($result) == 1) {
2145 return db_fetch_result($result, 0, "title");
2146 } else {
2147 return "Unknown feed ($id)";
2148 }
2149 } else {
2150 return "getFeedTitle($id) failed";
2151 }
2152
2153 }
3dd46f19
AD
2154
2155 function get_session_cookie_name() {
2156 return ((!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME);
2157 }
3ac2b520
AD
2158
2159 function print_init_params($link) {
2160 print "<init-params>";
2161 if ($_SESSION["stored-params"]) {
2162 foreach (array_keys($_SESSION["stored-params"]) as $key) {
5f57b06d
AD
2163 if ($key) {
2164 $value = htmlspecialchars($_SESSION["stored-params"][$key]);
2165 print "<param key=\"$key\" value=\"$value\"/>";
2166 }
3ac2b520
AD
2167 }
2168 }
2169
2170 print "<param key=\"daemon_enabled\" value=\"" . ENABLE_UPDATE_DAEMON . "\"/>";
2171 print "<param key=\"feeds_frame_refresh\" value=\"" . FEEDS_FRAME_REFRESH . "\"/>";
0d51e25d 2172 print "<param key=\"daemon_refresh_only\" value=\"" . DAEMON_REFRESH_ONLY . "\"/>";
3ac2b520
AD
2173
2174 print "<param key=\"on_catchup_show_next_feed\" value=\"" .
2175 get_pref($link, "ON_CATCHUP_SHOW_NEXT_FEED") . "\"/>";
2176
e8bd0da9
AD
2177 print "<param key=\"hide_read_feeds\" value=\"" .
2178 sprintf("%d", get_pref($link, "HIDE_READ_FEEDS")) . "\"/>";
2179
c9268ed5
AD
2180 print "<param key=\"feeds_sort_by_unread\" value=\"" .
2181 sprintf("%d", get_pref($link, "FEEDS_SORT_BY_UNREAD")) . "\"/>";
2182
f6d6e22f
AD
2183 print "<param key=\"confirm_feed_catchup\" value=\"" .
2184 sprintf("%d", get_pref($link, "CONFIRM_FEED_CATCHUP")) . "\"/>";
2185
ac7bcd71
AD
2186 print "<param key=\"cdm_auto_catchup\" value=\"" .
2187 sprintf("%d", get_pref($link, "CDM_AUTO_CATCHUP")) . "\"/>";
2188
3ac2b520
AD
2189 print "</init-params>";
2190 }
f54f515f
AD
2191
2192 function print_runtime_info($link) {
2193 print "<runtime-info>";
71ad883b
AD
2194 if (ENABLE_UPDATE_DAEMON) {
2195 print "<param key=\"daemon_is_running\" value=\"".
2196 sprintf("%d", file_is_locked("update_daemon.lock")) . "\"/>";
2197 }
d9fa39f1
AD
2198 if (CHECK_FOR_NEW_VERSION && $_SESSION["access_level"] >= 10) {
2199
2200 if ($_SESSION["last_version_check"] + 600 < time()) {
2201 $new_version_details = check_for_update($link);
2202
2203 print "<param key=\"new_version_available\" value=\"".
2204 sprintf("%d", $new_version_details != ""). "\"/>";
2205
2206 $_SESSION["last_version_check"] = time();
2207 }
2208 }
2209
f54f515f
AD
2210 print "</runtime-info>";
2211 }
ef393de7 2212
88040f57 2213 function getSearchSql($search, $match_on) {
ef393de7 2214
88040f57 2215 $search_query_part = "";
e20c9d88 2216
88040f57
AD
2217 $keywords = split(" ", $search);
2218 $query_keywords = array();
e20c9d88 2219
88040f57 2220 if ($match_on == "both") {
e20c9d88 2221
88040f57
AD
2222 foreach ($keywords as $k) {
2223 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%')
2224 OR UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2225 }
e20c9d88 2226
88040f57 2227 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2228
88040f57 2229 } else if ($match_on == "title") {
e20c9d88 2230
88040f57
AD
2231 foreach ($keywords as $k) {
2232 array_push($query_keywords, "(UPPER(ttrss_entries.title) LIKE UPPER('%$k%'))");
2233 }
e20c9d88 2234
88040f57 2235 $search_query_part = implode("AND", $query_keywords) . " AND ";
e20c9d88 2236
88040f57
AD
2237 } else if ($match_on == "content") {
2238
2239 foreach ($keywords as $k) {
2240 array_push($query_keywords, "(UPPER(ttrss_entries.content) LIKE UPPER('%$k%'))");
2241 }
2242 }
2243
2244 $search_query_part = implode("AND", $query_keywords);
2245
2246 return $search_query_part;
2247 }
2248
c1a0b534
AD
2249 function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0) {
2250
88040f57
AD
2251 if ($search) {
2252
2253 $search_query_part = getSearchSql($search, $match_on);
2254 $search_query_part .= " AND ";
e20c9d88 2255
ef393de7
AD
2256 } else {
2257 $search_query_part = "";
2258 }
2259
2260 $view_query_part = "";
2261
2262 if ($view_mode == "adaptive") {
2263 if ($search) {
2264 $view_query_part = " ";
2265 } else if ($feed != -1) {
f295c368 2266 $unread = getFeedUnread($link, $feed, $cat_view);
ef393de7
AD
2267 if ($unread > 0) {
2268 $view_query_part = " unread = true AND ";
2269 }
2270 }
2271 }
2272
2273 if ($view_mode == "marked") {
2274 $view_query_part = " marked = true AND ";
2275 }
2276
2277 if ($view_mode == "unread") {
2278 $view_query_part = " unread = true AND ";
2279 }
2280
2281 if ($limit > 0) {
2282 $limit_query_part = "LIMIT " . $limit;
2283 }
2284
2285 $vfeed_query_part = "";
2286
2287 // override query strategy and enable feed display when searching globally
2288 if ($search && $search_mode == "all_feeds") {
2289 $query_strategy_part = "ttrss_entries.id > 0";
2290 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2291 } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) {
2292 $query_strategy_part = "ttrss_entries.id > 0";
2293 $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE
2294 id = feed_id) as feed_title,";
2295 } else if ($feed >= 0 && $search && $search_mode == "this_cat") {
2296
2297 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
0a6c4846
AD
2298
2299 $tmp_result = false;
2300
2301 if ($cat_view) {
2302 $tmp_result = db_query($link, "SELECT id
2303 FROM ttrss_feeds WHERE cat_id = '$feed'");
2304 } else {
2305 $tmp_result = db_query($link, "SELECT id
2306 FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds
2307 WHERE id = '$feed') AND id != '$feed'");
2308 }
ef393de7
AD
2309
2310 $cat_siblings = array();
2311
2312 if (db_num_rows($tmp_result) > 0) {
2313 while ($p = db_fetch_assoc($tmp_result)) {
2314 array_push($cat_siblings, "feed_id = " . $p["id"]);
2315 }
2316
2317 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2318 $feed, implode(" OR ", $cat_siblings));
2319
2320 } else {
2321 $query_strategy_part = "ttrss_entries.id > 0";
2322 }
2323
2324 } else if ($feed >= 0) {
2325
2326 if ($cat_view) {
5c365f60 2327
ef393de7
AD
2328 if ($feed > 0) {
2329 $query_strategy_part = "cat_id = '$feed'";
2330 } else {
2331 $query_strategy_part = "cat_id IS NULL";
2332 }
2333
2334 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
5c365f60 2335
ef393de7
AD
2336 } else {
2337 $tmp_result = db_query($link, "SELECT id
2338 FROM ttrss_feeds WHERE parent_feed = '$feed'
2339 ORDER BY cat_id,title");
2340
2341 $parent_ids = array();
2342
2343 if (db_num_rows($tmp_result) > 0) {
2344 while ($p = db_fetch_assoc($tmp_result)) {
2345 array_push($parent_ids, "feed_id = " . $p["id"]);
2346 }
2347
2348 $query_strategy_part = sprintf("(feed_id = %d OR %s)",
2349 $feed, implode(" OR ", $parent_ids));
2350
2351 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2352 } else {
2353 $query_strategy_part = "feed_id = '$feed'";
2354 }
2355 }
2356 } else if ($feed == -1) { // starred virtual feed
2357 $query_strategy_part = "marked = true";
2358 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2359 } else if ($feed <= -10) { // labels
2360 $label_id = -$feed - 11;
2361
2362 $tmp_result = db_query($link, "SELECT sql_exp FROM ttrss_labels
2363 WHERE id = '$label_id'");
2364
2365 $query_strategy_part = db_fetch_result($tmp_result, 0, "sql_exp");
2366
2367 $vfeed_query_part = "ttrss_feeds.title AS feed_title,";
2368 } else {
2369 $query_strategy_part = "id > 0"; // dumb
2370 }
d6e5706d
AD
2371
2372 if (get_pref($link, 'REVERSE_HEADLINES')) {
2373 $order_by = "updated";
2374 } else {
2375 $order_by = "updated DESC";
2376 }
e939722a
AD
2377
2378 if ($override_order) {
2379 $order_by = $override_order;
2380 }
ef393de7
AD
2381
2382 $feed_title = "";
2383
2384 if ($search && $search_mode == "all_feeds") {
d1db26aa 2385 $feed_title = __("Global search results")." ($search)";
ef393de7 2386 } else if ($search && preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
d1db26aa 2387 $feed_title = __("Tag search results")." ($search, $feed)";
ef393de7
AD
2388 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) == false) {
2389 $feed_title = $feed;
2390 } else if (preg_match('/^-?[0-9][0-9]*$/', $feed) != false && $feed >= 0) {
2391
2392 if ($cat_view) {
5c365f60 2393
ef393de7
AD
2394 if ($feed != 0) {
2395 $result = db_query($link, "SELECT title FROM ttrss_feed_categories
2396 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2397 $feed_title = db_fetch_result($result, 0, "title");
2398 } else {
d1db26aa 2399 $feed_title = __("Uncategorized");
ef393de7 2400 }
e1eb2147
AD
2401
2402 if ($search) {
d1db26aa 2403 $feed_title = __("Category search results")." ($search, $feed_title)";
e1eb2147
AD
2404 }
2405
ef393de7
AD
2406 } else {
2407
2408 $result = db_query($link, "SELECT title,site_url,last_error FROM ttrss_feeds
2409 WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
2410
2411 $feed_title = db_fetch_result($result, 0, "title");
2412 $feed_site_url = db_fetch_result($result, 0, "site_url");
2413 $last_error = db_fetch_result($result, 0, "last_error");
e1eb2147
AD
2414
2415 if ($search) {
d1db26aa 2416 $feed_title = __("Feed search results") . " ($search, $feed_title)";
e1eb2147 2417 }
ef393de7
AD
2418 }
2419
2420 } else if ($feed == -1) {
d1db26aa 2421 $feed_title = __("Starred articles");
ef393de7
AD
2422 } else if ($feed < -10) {
2423 $label_id = -$feed - 11;
2424 $result = db_query($link, "SELECT description FROM ttrss_labels
2425 WHERE id = '$label_id'");
2426 $feed_title = db_fetch_result($result, 0, "description");
88040f57
AD
2427
2428 if ($search) {
d1db26aa 2429 $feed_title = __("Label search results") . " ($search, $feed_title)";
88040f57 2430 }
ef393de7
AD
2431 } else {
2432 $feed_title = "?";
2433 }
2434
2435 $feed_title = db_unescape_string($feed_title);
2436
2437 if ($feed < -10) error_reporting (0);
2438
2439 if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
2440
2441 if ($feed >= 0) {
2442 $feed_kind = "Feeds";
2443 } else {
2444 $feed_kind = "Labels";
2445 }
2446
2447 $content_query_part = "content as content_preview,";
203de776 2448
95a82c08
AD
2449 if ($limit_query_part) {
2450 $offset_query_part = "OFFSET $offset";
2451 }
2452
ef393de7 2453 $query = "SELECT
1f64b1be 2454 guid,
ef393de7
AD
2455 ttrss_entries.id,ttrss_entries.title,
2456 SUBSTRING(updated,1,16) as updated,
2457 unread,feed_id,marked,link,last_read,
2458 SUBSTRING(last_read,1,19) as last_read_noms,
2459 $vfeed_query_part
2460 $content_query_part
d4b4b9de
AD
2461 SUBSTRING(updated,1,19) as updated_noms,
2462 author
ef393de7
AD
2463 FROM
2464 ttrss_entries,ttrss_user_entries,ttrss_feeds
2465 WHERE
cfb02131 2466 ttrss_feeds.hidden = false AND
ef393de7
AD
2467 ttrss_user_entries.feed_id = ttrss_feeds.id AND
2468 ttrss_user_entries.ref_id = ttrss_entries.id AND
2469 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2470 $search_query_part
2471 $view_query_part
2472 $query_strategy_part ORDER BY $order_by
95a82c08 2473 $limit_query_part $offset_query_part";
ef393de7
AD
2474
2475 $result = db_query($link, $query);
2476
2477 if ($_GET["debug"]) print $query;
2478
2479 } else {
2480 // browsing by tag
2481
2482 $feed_kind = "Tags";
2483
2484 $result = db_query($link, "SELECT
1f64b1be 2485 guid,
ef393de7
AD
2486 ttrss_entries.id as id,title,
2487 SUBSTRING(updated,1,16) as updated,
2488 unread,feed_id,
2489 marked,link,last_read,
2490 SUBSTRING(last_read,1,19) as last_read_noms,
2491 $vfeed_query_part
2492 $content_query_part
2493 SUBSTRING(updated,1,19) as updated_noms
2494 FROM
2495 ttrss_entries,ttrss_user_entries,ttrss_tags
2496 WHERE
2497 ref_id = ttrss_entries.id AND
2498 ttrss_user_entries.owner_uid = '".$_SESSION["uid"]."' AND
2499 post_int_id = int_id AND tag_name = '$feed' AND
2500 $view_query_part
2501 $search_query_part
2502 $query_strategy_part ORDER BY $order_by
2503 $limit_query_part");
2504 }
2505
c7188969 2506 return array($result, $feed_title, $feed_site_url, $last_error);
ef393de7
AD
2507
2508 }
2509
e1eb2147 2510 function generate_syndicated_feed($link, $feed, $is_cat,
3baeeeca 2511 $search, $search_mode, $match_on) {
18664970
AD
2512
2513 $qfh_ret = queryFeedHeadlines($link, $feed,
e939722a 2514 30, false, $is_cat, $search, $search_mode, $match_on, "updated DESC");
18664970
AD
2515
2516 $result = $qfh_ret[0];
59e2aab4 2517 $feed_title = htmlspecialchars($qfh_ret[1]);
18664970
AD
2518 $feed_site_url = $qfh_ret[2];
2519 $last_error = $qfh_ret[3];
2520
3baeeeca
AD
2521 print "<rss version=\"2.0\">
2522 <channel>
2523 <title>$feed_title</title>
2524 <link>$feed_site_url</link>
2525 <generator>Tiny Tiny RSS v".VERSION."</generator>";
2526
2527 while ($line = db_fetch_assoc($result)) {
2528 print "<item>";
2529 print "<id>" . htmlspecialchars($line["guid"]) . "</id>";
2530 print "<link>" . htmlspecialchars($line["link"]) . "</link>";
2531
2532 $rfc822_date = date('r', strtotime($line["updated"]));
2533
2534 print "<pubDate>$rfc822_date</pubDate>";
2535
2536 print "<title>" .
2537 htmlspecialchars($line["title"]) . "</title>";
2538
2539 print "<description>" .
2540 htmlspecialchars($line["content_preview"]) . "</description>";
2541
2542 print "</item>";
2543 }
2544
2545 print "</channel></rss>";
18664970
AD
2546
2547 }
2548
0a6c4846
AD
2549 function getCategoryTitle($link, $cat_id) {
2550
2551 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE
2552 id = '$cat_id'");
2553
2554 if (db_num_rows($result) == 1) {
2555 return db_fetch_result($result, 0, "title");
2556 } else {
2557 return "Uncategorized";
2558 }
2559 }
2560
183ad07b 2561 function sanitize_rss($str) {
60452879 2562 $res = $str;
183ad07b
AD
2563
2564 $res = preg_replace('/<script.*?>/i',
60452879 2565 "<p class=\"scriptWarn\">Disabled script: ", $res);
183ad07b 2566
60452879
AD
2567 $res = preg_replace('/<\/script.*?>/i', "</p>", $res);
2568
8511f62f 2569/* $res = preg_replace('/<embed.*?>/i', "", $res);
183ad07b 2570
a262b161
AD
2571 $res = preg_replace('/<object.*?>.*?<\/object>/i',
2572 "<p class=\"objectWarn\">(Disabled html object
8511f62f 2573 - flash or other embedded content)</p>", $res); */
a262b161 2574
183ad07b
AD
2575 return $res;
2576 }
b72c3ef8 2577
9cd7c995
AD
2578 function send_headlines_digests($link, $limit = 100) {
2579
1ddba275
AD
2580 if (!DIGEST_ENABLE) return false;
2581
9cd7c995 2582 $user_limit = DIGEST_EMAIL_LIMIT;
5430c959 2583 $days = 1;
9cd7c995
AD
2584
2585 print "Sending digests, batch of max $user_limit users, days = $days, headline limit = $limit\n\n";
2586
2587 if (DB_TYPE == "pgsql") {
2588 $interval_query = "last_digest_sent < NOW() - INTERVAL '$days days'";
2589 } else if (DB_TYPE == "mysql") {
2590 $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL $days DAY)";
2591 }
2592
2593 $result = db_query($link, "SELECT id,email FROM ttrss_users
2594 WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)");
2595
2596 while ($line = db_fetch_assoc($result)) {
2597 if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) {
2598 print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... ";
2599
2600 $tuple = prepare_headlines_digest($link, $line["id"], $days, $limit);
2601 $digest = $tuple[0];
2602 $headlines_count = $tuple[1];
2603
2604 if ($headlines_count > 0) {
2605 $rc = mail($line["login"] . " <" . $line["email"] . ">",
2606 "[tt-rss] New headlines for last 24 hours", $digest,
3ab3c1f0
AD
2607 "From: " . MAIL_FROM . "\n".
2608 "Content-Type: text/plain; charset=\"utf-8\"\n".
2609 "Content-Transfer-Encoding: 8bit\n");
9cd7c995
AD
2610 print "RC=$rc\n";
2611 db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW()
2612 WHERE id = " . $line["id"]);
2613 } else {
2614 print "No headlines\n";
2615 }
2616 }
2617 }
2618
2619// $digest = prepare_headlines_digest($link, $user_id, $days, $limit);
2620
2621 }
2622
7e3634d9 2623 function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 100) {
d1db26aa 2624 $tmp = __("New headlines for last 24 hours, as of ") . date("Y/m/d H:m") . "\n";
7e3634d9
AD
2625 $tmp .= "=======================================================\n\n";
2626
2627 if (DB_TYPE == "pgsql") {
9cd7c995 2628 $interval_query = "ttrss_entries.date_entered > NOW() - INTERVAL '$days days'";
7e3634d9
AD
2629 } else if (DB_TYPE == "mysql") {
2630 $interval_query = "ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL $days DAY)";
2631 }
2632
2633 $result = db_query($link, "SELECT ttrss_entries.title,
2634 ttrss_feeds.title AS feed_title,
2635 date_entered,
2636 link,
2637 SUBSTRING(last_updated,1,19) AS last_updated
2638 FROM
2639 ttrss_user_entries,ttrss_entries,ttrss_feeds
2640 WHERE
2641 ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id
3dd9183c 2642 AND include_in_digest = true
7e3634d9 2643 AND $interval_query
448b0abd 2644 AND ttrss_user_entries.owner_uid = $user_id
7e3634d9
AD
2645 AND unread = true ORDER BY ttrss_feeds.title, date_entered DESC
2646 LIMIT $limit");
2647
2648 $cur_feed_title = "";
2649
9cd7c995
AD
2650 $headlines_count = db_num_rows($result);
2651
7e3634d9
AD
2652 while ($line = db_fetch_assoc($result)) {
2653 $updated = smart_date_time(strtotime($line["last_updated"]));
2654 $feed_title = $line["feed_title"];
2655
2656 if ($cur_feed_title != $feed_title) {
2657 $cur_feed_title = $feed_title;
2658
2659 $tmp .= "$feed_title\n\n";
2660 }
2661
2662 $tmp .= " * " . trim($line["title"]) . " - $updated\n";
2663 $tmp .= " " . trim($line["link"]) . "\n";
2664 $tmp .= "\n";
2665 }
2666
2667 $tmp .= "--- \n";
d1db26aa 2668 $tmp .= __("You have been sent this email because you have enabled daily digests in Tiny Tiny RSS at ") .
dfe6f833 2669 DIGEST_HOSTNAME . "\n".
d1db26aa 2670 __("To unsubscribe, visit your configuration options or contact instance owner.\n");
7e3634d9
AD
2671
2672
9cd7c995 2673 return array($tmp, $headlines_count);
7e3634d9
AD
2674 }
2675
d9fa39f1 2676 function check_for_update($link, $brief_fmt = true) {
b72c3ef8
AD
2677 $releases_feed = "http://tt-rss.spb.ru/releases.rss";
2678
2679 if (!CHECK_FOR_NEW_VERSION || $_SESSION["access_level"] < 10) {
2680 return;
2681 }
2682
2683 error_reporting(0);
2684 $rss = fetch_rss($releases_feed);
2685 error_reporting (DEFAULT_ERROR_LEVEL);
2686
2687 if ($rss) {
2688
2689 $items = $rss->items;
2690
2691 if (!$items || !is_array($items)) $items = $rss->entries;
2692 if (!$items || !is_array($items)) $items = $rss;
2693
da412ad3 2694 if (!is_array($items) || count($items) == 0) {
b72c3ef8 2695 return;
da412ad3 2696 }
b72c3ef8 2697
a41d2c65 2698 $latest_item = $items[0];
b72c3ef8 2699
a41d2c65 2700 $latest_version = trim(preg_replace("/(Milestone)|(completed)/", "", $latest_item["title"]));
b72c3ef8 2701
48e1a342
AD
2702 $release_url = sanitize_rss($latest_item["link"]);
2703 $content = sanitize_rss($latest_item["description"]);
2704
a41d2c65 2705 if (version_compare(VERSION, $latest_version) == -1) {
d9fa39f1 2706 if ($brief_fmt) {
0d32b41e 2707 return format_notice("<a href=\"javascript:showBlockElement('milestoneDetails')\">
d9fa39f1 2708 New version of Tiny-Tiny RSS ($latest_version) is available (click for details)</a>
0d32b41e 2709 <div id=\"milestoneDetails\">$content</div>");
d9fa39f1 2710 } else {
92625568
AD
2711 return "New version of Tiny-Tiny RSS ($latest_version) is available:
2712 <div class='milestoneDetails'>$content</div>
2713 Visit <a target=\"_new\" href=\"http://tt-rss.spb.ru/\">official site</a> for
2714 download and update information.";
d9fa39f1
AD
2715 }
2716
da412ad3 2717 }
b72c3ef8
AD
2718 }
2719 }
472782e8 2720
18eddb2c
AD
2721 function markArticlesById($link, $ids, $cmode) {
2722
2723 $tmp_ids = array();
2724
2725 foreach ($ids as $id) {
2726 array_push($tmp_ids, "ref_id = '$id'");
2727 }
2728
2729 $ids_qpart = join(" OR ", $tmp_ids);
2730
2731 if ($cmode == 0) {
2732 db_query($link, "UPDATE ttrss_user_entries SET
2733 marked = false,last_read = NOW()
2734 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2735 } else if ($cmode == 1) {
2736 db_query($link, "UPDATE ttrss_user_entries SET
2737 marked = true
2738 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2739 } else {
2740 db_query($link, "UPDATE ttrss_user_entries SET
2741 marked = NOT marked,last_read = NOW()
2742 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2743 }
2744 }
2745
472782e8
AD
2746 function catchupArticlesById($link, $ids, $cmode) {
2747
2748 $tmp_ids = array();
2749
2750 foreach ($ids as $id) {
2751 array_push($tmp_ids, "ref_id = '$id'");
2752 }
2753
2754 $ids_qpart = join(" OR ", $tmp_ids);
2755
2756 if ($cmode == 0) {
2757 db_query($link, "UPDATE ttrss_user_entries SET
2758 unread = false,last_read = NOW()
2759 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2760 } else if ($cmode == 1) {
2761 db_query($link, "UPDATE ttrss_user_entries SET
2762 unread = true
2763 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2764 } else {
2765 db_query($link, "UPDATE ttrss_user_entries SET
2766 unread = NOT unread,last_read = NOW()
2767 WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]);
2768 }
2769 }
2770
a262b161
AD
2771 function escape_for_form($s) {
2772 return htmlspecialchars(db_unescape_string($s));
2773 }
2774
1f64b1be
AD
2775 function make_guid_from_title($title) {
2776 return preg_replace("/[ \"\',.:;]/", "-",
2777 mb_strtolower(strip_tags($title)));
2778 }
2779
11befbb2
AD
2780 function print_headline_subtoolbar($link, $feed_site_url, $feed_title,
2781 $bottom = false, $rtl_content = false, $feed_id = 0,
2782 $is_cat = false, $search = false, $match_on = false,
95a82c08 2783 $search_mode = false, $offset = 0, $limit = 0) {
11befbb2 2784
e6c115b2
AD
2785 $user_page_offset = $offset + 1;
2786
11befbb2
AD
2787 if (!$bottom) {
2788 $class = "headlinesSubToolbar";
2789 $tid = "headlineActionsTop";
2790 } else {
2791 $class = "headlinesSubToolbar";
2792 $tid = "headlineActionsBottom";
2793 }
2794
2795 print "<table class=\"$class\" id=\"$tid\"
2796 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
2797
2798 if ($rtl_content) {
2799 $rtl_cpart = "RTL";
2800 } else {
2801 $rtl_cpart = "";
2802 }
2803
e6c115b2
AD
2804 $page_prev_link = "javascript:viewFeedGoPage(-1)";
2805 $page_next_link = "javascript:viewFeedGoPage(1)";
2806 $page_first_link = "javascript:viewFeedGoPage(0)";
203de776 2807
eb28b131
AD
2808 $catchup_page_link = "javascript:catchupPage()";
2809 $catchup_feed_link = "javascript:catchupCurrentFeed()";
c6008b62 2810
11befbb2
AD
2811 if (!get_pref($link, 'COMBINED_DISPLAY_MODE')) {
2812
c6008b62
AD
2813 $sel_all_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, '', true)";
2814 $sel_unread_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true)";
2815 $sel_none_link = "javascript:selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false)";
11befbb2 2816
c6008b62
AD
2817 $tog_unread_link = "javascript:selectionToggleUnread()";
2818 $tog_marked_link = "javascript:selectionToggleMarked()";
11befbb2 2819
c6008b62 2820 } else {
11befbb2 2821
c6008b62
AD
2822 $sel_all_link = "javascript:cdmSelectArticles('all')";
2823 $sel_unread_link = "javascript:cdmSelectArticles('unread')";
2824 $sel_none_link = "javascript:cdmSelectArticles('none')";
2825
2826 $tog_unread_link = "javascript:selectionToggleUnread(true)";
2827 $tog_marked_link = "javascript:selectionToggleMarked(true)";
2828
2829 }
2830
2dd2c13b 2831 if (!strstr($_SESSION["client.userAgent"], "MSIE")) {
c6008b62 2832
2dd2c13b
AD
2833 print "<td class=\"headlineActions$rtl_cpart\">
2834 <ul class=\"headlineDropdownMenu\">
2835 <li class=\"top2\">
3692e98f 2836 ".__('Select:')."
1025ad87
AD
2837 <a href=\"$sel_all_link\">".__('All')."</a>,
2838 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2839 <a href=\"$sel_none_link\">".__('None')."</a></li>
2dd2c13b
AD
2840 <li class=\"vsep\">&nbsp;</li>
2841 <li class=\"top\">Selection<ul>
1025ad87
AD
2842 <li onclick=\"$tog_unread_link\">".__('Toggle unread')."</li>
2843 <li onclick=\"$tog_marked_link\">".__('Toggle starred')."</li></ul></li>
2dd2c13b 2844 <li class=\"vsep\">&nbsp;</li>
1025ad87
AD
2845 <li class=\"top\"><a href=\"$catchup_page_link\">".__('Mark as read')."</a><ul>
2846 <li onclick=\"$catchup_page_link\">".__('This page')."</li>
2847 <li onclick=\"$catchup_feed_link\">".__('Entire feed')."</li></ul></li>
2848 <li class=\"vsep\">&nbsp;</li>";
95a82c08
AD
2849
2850 if ($limit != 0) {
2851 print "
1025ad87
AD
2852 <li class=\"top\"><a href=\"$page_next_link\">".__('Next page')."</a><ul>
2853 <li onclick=\"$page_prev_link\">".__('Previous page')."</li>
2854 <li onclick=\"$page_first_link\">".__('First page')."</li></ul></li>
95a82c08
AD
2855 </ul>";
2856 }
2857
2858 print "
2dd2c13b
AD
2859 </td>";
2860
2861 } else {
e6c115b2
AD
2862 // old style subtoolbar:
2863
2dd2c13b 2864 print "<td class=\"headlineActions$rtl_cpart\">".
d1db26aa 2865 __('Select:')."
1025ad87
AD
2866 <a href=\"$sel_all_link\">".__('All')."</a>,
2867 <a href=\"$sel_unread_link\">".__('Unread')."</a>,
2868 <a href=\"$sel_none_link\">".__('None')."</a>
2dd2c13b 2869 &nbsp;&nbsp;".
1025ad87
AD
2870 __('Toggle:')." <a href=\"$tog_unread_link\">".__('Unread')."</a>,
2871 <a href=\"$tog_marked_link\">".__('Starred')."</a>
2dd2c13b 2872 &nbsp;&nbsp;".
d1db26aa 2873 __('Mark as read:')."
1025ad87
AD
2874 <a href=\"#\" onclick=\"$catchup_page_link\">".__('Page')."</a>,
2875 <a href=\"#\" onclick=\"$catchup_feed_link\">".__('Feed')."</a>";
2dd2c13b
AD
2876 print "</td>";
2877
2878 }
c6008b62
AD
2879
2880 if ($search && $feed_id >= 0 && get_pref($link, 'ENABLE_LABELS') && GLOBAL_ENABLE_LABELS) {
2881 print "<td class=\"headlineActions$rtl_cpart\">
2882 <a href=\"javascript:labelFromSearch('$search', '$search_mode',
2883 '$match_on', '$feed_id', '$is_cat');\">
d1db26aa 2884 ".__('Convert to Label')."</a></td>";
11befbb2
AD
2885 }
2886
2887 print "<td class=\"headlineTitle$rtl_cpart\">";
2888
2889 if ($feed_site_url) {
2890 if (!$bottom) {
2891 $target = "target=\"_blank\"";
2892 }
2893 print "<a $target href=\"$feed_site_url\">$feed_title</a>";
2894 } else {
2895 print $feed_title;
2896 }
2897
2898 if ($search) {
2899 $search_q = "&q=$search&m=$match_on&smode=$search_mode";
2900 }
2901
e6c115b2
AD
2902 if ($user_page_offset > 1) {
2903 print " [$user_page_offset] ";
2904 }
2905
11befbb2 2906 if (!$bottom) {
e6c115b2 2907 print "
11befbb2
AD
2908 <a target=\"_new\"
2909 href=\"backend.php?op=rss&id=$feed_id&is_cat=$is_cat$search_q\">
2910 <img class=\"noborder\"
1025ad87 2911 alt=\"".__('Generated feed')."\" src=\"images/feed-icon-12x12.png\">
11befbb2
AD
2912 </a>";
2913 }
2914
2915 print "</td>";
2916 print "</tr></table>";
2917
2918 }
2919
f407c086
AD
2920 function outputFeedList($link, $tags = false) {
2921
2922 print "<ul class=\"feedList\" id=\"feedList\">\n";
2923
2924 $owner_uid = $_SESSION["uid"];
2925
cf4d339c 2926 /* virtual feeds */
f407c086 2927
cf4d339c 2928 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 2929 print "<li class=\"feedCat\">".__('Special')."</li>";
cf4d339c
AD
2930 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2931 }
f407c086 2932
cf4d339c 2933 $num_starred = getFeedUnread($link, -1);
f407c086 2934
cf4d339c 2935 $class = "virt";
f407c086 2936
cf4d339c 2937 if ($num_starred > 0) $class .= "Unread";
f407c086 2938
d1db26aa 2939 printFeedEntry(-1, $class, __("Starred articles"), $num_starred,
cf4d339c 2940 "images/mark_set.png", $link);
f407c086 2941
cf4d339c
AD
2942 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2943 print "</ul>\n";
2944 }
f407c086 2945
cf4d339c 2946 if (!$tags) {
f407c086
AD
2947
2948 if (GLOBAL_ENABLE_LABELS && get_pref($link, 'ENABLE_LABELS')) {
2949
2950 $result = db_query($link, "SELECT id,sql_exp,description FROM
2951 ttrss_labels WHERE owner_uid = '$owner_uid' ORDER by description");
2952
2953 if (db_num_rows($result) > 0) {
2954 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 2955 print "<li class=\"feedCat\">".__('Labels')."</li>";
f407c086
AD
2956 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
2957 } else {
2958 print "<li><hr></li>";
2959 }
2960 }
2961
2962 while ($line = db_fetch_assoc($result)) {
2963
2964 error_reporting (0);
2965
2966 $label_id = -$line['id'] - 11;
2967 $count = getFeedUnread($link, $label_id);
2968
2969 $class = "label";
2970
2971 if ($count > 0) {
2972 $class .= "Unread";
2973 }
2974
2975 error_reporting (DEFAULT_ERROR_LEVEL);
2976
2977 printFeedEntry($label_id,
2978 $class, db_unescape_string($line["description"]),
2979 $count, "images/label.png", $link);
2980
2981 }
2982
2983 if (db_num_rows($result) > 0) {
2984 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2985 print "</ul>";
2986 }
2987 }
2988
2989 }
2990
2991 if (!get_pref($link, 'ENABLE_FEED_CATS')) {
2992 print "<li><hr></li>";
2993 }
2994
2995 if (get_pref($link, 'ENABLE_FEED_CATS')) {
2996 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
2997 $order_by_qpart = "category,unread DESC,title";
2998 } else {
2999 $order_by_qpart = "category,title";
3000 }
3001 } else {
3002 if (get_pref($link, "FEEDS_SORT_BY_UNREAD")) {
3003 $order_by_qpart = "unread DESC,title";
3004 } else {
3005 $order_by_qpart = "title";
3006 }
3007 }
3008
3009 $result = db_query($link, "SELECT ttrss_feeds.*,
3010 SUBSTRING(last_updated,1,19) AS last_updated_noms,
3011 (SELECT COUNT(id) FROM ttrss_entries,ttrss_user_entries
3012 WHERE feed_id = ttrss_feeds.id AND unread = true
3013 AND ttrss_user_entries.ref_id = ttrss_entries.id
3014 AND owner_uid = '$owner_uid') as unread,
3015 cat_id,last_error,
3016 ttrss_feed_categories.title AS category,
3017 ttrss_feed_categories.collapsed
3018 FROM ttrss_feeds LEFT JOIN ttrss_feed_categories
3019 ON (ttrss_feed_categories.id = cat_id)
3020 WHERE
3021 ttrss_feeds.hidden = false AND
3022 ttrss_feeds.owner_uid = '$owner_uid' AND parent_feed IS NULL
3023 ORDER BY $order_by_qpart");
3024
3025 $actid = $_GET["actid"];
3026
3027 /* real feeds */
3028
3029 $lnum = 0;
3030
3031 $total_unread = 0;
3032
3033 $category = "";
3034
3035 $short_date = get_pref($link, 'SHORT_DATE_FORMAT');
3036
3037 while ($line = db_fetch_assoc($result)) {
3038
3039 $feed = db_unescape_string($line["title"]);
3040 $feed_id = $line["id"];
3041
3042 $subop = $_GET["subop"];
3043
3044 $unread = $line["unread"];
3045
3046 if (get_pref($link, 'HEADLINES_SMART_DATE')) {
3047 $last_updated = smart_date_time(strtotime($line["last_updated_noms"]));
3048 } else {
3049 $last_updated = date($short_date, strtotime($line["last_updated_noms"]));
3050 }
3051
3052 $rtl_content = sql_bool_to_bool($line["rtl_content"]);
3053
3054 if ($rtl_content) {
3055 $rtl_tag = "dir=\"RTL\"";
3056 } else {
3057 $rtl_tag = "";
3058 }
3059
3060 $tmp_result = db_query($link,
3061 "SELECT id,COUNT(unread) AS unread
3062 FROM ttrss_feeds LEFT JOIN ttrss_user_entries
3063 ON (ttrss_feeds.id = ttrss_user_entries.feed_id)
3064 WHERE parent_feed = '$feed_id' AND unread = true
3065 GROUP BY ttrss_feeds.id");
3066
3067 if (db_num_rows($tmp_result) > 0) {
3068 while ($l = db_fetch_assoc($tmp_result)) {
3069 $unread += $l["unread"];
3070 }
3071 }
3072
3073 $cat_id = $line["cat_id"];
3074
3075 $tmp_category = $line["category"];
3076
3077 if (!$tmp_category) {
d1db26aa 3078 $tmp_category = __("Uncategorized");
f407c086
AD
3079 }
3080
3081 // $class = ($lnum % 2) ? "even" : "odd";
3082
3083 if ($line["last_error"]) {
3084 $class = "error";
3085 } else {
3086 $class = "feed";
3087 }
3088
3089 if ($unread > 0) $class .= "Unread";
3090
3091 if ($actid == $feed_id) {
3092 $class .= "Selected";
3093 }
3094
3095 $total_unread += $unread;
3096
3097 if ($category != $tmp_category && get_pref($link, 'ENABLE_FEED_CATS')) {
3098
3099 if ($category) {
3100 print "</ul></li>";
3101 }
3102
3103 $category = $tmp_category;
3104
3105 $collapsed = $line["collapsed"];
3106
3107 // workaround for NULL category
d1db26aa 3108 if ($category == __("Uncategorized")) {
f407c086
AD
3109 if ($_COOKIE["ttrss_vf_uclps"] == 1) {
3110 $collapsed = "t";
3111 }
3112 }
3113
3114 if ($collapsed == "t" || $collapsed == "1") {
3115 $holder_class = "invisible";
3116 $ellipsis = "...";
3117 } else {
3118 $holder_class = "";
3119 $ellipsis = "";
3120 }
3121
3122 $cat_id = sprintf("%d", $cat_id);
3123
3124 $cat_unread = getCategoryUnread($link, $cat_id);
67dabe1a
AD
3125
3126 $catctr_class = ($cat_unread > 0) ? "catCtrHasUnread" : "catCtrNoUnread";
3127
f407c086 3128 print "<li class=\"feedCat\" id=\"FCAT-$cat_id\">
439bbe63 3129 <a id=\"FCATN-$cat_id\" href=\"javascript:toggleCollapseCat($cat_id)\">$tmp_category</a>
7210613a 3130 <a href=\"#\" onclick=\"javascript:viewCategory($cat_id)\" id=\"FCAP-$cat_id\">
67dabe1a
AD
3131 <span id=\"FCATCTR-$cat_id\" title=\"Click to browse category\"
3132 class=\"$catctr_class\">($cat_unread)</span> $ellipsis
f407c086
AD
3133 </a></li>";
3134
3135 // !!! NO SPACE before <ul...feedCatList - breaks firstChild DOM function
3136 // -> keyboard navigation, etc.
3137 print "<li id=\"feedCatHolder\" class=\"$holder_class\"><ul class=\"feedCatList\" id=\"FCATLIST-$cat_id\">";
3138 }
3139
3140 printFeedEntry($feed_id, $class, $feed, $unread,
99e37e09 3141 ICONS_DIR."/$feed_id.ico", $link, $rtl_content,
f407c086
AD
3142 $last_updated, $line["last_error"]);
3143
3144 ++$lnum;
3145 }
3146
3147 if (db_num_rows($result) == 0) {
d1db26aa 3148 print "<li>".__('No feeds to display.')."</li>";
f407c086
AD
3149 }
3150
3151 } else {
3152
3153 // tags
3154
3155/* $result = db_query($link, "SELECT tag_name,count(ttrss_entries.id) AS count
3156 FROM ttrss_tags,ttrss_entries,ttrss_user_entries WHERE
3157 post_int_id = ttrss_user_entries.int_id AND
3158 unread = true AND ref_id = ttrss_entries.id
3159 AND ttrss_tags.owner_uid = '$owner_uid' GROUP BY tag_name
3160 UNION
3161 select tag_name,0 as count FROM ttrss_tags WHERE owner_uid = '$owner_uid'
3162 ORDER BY tag_name"); */
3163
3164 if (get_pref($link, 'ENABLE_FEED_CATS')) {
d1db26aa 3165 print "<li class=\"feedCat\">".__('Tags')."</li>";
f407c086
AD
3166 print "<li id=\"feedCatHolder\"><ul class=\"feedCatList\">";
3167 }
3168
3169 $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id)
3170 FROM ttrss_user_entries WHERE int_id = post_int_id
3171 AND unread = true)) AS count FROM ttrss_tags
22e00732 3172 WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name ORDER BY tag_name");
f407c086
AD
3173
3174 $tags = array();
3175
3176 while ($line = db_fetch_assoc($result)) {
3177 $tags[$line["tag_name"]] += $line["count"];
3178 }
3179
3180 foreach (array_keys($tags) as $tag) {
3181
3182 $unread = $tags[$tag];
3183
3184 $class = "tag";
3185
3186 if ($unread > 0) {
3187 $class .= "Unread";
3188 }
3189
3190 printFeedEntry($tag, $class, $tag, $unread, "images/tag.png", $link);
3191
3192 }
3193
3194 if (db_num_rows($result) == 0) {
3195 print "<li>No tags to display.</li>";
3196 }
3197
3198 if (get_pref($link, 'ENABLE_FEED_CATS')) {
3199 print "</ul>\n";
3200 }
3201
3202 }
3203
3204 print "</ul>";
3205
3206 }
3207
0b126ac2
AD
3208 function get_article_tags($link, $id) {
3209
3210 $a_id = db_escape_string($id);
3211
3212 $tmp_result = db_query($link, "SELECT DISTINCT tag_name FROM
3213 ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE
93135102 3214 ref_id = '$a_id' AND owner_uid = '".$_SESSION["uid"]."' LIMIT 1) ORDER BY tag_name");
0b126ac2
AD
3215
3216 $tags = array();
3217
3218 while ($tmp_line = db_fetch_assoc($tmp_result)) {
3219 array_push($tags, $tmp_line["tag_name"]);
3220 }
3221
3222 return $tags;
3223 }
3224
d62a3b63
AD
3225 function trim_value(&$value) {
3226 $value = trim($value);
3227 }
3228
3229 function trim_array($array) {
3230 $tmp = $array;
3231 array_walk($tmp, 'trim_value');
3232 return $tmp;
3233 }
3234
be832a1a 3235 function tag_is_valid($tag) {
ef063748
AD
3236 if ($tag == '') return false;
3237 if (preg_match("/^[0-9]*$/", $tag)) return false;
3238
3239 $tag = iconv("utf-8", "utf-8", $tag);
3240 if (!$tag) return false;
3241
3242 return true;
be832a1a
AD
3243 }
3244
793185a9
AD
3245 function render_login_form($link, $mobile = false) {
3246 if (!$mobile) {
3247 require_once "login_form.php";
3248 } else {
3249 require_once "mobile/login_form.php";
3250 }
01a87dff
AD
3251 }
3252
dc56b3b7
AD
3253 // from http://developer.apple.com/internet/safari/faq.html
3254 function no_cache_incantation() {
3255 header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :)
3256 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
3257 header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
3258 header("Cache-Control: post-check=0, pre-check=0", false);
3259 header("Pragma: no-cache"); // HTTP/1.0
3260 }
3261
42395d28
AD
3262 function format_warning($msg, $id = "") {
3263 return "<div class=\"warning\" id=\"$id\">
0d32b41e
AD
3264 <img src=\"images/sign_excl.png\">$msg</div>";
3265 }
3266
3267 function format_notice($msg) {
3268 return "<div class=\"notice\">
3269 <img src=\"images/sign_info.png\">$msg</div>";
3270 }
3271
4dccf1ed
AD
3272 function print_notice($msg) {
3273 return print format_notice($msg);
3274 }
3275
3276 function print_warning($msg) {
3277 return print format_warning($msg);
3278 }
3279
4dccf1ed
AD
3280 function T_sprintf() {
3281 $args = func_get_args();
3282 return vsprintf(__(array_shift($args)), $args);
3283 }
3284
40d13c28 3285?>