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