functions.php 53 KB

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