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