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