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