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