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