]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
6e658547 | 2 | define('EXPECTED_CONFIG_VERSION', 26); |
3b19a53a | 3 | define('SCHEMA_VERSION', 124); |
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; |
3f6f0857 | 13 | $fetch_curl_used = false; |
4f71d743 | 14 | $suppress_debugging = false; |
23d2471c | 15 | |
d68629dc | 16 | mb_internal_encoding("UTF-8"); |
324944f3 | 17 | date_default_timezone_set('UTC'); |
8a7f5767 CW |
18 | if (defined('E_DEPRECATED')) { |
19 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED); | |
20 | } else { | |
21 | error_reporting(E_ALL & ~E_NOTICE); | |
22 | } | |
cce28758 | 23 | |
40d13c28 | 24 | require_once 'config.php'; |
cc17c205 | 25 | |
046ec657 BK |
26 | /** |
27 | * Define a constant if not already defined | |
28 | * | |
29 | * @param string $name The constant name. | |
30 | * @param mixed $value The constant value. | |
31 | * @access public | |
32 | * @return boolean True if defined successfully or not. | |
33 | */ | |
34 | function define_default($name, $value) { | |
046ec657 BK |
35 | defined($name) or define($name, $value); |
36 | } | |
37 | ||
9ef8798b BK |
38 | ///// Some defaults that you can override in config.php ////// |
39 | ||
40 | define_default('FEED_FETCH_TIMEOUT', 45); | |
41 | // How may seconds to wait for response when requesting feed from a site | |
42 | define_default('FEED_FETCH_NO_CACHE_TIMEOUT', 15); | |
43 | // How may seconds to wait for response when requesting feed from a | |
44 | // site when that feed wasn't cached before | |
45 | define_default('FILE_FETCH_TIMEOUT', 45); | |
46 | // Default timeout when fetching files from remote sites | |
47 | define_default('FILE_FETCH_CONNECT_TIMEOUT', 15); | |
48 | // How many seconds to wait for initial response from website when | |
49 | // fetching files from remote sites | |
046ec657 | 50 | |
fc2b26a6 AD |
51 | if (DB_TYPE == "pgsql") { |
52 | define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE'); | |
53 | } else { | |
54 | define('SUBSTRING_FOR_DATE', 'SUBSTRING'); | |
55 | } | |
56 | ||
9632f884 AD |
57 | /** |
58 | * Return available translations names. | |
8d505d78 | 59 | * |
9632f884 AD |
60 | * @access public |
61 | * @return array A array of available translations. | |
62 | */ | |
f8c612d4 | 63 | function get_translations() { |
6a214f92 | 64 | $tr = array( |
8d505d78 | 65 | "auto" => "Detect automatically", |
a3162add | 66 | "ca_CA" => "Català", |
a06b79c4 | 67 | "cs_CZ" => "Česky", |
6a214f92 | 68 | "en_US" => "English", |
36d0510c | 69 | "es_ES" => "Español", |
a927fe7b | 70 | "de_DE" => "Deutsch", |
6a214f92 | 71 | "fr_FR" => "Français", |
e78fd196 | 72 | "hu_HU" => "Magyar (Hungarian)", |
bb5d3960 | 73 | "it_IT" => "Italiano", |
1d004f12 | 74 | "ja_JP" => "日本語 (Japanese)", |
7b6c1ca7 | 75 | "lv_LV" => "Latviešu", |
592535d7 | 76 | "nb_NO" => "Norwegian bokmål", |
9e7f1f12 | 77 | "nl_NL" => "Dutch", |
ea45791a | 78 | "pl_PL" => "Polski", |
d3b923c9 | 79 | "ru_RU" => "Русский", |
9a063469 | 80 | "pt_BR" => "Portuguese/Brazil", |
d8cdb08f | 81 | "pt_PT" => "Portuguese/Portugal", |
5d608138 | 82 | "zh_CN" => "Simplified Chinese", |
8dc5e7f0 | 83 | "zh_TW" => "Traditional Chinese", |
2324f153 | 84 | "sv_SE" => "Svenska", |
76d78eb2 | 85 | "fi_FI" => "Suomi", |
42a5abdc | 86 | "tr_TR" => "Türkçe"); |
f8c612d4 AD |
87 | |
88 | return $tr; | |
89 | } | |
90 | ||
7b26a148 AD |
91 | require_once "lib/accept-to-gettext.php"; |
92 | require_once "lib/gettext/gettext.inc"; | |
aba609e0 | 93 | |
6b461797 | 94 | require_once "lib/languagedetect/LanguageDetect.php"; |
87d7e850 | 95 | |
7b26a148 | 96 | function startup_gettext() { |
8d505d78 | 97 | |
7b26a148 AD |
98 | # Get locale from Accept-Language header |
99 | $lang = al2gt(array_keys(get_translations()), "text/html"); | |
89cb787e | 100 | |
7b26a148 AD |
101 | if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) { |
102 | $lang = _TRANSLATION_OVERRIDE_DEFAULT; | |
103 | } | |
89cb787e | 104 | |
b18d109f AD |
105 | if ($_SESSION["uid"] && get_schema_version() >= 120) { |
106 | $pref_lang = get_pref("USER_LANGUAGE", $_SESSION["uid"]); | |
1ee4900a | 107 | |
c4db796f | 108 | if ($pref_lang && $pref_lang != 'auto') { |
7b149552 AD |
109 | $lang = $pref_lang; |
110 | } | |
7b26a148 | 111 | } |
7c33dbd4 | 112 | |
7b26a148 AD |
113 | if ($lang) { |
114 | if (defined('LC_MESSAGES')) { | |
115 | _setlocale(LC_MESSAGES, $lang); | |
116 | } else if (defined('LC_ALL')) { | |
117 | _setlocale(LC_ALL, $lang); | |
8d039718 | 118 | } |
aba609e0 | 119 | |
d98e76d9 | 120 | _bindtextdomain("messages", "locale"); |
865220a4 | 121 | |
7b26a148 AD |
122 | _textdomain("messages"); |
123 | _bind_textdomain_codeset("messages", "UTF-8"); | |
865220a4 | 124 | } |
7b26a148 AD |
125 | } |
126 | ||
b619ff15 | 127 | require_once 'db-prefs.php'; |
8911ac8b | 128 | require_once 'version.php'; |
87d7e850 AD |
129 | require_once 'ccache.php'; |
130 | require_once 'labels.php'; | |
40d13c28 | 131 | |
fb850eec | 132 | define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)'); |
500943a4 AD |
133 | ini_set('user_agent', SELF_USER_AGENT); |
134 | ||
b0f379df | 135 | require_once 'lib/pubsubhubbub/publisher.php'; |
010efc9b | 136 | |
7d96bfcd AD |
137 | $schema_version = false; |
138 | ||
4f71d743 AD |
139 | function _debug_suppress($suppress) { |
140 | global $suppress_debugging; | |
141 | ||
142 | $suppress_debugging = $suppress; | |
143 | } | |
144 | ||
45004d43 AD |
145 | /** |
146 | * Print a timestamped debug message. | |
8d505d78 | 147 | * |
45004d43 AD |
148 | * @param string $msg The debug message. |
149 | * @return void | |
150 | */ | |
68cccafc | 151 | function _debug($msg, $show = true) { |
4f71d743 AD |
152 | global $suppress_debugging; |
153 | ||
154 | //echo "[$suppress_debugging] $msg $show\n"; | |
155 | ||
156 | if ($suppress_debugging) return false; | |
9ec10352 | 157 | |
6f9e33e4 | 158 | $ts = strftime("%H:%M:%S", time()); |
2a6a9395 AD |
159 | if (function_exists('posix_getpid')) { |
160 | $ts = "$ts/" . posix_getpid(); | |
161 | } | |
2191eb7a | 162 | |
68cccafc | 163 | if ($show && !(defined('QUIET') && QUIET)) { |
2191eb7a AD |
164 | print "[$ts] $msg\n"; |
165 | } | |
166 | ||
167 | if (defined('LOGFILE')) { | |
168 | $fp = fopen(LOGFILE, 'a+'); | |
169 | ||
170 | if ($fp) { | |
a33558a6 AD |
171 | $locked = false; |
172 | ||
173 | if (function_exists("flock")) { | |
174 | $tries = 0; | |
175 | ||
176 | // try to lock logfile for writing | |
177 | while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) { | |
178 | sleep(1); | |
179 | ++$tries; | |
180 | } | |
181 | ||
182 | if (!$locked) { | |
183 | fclose($fp); | |
184 | return; | |
185 | } | |
186 | } | |
187 | ||
2191eb7a | 188 | fputs($fp, "[$ts] $msg\n"); |
a33558a6 AD |
189 | |
190 | if (function_exists("flock")) { | |
191 | flock($fp, LOCK_UN); | |
192 | } | |
193 | ||
2191eb7a AD |
194 | fclose($fp); |
195 | } | |
196 | } | |
197 | ||
45004d43 | 198 | } // function _debug |
6f9e33e4 | 199 | |
9632f884 AD |
200 | /** |
201 | * Purge a feed old posts. | |
8d505d78 | 202 | * |
9632f884 AD |
203 | * @param mixed $link A database connection. |
204 | * @param mixed $feed_id The id of the purged feed. | |
205 | * @param mixed $purge_interval Olderness of purged posts. | |
206 | * @param boolean $debug Set to True to enable the debug. False by default. | |
207 | * @access public | |
208 | * @return void | |
209 | */ | |
a42c55f0 | 210 | function purge_feed($feed_id, $purge_interval, $debug = false) { |
ad507f85 | 211 | |
a42c55f0 | 212 | if (!$purge_interval) $purge_interval = feed_purge_interval($feed_id); |
8d505d78 | 213 | |
ad507f85 | 214 | $rows = -1; |
4c193675 | 215 | |
6322ac79 | 216 | $result = db_query( |
07d0efe9 AD |
217 | "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'"); |
218 | ||
219 | $owner_uid = false; | |
220 | ||
221 | if (db_num_rows($result) == 1) { | |
222 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
223 | } | |
224 | ||
ab954dff AD |
225 | if ($purge_interval == -1 || !$purge_interval) { |
226 | if ($owner_uid) { | |
a42c55f0 | 227 | ccache_update($feed_id, $owner_uid); |
ab954dff AD |
228 | } |
229 | return; | |
230 | } | |
231 | ||
07d0efe9 AD |
232 | if (!$owner_uid) return; |
233 | ||
3907ef71 | 234 | if (FORCE_ARTICLE_PURGE == 0) { |
a42c55f0 | 235 | $purge_unread = get_pref("PURGE_UNREAD_ARTICLES", |
3907ef71 AD |
236 | $owner_uid, false); |
237 | } else { | |
238 | $purge_unread = true; | |
239 | $purge_interval = FORCE_ARTICLE_PURGE; | |
240 | } | |
07d0efe9 AD |
241 | |
242 | if (!$purge_unread) $query_limit = " unread = false AND "; | |
243 | ||
fefa6ca3 | 244 | if (DB_TYPE == "pgsql") { |
6322ac79 | 245 | $pg_version = get_pgsql_version(); |
6e7f8d26 AD |
246 | |
247 | if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) { | |
1e59ae35 | 248 | |
a42c55f0 | 249 | $result = db_query("DELETE FROM ttrss_user_entries WHERE |
8d505d78 AD |
250 | ttrss_entries.id = ref_id AND |
251 | marked = false AND | |
252 | feed_id = '$feed_id' AND | |
07d0efe9 | 253 | $query_limit |
25ea2805 | 254 | ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 AD |
255 | |
256 | } else { | |
257 | ||
a42c55f0 | 258 | $result = db_query("DELETE FROM ttrss_user_entries |
8d505d78 AD |
259 | USING ttrss_entries |
260 | WHERE ttrss_entries.id = ref_id AND | |
261 | marked = false AND | |
262 | feed_id = '$feed_id' AND | |
07d0efe9 | 263 | $query_limit |
25ea2805 | 264 | ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'"); |
1e59ae35 | 265 | } |
ad507f85 | 266 | |
fefa6ca3 | 267 | } else { |
8d505d78 | 268 | |
a42c55f0 | 269 | /* $result = db_query("DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 270 | marked = false AND feed_id = '$feed_id' AND |
8d505d78 | 271 | (SELECT date_updated FROM ttrss_entries WHERE |
30f1746f AD |
272 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */ |
273 | ||
a42c55f0 | 274 | $result = db_query("DELETE FROM ttrss_user_entries |
8d505d78 AD |
275 | USING ttrss_user_entries, ttrss_entries |
276 | WHERE ttrss_entries.id = ref_id AND | |
277 | marked = false AND | |
278 | feed_id = '$feed_id' AND | |
07d0efe9 | 279 | $query_limit |
25ea2805 | 280 | ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); |
ad507f85 AD |
281 | } |
282 | ||
a08f94bd | 283 | $rows = db_affected_rows($result); |
3f6f0857 | 284 | |
a42c55f0 | 285 | ccache_update($feed_id, $owner_uid); |
ced46404 | 286 | |
ad507f85 | 287 | if ($debug) { |
6f9e33e4 | 288 | _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles"); |
fefa6ca3 | 289 | } |
2ea09bde AD |
290 | |
291 | return $rows; | |
9632f884 | 292 | } // function purge_feed |
fefa6ca3 | 293 | |
a42c55f0 | 294 | function feed_purge_interval($feed_id) { |
07d0efe9 | 295 | |
a42c55f0 | 296 | $result = db_query("SELECT purge_interval, owner_uid FROM ttrss_feeds |
07d0efe9 AD |
297 | WHERE id = '$feed_id'"); |
298 | ||
299 | if (db_num_rows($result) == 1) { | |
300 | $purge_interval = db_fetch_result($result, 0, "purge_interval"); | |
301 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
302 | ||
6322ac79 | 303 | if ($purge_interval == 0) $purge_interval = get_pref( |
863be6ca | 304 | 'PURGE_OLD_DAYS', $owner_uid); |
07d0efe9 AD |
305 | |
306 | return $purge_interval; | |
307 | ||
308 | } else { | |
309 | return -1; | |
310 | } | |
311 | } | |
312 | ||
a42c55f0 | 313 | function purge_orphans($do_output = false) { |
a2d79981 | 314 | |
71604ca4 | 315 | // purge orphaned posts in main content table |
a42c55f0 | 316 | $result = db_query("DELETE FROM ttrss_entries WHERE |
71604ca4 | 317 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); |
a2d79981 AD |
318 | |
319 | if ($do_output) { | |
a42c55f0 | 320 | $rows = db_affected_rows($result); |
a2d79981 AD |
321 | _debug("Purged $rows orphaned posts."); |
322 | } | |
c3a8d71a AD |
323 | } |
324 | ||
a42c55f0 AD |
325 | function get_feed_update_interval($feed_id) { |
326 | $result = db_query("SELECT owner_uid, update_interval FROM | |
c7d57b66 AD |
327 | ttrss_feeds WHERE id = '$feed_id'"); |
328 | ||
329 | if (db_num_rows($result) == 1) { | |
330 | $update_interval = db_fetch_result($result, 0, "update_interval"); | |
331 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); | |
332 | ||
333 | if ($update_interval != 0) { | |
334 | return $update_interval; | |
335 | } else { | |
a42c55f0 | 336 | return get_pref('DEFAULT_UPDATE_INTERVAL', $owner_uid, false); |
c7d57b66 AD |
337 | } |
338 | ||
339 | } else { | |
340 | return -1; | |
341 | } | |
342 | } | |
343 | ||
f826070c | 344 | function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false, $timeout = false, $timestamp = 0, $useragent = false) { |
8d505d78 | 345 | |
23d2471c | 346 | global $fetch_last_error; |
7a01dc77 | 347 | global $fetch_last_error_code; |
ef39be2b | 348 | global $fetch_last_content_type; |
3f6f0857 | 349 | global $fetch_curl_used; |
33373eca | 350 | |
2375b6b6 | 351 | $url = str_replace(' ', '%20', $url); |
23d2471c | 352 | |
3f6f0857 AD |
353 | if (!defined('NO_CURL') && function_exists('curl_init')) { |
354 | ||
355 | $fetch_curl_used = true; | |
b799dc8b | 356 | |
438a3ecb | 357 | if (ini_get("safe_mode") || ini_get("open_basedir")) { |
e2b4c0b7 RH |
358 | $new_url = geturl($url); |
359 | if (!$new_url) { | |
360 | // geturl has already populated $fetch_last_error | |
361 | return false; | |
362 | } | |
363 | $ch = curl_init($new_url); | |
b799dc8b AD |
364 | } else { |
365 | $ch = curl_init($url); | |
366 | } | |
a1af1574 | 367 | |
81c20663 | 368 | if ($timestamp && !$post_query) { |
7a01dc77 AD |
369 | curl_setopt($ch, CURLOPT_HTTPHEADER, |
370 | array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp))); | |
371 | } | |
372 | ||
8401101d BK |
373 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : FILE_FETCH_CONNECT_TIMEOUT); |
374 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : FILE_FETCH_TIMEOUT); | |
438a3ecb | 375 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode") && !ini_get("open_basedir")); |
a1af1574 AD |
376 | curl_setopt($ch, CURLOPT_MAXREDIRS, 20); |
377 | curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); | |
378 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
8d505d78 | 379 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
5f6804bc | 380 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
f826070c AD |
381 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent : |
382 | SELF_USER_AGENT); | |
3f6f0857 | 383 | curl_setopt($ch, CURLOPT_ENCODING, ""); |
48b657fc | 384 | curl_setopt($ch, CURLOPT_REFERER, $url); |
0f6b9263 AD |
385 | |
386 | if (!ini_get("safe_mode") && !ini_get("open_basedir")) { | |
387 | curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null"); | |
388 | } | |
8d505d78 | 389 | |
05f14a7d AD |
390 | if (defined('_CURL_HTTP_PROXY')) { |
391 | curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY); | |
392 | } | |
393 | ||
ae5f7bb1 AD |
394 | if ($post_query) { |
395 | curl_setopt($ch, CURLOPT_POST, true); | |
396 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query); | |
397 | } | |
398 | ||
d86945c7 | 399 | if ((OPENSSL_VERSION_NUMBER >= 0x0090808f) && (OPENSSL_VERSION_NUMBER < 0x10000000)) { |
893960b0 | 400 | curl_setopt($ch, CURLOPT_SSLVERSION, 3); |
d86945c7 AD |
401 | } |
402 | ||
8d505d78 AD |
403 | if ($login && $pass) |
404 | curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass"); | |
a1af1574 | 405 | |
fb074239 | 406 | $contents = @curl_exec($ch); |
268a06dc | 407 | |
48b657fc AD |
408 | if (curl_errno($ch) === 23 || curl_errno($ch) === 61) { |
409 | curl_setopt($ch, CURLOPT_ENCODING, 'none'); | |
410 | $contents = @curl_exec($ch); | |
fb850eec AD |
411 | } |
412 | ||
a1af1574 | 413 | if ($contents === false) { |
fb850eec | 414 | $fetch_last_error = curl_errno($ch) . " " . curl_error($ch); |
a1af1574 AD |
415 | curl_close($ch); |
416 | return false; | |
4065b60b AD |
417 | } |
418 | ||
8d505d78 | 419 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
ef39be2b | 420 | $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
4065b60b | 421 | |
7a01dc77 AD |
422 | $fetch_last_error_code = $http_code; |
423 | ||
ef39be2b | 424 | if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) { |
fb850eec AD |
425 | if (curl_errno($ch) != 0) { |
426 | $fetch_last_error = curl_errno($ch) . " " . curl_error($ch); | |
427 | } else { | |
428 | $fetch_last_error = "HTTP Code: $http_code"; | |
429 | } | |
430 | curl_close($ch); | |
a1af1574 AD |
431 | return false; |
432 | } | |
4065b60b | 433 | |
fb850eec AD |
434 | curl_close($ch); |
435 | ||
a1af1574 | 436 | return $contents; |
4065b60b | 437 | } else { |
3f6f0857 AD |
438 | |
439 | $fetch_curl_used = false; | |
440 | ||
d3911f80 | 441 | if ($login && $pass){ |
8d505d78 AD |
442 | $url_parts = array(); |
443 | ||
444 | preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts); | |
445 | ||
d3911f80 AD |
446 | $pass = urlencode($pass); |
447 | ||
8d505d78 AD |
448 | if ($url_parts[1] && $url_parts[2]) { |
449 | $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2]; | |
450 | } | |
451 | } | |
452 | ||
01311d86 AD |
453 | if (!$post_query && $timestamp) { |
454 | $context = stream_context_create(array( | |
455 | 'http' => array( | |
456 | 'method' => 'GET', | |
457 | 'header' => "If-Modified-Since: ".gmdate("D, d M Y H:i:s \\G\\M\\T\r\n", $timestamp) | |
458 | ))); | |
459 | } else { | |
460 | $context = NULL; | |
461 | } | |
462 | ||
6122c449 AD |
463 | $old_error = error_get_last(); |
464 | ||
01311d86 | 465 | $data = @file_get_contents($url, false, $context); |
23d2471c | 466 | |
ef39be2b | 467 | $fetch_last_content_type = false; // reset if no type was sent from server |
37ddf5b7 | 468 | if (isset($http_response_header) && is_array($http_response_header)) { |
df25e4d2 AD |
469 | foreach ($http_response_header as $h) { |
470 | if (substr(strtolower($h), 0, 13) == 'content-type:') { | |
471 | $fetch_last_content_type = substr($h, 14); | |
472 | // don't abort here b/c there might be more than one | |
473 | // e.g. if we were being redirected -- last one is the right one | |
474 | } | |
01311d86 AD |
475 | |
476 | if (substr(strtolower($h), 0, 7) == 'http/1.') { | |
477 | $fetch_last_error_code = (int) substr($h, 9, 3); | |
478 | } | |
ef39be2b MB |
479 | } |
480 | } | |
481 | ||
6122c449 | 482 | if (!$data) { |
23d2471c | 483 | $error = error_get_last(); |
6122c449 AD |
484 | |
485 | if ($error['message'] != $old_error['message']) { | |
486 | $fetch_last_error = $error["message"]; | |
487 | } else { | |
488 | $fetch_last_error = "HTTP Code: $fetch_last_error_code"; | |
489 | } | |
23d2471c AD |
490 | } |
491 | return $data; | |
4065b60b AD |
492 | } |
493 | ||
494 | } | |
78800912 | 495 | |
9632f884 AD |
496 | /** |
497 | * Try to determine the favicon URL for a feed. | |
498 | * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/) | |
499 | * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php | |
8d505d78 | 500 | * |
9632f884 AD |
501 | * @param string $url A feed or page URL |
502 | * @access public | |
503 | * @return mixed The favicon URL, or false if none was found. | |
504 | */ | |
1bd11fdf | 505 | function get_favicon_url($url) { |
99331724 | 506 | |
1bd11fdf | 507 | $favicon_url = false; |
ed214298 | 508 | |
4065b60b | 509 | if ($html = @fetch_file_contents($url)) { |
78800912 | 510 | |
ed214298 | 511 | libxml_use_internal_errors(true); |
c798704b | 512 | |
ed214298 AD |
513 | $doc = new DOMDocument(); |
514 | $doc->loadHTML($html); | |
515 | $xpath = new DOMXPath($doc); | |
717f5e64 | 516 | |
a712429e AD |
517 | $base = $xpath->query('/html/head/base'); |
518 | foreach ($base as $b) { | |
519 | $url = $b->getAttribute("href"); | |
520 | break; | |
521 | } | |
522 | ||
1bd11fdf | 523 | $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]'); |
ed214298 AD |
524 | if (count($entries) > 0) { |
525 | foreach ($entries as $entry) { | |
1bd11fdf AD |
526 | $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href")); |
527 | break; | |
ed214298 | 528 | } |
8d505d78 | 529 | } |
4065b60b | 530 | } |
c798704b | 531 | |
1bd11fdf AD |
532 | if (!$favicon_url) |
533 | $favicon_url = rewrite_relative_url($url, "/favicon.ico"); | |
534 | ||
535 | return $favicon_url; | |
536 | } // function get_favicon_url | |
537 | ||
6322ac79 | 538 | function check_feed_favicon($site_url, $feed) { |
882311d9 | 539 | # print "FAVICON [$site_url]: $favicon_url\n"; |
4065b60b | 540 | |
1bd11fdf AD |
541 | $icon_file = ICONS_DIR . "/$feed.ico"; |
542 | ||
543 | if (!file_exists($icon_file)) { | |
544 | $favicon_url = get_favicon_url($site_url); | |
545 | ||
546 | if ($favicon_url) { | |
547 | // Limiting to "image" type misses those served with text/plain | |
548 | $contents = fetch_file_contents($favicon_url); // , "image"); | |
549 | ||
550 | if ($contents) { | |
551 | // Crude image type matching. | |
552 | // Patterns gleaned from the file(1) source code. | |
553 | if (preg_match('/^\x00\x00\x01\x00/', $contents)) { | |
554 | // 0 string \000\000\001\000 MS Windows icon resource | |
555 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource"); | |
556 | } | |
557 | elseif (preg_match('/^GIF8/', $contents)) { | |
558 | // 0 string GIF8 GIF image data | |
559 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image"); | |
560 | } | |
561 | elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) { | |
562 | // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data | |
563 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image"); | |
564 | } | |
565 | elseif (preg_match('/^\xff\xd8/', $contents)) { | |
566 | // 0 beshort 0xffd8 JPEG image data | |
567 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image"); | |
568 | } | |
569 | else { | |
570 | //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type"); | |
571 | $contents = ""; | |
572 | } | |
573 | } | |
574 | ||
575 | if ($contents) { | |
576 | $fp = @fopen($icon_file, "w"); | |
577 | ||
578 | if ($fp) { | |
579 | fwrite($fp, $contents); | |
580 | fclose($fp); | |
581 | chmod($icon_file, 0644); | |
582 | } | |
583 | } | |
584 | } | |
2cfbb448 | 585 | return $icon_file; |
78800912 AD |
586 | } |
587 | } | |
14118af3 | 588 | |
f175937c | 589 | function print_select($id, $default, $values, $attributes = "") { |
79f3553b | 590 | print "<select name=\"$id\" id=\"$id\" $attributes>"; |
a0d53889 AD |
591 | foreach ($values as $v) { |
592 | if ($v == $default) | |
60807300 | 593 | $sel = "selected=\"1\""; |
a0d53889 AD |
594 | else |
595 | $sel = ""; | |
8d505d78 | 596 | |
e88c1943 AD |
597 | $v = trim($v); |
598 | ||
60807300 | 599 | print "<option value=\"$v\" $sel>$v</option>"; |
a0d53889 AD |
600 | } |
601 | print "</select>"; | |
602 | } | |
40d13c28 | 603 | |
79f3553b AD |
604 | function print_select_hash($id, $default, $values, $attributes = "") { |
605 | print "<select name=\"$id\" id='$id' $attributes>"; | |
673d54ca AD |
606 | foreach (array_keys($values) as $v) { |
607 | if ($v == $default) | |
74d5c8fa | 608 | $sel = 'selected="selected"'; |
673d54ca AD |
609 | else |
610 | $sel = ""; | |
8d505d78 | 611 | |
e88c1943 AD |
612 | $v = trim($v); |
613 | ||
673d54ca AD |
614 | print "<option $sel value=\"$v\">".$values[$v]."</option>"; |
615 | } | |
616 | ||
617 | print "</select>"; | |
618 | } | |
619 | ||
f541eb78 | 620 | function print_radio($id, $default, $true_is, $values, $attributes = "") { |
77e96719 | 621 | foreach ($values as $v) { |
8d505d78 | 622 | |
77e96719 | 623 | if ($v == $default) |
5da169d9 | 624 | $sel = "checked"; |
77e96719 | 625 | else |
5da169d9 AD |
626 | $sel = ""; |
627 | ||
f541eb78 | 628 | if ($v == $true_is) { |
5da169d9 AD |
629 | $sel .= " value=\"1\""; |
630 | } else { | |
631 | $sel .= " value=\"0\""; | |
632 | } | |
8d505d78 AD |
633 | |
634 | print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\" | |
69654950 | 635 | type=\"radio\" $sel $attributes name=\"$id\"> $v "; |
77e96719 AD |
636 | |
637 | } | |
638 | } | |
639 | ||
a42c55f0 | 640 | function initialize_user_prefs($uid, $profile = false) { |
ff485f1d | 641 | |
a42c55f0 | 642 | $uid = db_escape_string($uid); |
ff485f1d | 643 | |
d9084cf2 AD |
644 | if (!$profile) { |
645 | $profile = "NULL"; | |
f9aa6a89 | 646 | $profile_qpart = "AND profile IS NULL"; |
d9084cf2 | 647 | } else { |
f9aa6a89 | 648 | $profile_qpart = "AND profile = '$profile'"; |
d9084cf2 AD |
649 | } |
650 | ||
6322ac79 | 651 | if (get_schema_version() < 63) $profile_qpart = ""; |
f9aa6a89 | 652 | |
a42c55f0 | 653 | db_query("BEGIN"); |
ff485f1d | 654 | |
a42c55f0 | 655 | $result = db_query("SELECT pref_name,def_value FROM ttrss_prefs"); |
8d505d78 | 656 | |
a42c55f0 | 657 | $u_result = db_query("SELECT pref_name |
f9aa6a89 | 658 | FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart"); |
ff485f1d AD |
659 | |
660 | $active_prefs = array(); | |
661 | ||
662 | while ($line = db_fetch_assoc($u_result)) { | |
8d505d78 | 663 | array_push($active_prefs, $line["pref_name"]); |
ff485f1d AD |
664 | } |
665 | ||
666 | while ($line = db_fetch_assoc($result)) { | |
667 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
668 | // print "adding " . $line["pref_name"] . "<br>"; | |
669 | ||
a42c55f0 AD |
670 | $line["def_value"] = db_escape_string($line["def_value"]); |
671 | $line["pref_name"] = db_escape_string($line["pref_name"]); | |
d296ba50 | 672 | |
6322ac79 | 673 | if (get_schema_version() < 63) { |
a42c55f0 | 674 | db_query("INSERT INTO ttrss_user_prefs |
8d505d78 | 675 | (owner_uid,pref_name,value) VALUES |
f9aa6a89 AD |
676 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); |
677 | ||
678 | } else { | |
a42c55f0 | 679 | db_query("INSERT INTO ttrss_user_prefs |
8d505d78 | 680 | (owner_uid,pref_name,value, profile) VALUES |
f9aa6a89 AD |
681 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)"); |
682 | } | |
ff485f1d AD |
683 | |
684 | } | |
685 | } | |
686 | ||
a42c55f0 | 687 | db_query("COMMIT"); |
ff485f1d AD |
688 | |
689 | } | |
956c7629 | 690 | |
8de8bfb8 AD |
691 | function get_ssl_certificate_id() { |
692 | if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) { | |
693 | return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] . | |
694 | $_SERVER["REDIRECT_SSL_CLIENT_V_START"] . | |
695 | $_SERVER["REDIRECT_SSL_CLIENT_V_END"] . | |
696 | $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]); | |
697 | } | |
ac617ebc GG |
698 | if ($_SERVER["SSL_CLIENT_M_SERIAL"]) { |
699 | return sha1($_SERVER["SSL_CLIENT_M_SERIAL"] . | |
700 | $_SERVER["SSL_CLIENT_V_START"] . | |
701 | $_SERVER["SSL_CLIENT_V_END"] . | |
702 | $_SERVER["SSL_CLIENT_S_DN"]); | |
703 | } | |
8de8bfb8 AD |
704 | return ""; |
705 | } | |
706 | ||
a42c55f0 | 707 | function authenticate_user($login, $password, $check_only = false) { |
c8437f35 | 708 | |
131b01b3 | 709 | if (!SINGLE_USER_MODE) { |
0d421af8 | 710 | $user_id = false; |
0f28f81f | 711 | |
1ffe3391 | 712 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_AUTH_USER) as $plugin) { |
0f28f81f AD |
713 | |
714 | $user_id = (int) $plugin->authenticate($login, $password); | |
715 | ||
716 | if ($user_id) { | |
717 | $_SESSION["auth_module"] = strtolower(get_class($plugin)); | |
718 | break; | |
719 | } | |
461766f3 AD |
720 | } |
721 | ||
0d421af8 | 722 | if ($user_id && !$check_only) { |
70400171 AD |
723 | @session_start(); |
724 | ||
0d421af8 | 725 | $_SESSION["uid"] = $user_id; |
3472c4c5 | 726 | $_SESSION["version"] = VERSION_STATIC; |
0d421af8 | 727 | |
a42c55f0 | 728 | $result = db_query("SELECT login,access_level,pwd_hash FROM ttrss_users |
0d421af8 | 729 | WHERE id = '$user_id'"); |
8d505d78 | 730 | |
131b01b3 AD |
731 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); |
732 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); | |
76d78eb2 | 733 | $_SESSION["csrf_token"] = uniqid(rand(), true); |
8d505d78 | 734 | |
a42c55f0 | 735 | db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
131b01b3 | 736 | $_SESSION["uid"]); |
8d505d78 | 737 | |
131b01b3 | 738 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; |
837ec70e | 739 | $_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']); |
1a9f4d3c | 740 | $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash"); |
91c5f229 AD |
741 | |
742 | $_SESSION["last_version_check"] = time(); | |
8d505d78 | 743 | |
a42c55f0 | 744 | initialize_user_prefs($_SESSION["uid"]); |
8d505d78 | 745 | |
131b01b3 AD |
746 | return true; |
747 | } | |
8d505d78 | 748 | |
131b01b3 | 749 | return false; |
503eb349 | 750 | |
131b01b3 | 751 | } else { |
503eb349 | 752 | |
131b01b3 AD |
753 | $_SESSION["uid"] = 1; |
754 | $_SESSION["name"] = "admin"; | |
787e5ebc | 755 | $_SESSION["access_level"] = 10; |
21e42e5f | 756 | |
0d421af8 AD |
757 | $_SESSION["hide_hello"] = true; |
758 | $_SESSION["hide_logout"] = true; | |
759 | ||
d5fd183d AD |
760 | $_SESSION["auth_module"] = false; |
761 | ||
21e42e5f | 762 | if (!$_SESSION["csrf_token"]) { |
76d78eb2 | 763 | $_SESSION["csrf_token"] = uniqid(rand(), true); |
21e42e5f | 764 | } |
f557cd78 | 765 | |
0bbba72d | 766 | $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"]; |
8d505d78 | 767 | |
a42c55f0 | 768 | initialize_user_prefs($_SESSION["uid"]); |
8d505d78 | 769 | |
c8437f35 AD |
770 | return true; |
771 | } | |
c8437f35 AD |
772 | } |
773 | ||
e6cb77a0 AD |
774 | function make_password($length = 8) { |
775 | ||
85db6213 AD |
776 | $password = ""; |
777 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; | |
778 | ||
779 | $i = 0; | |
780 | ||
781 | while ($i < $length) { | |
782 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
783 | ||
784 | if (!strstr($password, $char)) { | |
785 | $password .= $char; | |
786 | $i++; | |
787 | } | |
788 | } | |
789 | return $password; | |
e6cb77a0 AD |
790 | } |
791 | ||
792 | // this is called after user is created to initialize default feeds, labels | |
793 | // or whatever else | |
8d505d78 | 794 | |
e6cb77a0 AD |
795 | // user preferences are checked on every login, not here |
796 | ||
a42c55f0 | 797 | function initialize_user($uid) { |
e6cb77a0 | 798 | |
a42c55f0 | 799 | db_query("insert into ttrss_feeds (owner_uid,title,feed_url) |
74bff337 | 800 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
b6d486a3 | 801 | 'http://tt-rss.org/releases.rss')"); |
3b0feb9b | 802 | |
a42c55f0 | 803 | db_query("insert into ttrss_feeds (owner_uid,title,feed_url) |
cd2cd415 | 804 | values ('$uid', 'Tiny Tiny RSS: Forum', |
f0855b88 | 805 | 'http://tt-rss.org/forum/rss.php')"); |
3b0feb9b | 806 | } |
e6cb77a0 | 807 | |
b8aa49bc | 808 | function logout_user() { |
5ccc1cf5 AD |
809 | session_destroy(); |
810 | if (isset($_COOKIE[session_name()])) { | |
811 | setcookie(session_name(), '', time()-42000, '/'); | |
812 | } | |
b8aa49bc AD |
813 | } |
814 | ||
8484ce22 AD |
815 | function validate_csrf($csrf_token) { |
816 | return $csrf_token == $_SESSION['csrf_token']; | |
817 | } | |
818 | ||
a42c55f0 | 819 | function load_user_plugins($owner_uid) { |
6d45a152 | 820 | if ($owner_uid && SCHEMA_VERSION >= 100) { |
a42c55f0 | 821 | $plugins = get_pref("_ENABLED_PLUGINS", $owner_uid); |
de612e7a | 822 | |
1ffe3391 | 823 | PluginHost::getInstance()->load($plugins, PluginHost::KIND_USER, $owner_uid); |
e9c04fd4 | 824 | |
6322ac79 | 825 | if (get_schema_version() > 100) { |
1ffe3391 | 826 | PluginHost::getInstance()->load_data(); |
e9c04fd4 | 827 | } |
de612e7a AD |
828 | } |
829 | } | |
830 | ||
6322ac79 | 831 | function login_sequence() { |
97acbaf1 | 832 | if (SINGLE_USER_MODE) { |
25db6c51 | 833 | @session_start(); |
a42c55f0 | 834 | authenticate_user("admin", null); |
68349f55 | 835 | startup_gettext(); |
a42c55f0 | 836 | load_user_plugins($_SESSION["uid"]); |
97acbaf1 | 837 | } else { |
6322ac79 | 838 | if (!validate_session()) $_SESSION["uid"] = false; |
d0eef2a3 AD |
839 | |
840 | if (!$_SESSION["uid"]) { | |
97acbaf1 | 841 | |
a42c55f0 AD |
842 | if (AUTH_AUTO_LOGIN && authenticate_user(null, null)) { |
843 | $_SESSION["ref_schema_version"] = get_schema_version(true); | |
97acbaf1 | 844 | } else { |
a42c55f0 | 845 | authenticate_user(null, null, true); |
97acbaf1 AD |
846 | } |
847 | ||
d0eef2a3 | 848 | if (!$_SESSION["uid"]) { |
d0eef2a3 AD |
849 | @session_destroy(); |
850 | setcookie(session_name(), '', time()-42000, '/'); | |
9ce7a554 | 851 | |
6322ac79 | 852 | render_login_form(); |
d0eef2a3 AD |
853 | exit; |
854 | } | |
4ad99f23 | 855 | |
97acbaf1 AD |
856 | } else { |
857 | /* bump login timestamp */ | |
a42c55f0 | 858 | db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
97acbaf1 | 859 | $_SESSION["uid"]); |
06b0777f | 860 | $_SESSION["last_login_update"] = time(); |
01a87dff AD |
861 | } |
862 | ||
de612e7a | 863 | if ($_SESSION["uid"]) { |
7b149552 | 864 | startup_gettext(); |
a42c55f0 | 865 | load_user_plugins($_SESSION["uid"]); |
b1b1d25f AD |
866 | |
867 | /* cleanup ccache */ | |
868 | ||
a42c55f0 | 869 | db_query("DELETE FROM ttrss_counters_cache WHERE owner_uid = ". |
b1b1d25f AD |
870 | $_SESSION["uid"] . " AND |
871 | (SELECT COUNT(id) FROM ttrss_feeds WHERE | |
872 | ttrss_feeds.id = feed_id) = 0"); | |
873 | ||
a42c55f0 | 874 | db_query("DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ". |
b1b1d25f AD |
875 | $_SESSION["uid"] . " AND |
876 | (SELECT COUNT(id) FROM ttrss_feed_categories WHERE | |
877 | ttrss_feed_categories.id = feed_id) = 0"); | |
878 | ||
de612e7a | 879 | } |
b1b1d25f | 880 | |
b8aa49bc | 881 | } |
afc3cf55 | 882 | } |
3547842a | 883 | |
411fe209 | 884 | function truncate_string($str, $max_len, $suffix = '…') { |
878a0083 | 885 | if (mb_strlen($str, "utf-8") > $max_len) { |
411fe209 | 886 | return mb_substr($str, 0, $max_len, "utf-8") . $suffix; |
3547842a AD |
887 | } else { |
888 | return $str; | |
889 | } | |
890 | } | |
54a60e1a | 891 | |
ab4b768f AD |
892 | function convert_timestamp($timestamp, $source_tz, $dest_tz) { |
893 | ||
894 | try { | |
895 | $source_tz = new DateTimeZone($source_tz); | |
896 | } catch (Exception $e) { | |
897 | $source_tz = new DateTimeZone('UTC'); | |
898 | } | |
899 | ||
900 | try { | |
901 | $dest_tz = new DateTimeZone($dest_tz); | |
902 | } catch (Exception $e) { | |
903 | $dest_tz = new DateTimeZone('UTC'); | |
904 | } | |
905 | ||
906 | $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz); | |
907 | return $dt->format('U') + $dest_tz->getOffset($dt); | |
908 | } | |
909 | ||
a42c55f0 | 910 | function make_local_datetime($timestamp, $long, $owner_uid = false, |
324944f3 AD |
911 | $no_smart_dt = false) { |
912 | ||
913 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
914 | if (!$timestamp) $timestamp = '1970-01-01 0:00'; | |
915 | ||
7d96bfcd | 916 | global $utc_tz; |
5ddef5ba AD |
917 | global $user_tz; |
918 | ||
919 | if (!$utc_tz) $utc_tz = new DateTimeZone('UTC'); | |
324944f3 | 920 | |
90e5f4f1 AD |
921 | $timestamp = substr($timestamp, 0, 19); |
922 | ||
7d96bfcd AD |
923 | # We store date in UTC internally |
924 | $dt = new DateTime($timestamp, $utc_tz); | |
925 | ||
5ddef5ba | 926 | $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid); |
7d96bfcd | 927 | |
6bfc97da | 928 | if ($user_tz_string != 'Automatic') { |
324944f3 | 929 | |
6bfc97da AD |
930 | try { |
931 | if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string); | |
932 | } catch (Exception $e) { | |
933 | $user_tz = $utc_tz; | |
934 | } | |
935 | ||
936 | $tz_offset = $user_tz->getOffset($dt); | |
937 | } else { | |
96f0cbe3 | 938 | $tz_offset = (int) -$_SESSION["clientTzOffset"]; |
6bfc97da | 939 | } |
5ddef5ba | 940 | |
7d96bfcd | 941 | $user_timestamp = $dt->format('U') + $tz_offset; |
324944f3 | 942 | |
1dc52ae7 | 943 | if (!$no_smart_dt) { |
a42c55f0 | 944 | return smart_date_time($user_timestamp, |
7d96bfcd | 945 | $tz_offset, $owner_uid); |
324944f3 AD |
946 | } else { |
947 | if ($long) | |
a42c55f0 | 948 | $format = get_pref('LONG_DATE_FORMAT', $owner_uid); |
324944f3 | 949 | else |
a42c55f0 | 950 | $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); |
324944f3 AD |
951 | |
952 | return date($format, $user_timestamp); | |
953 | } | |
954 | } | |
955 | ||
a42c55f0 | 956 | function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false) { |
2a5c136e AD |
957 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; |
958 | ||
959 | if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) { | |
be773442 | 960 | return date("G:i", $timestamp); |
2a5c136e | 961 | } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) { |
a42c55f0 | 962 | $format = get_pref('SHORT_DATE_FORMAT', $owner_uid); |
2a5c136e | 963 | return date($format, $timestamp); |
be773442 | 964 | } else { |
a42c55f0 | 965 | $format = get_pref('LONG_DATE_FORMAT', $owner_uid); |
2a5c136e | 966 | return date($format, $timestamp); |
be773442 AD |
967 | } |
968 | } | |
969 | ||
e3c99f3b | 970 | function sql_bool_to_bool($s) { |
9955a134 | 971 | if ($s == "t" || $s == "1" || strtolower($s) == "true") { |
e3c99f3b AD |
972 | return true; |
973 | } else { | |
974 | return false; | |
975 | } | |
976 | } | |
8d505d78 | 977 | |
badac687 AD |
978 | function bool_to_sql_bool($s) { |
979 | if ($s) { | |
980 | return "true"; | |
981 | } else { | |
982 | return "false"; | |
983 | } | |
984 | } | |
e3c99f3b | 985 | |
fcfa9ef1 AD |
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. | |
a42c55f0 | 989 | function get_schema_version($nocache = false) { |
7d96bfcd AD |
990 | global $schema_version; |
991 | ||
e2cf81e2 | 992 | if (!$schema_version && !$nocache) { |
a42c55f0 | 993 | $result = db_query("SELECT schema_version FROM ttrss_version"); |
199db684 | 994 | $version = db_fetch_result($result, 0, "schema_version"); |
7d96bfcd | 995 | $schema_version = $version; |
199db684 | 996 | return $version; |
7d96bfcd AD |
997 | } else { |
998 | return $schema_version; | |
999 | } | |
e4c51a6c AD |
1000 | } |
1001 | ||
6322ac79 | 1002 | function sanity_check() { |
31303c6b | 1003 | require_once 'errors.php'; |
cacc1877 | 1004 | global $ERRORS; |
ebb948c2 | 1005 | |
6043fb7e | 1006 | $error_code = 0; |
a42c55f0 | 1007 | $schema_version = get_schema_version(true); |
6043fb7e AD |
1008 | |
1009 | if ($schema_version != SCHEMA_VERSION) { | |
1010 | $error_code = 5; | |
1011 | } | |
1012 | ||
aec3ce39 | 1013 | if (DB_TYPE == "mysql") { |
a42c55f0 | 1014 | $result = db_query("SELECT true", false); |
aec3ce39 AD |
1015 | if (db_num_rows($result) != 1) { |
1016 | $error_code = 10; | |
1017 | } | |
1018 | } | |
1019 | ||
a42c55f0 | 1020 | if (db_escape_string("testTEST") != "testTEST") { |
f29ba148 AD |
1021 | $error_code = 12; |
1022 | } | |
1023 | ||
ebb948c2 | 1024 | return array("code" => $error_code, "message" => $ERRORS[$error_code]); |
6043fb7e AD |
1025 | } |
1026 | ||
27981ca3 | 1027 | function file_is_locked($filename) { |
8ff2a86c AD |
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 | } | |
31a6d42d | 1037 | fclose($fp); |
8ff2a86c AD |
1038 | return true; |
1039 | } else { | |
31a6d42d AD |
1040 | return false; |
1041 | } | |
27981ca3 | 1042 | } |
8ff2a86c AD |
1043 | return true; // consider the file always locked and skip the test |
1044 | } else { | |
1045 | return false; | |
27981ca3 | 1046 | } |
27981ca3 AD |
1047 | } |
1048 | ||
8ff2a86c | 1049 | |
fcb4c0c9 | 1050 | function make_lockfile($filename) { |
cfa43e02 | 1051 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); |
fcb4c0c9 | 1052 | |
a44bfcfd | 1053 | if ($fp && flock($fp, LOCK_EX | LOCK_NB)) { |
58fc7095 AD |
1054 | $stat_h = fstat($fp); |
1055 | $stat_f = stat(LOCK_DIRECTORY . "/$filename"); | |
1056 | ||
1fcebfb3 AD |
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 | } | |
58fc7095 AD |
1063 | } |
1064 | ||
4c59adb1 AD |
1065 | if (function_exists('posix_getpid')) { |
1066 | fwrite($fp, posix_getpid() . "\n"); | |
1067 | } | |
fcb4c0c9 AD |
1068 | return $fp; |
1069 | } else { | |
1070 | return false; | |
1071 | } | |
1072 | } | |
1073 | ||
bf7fcde8 | 1074 | function make_stampfile($filename) { |
cfa43e02 | 1075 | $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); |
bf7fcde8 | 1076 | |
8e00ae9b | 1077 | if (flock($fp, LOCK_EX | LOCK_NB)) { |
bf7fcde8 | 1078 | fwrite($fp, time() . "\n"); |
8e00ae9b | 1079 | flock($fp, LOCK_UN); |
bf7fcde8 AD |
1080 | fclose($fp); |
1081 | return true; | |
1082 | } else { | |
1083 | return false; | |
1084 | } | |
1085 | } | |
1086 | ||
894ebcf5 | 1087 | function sql_random_function() { |
8c0496f7 | 1088 | if (DB_TYPE == "mysql") { |
894ebcf5 AD |
1089 | return "RAND()"; |
1090 | } else { | |
1091 | return "RANDOM()"; | |
1092 | } | |
1093 | } | |
1094 | ||
a42c55f0 | 1095 | function catchup_feed($feed, $cat_view, $owner_uid = false, $max_id = false, $mode = 'all') { |
c7e51de1 AD |
1096 | |
1097 | if (!$owner_uid) $owner_uid = $_SESSION['uid']; | |
88040f57 | 1098 | |
37c03d3a | 1099 | //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) { |
22fdebff | 1100 | |
c8b693cf AD |
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") { | |
94828a8b | 1108 | $date_qpart = "date_entered < NOW() - INTERVAL '1 day' "; |
c8b693cf | 1109 | } else { |
d5381016 | 1110 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) "; |
c8b693cf AD |
1111 | } |
1112 | break; | |
1113 | case "1week": | |
1114 | if (DB_TYPE == "pgsql") { | |
94828a8b | 1115 | $date_qpart = "date_entered < NOW() - INTERVAL '1 week' "; |
c8b693cf | 1116 | } else { |
94828a8b | 1117 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) "; |
c8b693cf AD |
1118 | } |
1119 | break; | |
8d476359 | 1120 | case "2week": |
c8b693cf | 1121 | if (DB_TYPE == "pgsql") { |
94828a8b | 1122 | $date_qpart = "date_entered < NOW() - INTERVAL '2 week' "; |
c8b693cf | 1123 | } else { |
94828a8b | 1124 | $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) "; |
c8b693cf AD |
1125 | } |
1126 | break; | |
1127 | default: | |
1128 | $date_qpart = "true"; | |
1129 | } | |
1130 | ||
37c03d3a | 1131 | if (is_numeric($feed)) { |
23aa0d16 AD |
1132 | if ($cat_view) { |
1133 | ||
72a2f4f5 | 1134 | if ($feed >= 0) { |
f9fca8cb AD |
1135 | |
1136 | if ($feed > 0) { | |
a42c55f0 | 1137 | $children = getChildCategories($feed, $owner_uid); |
bda6afa2 AD |
1138 | array_push($children, $feed); |
1139 | ||
1140 | $children = join(",", $children); | |
1141 | ||
1142 | $cat_qpart = "cat_id IN ($children)"; | |
f9fca8cb AD |
1143 | } else { |
1144 | $cat_qpart = "cat_id IS NULL"; | |
1145 | } | |
8d505d78 | 1146 | |
a42c55f0 | 1147 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1148 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1149 | (SELECT id FROM | |
1150 | (SELECT 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)"); | |
23aa0d16 | 1153 | |
f9fca8cb | 1154 | } else if ($feed == -2) { |
23aa0d16 | 1155 | |
a42c55f0 | 1156 | db_query("UPDATE ttrss_user_entries |
8d505d78 | 1157 | SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*) |
15d8bd7d AD |
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"); | |
23aa0d16 AD |
1160 | } |
1161 | ||
1162 | } else if ($feed > 0) { | |
1163 | ||
a42c55f0 | 1164 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1165 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1166 | (SELECT id FROM | |
1167 | (SELECT 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)"); | |
8d505d78 | 1169 | |
f822a8e5 | 1170 | } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred |
23aa0d16 AD |
1171 | |
1172 | if ($feed == -1) { | |
a42c55f0 | 1173 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1174 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1175 | (SELECT id FROM | |
1176 | (SELECT 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)"); | |
23aa0d16 | 1178 | } |
e4f4b46f AD |
1179 | |
1180 | if ($feed == -2) { | |
a42c55f0 | 1181 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1182 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1183 | (SELECT id FROM | |
1184 | (SELECT 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)"); | |
e4f4b46f AD |
1186 | } |
1187 | ||
2d24f032 AD |
1188 | if ($feed == -3) { |
1189 | ||
a42c55f0 | 1190 | $intl = get_pref("FRESH_ARTICLE_MAX_AGE"); |
c1d7e6c3 | 1191 | |
2d24f032 | 1192 | if (DB_TYPE == "pgsql") { |
cfd34086 | 1193 | $match_part = "date_entered > NOW() - INTERVAL '$intl hour' "; |
2d24f032 | 1194 | } else { |
cfd34086 | 1195 | $match_part = "date_entered > DATE_SUB(NOW(), |
c1d7e6c3 | 1196 | INTERVAL $intl HOUR) "; |
2d24f032 AD |
1197 | } |
1198 | ||
a42c55f0 | 1199 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1200 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1201 | (SELECT id FROM | |
1202 | (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id | |
cfd34086 | 1203 | AND owner_uid = $owner_uid AND unread = true AND $date_qpart AND $match_part) as tmp)"); |
2d24f032 AD |
1204 | } |
1205 | ||
3584cb11 | 1206 | if ($feed == -4) { |
a42c55f0 | 1207 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1208 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1209 | (SELECT id FROM | |
1210 | (SELECT 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)"); | |
3584cb11 AD |
1212 | } |
1213 | ||
f822a8e5 | 1214 | } else if ($feed < LABEL_BASE_INDEX) { // label |
23aa0d16 | 1215 | |
f822a8e5 | 1216 | $label_id = feed_to_label_id($feed); |
23aa0d16 | 1217 | |
a42c55f0 | 1218 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1219 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1220 | (SELECT id FROM | |
1221 | (SELECT 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)"); | |
23aa0d16 | 1224 | |
23aa0d16 | 1225 | } |
ad0056a8 | 1226 | |
a42c55f0 | 1227 | ccache_update($feed, $owner_uid, $cat_view); |
ad0056a8 | 1228 | |
23aa0d16 | 1229 | } else { // tag |
a42c55f0 | 1230 | db_query("UPDATE ttrss_user_entries |
c8b693cf AD |
1231 | SET unread = false, last_read = NOW() WHERE ref_id IN |
1232 | (SELECT id FROM | |
1233 | (SELECT 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)"); | |
23aa0d16 | 1236 | |
23aa0d16 AD |
1237 | } |
1238 | } | |
1239 | ||
6322ac79 AD |
1240 | function getAllCounters() { |
1241 | $data = getGlobalCounters(); | |
8d505d78 | 1242 | |
6322ac79 AD |
1243 | $data = array_merge($data, getVirtCounters()); |
1244 | $data = array_merge($data, getLabelCounters()); | |
6f7798b6 | 1245 | $data = array_merge($data, getFeedCounters()); |
6322ac79 | 1246 | $data = array_merge($data, getCategoryCounters()); |
6a7817c1 AD |
1247 | |
1248 | return $data; | |
8d505d78 | 1249 | } |
a9cb1f83 | 1250 | |
d8cdb08f | 1251 | function getCategoryTitle($cat_id) |