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