]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | date_default_timezone_set('UTC'); | |
3 | if (defined('E_DEPRECATED')) { | |
4 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); | |
5 | } else { | |
6 | error_reporting(E_ALL & ~E_NOTICE); | |
7 | } | |
8 | ||
9 | require_once 'config.php'; | |
10 | ||
11 | if (DB_TYPE == "pgsql") { | |
12 | define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE'); | |
13 | } else { | |
14 | define('SUBSTRING_FOR_DATE', 'SUBSTRING'); | |
15 | } | |
16 | ||
17 | define('THEME_VERSION_REQUIRED', 1.1); | |
18 | ||
19 | /** | |
20 | * Return available translations names. | |
21 | * | |
22 | * @access public | |
23 | * @return array A array of available translations. | |
24 | */ | |
25 | function get_translations() { | |
26 | $tr = array( | |
27 | "auto" => "Detect automatically", | |
28 | "ca_CA" => "Català", | |
29 | "en_US" => "English", | |
30 | "es_ES" => "Español", | |
31 | "de_DE" => "Deutsch", | |
32 | "fr_FR" => "Français", | |
33 | "hu_HU" => "Magyar (Hungarian)", | |
34 | "it_IT" => "Italiano", | |
35 | "ja_JP" => "日本語 (Japanese)", | |
36 | "nb_NO" => "Norwegian bokmål", | |
37 | "ru_RU" => "Русский", | |
38 | "pt_BR" => "Portuguese/Brazil", | |
39 | "zh_CN" => "Simplified Chinese"); | |
40 | ||
41 | return $tr; | |
42 | } | |
43 | ||
44 | require_once "lib/accept-to-gettext.php"; | |
45 | require_once "lib/gettext/gettext.inc"; | |
46 | ||
47 | function startup_gettext() { | |
48 | ||
49 | # Get locale from Accept-Language header | |
50 | $lang = al2gt(array_keys(get_translations()), "text/html"); | |
51 | ||
52 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { | |
53 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
54 | } | |
55 | ||
56 | if ($_COOKIE["ttrss_lang"] && $_COOKIE["ttrss_lang"] != "auto") { | |
57 | $lang = $_COOKIE["ttrss_lang"]; | |
58 | } | |
59 | ||
60 | /* In login action of mobile version */ | |
61 | if ($_POST["language"] && defined('MOBILE_VERSION')) { | |
62 | $lang = $_POST["language"]; | |
63 | $_COOKIE["ttrss_lang"] = $lang; | |
64 | } | |
65 | ||
66 | if ($lang) { | |
67 | if (defined('LC_MESSAGES')) { | |
68 | _setlocale(LC_MESSAGES, $lang); | |
69 | } else if (defined('LC_ALL')) { | |
70 | _setlocale(LC_ALL, $lang); | |
71 | } | |
72 | ||
73 | if (defined('MOBILE_VERSION')) { | |
74 | _bindtextdomain("messages", "../locale"); | |
75 | } else { | |
76 | _bindtextdomain("messages", "locale"); | |
77 | } | |
78 | ||
79 | _textdomain("messages"); | |
80 | _bind_textdomain_codeset("messages", "UTF-8"); | |
81 | } | |
82 | } | |
83 | ||
84 | startup_gettext(); | |
85 | ||
86 | if (defined('MEMCACHE_SERVER')) { | |
87 | $memcache = new Memcache; | |
88 | $memcache->connect(MEMCACHE_SERVER, 11211); | |
89 | } | |
90 | ||
91 | require_once 'db-prefs.php'; | |
92 | require_once 'version.php'; | |
93 | ||
94 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); | |
95 | ||
96 | define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)'); | |
97 | define('MAGPIE_USER_AGENT', SELF_USER_AGENT); | |
98 | ||
99 | ini_set('user_agent', SELF_USER_AGENT); | |
100 | ||
101 | require_once 'lib/pubsubhubbub/publisher.php'; | |
102 | ||
103 | $purifier = false; | |
104 | ||
105 | $tz_offset = -1; | |
106 | $utc_tz = new DateTimeZone('UTC'); | |
107 | $schema_version = false; | |
108 | ||
109 | /** | |
110 | * Print a timestamped debug message. | |
111 | * | |
112 | * @param string $msg The debug message. | |
113 | * @return void | |
114 | */ | |
115 | function _debug($msg) { | |
116 | $ts = strftime("%H:%M:%S", time()); | |
117 | if (function_exists('posix_getpid')) { | |
118 | $ts = "$ts/" . posix_getpid(); | |
119 | } | |
120 | print "[$ts] $msg\n"; | |
121 | } // function _debug | |
122 | ||
123 | /** | |
124 | * Purge a feed old posts. | |
125 | * | |
126 | * @param mixed $link A database connection. | |
127 | * @param mixed $feed_id The id of the purged feed. | |
128 | * @param mixed $purge_interval Olderness of purged posts. | |
129 | * @param boolean $debug Set to True to enable the debug. False by default. | |
130 | * @access public | |
131 | * @return void | |
132 | */ | |
133 | function purge_feed($link, $feed_id, $purge_interval, $debug = false) { | |
134 | ||
135 | if (!$purge_interval) $purge_interval = feed_purge_interval($link, $feed_id); | |
136 | ||
137 | $rows = -1; | |
138 | ||
139 | $result = db_query($link, | |
140 | "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'"); | |
141 | ||
142 | $owner_uid = false; | |
143 | ||
144 | if (db_num_rows($result) == 1) { | |
145 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
146 | } | |
147 | ||
148 | if ($purge_interval == -1 || !$purge_interval) { | |
149 | if ($owner_uid) { | |
150 | ccache_update($link, $feed_id, $owner_uid); | |
151 | } | |
152 | return; | |
153 | } | |
154 | ||
155 | if (!$owner_uid) return; | |
156 | ||
157 | if (FORCE_ARTICLE_PURGE == 0) { | |
158 | $purge_unread = get_pref($link, "PURGE_UNREAD_ARTICLES", | |
159 | $owner_uid, false); | |
160 | } else { | |
161 | $purge_unread = true; | |
162 | $purge_interval = FORCE_ARTICLE_PURGE; | |
163 | } | |
164 | ||
165 | if (!$purge_unread) $query_limit = " unread = false AND "; | |
166 | ||
167 | if (DB_TYPE == "pgsql") { | |
168 | $pg_version = get_pgsql_version($link); | |
169 | ||
170 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
171 | ||
172 | $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
173 | ttrss_entries.id = ref_id AND | |
174 | marked = false AND | |
175 | feed_id = '$feed_id' AND | |
176 | $query_limit | |
177 | ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); | |
178 | ||
179 | } else { | |
180 | ||
181 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
182 | USING ttrss_entries | |
183 | WHERE ttrss_entries.id = ref_id AND | |
184 | marked = false AND | |
185 | feed_id = '$feed_id' AND | |
186 | $query_limit | |
187 | ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); | |
188 | } | |
189 | ||
190 | $rows = pg_affected_rows($result); | |
191 | ||
192 | } else { | |
193 | ||
194 | /* $result = db_query($link, "DELETE FROM ttrss_user_entries WHERE | |
195 | marked = false AND feed_id = '$feed_id' AND | |
196 | (SELECT date_updated FROM ttrss_entries WHERE | |
197 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ | |
198 | ||
199 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
200 | USING ttrss_user_entries, ttrss_entries | |
201 | WHERE ttrss_entries.id = ref_id AND | |
202 | marked = false AND | |
203 | feed_id = '$feed_id' AND | |
204 | $query_limit | |
205 | ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
206 | ||
207 | $rows = mysql_affected_rows($link); | |
208 | ||
209 | } | |
210 | ||
211 | ccache_update($link, $feed_id, $owner_uid); | |
212 | ||
213 | if ($debug) { | |
214 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); | |
215 | } | |
216 | } // function purge_feed | |
217 | ||
218 | function feed_purge_interval($link, $feed_id) { | |
219 | ||
220 | $result = db_query($link, "SELECT purge_interval, owner_uid FROM ttrss_feeds | |
221 | WHERE id = '$feed_id'"); | |
222 | ||
223 | if (db_num_rows($result) == 1) { | |
224 | $purge_interval = db_fetch_result($result, 0, "purge_interval"); | |
225 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
226 | ||
227 | if ($purge_interval == 0) $purge_interval = get_pref($link, | |
228 | 'PURGE_OLD_DAYS', $owner_uid); | |
229 | ||
230 | return $purge_interval; | |
231 | ||
232 | } else { | |
233 | return -1; | |
234 | } | |
235 | } | |
236 | ||
237 | function purge_orphans($link, $do_output = false) { | |
238 | ||
239 | // purge orphaned posts in main content table | |
240 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
241 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
242 | ||
243 | if ($do_output) { | |
244 | $rows = db_affected_rows($link, $result); | |
245 | _debug("Purged $rows orphaned posts."); | |
246 | } | |
247 | } | |
248 | ||
249 | function get_feed_update_interval($link, $feed_id) { | |
250 | $result = db_query($link, "SELECT owner_uid, update_interval FROM | |
251 | ttrss_feeds WHERE id = '$feed_id'"); | |
252 | ||
253 | if (db_num_rows($result) == 1) { | |
254 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
255 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
256 | ||
257 | if ($update_interval != 0) { | |
258 | return $update_interval; | |
259 | } else { | |
260 | return get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $owner_uid, false); | |
261 | } | |
262 | ||
263 | } else { | |
264 | return -1; | |
265 | } | |
266 | } | |
267 | ||
268 | function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false) { | |
269 | $login = urlencode($login); | |
270 | $pass = urlencode($pass); | |
271 | ||
272 | if (function_exists('curl_init') && !ini_get("open_basedir")) { | |
273 | $ch = curl_init($url); | |
274 | ||
275 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); | |
276 | curl_setopt($ch, CURLOPT_TIMEOUT, 45); | |
277 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
278 | curl_setopt($ch, CURLOPT_MAXREDIRS, 20); | |
279 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); | |
280 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
281 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
282 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
283 | curl_setopt($ch, CURLOPT_USERAGENT, SELF_USER_AGENT); | |
284 | curl_setopt($ch, CURLOPT_ENCODING , "gzip"); | |
285 | ||
286 | if ($post_query) { | |
287 | curl_setopt($ch, CURLOPT_POST, true); | |
288 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query); | |
289 | } | |
290 | ||
291 | if ($login && $pass) | |
292 | curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass"); | |
293 | ||
294 | $contents = @curl_exec($ch); | |
295 | ||
296 | if ($contents === false) { | |
297 | curl_close($ch); | |
298 | return false; | |
299 | } | |
300 | ||
301 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
302 | $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
303 | curl_close($ch); | |
304 | ||
305 | if ($http_code != 200 || $type && strpos($content_type, "$type") === false) { | |
306 | return false; | |
307 | } | |
308 | ||
309 | return $contents; | |
310 | } else { | |
311 | if ($login && $pass ){ | |
312 | $url_parts = array(); | |
313 | ||
314 | preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts); | |
315 | ||
316 | if ($url_parts[1] && $url_parts[2]) { | |
317 | $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2]; | |
318 | } | |
319 | } | |
320 | ||
321 | return @file_get_contents($url); | |
322 | } | |
323 | ||
324 | } | |
325 | ||
326 | /** | |
327 | * Try to determine the favicon URL for a feed. | |
328 | * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
329 | * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
330 | * | |
331 | * @param string $url A feed or page URL | |
332 | * @access public | |
333 | * @return mixed The favicon URL, or false if none was found. | |
334 | */ | |
335 | function get_favicon_url($url) { | |
336 | ||
337 | $favicon_url = false; | |
338 | ||
339 | if ($html = @fetch_file_contents($url)) { | |
340 | ||
341 | libxml_use_internal_errors(true); | |
342 | ||
343 | $doc = new DOMDocument(); | |
344 | $doc->loadHTML($html); | |
345 | $xpath = new DOMXPath($doc); | |
346 | ||
347 | $base = $xpath->query('/html/head/base'); | |
348 | foreach ($base as $b) { | |
349 | $url = $b->getAttribute("href"); | |
350 | break; | |
351 | } | |
352 | ||
353 | $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]'); | |
354 | if (count($entries) > 0) { | |
355 | foreach ($entries as $entry) { | |
356 | $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href")); | |
357 | break; | |
358 | } | |
359 | } | |
360 | } | |
361 | ||
362 | if (!$favicon_url) | |
363 | $favicon_url = rewrite_relative_url($url, "/favicon.ico"); | |
364 | ||
365 | return $favicon_url; | |
366 | } // function get_favicon_url | |
367 | ||
368 | function check_feed_favicon($site_url, $feed, $link) { | |
369 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
370 | ||
371 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
372 | ||
373 | if (!file_exists($icon_file)) { | |
374 | $favicon_url = get_favicon_url($site_url); | |
375 | ||
376 | if ($favicon_url) { | |
377 | // Limiting to "image" type misses those served with text/plain | |
378 | $contents = fetch_file_contents($favicon_url); // , "image"); | |
379 | ||
380 | if ($contents) { | |
381 | // Crude image type matching. | |
382 | // Patterns gleaned from the file(1) source code. | |
383 | if (preg_match('/^\x00\x00\x01\x00/', $contents)) { | |
384 | // 0 string \000\000\001\000 MS Windows icon resource | |
385 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); | |
386 | } | |
387 | elseif (preg_match('/^GIF8/', $contents)) { | |
388 | // 0 string GIF8 GIF image data | |
389 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); | |
390 | } | |
391 | elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { | |
392 | // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data | |
393 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); | |
394 | } | |
395 | elseif (preg_match('/^\xff\xd8/', $contents)) { | |
396 | // 0 beshort 0xffd8 JPEG image data | |
397 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); | |
398 | } | |
399 | else { | |
400 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); | |
401 | $contents = ""; | |
402 | } | |
403 | } | |
404 | ||
405 | if ($contents) { | |
406 | $fp = @fopen($icon_file, "w"); | |
407 | ||
408 | if ($fp) { | |
409 | fwrite($fp, $contents); | |
410 | fclose($fp); | |
411 | chmod($icon_file, 0644); | |
412 | } | |
413 | } | |
414 | } | |
415 | } | |
416 | } | |
417 | ||
418 | function print_select($id, $default, $values, $attributes = "") { | |
419 | print "<select name=\"$id\" id=\"$id\" $attributes>"; | |
420 | foreach ($values as $v) { | |
421 | if ($v == $default) | |
422 | $sel = "selected=\"1\""; | |
423 | else | |
424 | $sel = ""; | |
425 | ||
426 | print "<option value=\"$v\" $sel>$v</option>"; | |
427 | } | |
428 | print "</select>"; | |
429 | } | |
430 | ||
431 | function print_select_hash($id, $default, $values, $attributes = "") { | |
432 | print "<select name=\"$id\" id='$id' $attributes>"; | |
433 | foreach (array_keys($values) as $v) { | |
434 | if ($v == $default) | |
435 | $sel = 'selected="selected"'; | |
436 | else | |
437 | $sel = ""; | |
438 | ||
439 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; | |
440 | } | |
441 | ||
442 | print "</select>"; | |
443 | } | |
444 | ||
445 | function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) { | |
446 | $matches = array(); | |
447 | ||
448 | if ($filters["title"]) { | |
449 | foreach ($filters["title"] as $filter) { | |
450 | $reg_exp = $filter["reg_exp"]; | |
451 | $inverse = $filter["inverse"]; | |
452 | if ((!$inverse && @preg_match("/$reg_exp/i", $title)) || | |
453 | ($inverse && !@preg_match("/$reg_exp/i", $title))) { | |
454 | ||
455 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
460 | if ($filters["content"]) { | |
461 | foreach ($filters["content"] as $filter) { | |
462 | $reg_exp = $filter["reg_exp"]; | |
463 | $inverse = $filter["inverse"]; | |
464 | ||
465 | if ((!$inverse && @preg_match("/$reg_exp/i", $content)) || | |
466 | ($inverse && !@preg_match("/$reg_exp/i", $content))) { | |
467 | ||
468 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | if ($filters["both"]) { | |
474 | foreach ($filters["both"] as $filter) { | |
475 | $reg_exp = $filter["reg_exp"]; | |
476 | $inverse = $filter["inverse"]; | |
477 | ||
478 | if ($inverse) { | |
479 | if (!@preg_match("/$reg_exp/i", $title) && !preg_match("/$reg_exp/i", $content)) { | |
480 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
481 | } | |
482 | } else { | |
483 | if (@preg_match("/$reg_exp/i", $title) || preg_match("/$reg_exp/i", $content)) { | |
484 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
485 | } | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
490 | if ($filters["link"]) { | |
491 | $reg_exp = $filter["reg_exp"]; | |
492 | foreach ($filters["link"] as $filter) { | |
493 | $reg_exp = $filter["reg_exp"]; | |
494 | $inverse = $filter["inverse"]; | |
495 | ||
496 | if ((!$inverse && @preg_match("/$reg_exp/i", $link)) || | |
497 | ($inverse && !@preg_match("/$reg_exp/i", $link))) { | |
498 | ||
499 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
500 | } | |
501 | } | |
502 | } | |
503 | ||
504 | if ($filters["date"]) { | |
505 | $reg_exp = $filter["reg_exp"]; | |
506 | foreach ($filters["date"] as $filter) { | |
507 | $date_modifier = $filter["filter_param"]; | |
508 | $inverse = $filter["inverse"]; | |
509 | $check_timestamp = strtotime($filter["reg_exp"]); | |
510 | ||
511 | # no-op when timestamp doesn't parse to prevent misfires | |
512 | ||
513 | if ($check_timestamp) { | |
514 | $match_ok = false; | |
515 | ||
516 | if ($date_modifier == "before" && $timestamp < $check_timestamp || | |
517 | $date_modifier == "after" && $timestamp > $check_timestamp) { | |
518 | $match_ok = true; | |
519 | } | |
520 | ||
521 | if ($inverse) $match_ok = !$match_ok; | |
522 | ||
523 | if ($match_ok) { | |
524 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
525 | } | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | if ($filters["author"]) { | |
531 | foreach ($filters["author"] as $filter) { | |
532 | $reg_exp = $filter["reg_exp"]; | |
533 | $inverse = $filter["inverse"]; | |
534 | if ((!$inverse && @preg_match("/$reg_exp/i", $author)) || | |
535 | ($inverse && !@preg_match("/$reg_exp/i", $author))) { | |
536 | ||
537 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
538 | } | |
539 | } | |
540 | } | |
541 | ||
542 | if ($filters["tag"]) { | |
543 | ||
544 | $tag_string = join(",", $tags); | |
545 | ||
546 | foreach ($filters["tag"] as $filter) { | |
547 | $reg_exp = $filter["reg_exp"]; | |
548 | $inverse = $filter["inverse"]; | |
549 | ||
550 | if ((!$inverse && @preg_match("/$reg_exp/i", $tag_string)) || | |
551 | ($inverse && !@preg_match("/$reg_exp/i", $tag_string))) { | |
552 | ||
553 | array_push($matches, array($filter["action"], $filter["action_param"])); | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
558 | ||
559 | return $matches; | |
560 | } | |
561 | ||
562 | function find_article_filter($filters, $filter_name) { | |
563 | foreach ($filters as $f) { | |
564 | if ($f[0] == $filter_name) { | |
565 | return $f; | |
566 | }; | |
567 | } | |
568 | return false; | |
569 | } | |
570 | ||
571 | function calculate_article_score($filters) { | |
572 | $score = 0; | |
573 | ||
574 | foreach ($filters as $f) { | |
575 | if ($f[0] == "score") { | |
576 | $score += $f[1]; | |
577 | }; | |
578 | } | |
579 | return $score; | |
580 | } | |
581 | ||
582 | function assign_article_to_labels($link, $id, $filters, $owner_uid) { | |
583 | foreach ($filters as $f) { | |
584 | if ($f[0] == "label") { | |
585 | label_add_article($link, $id, $f[1], $owner_uid); | |
586 | }; | |
587 | } | |
588 | } | |
589 | ||
590 | function getmicrotime() { | |
591 | list($usec, $sec) = explode(" ",microtime()); | |
592 | return ((float)$usec + (float)$sec); | |
593 | } | |
594 | ||
595 | function print_radio($id, $default, $true_is, $values, $attributes = "") { | |
596 | foreach ($values as $v) { | |
597 | ||
598 | if ($v == $default) | |
599 | $sel = "checked"; | |
600 | else | |
601 | $sel = ""; | |
602 | ||
603 | if ($v == $true_is) { | |
604 | $sel .= " value=\"1\""; | |
605 | } else { | |
606 | $sel .= " value=\"0\""; | |
607 | } | |
608 | ||
609 | print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\" | |
610 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
611 | ||
612 | } | |
613 | } | |
614 | ||
615 | function initialize_user_prefs($link, $uid, $profile = false) { | |
616 | ||
617 | $uid = db_escape_string($uid); | |
618 | ||
619 | if (!$profile) { | |
620 | $profile = "NULL"; | |
621 | $profile_qpart = "AND profile IS NULL"; | |
622 | } else { | |
623 | $profile_qpart = "AND profile = '$profile'"; | |
624 | } | |
625 | ||
626 | if (get_schema_version($link) < 63) $profile_qpart = ""; | |
627 | ||
628 | db_query($link, "BEGIN"); | |
629 | ||
630 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
631 | ||
632 | $u_result = db_query($link, "SELECT pref_name | |
633 | FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart"); | |
634 | ||
635 | $active_prefs = array(); | |
636 | ||
637 | while ($line = db_fetch_assoc($u_result)) { | |
638 | array_push($active_prefs, $line["pref_name"]); | |
639 | } | |
640 | ||
641 | while ($line = db_fetch_assoc($result)) { | |
642 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
643 | // print "adding " . $line["pref_name"] . "<br>"; | |
644 | ||
645 | if (get_schema_version($link) < 63) { | |
646 | db_query($link, "INSERT INTO ttrss_user_prefs | |
647 | (owner_uid,pref_name,value) VALUES | |
648 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
649 | ||
650 | } else { | |
651 | db_query($link, "INSERT INTO ttrss_user_prefs | |
652 | (owner_uid,pref_name,value, profile) VALUES | |
653 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)"); | |
654 | } | |
655 | ||
656 | } | |
657 | } | |
658 | ||
659 | db_query($link, "COMMIT"); | |
660 | ||
661 | } | |
662 | ||
663 | function get_ssl_certificate_id() { | |
664 | if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) { | |
665 | return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] . | |
666 | $_SERVER["REDIRECT_SSL_CLIENT_V_START"] . | |
667 | $_SERVER["REDIRECT_SSL_CLIENT_V_END"] . | |
668 | $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]); | |
669 | } | |
670 | return ""; | |
671 | } | |
672 | ||
673 | function get_login_by_ssl_certificate($link) { | |
674 | ||
675 | $cert_serial = db_escape_string(get_ssl_certificate_id()); | |
676 | ||
677 | if ($cert_serial) { | |
678 | $result = db_query($link, "SELECT login FROM ttrss_user_prefs, ttrss_users | |
679 | WHERE pref_name = 'SSL_CERT_SERIAL' AND value = '$cert_serial' AND | |
680 | owner_uid = ttrss_users.id"); | |
681 | ||
682 | if (db_num_rows($result) != 0) { | |
683 | return db_escape_string(db_fetch_result($result, 0, "login")); | |
684 | } | |
685 | } | |
686 | ||
687 | return ""; | |
688 | } | |
689 | ||
690 | function get_remote_user($link) { | |
691 | ||
692 | if (defined('ALLOW_REMOTE_USER_AUTH') && ALLOW_REMOTE_USER_AUTH) { | |
693 | return db_escape_string($_SERVER["REMOTE_USER"]); | |
694 | } | |
695 | ||
696 | return db_escape_string(get_login_by_ssl_certificate($link)); | |
697 | } | |
698 | ||
699 | function get_remote_fakepass($link) { | |
700 | if (get_remote_user($link)) | |
701 | return "******"; | |
702 | else | |
703 | return ""; | |
704 | } | |
705 | ||
706 | function authenticate_user($link, $login, $password, $force_auth = false) { | |
707 | ||
708 | if (!SINGLE_USER_MODE) { | |
709 | ||
710 | $pwd_hash1 = encrypt_password($password); | |
711 | $pwd_hash2 = encrypt_password($password, $login); | |
712 | $login = db_escape_string($login); | |
713 | ||
714 | $remote_user = get_remote_user($link); | |
715 | ||
716 | if ($remote_user && $remote_user == $login && $login != "admin") { | |
717 | ||
718 | $login = $remote_user; | |
719 | ||
720 | $query = "SELECT id,login,access_level,pwd_hash | |
721 | FROM ttrss_users WHERE | |
722 | login = '$login'"; | |
723 | ||
724 | if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER | |
725 | && $_SERVER["REMOTE_USER"]) { | |
726 | $result = db_query($link, $query); | |
727 | ||
728 | // First login ? | |
729 | if (db_num_rows($result) == 0) { | |
730 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); | |
731 | $pwd_hash = encrypt_password($password, $salt, true); | |
732 | ||
733 | $query2 = "INSERT INTO ttrss_users | |
734 | (login,access_level,last_login,created,pwd_hash,salt) | |
735 | VALUES ('$login', 0, null, NOW(), '$pwd_hash','$salt')"; | |
736 | db_query($link, $query2); | |
737 | } | |
738 | } | |
739 | ||
740 | } else if (get_schema_version($link) > 87) { | |
741 | $result = db_query($link, "SELECT salt FROM ttrss_users WHERE | |
742 | login = '$login'"); | |
743 | ||
744 | if (db_num_rows($result) != 1) { | |
745 | return false; | |
746 | } | |
747 | ||
748 | $salt = db_fetch_result($result, 0, "salt"); | |
749 | ||
750 | if ($salt == "") { | |
751 | ||
752 | $query = "SELECT id,login,access_level,pwd_hash | |
753 | FROM ttrss_users WHERE | |
754 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR | |
755 | pwd_hash = '$pwd_hash2')"; | |
756 | ||
757 | // verify and upgrade password to new salt base | |
758 | ||
759 | $result = db_query($link, $query); | |
760 | ||
761 | if (db_num_rows($result) == 1) { | |
762 | // upgrade password to MODE2 | |
763 | ||
764 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); | |
765 | $pwd_hash = encrypt_password($password, $salt, true); | |
766 | ||
767 | db_query($link, "UPDATE ttrss_users SET | |
768 | pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'"); | |
769 | ||
770 | $query = "SELECT id,login,access_level,pwd_hash | |
771 | FROM ttrss_users WHERE | |
772 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
773 | ||
774 | } else { | |
775 | return false; | |
776 | } | |
777 | ||
778 | } else { | |
779 | ||
780 | $pwd_hash = encrypt_password($password, $salt, true); | |
781 | ||
782 | $query = "SELECT id,login,access_level,pwd_hash | |
783 | FROM ttrss_users WHERE | |
784 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
785 | ||
786 | } | |
787 | } else { | |
788 | $query = "SELECT id,login,access_level,pwd_hash | |
789 | FROM ttrss_users WHERE | |
790 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR | |
791 | pwd_hash = '$pwd_hash2')"; | |
792 | } | |
793 | ||
794 | $result = db_query($link, $query); | |
795 | ||
796 | if (db_num_rows($result) == 1) { | |
797 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
798 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
799 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
800 | $_SESSION["csrf_token"] = sha1(uniqid(rand(), true)); | |
801 | ||
802 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
803 | $_SESSION["uid"]); | |
804 | ||
805 | ||
806 | // LemonLDAP can send user informations via HTTP HEADER | |
807 | if (defined('AUTO_CREATE_USER') && AUTO_CREATE_USER){ | |
808 | // update user name | |
809 | $fullname = $_SERVER['HTTP_USER_NAME'] ? $_SERVER['HTTP_USER_NAME'] : $_SERVER['AUTHENTICATE_CN']; | |
810 | if ($fullname){ | |
811 | $fullname = db_escape_string($fullname); | |
812 | db_query($link, "UPDATE ttrss_users SET full_name = '$fullname' WHERE id = " . | |
813 | $_SESSION["uid"]); | |
814 | } | |
815 | // update user mail | |
816 | $email = $_SERVER['HTTP_USER_MAIL'] ? $_SERVER['HTTP_USER_MAIL'] : $_SERVER['AUTHENTICATE_MAIL']; | |
817 | if ($email){ | |
818 | $email = db_escape_string($email); | |
819 | db_query($link, "UPDATE ttrss_users SET email = '$email' WHERE id = " . | |
820 | $_SESSION["uid"]); | |
821 | } | |
822 | } | |
823 | ||
824 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
825 | $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash"); | |
826 | ||
827 | $_SESSION["last_version_check"] = time(); | |
828 | ||
829 | initialize_user_prefs($link, $_SESSION["uid"]); | |
830 | ||
831 | return true; | |
832 | } | |
833 | ||
834 | return false; | |
835 | ||
836 | } else { | |
837 | ||
838 | $_SESSION["uid"] = 1; | |
839 | $_SESSION["name"] = "admin"; | |
840 | $_SESSION["access_level"] = 10; | |
841 | ||
842 | if (!$_SESSION["csrf_token"]) { | |
843 | $_SESSION["csrf_token"] = sha1(uniqid(rand(), true)); | |
844 | } | |
845 | ||
846 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
847 | ||
848 | initialize_user_prefs($link, $_SESSION["uid"]); | |
849 | ||
850 | return true; | |
851 | } | |
852 | } | |
853 | ||
854 | function make_password($length = 8) { | |
855 | ||
856 | $password = ""; | |
857 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; | |
858 | ||
859 | $i = 0; | |
860 | ||
861 | while ($i < $length) { | |
862 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
863 | ||
864 | if (!strstr($password, $char)) { | |
865 | $password .= $char; | |
866 | $i++; | |
867 | } | |
868 | } | |
869 | return $password; | |
870 | } | |
871 | ||
872 | // this is called after user is created to initialize default feeds, labels | |
873 | // or whatever else | |
874 | ||
875 | // user preferences are checked on every login, not here | |
876 | ||
877 | function initialize_user($link, $uid) { | |
878 | ||
879 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
880 | values ('$uid', 'Tiny Tiny RSS: New Releases', | |
881 | 'http://tt-rss.org/releases.rss')"); | |
882 | ||
883 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
884 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
885 | 'http://tt-rss.org/forum/rss.php')"); | |
886 | } | |
887 | ||
888 | function logout_user() { | |
889 | session_destroy(); | |
890 | if (isset($_COOKIE[session_name()])) { | |
891 | setcookie(session_name(), '', time()-42000, '/'); | |
892 | } | |
893 | } | |
894 | ||
895 | function validate_csrf($csrf_token) { | |
896 | return $csrf_token == $_SESSION['csrf_token']; | |
897 | } | |
898 | ||
899 | function validate_session($link) { | |
900 | if (SINGLE_USER_MODE) return true; | |
901 | ||
902 | $check_ip = $_SESSION['ip_address']; | |
903 | ||
904 | switch (SESSION_CHECK_ADDRESS) { | |
905 | case 0: | |
906 | $check_ip = ''; | |
907 | break; | |
908 | case 1: | |
909 | $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1); | |
910 | break; | |
911 | case 2: | |
912 | $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')); | |
913 | $check_ip = substr($check_ip, 0, strrpos($check_ip, '.')+1); | |
914 | break; | |
915 | }; | |
916 | ||
917 | if ($check_ip && strpos($_SERVER['REMOTE_ADDR'], $check_ip) !== 0) { | |
918 | $_SESSION["login_error_msg"] = | |
919 | __("Session failed to validate (incorrect IP)"); | |
920 | return false; | |
921 | } | |
922 | ||
923 | if ($_SESSION["ref_schema_version"] != get_schema_version($link, true)) | |
924 | return false; | |
925 | ||
926 | if ($_SESSION["uid"]) { | |
927 | ||
928 | $result = db_query($link, | |
929 | "SELECT pwd_hash FROM ttrss_users WHERE id = '".$_SESSION["uid"]."'"); | |
930 | ||
931 | $pwd_hash = db_fetch_result($result, 0, "pwd_hash"); | |
932 | ||
933 | if ($pwd_hash != $_SESSION["pwd_hash"]) { | |
934 | return false; | |
935 | } | |
936 | } | |
937 | ||
938 | /* if ($_SESSION["cookie_lifetime"] && $_SESSION["uid"]) { | |
939 | ||
940 | //print_r($_SESSION); | |
941 | ||
942 | if (time() > $_SESSION["cookie_lifetime"]) { | |
943 | return false; | |
944 | } | |
945 | } */ | |
946 | ||
947 | return true; | |
948 | } | |
949 | ||
950 | function login_sequence($link, $mobile = false) { | |
951 | $_SESSION["prefs_cache"] = array(); | |
952 | ||
953 | if (!SINGLE_USER_MODE) { | |
954 | ||
955 | $login_action = $_POST["login_action"]; | |
956 | ||
957 | # try to authenticate user if called from login form | |
958 | if ($login_action == "do_login") { | |
959 | $login = db_escape_string($_POST["login"]); | |
960 | $password = $_POST["password"]; | |
961 | $remember_me = $_POST["remember_me"]; | |
962 | ||
963 | if (authenticate_user($link, $login, $password)) { | |
964 | $_POST["password"] = ""; | |
965 | ||
966 | $_SESSION["language"] = $_POST["language"]; | |
967 | $_SESSION["ref_schema_version"] = get_schema_version($link, true); | |
968 | $_SESSION["bw_limit"] = !!$_POST["bw_limit"]; | |
969 | ||
970 | if ($_POST["profile"]) { | |
971 | ||
972 | $profile = db_escape_string($_POST["profile"]); | |
973 | ||
974 | $result = db_query($link, "SELECT id FROM ttrss_settings_profiles | |
975 | WHERE id = '$profile' AND owner_uid = " . $_SESSION["uid"]); | |
976 | ||
977 | if (db_num_rows($result) != 0) { | |
978 | $_SESSION["profile"] = $profile; | |
979 | $_SESSION["prefs_cache"] = array(); | |
980 | } | |
981 | } | |
982 | ||
983 | if ($_REQUEST['return']) { | |
984 | header("Location: " . $_REQUEST['return']); | |
985 | } else { | |
986 | header("Location: " . $_SERVER["REQUEST_URI"]); | |
987 | } | |
988 | ||
989 | exit; | |
990 | ||
991 | return; | |
992 | } else { | |
993 | $_SESSION["login_error_msg"] = __("Incorrect username or password"); | |
994 | } | |
995 | } | |
996 | ||
997 | if (!$_SESSION["uid"] || !validate_session($link)) { | |
998 | ||
999 | if (get_remote_user($link) && AUTO_LOGIN) { | |
1000 | authenticate_user($link, get_remote_user($link), null); | |
1001 | $_SESSION["ref_schema_version"] = get_schema_version($link, true); | |
1002 | } else { | |
1003 | render_login_form($link, $mobile); | |
1004 | //header("Location: login.php"); | |
1005 | exit; | |
1006 | } | |
1007 | } else { | |
1008 | /* bump login timestamp */ | |
1009 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
1010 | $_SESSION["uid"]); | |
1011 | ||
1012 | if ($_SESSION["language"] && SESSION_COOKIE_LIFETIME > 0) { | |
1013 | setcookie("ttrss_lang", $_SESSION["language"], | |
1014 | time() + SESSION_COOKIE_LIFETIME); | |
1015 | } | |
1016 | ||
1017 | // try to remove possible duplicates from feed counter cache | |
1018 | // ccache_cleanup($link, $_SESSION["uid"]); | |
1019 | } | |
1020 | ||
1021 | } else { | |
1022 | return authenticate_user($link, "admin", null); | |
1023 | } | |
1024 | } | |
1025 | ||
1026 | function truncate_string($str, $max_len, $suffix = '…') { | |
1027 | if (mb_strlen($str, "utf-8") > $max_len - 3) { | |
1028 | return mb_substr($str, 0, $max_len, "utf-8") . $suffix; | |
1029 | } else { | |
1030 | return $str; | |
1031 | } | |
1032 | } | |
1033 | ||
1034 | function theme_image($link, $filename) { | |
1035 | if ($link) { | |
1036 | $theme_path = get_user_theme_path($link); | |
1037 | ||
1038 | if ($theme_path && is_file($theme_path.$filename)) { | |
1039 | return $theme_path.$filename; | |
1040 | } else { | |
1041 | return $filename; | |
1042 | } | |
1043 | } else { | |
1044 | return $filename; | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | function get_user_theme($link) { | |
1049 | ||
1050 | if (get_schema_version($link) >= 63 && $_SESSION["uid"]) { | |
1051 | $theme_name = get_pref($link, "_THEME_ID"); | |
1052 | if (is_dir("themes/$theme_name")) { | |
1053 | return $theme_name; | |
1054 | } else { | |
1055 | return ''; | |
1056 | } | |
1057 | } else { | |
1058 | return ''; | |
1059 | } | |
1060 | ||
1061 | } | |
1062 | ||
1063 | function get_user_theme_path($link) { | |
1064 | $theme_path = ''; | |
1065 | ||
1066 | if (get_schema_version($link) >= 63 && $_SESSION["uid"]) { | |
1067 | $theme_name = get_pref($link, "_THEME_ID"); | |
1068 | ||
1069 | if ($theme_name && is_dir("themes/$theme_name")) { | |
1070 | $theme_path = "themes/$theme_name/"; | |
1071 | } else { | |
1072 | $theme_name = ''; | |
1073 | } | |
1074 | } else { | |
1075 | $theme_path = ''; | |
1076 | } | |
1077 | ||
1078 | if ($theme_path) { | |
1079 | if (is_file("$theme_path/theme.ini")) { | |
1080 | $ini = parse_ini_file("$theme_path/theme.ini", true); | |
1081 | if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED) { | |
1082 | return $theme_path; | |
1083 | } | |
1084 | } | |
1085 | } | |
1086 | return ''; | |
1087 | } | |
1088 | ||
1089 | function get_user_theme_options($link) { | |
1090 | $t = get_user_theme_path($link); | |
1091 | ||
1092 | if ($t) { | |
1093 | if (is_file("$t/theme.ini")) { | |
1094 | $ini = parse_ini_file("$t/theme.ini", true); | |
1095 | if ($ini['theme']['version']) { | |
1096 | return $ini['theme']['options']; | |
1097 | } | |
1098 | } | |
1099 | } | |
1100 | return ''; | |
1101 | } | |
1102 | ||
1103 | function print_theme_includes($link) { | |
1104 | ||
1105 | $t = get_user_theme_path($link); | |
1106 | $time = time(); | |
1107 | ||
1108 | if ($t) { | |
1109 | print "<link rel=\"stylesheet\" type=\"text/css\" | |
1110 | href=\"$t/theme.css?$time \">"; | |
1111 | if (file_exists("$t/theme.js")) { | |
1112 | print "<script type=\"text/javascript\" src=\"$t/theme.js?$time\"> | |
1113 | </script>"; | |
1114 | } | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | function get_all_themes() { | |
1119 | $themes = glob("themes/*"); | |
1120 | ||
1121 | asort($themes); | |
1122 | ||
1123 | $rv = array(); | |
1124 | ||
1125 | foreach ($themes as $t) { | |
1126 | if (is_file("$t/theme.ini")) { | |
1127 | $ini = parse_ini_file("$t/theme.ini", true); | |
1128 | if ($ini['theme']['version'] >= THEME_VERSION_REQUIRED && | |
1129 | !$ini['theme']['disabled']) { | |
1130 | $entry = array(); | |
1131 | $entry["path"] = $t; | |
1132 | $entry["base"] = basename($t); | |
1133 | $entry["name"] = $ini['theme']['name']; | |
1134 | $entry["version"] = $ini['theme']['version']; | |
1135 | $entry["author"] = $ini['theme']['author']; | |
1136 | $entry["options"] = $ini['theme']['options']; | |
1137 | array_push($rv, $entry); | |
1138 | } | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | return $rv; | |
1143 | } | |
1144 | ||
1145 | function convert_timestamp($timestamp, $source_tz, $dest_tz) { | |
1146 | ||
1147 | try { | |
1148 | $source_tz = new DateTimeZone($source_tz); | |
1149 | } catch (Exception $e) { | |
1150 | $source_tz = new DateTimeZone('UTC'); | |
1151 | } | |
1152 | ||
1153 | try { | |
1154 | $dest_tz = new DateTimeZone($dest_tz); | |
1155 | } catch (Exception $e) { | |
1156 | $dest_tz = new DateTimeZone('UTC'); | |
1157 | } | |
1158 | ||
1159 | $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz); | |
1160 | return $dt->format('U') + $dest_tz->getOffset($dt); | |
1161 | } | |
1162 | ||
1163 | function make_local_datetime($link, $timestamp, $long, $owner_uid = false, | |
1164 | $no_smart_dt = false) { | |
1165 | ||
1166 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
1167 | if (!$timestamp) $timestamp = '1970-01-01 0:00'; | |
1168 | ||
1169 | global $utc_tz; | |
1170 | global $tz_offset; | |
1171 | ||
1172 | # We store date in UTC internally | |
1173 | $dt = new DateTime($timestamp, $utc_tz); | |
1174 | ||
1175 | if ($tz_offset == -1) { | |
1176 | ||
1177 | $user_tz_string = get_pref($link, 'USER_TIMEZONE', $owner_uid); | |
1178 | ||
1179 | try { | |
1180 | $user_tz = new DateTimeZone($user_tz_string); | |
1181 | } catch (Exception $e) { | |
1182 | $user_tz = $utc_tz; | |
1183 | } | |
1184 | ||
1185 | $tz_offset = $user_tz->getOffset($dt); | |
1186 | } | |
1187 | ||
1188 | $user_timestamp = $dt->format('U') + $tz_offset; | |
1189 | ||
1190 | if (!$no_smart_dt) { | |
1191 | return smart_date_time($link, $user_timestamp, | |
1192 | $tz_offset, $owner_uid); | |
1193 | } else { | |
1194 | if ($long) | |
1195 | $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid); | |
1196 | else | |
1197 | $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid); | |
1198 | ||
1199 | return date($format, $user_timestamp); | |
1200 | } | |
1201 | } | |
1202 | ||
1203 | function smart_date_time($link, $timestamp, $tz_offset = 0, $owner_uid = false) { | |
1204 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
1205 | ||
1206 | if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) { | |
1207 | return date("G:i", $timestamp); | |
1208 | } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) { | |
1209 | $format = get_pref($link, 'SHORT_DATE_FORMAT', $owner_uid); | |
1210 | return date($format, $timestamp); | |
1211 | } else { | |
1212 | $format = get_pref($link, 'LONG_DATE_FORMAT', $owner_uid); | |
1213 | return date($format, $timestamp); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | function sql_bool_to_bool($s) { | |
1218 | if ($s == "t" || $s == "1" || $s == "true") { | |
1219 | return true; | |
1220 | } else { | |
1221 | return false; | |
1222 | } | |
1223 | } | |
1224 | ||
1225 | function bool_to_sql_bool($s) { | |
1226 | if ($s) { | |
1227 | return "true"; | |
1228 | } else { | |
1229 | return "false"; | |
1230 | } | |
1231 | } | |
1232 | ||
1233 | // Session caching removed due to causing wrong redirects to upgrade | |
1234 | // script when get_schema_version() is called on an obsolete session | |
1235 | // created on a previous schema version. | |
1236 | function get_schema_version($link, $nocache = false) { | |
1237 | global $schema_version; | |
1238 | ||
1239 | if (!$schema_version) { | |
1240 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
1241 | $version = db_fetch_result($result, 0, "schema_version"); | |
1242 | $schema_version = $version; | |
1243 | return $version; | |
1244 | } else { | |
1245 | return $schema_version; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | function sanity_check($link) { | |
1250 | require_once 'errors.php'; | |
1251 | ||
1252 | $error_code = 0; | |
1253 | $schema_version = get_schema_version($link, true); | |
1254 | ||
1255 | if ($schema_version != SCHEMA_VERSION) { | |
1256 | $error_code = 5; | |
1257 | } | |
1258 | ||
1259 | if (DB_TYPE == "mysql") { | |
1260 | $result = db_query($link, "SELECT true", false); | |
1261 | if (db_num_rows($result) != 1) { | |
1262 | $error_code = 10; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | if (db_escape_string("testTEST") != "testTEST") { | |
1267 | $error_code = 12; | |
1268 | } | |
1269 | ||
1270 | return array("code" => $error_code, "message" => $ERRORS[$error_code]); | |
1271 | } | |
1272 | ||
1273 | function file_is_locked($filename) { | |
1274 | if (function_exists('flock')) { | |
1275 | $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r"); | |
1276 | if ($fp) { | |
1277 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1278 | flock($fp, LOCK_UN); | |
1279 | fclose($fp); | |
1280 | return false; | |
1281 | } | |
1282 | fclose($fp); | |
1283 | return true; | |
1284 | } else { | |
1285 | return false; | |
1286 | } | |
1287 | } | |
1288 | return true; // consider the file always locked and skip the test | |
1289 | } | |
1290 | ||
1291 | function make_lockfile($filename) { | |
1292 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); | |
1293 | ||
1294 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1295 | if (function_exists('posix_getpid')) { | |
1296 | fwrite($fp, posix_getpid() . "\n"); | |
1297 | } | |
1298 | return $fp; | |
1299 | } else { | |
1300 | return false; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | function make_stampfile($filename) { | |
1305 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); | |
1306 | ||
1307 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1308 | fwrite($fp, time() . "\n"); | |
1309 | flock($fp, LOCK_UN); | |
1310 | fclose($fp); | |
1311 | return true; | |
1312 | } else { | |
1313 | return false; | |
1314 | } | |
1315 | } | |
1316 | ||
1317 | function sql_random_function() { | |
1318 | if (DB_TYPE == "mysql") { | |
1319 | return "RAND()"; | |
1320 | } else { | |
1321 | return "RANDOM()"; | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | function catchup_feed($link, $feed, $cat_view, $owner_uid = false, $max_id = false) { | |
1326 | ||
1327 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
1328 | ||
1329 | //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
1330 | ||
1331 | $ref_check_qpart = ($max_id && | |
1332 | !get_pref($link, 'REVERSE_HEADLINES')) ? "ref_id <= '$max_id'" : "true"; | |
1333 | ||
1334 | if (is_numeric($feed)) { | |
1335 | if ($cat_view) { | |
1336 | ||
1337 | if ($feed >= 0) { | |
1338 | ||
1339 | if ($feed > 0) { | |
1340 | $cat_qpart = "cat_id = '$feed'"; | |
1341 | } else { | |
1342 | $cat_qpart = "cat_id IS NULL"; | |
1343 | } | |
1344 | ||
1345 | $tmp_result = db_query($link, "SELECT id | |
1346 | FROM ttrss_feeds WHERE $cat_qpart AND owner_uid = $owner_uid"); | |
1347 | ||
1348 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
1349 | ||
1350 | $tmp_feed = $tmp_line["id"]; | |
1351 | ||
1352 | db_query($link, "UPDATE ttrss_user_entries | |
1353 | SET unread = false,last_read = NOW() | |
1354 | WHERE feed_id = '$tmp_feed' | |
1355 | AND $ref_check_qpart | |
1356 | AND owner_uid = $owner_uid"); | |
1357 | } | |
1358 | } else if ($feed == -2) { | |
1359 | ||
1360 | db_query($link, "UPDATE ttrss_user_entries | |
1361 | SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*) | |
1362 | FROM ttrss_user_labels2 WHERE article_id = ref_id) > 0 | |
1363 | AND $ref_check_qpart | |
1364 | AND unread = true AND owner_uid = $owner_uid"); | |
1365 | } | |
1366 | ||
1367 | } else if ($feed > 0) { | |
1368 | ||
1369 | db_query($link, "UPDATE ttrss_user_entries | |
1370 | SET unread = false,last_read = NOW() | |
1371 | WHERE feed_id = '$feed' | |
1372 | AND $ref_check_qpart | |
1373 | AND owner_uid = $owner_uid"); | |
1374 | ||
1375 | } else if ($feed < 0 && $feed > -10) { // special, like starred | |
1376 | ||
1377 | if ($feed == -1) { | |
1378 | db_query($link, "UPDATE ttrss_user_entries | |
1379 | SET unread = false,last_read = NOW() | |
1380 | WHERE marked = true | |
1381 | AND $ref_check_qpart | |
1382 | AND owner_uid = $owner_uid"); | |
1383 | } | |
1384 | ||
1385 | if ($feed == -2) { | |
1386 | db_query($link, "UPDATE ttrss_user_entries | |
1387 | SET unread = false,last_read = NOW() | |
1388 | WHERE published = true | |
1389 | AND $ref_check_qpart | |
1390 | AND owner_uid = $owner_uid"); | |
1391 | } | |
1392 | ||
1393 | if ($feed == -3) { | |
1394 | ||
1395 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE"); | |
1396 | ||
1397 | if (DB_TYPE == "pgsql") { | |
1398 | $match_part = "updated > NOW() - INTERVAL '$intl hour' "; | |
1399 | } else { | |
1400 | $match_part = "updated > DATE_SUB(NOW(), | |
1401 | INTERVAL $intl HOUR) "; | |
1402 | } | |
1403 | ||
1404 | $result = db_query($link, "SELECT id FROM ttrss_entries, | |
1405 | ttrss_user_entries WHERE $match_part AND | |
1406 | unread = true AND | |
1407 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1408 | owner_uid = $owner_uid"); | |
1409 | ||
1410 | $affected_ids = array(); | |
1411 | ||
1412 | while ($line = db_fetch_assoc($result)) { | |
1413 | array_push($affected_ids, $line["id"]); | |
1414 | } | |
1415 | ||
1416 | catchupArticlesById($link, $affected_ids, 0); | |
1417 | } | |
1418 | ||
1419 | if ($feed == -4) { | |
1420 | db_query($link, "UPDATE ttrss_user_entries | |
1421 | SET unread = false,last_read = NOW() | |
1422 | WHERE $ref_check_qpart AND owner_uid = $owner_uid"); | |
1423 | } | |
1424 | ||
1425 | } else if ($feed < -10) { // label | |
1426 | ||
1427 | $label_id = -$feed - 11; | |
1428 | ||
1429 | db_query($link, "UPDATE ttrss_user_entries, ttrss_user_labels2 | |
1430 | SET unread = false, last_read = NOW() | |
1431 | WHERE label_id = '$label_id' AND unread = true | |
1432 | AND $ref_check_qpart | |
1433 | AND owner_uid = '$owner_uid' AND ref_id = article_id"); | |
1434 | ||
1435 | } | |
1436 | ||
1437 | ccache_update($link, $feed, $owner_uid, $cat_view); | |
1438 | ||
1439 | } else { // tag | |
1440 | db_query($link, "BEGIN"); | |
1441 | ||
1442 | $tag_name = db_escape_string($feed); | |
1443 | ||
1444 | $result = db_query($link, "SELECT post_int_id FROM ttrss_tags | |
1445 | WHERE tag_name = '$tag_name' AND owner_uid = $owner_uid"); | |
1446 | ||
1447 | while ($line = db_fetch_assoc($result)) { | |
1448 | db_query($link, "UPDATE ttrss_user_entries SET | |
1449 | unread = false, last_read = NOW() | |
1450 | WHERE $ref_check_qpart AND int_id = " . $line["post_int_id"]); | |
1451 | } | |
1452 | db_query($link, "COMMIT"); | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | function getAllCounters($link, $omode = "flc", $active_feed = false) { | |
1457 | ||
1458 | if (!$omode) $omode = "flc"; | |
1459 | ||
1460 | $data = getGlobalCounters($link); | |
1461 | ||
1462 | $data = array_merge($data, getVirtCounters($link)); | |
1463 | ||
1464 | if (strchr($omode, "l")) $data = array_merge($data, getLabelCounters($link)); | |
1465 | if (strchr($omode, "f")) $data = array_merge($data, getFeedCounters($link, $active_feed)); | |
1466 | if (strchr($omode, "t")) $data = array_merge($data, getTagCounters($link)); | |
1467 | if (strchr($omode, "c")) $data = array_merge($data, getCategoryCounters($link)); | |
1468 | ||
1469 | return $data; | |
1470 | } | |
1471 | ||
1472 | function getCategoryTitle($link, $cat_id) { | |
1473 | ||
1474 | if ($cat_id == -1) { | |
1475 | return __("Special"); | |
1476 | } else if ($cat_id == -2) { | |
1477 | return __("Labels"); | |
1478 | } else { | |
1479 | ||
1480 | $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE | |
1481 | id = '$cat_id'"); | |
1482 | ||
1483 | if (db_num_rows($result) == 1) { | |
1484 | return db_fetch_result($result, 0, "title"); | |
1485 | } else { | |
1486 | return "Uncategorized"; | |
1487 | } | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | ||
1492 | function getCategoryCounters($link) { | |
1493 | $ret_arr = array(); | |
1494 | ||
1495 | /* Labels category */ | |
1496 | ||
1497 | $cv = array("id" => -2, "kind" => "cat", | |
1498 | "counter" => getCategoryUnread($link, -2)); | |
1499 | ||
1500 | array_push($ret_arr, $cv); | |
1501 | ||
1502 | $age_qpart = getMaxAgeSubquery(); | |
1503 | ||
1504 | $result = db_query($link, "SELECT id AS cat_id, value AS unread | |
1505 | FROM ttrss_feed_categories, ttrss_cat_counters_cache | |
1506 | WHERE ttrss_cat_counters_cache.feed_id = id AND | |
1507 | ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]); | |
1508 | ||
1509 | while ($line = db_fetch_assoc($result)) { | |
1510 | $line["cat_id"] = (int) $line["cat_id"]; | |
1511 | ||
1512 | $cv = array("id" => $line["cat_id"], "kind" => "cat", | |
1513 | "counter" => $line["unread"]); | |
1514 | ||
1515 | array_push($ret_arr, $cv); | |
1516 | } | |
1517 | ||
1518 | /* Special case: NULL category doesn't actually exist in the DB */ | |
1519 | ||
1520 | $cv = array("id" => 0, "kind" => "cat", | |
1521 | "counter" => ccache_find($link, 0, $_SESSION["uid"], true)); | |
1522 | ||
1523 | array_push($ret_arr, $cv); | |
1524 | ||
1525 | return $ret_arr; | |
1526 | } | |
1527 | ||
1528 | function getCategoryUnread($link, $cat, $owner_uid = false) { | |
1529 | ||
1530 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1531 | ||
1532 | if ($cat >= 0) { | |
1533 | ||
1534 | if ($cat != 0) { | |
1535 | $cat_query = "cat_id = '$cat'"; | |
1536 | } else { | |
1537 | $cat_query = "cat_id IS NULL"; | |
1538 | } | |
1539 | ||
1540 | $age_qpart = getMaxAgeSubquery(); | |
1541 | ||
1542 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE $cat_query | |
1543 | AND owner_uid = " . $owner_uid); | |
1544 | ||
1545 | $cat_feeds = array(); | |
1546 | while ($line = db_fetch_assoc($result)) { | |
1547 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1548 | } | |
1549 | ||
1550 | if (count($cat_feeds) == 0) return 0; | |
1551 | ||
1552 | $match_part = implode(" OR ", $cat_feeds); | |
1553 | ||
1554 | $result = db_query($link, "SELECT COUNT(int_id) AS unread | |
1555 | FROM ttrss_user_entries,ttrss_entries | |
1556 | WHERE unread = true AND ($match_part) AND id = ref_id | |
1557 | AND $age_qpart AND owner_uid = " . $owner_uid); | |
1558 | ||
1559 | $unread = 0; | |
1560 | ||
1561 | # this needs to be rewritten | |
1562 | while ($line = db_fetch_assoc($result)) { | |
1563 | $unread += $line["unread"]; | |
1564 | } | |
1565 | ||
1566 | return $unread; | |
1567 | } else if ($cat == -1) { | |
1568 | return getFeedUnread($link, -1) + getFeedUnread($link, -2) + getFeedUnread($link, -3) + getFeedUnread($link, 0); | |
1569 | } else if ($cat == -2) { | |
1570 | ||
1571 | $result = db_query($link, " | |
1572 | SELECT COUNT(unread) AS unread FROM | |
1573 | ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds | |
1574 | WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND | |
1575 | ttrss_labels2.owner_uid = '$owner_uid' | |
1576 | AND unread = true AND feed_id = ttrss_feeds.id | |
1577 | AND ttrss_user_entries.owner_uid = '$owner_uid'"); | |
1578 | ||
1579 | $unread = db_fetch_result($result, 0, "unread"); | |
1580 | ||
1581 | return $unread; | |
1582 | ||
1583 | } | |
1584 | } | |
1585 | ||
1586 | function getMaxAgeSubquery($days = COUNTERS_MAX_AGE) { | |
1587 | if (DB_TYPE == "pgsql") { | |
1588 | return "ttrss_entries.date_updated > | |
1589 | NOW() - INTERVAL '$days days'"; | |
1590 | } else { | |
1591 | return "ttrss_entries.date_updated > | |
1592 | DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
1593 | } | |
1594 | } | |
1595 | ||
1596 | function getFeedUnread($link, $feed, $is_cat = false) { | |
1597 | return getFeedArticles($link, $feed, $is_cat, true, $_SESSION["uid"]); | |
1598 | } | |
1599 | ||
1600 | function getLabelUnread($link, $label_id, $owner_uid = false) { | |
1601 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1602 | ||
1603 | $result = db_query($link, " | |
1604 | SELECT COUNT(unread) AS unread FROM | |
1605 | ttrss_user_entries, ttrss_labels2, ttrss_user_labels2, ttrss_feeds | |
1606 | WHERE label_id = ttrss_labels2.id AND article_id = ref_id AND | |
1607 | ttrss_labels2.owner_uid = '$owner_uid' AND ttrss_labels2.id = '$label_id' | |
1608 | AND unread = true AND feed_id = ttrss_feeds.id | |
1609 | AND ttrss_user_entries.owner_uid = '$owner_uid'"); | |
1610 | ||
1611 | if (db_num_rows($result) != 0) { | |
1612 | return db_fetch_result($result, 0, "unread"); | |
1613 | } else { | |
1614 | return 0; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | function getFeedArticles($link, $feed, $is_cat = false, $unread_only = false, | |
1619 | $owner_uid = false) { | |
1620 | ||
1621 | $n_feed = (int) $feed; | |
1622 | ||
1623 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1624 | ||
1625 | if ($unread_only) { | |
1626 | $unread_qpart = "unread = true"; | |
1627 | } else { | |
1628 | $unread_qpart = "true"; | |
1629 | } | |
1630 | ||
1631 | $age_qpart = getMaxAgeSubquery(); | |
1632 | ||
1633 | if ($is_cat) { | |
1634 | return getCategoryUnread($link, $n_feed, $owner_uid); | |
1635 | } if ($feed != "0" && $n_feed == 0) { | |
1636 | ||
1637 | $feed = db_escape_string($feed); | |
1638 | ||
1639 | $result = db_query($link, "SELECT SUM((SELECT COUNT(int_id) | |
1640 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id | |
1641 | AND ref_id = id AND $age_qpart | |
1642 | AND $unread_qpart)) AS count FROM ttrss_tags | |
1643 | WHERE owner_uid = $owner_uid AND tag_name = '$feed'"); | |
1644 | return db_fetch_result($result, 0, "count"); | |
1645 | ||
1646 | } else if ($n_feed == -1) { | |
1647 | $match_part = "marked = true"; | |
1648 | } else if ($n_feed == -2) { | |
1649 | $match_part = "published = true"; | |
1650 | } else if ($n_feed == -3) { | |
1651 | $match_part = "unread = true AND score >= 0"; | |
1652 | ||
1653 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid); | |
1654 | ||
1655 | if (DB_TYPE == "pgsql") { | |
1656 | $match_part .= " AND updated > NOW() - INTERVAL '$intl hour' "; | |
1657 | } else { | |
1658 | $match_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; | |
1659 | } | |
1660 | } else if ($n_feed == -4) { | |
1661 | $match_part = "true"; | |
1662 | } else if ($n_feed >= 0) { | |
1663 | ||
1664 | if ($n_feed != 0) { | |
1665 | $match_part = "feed_id = '$n_feed'"; | |
1666 | } else { | |
1667 | $match_part = "feed_id IS NULL"; | |
1668 | } | |
1669 | ||
1670 | } else if ($feed < -10) { | |
1671 | ||
1672 | $label_id = -$feed - 11; | |
1673 | ||
1674 | return getLabelUnread($link, $label_id, $owner_uid); | |
1675 | ||
1676 | } | |
1677 | ||
1678 | if ($match_part) { | |
1679 | ||
1680 | if ($n_feed != 0) { | |
1681 | $from_qpart = "ttrss_user_entries,ttrss_feeds,ttrss_entries"; | |
1682 | $feeds_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND"; | |
1683 | } else { | |
1684 | $from_qpart = "ttrss_user_entries,ttrss_entries"; | |
1685 | $feeds_qpart = ''; | |
1686 | } | |
1687 | ||
1688 | $query = "SELECT count(int_id) AS unread | |
1689 | FROM $from_qpart WHERE | |
1690 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
1691 | $age_qpart AND | |
1692 | $feeds_qpart | |
1693 | $unread_qpart AND ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid"; | |
1694 | ||
1695 | $result = db_query($link, $query); | |
1696 | ||
1697 | } else { | |
1698 | ||
1699 | $result = db_query($link, "SELECT COUNT(post_int_id) AS unread | |
1700 | FROM ttrss_tags,ttrss_user_entries,ttrss_entries | |
1701 | WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id | |
1702 | AND $unread_qpart AND $age_qpart AND | |
1703 | ttrss_tags.owner_uid = " . $owner_uid); | |
1704 | } | |
1705 | ||
1706 | $unread = db_fetch_result($result, 0, "unread"); | |
1707 | ||
1708 | return $unread; | |
1709 | } | |
1710 | ||
1711 | function getGlobalUnread($link, $user_id = false) { | |
1712 | ||
1713 | if (!$user_id) { | |
1714 | $user_id = $_SESSION["uid"]; | |
1715 | } | |
1716 | ||
1717 | $result = db_query($link, "SELECT SUM(value) AS c_id FROM ttrss_counters_cache | |
1718 | WHERE owner_uid = '$user_id' AND feed_id > 0"); | |
1719 | ||
1720 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1721 | ||
1722 | return $c_id; | |
1723 | } | |
1724 | ||
1725 | function getGlobalCounters($link, $global_unread = -1) { | |
1726 | $ret_arr = array(); | |
1727 | ||
1728 | if ($global_unread == -1) { | |
1729 | $global_unread = getGlobalUnread($link); | |
1730 | } | |
1731 | ||
1732 | $cv = array("id" => "global-unread", | |
1733 | "counter" => $global_unread); | |
1734 | ||
1735 | array_push($ret_arr, $cv); | |
1736 | ||
1737 | $result = db_query($link, "SELECT COUNT(id) AS fn FROM | |
1738 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1739 | ||
1740 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1741 | ||
1742 | $cv = array("id" => "subscribed-feeds", | |
1743 | "counter" => $subscribed_feeds); | |
1744 | ||
1745 | array_push($ret_arr, $cv); | |
1746 | ||
1747 | return $ret_arr; | |
1748 | } | |
1749 | ||
1750 | function getTagCounters($link) { | |
1751 | ||
1752 | $ret_arr = array(); | |
1753 | ||
1754 | $age_qpart = getMaxAgeSubquery(); | |
1755 | ||
1756 | $result = db_query($link, "SELECT tag_name,SUM((SELECT COUNT(int_id) | |
1757 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id | |
1758 | AND ref_id = id AND $age_qpart | |
1759 | AND unread = true)) AS count FROM ttrss_tags | |
1760 | WHERE owner_uid = ".$_SESSION['uid']." GROUP BY tag_name | |
1761 | ORDER BY count DESC LIMIT 55"); | |
1762 | ||
1763 | $tags = array(); | |
1764 | ||
1765 | while ($line = db_fetch_assoc($result)) { | |
1766 | $tags[$line["tag_name"]] += $line["count"]; | |
1767 | } | |
1768 | ||
1769 | foreach (array_keys($tags) as $tag) { | |
1770 | $unread = $tags[$tag]; | |
1771 | $tag = htmlspecialchars($tag); | |
1772 | ||
1773 | $cv = array("id" => $tag, | |
1774 | "kind" => "tag", | |
1775 | "counter" => $unread); | |
1776 | ||
1777 | array_push($ret_arr, $cv); | |
1778 | } | |
1779 | ||
1780 | return $ret_arr; | |
1781 | } | |
1782 | ||
1783 | function getVirtCounters($link) { | |
1784 | ||
1785 | $ret_arr = array(); | |
1786 | ||
1787 | for ($i = 0; $i >= -4; $i--) { | |
1788 | ||
1789 | $count = getFeedUnread($link, $i); | |
1790 | ||
1791 | $cv = array("id" => $i, | |
1792 | "counter" => $count); | |
1793 | ||
1794 | // if (get_pref($link, 'EXTENDED_FEEDLIST')) | |
1795 | // $cv["xmsg"] = getFeedArticles($link, $i)." ".__("total"); | |
1796 | ||
1797 | array_push($ret_arr, $cv); | |
1798 | } | |
1799 | ||
1800 | return $ret_arr; | |
1801 | } | |
1802 | ||
1803 | function getLabelCounters($link, $descriptions = false) { | |
1804 | ||
1805 | $ret_arr = array(); | |
1806 | ||
1807 | $age_qpart = getMaxAgeSubquery(); | |
1808 | ||
1809 | $owner_uid = $_SESSION["uid"]; | |
1810 | ||
1811 | $result = db_query($link, "SELECT id, caption FROM ttrss_labels2 | |
1812 | WHERE owner_uid = '$owner_uid'"); | |
1813 | ||
1814 | while ($line = db_fetch_assoc($result)) { | |
1815 | ||
1816 | $id = -$line["id"] - 11; | |
1817 | ||
1818 | $label_name = $line["caption"]; | |
1819 | $count = getFeedUnread($link, $id); | |
1820 | ||
1821 | $cv = array("id" => $id, | |
1822 | "counter" => $count); | |
1823 | ||
1824 | if ($descriptions) | |
1825 | $cv["description"] = $label_name; | |
1826 | ||
1827 | // if (get_pref($link, 'EXTENDED_FEEDLIST')) | |
1828 | // $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total"); | |
1829 | ||
1830 | array_push($ret_arr, $cv); | |
1831 | } | |
1832 | ||
1833 | return $ret_arr; | |
1834 | } | |
1835 | ||
1836 | function getFeedCounters($link, $active_feed = false) { | |
1837 | ||
1838 | $ret_arr = array(); | |
1839 | ||
1840 | $age_qpart = getMaxAgeSubquery(); | |
1841 | ||
1842 | $query = "SELECT ttrss_feeds.id, | |
1843 | ttrss_feeds.title, | |
1844 | ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, | |
1845 | last_error, value AS count | |
1846 | FROM ttrss_feeds, ttrss_counters_cache | |
1847 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
1848 | AND ttrss_counters_cache.feed_id = id"; | |
1849 | ||
1850 | $result = db_query($link, $query); | |
1851 | $fctrs_modified = false; | |
1852 | ||
1853 | while ($line = db_fetch_assoc($result)) { | |
1854 | ||
1855 | $id = $line["id"]; | |
1856 | $count = $line["count"]; | |
1857 | $last_error = htmlspecialchars($line["last_error"]); | |
1858 | ||
1859 | $last_updated = make_local_datetime($link, $line['last_updated'], false); | |
1860 | ||
1861 | $has_img = feed_has_icon($id); | |
1862 | ||
1863 | if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2) | |
1864 | $last_updated = ''; | |
1865 | ||
1866 | $cv = array("id" => $id, | |
1867 | "updated" => $last_updated, | |
1868 | "counter" => $count, | |
1869 | "has_img" => (int) $has_img); | |
1870 | ||
1871 | if ($last_error) | |
1872 | $cv["error"] = $last_error; | |
1873 | ||
1874 | // if (get_pref($link, 'EXTENDED_FEEDLIST')) | |
1875 | // $cv["xmsg"] = getFeedArticles($link, $id)." ".__("total"); | |
1876 | ||
1877 | if ($active_feed && $id == $active_feed) | |
1878 | $cv["title"] = truncate_string($line["title"], 30); | |
1879 | ||
1880 | array_push($ret_arr, $cv); | |
1881 | ||
1882 | } | |
1883 | ||
1884 | return $ret_arr; | |
1885 | } | |
1886 | ||
1887 | function get_pgsql_version($link) { | |
1888 | $result = db_query($link, "SELECT version() AS version"); | |
1889 | $version = explode(" ", db_fetch_result($result, 0, "version")); | |
1890 | return $version[1]; | |
1891 | } | |
1892 | ||
1893 | /** | |
1894 | * @return integer Status code: | |
1895 | * 0 - OK, Feed already exists | |
1896 | * 1 - OK, Feed added | |
1897 | * 2 - Invalid URL | |
1898 | * 3 - URL content is HTML, no feeds available | |
1899 | * 4 - URL content is HTML which contains multiple feeds. | |
1900 | * Here you should call extractfeedurls in rpc-backend | |
1901 | * to get all possible feeds. | |
1902 | * 5 - Couldn't download the URL content. | |
1903 | */ | |
1904 | function subscribe_to_feed($link, $url, $cat_id = 0, | |
1905 | $auth_login = '', $auth_pass = '', $need_auth = false) { | |
1906 | ||
1907 | require_once "include/rssfuncs.php"; | |
1908 | ||
1909 | $url = fix_url($url); | |
1910 | ||
1911 | if (!$url || !validate_feed_url($url)) return 2; | |
1912 | ||
1913 | $update_method = 0; | |
1914 | ||
1915 | $result = db_query($link, "SELECT twitter_oauth FROM ttrss_users | |
1916 | WHERE id = ".$_SESSION['uid']); | |
1917 | ||
1918 | $has_oauth = db_fetch_result($result, 0, 'twitter_oauth'); | |
1919 | ||
1920 | if (!$need_auth || !$has_oauth || strpos($url, '://api.twitter.com') === false) { | |
1921 | if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5; | |
1922 | ||
1923 | if (url_is_html($url, $auth_login, $auth_pass)) { | |
1924 | $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass); | |
1925 | if (count($feedUrls) == 0) { | |
1926 | return 3; | |
1927 | } else if (count($feedUrls) > 1) { | |
1928 | return 4; | |
1929 | } | |
1930 | //use feed url as new URL | |
1931 | $url = key($feedUrls); | |
1932 | } | |
1933 | ||
1934 | } else { | |
1935 | if (!fetch_twitter_rss($link, $url, $_SESSION['uid'])) | |
1936 | return 5; | |
1937 | ||
1938 | $update_method = 3; | |
1939 | } | |
1940 | if ($cat_id == "0" || !$cat_id) { | |
1941 | $cat_qpart = "NULL"; | |
1942 | } else { | |
1943 | $cat_qpart = "'$cat_id'"; | |
1944 | } | |
1945 | ||
1946 | $result = db_query($link, | |
1947 | "SELECT id FROM ttrss_feeds | |
1948 | WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]); | |
1949 | ||
1950 | if (db_num_rows($result) == 0) { | |
1951 | $result = db_query($link, | |
1952 | "INSERT INTO ttrss_feeds | |
1953 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method) | |
1954 | VALUES ('".$_SESSION["uid"]."', '$url', | |
1955 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', '$update_method')"); | |
1956 | ||
1957 | $result = db_query($link, | |
1958 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$url' | |
1959 | AND owner_uid = " . $_SESSION["uid"]); | |
1960 | ||
1961 | $feed_id = db_fetch_result($result, 0, "id"); | |
1962 | ||
1963 | if ($feed_id) { | |
1964 | update_rss_feed($link, $feed_id, true); | |
1965 | } | |
1966 | ||
1967 | return 1; | |
1968 | } else { | |
1969 | return 0; | |
1970 | } | |
1971 | } | |
1972 | ||
1973 | function print_feed_select($link, $id, $default_id = "", | |
1974 | $attributes = "", $include_all_feeds = true) { | |
1975 | ||
1976 | print "<select id=\"$id\" name=\"$id\" $attributes>"; | |
1977 | if ($include_all_feeds) { | |
1978 | print "<option value=\"0\">".__('All feeds')."</option>"; | |
1979 | } | |
1980 | ||
1981 | $result = db_query($link, "SELECT id,title FROM ttrss_feeds | |
1982 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
1983 | ||
1984 | if (db_num_rows($result) > 0 && $include_all_feeds) { | |
1985 | print "<option disabled>--------</option>"; | |
1986 | } | |
1987 | ||
1988 | while ($line = db_fetch_assoc($result)) { | |
1989 | if ($line["id"] == $default_id) { | |
1990 | $is_selected = "selected=\"1\""; | |
1991 | } else { | |
1992 | $is_selected = ""; | |
1993 | } | |
1994 | ||
1995 | $title = truncate_string(htmlspecialchars($line["title"]), 40); | |
1996 | ||
1997 | printf("<option $is_selected value='%d'>%s</option>", | |
1998 | $line["id"], $title); | |
1999 | } | |
2000 | ||
2001 | print "</select>"; | |
2002 | } | |
2003 | ||
2004 | function print_feed_cat_select($link, $id, $default_id = "", | |
2005 | $attributes = "", $include_all_cats = true) { | |
2006 | ||
2007 | print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>"; | |
2008 | ||
2009 | if ($include_all_cats) { | |
2010 | print "<option value=\"0\">".__('Uncategorized')."</option>"; | |
2011 | } | |
2012 | ||
2013 | $result = db_query($link, "SELECT id,title FROM ttrss_feed_categories | |
2014 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title"); | |
2015 | ||
2016 | if (db_num_rows($result) > 0 && $include_all_cats) { | |
2017 | print "<option disabled=\"1\">--------</option>"; | |
2018 | } | |
2019 | ||
2020 | while ($line = db_fetch_assoc($result)) { | |
2021 | if ($line["id"] == $default_id) { | |
2022 | $is_selected = "selected=\"1\""; | |
2023 | } else { | |
2024 | $is_selected = ""; | |
2025 | } | |
2026 | ||
2027 | if ($line["title"]) | |
2028 | printf("<option $is_selected value='%d'>%s</option>", | |
2029 | $line["id"], htmlspecialchars($line["title"])); | |
2030 | } | |
2031 | ||
2032 | # print "<option value=\"ADD_CAT\">" .__("Add category...") . "</option>"; | |
2033 | ||
2034 | print "</select>"; | |
2035 | } | |
2036 | ||
2037 | function checkbox_to_sql_bool($val) { | |
2038 | return ($val == "on") ? "true" : "false"; | |
2039 | } | |
2040 | ||
2041 | function getFeedCatTitle($link, $id) { | |
2042 | if ($id == -1) { | |
2043 | return __("Special"); | |
2044 | } else if ($id < -10) { | |
2045 | return __("Labels"); | |
2046 | } else if ($id > 0) { | |
2047 | $result = db_query($link, "SELECT ttrss_feed_categories.title | |
2048 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
2049 | cat_id = ttrss_feed_categories.id"); | |
2050 | if (db_num_rows($result) == 1) { | |
2051 | return db_fetch_result($result, 0, "title"); | |
2052 | } else { | |
2053 | return __("Uncategorized"); | |
2054 | } | |
2055 | } else { | |
2056 | return "getFeedCatTitle($id) failed"; | |
2057 | } | |
2058 | ||
2059 | } | |
2060 | ||
2061 | function getFeedIcon($id) { | |
2062 | switch ($id) { | |
2063 | case 0: | |
2064 | return "images/archive.png"; | |
2065 | break; | |
2066 | case -1: | |
2067 | return "images/mark_set.png"; | |
2068 | break; | |
2069 | case -2: | |
2070 | return "images/pub_set.png"; | |
2071 | break; | |
2072 | case -3: | |
2073 | return "images/fresh.png"; | |
2074 | break; | |
2075 | case -4: | |
2076 | return "images/tag.png"; | |
2077 | break; | |
2078 | default: | |
2079 | if ($id < -10) { | |
2080 | return "images/label.png"; | |
2081 | } else { | |
2082 | if (file_exists(ICONS_DIR . "/$id.ico")) | |
2083 | return ICONS_URL . "/$id.ico"; | |
2084 | } | |
2085 | break; | |
2086 | } | |
2087 | } | |
2088 | ||
2089 | function getFeedTitle($link, $id) { | |
2090 | if ($id == -1) { | |
2091 | return __("Starred articles"); | |
2092 | } else if ($id == -2) { | |
2093 | return __("Published articles"); | |
2094 | } else if ($id == -3) { | |
2095 | return __("Fresh articles"); | |
2096 | } else if ($id == -4) { | |
2097 | return __("All articles"); | |
2098 | } else if ($id === 0 || $id === "0") { | |
2099 | return __("Archived articles"); | |
2100 | } else if ($id < -10) { | |
2101 | $label_id = -$id - 11; | |
2102 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'"); | |
2103 | if (db_num_rows($result) == 1) { | |
2104 | return db_fetch_result($result, 0, "caption"); | |
2105 | } else { | |
2106 | return "Unknown label ($label_id)"; | |
2107 | } | |
2108 | ||
2109 | } else if (is_numeric($id) && $id > 0) { | |
2110 | $result = db_query($link, "SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
2111 | if (db_num_rows($result) == 1) { | |
2112 | return db_fetch_result($result, 0, "title"); | |
2113 | } else { | |
2114 | return "Unknown feed ($id)"; | |
2115 | } | |
2116 | } else { | |
2117 | return $id; | |
2118 | } | |
2119 | } | |
2120 | ||
2121 | function make_init_params($link) { | |
2122 | $params = array(); | |
2123 | ||
2124 | $params["theme"] = get_user_theme($link); | |
2125 | $params["theme_options"] = get_user_theme_options($link); | |
2126 | ||
2127 | $params["sign_progress"] = theme_image($link, "images/indicator_white.gif"); | |
2128 | $params["sign_progress_tiny"] = theme_image($link, "images/indicator_tiny.gif"); | |
2129 | $params["sign_excl"] = theme_image($link, "images/sign_excl.png"); | |
2130 | $params["sign_info"] = theme_image($link, "images/sign_info.png"); | |
2131 | ||
2132 | foreach (array("ON_CATCHUP_SHOW_NEXT_FEED", "HIDE_READ_FEEDS", | |
2133 | "ENABLE_FEED_CATS", "FEEDS_SORT_BY_UNREAD", "CONFIRM_FEED_CATCHUP", | |
2134 | "CDM_AUTO_CATCHUP", "FRESH_ARTICLE_MAX_AGE", "DEFAULT_ARTICLE_LIMIT", | |
2135 | "HIDE_READ_SHOWS_SPECIAL", "COMBINED_DISPLAY_MODE") as $param) { | |
2136 | ||
2137 | $params[strtolower($param)] = (int) get_pref($link, $param); | |
2138 | } | |
2139 | ||
2140 | $params["icons_url"] = ICONS_URL; | |
2141 | $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME; | |
2142 | $params["default_view_mode"] = get_pref($link, "_DEFAULT_VIEW_MODE"); | |
2143 | $params["default_view_limit"] = (int) get_pref($link, "_DEFAULT_VIEW_LIMIT"); | |
2144 | $params["default_view_order_by"] = get_pref($link, "_DEFAULT_VIEW_ORDER_BY"); | |
2145 | $params["bw_limit"] = (int) $_SESSION["bw_limit"]; | |
2146 | ||
2147 | $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM | |
2148 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
2149 | ||
2150 | $max_feed_id = db_fetch_result($result, 0, "mid"); | |
2151 | $num_feeds = db_fetch_result($result, 0, "nf"); | |
2152 | ||
2153 | $params["max_feed_id"] = (int) $max_feed_id; | |
2154 | $params["num_feeds"] = (int) $num_feeds; | |
2155 | ||
2156 | $params["collapsed_feedlist"] = (int) get_pref($link, "_COLLAPSED_FEEDLIST"); | |
2157 | ||
2158 | $params["csrf_token"] = $_SESSION["csrf_token"]; | |
2159 | ||
2160 | return $params; | |
2161 | } | |
2162 | ||
2163 | function make_runtime_info($link) { | |
2164 | $data = array(); | |
2165 | ||
2166 | $result = db_query($link, "SELECT MAX(id) AS mid, COUNT(*) AS nf FROM | |
2167 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
2168 | ||
2169 | $max_feed_id = db_fetch_result($result, 0, "mid"); | |
2170 | $num_feeds = db_fetch_result($result, 0, "nf"); | |
2171 | ||
2172 | $data["max_feed_id"] = (int) $max_feed_id; | |
2173 | $data["num_feeds"] = (int) $num_feeds; | |
2174 | ||
2175 | $data['last_article_id'] = getLastArticleId($link); | |
2176 | $data['cdm_expanded'] = get_pref($link, 'CDM_EXPANDED'); | |
2177 | ||
2178 | if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) { | |
2179 | ||
2180 | $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock"); | |
2181 | ||
2182 | if (time() - $_SESSION["daemon_stamp_check"] > 30) { | |
2183 | ||
2184 | $stamp = (int) @file_get_contents(LOCK_DIRECTORY . "/update_daemon.stamp"); | |
2185 | ||
2186 | if ($stamp) { | |
2187 | $stamp_delta = time() - $stamp; | |
2188 | ||
2189 | if ($stamp_delta > 1800) { | |
2190 | $stamp_check = 0; | |
2191 | } else { | |
2192 | $stamp_check = 1; | |
2193 | $_SESSION["daemon_stamp_check"] = time(); | |
2194 | } | |
2195 | ||
2196 | $data['daemon_stamp_ok'] = $stamp_check; | |
2197 | ||
2198 | $stamp_fmt = date("Y.m.d, G:i", $stamp); | |
2199 | ||
2200 | $data['daemon_stamp'] = $stamp_fmt; | |
2201 | } | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | if ($_SESSION["last_version_check"] + 86400 + rand(-1000, 1000) < time()) { | |
2206 | $new_version_details = @check_for_update($link); | |
2207 | ||
2208 | $data['new_version_available'] = (int) ($new_version_details != false); | |
2209 | ||
2210 | $_SESSION["last_version_check"] = time(); | |
2211 | } | |
2212 | ||
2213 | return $data; | |
2214 | } | |
2215 | ||
2216 | function search_to_sql($link, $search, $match_on) { | |
2217 | ||
2218 | $search_query_part = ""; | |
2219 | ||
2220 | $keywords = explode(" ", $search); | |
2221 | $query_keywords = array(); | |
2222 | ||
2223 | foreach ($keywords as $k) { | |
2224 | if (strpos($k, "-") === 0) { | |
2225 | $k = substr($k, 1); | |
2226 | $not = "NOT"; | |
2227 | } else { | |
2228 | $not = ""; | |
2229 | } | |
2230 | ||
2231 | $commandpair = explode(":", mb_strtolower($k), 2); | |
2232 | ||
2233 | if ($commandpair[0] == "note" && $commandpair[1]) { | |
2234 | ||
2235 | if ($commandpair[1] == "true") | |
2236 | array_push($query_keywords, "($not (note IS NOT NULL AND note != ''))"); | |
2237 | else | |
2238 | array_push($query_keywords, "($not (note IS NULL OR note = ''))"); | |
2239 | ||
2240 | } else if ($commandpair[0] == "star" && $commandpair[1]) { | |
2241 | ||
2242 | if ($commandpair[1] == "true") | |
2243 | array_push($query_keywords, "($not (marked = true))"); | |
2244 | else | |
2245 | array_push($query_keywords, "($not (marked = false))"); | |
2246 | ||
2247 | } else if ($commandpair[0] == "pub" && $commandpair[1]) { | |
2248 | ||
2249 | if ($commandpair[1] == "true") | |
2250 | array_push($query_keywords, "($not (published = true))"); | |
2251 | else | |
2252 | array_push($query_keywords, "($not (published = false))"); | |
2253 | ||
2254 | } else if (strpos($k, "@") === 0) { | |
2255 | ||
2256 | $user_tz_string = get_pref($link, 'USER_TIMEZONE', $_SESSION['uid']); | |
2257 | $orig_ts = strtotime(substr($k, 1)); | |
2258 | $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC')); | |
2259 | ||
2260 | //$k = date("Y-m-d", strtotime(substr($k, 1))); | |
2261 | ||
2262 | array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')"); | |
2263 | } else if ($match_on == "both") { | |
2264 | array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%') | |
2265 | OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))"); | |
2266 | } else if ($match_on == "title") { | |
2267 | array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%'))"); | |
2268 | } else if ($match_on == "content") { | |
2269 | array_push($query_keywords, "(UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))"); | |
2270 | } | |
2271 | } | |
2272 | ||
2273 | $search_query_part = implode("AND", $query_keywords); | |
2274 | ||
2275 | return $search_query_part; | |
2276 | } | |
2277 | ||
2278 | ||
2279 | function queryFeedHeadlines($link, $feed, $limit, $view_mode, $cat_view, $search, $search_mode, $match_on, $override_order = false, $offset = 0, $owner_uid = 0, $filter = false, $since_id = 0) { | |
2280 | ||
2281 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
2282 | ||
2283 | $ext_tables_part = ""; | |
2284 | ||
2285 | if ($search) { | |
2286 | ||
2287 | if (SPHINX_ENABLED) { | |
2288 | $ids = join(",", @sphinx_search($search, 0, 500)); | |
2289 | ||
2290 | if ($ids) | |
2291 | $search_query_part = "ref_id IN ($ids) AND "; | |
2292 | else | |
2293 | $search_query_part = "ref_id = -1 AND "; | |
2294 | ||
2295 | } else { | |
2296 | $search_query_part = search_to_sql($link, $search, $match_on); | |
2297 | $search_query_part .= " AND "; | |
2298 | } | |
2299 | ||
2300 | } else { | |
2301 | $search_query_part = ""; | |
2302 | } | |
2303 | ||
2304 | if ($filter) { | |
2305 | $filter_query_part = filter_to_sql($filter); | |
2306 | } else { | |
2307 | $filter_query_part = ""; | |
2308 | } | |
2309 | ||
2310 | if ($since_id) { | |
2311 | $since_id_part = "ttrss_entries.id > $since_id AND "; | |
2312 | } else { | |
2313 | $since_id_part = ""; | |
2314 | } | |
2315 | ||
2316 | $view_query_part = ""; | |
2317 | ||
2318 | if ($view_mode == "adaptive" || $view_query_part == "noscores") { | |
2319 | if ($search) { | |
2320 | $view_query_part = " "; | |
2321 | } else if ($feed != -1) { | |
2322 | $unread = getFeedUnread($link, $feed, $cat_view); | |
2323 | if ($unread > 0) { | |
2324 | $view_query_part = " unread = true AND "; | |
2325 | } | |
2326 | } | |
2327 | } | |
2328 | ||
2329 | if ($view_mode == "marked") { | |
2330 | $view_query_part = " marked = true AND "; | |
2331 | } | |
2332 | ||
2333 | if ($view_mode == "published") { | |
2334 | $view_query_part = " published = true AND "; | |
2335 | } | |
2336 | ||
2337 | if ($view_mode == "unread") { | |
2338 | $view_query_part = " unread = true AND "; | |
2339 | } | |
2340 | ||
2341 | if ($view_mode == "updated") { | |
2342 | $view_query_part = " (last_read is null and unread = false) AND "; | |
2343 | } | |
2344 | ||
2345 | if ($limit > 0) { | |
2346 | $limit_query_part = "LIMIT " . $limit; | |
2347 | } | |
2348 | ||
2349 | $vfeed_query_part = ""; | |
2350 | ||
2351 | // override query strategy and enable feed display when searching globally | |
2352 | if ($search && $search_mode == "all_feeds") { | |
2353 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2354 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2355 | /* tags */ | |
2356 | } else if (preg_match("/^-?[0-9][0-9]*$/", $feed) == false) { | |
2357 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2358 | $vfeed_query_part = "(SELECT title FROM ttrss_feeds WHERE | |
2359 | id = feed_id) as feed_title,"; | |
2360 | } else if ($feed > 0 && $search && $search_mode == "this_cat") { | |
2361 | ||
2362 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2363 | ||
2364 | $tmp_result = false; | |
2365 | ||
2366 | if ($cat_view) { | |
2367 | $tmp_result = db_query($link, "SELECT id | |
2368 | FROM ttrss_feeds WHERE cat_id = '$feed'"); | |
2369 | } else { | |
2370 | $tmp_result = db_query($link, "SELECT id | |
2371 | FROM ttrss_feeds WHERE cat_id = (SELECT cat_id FROM ttrss_feeds | |
2372 | WHERE id = '$feed') AND id != '$feed'"); | |
2373 | } | |
2374 | ||
2375 | $cat_siblings = array(); | |
2376 | ||
2377 | if (db_num_rows($tmp_result) > 0) { | |
2378 | while ($p = db_fetch_assoc($tmp_result)) { | |
2379 | array_push($cat_siblings, "feed_id = " . $p["id"]); | |
2380 | } | |
2381 | ||
2382 | $query_strategy_part = sprintf("(feed_id = %d OR %s)", | |
2383 | $feed, implode(" OR ", $cat_siblings)); | |
2384 | ||
2385 | } else { | |
2386 | $query_strategy_part = "ttrss_entries.id > 0"; | |
2387 | } | |
2388 | ||
2389 | } else if ($feed > 0) { | |
2390 | ||
2391 | if ($cat_view) { | |
2392 | ||
2393 | if ($feed > 0) { | |
2394 | $query_strategy_part = "cat_id = '$feed'"; | |
2395 | } else { | |
2396 | $query_strategy_part = "cat_id IS NULL"; | |
2397 | } | |
2398 | ||
2399 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2400 | ||
2401 | } else { | |
2402 | $query_strategy_part = "feed_id = '$feed'"; | |
2403 | } | |
2404 | } else if ($feed == 0 && !$cat_view) { // archive virtual feed | |
2405 | $query_strategy_part = "feed_id IS NULL"; | |
2406 | } else if ($feed == 0 && $cat_view) { // uncategorized | |
2407 | $query_strategy_part = "cat_id IS NULL AND feed_id IS NOT NULL"; | |
2408 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2409 | } else if ($feed == -1) { // starred virtual feed | |
2410 | $query_strategy_part = "marked = true"; | |
2411 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2412 | } else if ($feed == -2) { // published virtual feed OR labels category | |
2413 | ||
2414 | if (!$cat_view) { | |
2415 | $query_strategy_part = "published = true"; | |
2416 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2417 | } else { | |
2418 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2419 | ||
2420 | $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2"; | |
2421 | ||
2422 | $query_strategy_part = "ttrss_labels2.id = ttrss_user_labels2.label_id AND | |
2423 | ttrss_user_labels2.article_id = ref_id"; | |
2424 | ||
2425 | } | |
2426 | ||
2427 | } else if ($feed == -3) { // fresh virtual feed | |
2428 | $query_strategy_part = "unread = true AND score >= 0"; | |
2429 | ||
2430 | $intl = get_pref($link, "FRESH_ARTICLE_MAX_AGE", $owner_uid); | |
2431 | ||
2432 | if (DB_TYPE == "pgsql") { | |
2433 | $query_strategy_part .= " AND updated > NOW() - INTERVAL '$intl hour' "; | |
2434 | } else { | |
2435 | $query_strategy_part .= " AND updated > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; | |
2436 | } | |
2437 | ||
2438 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2439 | } else if ($feed == -4) { // all articles virtual feed | |
2440 | $query_strategy_part = "true"; | |
2441 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2442 | } else if ($feed <= -10) { // labels | |
2443 | $label_id = -$feed - 11; | |
2444 | ||
2445 | $query_strategy_part = "label_id = '$label_id' AND | |
2446 | ttrss_labels2.id = ttrss_user_labels2.label_id AND | |
2447 | ttrss_user_labels2.article_id = ref_id"; | |
2448 | ||
2449 | $vfeed_query_part = "ttrss_feeds.title AS feed_title,"; | |
2450 | $ext_tables_part = ",ttrss_labels2,ttrss_user_labels2"; | |
2451 | ||
2452 | } else { | |
2453 | $query_strategy_part = "id > 0"; // dumb | |
2454 | } | |
2455 | ||
2456 | if (get_pref($link, "SORT_HEADLINES_BY_FEED_DATE", $owner_uid)) { | |
2457 | $date_sort_field = "updated"; | |
2458 | } else { | |
2459 | $date_sort_field = "date_entered"; | |
2460 | } | |
2461 | ||
2462 | if (get_pref($link, 'REVERSE_HEADLINES', $owner_uid)) { | |
2463 | $order_by = "$date_sort_field"; | |
2464 | } else { | |
2465 | $order_by = "$date_sort_field DESC"; | |
2466 | } | |
2467 | ||
2468 | if ($view_mode != "noscores") { | |
2469 | $order_by = "score DESC, $order_by"; | |
2470 | } | |
2471 | ||
2472 | if ($override_order) { | |
2473 | $order_by = $override_order; | |
2474 | } | |
2475 | ||
2476 | $feed_title = ""; | |
2477 | ||
2478 | if ($search) { | |
2479 | $feed_title = "Search results"; | |
2480 | } else { | |
2481 | if ($cat_view) { | |
2482 | $feed_title = getCategoryTitle($link, $feed); | |
2483 | } else { | |
2484 | if (is_numeric($feed) && $feed > 0) { | |
2485 | $result = db_query($link, "SELECT title,site_url,last_error | |
2486 | FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = $owner_uid"); | |
2487 | ||
2488 | $feed_title = db_fetch_result($result, 0, "title"); | |
2489 | $feed_site_url = db_fetch_result($result, 0, "site_url"); | |
2490 | $last_error = db_fetch_result($result, 0, "last_error"); | |
2491 | } else { | |
2492 | $feed_title = getFeedTitle($link, $feed); | |
2493 | } | |
2494 | } | |
2495 | } | |
2496 | ||
2497 | $content_query_part = "content as content_preview,"; | |
2498 | ||
2499 | if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { | |
2500 | ||
2501 | if ($feed >= 0) { | |
2502 | $feed_kind = "Feeds"; | |
2503 | } else { | |
2504 | $feed_kind = "Labels"; | |
2505 | } | |
2506 | ||
2507 | if ($limit_query_part) { | |
2508 | $offset_query_part = "OFFSET $offset"; | |
2509 | } | |
2510 | ||
2511 | if ($vfeed_query_part && get_pref($link, 'VFEED_GROUP_BY_FEED', $owner_uid)) { | |
2512 | if (!$override_order) { | |
2513 | $order_by = "ttrss_feeds.title, $order_by"; | |
2514 | } | |
2515 | } | |
2516 | ||
2517 | if ($feed != "0") { | |
2518 | $from_qpart = "ttrss_entries,ttrss_user_entries,ttrss_feeds$ext_tables_part"; | |
2519 | $feed_check_qpart = "ttrss_user_entries.feed_id = ttrss_feeds.id AND"; | |
2520 | ||
2521 | } else { | |
2522 | $from_qpart = "ttrss_entries,ttrss_user_entries$ext_tables_part | |
2523 | LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)"; | |
2524 | } | |
2525 | ||
2526 | $query = "SELECT DISTINCT | |
2527 | date_entered, | |
2528 | guid, | |
2529 | ttrss_entries.id,ttrss_entries.title, | |
2530 | updated, | |
2531 | label_cache, | |
2532 | tag_cache, | |
2533 | always_display_enclosures, | |
2534 | site_url, | |
2535 | note, | |
2536 | num_comments, | |
2537 | comments, | |
2538 | int_id, | |
2539 | unread,feed_id,marked,published,link,last_read,orig_feed_id, | |
2540 | ".SUBSTRING_FOR_DATE."(last_read,1,19) as last_read_noms, | |
2541 | $vfeed_query_part | |
2542 | $content_query_part | |
2543 | ".SUBSTRING_FOR_DATE."(updated,1,19) as updated_noms, | |
2544 | author,score | |
2545 | FROM | |
2546 | $from_qpart | |
2547 | WHERE | |
2548 | $feed_check_qpart | |
2549 | ttrss_user_entries.ref_id = ttrss_entries.id AND | |
2550 | ttrss_user_entries.owner_uid = '$owner_uid' AND | |
2551 | $search_query_part | |
2552 | $filter_query_part | |
2553 | $view_query_part | |
2554 | $since_id_part | |
2555 | $query_strategy_part ORDER BY $order_by | |
2556 | $limit_query_part $offset_query_part"; | |
2557 | ||
2558 | if ($_REQUEST["debug"]) print $query; | |
2559 | ||
2560 | $result = db_query($link, $query); | |
2561 | ||
2562 | } else { | |
2563 | // browsing by tag | |
2564 | ||
2565 | $select_qpart = "SELECT DISTINCT " . | |
2566 | "date_entered," . | |
2567 | "guid," . | |
2568 | "note," . | |
2569 | "ttrss_entries.id as id," . | |
2570 | "title," . | |
2571 | "updated," . | |
2572 | "unread," . | |
2573 | "feed_id," . | |
2574 | "orig_feed_id," . | |
2575 | "marked," . | |
2576 | "num_comments, " . | |
2577 | "comments, " . | |
2578 | "tag_cache," . | |
2579 | "label_cache," . | |
2580 | "link," . | |
2581 | "last_read," . | |
2582 | SUBSTRING_FOR_DATE . "(last_read,1,19) as last_read_noms," . | |
2583 | $since_id_part . | |
2584 | $vfeed_query_part . | |
2585 | $content_query_part . | |
2586 | SUBSTRING_FOR_DATE . "(updated,1,19) as updated_noms," . | |
2587 | "score "; | |
2588 | ||
2589 | $feed_kind = "Tags"; | |
2590 | $all_tags = explode(",", $feed); | |
2591 | if ($search_mode == 'any') { | |
2592 | $tag_sql = "tag_name in (" . implode(", ", array_map("db_quote", $all_tags)) . ")"; | |
2593 | $from_qpart = " FROM ttrss_entries,ttrss_user_entries,ttrss_tags "; | |
2594 | $where_qpart = " WHERE " . | |
2595 | "ref_id = ttrss_entries.id AND " . | |
2596 | "ttrss_user_entries.owner_uid = $owner_uid AND " . | |
2597 | "post_int_id = int_id AND $tag_sql AND " . | |
2598 | $view_query_part . | |
2599 | $search_query_part . | |
2600 | $query_strategy_part . " ORDER BY $order_by " . | |
2601 | $limit_query_part; | |
2602 | ||
2603 | } else { | |
2604 | $i = 1; | |
2605 | $sub_selects = array(); | |
2606 | $sub_ands = array(); | |
2607 | foreach ($all_tags as $term) { | |
2608 | array_push($sub_selects, "(SELECT post_int_id from ttrss_tags WHERE tag_name = " . db_quote($term) . " AND owner_uid = $owner_uid) as A$i"); | |
2609 | $i++; | |
2610 | } | |
2611 | if ($i > 2) { | |
2612 | $x = 1; | |
2613 | $y = 2; | |
2614 | do { | |
2615 | array_push($sub_ands, "A$x.post_int_id = A$y.post_int_id"); | |
2616 | $x++; | |
2617 | $y++; | |
2618 | } while ($y < $i); | |
2619 | } | |
2620 | array_push($sub_ands, "A1.post_int_id = ttrss_user_entries.int_id and ttrss_user_entries.owner_uid = $owner_uid"); | |
2621 | array_push($sub_ands, "ttrss_user_entries.ref_id = ttrss_entries.id"); | |
2622 | $from_qpart = " FROM " . implode(", ", $sub_selects) . ", ttrss_user_entries, ttrss_entries"; | |
2623 | $where_qpart = " WHERE " . implode(" AND ", $sub_ands); | |
2624 | } | |
2625 | // error_log("TAG SQL: " . $tag_sql); | |
2626 | // $tag_sql = "tag_name = '$feed'"; DEFAULT way | |
2627 | ||
2628 | // error_log("[". $select_qpart . "][" . $from_qpart . "][" .$where_qpart . "]"); | |
2629 | $result = db_query($link, $select_qpart . $from_qpart . $where_qpart); | |
2630 | } | |
2631 | ||
2632 | return array($result, $feed_title, $feed_site_url, $last_error); | |
2633 | ||
2634 | } | |
2635 | ||
2636 | function sanitize($link, $str, $force_strip_tags = false, $owner = false, $site_url = false) { | |
2637 | global $purifier; | |
2638 | ||
2639 | if (!$owner) $owner = $_SESSION["uid"]; | |
2640 | ||
2641 | $res = trim($str); if (!$res) return ''; | |
2642 | ||
2643 | // create global Purifier object if needed | |
2644 | if (!$purifier) { | |
2645 | require_once 'lib/htmlpurifier/library/HTMLPurifier.auto.php'; | |
2646 | ||
2647 | $config = HTMLPurifier_Config::createDefault(); | |
2648 | ||
2649 | $allowed = "p,a[href],i,em,b,strong,code,pre,blockquote,br,img[src|alt|title|align|hspace],ul,ol,li,h1,h2,h3,h4,s,object[classid|type|id|name|width|height|codebase],param[name|value],table,tr,td,span[class]"; | |
2650 | ||
2651 | $config->set('HTML.SafeObject', true); | |
2652 | @$config->set('HTML', 'Allowed', $allowed); | |
2653 | $config->set('Output.FlashCompat', true); | |
2654 | $config->set('Attr.EnableID', true); | |
2655 | if (!defined('MOBILE_VERSION')) { | |
2656 | @$config->set('Cache', 'SerializerPath', CACHE_DIR . "/htmlpurifier"); | |
2657 | } else { | |
2658 | @$config->set('Cache', 'SerializerPath', "../" . CACHE_DIR . "/htmlpurifier"); | |
2659 | } | |
2660 | ||
2661 | $config->set('Filter.YouTube', true); | |
2662 | ||
2663 | $purifier = new HTMLPurifier($config); | |
2664 | } | |
2665 | ||
2666 | $res = $purifier->purify($res); | |
2667 | ||
2668 | if (get_pref($link, "STRIP_IMAGES", $owner)) { | |
2669 | $res = preg_replace('/<img[^>]+>/is', '', $res); | |
2670 | } | |
2671 | ||
2672 | if (strpos($res, "href=") === false) | |
2673 | $res = rewrite_urls($res); | |
2674 | ||
2675 | $charset_hack = '<head> | |
2676 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
2677 | </head>'; | |
2678 | ||
2679 | $res = trim($res); if (!$res) return ''; | |
2680 | ||
2681 | libxml_use_internal_errors(true); | |
2682 | ||
2683 | $doc = new DOMDocument(); | |
2684 | $doc->loadHTML($charset_hack . $res); | |
2685 | $xpath = new DOMXPath($doc); | |
2686 | ||
2687 | $entries = $xpath->query('(//a[@href]|//img[@src])'); | |
2688 | $br_inserted = 0; | |
2689 | ||
2690 | foreach ($entries as $entry) { | |
2691 | ||
2692 | if ($site_url) { | |
2693 | ||
2694 | if ($entry->hasAttribute('href')) | |
2695 | $entry->setAttribute('href', | |
2696 | rewrite_relative_url($site_url, $entry->getAttribute('href'))); | |
2697 | ||
2698 | if ($entry->hasAttribute('src')) | |
2699 | if (preg_match('/^image.php\?i=[a-z0-9]+$/', $entry->getAttribute('src')) == 0) | |
2700 | $entry->setAttribute('src', | |
2701 | rewrite_relative_url($site_url, $entry->getAttribute('src'))); | |
2702 | } | |
2703 | ||
2704 | if (strtolower($entry->nodeName) == "a") { | |
2705 | $entry->setAttribute("target", "_blank"); | |
2706 | } | |
2707 | ||
2708 | if (strtolower($entry->nodeName) == "img" && !$br_inserted) { | |
2709 | $br = $doc->createElement("br"); | |
2710 | ||
2711 | if ($entry->parentNode->nextSibling) { | |
2712 | $entry->parentNode->insertBefore($br, $entry->nextSibling); | |
2713 | $br_inserted = 1; | |
2714 | } | |
2715 | ||
2716 | } | |
2717 | } | |
2718 | ||
2719 | $node = $doc->getElementsByTagName('body')->item(0); | |
2720 | ||
2721 | return $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
2722 | } | |
2723 | ||
2724 | /** | |
2725 | * Send by mail a digest of last articles. | |
2726 | * | |
2727 | * @param mixed $link The database connection. | |
2728 | * @param integer $limit The maximum number of articles by digest. | |
2729 | * @return boolean Return false if digests are not enabled. | |
2730 | */ | |
2731 | function send_headlines_digests($link, $debug = false) { | |
2732 | ||
2733 | require_once 'lib/phpmailer/class.phpmailer.php'; | |
2734 | ||
2735 | $user_limit = 15; // amount of users to process (e.g. emails to send out) | |
2736 | $limit = 1000; // maximum amount of headlines to include | |
2737 | ||
2738 | if ($debug) _debug("Sending digests, batch of max $user_limit users, headline limit = $limit"); | |
2739 | ||
2740 | if (DB_TYPE == "pgsql") { | |
2741 | $interval_query = "last_digest_sent < NOW() - INTERVAL '1 days'"; | |
2742 | } else if (DB_TYPE == "mysql") { | |
2743 | $interval_query = "last_digest_sent < DATE_SUB(NOW(), INTERVAL 1 DAY)"; | |
2744 | } | |
2745 | ||
2746 | $result = db_query($link, "SELECT id,email FROM ttrss_users | |
2747 | WHERE email != '' AND (last_digest_sent IS NULL OR $interval_query)"); | |
2748 | ||
2749 | while ($line = db_fetch_assoc($result)) { | |
2750 | ||
2751 | if (get_pref($link, 'DIGEST_ENABLE', $line['id'], false)) { | |
2752 | $preferred_ts = strtotime(get_pref($link, 'DIGEST_PREFERRED_TIME', $line['id'], '00:00')); | |
2753 | ||
2754 | // try to send digests within 2 hours of preferred time | |
2755 | if ($preferred_ts && time() >= $preferred_ts && | |
2756 | time() - $preferred_ts <= 7200) { | |
2757 | ||
2758 | if ($debug) print "Sending digest for UID:" . $line['id'] . " - " . $line["email"] . " ... "; | |
2759 | ||
2760 | $do_catchup = get_pref($link, 'DIGEST_CATCHUP', $line['id'], false); | |
2761 | ||
2762 | global $tz_offset; | |
2763 | ||
2764 | // reset tz_offset global to prevent tz cache clash between users | |
2765 | $tz_offset = -1; | |
2766 | ||
2767 | $tuple = prepare_headlines_digest($link, $line["id"], 1, $limit); | |
2768 | $digest = $tuple[0]; | |
2769 | $headlines_count = $tuple[1]; | |
2770 | $affected_ids = $tuple[2]; | |
2771 | $digest_text = $tuple[3]; | |
2772 | ||
2773 | if ($headlines_count > 0) { | |
2774 | ||
2775 | $mail = new PHPMailer(); | |
2776 | ||
2777 | $mail->PluginDir = "lib/phpmailer/"; | |
2778 | $mail->SetLanguage("en", "lib/phpmailer/language/"); | |
2779 | ||
2780 | $mail->CharSet = "UTF-8"; | |
2781 | ||
2782 | $mail->From = SMTP_FROM_ADDRESS; | |
2783 | $mail->FromName = SMTP_FROM_NAME; | |
2784 | $mail->AddAddress($line["email"], $line["login"]); | |
2785 | ||
2786 | if (SMTP_HOST) { | |
2787 | $mail->Host = SMTP_HOST; | |
2788 | $mail->Mailer = "smtp"; | |
2789 | $mail->SMTPAuth = SMTP_LOGIN != ''; | |
2790 | $mail->Username = SMTP_LOGIN; | |
2791 | $mail->Password = SMTP_PASSWORD; | |
2792 | } | |
2793 | ||
2794 | $mail->IsHTML(true); | |
2795 | $mail->Subject = DIGEST_SUBJECT; | |
2796 | $mail->Body = $digest; | |
2797 | $mail->AltBody = $digest_text; | |
2798 | ||
2799 | $rc = $mail->Send(); | |
2800 | ||
2801 | if (!$rc && $debug) print "ERROR: " . $mail->ErrorInfo; | |
2802 | ||
2803 | if ($debug) print "RC=$rc\n"; | |
2804 | ||
2805 | if ($rc && $do_catchup) { | |
2806 | if ($debug) print "Marking affected articles as read...\n"; | |
2807 | catchupArticlesById($link, $affected_ids, 0, $line["id"]); | |
2808 | } | |
2809 | } else { | |
2810 | if ($debug) print "No headlines\n"; | |
2811 | } | |
2812 | ||
2813 | db_query($link, "UPDATE ttrss_users SET last_digest_sent = NOW() | |
2814 | WHERE id = " . $line["id"]); | |
2815 | ||
2816 | } | |
2817 | } | |
2818 | } | |
2819 | ||
2820 | if ($debug) _debug("All done."); | |
2821 | ||
2822 | } | |
2823 | ||
2824 | function prepare_headlines_digest($link, $user_id, $days = 1, $limit = 1000) { | |
2825 | ||
2826 | require_once "lib/MiniTemplator.class.php"; | |
2827 | ||
2828 | $tpl = new MiniTemplator; | |
2829 | $tpl_t = new MiniTemplator; | |
2830 | ||
2831 | $tpl->readTemplateFromFile("templates/digest_template_html.txt"); | |
2832 | $tpl_t->readTemplateFromFile("templates/digest_template.txt"); | |
2833 | ||
2834 | $user_tz_string = get_pref($link, 'USER_TIMEZONE', $user_id); | |
2835 | $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string); | |
2836 | ||
2837 | $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); | |
2838 | $tpl->setVariable('CUR_TIME', date('G:i', $local_ts)); | |
2839 | ||
2840 | $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); | |
2841 | $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts)); | |
2842 | ||
2843 | $affected_ids = array(); | |
2844 | ||
2845 | if (DB_TYPE == "pgsql") { | |
2846 | $interval_query = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'"; | |
2847 | } else if (DB_TYPE == "mysql") { | |
2848 | $interval_query = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
2849 | } | |
2850 | ||
2851 | $result = db_query($link, "SELECT ttrss_entries.title, | |
2852 | ttrss_feeds.title AS feed_title, | |
2853 | COALESCE(ttrss_feed_categories.title, '".__('Uncategorized')."') AS cat_title, | |
2854 | date_updated, | |
2855 | ttrss_user_entries.ref_id, | |
2856 | link, | |
2857 | score, | |
2858 | content, | |
2859 | ".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
2860 | FROM | |
2861 | ttrss_user_entries,ttrss_entries,ttrss_feeds | |
2862 | LEFT JOIN | |
2863 | ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id) | |
2864 | WHERE | |
2865 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id | |
2866 | AND include_in_digest = true | |
2867 | AND $interval_query | |
2868 | AND ttrss_user_entries.owner_uid = $user_id | |
2869 | AND unread = true | |
2870 | AND score >= 0 | |
2871 | ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC | |
2872 | LIMIT $limit"); | |
2873 | ||
2874 | $cur_feed_title = ""; | |
2875 | ||
2876 | $headlines_count = db_num_rows($result); | |
2877 | ||
2878 | $headlines = array(); | |
2879 | ||
2880 | while ($line = db_fetch_assoc($result)) { | |
2881 | array_push($headlines, $line); | |
2882 | } | |
2883 | ||
2884 | for ($i = 0; $i < sizeof($headlines); $i++) { | |
2885 | ||
2886 | $line = $headlines[$i]; | |
2887 | ||
2888 | array_push($affected_ids, $line["ref_id"]); | |
2889 | ||
2890 | $updated = make_local_datetime($link, $line['last_updated'], false, | |
2891 | $user_id); | |
2892 | ||
2893 | /* if ($line["score"] != 0) { | |
2894 | if ($line["score"] > 0) $line["score"] = '+' . $line["score"]; | |
2895 | ||
2896 | $line["title"] .= " (".$line['score'].")"; | |
2897 | } */ | |
2898 | ||
2899 | if (get_pref($link, 'ENABLE_FEED_CATS', $user_id)) { | |
2900 | $line['feed_title'] = $line['cat_title'] . " / " . $line['feed_title']; | |
2901 | } | |
2902 | ||
2903 | $tpl->setVariable('FEED_TITLE', $line["feed_title"]); | |
2904 | $tpl->setVariable('ARTICLE_TITLE', $line["title"]); | |
2905 | $tpl->setVariable('ARTICLE_LINK', $line["link"]); | |
2906 | $tpl->setVariable('ARTICLE_UPDATED', $updated); | |
2907 | $tpl->setVariable('ARTICLE_EXCERPT', | |
2908 | truncate_string(strip_tags($line["content"]), 300)); | |
2909 | // $tpl->setVariable('ARTICLE_CONTENT', | |
2910 | // strip_tags($article_content)); | |
2911 | ||
2912 | $tpl->addBlock('article'); | |
2913 | ||
2914 | $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]); | |
2915 | $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]); | |
2916 | $tpl_t->setVariable('ARTICLE_LINK', $line["link"]); | |
2917 | $tpl_t->setVariable('ARTICLE_UPDATED', $updated); | |
2918 | // $tpl_t->setVariable('ARTICLE_EXCERPT', | |
2919 | // truncate_string(strip_tags($line["excerpt"]), 100)); | |
2920 | ||
2921 | $tpl_t->addBlock('article'); | |
2922 | ||
2923 | if ($headlines[$i]['feed_title'] != $headlines[$i+1]['feed_title']) { | |
2924 | $tpl->addBlock('feed'); | |
2925 | $tpl_t->addBlock('feed'); | |
2926 | } | |
2927 | ||
2928 | } | |
2929 | ||
2930 | $tpl->addBlock('digest'); | |
2931 | $tpl->generateOutputToString($tmp); | |
2932 | ||
2933 | $tpl_t->addBlock('digest'); | |
2934 | $tpl_t->generateOutputToString($tmp_t); | |
2935 | ||
2936 | return array($tmp, $headlines_count, $affected_ids, $tmp_t); | |
2937 | } | |
2938 | ||
2939 | function check_for_update($link) { | |
2940 | if (CHECK_FOR_NEW_VERSION && $_SESSION['access_level'] >= 10) { | |
2941 | $version_url = "http://tt-rss.org/version.php?ver=" . VERSION; | |
2942 | ||
2943 | $version_data = @fetch_file_contents($version_url); | |
2944 | ||
2945 | if ($version_data) { | |
2946 | $version_data = json_decode($version_data, true); | |
2947 | if ($version_data && $version_data['version']) { | |
2948 | ||
2949 | if (version_compare(VERSION, $version_data['version']) == -1) { | |
2950 | return $version_data; | |
2951 | } | |
2952 | } | |
2953 | } | |
2954 | } | |
2955 | return false; | |
2956 | } | |
2957 | ||
2958 | function markArticlesById($link, $ids, $cmode) { | |
2959 | ||
2960 | $tmp_ids = array(); | |
2961 | ||
2962 | foreach ($ids as $id) { | |
2963 | array_push($tmp_ids, "ref_id = '$id'"); | |
2964 | } | |
2965 | ||
2966 | $ids_qpart = join(" OR ", $tmp_ids); | |
2967 | ||
2968 | if ($cmode == 0) { | |
2969 | db_query($link, "UPDATE ttrss_user_entries SET | |
2970 | marked = false,last_read = NOW() | |
2971 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2972 | } else if ($cmode == 1) { | |
2973 | db_query($link, "UPDATE ttrss_user_entries SET | |
2974 | marked = true | |
2975 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2976 | } else { | |
2977 | db_query($link, "UPDATE ttrss_user_entries SET | |
2978 | marked = NOT marked,last_read = NOW() | |
2979 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2980 | } | |
2981 | } | |
2982 | ||
2983 | function publishArticlesById($link, $ids, $cmode) { | |
2984 | ||
2985 | $tmp_ids = array(); | |
2986 | ||
2987 | foreach ($ids as $id) { | |
2988 | array_push($tmp_ids, "ref_id = '$id'"); | |
2989 | } | |
2990 | ||
2991 | $ids_qpart = join(" OR ", $tmp_ids); | |
2992 | ||
2993 | if ($cmode == 0) { | |
2994 | db_query($link, "UPDATE ttrss_user_entries SET | |
2995 | published = false,last_read = NOW() | |
2996 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
2997 | } else if ($cmode == 1) { | |
2998 | db_query($link, "UPDATE ttrss_user_entries SET | |
2999 | published = true | |
3000 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3001 | } else { | |
3002 | db_query($link, "UPDATE ttrss_user_entries SET | |
3003 | published = NOT published,last_read = NOW() | |
3004 | WHERE ($ids_qpart) AND owner_uid = " . $_SESSION["uid"]); | |
3005 | } | |
3006 | ||
3007 | if (PUBSUBHUBBUB_HUB) { | |
3008 | $rss_link = get_self_url_prefix() . | |
3009 | "/public.php?op=rss&id=-2&key=" . | |
3010 | get_feed_access_key($link, -2, false); | |
3011 | ||
3012 | $p = new Publisher(PUBSUBHUBBUB_HUB); | |
3013 | ||
3014 | $pubsub_result = $p->publish_update($rss_link); | |
3015 | } | |
3016 | } | |
3017 | ||
3018 | function catchupArticlesById($link, $ids, $cmode, $owner_uid = false) { | |
3019 | ||
3020 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
3021 | if (count($ids) == 0) return; | |
3022 | ||
3023 | $tmp_ids = array(); | |
3024 | ||
3025 | foreach ($ids as $id) { | |
3026 | array_push($tmp_ids, "ref_id = '$id'"); | |
3027 | } | |
3028 | ||
3029 | $ids_qpart = join(" OR ", $tmp_ids); | |
3030 | ||
3031 | if ($cmode == 0) { | |
3032 | db_query($link, "UPDATE ttrss_user_entries SET | |
3033 | unread = false,last_read = NOW() | |
3034 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3035 | } else if ($cmode == 1) { | |
3036 | db_query($link, "UPDATE ttrss_user_entries SET | |
3037 | unread = true | |
3038 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3039 | } else { | |
3040 | db_query($link, "UPDATE ttrss_user_entries SET | |
3041 | unread = NOT unread,last_read = NOW() | |
3042 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3043 | } | |
3044 | ||
3045 | /* update ccache */ | |
3046 | ||
3047 | $result = db_query($link, "SELECT DISTINCT feed_id FROM ttrss_user_entries | |
3048 | WHERE ($ids_qpart) AND owner_uid = $owner_uid"); | |
3049 | ||
3050 | while ($line = db_fetch_assoc($result)) { | |
3051 | ccache_update($link, $line["feed_id"], $owner_uid); | |
3052 | } | |
3053 | } | |
3054 | ||
3055 | function catchupArticleById($link, $id, $cmode) { | |
3056 | ||
3057 | if ($cmode == 0) { | |
3058 | db_query($link, "UPDATE ttrss_user_entries SET | |
3059 | unread = false,last_read = NOW() | |
3060 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3061 | } else if ($cmode == 1) { | |
3062 | db_query($link, "UPDATE ttrss_user_entries SET | |
3063 | unread = true | |
3064 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3065 | } else { | |
3066 | db_query($link, "UPDATE ttrss_user_entries SET | |
3067 | unread = NOT unread,last_read = NOW() | |
3068 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
3069 | } | |
3070 | ||
3071 | $feed_id = getArticleFeed($link, $id); | |
3072 | ccache_update($link, $feed_id, $_SESSION["uid"]); | |
3073 | } | |
3074 | ||
3075 | function make_guid_from_title($title) { | |
3076 | return preg_replace("/[ \"\',.:;]/", "-", | |
3077 | mb_strtolower(strip_tags($title), 'utf-8')); | |
3078 | } | |
3079 | ||
3080 | function get_article_tags($link, $id, $owner_uid = 0, $tag_cache = false) { | |
3081 | ||
3082 | global $memcache; | |
3083 | ||
3084 | $a_id = db_escape_string($id); | |
3085 | ||
3086 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
3087 | ||
3088 | $query = "SELECT DISTINCT tag_name, | |
3089 | owner_uid as owner FROM | |
3090 | ttrss_tags WHERE post_int_id = (SELECT int_id FROM ttrss_user_entries WHERE | |
3091 | ref_id = '$a_id' AND owner_uid = '$owner_uid' LIMIT 1) ORDER BY tag_name"; | |
3092 | ||
3093 | $obj_id = md5("TAGS:$owner_uid:$id"); | |
3094 | $tags = array(); | |
3095 | ||
3096 | if ($memcache && $obj = $memcache->get($obj_id)) { | |
3097 | $tags = $obj; | |
3098 | } else { | |
3099 | /* check cache first */ | |
3100 | ||
3101 | if ($tag_cache === false) { | |
3102 | $result = db_query($link, "SELECT tag_cache FROM ttrss_user_entries | |
3103 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
3104 | ||
3105 | $tag_cache = db_fetch_result($result, 0, "tag_cache"); | |
3106 | } | |
3107 | ||
3108 | if ($tag_cache) { | |
3109 | $tags = explode(",", $tag_cache); | |
3110 | } else { | |
3111 | ||
3112 | /* do it the hard way */ | |
3113 | ||
3114 | $tmp_result = db_query($link, $query); | |
3115 | ||
3116 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
3117 | array_push($tags, $tmp_line["tag_name"]); | |
3118 | } | |
3119 | ||
3120 | /* update the cache */ | |
3121 | ||
3122 | $tags_str = db_escape_string(join(",", $tags)); | |
3123 | ||
3124 | db_query($link, "UPDATE ttrss_user_entries | |
3125 | SET tag_cache = '$tags_str' WHERE ref_id = '$id' | |
3126 | AND owner_uid = $owner_uid"); | |
3127 | } | |
3128 | ||
3129 | if ($memcache) $memcache->add($obj_id, $tags, 0, 3600); | |
3130 | } | |
3131 | ||
3132 | return $tags; | |
3133 | } | |
3134 | ||
3135 | function trim_array($array) { | |
3136 | $tmp = $array; | |
3137 | array_walk($tmp, 'trim'); | |
3138 | return $tmp; | |
3139 | } | |
3140 | ||
3141 | function tag_is_valid($tag) { | |
3142 | if ($tag == '') return false; | |
3143 | if (preg_match("/^[0-9]*$/", $tag)) return false; | |
3144 | if (mb_strlen($tag) > 250) return false; | |
3145 | ||
3146 | if (function_exists('iconv')) { | |
3147 | $tag = iconv("utf-8", "utf-8", $tag); | |
3148 | } | |
3149 | ||
3150 | if (!$tag) return false; | |
3151 | ||
3152 | return true; | |
3153 | } | |
3154 | ||
3155 | function render_login_form($link, $mobile = 0) { | |
3156 | switch ($mobile) { | |
3157 | case 0: | |
3158 | require_once "login_form.php"; | |
3159 | break; | |
3160 | case 1: | |
3161 | require_once "mobile/login_form.php"; | |
3162 | break; | |
3163 | case 2: | |
3164 | require_once "mobile/classic/login_form.php"; | |
3165 | } | |
3166 | } | |
3167 | ||
3168 | // from http://developer.apple.com/internet/safari/faq.html | |
3169 | function no_cache_incantation() { | |
3170 | header("Expires: Mon, 22 Dec 1980 00:00:00 GMT"); // Happy birthday to me :) | |
3171 | header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified | |
3172 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1 | |
3173 | header("Cache-Control: post-check=0, pre-check=0", false); | |
3174 | header("Pragma: no-cache"); // HTTP/1.0 | |
3175 | } | |
3176 | ||
3177 | function format_warning($msg, $id = "") { | |
3178 | global $link; | |
3179 | return "<div class=\"warning\" id=\"$id\"> | |
3180 | <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>"; | |
3181 | } | |
3182 | ||
3183 | function format_notice($msg, $id = "") { | |
3184 | global $link; | |
3185 | return "<div class=\"notice\" id=\"$id\"> | |
3186 | <img src=\"".theme_image($link, "images/sign_info.png")."\">$msg</div>"; | |
3187 | } | |
3188 | ||
3189 | function format_error($msg, $id = "") { | |
3190 | global $link; | |
3191 | return "<div class=\"error\" id=\"$id\"> | |
3192 | <img src=\"".theme_image($link, "images/sign_excl.png")."\">$msg</div>"; | |
3193 | } | |
3194 | ||
3195 | function print_notice($msg) { | |
3196 | return print format_notice($msg); | |
3197 | } | |
3198 | ||
3199 | function print_warning($msg) { | |
3200 | return print format_warning($msg); | |
3201 | } | |
3202 | ||
3203 | function print_error($msg) { | |
3204 | return print format_error($msg); | |
3205 | } | |
3206 | ||
3207 | ||
3208 | function T_sprintf() { | |
3209 | $args = func_get_args(); | |
3210 | return vsprintf(__(array_shift($args)), $args); | |
3211 | } | |
3212 | ||
3213 | function format_inline_player($link, $url, $ctype) { | |
3214 | ||
3215 | $entry = ""; | |
3216 | ||
3217 | if (strpos($ctype, "audio/") === 0) { | |
3218 | ||
3219 | if ($_SESSION["hasAudio"] && (strpos($ctype, "ogg") !== false || | |
3220 | strpos($_SERVER['HTTP_USER_AGENT'], "Chrome") !== false || | |
3221 | strpos($_SERVER['HTTP_USER_AGENT'], "Safari") !== false )) { | |
3222 | ||
3223 | $id = 'AUDIO-' . uniqid(); | |
3224 | ||
3225 | $entry .= "<audio id=\"$id\"\"> | |
3226 | <source src=\"$url\"></source> | |
3227 | </audio>"; | |
3228 | ||
3229 | $entry .= "<span onclick=\"player(this)\" | |
3230 | title=\"".__("Click to play")."\" status=\"0\" | |
3231 | class=\"player\" audio-id=\"$id\">".__("Play")."</span>"; | |
3232 | ||
3233 | } else { | |
3234 | ||
3235 | $entry .= "<object type=\"application/x-shockwave-flash\" | |
3236 | data=\"lib/button/musicplayer.swf?song_url=$url\" | |
3237 | width=\"17\" height=\"17\" style='float : left; margin-right : 5px;'> | |
3238 | <param name=\"movie\" | |
3239 | value=\"lib/button/musicplayer.swf?song_url=$url\" /> | |
3240 | </object>"; | |
3241 | } | |
3242 | } | |
3243 | ||
3244 | $filename = substr($url, strrpos($url, "/")+1); | |
3245 | ||
3246 | $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
3247 | $filename . " (" . $ctype . ")" . "</a>"; | |
3248 | ||
3249 | return $entry; | |
3250 | } | |
3251 | ||
3252 | function format_article($link, $id, $mark_as_read = true, $zoom_mode = false, $owner_uid = false) { | |
3253 | ||
3254 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
3255 | ||
3256 | $rv = array(); | |
3257 | ||
3258 | $rv['id'] = $id; | |
3259 | ||
3260 | /* we can figure out feed_id from article id anyway, why do we | |
3261 | * pass feed_id here? let's ignore the argument :( */ | |
3262 | ||
3263 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
3264 | WHERE ref_id = '$id'"); | |
3265 | ||
3266 | $feed_id = (int) db_fetch_result($result, 0, "feed_id"); | |
3267 | ||
3268 | $rv['feed_id'] = $feed_id; | |
3269 | ||
3270 | //if (!$zoom_mode) { print "<article id='$id'><![CDATA["; }; | |
3271 | ||
3272 | $result = db_query($link, "SELECT rtl_content, always_display_enclosures FROM ttrss_feeds | |
3273 | WHERE id = '$feed_id' AND owner_uid = $owner_uid"); | |
3274 | ||
3275 | if (db_num_rows($result) == 1) { | |
3276 | $rtl_content = sql_bool_to_bool(db_fetch_result($result, 0, "rtl_content")); | |
3277 | $always_display_enclosures = sql_bool_to_bool(db_fetch_result($result, 0, "always_display_enclosures")); | |
3278 | } else { | |
3279 | $rtl_content = false; | |
3280 | $always_display_enclosures = false; | |
3281 | } | |
3282 | ||
3283 | if ($rtl_content) { | |
3284 | $rtl_tag = "dir=\"RTL\""; | |
3285 | $rtl_class = "RTL"; | |
3286 | } else { | |
3287 | $rtl_tag = ""; | |
3288 | $rtl_class = ""; | |
3289 | } | |
3290 | ||
3291 | if ($mark_as_read) { | |
3292 | $result = db_query($link, "UPDATE ttrss_user_entries | |
3293 | SET unread = false,last_read = NOW() | |
3294 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
3295 | ||
3296 | ccache_update($link, $feed_id, $owner_uid); | |
3297 | } | |
3298 | ||
3299 | $result = db_query($link, "SELECT title,link,content,feed_id,comments,int_id, | |
3300 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, | |
3301 | (SELECT icon_url FROM ttrss_feeds WHERE id = feed_id) as icon_url, | |
3302 | (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) as site_url, | |
3303 | num_comments, | |
3304 | tag_cache, | |
3305 | author, | |
3306 | orig_feed_id, | |
3307 | note | |
3308 | FROM ttrss_entries,ttrss_user_entries | |
3309 | WHERE id = '$id' AND ref_id = id AND owner_uid = $owner_uid"); | |
3310 | ||
3311 | if ($result) { | |
3312 | ||
3313 | $line = db_fetch_assoc($result); | |
3314 | ||
3315 | if ($line["icon_url"]) { | |
3316 | $feed_icon = "<img src=\"" . $line["icon_url"] . "\">"; | |
3317 | } else { | |
3318 | $feed_icon = " "; | |
3319 | } | |
3320 | ||
3321 | $feed_site_url = $line['site_url']; | |
3322 | ||
3323 | $num_comments = $line["num_comments"]; | |
3324 | $entry_comments = ""; | |
3325 | ||
3326 | if ($num_comments > 0) { | |
3327 | if ($line["comments"]) { | |
3328 | $comments_url = $line["comments"]; | |
3329 | } else { | |
3330 | $comments_url = $line["link"]; | |
3331 | } | |
3332 | $entry_comments = "<a target='_blank' href=\"$comments_url\">$num_comments comments</a>"; | |
3333 | } else { | |
3334 | if ($line["comments"] && $line["link"] != $line["comments"]) { | |
3335 | $entry_comments = "<a target='_blank' href=\"".$line["comments"]."\">comments</a>"; | |
3336 | } | |
3337 | } | |
3338 | ||
3339 | if ($zoom_mode) { | |
3340 | header("Content-Type: text/html"); | |
3341 | $rv['content'] .= "<html><head> | |
3342 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
3343 | <title>Tiny Tiny RSS - ".$line["title"]."</title> | |
3344 | <link rel=\"stylesheet\" type=\"text/css\" href=\"tt-rss.css\"> | |
3345 | </head><body>"; | |
3346 | } | |
3347 | ||
3348 | $rv['content'] .= "<div id=\"PTITLE-$id\" style=\"display : none\">" . | |
3349 | truncate_string(strip_tags($line['title']), 15) . "</div>"; | |
3350 | ||
3351 | $rv['content'] .= "<div id=\"PTITLE-FULL-$id\" style=\"display : none\">" . | |
3352 | strip_tags($line['title']) . "</div>"; | |
3353 | ||
3354 | $rv['content'] .= "<div class=\"postReply\" id=\"POST-$id\">"; | |
3355 | ||
3356 | $rv['content'] .= "<div onclick=\"return postClicked(event, $id)\" | |
3357 | class=\"postHeader\" id=\"POSTHDR-$id\">"; | |
3358 | ||
3359 | $entry_author = $line["author"]; | |
3360 | ||
3361 | if ($entry_author) { | |
3362 | $entry_author = __(" - ") . $entry_author; | |
3363 | } | |
3364 | ||
3365 | $parsed_updated = make_local_datetime($link, $line["updated"], true, | |
3366 | $owner_uid, true); | |
3367 | ||
3368 | $rv['content'] .= "<div class=\"postDate$rtl_class\">$parsed_updated</div>"; | |
3369 | ||
3370 | if ($line["link"]) { | |
3371 | $rv['content'] .= "<div class='postTitle' clear='both'><a target='_blank' | |
3372 | title=\"".htmlspecialchars($line['title'])."\" | |
3373 | href=\"" . | |
3374 | $line["link"] . "\">" . | |
3375 | truncate_string($line["title"], 100) . | |
3376 | "<span class='author'>$entry_author</span></a></div>"; | |
3377 | } else { | |
3378 | $rv['content'] .= "<div class='postTitle' clear='both'>" . $line["title"] . "$entry_author</div>"; | |
3379 | } | |
3380 | ||
3381 | $tag_cache = $line["tag_cache"]; | |
3382 | ||
3383 | if (!$tag_cache) | |
3384 | $tags = get_article_tags($link, $id, $owner_uid); | |
3385 | else | |
3386 | $tags = explode(",", $tag_cache); | |
3387 | ||
3388 | $tags_str = format_tags_string($tags, $id); | |
3389 | $tags_str_full = join(", ", $tags); | |
3390 | ||
3391 | if (!$tags_str_full) $tags_str_full = __("no tags"); | |
3392 | ||
3393 | if (!$entry_comments) $entry_comments = " "; # placeholder | |
3394 | ||
3395 | $rv['content'] .= "<div class='postTags' style='float : right'> | |
3396 | <img src='".theme_image($link, 'images/tag.png')."' | |
3397 | class='tagsPic' alt='Tags' title='Tags'> "; | |
3398 | ||
3399 | if (!$zoom_mode) { | |
3400 | $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span> | |
3401 | <a title=\"".__('Edit tags for this article')."\" | |
3402 | href=\"#\" onclick=\"editArticleTags($id, $feed_id)\">(+)</a>"; | |
3403 | ||
3404 | $rv['content'] .= "<div dojoType=\"dijit.Tooltip\" | |
3405 | id=\"ATSTRTIP-$id\" connectId=\"ATSTR-$id\" | |
3406 | position=\"below\">$tags_str_full</div>"; | |
3407 | ||
3408 | $rv['content'] .= "<img src=\"".theme_image($link, 'images/art-zoom.png')."\" | |
3409 | class='tagsPic' style=\"cursor : pointer\" | |
3410 | onclick=\"postOpenInNewTab(event, $id)\" | |
3411 | alt='Zoom' title='".__('Open article in new tab')."'>"; | |
3412 | ||
3413 | $button_plugins = explode(",", ARTICLE_BUTTON_PLUGINS); | |
3414 | ||
3415 | foreach ($button_plugins as $p) { | |
3416 | $pclass = trim("${p}_button"); | |
3417 | ||
3418 | if (class_exists($pclass)) { | |
3419 | $plugin = new $pclass($link); | |
3420 | $rv['content'] .= $plugin->render($id, $line); | |
3421 | } | |
3422 | } | |
3423 | ||
3424 | $rv['content'] .= "<img src=\"".theme_image($link, 'images/digest_checkbox.png')."\" | |
3425 | class='tagsPic' style=\"cursor : pointer\" | |
3426 | onclick=\"closeArticlePanel($id)\" | |
3427 | title='".__('Close article')."'>"; | |
3428 | ||
3429 | } else { | |
3430 | $tags_str = strip_tags($tags_str); | |
3431 | $rv['content'] .= "<span id=\"ATSTR-$id\">$tags_str</span>"; | |
3432 | } | |
3433 | $rv['content'] .= "</div>"; | |
3434 | $rv['content'] .= "<div clear='both'>$entry_comments</div>"; | |
3435 | ||
3436 | if ($line["orig_feed_id"]) { | |
3437 | ||
3438 | $tmp_result = db_query($link, "SELECT * FROM ttrss_archived_feeds | |
3439 | WHERE id = ".$line["orig_feed_id"]); | |
3440 | ||
3441 | if (db_num_rows($tmp_result) != 0) { | |
3442 | ||
3443 | $rv['content'] .= "<div clear='both'>"; | |
3444 | $rv['content'] .= __("Originally from:"); | |
3445 | ||
3446 | $rv['content'] .= " "; | |
3447 | ||
3448 | $tmp_line = db_fetch_assoc($tmp_result); | |
3449 | ||
3450 | $rv['content'] .= "<a target='_blank' | |
3451 | href=' " . htmlspecialchars($tmp_line['site_url']) . "'>" . | |
3452 | $tmp_line['title'] . "</a>"; | |
3453 | ||
3454 | $rv['content'] .= " "; | |
3455 | ||
3456 | $rv['content'] .= "<a target='_blank' href='" . htmlspecialchars($tmp_line['feed_url']) . "'>"; | |
3457 | $rv['content'] .= "<img title='".__('Feed URL')."'class='tinyFeedIcon' src='images/pub_set.png'></a>"; | |
3458 | ||
3459 | $rv['content'] .= "</div>"; | |
3460 | } | |
3461 | } | |
3462 | ||
3463 | $rv['content'] .= "</div>"; | |
3464 | ||
3465 | $rv['content'] .= "<div id=\"POSTNOTE-$id\">"; | |
3466 | if ($line['note']) { | |
3467 | $rv['content'] .= format_article_note($id, $line['note']); | |
3468 | } | |
3469 | $rv['content'] .= "</div>"; | |
3470 | ||
3471 | $rv['content'] .= "<div class=\"postIcon\">" . | |
3472 | "<a target=\"_blank\" title=\"".__("Visit the website")."\"$ | |
3473 | href=\"".htmlspecialchars($feed_site_url)."\">". | |
3474 | $feed_icon . "</a></div>"; | |
3475 | ||
3476 | $rv['content'] .= "<div class=\"postContent\">"; | |
3477 | ||
3478 | $article_content = sanitize($link, $line["content"], false, $owner_uid, | |
3479 | $feed_site_url); | |
3480 | ||
3481 | $rv['content'] .= $article_content; | |
3482 | ||
3483 | $rv['content'] .= format_article_enclosures($link, $id, | |
3484 | $always_display_enclosures, $article_content); | |
3485 | ||
3486 | $rv['content'] .= "</div>"; | |
3487 | ||
3488 | $rv['content'] .= "</div>"; | |
3489 | ||
3490 | } | |
3491 | ||
3492 | if ($zoom_mode) { | |
3493 | $rv['content'] .= " | |
3494 | <div style=\"text-align : center\"> | |
3495 | <button onclick=\"return window.close()\">". | |
3496 | __("Close this window")."</button></div>"; | |
3497 | $rv['content'] .= "</body></html>"; | |
3498 | } | |
3499 | ||
3500 | return $rv; | |
3501 | ||
3502 | } | |
3503 | ||
3504 | function print_checkpoint($n, $s) { | |
3505 | $ts = getmicrotime(); | |
3506 | echo sprintf("<!-- CP[$n] %.4f seconds -->", $ts - $s); | |
3507 | return $ts; | |
3508 | } | |
3509 | ||
3510 | function sanitize_tag($tag) { | |
3511 | $tag = trim($tag); | |
3512 | ||
3513 | $tag = mb_strtolower($tag, 'utf-8'); | |
3514 | ||
3515 | $tag = preg_replace('/[\'\"\+\>\<]/', "", $tag); | |
3516 | ||
3517 | // $tag = str_replace('"', "", $tag); | |
3518 | // $tag = str_replace("+", " ", $tag); | |
3519 | $tag = str_replace("technorati tag: ", "", $tag); | |
3520 | ||
3521 | return $tag; | |
3522 | } | |
3523 | ||
3524 | function get_self_url_prefix() { | |
3525 | return SELF_URL_PATH; | |
3526 | } | |
3527 | ||
3528 | function opml_publish_url($link){ | |
3529 | ||
3530 | $url_path = get_self_url_prefix(); | |
3531 | $url_path .= "/opml.php?op=publish&key=" . | |
3532 | get_feed_access_key($link, 'OPML:Publish', false, $_SESSION["uid"]); | |
3533 | ||
3534 | return $url_path; | |
3535 | } | |
3536 | ||
3537 | /** | |
3538 | * Purge a feed contents, marked articles excepted. | |
3539 | * | |
3540 | * @param mixed $link The database connection. | |
3541 | * @param integer $id The id of the feed to purge. | |
3542 | * @return void | |
3543 | */ | |
3544 | function clear_feed_articles($link, $id) { | |
3545 | ||
3546 | if ($id != 0) { | |
3547 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
3548 | WHERE feed_id = '$id' AND marked = false AND owner_uid = " . $_SESSION["uid"]); | |
3549 | } else { | |
3550 | $result = db_query($link, "DELETE FROM ttrss_user_entries | |
3551 | WHERE feed_id IS NULL AND marked = false AND owner_uid = " . $_SESSION["uid"]); | |
3552 | } | |
3553 | ||
3554 | $result = db_query($link, "DELETE FROM ttrss_entries WHERE | |
3555 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
3556 | ||
3557 | ccache_update($link, $id, $_SESSION['uid']); | |
3558 | } // function clear_feed_articles | |
3559 | ||
3560 | /** | |
3561 | * Compute the Mozilla Firefox feed adding URL from server HOST and REQUEST_URI. | |
3562 | * | |
3563 | * @return string The Mozilla Firefox feed adding URL. | |
3564 | */ | |
3565 | function add_feed_url() { | |
3566 | //$url_path = ($_SERVER['HTTPS'] != "on" ? 'http://' : 'https://') . $_SERVER["HTTP_HOST"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); | |
3567 | ||
3568 | $url_path = get_self_url_prefix() . | |
3569 | "/backend.php?op=pref-feeds&quiet=1&method=add&feed_url=%s"; | |
3570 | return $url_path; | |
3571 | } // function add_feed_url | |
3572 | ||
3573 | function encrypt_password($pass, $salt = '', $mode2 = false) { | |
3574 | if ($salt && $mode2) { | |
3575 | return "MODE2:" . hash('sha256', $salt . $pass); | |
3576 | } else if ($salt) { | |
3577 | return "SHA1X:" . sha1("$salt:$pass"); | |
3578 | } else { | |
3579 | return "SHA1:" . sha1($pass); | |
3580 | } | |
3581 | } // function encrypt_password | |
3582 | ||
3583 | function sanitize_article_content($text) { | |
3584 | # we don't support CDATA sections in articles, they break our own escaping | |
3585 | $text = preg_replace("/\[\[CDATA/", "", $text); | |
3586 | $text = preg_replace("/\]\]\>/", "", $text); | |
3587 | return $text; | |
3588 | } | |
3589 | ||
3590 | function load_filters($link, $feed, $owner_uid, $action_id = false) { | |
3591 | $filters = array(); | |
3592 | ||
3593 | global $memcache; | |
3594 | ||
3595 | $obj_id = md5("FILTER:$feed:$owner_uid:$action_id"); | |
3596 | ||
3597 | if ($memcache && $obj = $memcache->get($obj_id)) { | |
3598 | ||
3599 | return $obj; | |
3600 | ||
3601 | } else { | |
3602 | ||
3603 | if ($action_id) $ftype_query_part = "action_id = '$action_id' AND"; | |
3604 | ||
3605 | $result = db_query($link, "SELECT reg_exp, | |
3606 | ttrss_filter_types.name AS name, | |
3607 | ttrss_filter_actions.name AS action, | |
3608 | inverse, | |
3609 | action_param, | |
3610 | filter_param | |
3611 | FROM ttrss_filters | |
3612 | LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = '$feed'), | |
3613 | ttrss_filter_types,ttrss_filter_actions | |
3614 | WHERE | |
3615 | enabled = true AND | |
3616 | $ftype_query_part | |
3617 | ttrss_filters.owner_uid = $owner_uid AND | |
3618 | ttrss_filter_types.id = filter_type AND | |
3619 | ttrss_filter_actions.id = action_id AND | |
3620 | ((cat_filter = true AND ttrss_feeds.cat_id = ttrss_filters.cat_id) OR | |
3621 | (cat_filter = true AND ttrss_feeds.cat_id IS NULL AND | |
3622 | ttrss_filters.cat_id IS NULL) OR | |
3623 | (cat_filter = false AND (feed_id IS NULL OR feed_id = '$feed'))) | |
3624 | ORDER BY reg_exp"); | |
3625 | ||
3626 | while ($line = db_fetch_assoc($result)) { | |
3627 | ||
3628 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); | |
3629 | $filter["reg_exp"] = $line["reg_exp"]; | |
3630 | $filter["action"] = $line["action"]; | |
3631 | $filter["action_param"] = $line["action_param"]; | |
3632 | $filter["filter_param"] = $line["filter_param"]; | |
3633 | $filter["inverse"] = sql_bool_to_bool($line["inverse"]); | |
3634 | ||
3635 | array_push($filters[$line["name"]], $filter); | |
3636 | } | |
3637 | ||
3638 | if ($memcache) $memcache->add($obj_id, $filters, 0, 3600*8); | |
3639 | ||
3640 | return $filters; | |
3641 | } | |
3642 | } | |
3643 | ||
3644 | function get_score_pic($score) { | |
3645 | if ($score > 100) { | |
3646 | return "score_high.png"; | |
3647 | } else if ($score > 0) { | |
3648 | return "score_half_high.png"; | |
3649 | } else if ($score < -100) { | |
3650 | return "score_low.png"; | |
3651 | } else if ($score < 0) { | |
3652 | return "score_half_low.png"; | |
3653 | } else { | |
3654 | return "score_neutral.png"; | |
3655 | } | |
3656 | } | |
3657 | ||
3658 | function feed_has_icon($id) { | |
3659 | return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 0; | |
3660 | } | |
3661 | ||
3662 | function init_connection($link) { | |
3663 | if ($link) { | |
3664 | ||
3665 | if (DB_TYPE == "pgsql") { | |
3666 | pg_query($link, "set client_encoding = 'UTF-8'"); | |
3667 | pg_set_client_encoding("UNICODE"); | |
3668 | pg_query($link, "set datestyle = 'ISO, european'"); | |
3669 | pg_query($link, "set TIME ZONE 0"); | |
3670 | } else { | |
3671 | db_query($link, "SET time_zone = '+0:0'"); | |
3672 | ||
3673 | if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) { | |
3674 | db_query($link, "SET NAMES " . MYSQL_CHARSET); | |
3675 | } | |
3676 | } | |
3677 | return true; | |
3678 | } else { | |
3679 | print "Unable to connect to database:" . db_last_error(); | |
3680 | return false; | |
3681 | } | |
3682 | } | |
3683 | ||
3684 | /* function ccache_zero($link, $feed_id, $owner_uid) { | |
3685 | db_query($link, "UPDATE ttrss_counters_cache SET | |
3686 | value = 0, updated = NOW() WHERE | |
3687 | feed_id = '$feed_id' AND owner_uid = '$owner_uid'"); | |
3688 | } */ | |
3689 | ||
3690 | function ccache_zero_all($link, $owner_uid) { | |
3691 | db_query($link, "UPDATE ttrss_counters_cache SET | |
3692 | value = 0 WHERE owner_uid = '$owner_uid'"); | |
3693 | ||
3694 | db_query($link, "UPDATE ttrss_cat_counters_cache SET | |
3695 | value = 0 WHERE owner_uid = '$owner_uid'"); | |
3696 | } | |
3697 | ||
3698 | function ccache_remove($link, $feed_id, $owner_uid, $is_cat = false) { | |
3699 | ||
3700 | if (!$is_cat) { | |
3701 | $table = "ttrss_counters_cache"; | |
3702 | } else { | |
3703 | $table = "ttrss_cat_counters_cache"; | |
3704 | } | |
3705 | ||
3706 | db_query($link, "DELETE FROM $table WHERE | |
3707 | feed_id = '$feed_id' AND owner_uid = '$owner_uid'"); | |
3708 | ||
3709 | } | |
3710 | ||
3711 | function ccache_update_all($link, $owner_uid) { | |
3712 | ||
3713 | if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid)) { | |
3714 | ||
3715 | $result = db_query($link, "SELECT feed_id FROM ttrss_cat_counters_cache | |
3716 | WHERE feed_id > 0 AND owner_uid = '$owner_uid'"); | |
3717 | ||
3718 | while ($line = db_fetch_assoc($result)) { | |
3719 | ccache_update($link, $line["feed_id"], $owner_uid, true); | |
3720 | } | |
3721 | ||
3722 | /* We have to manually include category 0 */ | |
3723 | ||
3724 | ccache_update($link, 0, $owner_uid, true); | |
3725 | ||
3726 | } else { | |
3727 | $result = db_query($link, "SELECT feed_id FROM ttrss_counters_cache | |
3728 | WHERE feed_id > 0 AND owner_uid = '$owner_uid'"); | |
3729 | ||
3730 | while ($line = db_fetch_assoc($result)) { | |
3731 | print ccache_update($link, $line["feed_id"], $owner_uid); | |
3732 | ||
3733 | } | |
3734 | ||
3735 | } | |
3736 | } | |
3737 | ||
3738 | function ccache_find($link, $feed_id, $owner_uid, $is_cat = false, | |
3739 | $no_update = false) { | |
3740 | ||
3741 | if (!is_numeric($feed_id)) return; | |
3742 | ||
3743 | if (!$is_cat) { | |
3744 | $table = "ttrss_counters_cache"; | |
3745 | if ($feed_id > 0) { | |
3746 | $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds | |
3747 | WHERE id = '$feed_id'"); | |
3748 | $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid"); | |
3749 | } | |
3750 | } else { | |
3751 | $table = "ttrss_cat_counters_cache"; | |
3752 | } | |
3753 | ||
3754 | if (DB_TYPE == "pgsql") { | |
3755 | $date_qpart = "updated > NOW() - INTERVAL '15 minutes'"; | |
3756 | } else if (DB_TYPE == "mysql") { | |
3757 | $date_qpart = "updated > DATE_SUB(NOW(), INTERVAL 15 MINUTE)"; | |
3758 | } | |
3759 | ||
3760 | $result = db_query($link, "SELECT value FROM $table | |
3761 | WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' | |
3762 | LIMIT 1"); | |
3763 | ||
3764 | if (db_num_rows($result) == 1) { | |
3765 | return db_fetch_result($result, 0, "value"); | |
3766 | } else { | |
3767 | if ($no_update) { | |
3768 | return -1; | |
3769 | } else { | |
3770 | return ccache_update($link, $feed_id, $owner_uid, $is_cat); | |
3771 | } | |
3772 | } | |
3773 | ||
3774 | } | |
3775 | ||
3776 | function ccache_update($link, $feed_id, $owner_uid, $is_cat = false, | |
3777 | $update_pcat = true) { | |
3778 | ||
3779 | if (!is_numeric($feed_id)) return; | |
3780 | ||
3781 | if (!$is_cat && $feed_id > 0) { | |
3782 | $tmp_result = db_query($link, "SELECT owner_uid FROM ttrss_feeds | |
3783 | WHERE id = '$feed_id'"); | |
3784 | $owner_uid = db_fetch_result($tmp_result, 0, "owner_uid"); | |
3785 | } | |
3786 | ||
3787 | $prev_unread = ccache_find($link, $feed_id, $owner_uid, $is_cat, true); | |
3788 | ||
3789 | /* When updating a label, all we need to do is recalculate feed counters | |
3790 | * because labels are not cached */ | |
3791 | ||
3792 | if ($feed_id < 0) { | |
3793 | ccache_update_all($link, $owner_uid); | |
3794 | return; | |
3795 | } | |
3796 | ||
3797 | if (!$is_cat) { | |
3798 | $table = "ttrss_counters_cache"; | |
3799 | } else { | |
3800 | $table = "ttrss_cat_counters_cache"; | |
3801 | } | |
3802 | ||
3803 | if ($is_cat && $feed_id >= 0) { | |
3804 | if ($feed_id != 0) { | |
3805 | $cat_qpart = "cat_id = '$feed_id'"; | |
3806 | } else { | |
3807 | $cat_qpart = "cat_id IS NULL"; | |
3808 | } | |
3809 | ||
3810 | /* Recalculate counters for child feeds */ | |
3811 | ||
3812 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
3813 | WHERE owner_uid = '$owner_uid' AND $cat_qpart"); | |
3814 | ||
3815 | while ($line = db_fetch_assoc($result)) { | |
3816 | ccache_update($link, $line["id"], $owner_uid, false, false); | |
3817 | } | |
3818 | ||
3819 | $result = db_query($link, "SELECT SUM(value) AS sv | |
3820 | FROM ttrss_counters_cache, ttrss_feeds | |
3821 | WHERE id = feed_id AND $cat_qpart AND | |
3822 | ttrss_feeds.owner_uid = '$owner_uid'"); | |
3823 | ||
3824 | $unread = (int) db_fetch_result($result, 0, "sv"); | |
3825 | ||
3826 | } else { | |
3827 | $unread = (int) getFeedArticles($link, $feed_id, $is_cat, true, $owner_uid); | |
3828 | } | |
3829 | ||
3830 | db_query($link, "BEGIN"); | |
3831 | ||
3832 | $result = db_query($link, "SELECT feed_id FROM $table | |
3833 | WHERE owner_uid = '$owner_uid' AND feed_id = '$feed_id' LIMIT 1"); | |
3834 | ||
3835 | if (db_num_rows($result) == 1) { | |
3836 | db_query($link, "UPDATE $table SET | |
3837 | value = '$unread', updated = NOW() WHERE | |
3838 | feed_id = '$feed_id' AND owner_uid = '$owner_uid'"); | |
3839 | ||
3840 | } else { | |
3841 | db_query($link, "INSERT INTO $table | |
3842 | (feed_id, value, owner_uid, updated) | |
3843 | VALUES | |
3844 | ($feed_id, $unread, $owner_uid, NOW())"); | |
3845 | } | |
3846 | ||
3847 | db_query($link, "COMMIT"); | |
3848 | ||
3849 | if ($feed_id > 0 && $prev_unread != $unread) { | |
3850 | ||
3851 | if (!$is_cat) { | |
3852 | ||
3853 | /* Update parent category */ | |
3854 | ||
3855 | if ($update_pcat) { | |
3856 | ||
3857 | $result = db_query($link, "SELECT cat_id FROM ttrss_feeds | |
3858 | WHERE owner_uid = '$owner_uid' AND id = '$feed_id'"); | |
3859 | ||
3860 | $cat_id = (int) db_fetch_result($result, 0, "cat_id"); | |
3861 | ||
3862 | ccache_update($link, $cat_id, $owner_uid, true); | |
3863 | ||
3864 | } | |
3865 | } | |
3866 | } else if ($feed_id < 0) { | |
3867 | ccache_update_all($link, $owner_uid); | |
3868 | } | |
3869 | ||
3870 | return $unread; | |
3871 | } | |
3872 | ||
3873 | /* function ccache_cleanup($link, $owner_uid) { | |
3874 | ||
3875 | if (DB_TYPE == "pgsql") { | |
3876 | db_query($link, "DELETE FROM ttrss_counters_cache AS c1 WHERE | |
3877 | (SELECT count(*) FROM ttrss_counters_cache AS c2 | |
3878 | WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1 | |
3879 | AND owner_uid = '$owner_uid'"); | |
3880 | ||
3881 | db_query($link, "DELETE FROM ttrss_cat_counters_cache AS c1 WHERE | |
3882 | (SELECT count(*) FROM ttrss_cat_counters_cache AS c2 | |
3883 | WHERE c1.feed_id = c2.feed_id AND c2.owner_uid = c1.owner_uid) > 1 | |
3884 | AND owner_uid = '$owner_uid'"); | |
3885 | } else { | |
3886 | db_query($link, "DELETE c1 FROM | |
3887 | ttrss_counters_cache AS c1, | |
3888 | ttrss_counters_cache AS c2 | |
3889 | WHERE | |
3890 | c1.owner_uid = '$owner_uid' AND | |
3891 | c1.owner_uid = c2.owner_uid AND | |
3892 | c1.feed_id = c2.feed_id"); | |
3893 | ||
3894 | db_query($link, "DELETE c1 FROM | |
3895 | ttrss_cat_counters_cache AS c1, | |
3896 | ttrss_cat_counters_cache AS c2 | |
3897 | WHERE | |
3898 | c1.owner_uid = '$owner_uid' AND | |
3899 | c1.owner_uid = c2.owner_uid AND | |
3900 | c1.feed_id = c2.feed_id"); | |
3901 | ||
3902 | } | |
3903 | } */ | |
3904 | ||
3905 | function label_find_id($link, $label, $owner_uid) { | |
3906 | $result = db_query($link, | |
3907 | "SELECT id FROM ttrss_labels2 WHERE caption = '$label' | |
3908 | AND owner_uid = '$owner_uid' LIMIT 1"); | |
3909 | ||
3910 | if (db_num_rows($result) == 1) { | |
3911 | return db_fetch_result($result, 0, "id"); | |
3912 | } else { | |
3913 | return 0; | |
3914 | } | |
3915 | } | |
3916 | ||
3917 | function get_article_labels($link, $id) { | |
3918 | global $memcache; | |
3919 | ||
3920 | $obj_id = md5("LABELS:$id:" . $_SESSION["uid"]); | |
3921 | ||
3922 | $rv = array(); | |
3923 | ||
3924 | if ($memcache && $obj = $memcache->get($obj_id)) { | |
3925 | return $obj; | |
3926 | } else { | |
3927 | ||
3928 | $result = db_query($link, "SELECT label_cache FROM | |
3929 | ttrss_user_entries WHERE ref_id = '$id' AND owner_uid = " . | |
3930 | $_SESSION["uid"]); | |
3931 | ||
3932 | $label_cache = db_fetch_result($result, 0, "label_cache"); | |
3933 | ||
3934 | if ($label_cache) { | |
3935 | ||
3936 | $label_cache = json_decode($label_cache, true); | |
3937 | ||
3938 | if ($label_cache["no-labels"] == 1) | |
3939 | return $rv; | |
3940 | else | |
3941 | return $label_cache; | |
3942 | } | |
3943 | ||
3944 | $result = db_query($link, | |
3945 | "SELECT DISTINCT label_id,caption,fg_color,bg_color | |
3946 | FROM ttrss_labels2, ttrss_user_labels2 | |
3947 | WHERE id = label_id | |
3948 | AND article_id = '$id' | |
3949 | AND owner_uid = ".$_SESSION["uid"] . " | |
3950 | ORDER BY caption"); | |
3951 | ||
3952 | while ($line = db_fetch_assoc($result)) { | |
3953 | $rk = array($line["label_id"], $line["caption"], $line["fg_color"], | |
3954 | $line["bg_color"]); | |
3955 | array_push($rv, $rk); | |
3956 | } | |
3957 | if ($memcache) $memcache->add($obj_id, $rv, 0, 3600); | |
3958 | ||
3959 | if (count($rv) > 0) | |
3960 | label_update_cache($link, $id, $rv); | |
3961 | else | |
3962 | label_update_cache($link, $id, array("no-labels" => 1)); | |
3963 | } | |
3964 | ||
3965 | return $rv; | |
3966 | } | |
3967 | ||
3968 | ||
3969 | function label_find_caption($link, $label, $owner_uid) { | |
3970 | $result = db_query($link, | |
3971 | "SELECT caption FROM ttrss_labels2 WHERE id = '$label' | |
3972 | AND owner_uid = '$owner_uid' LIMIT 1"); | |
3973 | ||
3974 | if (db_num_rows($result) == 1) { | |
3975 | return db_fetch_result($result, 0, "caption"); | |
3976 | } else { | |
3977 | return ""; | |
3978 | } | |
3979 | } | |
3980 | ||
3981 | function label_update_cache($link, $id, $labels = false, $force = false) { | |
3982 | ||
3983 | if ($force) | |
3984 | label_clear_cache($link, $id); | |
3985 | ||
3986 | if (!$labels) | |
3987 | $labels = get_article_labels($link, $id); | |
3988 | ||
3989 | $labels = db_escape_string(json_encode($labels)); | |
3990 | ||
3991 | db_query($link, "UPDATE ttrss_user_entries SET | |
3992 | label_cache = '$labels' WHERE ref_id = '$id'"); | |
3993 | ||
3994 | } | |
3995 | ||
3996 | function label_clear_cache($link, $id) { | |
3997 | ||
3998 | db_query($link, "UPDATE ttrss_user_entries SET | |
3999 | label_cache = '' WHERE ref_id = '$id'"); | |
4000 | ||
4001 | } | |
4002 | ||
4003 | function label_remove_article($link, $id, $label, $owner_uid) { | |
4004 | ||
4005 | $label_id = label_find_id($link, $label, $owner_uid); | |
4006 | ||
4007 | if (!$label_id) return; | |
4008 | ||
4009 | $result = db_query($link, | |
4010 | "DELETE FROM ttrss_user_labels2 | |
4011 | WHERE | |
4012 | label_id = '$label_id' AND | |
4013 | article_id = '$id'"); | |
4014 | ||
4015 | label_clear_cache($link, $id); | |
4016 | } | |
4017 | ||
4018 | function label_add_article($link, $id, $label, $owner_uid) { | |
4019 | ||
4020 | global $memcache; | |
4021 | ||
4022 | if ($memcache) { | |
4023 | $obj_id = md5("LABELS:$id:$owner_uid"); | |
4024 | $memcache->delete($obj_id); | |
4025 | } | |
4026 | ||
4027 | $label_id = label_find_id($link, $label, $owner_uid); | |
4028 | ||
4029 | if (!$label_id) return; | |
4030 | ||
4031 | $result = db_query($link, | |
4032 | "SELECT | |
4033 | article_id FROM ttrss_labels2, ttrss_user_labels2 | |
4034 | WHERE | |
4035 | label_id = id AND | |
4036 | label_id = '$label_id' AND | |
4037 | article_id = '$id' AND owner_uid = '$owner_uid' | |
4038 | LIMIT 1"); | |
4039 | ||
4040 | if (db_num_rows($result) == 0) { | |
4041 | db_query($link, "INSERT INTO ttrss_user_labels2 | |
4042 | (label_id, article_id) VALUES ('$label_id', '$id')"); | |
4043 | } | |
4044 | ||
4045 | label_clear_cache($link, $id); | |
4046 | ||
4047 | } | |
4048 | ||
4049 | function label_remove($link, $id, $owner_uid) { | |
4050 | global $memcache; | |
4051 | ||
4052 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
4053 | ||
4054 | if ($memcache) { | |
4055 | $obj_id = md5("LABELS:$id:$owner_uid"); | |
4056 | $memcache->delete($obj_id); | |
4057 | } | |
4058 | ||
4059 | db_query($link, "BEGIN"); | |
4060 | ||
4061 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 | |
4062 | WHERE id = '$id'"); | |
4063 | ||
4064 | $caption = db_fetch_result($result, 0, "caption"); | |
4065 | ||
4066 | $result = db_query($link, "DELETE FROM ttrss_labels2 WHERE id = '$id' | |
4067 | AND owner_uid = " . $owner_uid); | |
4068 | ||
4069 | if (db_affected_rows($link, $result) != 0 && $caption) { | |
4070 | ||
4071 | /* Remove access key for the label */ | |
4072 | ||
4073 | $ext_id = -11 - $id; | |
4074 | ||
4075 | db_query($link, "DELETE FROM ttrss_access_keys WHERE | |
4076 | feed_id = '$ext_id' AND owner_uid = $owner_uid"); | |
4077 | ||
4078 | /* Disable filters that reference label being removed */ | |
4079 | ||
4080 | db_query($link, "UPDATE ttrss_filters SET | |
4081 | enabled = false WHERE action_param = '$caption' | |
4082 | AND action_id = 7 | |
4083 | AND owner_uid = " . $owner_uid); | |
4084 | ||
4085 | /* Remove cached data */ | |
4086 | ||
4087 | db_query($link, "UPDATE ttrss_user_entries SET label_cache = '' | |
4088 | WHERE label_cache LIKE '%$caption%' AND owner_uid = " . $owner_uid); | |
4089 | ||
4090 | } | |
4091 | ||
4092 | db_query($link, "COMMIT"); | |
4093 | } | |
4094 | ||
4095 | function label_create($link, $caption, $fg_color = '', $bg_color = '', $owner_uid) { | |
4096 | ||
4097 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
4098 | ||
4099 | db_query($link, "BEGIN"); | |
4100 | ||
4101 | $result = false; | |
4102 | ||
4103 | $result = db_query($link, "SELECT id FROM ttrss_labels2 | |
4104 | WHERE caption = '$caption' AND owner_uid = $owner_uid"); | |
4105 | ||
4106 | if (db_num_rows($result) == 0) { | |
4107 | $result = db_query($link, | |
4108 | "INSERT INTO ttrss_labels2 (caption,owner_uid,fg_color,bg_color) | |
4109 | VALUES ('$caption', '$owner_uid', '$fg_color', '$bg_color')"); | |
4110 | ||
4111 | $result = db_affected_rows($link, $result) != 0; | |
4112 | } | |
4113 | ||
4114 | db_query($link, "COMMIT"); | |
4115 | ||
4116 | return $result; | |
4117 | } | |
4118 | ||
4119 | function format_tags_string($tags, $id) { | |
4120 | ||
4121 | $tags_str = ""; | |
4122 | $tags_nolinks_str = ""; | |
4123 | ||
4124 | $num_tags = 0; | |
4125 | ||
4126 | $tag_limit = 6; | |
4127 | ||
4128 | $formatted_tags = array(); | |
4129 | ||
4130 | foreach ($tags as $tag) { | |
4131 | $num_tags++; | |
4132 | $tag_escaped = str_replace("'", "\\'", $tag); | |
4133 | ||
4134 | if (mb_strlen($tag) > 30) { | |
4135 | $tag = truncate_string($tag, 30); | |
4136 | } | |
4137 | ||
4138 | $tag_str = "<a href=\"javascript:viewfeed('$tag_escaped')\">$tag</a>"; | |
4139 | ||
4140 | array_push($formatted_tags, $tag_str); | |
4141 | ||
4142 | $tmp_tags_str = implode(", ", $formatted_tags); | |
4143 | ||
4144 | if ($num_tags == $tag_limit || mb_strlen($tmp_tags_str) > 150) { | |
4145 | break; | |
4146 | } | |
4147 | } | |
4148 | ||
4149 | $tags_str = implode(", ", $formatted_tags); | |
4150 | ||
4151 | if ($num_tags < count($tags)) { | |
4152 | $tags_str .= ", …"; | |
4153 | } | |
4154 | ||
4155 | if ($num_tags == 0) { | |
4156 | $tags_str = __("no tags"); | |
4157 | } | |
4158 | ||
4159 | return $tags_str; | |
4160 | ||
4161 | } | |
4162 | ||
4163 | function format_article_labels($labels, $id) { | |
4164 | ||
4165 | $labels_str = ""; | |
4166 | ||
4167 | foreach ($labels as $l) { | |
4168 | $labels_str .= sprintf("<span class='hlLabelRef' | |
4169 | style='color : %s; background-color : %s'>%s</span>", | |
4170 | $l[2], $l[3], $l[1]); | |
4171 | } | |
4172 | ||
4173 | return $labels_str; | |
4174 | ||
4175 | } | |
4176 | ||
4177 | function format_article_note($id, $note) { | |
4178 | ||
4179 | $str = "<div class='articleNote' onclick=\"editArticleNote($id)\"> | |
4180 | <div class='noteEdit' onclick=\"editArticleNote($id)\">". | |
4181 | __('(edit note)')."</div>$note</div>"; | |
4182 | ||
4183 | return $str; | |
4184 | } | |
4185 | ||
4186 | function toggle_collapse_cat($link, $cat_id, $mode) { | |
4187 | if ($cat_id > 0) { | |
4188 | $mode = bool_to_sql_bool($mode); | |
4189 | ||
4190 | db_query($link, "UPDATE ttrss_feed_categories SET | |
4191 | collapsed = $mode WHERE id = '$cat_id' AND owner_uid = " . | |
4192 | $_SESSION["uid"]); | |
4193 | } else { | |
4194 | $pref_name = ''; | |
4195 | ||
4196 | switch ($cat_id) { | |
4197 | case -1: | |
4198 | $pref_name = '_COLLAPSED_SPECIAL'; | |
4199 | break; | |
4200 | case -2: | |
4201 | $pref_name = '_COLLAPSED_LABELS'; | |
4202 | break; | |
4203 | case 0: | |
4204 | $pref_name = '_COLLAPSED_UNCAT'; | |
4205 | break; | |
4206 | } | |
4207 | ||
4208 | if ($pref_name) { | |
4209 | if ($mode) { | |
4210 | set_pref($link, $pref_name, 'true'); | |
4211 | } else { | |
4212 | set_pref($link, $pref_name, 'false'); | |
4213 | } | |
4214 | } | |
4215 | } | |
4216 | } | |
4217 | ||
4218 | function remove_feed($link, $id, $owner_uid) { | |
4219 | ||
4220 | if ($id > 0) { | |
4221 | ||
4222 | /* save starred articles in Archived feed */ | |
4223 | ||
4224 | db_query($link, "BEGIN"); | |
4225 | ||
4226 | /* prepare feed if necessary */ | |
4227 | ||
4228 | $result = db_query($link, "SELECT id FROM ttrss_archived_feeds | |
4229 | WHERE id = '$id'"); | |
4230 | ||
4231 | if (db_num_rows($result) == 0) { | |
4232 | db_query($link, "INSERT INTO ttrss_archived_feeds | |
4233 | (id, owner_uid, title, feed_url, site_url) | |
4234 | SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds | |
4235 | WHERE id = '$id'"); | |
4236 | } | |
4237 | ||
4238 | db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL, | |
4239 | orig_feed_id = '$id' WHERE feed_id = '$id' AND | |
4240 | marked = true AND owner_uid = $owner_uid"); | |
4241 | ||
4242 | /* Remove access key for the feed */ | |
4243 | ||
4244 | db_query($link, "DELETE FROM ttrss_access_keys WHERE | |
4245 | feed_id = '$id' AND owner_uid = $owner_uid"); | |
4246 | ||
4247 | /* remove the feed */ | |
4248 | ||
4249 | db_query($link, "DELETE FROM ttrss_feeds | |
4250 | WHERE id = '$id' AND owner_uid = $owner_uid"); | |
4251 | ||
4252 | db_query($link, "COMMIT"); | |
4253 | ||
4254 | if (file_exists(ICONS_DIR . "/$id.ico")) { | |
4255 | unlink(ICONS_DIR . "/$id.ico"); | |
4256 | } | |
4257 | ||
4258 | ccache_remove($link, $id, $owner_uid); | |
4259 | ||
4260 | } else { | |
4261 | label_remove($link, -11-$id, $owner_uid); | |
4262 | ccache_remove($link, -11-$id, $owner_uid); | |
4263 | } | |
4264 | } | |
4265 | ||
4266 | function add_feed_category($link, $feed_cat) { | |
4267 | ||
4268 | if (!$feed_cat) return false; | |
4269 | ||
4270 | db_query($link, "BEGIN"); | |
4271 | ||
4272 | $result = db_query($link, | |
4273 | "SELECT id FROM ttrss_feed_categories | |
4274 | WHERE title = '$feed_cat' AND owner_uid = ".$_SESSION["uid"]); | |
4275 | ||
4276 | if (db_num_rows($result) == 0) { | |
4277 | ||
4278 | $result = db_query($link, | |
4279 | "INSERT INTO ttrss_feed_categories (owner_uid,title) | |
4280 | VALUES ('".$_SESSION["uid"]."', '$feed_cat')"); | |
4281 | ||
4282 | db_query($link, "COMMIT"); | |
4283 | ||
4284 | return true; | |
4285 | } | |
4286 | ||
4287 | return false; | |
4288 | } | |
4289 | ||
4290 | function remove_feed_category($link, $id, $owner_uid) { | |
4291 | ||
4292 | db_query($link, "DELETE FROM ttrss_feed_categories | |
4293 | WHERE id = '$id' AND owner_uid = $owner_uid"); | |
4294 | ||
4295 | ccache_remove($link, $id, $owner_uid, true); | |
4296 | } | |
4297 | ||
4298 | function archive_article($link, $id, $owner_uid) { | |
4299 | db_query($link, "BEGIN"); | |
4300 | ||
4301 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
4302 | WHERE ref_id = '$id' AND owner_uid = $owner_uid"); | |
4303 | ||
4304 | if (db_num_rows($result) != 0) { | |
4305 | ||
4306 | /* prepare the archived table */ | |
4307 | ||
4308 | $feed_id = (int) db_fetch_result($result, 0, "feed_id"); | |
4309 | ||
4310 | if ($feed_id) { | |
4311 | $result = db_query($link, "SELECT id FROM ttrss_archived_feeds | |
4312 | WHERE id = '$feed_id'"); | |
4313 | ||
4314 | if (db_num_rows($result) == 0) { | |
4315 | db_query($link, "INSERT INTO ttrss_archived_feeds | |
4316 | (id, owner_uid, title, feed_url, site_url) | |
4317 | SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds | |
4318 | WHERE id = '$feed_id'"); | |
4319 | } | |
4320 | ||
4321 | db_query($link, "UPDATE ttrss_user_entries | |
4322 | SET orig_feed_id = feed_id, feed_id = NULL | |
4323 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
4324 | } | |
4325 | } | |
4326 | ||
4327 | db_query($link, "COMMIT"); | |
4328 | } | |
4329 | ||
4330 | function getArticleFeed($link, $id) { | |
4331 | $result = db_query($link, "SELECT feed_id FROM ttrss_user_entries | |
4332 | WHERE ref_id = '$id' AND owner_uid = " . $_SESSION["uid"]); | |
4333 | ||
4334 | if (db_num_rows($result) != 0) { | |
4335 | return db_fetch_result($result, 0, "feed_id"); | |
4336 | } else { | |
4337 | return 0; | |
4338 | } | |
4339 | } | |
4340 | ||
4341 | /** | |
4342 | * Fixes incomplete URLs by prepending "http://". | |
4343 | * Also replaces feed:// with http://, and | |
4344 | * prepends a trailing slash if the url is a domain name only. | |
4345 | * | |
4346 | * @param string $url Possibly incomplete URL | |
4347 | * | |
4348 | * @return string Fixed URL. | |
4349 | */ | |
4350 | function fix_url($url) { | |
4351 | if (strpos($url, '://') === false) { | |
4352 | $url = 'http://' . $url; | |
4353 | } else if (substr($url, 0, 5) == 'feed:') { | |
4354 | $url = 'http:' . substr($url, 5); | |
4355 | } | |
4356 | ||
4357 | //prepend slash if the URL has no slash in it | |
4358 | // "http://www.example" -> "http://www.example/" | |
4359 | if (strpos($url, '/', strpos($url, ':') + 3) === false) { | |
4360 | $url .= '/'; | |
4361 | } | |
4362 | ||
4363 | if ($url != "http:///") | |
4364 | return $url; | |
4365 | else | |
4366 | return ''; | |
4367 | } | |
4368 | ||
4369 | function validate_feed_url($url) { | |
4370 | $parts = parse_url($url); | |
4371 | ||
4372 | return ($parts['scheme'] == 'http' || $parts['scheme'] == 'feed' || $parts['scheme'] == 'https'); | |
4373 | ||
4374 | } | |
4375 | ||
4376 | function get_article_enclosures($link, $id) { | |
4377 | ||
4378 | global $memcache; | |
4379 | ||
4380 | $query = "SELECT * FROM ttrss_enclosures | |
4381 | WHERE post_id = '$id' AND content_url != ''"; | |
4382 | ||
4383 | $obj_id = md5("ENCLOSURES:$id"); | |
4384 | ||
4385 | $rv = array(); | |
4386 | ||
4387 | if ($memcache && $obj = $memcache->get($obj_id)) { | |
4388 | $rv = $obj; | |
4389 | } else { | |
4390 | $result = db_query($link, $query); | |
4391 | ||
4392 | if (db_num_rows($result) > 0) { | |
4393 | while ($line = db_fetch_assoc($result)) { | |
4394 | array_push($rv, $line); | |
4395 | } | |
4396 | if ($memcache) $memcache->add($obj_id, $rv, 0, 3600); | |
4397 | } | |
4398 | } | |
4399 | ||
4400 | return $rv; | |
4401 | } | |
4402 | ||
4403 | function api_get_feeds($link, $cat_id, $unread_only, $limit, $offset) { | |
4404 | ||
4405 | $feeds = array(); | |
4406 | ||
4407 | /* Labels */ | |
4408 | ||
4409 | if ($cat_id == -4 || $cat_id == -2) { | |
4410 | $counters = getLabelCounters($link, true); | |
4411 | ||
4412 | foreach (array_values($counters) as $cv) { | |
4413 | ||
4414 | $unread = $cv["counter"]; | |
4415 | ||
4416 | if ($unread || !$unread_only) { | |
4417 | ||
4418 | $row = array( | |
4419 | "id" => $cv["id"], | |
4420 | "title" => $cv["description"], | |
4421 | "unread" => $cv["counter"], | |
4422 | "cat_id" => -2, | |
4423 | ); | |
4424 | ||
4425 | array_push($feeds, $row); | |
4426 | } | |
4427 | } | |
4428 | } | |
4429 | ||
4430 | /* Virtual feeds */ | |
4431 | ||
4432 | if ($cat_id == -4 || $cat_id == -1) { | |
4433 | foreach (array(-1, -2, -3, -4, 0) as $i) { | |
4434 | $unread = getFeedUnread($link, $i); | |
4435 | ||
4436 | if ($unread || !$unread_only) { | |
4437 | $title = getFeedTitle($link, $i); | |
4438 | ||
4439 | $row = array( | |
4440 | "id" => $i, | |
4441 | "title" => $title, | |
4442 | "unread" => $unread, | |
4443 | "cat_id" => -1, | |
4444 | ); | |
4445 | array_push($feeds, $row); | |
4446 | } | |
4447 | ||
4448 | } | |
4449 | } | |
4450 | ||
4451 | /* Real feeds */ | |
4452 | ||
4453 | if ($limit) { | |
4454 | $limit_qpart = "LIMIT $limit OFFSET $offset"; | |
4455 | } else { | |
4456 | $limit_qpart = ""; | |
4457 | } | |
4458 | ||
4459 | if ($cat_id == -4 || $cat_id == -3) { | |
4460 | $result = db_query($link, "SELECT | |
4461 | id, feed_url, cat_id, title, order_id, ". | |
4462 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
4463 | FROM ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"] . | |
4464 | " ORDER BY cat_id, title " . $limit_qpart); | |
4465 | } else { | |
4466 | ||
4467 | if ($cat_id) | |
4468 | $cat_qpart = "cat_id = '$cat_id'"; | |
4469 | else | |
4470 | $cat_qpart = "cat_id IS NULL"; | |
4471 | ||
4472 | $result = db_query($link, "SELECT | |
4473 | id, feed_url, cat_id, title, order_id, ". | |
4474 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
4475 | FROM ttrss_feeds WHERE | |
4476 | $cat_qpart AND owner_uid = " . $_SESSION["uid"] . | |
4477 | " ORDER BY cat_id, title " . $limit_qpart); | |
4478 | } | |
4479 | ||
4480 | while ($line = db_fetch_assoc($result)) { | |
4481 | ||
4482 | $unread = getFeedUnread($link, $line["id"]); | |
4483 | ||
4484 | $has_icon = feed_has_icon($line['id']); | |
4485 | ||
4486 | if ($unread || !$unread_only) { | |
4487 | ||
4488 | $row = array( | |
4489 | "feed_url" => $line["feed_url"], | |
4490 | "title" => $line["title"], | |
4491 | "id" => (int)$line["id"], | |
4492 | "unread" => (int)$unread, | |
4493 | "has_icon" => $has_icon, | |
4494 | "cat_id" => (int)$line["cat_id"], | |
4495 | "last_updated" => strtotime($line["last_updated"]), | |
4496 | "order_id" => (int) $line["order_id"], | |
4497 | ); | |
4498 | ||
4499 | array_push($feeds, $row); | |
4500 | } | |
4501 | } | |
4502 | ||
4503 | return $feeds; | |
4504 | } | |
4505 | ||
4506 | function api_get_headlines($link, $feed_id, $limit, $offset, | |
4507 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order, | |
4508 | $include_attachments, $since_id, | |
4509 | $search = "", $search_mode = "", $match_on = "") { | |
4510 | ||
4511 | $qfh_ret = queryFeedHeadlines($link, $feed_id, $limit, | |
4512 | $view_mode, $is_cat, $search, $search_mode, $match_on, | |
4513 | $order, $offset, 0, false, $since_id); | |
4514 | ||
4515 | $result = $qfh_ret[0]; | |
4516 | $feed_title = $qfh_ret[1]; | |
4517 | ||
4518 | $headlines = array(); | |
4519 | ||
4520 | while ($line = db_fetch_assoc($result)) { | |
4521 | $is_updated = ($line["last_read"] == "" && | |
4522 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
4523 | ||
4524 | $tags = explode(",", $line["tag_cache"]); | |
4525 | $labels = json_decode($line["label_cache"], true); | |
4526 | ||
4527 | //if (!$tags) $tags = get_article_tags($link, $line["id"]); | |
4528 | //if (!$labels) $labels = get_article_labels($link, $line["id"]); | |
4529 | ||
4530 | $headline_row = array( | |
4531 | "id" => (int)$line["id"], | |
4532 | "unread" => sql_bool_to_bool($line["unread"]), | |
4533 | "marked" => sql_bool_to_bool($line["marked"]), | |
4534 | "published" => sql_bool_to_bool($line["published"]), | |
4535 | "updated" => strtotime($line["updated"]), | |
4536 | "is_updated" => $is_updated, | |
4537 | "title" => $line["title"], | |
4538 | "link" => $line["link"], | |
4539 | "feed_id" => $line["feed_id"], | |
4540 | "tags" => $tags, | |
4541 | ); | |
4542 | ||
4543 | if ($include_attachments) | |
4544 | $headline_row['attachments'] = get_article_enclosures($link, | |
4545 | $line['id']); | |
4546 | ||
4547 | if ($show_excerpt) { | |
4548 | $excerpt = truncate_string(strip_tags($line["content_preview"]), 100); | |
4549 | $headline_row["excerpt"] = $excerpt; | |
4550 | } | |
4551 | ||
4552 | if ($show_content) { | |
4553 | $headline_row["content"] = $line["content_preview"]; | |
4554 | } | |
4555 | ||
4556 | // unify label output to ease parsing | |
4557 | if ($labels["no-labels"] == 1) $labels = array(); | |
4558 | ||
4559 | $headline_row["labels"] = $labels; | |
4560 | ||
4561 | $headline_row["feed_title"] = $line["feed_title"]; | |
4562 | ||
4563 | array_push($headlines, $headline_row); | |
4564 | } | |
4565 | ||
4566 | return $headlines; | |
4567 | } | |
4568 | ||
4569 | function generate_error_feed($link, $error) { | |
4570 | $reply = array(); | |
4571 | ||
4572 | $reply['headlines']['id'] = -6; | |
4573 | $reply['headlines']['is_cat'] = false; | |
4574 | ||
4575 | $reply['headlines']['toolbar'] = ''; | |
4576 | $reply['headlines']['content'] = "<div class='whiteBox'>". $error . "</div>"; | |
4577 | ||
4578 | $reply['headlines-info'] = array("count" => 0, | |
4579 | "vgroup_last_feed" => '', | |
4580 | "unread" => 0, | |
4581 | "disable_cache" => true); | |
4582 | ||
4583 | return $reply; | |
4584 | } | |
4585 | ||
4586 | ||
4587 | function generate_dashboard_feed($link) { | |
4588 | $reply = array(); | |
4589 | ||
4590 | $reply['headlines']['id'] = -5; | |
4591 | $reply['headlines']['is_cat'] = false; | |
4592 | ||
4593 | $reply['headlines']['toolbar'] = ''; | |
4594 | $reply['headlines']['content'] = "<div class='whiteBox'>".__('No feed selected.'); | |
4595 | ||
4596 | $reply['headlines']['content'] .= "<p class=\"small\"><span class=\"insensitive\">"; | |
4597 | ||
4598 | $result = db_query($link, "SELECT ".SUBSTRING_FOR_DATE."(MAX(last_updated), 1, 19) AS last_updated FROM ttrss_feeds | |
4599 | WHERE owner_uid = " . $_SESSION['uid']); | |
4600 | ||
4601 | $last_updated = db_fetch_result($result, 0, "last_updated"); | |
4602 | $last_updated = make_local_datetime($link, $last_updated, false); | |
4603 | ||
4604 | $reply['headlines']['content'] .= sprintf(__("Feeds last updated at %s"), $last_updated); | |
4605 | ||
4606 | $result = db_query($link, "SELECT COUNT(id) AS num_errors | |
4607 | FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]); | |
4608 | ||
4609 | $num_errors = db_fetch_result($result, 0, "num_errors"); | |
4610 | ||
4611 | if ($num_errors > 0) { | |
4612 | $reply['headlines']['content'] .= "<br/>"; | |
4613 | $reply['headlines']['content'] .= "<a class=\"insensitive\" href=\"#\" onclick=\"showFeedsWithErrors()\">". | |
4614 | __('Some feeds have update errors (click for details)')."</a>"; | |
4615 | } | |
4616 | $reply['headlines']['content'] .= "</span></p>"; | |
4617 | ||
4618 | $reply['headlines-info'] = array("count" => 0, | |
4619 | "vgroup_last_feed" => '', | |
4620 | "unread" => 0, | |
4621 | "disable_cache" => true); | |
4622 | ||
4623 | return $reply; | |
4624 | } | |
4625 | ||
4626 | function save_email_address($link, $email) { | |
4627 | // FIXME: implement persistent storage of emails | |
4628 | ||
4629 | if (!$_SESSION['stored_emails']) | |
4630 | $_SESSION['stored_emails'] = array(); | |
4631 | ||
4632 | if (!in_array($email, $_SESSION['stored_emails'])) | |
4633 | array_push($_SESSION['stored_emails'], $email); | |
4634 | } | |
4635 | ||
4636 | function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) { | |
4637 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
4638 | ||
4639 | $sql_is_cat = bool_to_sql_bool($is_cat); | |
4640 | ||
4641 | $result = db_query($link, "SELECT access_key FROM ttrss_access_keys | |
4642 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
4643 | AND owner_uid = " . $owner_uid); | |
4644 | ||
4645 | if (db_num_rows($result) == 1) { | |
4646 | $key = db_escape_string(sha1(uniqid(rand(), true))); | |
4647 | ||
4648 | db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key' | |
4649 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
4650 | AND owner_uid = " . $owner_uid); | |
4651 | ||
4652 | return $key; | |
4653 | ||
4654 | } else { | |
4655 | return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid); | |
4656 | } | |
4657 | } | |
4658 | ||
4659 | function get_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) { | |
4660 | ||
4661 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
4662 | ||
4663 | $sql_is_cat = bool_to_sql_bool($is_cat); | |
4664 | ||
4665 | $result = db_query($link, "SELECT access_key FROM ttrss_access_keys | |
4666 | WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat | |
4667 | AND owner_uid = " . $owner_uid); | |
4668 | ||
4669 | if (db_num_rows($result) == 1) { | |
4670 | return db_fetch_result($result, 0, "access_key"); | |
4671 | } else { | |
4672 | $key = db_escape_string(sha1(uniqid(rand(), true))); | |
4673 | ||
4674 | $result = db_query($link, "INSERT INTO ttrss_access_keys | |
4675 | (access_key, feed_id, is_cat, owner_uid) | |
4676 | VALUES ('$key', '$feed_id', $sql_is_cat, '$owner_uid')"); | |
4677 | ||
4678 | return $key; | |
4679 | } | |
4680 | return false; | |
4681 | } | |
4682 | ||
4683 | /** | |
4684 | * Extracts RSS/Atom feed URLs from the given HTML URL. | |
4685 | * | |
4686 | * @param string $url HTML page URL | |
4687 | * | |
4688 | * @return array Array of feeds. Key is the full URL, value the title | |
4689 | */ | |
4690 | function get_feeds_from_html($url, $login = false, $pass = false) | |
4691 | { | |
4692 | $url = fix_url($url); | |
4693 | $baseUrl = substr($url, 0, strrpos($url, '/') + 1); | |
4694 | ||
4695 | libxml_use_internal_errors(true); | |
4696 | ||
4697 | $content = @fetch_file_contents($url, false, $login, $pass); | |
4698 | ||
4699 | $doc = new DOMDocument(); | |
4700 | $doc->loadHTML($content); | |
4701 | $xpath = new DOMXPath($doc); | |
4702 | $entries = $xpath->query('/html/head/link[@rel="alternate"]'); | |
4703 | $feedUrls = array(); | |
4704 | foreach ($entries as $entry) { | |
4705 | if ($entry->hasAttribute('href')) { | |
4706 | $title = $entry->getAttribute('title'); | |
4707 | if ($title == '') { | |
4708 | $title = $entry->getAttribute('type'); | |
4709 | } | |
4710 | $feedUrl = rewrite_relative_url( | |
4711 | $baseUrl, $entry->getAttribute('href') | |
4712 | ); | |
4713 | $feedUrls[$feedUrl] = $title; | |
4714 | } | |
4715 | } | |
4716 | return $feedUrls; | |
4717 | } | |
4718 | ||
4719 | /** | |
4720 | * Checks if the content behind the given URL is a HTML file | |
4721 | * | |
4722 | * @param string $url URL to check | |
4723 | * | |
4724 | * @return boolean True if the URL contains HTML content | |
4725 | */ | |
4726 | function url_is_html($url, $login = false, $pass = false) { | |
4727 | $content = substr(fetch_file_contents($url, false, $login, $pass), 0, 1000); | |
4728 | ||
4729 | if (stripos($content, '<html>') === false | |
4730 | && stripos($content, '<html ') === false | |
4731 | ) { | |
4732 | return false; | |
4733 | } | |
4734 | ||
4735 | return true; | |
4736 | } | |
4737 | ||
4738 | function print_label_select($link, $name, $value, $attributes = "") { | |
4739 | ||
4740 | $result = db_query($link, "SELECT caption FROM ttrss_labels2 | |
4741 | WHERE owner_uid = '".$_SESSION["uid"]."' ORDER BY caption"); | |
4742 | ||
4743 | print "<select default=\"$value\" name=\"" . htmlspecialchars($name) . | |
4744 | "\" $attributes onchange=\"labelSelectOnChange(this)\" >"; | |
4745 | ||
4746 | while ($line = db_fetch_assoc($result)) { | |
4747 | ||
4748 | $issel = ($line["caption"] == $value) ? "selected=\"1\"" : ""; | |
4749 | ||
4750 | print "<option value=\"".htmlspecialchars($line["caption"])."\" | |
4751 | $issel>" . htmlspecialchars($line["caption"]) . "</option>"; | |
4752 | ||
4753 | } | |
4754 | ||
4755 | # print "<option value=\"ADD_LABEL\">" .__("Add label...") . "</option>"; | |
4756 | ||
4757 | print "</select>"; | |
4758 | ||
4759 | ||
4760 | } | |
4761 | ||
4762 | function format_article_enclosures($link, $id, $always_display_enclosures, | |
4763 | $article_content) { | |
4764 | ||
4765 | $result = get_article_enclosures($link, $id); | |
4766 | $rv = ''; | |
4767 | ||
4768 | if (count($result) > 0) { | |
4769 | ||
4770 | $entries_html = array(); | |
4771 | $entries = array(); | |
4772 | ||
4773 | foreach ($result as $line) { | |
4774 | ||
4775 | $url = $line["content_url"]; | |
4776 | $ctype = $line["content_type"]; | |
4777 | ||
4778 | if (!$ctype) $ctype = __("unknown type"); | |
4779 | ||
4780 | # $filename = substr($url, strrpos($url, "/")+1); | |
4781 | ||
4782 | $entry = format_inline_player($link, $url, $ctype); | |
4783 | ||
4784 | # $entry .= " <a target=\"_blank\" href=\"" . htmlspecialchars($url) . "\">" . | |
4785 | # $filename . " (" . $ctype . ")" . "</a>"; | |
4786 | ||
4787 | array_push($entries_html, $entry); | |
4788 | ||
4789 | $entry = array(); | |
4790 | ||
4791 | $entry["type"] = $ctype; | |
4792 | $entry["filename"] = $filename; | |
4793 | $entry["url"] = $url; | |
4794 | ||
4795 | array_push($entries, $entry); | |
4796 | } | |
4797 | ||
4798 | $rv .= "<div class=\"postEnclosures\">"; | |
4799 | ||
4800 | if (!get_pref($link, "STRIP_IMAGES")) { | |
4801 | if ($always_display_enclosures || | |
4802 | !preg_match("/<img/i", $article_content)) { | |
4803 | ||
4804 | foreach ($entries as $entry) { | |
4805 | ||
4806 | if (preg_match("/image/", $entry["type"]) || | |
4807 | preg_match("/\.(jpg|png|gif|bmp)/i", $entry["filename"])) { | |
4808 | ||
4809 | $rv .= "<p><img | |
4810 | alt=\"".htmlspecialchars($entry["filename"])."\" | |
4811 | src=\"" .htmlspecialchars($entry["url"]) . "\"/></p>"; | |
4812 | } | |
4813 | } | |
4814 | } | |
4815 | } | |
4816 | ||
4817 | if (count($entries) == 1) { | |
4818 | $rv .= __("Attachment:") . " "; | |
4819 | } else { | |
4820 | $rv .= __("Attachments:") . " "; | |
4821 | } | |
4822 | ||
4823 | $rv .= join(", ", $entries_html); | |
4824 | ||
4825 | $rv .= "</div>"; | |
4826 | } | |
4827 | ||
4828 | return $rv; | |
4829 | } | |
4830 | ||
4831 | function getLastArticleId($link) { | |
4832 | $result = db_query($link, "SELECT MAX(ref_id) AS id FROM ttrss_user_entries | |
4833 | WHERE owner_uid = " . $_SESSION["uid"]); | |
4834 | ||
4835 | if (db_num_rows($result) == 1) { | |
4836 | return db_fetch_result($result, 0, "id"); | |
4837 | } else { | |
4838 | return -1; | |
4839 | } | |
4840 | } | |
4841 | ||
4842 | function build_url($parts) { | |
4843 | return $parts['scheme'] . "://" . $parts['host'] . $parts['path']; | |
4844 | } | |
4845 | ||
4846 | /** | |
4847 | * Converts a (possibly) relative URL to a absolute one. | |
4848 | * | |
4849 | * @param string $url Base URL (i.e. from where the document is) | |
4850 | * @param string $rel_url Possibly relative URL in the document | |
4851 | * | |
4852 | * @return string Absolute URL | |
4853 | */ | |
4854 | function rewrite_relative_url($url, $rel_url) { | |
4855 | if (strpos($rel_url, "magnet:") === 0) { | |
4856 | return $rel_url; | |
4857 | } else if (strpos($rel_url, "://") !== false) { | |
4858 | return $rel_url; | |
4859 | } else if (strpos($rel_url, "/") === 0) | |
4860 | { | |
4861 | $parts = parse_url($url); | |
4862 | $parts['path'] = $rel_url; | |
4863 | ||
4864 | return build_url($parts); | |
4865 | ||
4866 | } else { | |
4867 | $parts = parse_url($url); | |
4868 | if (!isset($parts['path'])) { | |
4869 | $parts['path'] = '/'; | |
4870 | } | |
4871 | $dir = $parts['path']; | |
4872 | if (substr($dir, -1) !== '/') { | |
4873 | $dir = dirname($parts['path']); | |
4874 | $dir !== '/' && $dir .= '/'; | |
4875 | } | |
4876 | $parts['path'] = $dir . $rel_url; | |
4877 | ||
4878 | return build_url($parts); | |
4879 | } | |
4880 | } | |
4881 | ||
4882 | function sphinx_search($query, $offset = 0, $limit = 30) { | |
4883 | require_once 'lib/sphinxapi.php'; | |
4884 | ||
4885 | $sphinxClient = new SphinxClient(); | |
4886 | ||
4887 | $sphinxClient->SetServer('localhost', 9312); | |
4888 | $sphinxClient->SetConnectTimeout(1); | |
4889 | ||
4890 | $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, | |
4891 | 'feed_title' => 20)); | |
4892 | ||
4893 | $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); | |
4894 | $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); | |
4895 | $sphinxClient->SetLimits($offset, $limit, 1000); | |
4896 | $sphinxClient->SetArrayResult(false); | |
4897 | $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid'])); | |
4898 | ||
4899 | $result = $sphinxClient->Query($query, SPHINX_INDEX); | |
4900 | ||
4901 | $ids = array(); | |
4902 | ||
4903 | if (is_array($result['matches'])) { | |
4904 | foreach (array_keys($result['matches']) as $int_id) { | |
4905 | $ref_id = $result['matches'][$int_id]['attrs']['ref_id']; | |
4906 | array_push($ids, $ref_id); | |
4907 | } | |
4908 | } | |
4909 | ||
4910 | return $ids; | |
4911 | } | |
4912 | ||
4913 | function cleanup_tags($link, $days = 14, $limit = 1000) { | |
4914 | ||
4915 | if (DB_TYPE == "pgsql") { | |
4916 | $interval_query = "date_updated < NOW() - INTERVAL '$days days'"; | |
4917 | } else if (DB_TYPE == "mysql") { | |
4918 | $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)"; | |
4919 | } | |
4920 | ||
4921 | $tags_deleted = 0; | |
4922 | ||
4923 | while ($limit > 0) { | |
4924 | $limit_part = 500; | |
4925 | ||
4926 | $query = "SELECT ttrss_tags.id AS id | |
4927 | FROM ttrss_tags, ttrss_user_entries, ttrss_entries | |
4928 | WHERE post_int_id = int_id AND $interval_query AND | |
4929 | ref_id = ttrss_entries.id AND tag_cache != '' LIMIT $limit_part"; | |
4930 | ||
4931 | $result = db_query($link, $query); | |
4932 | ||
4933 | $ids = array(); | |
4934 | ||
4935 | while ($line = db_fetch_assoc($result)) { | |
4936 | array_push($ids, $line['id']); | |
4937 | } | |
4938 | ||
4939 | if (count($ids) > 0) { | |
4940 | $ids = join(",", $ids); | |
4941 | print "."; | |
4942 | ||
4943 | $tmp_result = db_query($link, "DELETE FROM ttrss_tags WHERE id IN ($ids)"); | |
4944 | $tags_deleted += db_affected_rows($link, $tmp_result); | |
4945 | } else { | |
4946 | break; | |
4947 | } | |
4948 | ||
4949 | $limit -= $limit_part; | |
4950 | } | |
4951 | ||
4952 | print "\n"; | |
4953 | ||
4954 | return $tags_deleted; | |
4955 | } | |
4956 | ||
4957 | function print_user_stylesheet($link) { | |
4958 | $value = get_pref($link, 'USER_STYLESHEET'); | |
4959 | ||
4960 | if ($value) { | |
4961 | print "<style type=\"text/css\">"; | |
4962 | print str_replace("<br/>", "\n", $value); | |
4963 | print "</style>"; | |
4964 | } | |
4965 | ||
4966 | } | |
4967 | ||
4968 | /* function rewrite_urls($line) { | |
4969 | global $url_regex; | |
4970 | ||
4971 | $urls = null; | |
4972 | ||
4973 | $result = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i", | |
4974 | "<a target=\"_blank\" href=\"\\1\">\\1</a>", $line); | |
4975 | ||
4976 | return $result; | |
4977 | } */ | |
4978 | ||
4979 | function rewrite_urls($html) { | |
4980 | libxml_use_internal_errors(true); | |
4981 | ||
4982 | $charset_hack = '<head> | |
4983 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
4984 | </head>'; | |
4985 | ||
4986 | $doc = new DOMDocument(); | |
4987 | $doc->loadHTML($charset_hack . $html); | |
4988 | $xpath = new DOMXPath($doc); | |
4989 | ||
4990 | $entries = $xpath->query('//*/text()'); | |
4991 | ||
4992 | foreach ($entries as $entry) { | |
4993 | if (strstr($entry->wholeText, "://") !== false) { | |
4994 | $text = preg_replace("/((?<!=.)((http|https|ftp)+):\/\/[^ ,!]+)/i", | |
4995 | "<a target=\"_blank\" href=\"\\1\">\\1</a>", $entry->wholeText); | |
4996 | ||
4997 | if ($text != $entry->wholeText) { | |
4998 | $cdoc = new DOMDocument(); | |
4999 | $cdoc->loadHTML($charset_hack . $text); | |
5000 | ||
5001 | ||
5002 | foreach ($cdoc->childNodes as $cnode) { | |
5003 | $cnode = $doc->importNode($cnode, true); | |
5004 | ||
5005 | if ($cnode) { | |
5006 | $entry->parentNode->insertBefore($cnode); | |
5007 | } | |
5008 | } | |
5009 | ||
5010 | $entry->parentNode->removeChild($entry); | |
5011 | ||
5012 | } | |
5013 | } | |
5014 | } | |
5015 | ||
5016 | $node = $doc->getElementsByTagName('body')->item(0); | |
5017 | ||
5018 | // http://tt-rss.org/forum/viewtopic.php?f=1&t=970 | |
5019 | if ($node) | |
5020 | return $doc->saveXML($node, LIBXML_NOEMPTYTAG); | |
5021 | else | |
5022 | return $html; | |
5023 | } | |
5024 | ||
5025 | function filter_to_sql($filter) { | |
5026 | $query = ""; | |
5027 | ||
5028 | $regexp_valid = preg_match('/' . $filter['reg_exp'] . '/', | |
5029 | $filter['reg_exp']) !== FALSE; | |
5030 | ||
5031 | if ($regexp_valid) { | |
5032 | ||
5033 | if (DB_TYPE == "pgsql") | |
5034 | $reg_qpart = "~"; | |
5035 | else | |
5036 | $reg_qpart = "REGEXP"; | |
5037 | ||
5038 | switch ($filter["type"]) { | |
5039 | case "title": | |
5040 | $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('". | |
5041 | $filter['reg_exp'] . "')"; | |
5042 | break; | |
5043 | case "content": | |
5044 | $query = "LOWER(ttrss_entries.content) $reg_qpart LOWER('". | |
5045 | $filter['reg_exp'] . "')"; | |
5046 | break; | |
5047 | case "both": | |
5048 | $query = "LOWER(ttrss_entries.title) $reg_qpart LOWER('". | |
5049 | $filter['reg_exp'] . "') OR LOWER(" . | |
5050 | "ttrss_entries.content) $reg_qpart LOWER('" . $filter['reg_exp'] . "')"; | |
5051 | break; | |
5052 | case "tag": | |
5053 | $query = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('". | |
5054 | $filter['reg_exp'] . "')"; | |
5055 | break; | |
5056 | case "link": | |
5057 | $query = "LOWER(ttrss_entries.link) $reg_qpart LOWER('". | |
5058 | $filter['reg_exp'] . "')"; | |
5059 | break; | |
5060 | case "date": | |
5061 | ||
5062 | if ($filter["filter_param"] == "before") | |
5063 | $cmp_qpart = "<"; | |
5064 | else | |
5065 | $cmp_qpart = ">="; | |
5066 | ||
5067 | $timestamp = date("Y-m-d H:N:s", strtotime($filter["reg_exp"])); | |
5068 | $query = "ttrss_entries.date_entered $cmp_qpart '$timestamp'"; | |
5069 | break; | |
5070 | case "author": | |
5071 | $query = "LOWER(ttrss_entries.author) $reg_qpart LOWER('". | |
5072 | $filter['reg_exp'] . "')"; | |
5073 | break; | |
5074 | } | |
5075 | ||
5076 | if ($filter["inverse"]) | |
5077 | $query = "NOT ($query)"; | |
5078 | ||
5079 | if ($query) { | |
5080 | if (DB_TYPE == "pgsql") { | |
5081 | $query = " ($query) AND ttrss_entries.date_entered > NOW() - INTERVAL '14 days'"; | |
5082 | } else { | |
5083 | $query = " ($query) AND ttrss_entries.date_entered > DATE_SUB(NOW(), INTERVAL 14 DAY)"; | |
5084 | } | |
5085 | $query .= " AND "; | |
5086 | } | |
5087 | ||
5088 | return $query; | |
5089 | } else { | |
5090 | return false; | |
5091 | } | |
5092 | } | |
5093 | ||
5094 | // Status codes: | |
5095 | // -1 - never connected | |
5096 | // 0 - no data received | |
5097 | // 1 - data received successfully | |
5098 | // 2 - did not receive valid data | |
5099 | // >10 - server error, code + 10 (e.g. 16 means server error 6) | |
5100 | ||
5101 | function get_linked_feeds($link, $instance_id = false) { | |
5102 | if ($instance_id) | |
5103 | $instance_qpart = "id = '$instance_id' AND "; | |
5104 | else | |
5105 | $instance_qpart = ""; | |
5106 | ||
5107 | if (DB_TYPE == "pgsql") { | |
5108 | $date_qpart = "last_connected < NOW() - INTERVAL '6 hours'"; | |
5109 | } else { | |
5110 | $date_qpart = "last_connected < DATE_SUB(NOW(), INTERVAL 6 HOUR)"; | |
5111 | } | |
5112 | ||
5113 | $result = db_query($link, "SELECT id, access_key, access_url FROM ttrss_linked_instances | |
5114 | WHERE $instance_qpart $date_qpart ORDER BY last_connected"); | |
5115 | ||
5116 | while ($line = db_fetch_assoc($result)) { | |
5117 | $id = $line['id']; | |
5118 | ||
5119 | _debug("Updating: " . $line['access_url'] . " ($id)"); | |
5120 | ||
5121 | $fetch_url = $line['access_url'] . '/public.php?op=fbexport'; | |
5122 | $post_query = 'key=' . $line['access_key']; | |
5123 | ||
5124 | $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query); | |
5125 | ||
5126 | // try doing it the old way | |
5127 | if (!$feeds) { | |
5128 | $fetch_url = $line['access_url'] . '/backend.php?op=fbexport'; | |
5129 | $feeds = fetch_file_contents($fetch_url, false, false, false, $post_query); | |
5130 | } | |
5131 | ||
5132 | if ($feeds) { | |
5133 | $feeds = json_decode($feeds, true); | |
5134 | ||
5135 | if ($feeds) { | |
5136 | if ($feeds['error']) { | |
5137 | $status = $feeds['error']['code'] + 10; | |
5138 | } else { | |
5139 | $status = 1; | |
5140 | ||
5141 | if (count($feeds['feeds']) > 0) { | |
5142 | ||
5143 | db_query($link, "DELETE FROM ttrss_linked_feeds | |
5144 | WHERE instance_id = '$id'"); | |
5145 | ||
5146 | foreach ($feeds['feeds'] as $feed) { | |
5147 | $feed_url = db_escape_string($feed['feed_url']); | |
5148 | $title = db_escape_string($feed['title']); | |
5149 | $subscribers = db_escape_string($feed['subscribers']); | |
5150 | $site_url = db_escape_string($feed['site_url']); | |
5151 | ||
5152 | db_query($link, "INSERT INTO ttrss_linked_feeds | |
5153 | (feed_url, site_url, title, subscribers, instance_id, created, updated) | |
5154 | VALUES | |
5155 | ('$feed_url', '$site_url', '$title', '$subscribers', '$id', NOW(), NOW())"); | |
5156 | } | |
5157 | } else { | |
5158 | // received 0 feeds, this might indicate that | |
5159 | // the instance on the other hand is rebuilding feedbrowser cache | |
5160 | // we will try again later | |
5161 | ||
5162 | // TODO: maybe perform expiration based on updated here? | |
5163 | } | |
5164 | ||
5165 | _debug("Processed " . count($feeds['feeds']) . " feeds."); | |
5166 | } | |
5167 | } else { | |
5168 | $status = 2; | |
5169 | } | |
5170 | ||
5171 | } else { | |
5172 | $status = 0; | |
5173 | } | |
5174 | ||
5175 | _debug("Status: $status"); | |
5176 | ||
5177 | db_query($link, "UPDATE ttrss_linked_instances SET | |
5178 | last_status_out = '$status', last_connected = NOW() WHERE id = '$id'"); | |
5179 | ||
5180 | } | |
5181 | } | |
5182 | ||
5183 | function make_feed_browser($link, $search, $limit, $mode = 1) { | |
5184 | ||
5185 | $owner_uid = $_SESSION["uid"]; | |
5186 | $rv = ''; | |
5187 | ||
5188 | if ($search) { | |
5189 | $search_qpart = "AND (UPPER(feed_url) LIKE UPPER('%$search%') OR | |
5190 | UPPER(title) LIKE UPPER('%$search%'))"; | |
5191 | } else { | |
5192 | $search_qpart = ""; | |
5193 | } | |
5194 | ||
5195 | if ($mode == 1) { | |
5196 | /* $result = db_query($link, "SELECT feed_url, subscribers FROM | |
5197 | ttrss_feedbrowser_cache WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf | |
5198 | WHERE tf.feed_url = ttrss_feedbrowser_cache.feed_url | |
5199 | AND owner_uid = '$owner_uid') $search_qpart | |
5200 | ORDER BY subscribers DESC LIMIT $limit"); */ | |
5201 | ||
5202 | $result = db_query($link, "SELECT feed_url, site_url, title, SUM(subscribers) AS subscribers FROM | |
5203 | (SELECT feed_url, site_url, title, subscribers FROM ttrss_feedbrowser_cache UNION ALL | |
5204 | SELECT feed_url, site_url, title, subscribers FROM ttrss_linked_feeds) AS qqq | |
5205 | WHERE | |
5206 | (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf | |
5207 | WHERE tf.feed_url = qqq.feed_url | |
5208 | AND owner_uid = '$owner_uid') $search_qpart | |
5209 | GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT $limit"); | |
5210 | ||
5211 | } else if ($mode == 2) { | |
5212 | $result = db_query($link, "SELECT *, | |
5213 | (SELECT COUNT(*) FROM ttrss_user_entries WHERE | |
5214 | orig_feed_id = ttrss_archived_feeds.id) AS articles_archived | |
5215 | FROM | |
5216 | ttrss_archived_feeds | |
5217 | WHERE | |
5218 | (SELECT COUNT(*) FROM ttrss_feeds | |
5219 | WHERE ttrss_feeds.feed_url = ttrss_archived_feeds.feed_url AND | |
5220 | owner_uid = '$owner_uid') = 0 AND | |
5221 | owner_uid = '$owner_uid' $search_qpart | |
5222 | ORDER BY id DESC LIMIT $limit"); | |
5223 | } | |
5224 | ||
5225 | $feedctr = 0; | |
5226 | ||
5227 | while ($line = db_fetch_assoc($result)) { | |
5228 | ||
5229 | if ($mode == 1) { | |
5230 | ||
5231 | $feed_url = htmlspecialchars($line["feed_url"]); | |
5232 | $site_url = htmlspecialchars($line["site_url"]); | |
5233 | $subscribers = $line["subscribers"]; | |
5234 | ||
5235 | $check_box = "<input onclick='toggleSelectListRow2(this)' | |
5236 | dojoType=\"dijit.form.CheckBox\" | |
5237 | type=\"checkbox\" \">"; | |
5238 | ||
5239 | $class = ($feedctr % 2) ? "even" : "odd"; | |
5240 | ||
5241 | $site_url = "<a target=\"_blank\" | |
5242 | href=\"$site_url\"> | |
5243 | <span class=\"fb_feedTitle\">". | |
5244 | htmlspecialchars($line["title"])."</span></a>"; | |
5245 | ||
5246 | $feed_url = "<a target=\"_blank\" class=\"fb_feedUrl\" | |
5247 | href=\"$feed_url\"><img src='images/feed-icon-12x12.png' | |
5248 | style='vertical-align : middle'></a>"; | |
5249 | ||
5250 | $rv .= "<li>$check_box $feed_url $site_url". | |
5251 | " <span class='subscribers'>($subscribers)</span></li>"; | |
5252 | ||
5253 | } else if ($mode == 2) { | |
5254 | $feed_url = htmlspecialchars($line["feed_url"]); | |
5255 | $site_url = htmlspecialchars($line["site_url"]); | |
5256 | $title = htmlspecialchars($line["title"]); | |
5257 | ||
5258 | $check_box = "<input onclick='toggleSelectListRow2(this)' dojoType=\"dijit.form.CheckBox\" | |
5259 | type=\"checkbox\">"; | |
5260 | ||
5261 | $class = ($feedctr % 2) ? "even" : "odd"; | |
5262 | ||
5263 | if ($line['articles_archived'] > 0) { | |
5264 | $archived = sprintf(__("%d archived articles"), $line['articles_archived']); | |
5265 | $archived = " <span class='subscribers'>($archived)</span>"; | |
5266 | } else { | |
5267 | $archived = ''; | |
5268 | } | |
5269 | ||
5270 | $site_url = "<a target=\"_blank\" | |
5271 | href=\"$site_url\"> | |
5272 | <span class=\"fb_feedTitle\">". | |
5273 | htmlspecialchars($line["title"])."</span></a>"; | |
5274 | ||
5275 | $feed_url = "<a target=\"_blank\" class=\"fb_feedUrl\" | |
5276 | href=\"$feed_url\"><img src='images/feed-icon-12x12.png' | |
5277 | style='vertical-align : middle'></a>"; | |
5278 | ||
5279 | ||
5280 | $rv .= "<li id=\"FBROW-".$line["id"]."\">". | |
5281 | "$check_box $feed_url $site_url $archived</li>"; | |
5282 | } | |
5283 | ||
5284 | ++$feedctr; | |
5285 | } | |
5286 | ||
5287 | if ($feedctr == 0) { | |
5288 | $rv .= "<li style=\"text-align : center\"><p>".__('No feeds found.')."</p></li>"; | |
5289 | } | |
5290 | ||
5291 | return $rv; | |
5292 | } | |
5293 | ||
5294 | if (!function_exists('gzdecode')) { | |
5295 | function gzdecode($string) { // no support for 2nd argument | |
5296 | return file_get_contents('compress.zlib://data:who/cares;base64,'. | |
5297 | base64_encode($string)); | |
5298 | } | |
5299 | } | |
5300 | ||
5301 | function perform_data_import($link, $filename, $owner_uid) { | |
5302 | ||
5303 | $num_imported = 0; | |
5304 | $num_processed = 0; | |
5305 | $num_feeds_created = 0; | |
5306 | ||
5307 | $doc = @DOMDocument::load($filename); | |
5308 | ||
5309 | if (!$doc) { | |
5310 | $contents = file_get_contents($filename); | |
5311 | ||
5312 | if ($contents) { | |
5313 | $data = @gzuncompress($contents); | |
5314 | } | |
5315 | ||
5316 | if (!$data) { | |
5317 | $data = @gzdecode($contents); | |
5318 | } | |
5319 | ||
5320 | if ($data) | |
5321 | $doc = DOMDocument::loadXML($data); | |
5322 | } | |
5323 | ||
5324 | if ($doc) { | |
5325 | ||
5326 | $xpath = new DOMXpath($doc); | |
5327 | ||
5328 | $container = $doc->firstChild; | |
5329 | ||
5330 | if ($container && $container->hasAttribute('schema-version')) { | |
5331 | $schema_version = $container->getAttribute('schema-version'); | |
5332 | ||
5333 | if ($schema_version != SCHEMA_VERSION) { | |
5334 | print "<p>" .__("Could not import: incorrect schema version.") . "</p>"; | |
5335 | return; | |
5336 | } | |
5337 | ||
5338 | } else { | |
5339 | print "<p>" . __("Could not import: unrecognized document format.") . "</p>"; | |
5340 | return; | |
5341 | } | |
5342 | ||
5343 | $articles = $xpath->query("//article"); | |
5344 | ||
5345 | foreach ($articles as $article_node) { | |
5346 | if ($article_node->childNodes) { | |
5347 | ||
5348 | $ref_id = 0; | |
5349 | ||
5350 | $article = array(); | |
5351 | ||
5352 | foreach ($article_node->childNodes as $child) { | |
5353 | if ($child->nodeName != 'label_cache') | |
5354 | $article[$child->nodeName] = db_escape_string($child->nodeValue); | |
5355 | else | |
5356 | $article[$child->nodeName] = $child->nodeValue; | |
5357 | } | |
5358 | ||
5359 | //print_r($article); | |
5360 | ||
5361 | if ($article['guid']) { | |
5362 | ||
5363 | ++$num_processed; | |
5364 | ||
5365 | //db_query($link, "BEGIN"); | |
5366 | ||
5367 | //print 'GUID:' . $article['guid'] . "\n"; | |
5368 | ||
5369 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
5370 | WHERE guid = '".$article['guid']."'"); | |
5371 | ||
5372 | if (db_num_rows($result) == 0) { | |
5373 | ||
5374 | $result = db_query($link, | |
5375 | "INSERT INTO ttrss_entries | |
5376 | (title, | |
5377 | guid, | |
5378 | link, | |
5379 | updated, | |
5380 | content, | |
5381 | content_hash, | |
5382 | no_orig_date, | |
5383 | date_updated, | |
5384 | date_entered, | |
5385 | comments, | |
5386 | num_comments, | |
5387 | author) | |
5388 | VALUES | |
5389 | ('".$article['title']."', | |
5390 | '".$article['guid']."', | |
5391 | '".$article['link']."', | |
5392 | '".$article['updated']."', | |
5393 | '".$article['content']."', | |
5394 | '".sha1($article['content'])."', | |
5395 | false, | |
5396 | NOW(), | |
5397 | NOW(), | |
5398 | '', | |
5399 | '0', | |
5400 | '')"); | |
5401 | ||
5402 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
5403 | WHERE guid = '".$article['guid']."'"); | |
5404 | ||
5405 | if (db_num_rows($result) != 0) { | |
5406 | $ref_id = db_fetch_result($result, 0, "id"); | |
5407 | } | |
5408 | ||
5409 | } else { | |
5410 | $ref_id = db_fetch_result($result, 0, "id"); | |
5411 | } | |
5412 | ||
5413 | //print "Got ref ID: $ref_id\n"; | |
5414 | ||
5415 | if ($ref_id) { | |
5416 | ||
5417 | $feed_url = $article['feed_url']; | |
5418 | $feed_title = $article['feed_title']; | |
5419 | ||
5420 | $feed = 'NULL'; | |
5421 | ||
5422 | if ($feed_url && $feed_title) { | |
5423 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
5424 | WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); | |
5425 | ||
5426 | if (db_num_rows($result) != 0) { | |
5427 | $feed = db_fetch_result($result, 0, "id"); | |
5428 | } else { | |
5429 | // try autocreating feed in Uncategorized... | |
5430 | ||
5431 | $result = db_query($link, "INSERT INTO ttrss_feeds (owner_uid, | |
5432 | feed_url, title) VALUES ($owner_uid, '$feed_url', '$feed_title')"); | |
5433 | ||
5434 | $result = db_query($link, "SELECT id FROM ttrss_feeds | |
5435 | WHERE feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); | |
5436 | ||
5437 | if (db_num_rows($result) != 0) { | |
5438 | ++$num_feeds_created; | |
5439 | ||
5440 | $feed = db_fetch_result($result, 0, "id"); | |
5441 | } | |
5442 | } | |
5443 | } | |
5444 | ||
5445 | if ($feed != 'NULL') | |
5446 | $feed_qpart = "feed_id = $feed"; | |
5447 | else | |
5448 | $feed_qpart = "feed_id IS NULL"; | |
5449 | ||
5450 | //print "$ref_id / $feed / " . $article['title'] . "\n"; | |
5451 | ||
5452 | $result = db_query($link, "SELECT int_id FROM ttrss_user_entries | |
5453 | WHERE ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND $feed_qpart"); | |
5454 | ||
5455 | if (db_num_rows($result) == 0) { | |
5456 | ||
5457 | $marked = bool_to_sql_bool(sql_bool_to_bool($article['marked'])); | |
5458 | $published = bool_to_sql_bool(sql_bool_to_bool($article['published'])); | |
5459 | $score = (int) $article['score']; | |
5460 | ||
5461 | $tag_cache = $article['tag_cache']; | |
5462 | $label_cache = db_escape_string($article['label_cache']); | |
5463 | $note = $article['note']; | |
5464 | ||
5465 | //print "Importing " . $article['title'] . "<br/>"; | |
5466 | ||
5467 | ++$num_imported; | |
5468 | ||
5469 | $result = db_query($link, | |
5470 | "INSERT INTO ttrss_user_entries | |
5471 | (ref_id, owner_uid, feed_id, unread, last_read, marked, | |
5472 | published, score, tag_cache, label_cache, uuid, note) | |
5473 | VALUES ($ref_id, $owner_uid, $feed, false, | |
5474 | NULL, $marked, $published, $score, '$tag_cache', | |
5475 | '$label_cache', '', '$note')"); | |
5476 | ||
5477 | $label_cache = json_decode($label_cache, true); | |
5478 | ||
5479 | if (is_array($label_cache) && $label_cache["no-labels"] != 1) { | |
5480 | foreach ($label_cache as $label) { | |
5481 | ||
5482 | label_create($link, $label[1], | |
5483 | $label[2], $label[3], $owner_uid); | |
5484 | ||
5485 | label_add_article($link, $ref_id, $label[1], $owner_uid); | |
5486 | ||
5487 | } | |
5488 | } | |
5489 | ||
5490 | //db_query($link, "COMMIT"); | |
5491 | } | |
5492 | } | |
5493 | } | |
5494 | } | |
5495 | } | |
5496 | ||
5497 | print "<p>" . | |
5498 | T_sprintf("Finished: %d articles processed, %d imported, %d feeds created.", | |
5499 | $num_processed, $num_imported, $num_feeds_created) . | |
5500 | "</p>"; | |
5501 | ||
5502 | } else { | |
5503 | ||
5504 | print "<p>" . __("Could not load XML document.") . "</p>"; | |
5505 | ||
5506 | } | |
5507 | } | |
5508 | ||
5509 | function get_random_bytes($length) { | |
5510 | if (function_exists('openssl_random_pseudo_bytes')) { | |
5511 | return openssl_random_pseudo_bytes($length); | |
5512 | } else { | |
5513 | $output = ""; | |
5514 | ||
5515 | for ($i = 0; $i < $length; $i++) | |
5516 | $output .= chr(mt_rand(0, 255)); | |
5517 | ||
5518 | return $output; | |
5519 | } | |
5520 | } | |
5521 | ?> |