functions.php 52 KB

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