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