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