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