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