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