functions.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. <?php
  2. define('EXPECTED_CONFIG_VERSION', 26);
  3. define('SCHEMA_VERSION', 127);
  4. define('LABEL_BASE_INDEX', -1024);
  5. define('PLUGIN_FEED_BASE_INDEX', -128);
  6. define('COOKIE_LIFETIME_LONG', 86400*365);
  7. $fetch_last_error = false;
  8. $fetch_last_error_code = false;
  9. $fetch_last_content_type = false;
  10. $fetch_last_error_content = false; // curl only for the time being
  11. $fetch_curl_used = false;
  12. $suppress_debugging = false;
  13. libxml_disable_entity_loader(true);
  14. mb_internal_encoding("UTF-8");
  15. date_default_timezone_set('UTC');
  16. if (defined('E_DEPRECATED')) {
  17. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  18. } else {
  19. error_reporting(E_ALL & ~E_NOTICE);
  20. }
  21. require_once 'config.php';
  22. /**
  23. * Define a constant if not already defined
  24. *
  25. * @param string $name The constant name.
  26. * @param mixed $value The constant value.
  27. * @access public
  28. * @return boolean True if defined successfully or not.
  29. */
  30. function define_default($name, $value) {
  31. defined($name) or define($name, $value);
  32. }
  33. ///// Some defaults that you can override in config.php //////
  34. define_default('FEED_FETCH_TIMEOUT', 45);
  35. // How may seconds to wait for response when requesting feed from a site
  36. define_default('FEED_FETCH_NO_CACHE_TIMEOUT', 15);
  37. // How may seconds to wait for response when requesting feed from a
  38. // site when that feed wasn't cached before
  39. define_default('FILE_FETCH_TIMEOUT', 45);
  40. // Default timeout when fetching files from remote sites
  41. define_default('FILE_FETCH_CONNECT_TIMEOUT', 15);
  42. // How many seconds to wait for initial response from website when
  43. // fetching files from remote sites
  44. if (DB_TYPE == "pgsql") {
  45. define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
  46. } else {
  47. define('SUBSTRING_FOR_DATE', 'SUBSTRING');
  48. }
  49. /**
  50. * Return available translations names.
  51. *
  52. * @access public
  53. * @return array A array of available translations.
  54. */
  55. function get_translations() {
  56. $tr = array(
  57. "auto" => "Detect automatically",
  58. "ar_SA" => "العربيّة (Arabic)",
  59. "bg_BG" => "Bulgarian",
  60. "da_DA" => "Dansk",
  61. "ca_CA" => "Català",
  62. "cs_CZ" => "Česky",
  63. "en_US" => "English",
  64. "el_GR" => "Ελληνικά",
  65. "es_ES" => "Español (España)",
  66. "es_LA" => "Español",
  67. "de_DE" => "Deutsch",
  68. "fr_FR" => "Français",
  69. "hu_HU" => "Magyar (Hungarian)",
  70. "it_IT" => "Italiano",
  71. "ja_JP" => "日本語 (Japanese)",
  72. "lv_LV" => "Latviešu",
  73. "nb_NO" => "Norwegian bokmål",
  74. "nl_NL" => "Dutch",
  75. "pl_PL" => "Polski",
  76. "ru_RU" => "Русский",
  77. "pt_BR" => "Portuguese/Brazil",
  78. "pt_PT" => "Portuguese/Portugal",
  79. "zh_CN" => "Simplified Chinese",
  80. "zh_TW" => "Traditional Chinese",
  81. "sv_SE" => "Svenska",
  82. "fi_FI" => "Suomi",
  83. "tr_TR" => "Türkçe");
  84. return $tr;
  85. }
  86. require_once "lib/accept-to-gettext.php";
  87. require_once "lib/gettext/gettext.inc";
  88. function startup_gettext() {
  89. # Get locale from Accept-Language header
  90. $lang = al2gt(array_keys(get_translations()), "text/html");
  91. if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
  92. $lang = _TRANSLATION_OVERRIDE_DEFAULT;
  93. }
  94. if ($_SESSION["uid"] && get_schema_version() >= 120) {
  95. $pref_lang = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
  96. if ($pref_lang && $pref_lang != 'auto') {
  97. $lang = $pref_lang;
  98. }
  99. }
  100. if ($lang) {
  101. if (defined('LC_MESSAGES')) {
  102. _setlocale(LC_MESSAGES, $lang);
  103. } else if (defined('LC_ALL')) {
  104. _setlocale(LC_ALL, $lang);
  105. }
  106. _bindtextdomain("messages", "locale");
  107. _textdomain("messages");
  108. _bind_textdomain_codeset("messages", "UTF-8");
  109. }
  110. }
  111. require_once 'db-prefs.php';
  112. require_once 'version.php';
  113. require_once 'ccache.php';
  114. require_once 'labels.php';
  115. define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
  116. ini_set('user_agent', SELF_USER_AGENT);
  117. require_once 'lib/pubsubhubbub/publisher.php';
  118. $schema_version = false;
  119. function _debug_suppress($suppress) {
  120. global $suppress_debugging;
  121. $suppress_debugging = $suppress;
  122. }
  123. /**
  124. * Print a timestamped debug message.
  125. *
  126. * @param string $msg The debug message.
  127. * @return void
  128. */
  129. function _debug($msg, $show = true) {
  130. global $suppress_debugging;
  131. //echo "[$suppress_debugging] $msg $show\n";
  132. if ($suppress_debugging) return false;
  133. $ts = strftime("%H:%M:%S", time());
  134. if (function_exists('posix_getpid')) {
  135. $ts = "$ts/" . posix_getpid();
  136. }
  137. if ($show && !(defined('QUIET') && QUIET)) {
  138. print "[$ts] $msg\n";
  139. }
  140. if (defined('LOGFILE')) {
  141. $fp = fopen(LOGFILE, 'a+');
  142. if ($fp) {
  143. $locked = false;
  144. if (function_exists("flock")) {
  145. $tries = 0;
  146. // try to lock logfile for writing
  147. while ($tries < 5 && !$locked = flock($fp, LOCK_EX | LOCK_NB)) {
  148. sleep(1);
  149. ++$tries;
  150. }
  151. if (!$locked) {
  152. fclose($fp);
  153. return;
  154. }
  155. }
  156. fputs($fp, "[$ts] $msg\n");
  157. if (function_exists("flock")) {
  158. flock($fp, LOCK_UN);
  159. }
  160. fclose($fp);
  161. }
  162. }
  163. } // function _debug
  164. /**
  165. * Purge a feed old posts.
  166. *
  167. * @param mixed $link A database connection.
  168. * @param mixed $feed_id The id of the purged feed.
  169. * @param mixed $purge_interval Olderness of purged posts.
  170. * @param boolean $debug Set to True to enable the debug. False by default.
  171. * @access public
  172. * @return void
  173. */
  174. function purge_feed($feed_id, $purge_interval, $debug = false) {
  175. if (!$purge_interval) $purge_interval = feed_purge_interval($feed_id);
  176. $rows = -1;
  177. $result = db_query(
  178. "SELECT owner_uid FROM ttrss_feeds WHERE id = '$feed_id'");
  179. $owner_uid = false;
  180. if (db_num_rows($result) == 1) {
  181. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  182. }
  183. if ($purge_interval == -1 || !$purge_interval) {
  184. if ($owner_uid) {
  185. ccache_update($feed_id, $owner_uid);
  186. }
  187. return;
  188. }
  189. if (!$owner_uid) return;
  190. if (FORCE_ARTICLE_PURGE == 0) {
  191. $purge_unread = get_pref("PURGE_UNREAD_ARTICLES",
  192. $owner_uid, false);
  193. } else {
  194. $purge_unread = true;
  195. $purge_interval = FORCE_ARTICLE_PURGE;
  196. }
  197. if (!$purge_unread) $query_limit = " unread = false AND ";
  198. if (DB_TYPE == "pgsql") {
  199. $pg_version = get_pgsql_version();
  200. if (preg_match("/^7\./", $pg_version) || preg_match("/^8\.0/", $pg_version)) {
  201. $result = db_query("DELETE FROM ttrss_user_entries WHERE
  202. ttrss_entries.id = ref_id AND
  203. marked = false AND
  204. feed_id = '$feed_id' AND
  205. $query_limit
  206. ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
  207. } else {
  208. $result = db_query("DELETE FROM ttrss_user_entries
  209. USING ttrss_entries
  210. WHERE ttrss_entries.id = ref_id AND
  211. marked = false AND
  212. feed_id = '$feed_id' AND
  213. $query_limit
  214. ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
  215. }
  216. } else {
  217. /* $result = db_query("DELETE FROM ttrss_user_entries WHERE
  218. marked = false AND feed_id = '$feed_id' AND
  219. (SELECT date_updated FROM ttrss_entries WHERE
  220. id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); */
  221. $result = db_query("DELETE FROM ttrss_user_entries
  222. USING ttrss_user_entries, ttrss_entries
  223. WHERE ttrss_entries.id = ref_id AND
  224. marked = false AND
  225. feed_id = '$feed_id' AND
  226. $query_limit
  227. ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
  228. }
  229. $rows = db_affected_rows($result);
  230. ccache_update($feed_id, $owner_uid);
  231. if ($debug) {
  232. _debug("Purged feed $feed_id ($purge_interval): deleted $rows articles");
  233. }
  234. return $rows;
  235. } // function purge_feed
  236. function feed_purge_interval($feed_id) {
  237. $result = db_query("SELECT purge_interval, owner_uid FROM ttrss_feeds
  238. WHERE id = '$feed_id'");
  239. if (db_num_rows($result) == 1) {
  240. $purge_interval = db_fetch_result($result, 0, "purge_interval");
  241. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  242. if ($purge_interval == 0) $purge_interval = get_pref(
  243. 'PURGE_OLD_DAYS', $owner_uid);
  244. return $purge_interval;
  245. } else {
  246. return -1;
  247. }
  248. }
  249. function purge_orphans($do_output = false) {
  250. // purge orphaned posts in main content table
  251. $result = db_query("DELETE FROM ttrss_entries WHERE
  252. NOT EXISTS (SELECT ref_id FROM ttrss_user_entries WHERE ref_id = id)");
  253. if ($do_output) {
  254. $rows = db_affected_rows($result);
  255. _debug("Purged $rows orphaned posts.");
  256. }
  257. }
  258. function get_feed_update_interval($feed_id) {
  259. $result = db_query("SELECT owner_uid, update_interval FROM
  260. ttrss_feeds WHERE id = '$feed_id'");
  261. if (db_num_rows($result) == 1) {
  262. $update_interval = db_fetch_result($result, 0, "update_interval");
  263. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  264. if ($update_interval != 0) {
  265. return $update_interval;
  266. } else {
  267. return get_pref('DEFAULT_UPDATE_INTERVAL', $owner_uid, false);
  268. }
  269. } else {
  270. return -1;
  271. }
  272. }
  273. function fetch_file_contents($url, $type = false, $login = false, $pass = false, $post_query = false, $timeout = false, $timestamp = 0, $useragent = false) {
  274. global $fetch_last_error;
  275. global $fetch_last_error_code;
  276. global $fetch_last_error_content;
  277. global $fetch_last_content_type;
  278. global $fetch_curl_used;
  279. $url = ltrim($url, ' ');
  280. $url = str_replace(' ', '%20', $url);
  281. if (strpos($url, "//") === 0)
  282. $url = 'http:' . $url;
  283. if (!defined('NO_CURL') && function_exists('curl_init')) {
  284. $fetch_curl_used = true;
  285. if (ini_get("safe_mode") || ini_get("open_basedir") || defined("FORCE_GETURL")) {
  286. $new_url = geturl($url);
  287. if (!$new_url) {
  288. // geturl has already populated $fetch_last_error
  289. return false;
  290. }
  291. $ch = curl_init($new_url);
  292. } else {
  293. $ch = curl_init($url);
  294. }
  295. if ($timestamp && !$post_query) {
  296. curl_setopt($ch, CURLOPT_HTTPHEADER,
  297. array("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $timestamp)));
  298. }
  299. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : FILE_FETCH_CONNECT_TIMEOUT);
  300. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : FILE_FETCH_TIMEOUT);
  301. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("safe_mode") && !ini_get("open_basedir"));
  302. curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
  303. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  304. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  305. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  306. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  307. curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent :
  308. SELF_USER_AGENT);
  309. curl_setopt($ch, CURLOPT_ENCODING, "");
  310. //curl_setopt($ch, CURLOPT_REFERER, $url);
  311. if (!ini_get("safe_mode") && !ini_get("open_basedir")) {
  312. curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null");
  313. }
  314. if (defined('_CURL_HTTP_PROXY')) {
  315. curl_setopt($ch, CURLOPT_PROXY, _CURL_HTTP_PROXY);
  316. }
  317. if ($post_query) {
  318. curl_setopt($ch, CURLOPT_POST, true);
  319. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
  320. }
  321. if ($login && $pass)
  322. curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
  323. $contents = @curl_exec($ch);
  324. if (curl_errno($ch) === 23 || curl_errno($ch) === 61) {
  325. curl_setopt($ch, CURLOPT_ENCODING, 'none');
  326. $contents = @curl_exec($ch);
  327. }
  328. if ($contents === false) {
  329. $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
  330. curl_close($ch);
  331. return false;
  332. }
  333. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  334. $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  335. $fetch_last_error_code = $http_code;
  336. if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) {
  337. if (curl_errno($ch) != 0) {
  338. $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
  339. } else {
  340. $fetch_last_error = "HTTP Code: $http_code";
  341. }
  342. $fetch_last_error_content = $contents;
  343. curl_close($ch);
  344. return false;
  345. }
  346. curl_close($ch);
  347. return $contents;
  348. } else {
  349. $fetch_curl_used = false;
  350. if ($login && $pass){
  351. $url_parts = array();
  352. preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
  353. $pass = urlencode($pass);
  354. if ($url_parts[1] && $url_parts[2]) {
  355. $url = $url_parts[1] . "://$login:$pass@" . $url_parts[2];
  356. }
  357. }
  358. if (!$post_query && $timestamp) {
  359. $context = stream_context_create(array(
  360. 'http' => array(
  361. 'method' => 'GET',
  362. 'header' => "If-Modified-Since: ".gmdate("D, d M Y H:i:s \\G\\M\\T\r\n", $timestamp)
  363. )));
  364. } else {
  365. $context = NULL;
  366. }
  367. $old_error = error_get_last();
  368. $data = @file_get_contents($url, false, $context);
  369. $fetch_last_content_type = false; // reset if no type was sent from server
  370. if (isset($http_response_header) && is_array($http_response_header)) {
  371. foreach ($http_response_header as $h) {
  372. if (substr(strtolower($h), 0, 13) == 'content-type:') {
  373. $fetch_last_content_type = substr($h, 14);
  374. // don't abort here b/c there might be more than one
  375. // e.g. if we were being redirected -- last one is the right one
  376. }
  377. if (substr(strtolower($h), 0, 7) == 'http/1.') {
  378. $fetch_last_error_code = (int) substr($h, 9, 3);
  379. }
  380. }
  381. }
  382. if (!$data) {
  383. $error = error_get_last();
  384. if ($error['message'] != $old_error['message']) {
  385. $fetch_last_error = $error["message"];
  386. } else {
  387. $fetch_last_error = "HTTP Code: $fetch_last_error_code";
  388. }
  389. }
  390. return $data;
  391. }
  392. }
  393. /**
  394. * Try to determine the favicon URL for a feed.
  395. * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
  396. * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
  397. *
  398. * @param string $url A feed or page URL
  399. * @access public
  400. * @return mixed The favicon URL, or false if none was found.
  401. */
  402. function get_favicon_url($url) {
  403. $favicon_url = false;
  404. if ($html = @fetch_file_contents($url)) {
  405. libxml_use_internal_errors(true);
  406. $doc = new DOMDocument();
  407. $doc->loadHTML($html);
  408. $xpath = new DOMXPath($doc);
  409. $base = $xpath->query('/html/head/base');
  410. foreach ($base as $b) {
  411. $url = $b->getAttribute("href");
  412. break;
  413. }
  414. $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]');
  415. if (count($entries) > 0) {
  416. foreach ($entries as $entry) {
  417. $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
  418. break;
  419. }
  420. }
  421. }
  422. if (!$favicon_url)
  423. $favicon_url = rewrite_relative_url($url, "/favicon.ico");
  424. return $favicon_url;
  425. } // function get_favicon_url
  426. function check_feed_favicon($site_url, $feed) {
  427. # print "FAVICON [$site_url]: $favicon_url\n";
  428. $icon_file = ICONS_DIR . "/$feed.ico";
  429. if (!file_exists($icon_file)) {
  430. $favicon_url = get_favicon_url($site_url);
  431. if ($favicon_url) {
  432. // Limiting to "image" type misses those served with text/plain
  433. $contents = fetch_file_contents($favicon_url); // , "image");
  434. if ($contents) {
  435. // Crude image type matching.
  436. // Patterns gleaned from the file(1) source code.
  437. if (preg_match('/^\x00\x00\x01\x00/', $contents)) {
  438. // 0 string \000\000\001\000 MS Windows icon resource
  439. //error_log("check_feed_favicon: favicon_url=$favicon_url isa MS Windows icon resource");
  440. }
  441. elseif (preg_match('/^GIF8/', $contents)) {
  442. // 0 string GIF8 GIF image data
  443. //error_log("check_feed_favicon: favicon_url=$favicon_url isa GIF image");
  444. }
  445. elseif (preg_match('/^\x89PNG\x0d\x0a\x1a\x0a/', $contents)) {
  446. // 0 string \x89PNG\x0d\x0a\x1a\x0a PNG image data
  447. //error_log("check_feed_favicon: favicon_url=$favicon_url isa PNG image");
  448. }
  449. elseif (preg_match('/^\xff\xd8/', $contents)) {
  450. // 0 beshort 0xffd8 JPEG image data
  451. //error_log("check_feed_favicon: favicon_url=$favicon_url isa JPG image");
  452. }
  453. else {
  454. //error_log("check_feed_favicon: favicon_url=$favicon_url isa UNKNOWN type");
  455. $contents = "";
  456. }
  457. }
  458. if ($contents) {
  459. $fp = @fopen($icon_file, "w");
  460. if ($fp) {
  461. fwrite($fp, $contents);
  462. fclose($fp);
  463. chmod($icon_file, 0644);
  464. }
  465. }
  466. }
  467. return $icon_file;
  468. }
  469. }
  470. function print_select($id, $default, $values, $attributes = "") {
  471. print "<select name=\"$id\" id=\"$id\" $attributes>";
  472. foreach ($values as $v) {
  473. if ($v == $default)
  474. $sel = "selected=\"1\"";
  475. else
  476. $sel = "";
  477. $v = trim($v);
  478. print "<option value=\"$v\" $sel>$v</option>";
  479. }
  480. print "</select>";
  481. }
  482. function print_select_hash($id, $default, $values, $attributes = "") {
  483. print "<select name=\"$id\" id='$id' $attributes>";
  484. foreach (array_keys($values) as $v) {
  485. if ($v == $default)
  486. $sel = 'selected="selected"';
  487. else
  488. $sel = "";
  489. $v = trim($v);
  490. print "<option $sel value=\"$v\">".$values[$v]."</option>";
  491. }
  492. print "</select>";
  493. }
  494. function print_radio($id, $default, $true_is, $values, $attributes = "") {
  495. foreach ($values as $v) {
  496. if ($v == $default)
  497. $sel = "checked";
  498. else
  499. $sel = "";
  500. if ($v == $true_is) {
  501. $sel .= " value=\"1\"";
  502. } else {
  503. $sel .= " value=\"0\"";
  504. }
  505. print "<input class=\"noborder\" dojoType=\"dijit.form.RadioButton\"
  506. type=\"radio\" $sel $attributes name=\"$id\">&nbsp;$v&nbsp;";
  507. }
  508. }
  509. function initialize_user_prefs($uid, $profile = false) {
  510. $uid = db_escape_string($uid);
  511. if (!$profile) {
  512. $profile = "NULL";
  513. $profile_qpart = "AND profile IS NULL";
  514. } else {
  515. $profile_qpart = "AND profile = '$profile'";
  516. }
  517. if (get_schema_version() < 63) $profile_qpart = "";
  518. db_query("BEGIN");
  519. $result = db_query("SELECT pref_name,def_value FROM ttrss_prefs");
  520. $u_result = db_query("SELECT pref_name
  521. FROM ttrss_user_prefs WHERE owner_uid = '$uid' $profile_qpart");
  522. $active_prefs = array();
  523. while ($line = db_fetch_assoc($u_result)) {
  524. array_push($active_prefs, $line["pref_name"]);
  525. }
  526. while ($line = db_fetch_assoc($result)) {
  527. if (array_search($line["pref_name"], $active_prefs) === FALSE) {
  528. // print "adding " . $line["pref_name"] . "<br>";
  529. $line["def_value"] = db_escape_string($line["def_value"]);
  530. $line["pref_name"] = db_escape_string($line["pref_name"]);
  531. if (get_schema_version() < 63) {
  532. db_query("INSERT INTO ttrss_user_prefs
  533. (owner_uid,pref_name,value) VALUES
  534. ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')");
  535. } else {
  536. db_query("INSERT INTO ttrss_user_prefs
  537. (owner_uid,pref_name,value, profile) VALUES
  538. ('$uid', '".$line["pref_name"]."','".$line["def_value"]."', $profile)");
  539. }
  540. }
  541. }
  542. db_query("COMMIT");
  543. }
  544. function get_ssl_certificate_id() {
  545. if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) {
  546. return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
  547. $_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
  548. $_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
  549. $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
  550. }
  551. if ($_SERVER["SSL_CLIENT_M_SERIAL"]) {
  552. return sha1($_SERVER["SSL_CLIENT_M_SERIAL"] .
  553. $_SERVER["SSL_CLIENT_V_START"] .
  554. $_SERVER["SSL_CLIENT_V_END"] .
  555. $_SERVER["SSL_CLIENT_S_DN"]);
  556. }
  557. return "";
  558. }
  559. function authenticate_user($login, $password, $check_only = false) {
  560. if (!SINGLE_USER_MODE) {
  561. $user_id = false;
  562. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_AUTH_USER) as $plugin) {
  563. $user_id = (int) $plugin->authenticate($login, $password);
  564. if ($user_id) {
  565. $_SESSION["auth_module"] = strtolower(get_class($plugin));
  566. break;
  567. }
  568. }
  569. if ($user_id && !$check_only) {
  570. @session_start();
  571. $_SESSION["uid"] = $user_id;
  572. $_SESSION["version"] = VERSION_STATIC;
  573. $result = db_query("SELECT login,access_level,pwd_hash FROM ttrss_users
  574. WHERE id = '$user_id'");
  575. $_SESSION["name"] = db_fetch_result($result, 0, "login");
  576. $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
  577. $_SESSION["csrf_token"] = uniqid(rand(), true);
  578. db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
  579. $_SESSION["uid"]);
  580. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  581. $_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']);
  582. $_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
  583. $_SESSION["last_version_check"] = time();
  584. initialize_user_prefs($_SESSION["uid"]);
  585. return true;
  586. }
  587. return false;
  588. } else {
  589. $_SESSION["uid"] = 1;
  590. $_SESSION["name"] = "admin";
  591. $_SESSION["access_level"] = 10;
  592. $_SESSION["hide_hello"] = true;
  593. $_SESSION["hide_logout"] = true;
  594. $_SESSION["auth_module"] = false;
  595. if (!$_SESSION["csrf_token"]) {
  596. $_SESSION["csrf_token"] = uniqid(rand(), true);
  597. }
  598. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  599. initialize_user_prefs($_SESSION["uid"]);
  600. return true;
  601. }
  602. }
  603. function make_password($length = 8) {
  604. $password = "";
  605. $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ";
  606. $i = 0;
  607. while ($i < $length) {
  608. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  609. if (!strstr($password, $char)) {
  610. $password .= $char;
  611. $i++;
  612. }
  613. }
  614. return $password;
  615. }
  616. // this is called after user is created to initialize default feeds, labels
  617. // or whatever else
  618. // user preferences are checked on every login, not here
  619. function initialize_user($uid) {
  620. db_query("insert into ttrss_feeds (owner_uid,title,feed_url)
  621. values ('$uid', 'Tiny Tiny RSS: Forum',
  622. 'http://tt-rss.org/forum/rss.php')");
  623. }
  624. function logout_user() {
  625. session_destroy();
  626. if (isset($_COOKIE[session_name()])) {
  627. setcookie(session_name(), '', time()-42000, '/');
  628. }
  629. }
  630. function validate_csrf($csrf_token) {
  631. return $csrf_token == $_SESSION['csrf_token'];
  632. }
  633. function load_user_plugins($owner_uid) {
  634. if ($owner_uid && SCHEMA_VERSION >= 100) {
  635. $plugins = get_pref("_ENABLED_PLUGINS", $owner_uid);
  636. PluginHost::getInstance()->load($plugins, PluginHost::KIND_USER, $owner_uid);
  637. if (get_schema_version() > 100) {
  638. PluginHost::getInstance()->load_data();
  639. }
  640. }
  641. }
  642. function login_sequence() {
  643. if (SINGLE_USER_MODE) {
  644. @session_start();
  645. authenticate_user("admin", null);
  646. startup_gettext();
  647. load_user_plugins($_SESSION["uid"]);
  648. } else {
  649. if (!validate_session()) $_SESSION["uid"] = false;
  650. if (!$_SESSION["uid"]) {
  651. if (AUTH_AUTO_LOGIN && authenticate_user(null, null)) {
  652. $_SESSION["ref_schema_version"] = get_schema_version(true);
  653. } else {
  654. authenticate_user(null, null, true);
  655. }
  656. if (!$_SESSION["uid"]) {
  657. @session_destroy();
  658. setcookie(session_name(), '', time()-42000, '/');
  659. render_login_form();
  660. exit;
  661. }
  662. } else {
  663. /* bump login timestamp */
  664. db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
  665. $_SESSION["uid"]);
  666. $_SESSION["last_login_update"] = time();
  667. }
  668. if ($_SESSION["uid"]) {
  669. startup_gettext();
  670. load_user_plugins($_SESSION["uid"]);
  671. /* cleanup ccache */
  672. db_query("DELETE FROM ttrss_counters_cache WHERE owner_uid = ".
  673. $_SESSION["uid"] . " AND
  674. (SELECT COUNT(id) FROM ttrss_feeds WHERE
  675. ttrss_feeds.id = feed_id) = 0");
  676. db_query("DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ".
  677. $_SESSION["uid"] . " AND
  678. (SELECT COUNT(id) FROM ttrss_feed_categories WHERE
  679. ttrss_feed_categories.id = feed_id) = 0");
  680. }
  681. }
  682. }
  683. function truncate_string($str, $max_len, $suffix = '&hellip;') {
  684. if (mb_strlen($str, "utf-8") > $max_len) {
  685. return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
  686. } else {
  687. return $str;
  688. }
  689. }
  690. function convert_timestamp($timestamp, $source_tz, $dest_tz) {
  691. try {
  692. $source_tz = new DateTimeZone($source_tz);
  693. } catch (Exception $e) {
  694. $source_tz = new DateTimeZone('UTC');
  695. }
  696. try {
  697. $dest_tz = new DateTimeZone($dest_tz);
  698. } catch (Exception $e) {
  699. $dest_tz = new DateTimeZone('UTC');
  700. }
  701. $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
  702. return $dt->format('U') + $dest_tz->getOffset($dt);
  703. }
  704. function make_local_datetime($timestamp, $long, $owner_uid = false,
  705. $no_smart_dt = false, $eta_min = false) {
  706. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  707. if (!$timestamp) $timestamp = '1970-01-01 0:00';
  708. global $utc_tz;
  709. global $user_tz;
  710. if (!$utc_tz) $utc_tz = new DateTimeZone('UTC');
  711. $timestamp = substr($timestamp, 0, 19);
  712. # We store date in UTC internally
  713. $dt = new DateTime($timestamp, $utc_tz);
  714. $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid);
  715. if ($user_tz_string != 'Automatic') {
  716. try {
  717. if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string);
  718. } catch (Exception $e) {
  719. $user_tz = $utc_tz;
  720. }
  721. $tz_offset = $user_tz->getOffset($dt);
  722. } else {
  723. $tz_offset = (int) -$_SESSION["clientTzOffset"];
  724. }
  725. $user_timestamp = $dt->format('U') + $tz_offset;
  726. if (!$no_smart_dt) {
  727. return smart_date_time($user_timestamp,
  728. $tz_offset, $owner_uid, $eta_min);
  729. } else {
  730. if ($long)
  731. $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
  732. else
  733. $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
  734. return date($format, $user_timestamp);
  735. }
  736. }
  737. function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) {
  738. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  739. if ($eta_min && time() + $tz_offset - $timestamp < 3600) {
  740. return T_sprintf("%d min", date("i", time() + $tz_offset - $timestamp));
  741. } else if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
  742. return date("G:i", $timestamp);
  743. } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
  744. $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
  745. return date($format, $timestamp);
  746. } else {
  747. $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
  748. return date($format, $timestamp);
  749. }
  750. }
  751. function sql_bool_to_bool($s) {
  752. if ($s == "t" || $s == "1" || strtolower($s) == "true") {
  753. return true;
  754. } else {
  755. return false;
  756. }
  757. }
  758. function bool_to_sql_bool($s) {
  759. if ($s) {
  760. return "true";
  761. } else {
  762. return "false";
  763. }
  764. }
  765. // Session caching removed due to causing wrong redirects to upgrade
  766. // script when get_schema_version() is called on an obsolete session
  767. // created on a previous schema version.
  768. function get_schema_version($nocache = false) {
  769. global $schema_version;
  770. if (!$schema_version && !$nocache) {
  771. $result = db_query("SELECT schema_version FROM ttrss_version");
  772. $version = db_fetch_result($result, 0, "schema_version");
  773. $schema_version = $version;
  774. return $version;
  775. } else {
  776. return $schema_version;
  777. }
  778. }
  779. function sanity_check() {
  780. require_once 'errors.php';
  781. global $ERRORS;
  782. $error_code = 0;
  783. $schema_version = get_schema_version(true);
  784. if ($schema_version != SCHEMA_VERSION) {
  785. $error_code = 5;
  786. }
  787. if (DB_TYPE == "mysql") {
  788. $result = db_query("SELECT true", false);
  789. if (db_num_rows($result) != 1) {
  790. $error_code = 10;
  791. }
  792. }
  793. if (db_escape_string("testTEST") != "testTEST") {
  794. $error_code = 12;
  795. }
  796. return array("code" => $error_code, "message" => $ERRORS[$error_code]);
  797. }
  798. function file_is_locked($filename) {
  799. if (file_exists(LOCK_DIRECTORY . "/$filename")) {
  800. if (function_exists('flock')) {
  801. $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r");
  802. if ($fp) {
  803. if (flock($fp, LOCK_EX | LOCK_NB)) {
  804. flock($fp, LOCK_UN);
  805. fclose($fp);
  806. return false;
  807. }
  808. fclose($fp);
  809. return true;
  810. } else {
  811. return false;
  812. }
  813. }
  814. return true; // consider the file always locked and skip the test
  815. } else {
  816. return false;
  817. }
  818. }
  819. function make_lockfile($filename) {
  820. $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
  821. if ($fp && flock($fp, LOCK_EX | LOCK_NB)) {
  822. $stat_h = fstat($fp);
  823. $stat_f = stat(LOCK_DIRECTORY . "/$filename");
  824. if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  825. if ($stat_h["ino"] != $stat_f["ino"] ||
  826. $stat_h["dev"] != $stat_f["dev"]) {
  827. return false;
  828. }
  829. }
  830. if (function_exists('posix_getpid')) {
  831. fwrite($fp, posix_getpid() . "\n");
  832. }
  833. return $fp;
  834. } else {
  835. return false;
  836. }
  837. }
  838. function make_stampfile($filename) {
  839. $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
  840. if (flock($fp, LOCK_EX | LOCK_NB)) {
  841. fwrite($fp, time() . "\n");
  842. flock($fp, LOCK_UN);
  843. fclose($fp);
  844. return true;
  845. } else {
  846. return false;
  847. }
  848. }
  849. function sql_random_function() {
  850. if (DB_TYPE == "mysql") {
  851. return "RAND()";
  852. } else {
  853. return "RANDOM()";
  854. }
  855. }
  856. function catchup_feed($feed, $cat_view, $owner_uid = false, $max_id = false, $mode = 'all') {
  857. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  858. //if (preg_match("/^-?[0-9][0-9]*$/", $feed) != false) {
  859. // Todo: all this interval stuff needs some generic generator function
  860. $date_qpart = "false";
  861. switch ($mode) {
  862. case "1day":
  863. if (DB_TYPE == "pgsql") {
  864. $date_qpart = "date_entered < NOW() - INTERVAL '1 day' ";
  865. } else {
  866. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 DAY) ";
  867. }
  868. break;
  869. case "1week":
  870. if (DB_TYPE == "pgsql") {
  871. $date_qpart = "date_entered < NOW() - INTERVAL '1 week' ";
  872. } else {
  873. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 1 WEEK) ";
  874. }
  875. break;
  876. case "2week":
  877. if (DB_TYPE == "pgsql") {
  878. $date_qpart = "date_entered < NOW() - INTERVAL '2 week' ";
  879. } else {
  880. $date_qpart = "date_entered < DATE_SUB(NOW(), INTERVAL 2 WEEK) ";
  881. }
  882. break;
  883. default:
  884. $date_qpart = "true";
  885. }
  886. if (is_numeric($feed)) {
  887. if ($cat_view) {
  888. if ($feed >= 0) {
  889. if ($feed > 0) {
  890. $children = getChildCategories($feed, $owner_uid);
  891. array_push($children, $feed);
  892. $children = join(",", $children);
  893. $cat_qpart = "cat_id IN ($children)";
  894. } else {
  895. $cat_qpart = "cat_id IS NULL";
  896. }
  897. db_query("UPDATE ttrss_user_entries
  898. SET unread = false, last_read = NOW() WHERE ref_id IN
  899. (SELECT id FROM
  900. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  901. AND owner_uid = $owner_uid AND unread = true AND feed_id IN
  902. (SELECT id FROM ttrss_feeds WHERE $cat_qpart) AND $date_qpart) as tmp)");
  903. } else if ($feed == -2) {
  904. db_query("UPDATE ttrss_user_entries
  905. SET unread = false,last_read = NOW() WHERE (SELECT COUNT(*)
  906. FROM ttrss_user_labels2, ttrss_entries WHERE article_id = ref_id AND id = ref_id AND $date_qpart) > 0
  907. AND unread = true AND owner_uid = $owner_uid");
  908. }
  909. } else if ($feed > 0) {
  910. db_query("UPDATE ttrss_user_entries
  911. SET unread = false, last_read = NOW() WHERE ref_id IN
  912. (SELECT id FROM
  913. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  914. AND owner_uid = $owner_uid AND unread = true AND feed_id = $feed AND $date_qpart) as tmp)");
  915. } else if ($feed < 0 && $feed > LABEL_BASE_INDEX) { // special, like starred
  916. if ($feed == -1) {
  917. db_query("UPDATE ttrss_user_entries
  918. SET unread = false, last_read = NOW() WHERE ref_id IN
  919. (SELECT id FROM
  920. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  921. AND owner_uid = $owner_uid AND unread = true AND marked = true AND $date_qpart) as tmp)");
  922. }
  923. if ($feed == -2) {
  924. db_query("UPDATE ttrss_user_entries
  925. SET unread = false, last_read = NOW() WHERE ref_id IN
  926. (SELECT id FROM
  927. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  928. AND owner_uid = $owner_uid AND unread = true AND published = true AND $date_qpart) as tmp)");
  929. }
  930. if ($feed == -3) {
  931. $intl = get_pref("FRESH_ARTICLE_MAX_AGE");
  932. if (DB_TYPE == "pgsql") {
  933. $match_part = "date_entered > NOW() - INTERVAL '$intl hour' ";
  934. } else {
  935. $match_part = "date_entered > DATE_SUB(NOW(),
  936. INTERVAL $intl HOUR) ";
  937. }
  938. db_query("UPDATE ttrss_user_entries
  939. SET unread = false, last_read = NOW() WHERE ref_id IN
  940. (SELECT id FROM
  941. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  942. AND owner_uid = $owner_uid AND score >= 0 AND unread = true AND $date_qpart AND $match_part) as tmp)");
  943. }
  944. if ($feed == -4) {
  945. db_query("UPDATE ttrss_user_entries
  946. SET unread = false, last_read = NOW() WHERE ref_id IN
  947. (SELECT id FROM
  948. (SELECT id FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id
  949. AND owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
  950. }
  951. } else if ($feed < LABEL_BASE_INDEX) { // label
  952. $label_id = feed_to_label_id($feed);
  953. db_query("UPDATE ttrss_user_entries
  954. SET unread = false, last_read = NOW() WHERE ref_id IN
  955. (SELECT id FROM
  956. (SELECT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_user_labels2 WHERE ref_id = id
  957. AND label_id = '$label_id' AND ref_id = article_id
  958. AND owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
  959. }
  960. ccache_update($feed, $owner_uid, $cat_view);
  961. } else { // tag
  962. db_query("UPDATE ttrss_user_entries
  963. SET unread = false, last_read = NOW() WHERE ref_id IN
  964. (SELECT id FROM
  965. (SELECT ttrss_entries.id FROM ttrss_entries, ttrss_user_entries, ttrss_tags WHERE ref_id = ttrss_entries.id
  966. AND post_int_id = int_id AND tag_name = '$feed'
  967. AND ttrss_user_entries.owner_uid = $owner_uid AND unread = true AND $date_qpart) as tmp)");
  968. }
  969. }
  970. function getAllCounters() {
  971. $data = getGlobalCounters();
  972. $data = array_merge($data, getVirtCounters());
  973. $data = array_merge($data, getLabelCounters());
  974. $data = array_merge($data, getFeedCounters());
  975. $data = array_merge($data, getCategoryCounters());
  976. return $data;
  977. }
  978. function getCategoryTitle($cat_id) {
  979. if ($cat_id == -1) {
  980. return __("Special");
  981. } else if ($cat_id == -2) {
  982. return __("Labels");
  983. } else {
  984. $result = db_query("SELECT title FROM ttrss_feed_categories WHERE
  985. id = '$cat_id'");
  986. if (db_num_rows($result) == 1) {
  987. return db_fetch_result($result, 0, "title");
  988. } else {
  989. return __("Uncategorized");
  990. }
  991. }
  992. }
  993. function getCategoryCounters() {
  994. $ret_arr = array();
  995. /* Labels category */
  996. $cv = array("id" => -2, "kind" => "cat",
  997. "counter" => getCategoryUnread(-2));
  998. array_push($ret_arr, $cv);
  999. $result = db_query("SELECT id AS cat_id, value AS unread,
  1000. (SELECT COUNT(id) FROM ttrss_feed_categories AS c2
  1001. WHERE c2.parent_cat = ttrss_feed_categories.id) AS num_children
  1002. FROM ttrss_feed_categories, ttrss_cat_counters_cache
  1003. WHERE ttrss_cat_counters_cache.feed_id = id AND
  1004. ttrss_cat_counters_cache.owner_uid = ttrss_feed_categories.owner_uid AND
  1005. ttrss_feed_categories.owner_uid = " . $_SESSION["uid"]);
  1006. while ($line = db_fetch_assoc($result)) {
  1007. $line["cat_id"] = (int) $line["cat_id"];
  1008. if ($line["num_children"] > 0) {
  1009. $child_counter = getCategoryChildrenUnread($line["cat_id"], $_SESSION["uid"]);
  1010. } else {
  1011. $child_counter = 0;
  1012. }
  1013. $cv = array("id" => $line["cat_id"], "kind" => "cat",
  1014. "counter" => $line["unread"] + $child_counter);
  1015. array_push($ret_arr, $cv);
  1016. }
  1017. /* Special case: NULL category doesn't actually exist in the DB */
  1018. $cv = array("id" => 0, "kind" => "cat",
  1019. "counter" => (int) ccache_find(0, $_SESSION["uid"], true));
  1020. array_push($ret_arr, $cv);
  1021. return $ret_arr;
  1022. }
  1023. // only accepts real cats (>= 0)
  1024. function getCategoryChildrenUnread($cat, $owner_uid = false) {
  1025. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1026. $result = db_query("SELECT id FROM ttrss_feed_categories WHERE parent_cat = '$cat'
  1027. AND owner_uid = $owner_uid");
  1028. $unread = 0;
  1029. while ($line = db_fetch_assoc($result)) {
  1030. $unread += getCategoryUnread($line["id"], $owner_uid);
  1031. $unread += getCategoryChildrenUnread($line["id"], $owner_uid);
  1032. }
  1033. return $unread;
  1034. }
  1035. function getCategoryUnread($cat, $owner_uid = false) {
  1036. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1037. if ($cat >= 0) {
  1038. if ($cat != 0) {
  1039. $cat_query = "cat_id = '$cat'";
  1040. } else {
  1041. $cat_query = "cat_id IS NULL";
  1042. }
  1043. $result = db_query("SELECT id FROM ttrss_feeds WHERE $cat_query
  1044. AND owner_uid = " . $owner_uid);
  1045. $cat_feeds = array();
  1046. while ($line = db_fetch_assoc($result)) {
  1047. array_push($cat_feeds, "feed_id = " . $line["id"]);
  1048. }
  1049. if (count($cat_feeds) == 0) return 0;
  1050. $match_part = implode(" OR ", $cat_feeds);
  1051. $result = db_query("SELECT COUNT(int_id) AS unread
  1052. FROM ttrss_user_entries
  1053. WHERE unread = true AND ($match_part)
  1054. AND owner_uid = " . $owner_uid);
  1055. $unread = 0;
  1056. # this needs to be rewritten
  1057. while ($line = db_fetch_assoc($result)) {
  1058. $unread += $line["unread"];
  1059. }
  1060. return $unread;
  1061. } else if ($cat == -1) {
  1062. return getFeedUnread(-1) + getFeedUnread(-2) + getFeedUnread(-3) + getFeedUnread(0);
  1063. } else if ($cat == -2) {
  1064. $result = db_query("
  1065. SELECT COUNT(unread) AS unread FROM
  1066. ttrss_user_entries, ttrss_user_labels2
  1067. WHERE article_id = ref_id AND unread = true
  1068. AND ttrss_user_entries.owner_uid = '$owner_uid'");
  1069. $unread = db_fetch_result($result, 0, "unread");
  1070. return $unread;
  1071. }
  1072. }
  1073. function getFeedUnread($feed, $is_cat = false) {
  1074. return getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]);
  1075. }
  1076. function getLabelUnread($label_id, $owner_uid = false) {
  1077. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1078. $result = db_query("SELECT COUNT(ref_id) AS unread FROM ttrss_user_entries, ttrss_user_labels2
  1079. WHERE owner_uid = '$owner_uid' AND unread = true AND label_id = '$label_id' AND article_id = ref_id");
  1080. if (db_num_rows($result) != 0) {
  1081. return db_fetch_result($result, 0, "unread");
  1082. } else {
  1083. return 0;
  1084. }
  1085. }
  1086. function getFeedArticles($feed, $is_cat = false, $unread_only = false,
  1087. $owner_uid = false) {
  1088. $n_feed = (int) $feed;
  1089. $need_entries = false;
  1090. if (!$owner_uid) $owner_uid = $_SESSION["uid"];
  1091. if ($unread_only) {
  1092. $unread_qpart = "unread = true";
  1093. } else {
  1094. $unread_qpart = "true";
  1095. }
  1096. if ($is_cat) {
  1097. return getCategoryUnread($n_feed, $owner_uid);
  1098. } else if ($n_feed == -6) {
  1099. return 0;
  1100. } else if ($feed != "0" && $n_feed == 0) {
  1101. $feed = db_escape_string($feed);
  1102. $result = db_query("SELECT SUM((SELECT COUNT(int_id)
  1103. FROM ttrss_user_entries,ttrss_entries WHERE int_id = post_int_id
  1104. AND ref_id = id AND $unread_qpart)) AS count FROM ttrss_tags
  1105. WHERE owner_uid = $owner_uid AND tag_name = '$feed'");
  1106. return db_fetch_result($result, 0, "count");
  1107. } else if ($n_feed == -1) {
  1108. $match_part = "marked = true";
  1109. } else if ($n_feed == -2) {
  1110. $match_part = "published = true";
  1111. } else if ($n_feed == -3) {
  1112. $match_part = "unread = true AND score >= 0";
  1113. $intl = get_pref("FRESH_ARTICLE_MAX_AGE", $owner_uid);
  1114. if (DB_TYPE == "pgsql") {
  1115. $match_part .= " AND date_entered > NOW() - INTERVAL '$intl hour' ";
  1116. } else {
  1117. $match_part .= " AND date_entered > DATE_SUB(NOW(), INTERVAL $intl HOUR) ";
  1118. }
  1119. $need_entries = true;
  1120. } else if ($n_feed == -4) {
  1121. $match_part = "true";
  1122. } else if ($n_feed >= 0) {
  1123. if ($n_feed != 0) {
  1124. $match_part = "feed_id = '$n_feed'";
  1125. } else {
  1126. $match_part = "feed_id IS NULL";
  1127. }
  1128. } else if ($feed < LABEL_BASE_INDEX) {
  1129. $label_id = feed_to_label_id($feed);
  1130. return getLabelUnread($label_id, $owner_uid);
  1131. }
  1132. if ($match_part) {
  1133. if ($need_entries) {
  1134. $from_qpart = "ttrss_user_entries,ttrss_entries";
  1135. $from_where = "ttrss_entries.id = ttrss_user_entries.ref_id AND";
  1136. } else {
  1137. $from_qpart = "ttrss_user_entries";
  1138. $from_where = "";
  1139. }
  1140. $query = "SELECT count(int_id) AS unread
  1141. FROM $from_qpart WHERE
  1142. $unread_qpart AND $from_where ($match_part) AND ttrss_user_entries.owner_uid = $owner_uid";
  1143. //echo "[$feed/$query]\n";
  1144. $result = db_query($query);
  1145. } else {
  1146. $result = db_query("SELECT COUNT(post_int_id) AS unread
  1147. FROM ttrss_tags,ttrss_user_entries,ttrss_entries
  1148. WHERE tag_name = '$feed' AND post_int_id = int_id AND ref_id = ttrss_entries.id
  1149. AND $unread_qpart AND ttrss_tags.owner_uid = " . $owner_uid);
  1150. }
  1151. $unread = db_fetch_result($result, 0, "unread");
  1152. return $unread;
  1153. }
  1154. function getGlobalUnread($user_id = false) {
  1155. if (!$user_id) {
  1156. $user_id = $_SESSION["uid"];
  1157. }
  1158. $result = db_query("SELECT SUM(value) AS c_id FROM ttrss_counters_cache
  1159. WHERE owner_uid = '$user_id' AND feed_id > 0");
  1160. $c_id = db_fetch_result($result, 0, "c_id");
  1161. return $c_id;
  1162. }
  1163. function getGlobalCounters($global_unread = -1) {
  1164. $ret_arr = array();
  1165. if ($global_unread == -1) {
  1166. $global_unread = getGlobalUnread();
  1167. }
  1168. $cv = array("id" => "global-unread",
  1169. "counter" => (int) $global_unread);
  1170. array_push($ret_arr, $cv);
  1171. $result = db_query("SELECT COUNT(id) AS fn FROM
  1172. ttrss_feeds WHERE owner_uid = " . $_SESSION["uid"]);
  1173. $subscribed_feeds = db_fetch_result($result, 0, "fn");
  1174. $cv = array("id" => "subscribed-feeds",
  1175. "counter" => (int) $subscribed_feeds);
  1176. array_push($ret_arr, $cv);
  1177. return $ret_arr;
  1178. }
  1179. function getVirtCounters() {
  1180. $ret_arr = array();
  1181. for ($i = 0; $i >= -4; $i--) {
  1182. $count = getFeedUnread($i);
  1183. if ($i == 0 || $i == -1 || $i == -2)
  1184. $auxctr = getFeedArticles($i, false);
  1185. else
  1186. $auxctr = 0;
  1187. $cv = array("id" => $i,
  1188. "counter" => (int) $count,
  1189. "auxcounter" => $auxctr);
  1190. // if (get_pref('EXTENDED_FEEDLIST'))
  1191. // $cv["xmsg"] = getFeedArticles($i)." ".__("total");
  1192. array_push($ret_arr, $cv);
  1193. }
  1194. $feeds = PluginHost::getInstance()->get_feeds(-1);
  1195. if (is_array($feeds)) {
  1196. foreach ($feeds as $feed) {
  1197. $cv = array("id" => PluginHost::pfeed_to_feed_id($feed['id']),
  1198. "counter" => $feed['sender']->get_unread($feed['id']));
  1199. if (method_exists($feed['sender'], 'get_total'))
  1200. $cv["auxcounter"] = $feed['sender']->get_total($feed['id']);
  1201. array_push($ret_arr, $cv);
  1202. }
  1203. }
  1204. return $ret_arr;
  1205. }
  1206. function getLabelCounters($descriptions = false) {
  1207. $ret_arr = array();
  1208. $owner_uid = $_SESSION["uid"];
  1209. $result = db_query("SELECT id,caption,SUM(CASE WHEN u1.unread = true THEN 1 ELSE 0 END) AS unread, COUNT(u1.unread) AS total
  1210. FROM ttrss_labels2 LEFT JOIN ttrss_user_labels2 ON
  1211. (ttrss_labels2.id = label_id)
  1212. LEFT JOIN ttrss_user_entries AS u1 ON u1.ref_id = article_id
  1213. WHERE ttrss_labels2.owner_uid = $owner_uid GROUP BY ttrss_labels2.id,
  1214. ttrss_labels2.caption");
  1215. while ($line = db_fetch_assoc($result)) {
  1216. $id = label_to_feed_id($line["id"]);
  1217. $cv = array("id" => $id,
  1218. "counter" => (int) $line["unread"],
  1219. "auxcounter" => (int) $line["total"]);
  1220. if ($descriptions)
  1221. $cv["description"] = $line["caption"];
  1222. array_push($ret_arr, $cv);
  1223. }
  1224. return $ret_arr;
  1225. }
  1226. function getFeedCounters($active_feed = false) {
  1227. $ret_arr = array();
  1228. $query = "SELECT ttrss_feeds.id,
  1229. ttrss_feeds.title,
  1230. ".SUBSTRING_FOR_DATE."(ttrss_feeds.last_updated,1,19) AS last_updated,
  1231. last_error, value AS count
  1232. FROM ttrss_feeds, ttrss_counters_cache
  1233. WHERE ttrss_feeds.owner_uid = ".$_SESSION["uid"]."
  1234. AND ttrss_counters_cache.owner_uid = ttrss_feeds.owner_uid
  1235. AND ttrss_counters_cache.feed_id = id";
  1236. $result = db_query($query);
  1237. while ($line = db_fetch_assoc($result)) {
  1238. $id = $line["id"];
  1239. $count = $line["count"];
  1240. $last_error = htmlspecialchars($line["last_error"]);
  1241. $last_updated = make_local_datetime($line['last_updated'], false);
  1242. $has_img = feed_has_icon($id);
  1243. if (date('Y') - date('Y', strtotime($line['last_updated'])) > 2)
  1244. $last_updated = '';
  1245. $cv = array("id" => $id,
  1246. "updated" => $last_updated,
  1247. "counter" => (int) $count,
  1248. "has_img" => (int) $has_img);
  1249. if ($last_error)
  1250. $cv["error"] = $last_error;
  1251. // if (get_pref('EXTENDED_FEEDLIST'))
  1252. // $cv["xmsg"] = getFeedArticles($id)." ".__("total");
  1253. if ($active_feed && $id == $active_feed)
  1254. $cv["title"] = truncate_string($line["title"], 30);
  1255. array_push($ret_arr, $cv);
  1256. }
  1257. return $ret_arr;
  1258. }
  1259. function get_pgsql_version() {
  1260. $result = db_query("SELECT version() AS version");
  1261. $version = explode(" ", db_fetch_result($result, 0, "version"));
  1262. return $version[1];
  1263. }
  1264. /**
  1265. * @return array (code => Status code, message => error message if available)
  1266. *
  1267. * 0 - OK, Feed already exists
  1268. * 1 - OK, Feed added
  1269. * 2 - Invalid URL
  1270. * 3 - URL content is HTML, no feeds available
  1271. * 4 - URL content is HTML which contains multiple feeds.
  1272. * Here you should call extractfeedurls in rpc-backend
  1273. * to get all possible feeds.
  1274. * 5 - Couldn't download the URL content.
  1275. * 6 - Content is an invalid XML.
  1276. */
  1277. function subscribe_to_feed($url, $cat_id = 0,
  1278. $auth_login = '', $auth_pass = '') {
  1279. global $fetch_last_error;
  1280. require_once "include/rssfuncs.php";
  1281. $url = fix_url($url);
  1282. if (!$url || !validate_feed_url($url)) return array("code" => 2);
  1283. $contents = @fetch_file_contents($url, false, $auth_login, $auth_pass);
  1284. if (!$contents) {
  1285. return array("code" => 5, "message" => $fetch_last_error);
  1286. }
  1287. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SUBSCRIBE_FEED) as $plugin) {
  1288. $contents = $plugin->hook_subscribe_feed($contents, $url, $auth_login, $auth_pass);
  1289. }
  1290. if (is_html($contents)) {
  1291. $feedUrls = get_feeds_from_html($url, $contents);
  1292. if (count($feedUrls) == 0) {
  1293. return array("code" => 3);
  1294. } else if (count($feedUrls) > 1) {
  1295. return array("code" => 4, "feeds" => $feedUrls);
  1296. }
  1297. //use feed url as new URL
  1298. $url = key($feedUrls);
  1299. }
  1300. if ($cat_id == "0" || !$cat_id) {
  1301. $cat_qpart = "NULL";
  1302. } else {
  1303. $cat_qpart = "'$cat_id'";
  1304. }
  1305. $result = db_query(
  1306. "SELECT id FROM ttrss_feeds
  1307. WHERE feed_url = '$url' AND owner_uid = ".$_SESSION["uid"]);
  1308. if (strlen(FEED_CRYPT_KEY) > 0) {
  1309. require_once "crypt.php";
  1310. $auth_pass = substr(encrypt_string($auth_pass), 0, 250);
  1311. $auth_pass_encrypted = 'true';
  1312. } else {
  1313. $auth_pass_encrypted = 'false';
  1314. }
  1315. $auth_pass = db_escape_string($auth_pass);
  1316. if (db_num_rows($result) == 0) {
  1317. $result = db_query(
  1318. "INSERT INTO ttrss_feeds
  1319. (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted)
  1320. VALUES ('".$_SESSION["uid"]."', '$url',
  1321. '[Unknown]', $cat_qpart, '$auth_login', '$auth_pass', 0, $auth_pass_encrypted)");
  1322. $result = db_query(
  1323. "SELECT id FROM ttrss_feeds WHERE feed_url = '$url'
  1324. AND owner_uid = " . $_SESSION["uid"]);
  1325. $feed_id = db_fetch_result($result, 0, "id");
  1326. if ($feed_id) {
  1327. set_basic_feed_info($feed_id);
  1328. }
  1329. return array("code" => 1);
  1330. } else {
  1331. return array("code" => 0);
  1332. }
  1333. }
  1334. function print_feed_select($id, $default_id = "",
  1335. $attributes = "", $include_all_feeds = true,
  1336. $root_id = false, $nest_level = 0) {
  1337. if (!$root_id) {
  1338. print "<select id=\"$id\" name=\"$id\" $attributes>";
  1339. if ($include_all_feeds) {
  1340. $is_selected = ("0" == $default_id) ? "selected=\"1\"" : "";
  1341. print "<option $is_selected value=\"0\">".__('All feeds')."</option>";
  1342. }
  1343. }
  1344. if (get_pref('ENABLE_FEED_CATS')) {
  1345. if ($root_id)
  1346. $parent_qpart = "parent_cat = '$root_id'";
  1347. else
  1348. $parent_qpart = "parent_cat IS NULL";
  1349. $result = db_query("SELECT id,title,
  1350. (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
  1351. c2.parent_cat = ttrss_feed_categories.id) AS num_children
  1352. FROM ttrss_feed_categories
  1353. WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
  1354. while ($line = db_fetch_assoc($result)) {
  1355. for ($i = 0; $i < $nest_level; $i++)
  1356. $line["title"] = " - " . $line["title"];
  1357. $is_selected = ("CAT:".$line["id"] == $default_id) ? "selected=\"1\"" : "";
  1358. printf("<option $is_selected value='CAT:%d'>%s</option>",
  1359. $line["id"], htmlspecialchars($line["title"]));
  1360. if ($line["num_children"] > 0)
  1361. print_feed_select($id, $default_id, $attributes,
  1362. $include_all_feeds, $line["id"], $nest_level+1);
  1363. $feed_result = db_query("SELECT id,title FROM ttrss_feeds
  1364. WHERE cat_id = '".$line["id"]."' AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
  1365. while ($fline = db_fetch_assoc($feed_result)) {
  1366. $is_selected = ($fline["id"] == $default_id) ? "selected=\"1\"" : "";
  1367. $fline["title"] = " + " . $fline["title"];
  1368. for ($i = 0; $i < $nest_level; $i++)
  1369. $fline["title"] = " - " . $fline["title"];
  1370. printf("<option $is_selected value='%d'>%s</option>",
  1371. $fline["id"], htmlspecialchars($fline["title"]));
  1372. }
  1373. }
  1374. if (!$root_id) {
  1375. $default_is_cat = ($default_id == "CAT:0");
  1376. $is_selected = $default_is_cat ? "selected=\"1\"" : "";
  1377. printf("<option $is_selected value='CAT:0'>%s</option>",
  1378. __("Uncategorized"));
  1379. $feed_result = db_query("SELECT id,title FROM ttrss_feeds
  1380. WHERE cat_id IS NULL AND owner_uid = ".$_SESSION["uid"] . " ORDER BY title");
  1381. while ($fline = db_fetch_assoc($feed_result)) {
  1382. $is_selected = ($fline["id"] == $default_id && !$default_is_cat) ? "selected=\"1\"" : "";
  1383. $fline["title"] = " + " . $fline["title"];
  1384. for ($i = 0; $i < $nest_level; $i++)
  1385. $fline["title"] = " - " . $fline["title"];
  1386. printf("<option $is_selected value='%d'>%s</option>",
  1387. $fline["id"], htmlspecialchars($fline["title"]));
  1388. }
  1389. }
  1390. } else {
  1391. $result = db_query("SELECT id,title FROM ttrss_feeds
  1392. WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY title");
  1393. while ($line = db_fetch_assoc($result)) {
  1394. $is_selected = ($line["id"] == $default_id) ? "selected=\"1\"" : "";
  1395. printf("<option $is_selected value='%d'>%s</option>",
  1396. $line["id"], htmlspecialchars($line["title"]));
  1397. }
  1398. }
  1399. if (!$root_id) {
  1400. print "</select>";
  1401. }
  1402. }
  1403. function print_feed_cat_select($id, $default_id,
  1404. $attributes, $include_all_cats = true, $root_id = false, $nest_level = 0) {
  1405. if (!$root_id) {
  1406. print "<select id=\"$id\" name=\"$id\" default=\"$default_id\" onchange=\"catSelectOnChange(this)\" $attributes>";
  1407. }
  1408. if ($root_id)
  1409. $parent_qpart = "parent_cat = '$root_id'";
  1410. else
  1411. $parent_qpart = "parent_cat IS NULL";
  1412. $result = db_query("SELECT id,title,
  1413. (SELECT COUNT(id) FROM ttrss_feed_categories AS c2 WHERE
  1414. c2.parent_cat = ttrss_feed_categories.id) AS num_children
  1415. FROM ttrss_feed_categories
  1416. WHERE owner_uid = ".$_SESSION["uid"]." AND $parent_qpart ORDER BY title");
  1417. while ($line = db_fetch_assoc($result)) {
  1418. if ($line["id"] == $default_id) {
  1419. $is_selected = "selected=\"1\"";
  1420. } else {
  1421. $is_selected = "";
  1422. }
  1423. for ($i = 0; $i < $nest_level; $i++)
  1424. $line["title"] = " - " . $line["title"];
  1425. if ($line["title"])
  1426. printf("<option $is_selected value='%d'>%s</option>",
  1427. $line["id"], htmlspecialchars($line["title"]));
  1428. if ($line["num_children"] > 0)
  1429. print_feed_cat_select($id, $default_id, $attributes,
  1430. $include_all_cats, $line["id"], $nest_level+1);
  1431. }
  1432. if (!$root_id) {
  1433. if ($include_all_cats) {
  1434. if (db_num_rows($result) > 0) {
  1435. print "<option disabled=\"1\">--------</option>";
  1436. }
  1437. if ($default_id == 0) {
  1438. $is_selected = "selected=\"1\"";
  1439. } else {
  1440. $is_selected = "";
  1441. }
  1442. print "<option $is_selected value=\"0\">".__('Uncategorized')."</option>";
  1443. }
  1444. print "</select>";
  1445. }
  1446. }
  1447. function checkbox_to_sql_bool($val) {
  1448. return ($val == "on") ? "true" : "false";
  1449. }
  1450. function getFeedCatTitle($id) {
  1451. if ($id == -1) {
  1452. return __("Special");
  1453. } else if ($id < LABEL_BASE_INDEX) {
  1454. return __("Labels");
  1455. } else if ($id > 0) {
  1456. $result = db_query("SELECT ttrss_feed_categories.title
  1457. FROM ttrss_feeds, ttrss_feed_categories WHERE ttrss_feeds.id = '$id' AND
  1458. cat_id = ttrss_feed_categories.id");
  1459. if (db_num_rows($result) == 1) {
  1460. return db_fetch_result($result, 0, "title");
  1461. } else {
  1462. return __("Uncategorized");
  1463. }
  1464. } else {
  1465. return "getFeedCatTitle($id) failed";
  1466. }
  1467. }
  1468. function getFeedIcon($id) {
  1469. switch ($id) {
  1470. case 0:
  1471. return "images/archive.png";
  1472. break;
  1473. case -1:
  1474. return "images/star.png";
  1475. break;
  1476. case -2:
  1477. return "images/feed.png";
  1478. break;
  1479. case -3:
  1480. return "images/fresh.png";
  1481. break;
  1482. case -4:
  1483. return "images/folder.png";
  1484. break;
  1485. case -6:
  1486. return "images/time.png";
  1487. break;
  1488. default:
  1489. if ($id < LABEL_BASE_INDEX) {
  1490. return "images/label.png";
  1491. } else {
  1492. if (file_exists(ICONS_DIR . "/$id.ico"))
  1493. return ICONS_URL . "/$id.ico";
  1494. }
  1495. break;
  1496. }
  1497. return false;
  1498. }
  1499. function getFeedTitle($id, $cat = false) {
  1500. if ($cat) {
  1501. return getCategoryTitle($id);
  1502. } else if ($id == -1) {
  1503. return __("Starred articles");
  1504. } else if ($id == -2) {
  1505. return __("Published articles");
  1506. } else if ($id == -3) {
  1507. return __("Fresh articles");
  1508. } else if ($id == -4) {
  1509. return __("All articles");
  1510. } else if ($id === 0 || $id === "0") {
  1511. return __("Archived articles");
  1512. } else if ($id == -6) {
  1513. return __("Recently read");
  1514. } else if ($id < LABEL_BASE_INDEX) {
  1515. $label_id = feed_to_label_id($id);
  1516. $result = db_query("SELECT caption FROM ttrss_labels2 WHERE id = '$label_id'");
  1517. if (db_num_rows($result) == 1) {
  1518. return db_fetch_result($result, 0, "caption");
  1519. } else {
  1520. return "Unknown label ($label_id)";
  1521. }
  1522. } else if (is_numeric($id) && $id > 0) {
  1523. $result = db_query("SELECT title FROM ttrss_feeds WHERE id = '$id'");
  1524. if (db_num_rows($result) == 1) {
  1525. return db_fetch_result($result, 0, "title");
  1526. } else {
  1527. return "Unknown feed ($id)";
  1528. }
  1529. } else {
  1530. return $id;
  1531. }
  1532. }
  1533. // TODO: less dumb splitting
  1534. require_once "functions2.php";
  1535. ?>