]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | define('EXPECTED_CONFIG_VERSION', 26); | |
3 | define('SCHEMA_VERSION', 130); | |
4 | ||
5 | define('LABEL_BASE_INDEX', -1024); | |
6 | define('PLUGIN_FEED_BASE_INDEX', -128); | |
7 | ||
8 | define('COOKIE_LIFETIME_LONG', 86400*365); | |
9 | ||
10 | $fetch_last_error = false; | |
11 | $fetch_last_error_code = false; | |
12 | $fetch_last_content_type = false; | |
13 | $fetch_last_error_content = false; // curl only for the time being | |
14 | $fetch_curl_used = false; | |
15 | $suppress_debugging = false; | |
16 | ||
17 | libxml_disable_entity_loader(true); | |
18 | ||
19 | // separate test because this is included before sanity checks | |
20 | if (function_exists("mb_internal_encoding")) mb_internal_encoding("UTF-8"); | |
21 | ||
22 | date_default_timezone_set('UTC'); | |
23 | if (defined('E_DEPRECATED')) { | |
24 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); | |
25 | } else { | |
26 | error_reporting(E_ALL & ~E_NOTICE); | |
27 | } | |
28 | ||
29 | require_once 'config.php'; | |
30 | ||
31 | /** | |
32 | * Define a constant if not already defined | |
33 | * | |
34 | * @param string $name The constant name. | |
35 | * @param mixed $value The constant value. | |
36 | * @access public | |
37 | * @return boolean True if defined successfully or not. | |
38 | */ | |
39 | function define_default($name, $value) { | |
40 | defined($name) or define($name, $value); | |
41 | } | |
42 | ||
43 | ///// Some defaults that you can override in config.php ////// | |
44 | ||
45 | define_default('FEED_FETCH_TIMEOUT', 45); | |
46 | // How may seconds to wait for response when requesting feed from a site | |
47 | define_default('FEED_FETCH_NO_CACHE_TIMEOUT', 15); | |
48 | // How may seconds to wait for response when requesting feed from a | |
49 | // site when that feed wasn't cached before | |
50 | define_default('FILE_FETCH_TIMEOUT', 45); | |
51 | // Default timeout when fetching files from remote sites | |
52 | define_default('FILE_FETCH_CONNECT_TIMEOUT', 15); | |
53 | // How many seconds to wait for initial response from website when | |
54 | // fetching files from remote sites | |
55 | ||
56 | if (DB_TYPE == "pgsql") { | |
57 | define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE'); | |
58 | } else { | |
59 | define('SUBSTRING_FOR_DATE', 'SUBSTRING'); | |
60 | } | |
61 | ||
62 | /** | |
63 | * Return available translations names. | |
64 | * | |
65 | * @access public | |
66 | * @return array A array of available translations. | |
67 | */ | |
68 | function get_translations() { | |
69 | $tr = array( | |
70 | "auto" => "Detect automatically", | |
71 | "ar_SA" => "العربيّة (Arabic)", | |
72 | "bg_BG" => "Bulgarian", | |
73 | "da_DA" => "Dansk", | |
74 | "ca_CA" => "Català", | |
75 | "cs_CZ" => "Česky", | |
76 | "en_US" => "English", | |
77 | "el_GR" => "Ελληνικά", | |
78 | "es_ES" => "Español (España)", | |
79 | "es_LA" => "Español", | |
80 | "de_DE" => "Deutsch", | |
81 | "fr_FR" => "Français", | |
82 | "hu_HU" => "Magyar (Hungarian)", | |
83 | "it_IT" => "Italiano", | |
84 | "ja_JP" => "日本語 (Japanese)", | |
85 | "lv_LV" => "Latviešu", | |
86 | "nb_NO" => "Norwegian bokmål", | |
87 | "nl_NL" => "Dutch", | |
88 | "pl_PL" => "Polski", | |
89 | "ru_RU" => "Русский", | |
90 | "pt_BR" => "Portuguese/Brazil", | |
91 | "pt_PT" => "Portuguese/Portugal", | |
92 | "zh_CN" => "Simplified Chinese", | |
93 | "zh_TW" => "Traditional Chinese", | |
94 | "sv_SE" => "Svenska", | |
95 | "fi_FI" => "Suomi", | |
96 | "tr_TR" => "Türkçe"); | |
97 | ||
98 | return $tr; | |
99 | } | |
100 | ||
101 | require_once "lib/accept-to-gettext.php"; | |
102 | require_once "lib/gettext/gettext.inc"; | |
103 | ||
104 | function startup_gettext() { | |
105 | ||
106 | # Get locale from Accept-Language header | |
107 | $lang = al2gt(array_keys(get_translations()), "text/html"); | |
108 | ||
109 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { | |
110 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
111 | } | |
112 | ||
113 | if ($_SESSION["uid"] && get_schema_version() >= 120) { | |
114 | $pref_lang = get_pref("USER_LANGUAGE", $_SESSION["uid"]); | |
115 | ||
116 | if ($pref_lang && $pref_lang != 'auto') { | |
117 | $lang = $pref_lang; | |
118 | } | |
119 | } | |
120 | ||
121 | if ($lang) { | |
122 | if (defined('LC_MESSAGES')) { | |
123 | _setlocale(LC_MESSAGES, $lang); | |
124 | } else if (defined('LC_ALL')) { | |
125 | _setlocale(LC_ALL, $lang); | |
126 | } | |
127 | ||
128 | _bindtextdomain("messages", "locale"); | |
129 | ||
130 | _textdomain("messages"); | |
131 | _bind_textdomain_codeset("messages", "UTF-8"); | |
132 | } | |
133 | } | |
134 | ||
135 | require_once 'db-prefs.php'; | |
136 | require_once 'version.php'; | |
137 | require_once 'ccache.php'; | |
138 | require_once 'labels.php'; | |
139 | require_once 'controls.php'; | |
140 | ||
141 | define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)'); | |
142 | ini_set('user_agent', SELF_USER_AGENT); | |
143 | ||
144 | require_once 'lib/pubsubhubbub/Publisher.php'; | |
145 | ||
146 | $schema_version = false; | |
147 | ||
148 | function _debug_suppress($suppress) { | |
149 | global $suppress_debugging; | |
150 | ||
151 | $suppress_debugging = $suppress; | |
152 | } | |
153 | ||
154 | /** | |
155 | * Print a timestamped debug message. | |
156 | * | |
157 | * @param string $msg The debug message. | |
158 | * @return void | |
159 | */ | |
160 | function _debug($msg, $show = true) { | |
161 | global $suppress_debugging; | |
162 | ||
163 | //echo "[$suppress_debugging] $msg $show\n"; | |
164 | ||
165 | if ($suppress_debugging) return false; | |
166 | ||
167 | $ts = strftime("%H:%M:%S", time()); | |
168 | if (function_exists('posix_getpid')) { | |
169 | $ts = "$ts/" . posix_getpid(); | |
170 | } | |
171 | ||
172 | if ($show && !(defined('QUIET') && QUIET)) { | |
173 | print "[$ts] $msg\n"; | |
174 | } | |
175 | ||
176 | if (defined('LOGFILE')) { | |
177 | $fp = fopen(LOGFILE, 'a+'); | |
178 | ||
179 | if ($fp) { | |
180 | $locked = false; | |
181 | ||
182 | if (function_exists("flock")) { | |
183 | $tries = 0; | |
184 | ||
185 | // try to lock logfile for writing | |
186 | while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { | |
187 | sleep(1); | |
188 | ++$tries; | |
189 | } | |
190 | ||
191 | if (!$locked) { | |
192 | fclose($fp); | |
193 | return; | |
194 | } | |
195 | } | |
196 | ||
197 | fputs($fp, "[$ts] $msg\n"); | |
198 | ||
199 | if (function_exists("flock")) { | |
200 | flock($fp, LOCK_UN); | |
201 | } | |
202 | ||
203 | fclose($fp); | |
204 | } | |
205 | } | |
206 | ||
207 | } // function _debug | |
208 | ||
209 | /** | |
210 | * Purge a feed old posts. | |
211 | * | |
212 | * @param mixed $link A database connection. | |
213 | * @param mixed $feed_id The id of the purged feed. | |
214 | * @param mixed $purge_interval Olderness of purged posts. | |
215 | * @param boolean $debug Set to True to enable the debug. False by default. | |
216 | * @access public | |
217 | * @return void | |
218 | */ | |
219 | function purge_feed($feed_id, $purge_interval, $debug = false) { | |
220 | ||
221 | if (!$purge_interval) $purge_interval = feed_purge_interval($feed_id); | |
222 | ||
223 | $rows = -1; | |
224 | ||
225 | $result = db_query( | |
226 | "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'"); | |
227 | ||
228 | $owner_uid = false; | |
229 | ||
230 | if (db_num_rows($result) == 1) { | |
231 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
232 | } | |
233 | ||
234 | if ($purge_interval == -1 || !$purge_interval) { | |
235 | if ($owner_uid) { | |
236 | ccache_update($feed_id, $owner_uid); | |
237 | } | |
238 | return; | |
239 | } | |
240 | ||
241 | if (!$owner_uid) return; | |
242 | ||
243 | if (FORCE_ARTICLE_PURGE == 0) { | |
244 | $purge_unread = get_pref("PURGE_UNREAD_ARTICLES", | |
245 | $owner_uid, false); | |
246 | } else { | |
247 | $purge_unread = true; | |
248 | $purge_interval = FORCE_ARTICLE_PURGE; | |
249 | } | |
250 | ||
251 | if (!$purge_unread) $query_limit = " unread = false AND "; | |
252 | ||
253 | if (DB_TYPE == "pgsql") { | |
254 | $result = db_query("DELETE FROM ttrss_user_entries | |
255 | USING ttrss_entries | |
256 | WHERE ttrss_entries.id = ref_id AND | |
257 | marked = false AND | |
258 | feed_id = '$feed_id' AND | |
259 | $query_limit | |
260 | ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); | |
261 | ||
262 | } else { | |
263 | ||
264 | /* $result = db_query("DELETE FROM ttrss_user_entries WHERE | |
265 | marked = false AND feed_id = '$feed_id' AND | |
266 | (SELECT date_updated FROM ttrss_entries WHERE | |
267 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ | |
268 | ||
269 | $result = db_query("DELETE FROM ttrss_user_entries | |
270 | USING ttrss_user_entries, ttrss_entries | |
271 | WHERE ttrss_entries.id = ref_id AND | |
272 | marked = false AND | |
273 | feed_id = '$feed_id' AND | |
274 | $query_limit | |
275 | ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
276 | } | |
277 | ||
278 | $rows = db_affected_rows($result); | |
279 | ||
280 | ccache_update($feed_id, $owner_uid); | |
281 | ||
282 | if ($debug) { | |
283 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); | |
284 | } | |
285 | ||
286 | return $rows; | |
287 | } // function purge_feed | |
288 | ||
289 | function feed_purge_interval($feed_id) { | |
290 | ||
291 | $result = db_query("SELECT purge_interval, owner_uid FROM ttrss_feeds | |
292 | WHERE id = '$feed_id'"); | |
293 | ||
294 | if (db_num_rows($result) == 1) { | |
295 | $purge_interval = db_fetch_result($result, 0, "purge_interval"); | |
296 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
297 | ||
298 | if ($purge_interval == 0) $purge_interval = get_pref( | |
299 | 'PURGE_OLD_DAYS', $owner_uid); | |
300 | ||
301 | return $purge_interval; | |
302 | ||
303 | } else { | |
304 | return -1; | |
305 | } | |
306 | } | |
307 | ||
308 | function purge_orphans($do_output = false) { | |
309 | ||
310 | // purge orphaned posts in main content table | |
311 | $result = db_query("DELETE FROM ttrss_entries WHERE | |
312 | NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)"); | |
313 | ||
314 | if ($do_output) { | |
315 | $rows = db_affected_rows($result); | |
316 | _debug("Purged $rows orphaned posts."); | |
317 | } | |
318 | } | |
319 | ||
320 | function get_feed_update_interval($feed_id) { | |
321 | $result = db_query("SELECT owner_uid, update_interval FROM | |
322 | ttrss_feeds WHERE id = '$feed_id'"); | |
323 | ||
324 | if (db_num_rows($result) == 1) { | |
325 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
326 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
327 | ||
328 | if ($update_interval != 0) { | |
329 | return $update_interval; | |
330 | } else { | |
331 | return get_pref('DEFAULT_UPDATE_INTERVAL', $owner_uid, false); | |
332 | } | |
333 | ||
334 | } else { | |
335 | return -1; | |
336 | } | |
337 | } | |
338 | ||
339 | // TODO: multiple-argument way is deprecated, first parameter is a hash now | |
340 | function fetch_file_contents($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false, | |
341 | 4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) { | |
342 | ||
343 | global $fetch_last_error; | |
344 | global $fetch_last_error_code; | |
345 | global $fetch_last_error_content; | |
346 | global $fetch_last_content_type; | |
347 | global $fetch_curl_used; | |
348 | ||
349 | $fetch_last_error = false; | |
350 | $fetch_last_error_code = -1; | |
351 | $fetch_last_error_content = ""; | |
352 | $fetch_last_content_type = ""; | |
353 | $fetch_curl_used = false; | |
354 | ||
355 | if (!is_array($options)) { | |
356 | ||
357 | // falling back on compatibility shim | |
358 | $option_names = [ "url", "type", "login", "pass", "post_query", "timeout", "timestamp", "useragent" ]; | |
359 | $tmp = []; | |
360 | ||
361 | for ($i = 0; $i < func_num_args(); $i++) { | |
362 | $tmp[$option_names[$i]] = func_get_arg($i); | |
363 | } | |
364 | ||
365 | $options = $tmp; | |
366 | ||
367 | /*$options = array( | |
368 | "url" => func_get_arg(0), | |
369 | "type" => @func_get_arg(1), | |
370 | "login" => @func_get_arg(2), | |
371 | "pass" => @func_get_arg(3), | |
372 | "post_query" => @func_get_arg(4), | |
373 | "timeout" => @func_get_arg(5), | |
374 | "timestamp" => @func_get_arg(6), | |
375 | "useragent" => @func_get_arg(7) | |
376 | ); */ | |
377 | } | |
378 | ||
379 | $url = $options["url"]; | |
380 | $type = isset($options["type"]) ? $options["type"] : false; | |
381 | $login = isset($options["login"]) ? $options["login"] : false; | |
382 | $pass = isset($options["pass"]) ? $options["pass"] : false; | |
383 | $post_query = isset($options["post_query"]) ? $options["post_query"] : false; | |
384 | $timeout = isset($options["timeout"]) ? $options["timeout"] : false; | |
385 | $timestamp = isset($options["timestamp"]) ? $options["timestamp"] : 0; | |
386 | $useragent = isset($options["useragent"]) ? $options["useragent"] : false; | |
387 | $followlocation = isset($options["followlocation"]) ? $options["followlocation"] : true; | |
388 | ||
389 | $url = ltrim($url, ' '); | |
390 | $url = str_replace(' ', '%20', $url); | |
391 | ||
392 | if (strpos($url, "//") === 0) | |
393 | $url = 'http:' . $url; | |
394 | ||
395 | if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) { | |
396 | ||
397 | $fetch_curl_used = true; | |
398 | ||
399 | $ch = curl_init($url); | |
400 | ||
401 | if ($timestamp && !$post_query) { | |
402 | curl_setopt($ch, CURLOPT_HTTPHEADER, | |
403 | array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp))); | |
404 | } | |
405 | ||
406 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : FILE_FETCH_CONNECT_TIMEOUT); | |
407 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : FILE_FETCH_TIMEOUT); | |
408 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir") && $followlocation); | |
409 | curl_setopt($ch, CURLOPT_MAXREDIRS, 20); | |
410 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); | |
411 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
412 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); | |
413 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : | |
414 | SELF_USER_AGENT); | |
415 | curl_setopt($ch, CURLOPT_ENCODING, ""); | |
416 | //curl_setopt($ch, CURLOPT_REFERER, $url); | |
417 | ||
418 | if (!ini_get("open_basedir")) { | |
419 | curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null"); | |
420 | } | |
421 | ||
422 | if (defined('_CURL_HTTP_PROXY')) { | |
423 | curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY); | |
424 | } | |
425 | ||
426 | if ($post_query) { | |
427 | curl_setopt($ch, CURLOPT_POST, true); | |
428 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query); | |
429 | } | |
430 | ||
431 | if ($login && $pass) | |
432 | curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass"); | |
433 | ||
434 | $contents = @curl_exec($ch); | |
435 | ||
436 | if (curl_errno($ch) === 23 || curl_errno($ch) === 61) { | |
437 | curl_setopt($ch, CURLOPT_ENCODING, 'none'); | |
438 | $contents = @curl_exec($ch); | |
439 | } | |
440 | ||
441 | if ($contents === false) { | |
442 | $fetch_last_error = curl_errno($ch) . " " . curl_error($ch); | |
443 | curl_close($ch); | |
444 | return false; | |
445 | } | |
446 | ||
447 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
448 | $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); | |
449 | ||
450 | $fetch_last_error_code = $http_code; | |
451 | ||
452 | if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) { | |
453 | if (curl_errno($ch) != 0) { | |
454 | $fetch_last_error = curl_errno($ch) . " " . curl_error($ch); | |
455 | } else { | |
456 | $fetch_last_error = "HTTP Code: $http_code"; | |
457 | } | |
458 | $fetch_last_error_content = $contents; | |
459 | curl_close($ch); | |
460 | return false; | |
461 | } | |
462 | ||
463 | curl_close($ch); | |
464 | ||
465 | return $contents; | |
466 | } else { | |
467 | ||
468 | $fetch_curl_used = false; | |
469 | ||
470 | if ($login && $pass){ | |
471 | $url_parts = array(); | |
472 | ||
473 | preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts); | |
474 | ||
475 | $pass = urlencode($pass); | |
476 | ||
477 | if ($url_parts[1] && $url_parts[2]) { | |
478 | $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2]; | |
479 | } | |
480 | } | |
481 | ||
482 | // TODO: should this support POST requests or not? idk | |
483 | ||
484 | if (!$post_query && $timestamp) { | |
485 | $context = stream_context_create(array( | |
486 | 'http' => array( | |
487 | 'method' => 'GET', | |
488 | 'ignore_errors' => true, | |
489 | 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT, | |
490 | 'protocol_version'=> 1.1, | |
491 | 'header' => "If-Modified-Since: ".gmdate("D, d M Y H:i:s \\G\\M\\T\r\n", $timestamp) | |
492 | ))); | |
493 | } else { | |
494 | $context = stream_context_create(array( | |
495 | 'http' => array( | |
496 | 'method' => 'GET', | |
497 | 'ignore_errors' => true, | |
498 | 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT, | |
499 | 'protocol_version'=> 1.1 | |
500 | ))); | |
501 | } | |
502 | ||
503 | $old_error = error_get_last(); | |
504 | ||
505 | $data = @file_get_contents($url, false, $context); | |
506 | ||
507 | if (isset($http_response_header) && is_array($http_response_header)) { | |
508 | foreach ($http_response_header as $h) { | |
509 | if (substr(strtolower($h), 0, 13) == 'content-type:') { | |
510 | $fetch_last_content_type = substr($h, 14); | |
511 | // don't abort here b/c there might be more than one | |
512 | // e.g. if we were being redirected -- last one is the right one | |
513 | } | |
514 | ||
515 | if (substr(strtolower($h), 0, 7) == 'http/1.') { | |
516 | $fetch_last_error_code = (int) substr($h, 9, 3); | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | if ($fetch_last_error_code != 200) { | |
522 | $error = error_get_last(); | |
523 | ||
524 | if ($error['message'] != $old_error['message']) { | |
525 | $fetch_last_error = $error["message"]; | |
526 | } else { | |
527 | $fetch_last_error = "HTTP Code: $fetch_last_error_code"; | |
528 | } | |
529 | ||
530 | $fetch_last_error_content = $data; | |
531 | ||
532 | return false; | |
533 | } | |
534 | return $data; | |
535 | } | |
536 | ||
537 | } | |
538 | ||
539 | /** | |
540 | * Try to determine the favicon URL for a feed. | |
541 | * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
542 | * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
543 | * | |
544 | * @param string $url A feed or page URL | |
545 | * @access public | |
546 | * @return mixed The favicon URL, or false if none was found. | |
547 | */ | |
548 | function get_favicon_url($url) { | |
549 | ||
550 | $favicon_url = false; | |
551 | ||
552 | if ($html = @fetch_file_contents($url)) { | |
553 | ||
554 | libxml_use_internal_errors(true); | |
555 | ||
556 | $doc = new DOMDocument(); | |
557 | $doc->loadHTML($html); | |
558 | $xpath = new DOMXPath($doc); | |
559 | ||
560 | $base = $xpath->query('/html/head/base'); | |
561 | foreach ($base as $b) { | |
562 | $url = $b->getAttribute("href"); | |
563 | break; | |
564 | } | |
565 | ||
566 | $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]'); | |
567 | if (count($entries) > 0) { | |
568 | foreach ($entries as $entry) { | |
569 | $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href")); | |
570 | break; | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
575 | if (!$favicon_url) | |
576 | $favicon_url = rewrite_relative_url($url, "/favicon.ico"); | |
577 | ||
578 | return $favicon_url; | |
579 | } // function get_favicon_url | |
580 | ||
581 | function check_feed_favicon($site_url, $feed) { | |
582 | # print "FAVICON [$site_url]: $favicon_url\n"; | |
583 | ||
584 | $icon_file = ICONS_DIR . "/$feed.ico"; | |
585 | ||
586 | if (!file_exists($icon_file)) { | |
587 | $favicon_url = get_favicon_url($site_url); | |
588 | ||
589 | if ($favicon_url) { | |
590 | // Limiting to "image" type misses those served with text/plain | |
591 | $contents = fetch_file_contents($favicon_url); // , "image"); | |
592 | ||
593 | if ($contents) { | |
594 | // Crude image type matching. | |
595 | // Patterns gleaned from the file(1) source code. | |
596 | if (preg_match('/^\x00\x00\x01\x00/', $contents)) { | |
597 | // 0 string \000\000\001\000 MS Windows icon resource | |
598 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); | |
599 | } | |
600 | elseif (preg_match('/^GIF8/', $contents)) { | |
601 | // 0 string GIF8 GIF image data | |
602 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); | |
603 | } | |
604 | elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { | |
605 | // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data | |
606 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); | |
607 | } | |
608 | elseif (preg_match('/^\xff\xd8/', $contents)) { | |
609 | // 0 beshort 0xffd8 JPEG image data | |
610 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); | |
611 | } | |
612 | else { | |
613 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); | |
614 | $contents = ""; | |
615 | } | |
616 | } | |
617 | ||
618 | if ($contents) { | |
619 | $fp = @fopen($icon_file, "w"); | |
620 | ||
621 | if ($fp) { | |
622 | fwrite($fp, $contents); | |
623 | fclose($fp); | |
624 | chmod($icon_file, 0644); | |
625 | } | |
626 | } | |
627 | } | |
628 | return $icon_file; | |
629 | } | |
630 | } | |
631 | ||
632 | function initialize_user_prefs($uid, $profile = false) { | |
633 | ||
634 | $uid = db_escape_string($uid); | |
635 | ||
636 | if (!$profile) { | |
637 | $profile = "NULL"; | |
638 | $profile_qpart = "AND profile IS NULL"; | |
639 | } else { | |
640 | $profile_qpart = "AND profile = '$profile'"; | |
641 | } | |
642 | ||
643 | if (get_schema_version() < 63) $profile_qpart = ""; | |
644 | ||
645 | db_query("BEGIN"); | |
646 | ||
647 | $result = db_query("SELECT pref_name,def_value FROM ttrss_prefs"); | |
648 | ||
649 | $u_result = db_query("SELECT pref_name | |
650 | FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart"); | |
651 | ||
652 | $active_prefs = array(); | |
653 | ||
654 | while ($line = db_fetch_assoc($u_result)) { | |
655 | array_push($active_prefs, $line["pref_name"]); | |
656 | } | |
657 | ||
658 | while ($line = db_fetch_assoc($result)) { | |
659 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
660 | // print "adding " . $line["pref_name"] . "<br>"; | |
661 | ||
662 | $line["def_value"] = db_escape_string($line["def_value"]); | |
663 | $line["pref_name"] = db_escape_string($line["pref_name"]); | |
664 | ||
665 | if (get_schema_version() < 63) { | |
666 | db_query("INSERT INTO ttrss_user_prefs | |
667 | (owner_uid,pref_name,value) VALUES | |
668 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
669 | ||
670 | } else { | |
671 | db_query("INSERT INTO ttrss_user_prefs | |
672 | (owner_uid,pref_name,value, profile) VALUES | |
673 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)"); | |
674 | } | |
675 | ||
676 | } | |
677 | } | |
678 | ||
679 | db_query("COMMIT"); | |
680 | ||
681 | } | |
682 | ||
683 | function get_ssl_certificate_id() { | |
684 | if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) { | |
685 | return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] . | |
686 | $_SERVER["REDIRECT_SSL_CLIENT_V_START"] . | |
687 | $_SERVER["REDIRECT_SSL_CLIENT_V_END"] . | |
688 | $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]); | |
689 | } | |
690 | if ($_SERVER["SSL_CLIENT_M_SERIAL"]) { | |
691 | return sha1($_SERVER["SSL_CLIENT_M_SERIAL"] . | |
692 | $_SERVER["SSL_CLIENT_V_START"] . | |
693 | $_SERVER["SSL_CLIENT_V_END"] . | |
694 | $_SERVER["SSL_CLIENT_S_DN"]); | |
695 | } | |
696 | return ""; | |
697 | } | |
698 | ||
699 | function authenticate_user($login, $password, $check_only = false) { | |
700 | ||
701 | if (!SINGLE_USER_MODE) { | |
702 | $user_id = false; | |
703 | ||
704 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_AUTH_USER) as $plugin) { | |
705 | ||
706 | $user_id = (int) $plugin->authenticate($login, $password); | |
707 | ||
708 | if ($user_id) { | |
709 | $_SESSION["auth_module"] = strtolower(get_class($plugin)); | |
710 | break; | |
711 | } | |
712 | } | |
713 | ||
714 | if ($user_id && !$check_only) { | |
715 | @session_start(); | |
716 | ||
717 | $_SESSION["uid"] = $user_id; | |
718 | $_SESSION["version"] = VERSION_STATIC; | |
719 | ||
720 | $result = db_query("SELECT login,access_level,pwd_hash FROM ttrss_users | |
721 | WHERE id = '$user_id'"); | |
722 | ||
723 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
724 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
725 | $_SESSION["csrf_token"] = uniqid_short(); | |
726 | ||
727 | db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
728 | $_SESSION["uid"]); | |
729 | ||
730 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
731 | $_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']); | |
732 | $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash"); | |
733 | ||
734 | $_SESSION["last_version_check"] = time(); | |
735 | ||
736 | initialize_user_prefs($_SESSION["uid"]); | |
737 | ||
738 | return true; | |
739 | } | |
740 | ||
741 | return false; | |
742 | ||
743 | } else { | |
744 | ||
745 | $_SESSION["uid"] = 1; | |
746 | $_SESSION["name"] = "admin"; | |
747 | $_SESSION["access_level"] = 10; | |
748 | ||
749 | $_SESSION["hide_hello"] = true; | |
750 | $_SESSION["hide_logout"] = true; | |
751 | ||
752 | $_SESSION["auth_module"] = false; | |
753 | ||
754 | if (!$_SESSION["csrf_token"]) { | |
755 | $_SESSION["csrf_token"] = uniqid_short(); | |
756 | } | |
757 | ||
758 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; | |
759 | ||
760 | initialize_user_prefs($_SESSION["uid"]); | |
761 | ||
762 | return true; | |
763 | } | |
764 | } | |
765 | ||
766 | function make_password($length = 8) { | |
767 | ||
768 | $password = ""; | |
769 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; | |
770 | ||
771 | $i = 0; | |
772 | ||
773 | while ($i < $length) { | |
774 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
775 | ||
776 | if (!strstr($password, $char)) { | |
777 | $password .= $char; | |
778 | $i++; | |
779 | } | |
780 | } | |
781 | return $password; | |
782 | } | |
783 | ||
784 | // this is called after user is created to initialize default feeds, labels | |
785 | // or whatever else | |
786 | ||
787 | // user preferences are checked on every login, not here | |
788 | ||
789 | function initialize_user($uid) { | |
790 | ||
791 | db_query("insert into ttrss_feeds (owner_uid,title,feed_url) | |
792 | values ('$uid', 'Tiny Tiny RSS: Forum', | |
793 | 'http://tt-rss.org/forum/rss.php')"); | |
794 | } | |
795 | ||
796 | function logout_user() { | |
797 | session_destroy(); | |
798 | if (isset($_COOKIE[session_name()])) { | |
799 | setcookie(session_name(), '', time()-42000, '/'); | |
800 | } | |
801 | } | |
802 | ||
803 | function validate_csrf($csrf_token) { | |
804 | return $csrf_token == $_SESSION['csrf_token']; | |
805 | } | |
806 | ||
807 | function load_user_plugins($owner_uid, $pluginhost = false) { | |
808 | ||
809 | if (!$pluginhost) $pluginhost = PluginHost::getInstance(); | |
810 | ||
811 | if ($owner_uid && SCHEMA_VERSION >= 100) { | |
812 | $plugins = get_pref("_ENABLED_PLUGINS", $owner_uid); | |
813 | ||
814 | $pluginhost->load($plugins, PluginHost::KIND_USER, $owner_uid); | |
815 | ||
816 | if (get_schema_version() > 100) { | |
817 | $pluginhost->load_data(); | |
818 | } | |
819 | } | |
820 | } | |
821 | ||
822 | function login_sequence() { | |
823 | if (SINGLE_USER_MODE) { | |
824 | @session_start(); | |
825 | authenticate_user("admin", null); | |
826 | startup_gettext(); | |
827 | load_user_plugins($_SESSION["uid"]); | |
828 | } else { | |
829 | if (!validate_session()) $_SESSION["uid"] = false; | |
830 | ||
831 | if (!$_SESSION["uid"]) { | |
832 | ||
833 | if (AUTH_AUTO_LOGIN && authenticate_user(null, null)) { | |
834 | $_SESSION["ref_schema_version"] = get_schema_version(true); | |
835 | } else { | |
836 | authenticate_user(null, null, true); | |
837 | } | |
838 | ||
839 | if (!$_SESSION["uid"]) { | |
840 | @session_destroy(); | |
841 | setcookie(session_name(), '', time()-42000, '/'); | |
842 | ||
843 | render_login_form(); | |
844 | exit; | |
845 | } | |
846 | ||
847 | } else { | |
848 | /* bump login timestamp */ | |
849 | db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " . | |
850 | $_SESSION["uid"]); | |
851 | $_SESSION["last_login_update"] = time(); | |
852 | } | |
853 | ||
854 | if ($_SESSION["uid"]) { | |
855 | startup_gettext(); | |
856 | load_user_plugins($_SESSION["uid"]); | |
857 | ||
858 | /* cleanup ccache */ | |
859 | ||
860 | db_query("DELETE FROM ttrss_counters_cache WHERE owner_uid = ". | |
861 | $_SESSION["uid"] . " AND | |
862 | (SELECT COUNT(id) FROM ttrss_feeds WHERE | |
863 | ttrss_feeds.id = feed_id) = 0"); | |
864 | ||
865 | db_query("DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ". | |
866 | $_SESSION["uid"] . " AND | |
867 | (SELECT COUNT(id) FROM ttrss_feed_categories WHERE | |
868 | ttrss_feed_categories.id = feed_id) = 0"); | |
869 | ||
870 | } | |
871 | ||
872 | } | |
873 | } | |
874 | ||
875 | function truncate_string($str, $max_len, $suffix = '…') { | |
876 | if (mb_strlen($str, "utf-8") > $max_len) { | |
877 | return mb_substr($str, 0, $max_len, "utf-8") . $suffix; | |
878 | } else { | |
879 | return $str; | |
880 | } | |
881 | } | |
882 | ||
883 | // is not utf8 clean | |
884 | function truncate_middle($str, $max_len, $suffix = '…') { | |
885 | if (strlen($str) > $max_len) { | |
886 | return substr_replace($str, $suffix, $max_len / 2, mb_strlen($str) - $max_len); | |
887 | } else { | |
888 | return $str; | |
889 | } | |
890 | } | |
891 | ||
892 | function convert_timestamp($timestamp, $source_tz, $dest_tz) { | |
893 | ||
894 | try { | |
895 | $source_tz = new DateTimeZone($source_tz); | |
896 | } catch (Exception $e) { | |
897 | $source_tz = new DateTimeZone('UTC'); | |
898 | } | |
899 | ||
900 | try { | |
901 | $dest_tz = new DateTimeZone($dest_tz); | |
902 | } catch (Exception $e) { | |
903 | $dest_tz = new DateTimeZone('UTC'); | |
904 | } | |
905 | ||
906 | $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz); | |
907 | return $dt->format('U') + $dest_tz->getOffset($dt); | |
908 | } | |
909 | ||
910 | function make_local_datetime($timestamp, $long, $owner_uid = false, | |
911 | $no_smart_dt = false, $eta_min = false) { | |
912 | ||
913 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
914 | if (!$timestamp) $timestamp = '1970-01-01 0:00'; | |
915 | ||
916 | global $utc_tz; | |
917 | global $user_tz; | |
918 | ||
919 | if (!$utc_tz) $utc_tz = new DateTimeZone('UTC'); | |
920 | ||
921 | $timestamp = substr($timestamp, 0, 19); | |
922 | ||
923 | # We store date in UTC internally | |
924 | $dt = new DateTime($timestamp, $utc_tz); | |
925 | ||
926 | $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid); | |
927 | ||
928 | if ($user_tz_string != 'Automatic') { | |
929 | ||
930 | try { | |
931 | if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string); | |
932 | } catch (Exception $e) { | |
933 | $user_tz = $utc_tz; | |
934 | } | |
935 | ||
936 | $tz_offset = $user_tz->getOffset($dt); | |
937 | } else { | |
938 | $tz_offset = (int) -$_SESSION["clientTzOffset"]; | |
939 | } | |
940 | ||
941 | $user_timestamp = $dt->format('U') + $tz_offset; | |
942 | ||
943 | if (!$no_smart_dt) { | |
944 | return smart_date_time($user_timestamp, | |
945 | $tz_offset, $owner_uid, $eta_min); | |
946 | } else { | |
947 | if ($long) | |
948 | $format = get_pref('LONG_DATE_FORMAT', $owner_uid); | |
949 | else | |
950 | $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); | |
951 | ||
952 | return date($format, $user_timestamp); | |
953 | } | |
954 | } | |
955 | ||
956 | function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) { | |
957 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
958 | ||
959 | if ($eta_min && time() + $tz_offset - $timestamp < 3600) { | |
960 | return T_sprintf("%d min", date("i", time() + $tz_offset - $timestamp)); | |
961 | } else if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) { | |
962 | return date("G:i", $timestamp); | |
963 | } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) { | |
964 | $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); | |
965 | return date($format, $timestamp); | |
966 | } else { | |
967 | $format = get_pref('LONG_DATE_FORMAT', $owner_uid); | |
968 | return date($format, $timestamp); | |
969 | } | |
970 | } | |
971 | ||
972 | function sql_bool_to_bool($s) { | |
973 | if ($s == "t" || $s == "1" || strtolower($s) == "true") { | |
974 | return true; | |
975 | } else { | |
976 | return false; | |
977 | } | |
978 | } | |
979 | ||
980 | function bool_to_sql_bool($s) { | |
981 | if ($s) { | |
982 | return "true"; | |
983 | } else { | |
984 | return "false"; | |
985 | } | |
986 | } | |
987 | ||
988 | // Session caching removed due to causing wrong redirects to upgrade | |
989 | // script when get_schema_version() is called on an obsolete session | |
990 | // created on a previous schema version. | |
991 | function get_schema_version($nocache = false) { | |
992 | global $schema_version; | |
993 | ||
994 | if (!$schema_version && !$nocache) { | |
995 | $result = db_query("SELECT schema_version FROM ttrss_version"); | |
996 | $version = db_fetch_result($result, 0, "schema_version"); | |
997 | $schema_version = $version; | |
998 | return $version; | |
999 | } else { | |
1000 | return $schema_version; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | function sanity_check() { | |
1005 | require_once 'errors.php'; | |
1006 | global $ERRORS; | |
1007 | ||
1008 | $error_code = 0; | |
1009 | $schema_version = get_schema_version(true); | |
1010 | ||
1011 | if ($schema_version != SCHEMA_VERSION) { | |
1012 | $error_code = 5; | |
1013 | } | |
1014 | ||
1015 | if (DB_TYPE == "mysql") { | |
1016 | $result = db_query("SELECT true", false); | |
1017 | if (db_num_rows($result) != 1) { | |
1018 | $error_code = 10; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | if (db_escape_string("testTEST") != "testTEST") { | |
1023 | $error_code = 12; | |
1024 | } | |
1025 | ||
1026 | return array("code" => $error_code, "message" => $ERRORS[$error_code]); | |
1027 | } | |
1028 | ||
1029 | function file_is_locked($filename) { | |
1030 | if (file_exists(LOCK_DIRECTORY . "/$filename")) { | |
1031 | if (function_exists('flock')) { | |
1032 | $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r"); | |
1033 | if ($fp) { | |
1034 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1035 | flock($fp, LOCK_UN); | |
1036 | fclose($fp); | |
1037 | return false; | |
1038 | } | |
1039 | fclose($fp); | |
1040 | return true; | |
1041 | } else { | |
1042 | return false; | |
1043 | } | |
1044 | } | |
1045 | return true; // consider the file always locked and skip the test | |
1046 | } else { | |
1047 | return false; | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | ||
1052 | function make_lockfile($filename) { | |
1053 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); | |
1054 | ||
1055 | if ($fp && flock($fp, LOCK_EX | LOCK_NB)) { | |
1056 | $stat_h = fstat($fp); | |
1057 | $stat_f = stat(LOCK_DIRECTORY . "/$filename"); | |
1058 | ||
1059 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { | |
1060 | if ($stat_h["ino"] != $stat_f["ino"] || | |
1061 | $stat_h["dev"] != $stat_f["dev"]) { | |
1062 | ||
1063 | return false; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | if (function_exists('posix_getpid')) { | |
1068 | fwrite($fp, posix_getpid() . "\n"); | |
1069 | } | |
1070 | return $fp; | |
1071 | } else { | |
1072 | return false; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | function make_stampfile($filename) { | |
1077 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); | |
1078 | ||
1079 | if (flock($fp, LOCK_EX | LOCK_NB)) { | |
1080 | fwrite($fp, time() . "\n"); | |
1081 | flock($fp, LOCK_UN); | |
1082 | fclose($fp); | |
1083 | return true; | |
1084 | } else { | |
1085 | return false; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | function sql_random_function() { | |
1090 | if (DB_TYPE == "mysql") { | |
1091 | return "RAND()"; | |
1092 | } else { | |
1093 | return "RANDOM()"; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | function catchup_feed($feed, $cat_view, $owner_uid = false, $mode = 'all', $search = false) { | |
1098 | ||
1099 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
1100 | ||
1101 | // Todo: all this interval stuff needs some generic generator function | |
1102 | ||
1103 | $date_qpart = "false"; | |
1104 | $search_qpart = is_array($search) && $search[0] ? search_to_sql($search[0], $search[1])[0] : 'true'; | |
1105 | ||
1106 | switch ($mode) { | |
1107 | case "1day": | |
1108 | if (DB_TYPE == "pgsql") { | |
1109 | $date_qpart = "date_entered < NOW() - INTERVAL '1 day' "; | |
1110 | } else { | |
1111 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) "; | |
1112 | } | |
1113 | break; | |
1114 | case "1week": | |
1115 | if (DB_TYPE == "pgsql") { | |
1116 | $date_qpart = "date_entered < NOW() - INTERVAL '1 week' "; | |
1117 | } else { | |
1118 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) "; | |
1119 | } | |
1120 | break; | |
1121 | case "2week": | |
1122 | if (DB_TYPE == "pgsql") { | |
1123 | $date_qpart = "date_entered < NOW() - INTERVAL '2 week' "; | |
1124 | } else { | |
1125 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) "; | |
1126 | } | |
1127 | break; | |
1128 | default: | |
1129 | $date_qpart = "true"; | |
1130 | } | |
1131 | ||
1132 | if (is_numeric($feed)) { | |
1133 | if ($cat_view) { | |
1134 | ||
1135 | if ($feed >= 0) { | |
1136 | ||
1137 | if ($feed > 0) { | |
1138 | $children = getChildCategories($feed, $owner_uid); | |
1139 | array_push($children, $feed); | |
1140 | ||
1141 | $children = join(",", $children); | |
1142 | ||
1143 | $cat_qpart = "cat_id IN ($children)"; | |
1144 | } else { | |
1145 | $cat_qpart = "cat_id IS NULL"; | |
1146 | } | |
1147 | ||
1148 | db_query("UPDATE ttrss_user_entries | |
1149 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1150 | (SELECT id FROM | |
1151 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1152 | AND owner_uid = $owner_uid AND unread = true AND feed_id IN | |
1153 | (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart AND $search_qpart) as tmp)"); | |
1154 | ||
1155 | } else if ($feed == -2) { | |
1156 | ||
1157 | db_query("UPDATE ttrss_user_entries | |
1158 | SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*) | |
1159 | FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart AND $search_qpart) > 0 | |
1160 | AND unread = true AND owner_uid = $owner_uid"); | |
1161 | } | |
1162 | ||
1163 | } else if ($feed > 0) { | |
1164 | ||
1165 | db_query("UPDATE ttrss_user_entries | |
1166 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1167 | (SELECT id FROM | |
1168 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1169 | AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart AND $search_qpart) as tmp)"); | |
1170 | ||
1171 | } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred | |
1172 | ||
1173 | if ($feed == -1) { | |
1174 | db_query("UPDATE ttrss_user_entries | |
1175 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1176 | (SELECT id FROM | |
1177 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1178 | AND owner_uid = $owner_uid AND unread = true AND marked = true AND $date_qpart AND $search_qpart) as tmp)"); | |
1179 | } | |
1180 | ||
1181 | if ($feed == -2) { | |
1182 | db_query("UPDATE ttrss_user_entries | |
1183 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1184 | (SELECT id FROM | |
1185 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1186 | AND owner_uid = $owner_uid AND unread = true AND published = true AND $date_qpart AND $search_qpart) as tmp)"); | |
1187 | } | |
1188 | ||
1189 | if ($feed == -3) { | |
1190 | ||
1191 | $intl = get_pref("FRESH_ARTICLE_MAX_AGE"); | |
1192 | ||
1193 | if (DB_TYPE == "pgsql") { | |
1194 | $match_part = "date_entered > NOW() - INTERVAL '$intl hour' "; | |
1195 | } else { | |
1196 | $match_part = "date_entered > DATE_SUB(NOW(), | |
1197 | INTERVAL $intl HOUR) "; | |
1198 | } | |
1199 | ||
1200 | db_query("UPDATE ttrss_user_entries | |
1201 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1202 | (SELECT id FROM | |
1203 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1204 | AND owner_uid = $owner_uid AND score >= 0 AND unread = true AND $date_qpart AND $match_part AND $search_qpart) as tmp)"); | |
1205 | } | |
1206 | ||
1207 | if ($feed == -4) { | |
1208 | db_query("UPDATE ttrss_user_entries | |
1209 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1210 | (SELECT id FROM | |
1211 | (SELECT DISTINCT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
1212 | AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)"); | |
1213 | } | |
1214 | ||
1215 | } else if ($feed < LABEL_BASE_INDEX) { // label | |
1216 | ||
1217 | $label_id = feed_to_label_id($feed); | |
1218 | ||
1219 | db_query("UPDATE ttrss_user_entries | |
1220 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1221 | (SELECT id FROM | |
1222 | (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id | |
1223 | AND label_id = '$label_id' AND ref_id = article_id | |
1224 | AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)"); | |
1225 | ||
1226 | } | |
1227 | ||
1228 | ccache_update($feed, $owner_uid, $cat_view); | |
1229 | ||
1230 | } else { // tag | |
1231 | db_query("UPDATE ttrss_user_entries | |
1232 | SET unread = false, last_read = NOW() WHERE ref_id IN | |
1233 | (SELECT id FROM | |
1234 | (SELECT DISTINCT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id | |
1235 | AND post_int_id = int_id AND tag_name = '$feed' | |
1236 | AND ttrss_user_entries.owner_uid = $owner_uid AND unread = true AND $date_qpart AND $search_qpart) as tmp)"); | |
1237 | ||
1238 | } | |
1239 | } | |
1240 | ||
1241 | function getAllCounters() { | |
1242 | $data = getGlobalCounters(); | |
1243 | ||
1244 | $data = array_merge($data, getVirtCounters()); | |
1245 | $data = array_merge($data, getLabelCounters()); | |
1246 | $data = array_merge($data, getFeedCounters()); | |
1247 | $data = array_merge($data, getCategoryCounters()); | |
1248 | ||
1249 | return $data; | |
1250 | } | |
1251 | ||
1252 | function getCategoryTitle($cat_id) { | |
1253 | ||
1254 | if ($cat_id == -1) { | |
1255 | return __("Special"); | |
1256 | } else if ($cat_id == -2) { | |
1257 | return __("Labels"); | |
1258 | } else { | |
1259 | ||
1260 | $result = db_query("SELECT title FROM ttrss_feed_categories WHERE | |
1261 | id = '$cat_id'"); | |
1262 | ||
1263 | if (db_num_rows($result) == 1) { | |
1264 | return db_fetch_result($result, 0, "title"); | |
1265 | } else { | |
1266 | return __("Uncategorized"); | |
1267 | } | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | ||
1272 | function getCategoryCounters() { | |
1273 | $ret_arr = array(); | |
1274 | ||
1275 | /* Labels category */ | |
1276 | ||
1277 | $cv = array("id" => -2, "kind" => "cat", | |
1278 | "counter" => getCategoryUnread(-2)); | |
1279 | ||
1280 | array_push($ret_arr, $cv); | |
1281 | ||
1282 | $result = db_query("SELECT id AS cat_id, value AS unread, | |
1283 | (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 | |
1284 | WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children | |
1285 | FROM ttrss_feed_categories, ttrss_cat_counters_cache | |
1286 | WHERE ttrss_cat_counters_cache.feed_id = id AND | |
1287 | ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND | |
1288 | ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]); | |
1289 | ||
1290 | while ($line = db_fetch_assoc($result)) { | |
1291 | $line["cat_id"] = (int) $line["cat_id"]; | |
1292 | ||
1293 | if ($line["num_children"] > 0) { | |
1294 | $child_counter = getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]); | |
1295 | } else { | |
1296 | $child_counter = 0; | |
1297 | } | |
1298 | ||
1299 | $cv = array("id" => $line["cat_id"], "kind" => "cat", | |
1300 | "counter" => $line["unread"] + $child_counter); | |
1301 | ||
1302 | array_push($ret_arr, $cv); | |
1303 | } | |
1304 | ||
1305 | /* Special case: NULL category doesn't actually exist in the DB */ | |
1306 | ||
1307 | $cv = array("id" => 0, "kind" => "cat", | |
1308 | "counter" => (int) ccache_find(0, $_SESSION["uid"], true)); | |
1309 | ||
1310 | array_push($ret_arr, $cv); | |
1311 | ||
1312 | return $ret_arr; | |
1313 | } | |
1314 | ||
1315 | // only accepts real cats (>= 0) | |
1316 | function getCategoryChildrenUnread($cat, $owner_uid = false) { | |
1317 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1318 | ||
1319 | $result = db_query("SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat' | |
1320 | AND owner_uid = $owner_uid"); | |
1321 | ||
1322 | $unread = 0; | |
1323 | ||
1324 | while ($line = db_fetch_assoc($result)) { | |
1325 | $unread += getCategoryUnread($line["id"], $owner_uid); | |
1326 | $unread += getCategoryChildrenUnread($line["id"], $owner_uid); | |
1327 | } | |
1328 | ||
1329 | return $unread; | |
1330 | } | |
1331 | ||
1332 | function getCategoryUnread($cat, $owner_uid = false) { | |
1333 | ||
1334 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1335 | ||
1336 | if ($cat >= 0) { | |
1337 | ||
1338 | if ($cat != 0) { | |
1339 | $cat_query = "cat_id = '$cat'"; | |
1340 | } else { | |
1341 | $cat_query = "cat_id IS NULL"; | |
1342 | } | |
1343 | ||
1344 | $result = db_query("SELECT id FROM ttrss_feeds WHERE $cat_query | |
1345 | AND owner_uid = " . $owner_uid); | |
1346 | ||
1347 | $cat_feeds = array(); | |
1348 | while ($line = db_fetch_assoc($result)) { | |
1349 | array_push($cat_feeds, "feed_id = " . $line["id"]); | |
1350 | } | |
1351 | ||
1352 | if (count($cat_feeds) == 0) return 0; | |
1353 | ||
1354 | $match_part = implode(" OR ", $cat_feeds); | |
1355 | ||
1356 | $result = db_query("SELECT COUNT(int_id) AS unread | |
1357 | FROM ttrss_user_entries | |
1358 | WHERE unread = true AND ($match_part) | |
1359 | AND owner_uid = " . $owner_uid); | |
1360 | ||
1361 | $unread = 0; | |
1362 | ||
1363 | # this needs to be rewritten | |
1364 | while ($line = db_fetch_assoc($result)) { | |
1365 | $unread += $line["unread"]; | |
1366 | } | |
1367 | ||
1368 | return $unread; | |
1369 | } else if ($cat == -1) { | |
1370 | return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0); | |
1371 | } else if ($cat == -2) { | |
1372 | ||
1373 | $result = db_query(" | |
1374 | SELECT COUNT(unread) AS unread FROM | |
1375 | ttrss_user_entries, ttrss_user_labels2 | |
1376 | WHERE article_id = ref_id AND unread = true | |
1377 | AND ttrss_user_entries.owner_uid = '$owner_uid'"); | |
1378 | ||
1379 | $unread = db_fetch_result($result, 0, "unread"); | |
1380 | ||
1381 | return $unread; | |
1382 | ||
1383 | } | |
1384 | } | |
1385 | ||
1386 | function getFeedUnread($feed, $is_cat = false) { | |
1387 | return getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]); | |
1388 | } | |
1389 | ||
1390 | function getLabelUnread($label_id, $owner_uid = false) { | |
1391 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1392 | ||
1393 | $result = db_query("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2 | |
1394 | WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id"); | |
1395 | ||
1396 | if (db_num_rows($result) != 0) { | |
1397 | return db_fetch_result($result, 0, "unread"); | |
1398 | } else { | |
1399 | return 0; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | function getFeedArticles($feed, $is_cat = false, $unread_only = false, | |
1404 | $owner_uid = false) { | |
1405 | ||
1406 | $n_feed = (int) $feed; | |
1407 | $need_entries = false; | |
1408 | ||
1409 | if (!$owner_uid) $owner_uid = $_SESSION["uid"]; | |
1410 | ||
1411 | if ($unread_only) { | |
1412 | $unread_qpart = "unread = true"; | |
1413 | } else { | |
1414 | $unread_qpart = "true"; | |
1415 | } | |
1416 | ||
1417 | if ($is_cat) { | |
1418 | return getCategoryUnread($n_feed, $owner_uid); | |
1419 | } else if ($n_feed == -6) { | |
1420 | return 0; | |
1421 | } else if ($feed != "0" && $n_feed == 0) { | |
1422 | ||
1423 | $feed = db_escape_string($feed); | |
1424 | ||
1425 | $result = db_query("SELECT SUM((SELECT COUNT(int_id) | |
1426 | FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id | |
1427 | AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags | |
1428 | WHERE owner_uid = $owner_uid AND tag_name = '$feed'"); | |
1429 | return db_fetch_result($result, 0, "count"); | |
1430 | ||
1431 | } else if ($n_feed == -1) { | |
1432 | $match_part = "marked = true"; | |
1433 | } else if ($n_feed == -2) { | |
1434 | $match_part = "published = true"; | |
1435 | } else if ($n_feed == -3) { | |
1436 | $match_part = "unread = true AND score >= 0"; | |
1437 | ||
1438 | $intl = get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid); | |
1439 | ||
1440 | if (DB_TYPE == "pgsql") { | |
1441 | $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' "; | |
1442 | } else { | |
1443 | $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) "; | |
1444 | } | |
1445 | ||
1446 | $need_entries = true; | |
1447 | ||
1448 | } else if ($n_feed == -4) { | |
1449 | $match_part = "true"; | |
1450 | } else if ($n_feed >= 0) { | |
1451 | ||
1452 | if ($n_feed != 0) { | |
1453 | $match_part = "feed_id = '$n_feed'"; | |
1454 | } else { | |
1455 | $match_part = "feed_id IS NULL"; | |
1456 | } | |
1457 | ||
1458 | } else if ($feed < LABEL_BASE_INDEX) { | |
1459 | ||
1460 | $label_id = feed_to_label_id($feed); | |
1461 | ||
1462 | return getLabelUnread($label_id, $owner_uid); | |
1463 | ||
1464 | } | |
1465 | ||
1466 | if ($match_part) { | |
1467 | ||
1468 | if ($need_entries) { | |
1469 | $from_qpart = "ttrss_user_entries,ttrss_entries"; | |
1470 | $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND"; | |
1471 | } else { | |
1472 | $from_qpart = "ttrss_user_entries"; | |
1473 | $from_where = ""; | |
1474 | } | |
1475 | ||
1476 | $query = "SELECT count(int_id) AS unread | |
1477 | FROM $from_qpart WHERE | |
1478 | $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid"; | |
1479 | ||
1480 | //echo "[$feed/$query]\n"; | |
1481 | ||
1482 | $result = db_query($query); | |
1483 | ||
1484 | } else { | |
1485 | ||
1486 | $result = db_query("SELECT COUNT(post_int_id) AS unread | |
1487 | FROM ttrss_tags,ttrss_user_entries,ttrss_entries | |
1488 | WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id | |
1489 | AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid); | |
1490 | } | |
1491 | ||
1492 | $unread = db_fetch_result($result, 0, "unread"); | |
1493 | ||
1494 | return $unread; | |
1495 | } | |
1496 | ||
1497 | function getGlobalUnread($user_id = false) { | |
1498 | ||
1499 | if (!$user_id) { | |
1500 | $user_id = $_SESSION["uid"]; | |
1501 | } | |
1502 | ||
1503 | $result = db_query("SELECT SUM(value) AS c_id FROM ttrss_counters_cache | |
1504 | WHERE owner_uid = '$user_id' AND feed_id > 0"); | |
1505 | ||
1506 | $c_id = db_fetch_result($result, 0, "c_id"); | |
1507 | ||
1508 | return $c_id; | |
1509 | } | |
1510 | ||
1511 | function getGlobalCounters($global_unread = -1) { | |
1512 | $ret_arr = array(); | |
1513 | ||
1514 | if ($global_unread == -1) { | |
1515 | $global_unread = getGlobalUnread(); | |
1516 | } | |
1517 | ||
1518 | $cv = array("id" => "global-unread", | |
1519 | "counter" => (int) $global_unread); | |
1520 | ||
1521 | array_push($ret_arr, $cv); | |
1522 | ||
1523 | $result = db_query("SELECT COUNT(id) AS fn FROM | |
1524 | ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]); | |
1525 | ||
1526 | $subscribed_feeds = db_fetch_result($result, 0, "fn"); | |
1527 | ||
1528 | $cv = array("id" => "subscribed-feeds", | |
1529 | "counter" => (int) $subscribed_feeds); | |
1530 | ||
1531 | array_push($ret_arr, $cv); | |
1532 | ||
1533 | return $ret_arr; | |
1534 | } | |
1535 | ||
1536 | function getVirtCounters() { | |
1537 | ||
1538 | $ret_arr = array(); | |
1539 | ||
1540 | for ($i = 0; $i >= -4; $i--) { | |
1541 | ||
1542 | $count = getFeedUnread($i); | |
1543 | ||
1544 | if ($i == 0 || $i == -1 || $i == -2) | |
1545 | $auxctr = getFeedArticles($i, false); | |
1546 | else | |
1547 | $auxctr = 0; | |
1548 | ||
1549 | $cv = array("id" => $i, | |
1550 | "counter" => (int) $count, | |
1551 | "auxcounter" => (int) $auxctr); | |
1552 | ||
1553 | // if (get_pref('EXTENDED_FEEDLIST')) | |
1554 | // $cv["xmsg"] = getFeedArticles($i)." ".__("total"); | |
1555 | ||
1556 | array_push($ret_arr, $cv); | |
1557 | } | |
1558 | ||
1559 | $feeds = PluginHost::getInstance()->get_feeds(-1); | |
1560 | ||
1561 | if (is_array($feeds)) { | |
1562 | foreach ($feeds as $feed) { | |
1563 | $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']), | |
1564 | "counter" => $feed['sender']->get_unread($feed['id'])); | |
1565 | ||
1566 | if (method_exists($feed['sender'], 'get_total')) | |
1567 | $cv["auxcounter"] = $feed['sender']->get_total($feed['id']); | |
1568 | ||
1569 | array_push($ret_arr, $cv); | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | return $ret_arr; | |
1574 | } | |
1575 | ||
1576 | function getLabelCounters($descriptions = false) { | |
1577 | ||
1578 | $ret_arr = array(); | |
1579 | ||
1580 | $owner_uid = $_SESSION["uid"]; | |
1581 | ||
1582 | $result = db_query("SELECT id,caption,SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread, COUNT(u1.unread) AS total | |
1583 | FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON | |
1584 | (ttrss_labels2.id = label_id) | |
1585 | LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id | |
1586 | WHERE ttrss_labels2.owner_uid = $owner_uid AND u1.owner_uid = $owner_uid | |
1587 | GROUP BY ttrss_labels2.id, | |
1588 | ttrss_labels2.caption"); | |
1589 | ||
1590 | while ($line = db_fetch_assoc($result)) { | |
1591 | ||
1592 | $id = label_to_feed_id($line["id"]); | |
1593 | ||
1594 | $cv = array("id" => $id, | |
1595 | "counter" => (int) $line["unread"], | |
1596 | "auxcounter" => (int) $line["total"]); | |
1597 | ||
1598 | if ($descriptions) | |
1599 | $cv["description"] = $line["caption"]; | |
1600 | ||
1601 | array_push($ret_arr, $cv); | |
1602 | } | |
1603 | ||
1604 | return $ret_arr; | |
1605 | } | |
1606 | ||
1607 | function getFeedCounters($active_feed = false) { | |
1608 | ||
1609 | $ret_arr = array(); | |
1610 | ||
1611 | $query = "SELECT ttrss_feeds.id, | |
1612 | ttrss_feeds.title, | |
1613 | ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated, | |
1614 | last_error, value AS count | |
1615 | FROM ttrss_feeds, ttrss_counters_cache | |
1616 | WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]." | |
1617 | AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid | |
1618 | AND ttrss_counters_cache.feed_id = id"; | |
1619 | ||
1620 | $result = db_query($query); | |
1621 | ||
1622 | while ($line = db_fetch_assoc($result)) { | |
1623 | ||
1624 | $id = $line["id"]; | |
1625 | $count = $line["count"]; | |
1626 | $last_error = htmlspecialchars($line["last_error"]); | |
1627 | ||
1628 | $last_updated = make_local_datetime($line['last_updated'], false); | |
1629 | ||
1630 | $has_img = feed_has_icon($id); | |
1631 | ||
1632 | if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2) | |
1633 | $last_updated = ''; | |
1634 | ||
1635 | $cv = array("id" => $id, | |
1636 | "updated" => $last_updated, | |
1637 | "counter" => (int) $count, | |
1638 | "has_img" => (int) $has_img); | |
1639 | ||
1640 | if ($last_error) | |
1641 | $cv["error"] = $last_error; | |
1642 | ||
1643 | // if (get_pref('EXTENDED_FEEDLIST')) | |
1644 | // $cv["xmsg"] = getFeedArticles($id)." ".__("total"); | |
1645 | ||
1646 | if ($active_feed && $id == $active_feed) | |
1647 | $cv["title"] = truncate_string($line["title"], 30); | |
1648 | ||
1649 | array_push($ret_arr, $cv); | |
1650 | ||
1651 | } | |
1652 | ||
1653 | return $ret_arr; | |
1654 | } | |
1655 | ||
1656 | function get_pgsql_version() { | |
1657 | $result = db_query("SELECT version() AS version"); | |
1658 | $version = explode(" ", db_fetch_result($result, 0, "version")); | |
1659 | return $version[1]; | |
1660 | } | |
1661 | ||
1662 | /** | |
1663 | * @return array (code => Status code, message => error message if available) | |
1664 | * | |
1665 | * 0 - OK, Feed already exists | |
1666 | * 1 - OK, Feed added | |
1667 | * 2 - Invalid URL | |
1668 | * 3 - URL content is HTML, no feeds available | |
1669 | * 4 - URL content is HTML which contains multiple feeds. | |
1670 | * Here you should call extractfeedurls in rpc-backend | |
1671 | * to get all possible feeds. | |
1672 | * 5 - Couldn't download the URL content. | |
1673 | * 6 - Content is an invalid XML. | |
1674 | */ | |
1675 | function subscribe_to_feed($url, $cat_id = 0, | |
1676 | $auth_login = '', $auth_pass = '') { | |
1677 | ||
1678 | global $fetch_last_error; | |
1679 | global $fetch_last_error_content; | |
1680 | ||
1681 | require_once "include/rssfuncs.php"; | |
1682 | ||
1683 | $url = fix_url($url); | |
1684 | ||
1685 | if (!$url || !validate_feed_url($url)) return array("code" => 2); | |
1686 | ||
1687 | $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass); | |
1688 | ||
1689 | if (!$contents) { | |
1690 | if (preg_match("/cloudflare\.com/", $fetch_last_error_content)) { | |
1691 | $fetch_last_error .= " (feed behind Cloudflare)"; | |
1692 | } | |
1693 | ||
1694 | return array("code" => 5, "message" => $fetch_last_error); | |
1695 | } | |
1696 | ||
1697 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SUBSCRIBE_FEED) as $plugin) { | |
1698 | $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass); | |
1699 | } | |
1700 | ||
1701 | if (is_html($contents)) { | |
1702 | $feedUrls = get_feeds_from_html($url, $contents); | |
1703 | ||
1704 | if (count($feedUrls) == 0) { | |
1705 | return array("code" => 3); | |
1706 | } else if (count($feedUrls) > 1) { | |
1707 | return array("code" => 4, "feeds" => $feedUrls); | |
1708 | } | |
1709 | //use feed url as new URL | |
1710 | $url = key($feedUrls); | |
1711 | } | |
1712 | ||
1713 | if ($cat_id == "0" || !$cat_id) { | |
1714 | $cat_qpart = "NULL"; | |
1715 | } else { | |
1716 | $cat_qpart = "'$cat_id'"; | |
1717 | } | |
1718 | ||
1719 | $result = db_query( | |
1720 | "SELECT id FROM ttrss_feeds | |
1721 | WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]); | |
1722 | ||
1723 | $auth_pass_encrypted = 'false'; | |
1724 | $auth_pass = db_escape_string($auth_pass); | |
1725 | ||
1726 | if (db_num_rows($result) == 0) { | |
1727 | $result = db_query( | |
1728 | "INSERT INTO ttrss_feeds | |
1729 | (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted) | |
1730 | VALUES ('".$_SESSION["uid"]."', '$url', | |
1731 | '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', 0, $auth_pass_encrypted)"); | |
1732 | ||
1733 | $result = db_query( | |
1734 | "SELECT id FROM ttrss_feeds WHERE feed_url = '$url' | |
1735 | AND owner_uid = " . $_SESSION["uid"]); | |
1736 | ||
1737 | $feed_id = db_fetch_result($result, 0, "id"); | |
1738 | ||
1739 | if ($feed_id) { | |
1740 | set_basic_feed_info($feed_id); | |
1741 | } | |
1742 | ||
1743 | return array("code" => 1, "feed_id" => (int) $feed_id); | |
1744 | } else { | |
1745 | return array("code" => 0, "feed_id" => (int) db_fetch_result($result, 0, "id")); | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | function checkbox_to_sql_bool($val) { | |
1750 | return ($val == "on") ? "true" : "false"; | |
1751 | } | |
1752 | ||
1753 | function getFeedCatTitle($id) { | |
1754 | if ($id == -1) { | |
1755 | return __("Special"); | |
1756 | } else if ($id < LABEL_BASE_INDEX) { | |
1757 | return __("Labels"); | |
1758 | } else if ($id > 0) { | |
1759 | $result = db_query("SELECT ttrss_feed_categories.title | |
1760 | FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND | |
1761 | cat_id = ttrss_feed_categories.id"); | |
1762 | if (db_num_rows($result) == 1) { | |
1763 | return db_fetch_result($result, 0, "title"); | |
1764 | } else { | |
1765 | return __("Uncategorized"); | |
1766 | } | |
1767 | } else { | |
1768 | return "getFeedCatTitle($id) failed"; | |
1769 | } | |
1770 | ||
1771 | } | |
1772 | ||
1773 | function getFeedIcon($id) { | |
1774 | switch ($id) { | |
1775 | case 0: | |
1776 | return "images/archive.png"; | |
1777 | break; | |
1778 | case -1: | |
1779 | return "images/star.png"; | |
1780 | break; | |
1781 | case -2: | |
1782 | return "images/feed.png"; | |
1783 | break; | |
1784 | case -3: | |
1785 | return "images/fresh.png"; | |
1786 | break; | |
1787 | case -4: | |
1788 | return "images/folder.png"; | |
1789 | break; | |
1790 | case -6: | |
1791 | return "images/time.png"; | |
1792 | break; | |
1793 | default: | |
1794 | if ($id < LABEL_BASE_INDEX) { | |
1795 | return "images/label.png"; | |
1796 | } else { | |
1797 | if (file_exists(ICONS_DIR . "/$id.ico")) | |
1798 | return ICONS_URL . "/$id.ico"; | |
1799 | } | |
1800 | break; | |
1801 | } | |
1802 | ||
1803 | return false; | |
1804 | } | |
1805 | ||
1806 | function getFeedTitle($id, $cat = false) { | |
1807 | if ($cat) { | |
1808 | return getCategoryTitle($id); | |
1809 | } else if ($id == -1) { | |
1810 | return __("Starred articles"); | |
1811 | } else if ($id == -2) { | |
1812 | return __("Published articles"); | |
1813 | } else if ($id == -3) { | |
1814 | return __("Fresh articles"); | |
1815 | } else if ($id == -4) { | |
1816 | return __("All articles"); | |
1817 | } else if ($id === 0 || $id === "0") { | |
1818 | return __("Archived articles"); | |
1819 | } else if ($id == -6) { | |
1820 | return __("Recently read"); | |
1821 | } else if ($id < LABEL_BASE_INDEX) { | |
1822 | $label_id = feed_to_label_id($id); | |
1823 | $result = db_query("SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'"); | |
1824 | if (db_num_rows($result) == 1) { | |
1825 | return db_fetch_result($result, 0, "caption"); | |
1826 | } else { | |
1827 | return "Unknown label ($label_id)"; | |
1828 | } | |
1829 | ||
1830 | } else if (is_numeric($id) && $id > 0) { | |
1831 | $result = db_query("SELECT title FROM ttrss_feeds WHERE id = '$id'"); | |
1832 | if (db_num_rows($result) == 1) { | |
1833 | return db_fetch_result($result, 0, "title"); | |
1834 | } else { | |
1835 | return "Unknown feed ($id)"; | |
1836 | } | |
1837 | } else { | |
1838 | return $id; | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | function uniqid_short() { | |
1843 | return uniqid(base_convert(rand(), 10, 36)); | |
1844 | } | |
1845 | ||
1846 | // TODO: less dumb splitting | |
1847 | require_once "functions2.php"; |