functions.php 52 KB

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