functions.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. <?php
  2. define('EXPECTED_CONFIG_VERSION', 26);
  3. define('SCHEMA_VERSION', 138);
  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_effective_url = false;
  12. $fetch_curl_used = false;
  13. libxml_disable_entity_loader(true);
  14. // separate test because this is included before sanity checks
  15. if (function_exists("mb_internal_encoding")) mb_internal_encoding("UTF-8");
  16. date_default_timezone_set('UTC');
  17. if (defined('E_DEPRECATED')) {
  18. error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
  19. } else {
  20. error_reporting(E_ALL & ~E_NOTICE);
  21. }
  22. require_once 'config.php';
  23. /**
  24. * Define a constant if not already defined
  25. */
  26. function define_default($name, $value) {
  27. defined($name) or define($name, $value);
  28. }
  29. /* Some tunables you can override in config.php using define(): */
  30. define_default('FEED_FETCH_TIMEOUT', 45);
  31. // How may seconds to wait for response when requesting feed from a site
  32. define_default('FEED_FETCH_NO_CACHE_TIMEOUT', 15);
  33. // How may seconds to wait for response when requesting feed from a
  34. // site when that feed wasn't cached before
  35. define_default('FILE_FETCH_TIMEOUT', 45);
  36. // Default timeout when fetching files from remote sites
  37. define_default('FILE_FETCH_CONNECT_TIMEOUT', 15);
  38. // How many seconds to wait for initial response from website when
  39. // fetching files from remote sites
  40. define_default('DAEMON_UPDATE_LOGIN_LIMIT', 30);
  41. // stop updating feeds if users haven't logged in for X days
  42. define_default('DAEMON_FEED_LIMIT', 500);
  43. // feed limit for one update batch
  44. define_default('DAEMON_SLEEP_INTERVAL', 120);
  45. // default sleep interval between feed updates (sec)
  46. define_default('MIN_CACHE_FILE_SIZE', 1024);
  47. // do not cache files smaller than that (bytes)
  48. define_default('MAX_CACHE_FILE_SIZE', 64*1024*1024);
  49. // do not cache files larger than that (bytes)
  50. define_default('MAX_DOWNLOAD_FILE_SIZE', 16*1024*1024);
  51. // do not download general files larger than that (bytes)
  52. define_default('CACHE_MAX_DAYS', 7);
  53. // max age in days for various automatically cached (temporary) files
  54. define_default('MAX_CONDITIONAL_INTERVAL', 3600*12);
  55. // max interval between forced unconditional updates for servers
  56. // not complying with http if-modified-since (seconds)
  57. /* tunables end here */
  58. if (DB_TYPE == "pgsql") {
  59. define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE');
  60. } else {
  61. define('SUBSTRING_FOR_DATE', 'SUBSTRING');
  62. }
  63. /**
  64. * Return available translations names.
  65. *
  66. * @access public
  67. * @return array A array of available translations.
  68. */
  69. function get_translations() {
  70. $tr = array(
  71. "auto" => __("Detect automatically"),
  72. "ar_SA" => "العربيّة (Arabic)",
  73. "bg_BG" => "Bulgarian",
  74. "da_DA" => "Dansk",
  75. "ca_CA" => "Català",
  76. "cs_CZ" => "Česky",
  77. "en_US" => "English",
  78. "el_GR" => "Ελληνικά",
  79. "es_ES" => "Español (España)",
  80. "es_LA" => "Español",
  81. "de_DE" => "Deutsch",
  82. "fr_FR" => "Français",
  83. "hu_HU" => "Magyar (Hungarian)",
  84. "it_IT" => "Italiano",
  85. "ja_JP" => "日本語 (Japanese)",
  86. "lv_LV" => "Latviešu",
  87. "nb_NO" => "Norwegian bokmål",
  88. "nl_NL" => "Dutch",
  89. "pl_PL" => "Polski",
  90. "ru_RU" => "Русский",
  91. "pt_BR" => "Portuguese/Brazil",
  92. "pt_PT" => "Portuguese/Portugal",
  93. "zh_CN" => "Simplified Chinese",
  94. "zh_TW" => "Traditional Chinese",
  95. "uk_UA" => "Українська",
  96. "sv_SE" => "Svenska",
  97. "fi_FI" => "Suomi",
  98. "tr_TR" => "Türkçe");
  99. return $tr;
  100. }
  101. require_once "lib/accept-to-gettext.php";
  102. require_once "lib/gettext/gettext.inc";
  103. function startup_gettext() {
  104. # Get locale from Accept-Language header
  105. $lang = al2gt(array_keys(get_translations()), "text/html");
  106. if (defined('_TRANSLATION_OVERRIDE_DEFAULT')) {
  107. $lang = _TRANSLATION_OVERRIDE_DEFAULT;
  108. }
  109. if ($_SESSION["uid"] && get_schema_version() >= 120) {
  110. $pref_lang = get_pref("USER_LANGUAGE", $_SESSION["uid"]);
  111. if ($pref_lang && $pref_lang != 'auto') {
  112. $lang = $pref_lang;
  113. }
  114. }
  115. if ($lang) {
  116. if (defined('LC_MESSAGES')) {
  117. _setlocale(LC_MESSAGES, $lang);
  118. } else if (defined('LC_ALL')) {
  119. _setlocale(LC_ALL, $lang);
  120. }
  121. _bindtextdomain("messages", "locale");
  122. _textdomain("messages");
  123. _bind_textdomain_codeset("messages", "UTF-8");
  124. }
  125. }
  126. require_once 'db-prefs.php';
  127. require_once 'version.php';
  128. require_once 'controls.php';
  129. define('SELF_USER_AGENT', 'Tiny Tiny RSS/' . VERSION . ' (http://tt-rss.org/)');
  130. ini_set('user_agent', SELF_USER_AGENT);
  131. $schema_version = false;
  132. // TODO: compat wrapper, remove at some point
  133. function _debug($msg) {
  134. Debug::log($msg);
  135. }
  136. /**
  137. * Purge a feed old posts.
  138. *
  139. * @param mixed $link A database connection.
  140. * @param mixed $feed_id The id of the purged feed.
  141. * @param mixed $purge_interval Olderness of purged posts.
  142. * @param boolean $debug Set to True to enable the debug. False by default.
  143. * @access public
  144. * @return void
  145. */
  146. function purge_feed($feed_id, $purge_interval) {
  147. if (!$purge_interval) $purge_interval = feed_purge_interval($feed_id);
  148. $pdo = Db::pdo();
  149. $sth = $pdo->prepare("SELECT owner_uid FROM ttrss_feeds WHERE id = ?");
  150. $sth->execute([$feed_id]);
  151. $owner_uid = false;
  152. if ($row = $sth->fetch()) {
  153. $owner_uid = $row["owner_uid"];
  154. }
  155. if ($purge_interval == -1 || !$purge_interval) {
  156. if ($owner_uid) {
  157. CCache::update($feed_id, $owner_uid);
  158. }
  159. return;
  160. }
  161. if (!$owner_uid) return;
  162. if (FORCE_ARTICLE_PURGE == 0) {
  163. $purge_unread = get_pref("PURGE_UNREAD_ARTICLES",
  164. $owner_uid, false);
  165. } else {
  166. $purge_unread = true;
  167. $purge_interval = FORCE_ARTICLE_PURGE;
  168. }
  169. if (!$purge_unread)
  170. $query_limit = " unread = false AND ";
  171. else
  172. $query_limit = "";
  173. $purge_interval = (int) $purge_interval;
  174. if (DB_TYPE == "pgsql") {
  175. $sth = $pdo->prepare("DELETE FROM ttrss_user_entries
  176. USING ttrss_entries
  177. WHERE ttrss_entries.id = ref_id AND
  178. marked = false AND
  179. feed_id = ? AND
  180. $query_limit
  181. ttrss_entries.date_updated < NOW() - INTERVAL '$purge_interval days'");
  182. $sth->execute([$feed_id]);
  183. } else {
  184. $sth = $pdo->prepare("DELETE FROM ttrss_user_entries
  185. USING ttrss_user_entries, ttrss_entries
  186. WHERE ttrss_entries.id = ref_id AND
  187. marked = false AND
  188. feed_id = ? AND
  189. $query_limit
  190. ttrss_entries.date_updated < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)");
  191. $sth->execute([$feed_id]);
  192. }
  193. $rows = $sth->rowCount();
  194. CCache::update($feed_id, $owner_uid);
  195. Debug::log("Purged feed $feed_id ($purge_interval): deleted $rows articles");
  196. return $rows;
  197. } // function purge_feed
  198. function feed_purge_interval($feed_id) {
  199. $pdo = DB::pdo();
  200. $sth = $pdo->prepare("SELECT purge_interval, owner_uid FROM ttrss_feeds
  201. WHERE id = ?");
  202. $sth->execute([$feed_id]);
  203. if ($row = $sth->fetch()) {
  204. $purge_interval = $row["purge_interval"];
  205. $owner_uid = $row["owner_uid"];
  206. if ($purge_interval == 0) $purge_interval = get_pref(
  207. 'PURGE_OLD_DAYS', $owner_uid);
  208. return $purge_interval;
  209. } else {
  210. return -1;
  211. }
  212. }
  213. // TODO: max_size currently only works for CURL transfers
  214. // TODO: multiple-argument way is deprecated, first parameter is a hash now
  215. function fetch_file_contents($options /* previously: 0: $url , 1: $type = false, 2: $login = false, 3: $pass = false,
  216. 4: $post_query = false, 5: $timeout = false, 6: $timestamp = 0, 7: $useragent = false*/) {
  217. global $fetch_last_error;
  218. global $fetch_last_error_code;
  219. global $fetch_last_error_content;
  220. global $fetch_last_content_type;
  221. global $fetch_last_modified;
  222. global $fetch_effective_url;
  223. global $fetch_curl_used;
  224. $fetch_last_error = false;
  225. $fetch_last_error_code = -1;
  226. $fetch_last_error_content = "";
  227. $fetch_last_content_type = "";
  228. $fetch_curl_used = false;
  229. $fetch_last_modified = "";
  230. $fetch_effective_url = "";
  231. if (!is_array($options)) {
  232. // falling back on compatibility shim
  233. $option_names = [ "url", "type", "login", "pass", "post_query", "timeout", "last_modified", "useragent" ];
  234. $tmp = [];
  235. for ($i = 0; $i < func_num_args(); $i++) {
  236. $tmp[$option_names[$i]] = func_get_arg($i);
  237. }
  238. $options = $tmp;
  239. /*$options = array(
  240. "url" => func_get_arg(0),
  241. "type" => @func_get_arg(1),
  242. "login" => @func_get_arg(2),
  243. "pass" => @func_get_arg(3),
  244. "post_query" => @func_get_arg(4),
  245. "timeout" => @func_get_arg(5),
  246. "timestamp" => @func_get_arg(6),
  247. "useragent" => @func_get_arg(7)
  248. ); */
  249. }
  250. $url = $options["url"];
  251. $type = isset($options["type"]) ? $options["type"] : false;
  252. $login = isset($options["login"]) ? $options["login"] : false;
  253. $pass = isset($options["pass"]) ? $options["pass"] : false;
  254. $post_query = isset($options["post_query"]) ? $options["post_query"] : false;
  255. $timeout = isset($options["timeout"]) ? $options["timeout"] : false;
  256. $last_modified = isset($options["last_modified"]) ? $options["last_modified"] : "";
  257. $useragent = isset($options["useragent"]) ? $options["useragent"] : false;
  258. $followlocation = isset($options["followlocation"]) ? $options["followlocation"] : true;
  259. $max_size = isset($options["max_size"]) ? $options["max_size"] : MAX_DOWNLOAD_FILE_SIZE; // in bytes
  260. $http_accept = isset($options["http_accept"]) ? $options["http_accept"] : false;
  261. $url = ltrim($url, ' ');
  262. $url = str_replace(' ', '%20', $url);
  263. if (strpos($url, "//") === 0)
  264. $url = 'http:' . $url;
  265. if (!defined('NO_CURL') && function_exists('curl_init') && !ini_get("open_basedir")) {
  266. $fetch_curl_used = true;
  267. $ch = curl_init($url);
  268. $curl_http_headers = [];
  269. if ($last_modified && !$post_query)
  270. array_push($curl_http_headers, "If-Modified-Since: $last_modified");
  271. if ($http_accept)
  272. array_push($curl_http_headers, "Accept: " . $http_accept);
  273. if (count($curl_http_headers) > 0)
  274. curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_http_headers);
  275. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout ? $timeout : FILE_FETCH_CONNECT_TIMEOUT);
  276. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout ? $timeout : FILE_FETCH_TIMEOUT);
  277. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, !ini_get("open_basedir") && $followlocation);
  278. curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
  279. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  280. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  281. curl_setopt($ch, CURLOPT_HEADER, true);
  282. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  283. curl_setopt($ch, CURLOPT_USERAGENT, $useragent ? $useragent :
  284. SELF_USER_AGENT);
  285. curl_setopt($ch, CURLOPT_ENCODING, "");
  286. //curl_setopt($ch, CURLOPT_REFERER, $url);
  287. if ($max_size) {
  288. curl_setopt($ch, CURLOPT_NOPROGRESS, false);
  289. curl_setopt($ch, CURLOPT_BUFFERSIZE, 16384); // needed to get 5 arguments in progress function?
  290. // holy shit closures in php
  291. // download & upload are *expected* sizes respectively, could be zero
  292. curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function($curl_handle, $download_size, $downloaded, $upload_size, $uploaded) use( &$max_size) {
  293. Debug::log("[curl progressfunction] $downloaded $max_size", Debug::$LOG_EXTENDED);
  294. return ($downloaded > $max_size) ? 1 : 0; // if max size is set, abort when exceeding it
  295. });
  296. }
  297. if (!ini_get("open_basedir")) {
  298. curl_setopt($ch, CURLOPT_COOKIEJAR, "/dev/null");
  299. }
  300. if (defined('_HTTP_PROXY')) {
  301. curl_setopt($ch, CURLOPT_PROXY, _HTTP_PROXY);
  302. }
  303. if ($post_query) {
  304. curl_setopt($ch, CURLOPT_POST, true);
  305. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_query);
  306. }
  307. if ($login && $pass)
  308. curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
  309. $ret = @curl_exec($ch);
  310. $headers_length = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  311. $headers = explode("\r\n", substr($ret, 0, $headers_length));
  312. $contents = substr($ret, $headers_length);
  313. foreach ($headers as $header) {
  314. if (strstr($header, ": ") !== FALSE) {
  315. list ($key, $value) = explode(": ", $header);
  316. if (strtolower($key) == "last-modified") {
  317. $fetch_last_modified = $value;
  318. }
  319. }
  320. if (substr(strtolower($header), 0, 7) == 'http/1.') {
  321. $fetch_last_error_code = (int) substr($header, 9, 3);
  322. $fetch_last_error = $header;
  323. }
  324. }
  325. if (curl_errno($ch) === 23 || curl_errno($ch) === 61) {
  326. curl_setopt($ch, CURLOPT_ENCODING, 'none');
  327. $contents = @curl_exec($ch);
  328. }
  329. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  330. $fetch_last_content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  331. $fetch_effective_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  332. $fetch_last_error_code = $http_code;
  333. if ($http_code != 200 || $type && strpos($fetch_last_content_type, "$type") === false) {
  334. if (curl_errno($ch) != 0) {
  335. $fetch_last_error .= "; " . curl_errno($ch) . " " . curl_error($ch);
  336. }
  337. $fetch_last_error_content = $contents;
  338. curl_close($ch);
  339. return false;
  340. }
  341. if (!$contents) {
  342. $fetch_last_error = curl_errno($ch) . " " . curl_error($ch);
  343. curl_close($ch);
  344. return false;
  345. }
  346. curl_close($ch);
  347. $is_gzipped = RSSUtils::is_gzipped($contents);
  348. if ($is_gzipped) {
  349. $tmp = @gzdecode($contents);
  350. if ($tmp) $contents = $tmp;
  351. }
  352. return $contents;
  353. } else {
  354. $fetch_curl_used = false;
  355. if ($login && $pass){
  356. $url_parts = array();
  357. preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
  358. $pass = urlencode($pass);
  359. if ($url_parts[1] && $url_parts[2]) {
  360. $url = $url_parts[1] . "://$login:[email protected]" . $url_parts[2];
  361. }
  362. }
  363. // TODO: should this support POST requests or not? idk
  364. $context_options = array(
  365. 'http' => array(
  366. 'header' => array(
  367. 'Connection: close'
  368. ),
  369. 'method' => 'GET',
  370. 'ignore_errors' => true,
  371. 'timeout' => $timeout ? $timeout : FILE_FETCH_TIMEOUT,
  372. 'protocol_version'=> 1.1)
  373. );
  374. if (!$post_query && $last_modified)
  375. array_push($context_options['http']['header'], "If-Modified-Since: $last_modified");
  376. if ($http_accept)
  377. array_push($context_options['http']['header'], "Accept: $http_accept");
  378. if (defined('_HTTP_PROXY')) {
  379. $context_options['http']['request_fulluri'] = true;
  380. $context_options['http']['proxy'] = _HTTP_PROXY;
  381. }
  382. $context = stream_context_create($context_options);
  383. $old_error = error_get_last();
  384. $fetch_effective_url = $url;
  385. $data = @file_get_contents($url, false, $context);
  386. if (isset($http_response_header) && is_array($http_response_header)) {
  387. foreach ($http_response_header as $header) {
  388. if (strstr($header, ": ") !== FALSE) {
  389. list ($key, $value) = explode(": ", $header);
  390. $key = strtolower($key);
  391. if ($key == 'content-type') {
  392. $fetch_last_content_type = $value;
  393. // don't abort here b/c there might be more than one
  394. // e.g. if we were being redirected -- last one is the right one
  395. } else if ($key == 'last-modified') {
  396. $fetch_last_modified = $value;
  397. } else if ($key == 'location') {
  398. $fetch_effective_url = $value;
  399. }
  400. }
  401. if (substr(strtolower($header), 0, 7) == 'http/1.') {
  402. $fetch_last_error_code = (int) substr($header, 9, 3);
  403. $fetch_last_error = $header;
  404. }
  405. }
  406. }
  407. if ($fetch_last_error_code != 200) {
  408. $error = error_get_last();
  409. if ($error['message'] != $old_error['message']) {
  410. $fetch_last_error .= "; " . $error["message"];
  411. }
  412. $fetch_last_error_content = $data;
  413. return false;
  414. }
  415. $is_gzipped = RSSUtils::is_gzipped($data);
  416. if ($is_gzipped) {
  417. $tmp = @gzdecode($data);
  418. if ($tmp) $data = $tmp;
  419. }
  420. return $data;
  421. }
  422. }
  423. /**
  424. * Try to determine the favicon URL for a feed.
  425. * adapted from wordpress favicon plugin by Jeff Minard (http://thecodepro.com/)
  426. * http://dev.wp-plugins.org/file/favatars/trunk/favatars.php
  427. *
  428. * @param string $url A feed or page URL
  429. * @access public
  430. * @return mixed The favicon URL, or false if none was found.
  431. */
  432. function get_favicon_url($url) {
  433. $favicon_url = false;
  434. if ($html = @fetch_file_contents($url)) {
  435. libxml_use_internal_errors(true);
  436. $doc = new DOMDocument();
  437. $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
  438. $xpath = new DOMXPath($doc);
  439. $base = $xpath->query('/html/head/base[@href]');
  440. foreach ($base as $b) {
  441. $url = rewrite_relative_url($url, $b->getAttribute("href"));
  442. break;
  443. }
  444. $entries = $xpath->query('/html/head/link[@rel="shortcut icon" or @rel="icon"]');
  445. if (count($entries) > 0) {
  446. foreach ($entries as $entry) {
  447. $favicon_url = rewrite_relative_url($url, $entry->getAttribute("href"));
  448. break;
  449. }
  450. }
  451. }
  452. if (!$favicon_url)
  453. $favicon_url = rewrite_relative_url($url, "/favicon.ico");
  454. return $favicon_url;
  455. } // function get_favicon_url
  456. function initialize_user_prefs($uid, $profile = false) {
  457. if (get_schema_version() < 63) $profile_qpart = "";
  458. $pdo = DB::pdo();
  459. $in_nested_tr = false;
  460. try {
  461. $pdo->beginTransaction();
  462. } catch (Exception $e) {
  463. $in_nested_tr = true;
  464. }
  465. $sth = $pdo->query("SELECT pref_name,def_value FROM ttrss_prefs");
  466. if (!is_numeric($profile) || !$profile || get_schema_version() < 63) $profile = null;
  467. $u_sth = $pdo->prepare("SELECT pref_name
  468. FROM ttrss_user_prefs WHERE owner_uid = :uid AND
  469. (profile = :profile OR (:profile IS NULL AND profile IS NULL))");
  470. $u_sth->execute([':uid' => $uid, ':profile' => $profile]);
  471. $active_prefs = array();
  472. while ($line = $u_sth->fetch()) {
  473. array_push($active_prefs, $line["pref_name"]);
  474. }
  475. while ($line = $sth->fetch()) {
  476. if (array_search($line["pref_name"], $active_prefs) === FALSE) {
  477. // print "adding " . $line["pref_name"] . "<br>";
  478. if (get_schema_version() < 63) {
  479. $i_sth = $pdo->prepare("INSERT INTO ttrss_user_prefs
  480. (owner_uid,pref_name,value) VALUES
  481. (?, ?, ?)");
  482. $i_sth->execute([$uid, $line["pref_name"], $line["def_value"]]);
  483. } else {
  484. $i_sth = $pdo->prepare("INSERT INTO ttrss_user_prefs
  485. (owner_uid,pref_name,value, profile) VALUES
  486. (?, ?, ?, ?)");
  487. $i_sth->execute([$uid, $line["pref_name"], $line["def_value"], $profile]);
  488. }
  489. }
  490. }
  491. if (!$in_nested_tr) $pdo->commit();
  492. }
  493. function get_ssl_certificate_id() {
  494. if ($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"]) {
  495. return sha1($_SERVER["REDIRECT_SSL_CLIENT_M_SERIAL"] .
  496. $_SERVER["REDIRECT_SSL_CLIENT_V_START"] .
  497. $_SERVER["REDIRECT_SSL_CLIENT_V_END"] .
  498. $_SERVER["REDIRECT_SSL_CLIENT_S_DN"]);
  499. }
  500. if ($_SERVER["SSL_CLIENT_M_SERIAL"]) {
  501. return sha1($_SERVER["SSL_CLIENT_M_SERIAL"] .
  502. $_SERVER["SSL_CLIENT_V_START"] .
  503. $_SERVER["SSL_CLIENT_V_END"] .
  504. $_SERVER["SSL_CLIENT_S_DN"]);
  505. }
  506. return "";
  507. }
  508. function authenticate_user($login, $password, $check_only = false) {
  509. if (!SINGLE_USER_MODE) {
  510. $user_id = false;
  511. $auth_module = false;
  512. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_AUTH_USER) as $plugin) {
  513. $user_id = (int) $plugin->authenticate($login, $password);
  514. if ($user_id) {
  515. $auth_module = strtolower(get_class($plugin));
  516. break;
  517. }
  518. }
  519. if ($user_id && !$check_only) {
  520. session_start();
  521. session_regenerate_id(true);
  522. $_SESSION["uid"] = $user_id;
  523. $_SESSION["version"] = VERSION_STATIC;
  524. $_SESSION["auth_module"] = $auth_module;
  525. $pdo = DB::pdo();
  526. $sth = $pdo->prepare("SELECT login,access_level,pwd_hash FROM ttrss_users
  527. WHERE id = ?");
  528. $sth->execute([$user_id]);
  529. $row = $sth->fetch();
  530. $_SESSION["name"] = $row["login"];
  531. $_SESSION["access_level"] = $row["access_level"];
  532. $_SESSION["csrf_token"] = uniqid_short();
  533. $usth = $pdo->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
  534. $usth->execute([$user_id]);
  535. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  536. $_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']);
  537. $_SESSION["pwd_hash"] = $row["pwd_hash"];
  538. initialize_user_prefs($_SESSION["uid"]);
  539. return true;
  540. }
  541. return false;
  542. } else {
  543. $_SESSION["uid"] = 1;
  544. $_SESSION["name"] = "admin";
  545. $_SESSION["access_level"] = 10;
  546. $_SESSION["hide_hello"] = true;
  547. $_SESSION["hide_logout"] = true;
  548. $_SESSION["auth_module"] = false;
  549. if (!$_SESSION["csrf_token"]) {
  550. $_SESSION["csrf_token"] = uniqid_short();
  551. }
  552. $_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
  553. initialize_user_prefs($_SESSION["uid"]);
  554. return true;
  555. }
  556. }
  557. // this is used for user http parameters unless HTML code is actually needed
  558. function clean($param) {
  559. if (is_array($param)) {
  560. return array_map("strip_tags", $param);
  561. } else if (is_string($param)) {
  562. return strip_tags($param);
  563. } else {
  564. return $param;
  565. }
  566. }
  567. function make_password($length = 12) {
  568. $password = "";
  569. $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ*%+^";
  570. $i = 0;
  571. while ($i < $length) {
  572. try {
  573. $idx = function_exists("random_int") ? random_int(0, strlen($possible) - 1) : mt_rand(0, strlen($possible) - 1);
  574. } catch (Exception $e) {
  575. $idx = mt_rand(0, strlen($possible) - 1);
  576. }
  577. $char = substr($possible, $idx, 1);
  578. if (!strstr($password, $char)) {
  579. $password .= $char;
  580. $i++;
  581. }
  582. }
  583. return $password;
  584. }
  585. // this is called after user is created to initialize default feeds, labels
  586. // or whatever else
  587. // user preferences are checked on every login, not here
  588. function initialize_user($uid) {
  589. $pdo = DB::pdo();
  590. $sth = $pdo->prepare("insert into ttrss_feeds (owner_uid,title,feed_url)
  591. values (?, 'Tiny Tiny RSS: Forum',
  592. 'http://tt-rss.org/forum/rss.php')");
  593. $sth->execute([$uid]);
  594. }
  595. function logout_user() {
  596. @session_destroy();
  597. if (isset($_COOKIE[session_name()])) {
  598. setcookie(session_name(), '', time()-42000, '/');
  599. }
  600. session_commit();
  601. }
  602. function validate_csrf($csrf_token) {
  603. return $csrf_token == $_SESSION['csrf_token'];
  604. }
  605. function load_user_plugins($owner_uid, $pluginhost = false) {
  606. if (!$pluginhost) $pluginhost = PluginHost::getInstance();
  607. if ($owner_uid && SCHEMA_VERSION >= 100) {
  608. $plugins = get_pref("_ENABLED_PLUGINS", $owner_uid);
  609. $pluginhost->load($plugins, PluginHost::KIND_USER, $owner_uid);
  610. if (get_schema_version() > 100) {
  611. $pluginhost->load_data();
  612. }
  613. }
  614. }
  615. function login_sequence() {
  616. $pdo = Db::pdo();
  617. if (SINGLE_USER_MODE) {
  618. @session_start();
  619. authenticate_user("admin", null);
  620. startup_gettext();
  621. load_user_plugins($_SESSION["uid"]);
  622. } else {
  623. if (!validate_session()) $_SESSION["uid"] = false;
  624. if (!$_SESSION["uid"]) {
  625. if (AUTH_AUTO_LOGIN && authenticate_user(null, null)) {
  626. $_SESSION["ref_schema_version"] = get_schema_version(true);
  627. } else {
  628. authenticate_user(null, null, true);
  629. }
  630. if (!$_SESSION["uid"]) {
  631. logout_user();
  632. render_login_form();
  633. exit;
  634. }
  635. } else {
  636. /* bump login timestamp */
  637. $sth = $pdo->prepare("UPDATE ttrss_users SET last_login = NOW() WHERE id = ?");
  638. $sth->execute([$_SESSION['uid']]);
  639. $_SESSION["last_login_update"] = time();
  640. }
  641. if ($_SESSION["uid"]) {
  642. startup_gettext();
  643. load_user_plugins($_SESSION["uid"]);
  644. /* cleanup ccache */
  645. $sth = $pdo->prepare("DELETE FROM ttrss_counters_cache WHERE owner_uid = ?
  646. AND
  647. (SELECT COUNT(id) FROM ttrss_feeds WHERE
  648. ttrss_feeds.id = feed_id) = 0");
  649. $sth->execute([$_SESSION['uid']]);
  650. $sth = $pdo->prepare("DELETE FROM ttrss_cat_counters_cache WHERE owner_uid = ?
  651. AND
  652. (SELECT COUNT(id) FROM ttrss_feed_categories WHERE
  653. ttrss_feed_categories.id = feed_id) = 0");
  654. $sth->execute([$_SESSION['uid']]);
  655. }
  656. }
  657. }
  658. function truncate_string($str, $max_len, $suffix = '&hellip;') {
  659. if (mb_strlen($str, "utf-8") > $max_len) {
  660. return mb_substr($str, 0, $max_len, "utf-8") . $suffix;
  661. } else {
  662. return $str;
  663. }
  664. }
  665. function mb_substr_replace($original, $replacement, $position, $length) {
  666. $startString = mb_substr($original, 0, $position, "UTF-8");
  667. $endString = mb_substr($original, $position + $length, mb_strlen($original), "UTF-8");
  668. $out = $startString . $replacement . $endString;
  669. return $out;
  670. }
  671. function truncate_middle($str, $max_len, $suffix = '&hellip;') {
  672. if (mb_strlen($str) > $max_len) {
  673. return mb_substr_replace($str, $suffix, $max_len / 2, mb_strlen($str) - $max_len);
  674. } else {
  675. return $str;
  676. }
  677. }
  678. function convert_timestamp($timestamp, $source_tz, $dest_tz) {
  679. try {
  680. $source_tz = new DateTimeZone($source_tz);
  681. } catch (Exception $e) {
  682. $source_tz = new DateTimeZone('UTC');
  683. }
  684. try {
  685. $dest_tz = new DateTimeZone($dest_tz);
  686. } catch (Exception $e) {
  687. $dest_tz = new DateTimeZone('UTC');
  688. }
  689. $dt = new DateTime(date('Y-m-d H:i:s', $timestamp), $source_tz);
  690. return $dt->format('U') + $dest_tz->getOffset($dt);
  691. }
  692. function make_local_datetime($timestamp, $long, $owner_uid = false,
  693. $no_smart_dt = false, $eta_min = false) {
  694. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  695. if (!$timestamp) $timestamp = '1970-01-01 0:00';
  696. global $utc_tz;
  697. global $user_tz;
  698. if (!$utc_tz) $utc_tz = new DateTimeZone('UTC');
  699. $timestamp = substr($timestamp, 0, 19);
  700. # We store date in UTC internally
  701. $dt = new DateTime($timestamp, $utc_tz);
  702. $user_tz_string = get_pref('USER_TIMEZONE', $owner_uid);
  703. if ($user_tz_string != 'Automatic') {
  704. try {
  705. if (!$user_tz) $user_tz = new DateTimeZone($user_tz_string);
  706. } catch (Exception $e) {
  707. $user_tz = $utc_tz;
  708. }
  709. $tz_offset = $user_tz->getOffset($dt);
  710. } else {
  711. $tz_offset = (int) -$_SESSION["clientTzOffset"];
  712. }
  713. $user_timestamp = $dt->format('U') + $tz_offset;
  714. if (!$no_smart_dt) {
  715. return smart_date_time($user_timestamp,
  716. $tz_offset, $owner_uid, $eta_min);
  717. } else {
  718. if ($long)
  719. $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
  720. else
  721. $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
  722. return date($format, $user_timestamp);
  723. }
  724. }
  725. function smart_date_time($timestamp, $tz_offset = 0, $owner_uid = false, $eta_min = false) {
  726. if (!$owner_uid) $owner_uid = $_SESSION['uid'];
  727. if ($eta_min && time() + $tz_offset - $timestamp < 3600) {
  728. return T_sprintf("%d min", date("i", time() + $tz_offset - $timestamp));
  729. } else if (date("Y.m.d", $timestamp) == date("Y.m.d", time() + $tz_offset)) {
  730. $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
  731. if (strpos((strtolower($format)), "a") === false)
  732. return date("G:i", $timestamp);
  733. else
  734. return date("g:i a", $timestamp);
  735. } else if (date("Y", $timestamp) == date("Y", time() + $tz_offset)) {
  736. $format = get_pref('SHORT_DATE_FORMAT', $owner_uid);
  737. return date($format, $timestamp);
  738. } else {
  739. $format = get_pref('LONG_DATE_FORMAT', $owner_uid);
  740. return date($format, $timestamp);
  741. }
  742. }
  743. function sql_bool_to_bool($s) {
  744. return $s && ($s !== "f" && $s !== "false"); //no-op for PDO, backwards compat for legacy layer
  745. }
  746. function bool_to_sql_bool($s) {
  747. return $s ? 1 : 0;
  748. }
  749. // Session caching removed due to causing wrong redirects to upgrade
  750. // script when get_schema_version() is called on an obsolete session
  751. // created on a previous schema version.
  752. function get_schema_version($nocache = false) {
  753. global $schema_version;
  754. $pdo = DB::pdo();
  755. if (!$schema_version && !$nocache) {
  756. $row = $pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
  757. $version = $row["schema_version"];
  758. $schema_version = $version;
  759. return $version;
  760. } else {
  761. return $schema_version;
  762. }
  763. }
  764. function sanity_check() {
  765. require_once 'errors.php';
  766. global $ERRORS;
  767. $error_code = 0;
  768. $schema_version = get_schema_version(true);
  769. if ($schema_version != SCHEMA_VERSION) {
  770. $error_code = 5;
  771. }
  772. return array("code" => $error_code, "message" => $ERRORS[$error_code]);
  773. }
  774. function file_is_locked($filename) {
  775. if (file_exists(LOCK_DIRECTORY . "/$filename")) {
  776. if (function_exists('flock')) {
  777. $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r");
  778. if ($fp) {
  779. if (flock($fp, LOCK_EX | LOCK_NB)) {
  780. flock($fp, LOCK_UN);
  781. fclose($fp);
  782. return false;
  783. }
  784. fclose($fp);
  785. return true;
  786. } else {
  787. return false;
  788. }
  789. }
  790. return true; // consider the file always locked and skip the test
  791. } else {
  792. return false;
  793. }
  794. }
  795. function make_lockfile($filename) {
  796. $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
  797. if ($fp && flock($fp, LOCK_EX | LOCK_NB)) {
  798. $stat_h = fstat($fp);
  799. $stat_f = stat(LOCK_DIRECTORY . "/$filename");
  800. if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  801. if ($stat_h["ino"] != $stat_f["ino"] ||
  802. $stat_h["dev"] != $stat_f["dev"]) {
  803. return false;
  804. }
  805. }
  806. if (function_exists('posix_getpid')) {
  807. fwrite($fp, posix_getpid() . "\n");
  808. }
  809. return $fp;
  810. } else {
  811. return false;
  812. }
  813. }
  814. function make_stampfile($filename) {
  815. $fp = fopen(LOCK_DIRECTORY . "/$filename", "w");
  816. if (flock($fp, LOCK_EX | LOCK_NB)) {
  817. fwrite($fp, time() . "\n");
  818. flock($fp, LOCK_UN);
  819. fclose($fp);
  820. return true;
  821. } else {
  822. return false;
  823. }
  824. }
  825. function sql_random_function() {
  826. if (DB_TYPE == "mysql") {
  827. return "RAND()";
  828. } else {
  829. return "RANDOM()";
  830. }
  831. }
  832. function getFeedUnread($feed, $is_cat = false) {
  833. return Feeds::getFeedArticles($feed, $is_cat, true, $_SESSION["uid"]);
  834. }
  835. function checkbox_to_sql_bool($val) {
  836. return ($val == "on") ? 1 : 0;
  837. }
  838. function uniqid_short() {
  839. return uniqid(base_convert(rand(), 10, 36));
  840. }
  841. function make_init_params() {
  842. $params = array();
  843. foreach (array("ON_CATCHUP_SHOW_NEXT_FEED", "HIDE_READ_FEEDS",
  844. "ENABLE_FEED_CATS", "FEEDS_SORT_BY_UNREAD", "CONFIRM_FEED_CATCHUP",
  845. "CDM_AUTO_CATCHUP", "FRESH_ARTICLE_MAX_AGE",
  846. "HIDE_READ_SHOWS_SPECIAL", "COMBINED_DISPLAY_MODE") as $param) {
  847. $params[strtolower($param)] = (int) get_pref($param);
  848. }
  849. $params["check_for_updates"] = CHECK_FOR_UPDATES;
  850. $params["icons_url"] = ICONS_URL;
  851. $params["cookie_lifetime"] = SESSION_COOKIE_LIFETIME;
  852. $params["default_view_mode"] = get_pref("_DEFAULT_VIEW_MODE");
  853. $params["default_view_limit"] = (int) get_pref("_DEFAULT_VIEW_LIMIT");
  854. $params["default_view_order_by"] = get_pref("_DEFAULT_VIEW_ORDER_BY");
  855. $params["bw_limit"] = (int) $_SESSION["bw_limit"];
  856. $params["is_default_pw"] = Pref_Prefs::isdefaultpassword();
  857. $params["label_base_index"] = (int) LABEL_BASE_INDEX;
  858. $theme = get_pref( "USER_CSS_THEME", false, false);
  859. $params["theme"] = theme_exists($theme) ? $theme : "";
  860. $params["plugins"] = implode(", ", PluginHost::getInstance()->get_plugin_names());
  861. $params["php_platform"] = PHP_OS;
  862. $params["php_version"] = PHP_VERSION;
  863. $params["sanity_checksum"] = sha1(file_get_contents("include/sanity_check.php"));
  864. $pdo = Db::pdo();
  865. $sth = $pdo->prepare("SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
  866. ttrss_feeds WHERE owner_uid = ?");
  867. $sth->execute([$_SESSION['uid']]);
  868. $row = $sth->fetch();
  869. $max_feed_id = $row["mid"];
  870. $num_feeds = $row["nf"];
  871. $params["max_feed_id"] = (int) $max_feed_id;
  872. $params["num_feeds"] = (int) $num_feeds;
  873. $params["hotkeys"] = get_hotkeys_map();
  874. $params["csrf_token"] = $_SESSION["csrf_token"];
  875. $params["widescreen"] = (int) $_COOKIE["ttrss_widescreen"];
  876. $params['simple_update'] = defined('SIMPLE_UPDATE_MODE') && SIMPLE_UPDATE_MODE;
  877. $params["icon_indicator_white"] = base64_img("images/indicator_white.gif");
  878. $params["labels"] = Labels::get_all_labels($_SESSION["uid"]);
  879. return $params;
  880. }
  881. function get_hotkeys_info() {
  882. $hotkeys = array(
  883. __("Navigation") => array(
  884. "next_feed" => __("Open next feed"),
  885. "prev_feed" => __("Open previous feed"),
  886. "next_article" => __("Open next article"),
  887. "prev_article" => __("Open previous article"),
  888. "next_article_noscroll" => __("Open next article (don't scroll long articles)"),
  889. "prev_article_noscroll" => __("Open previous article (don't scroll long articles)"),
  890. "next_article_noexpand" => __("Move to next article (don't expand or mark read)"),
  891. "prev_article_noexpand" => __("Move to previous article (don't expand or mark read)"),
  892. "search_dialog" => __("Show search dialog")),
  893. __("Article") => array(
  894. "toggle_mark" => __("Toggle starred"),
  895. "toggle_publ" => __("Toggle published"),
  896. "toggle_unread" => __("Toggle unread"),
  897. "edit_tags" => __("Edit tags"),
  898. "open_in_new_window" => __("Open in new window"),
  899. "catchup_below" => __("Mark below as read"),
  900. "catchup_above" => __("Mark above as read"),
  901. "article_scroll_down" => __("Scroll down"),
  902. "article_scroll_up" => __("Scroll up"),
  903. "select_article_cursor" => __("Select article under cursor"),
  904. "email_article" => __("Email article"),
  905. "close_article" => __("Close/collapse article"),
  906. "toggle_expand" => __("Toggle article expansion (combined mode)"),
  907. "toggle_widescreen" => __("Toggle widescreen mode"),
  908. "toggle_embed_original" => __("Toggle embed original")),
  909. __("Article selection") => array(
  910. "select_all" => __("Select all articles"),
  911. "select_unread" => __("Select unread"),
  912. "select_marked" => __("Select starred"),
  913. "select_published" => __("Select published"),
  914. "select_invert" => __("Invert selection"),
  915. "select_none" => __("Deselect everything")),
  916. __("Feed") => array(
  917. "feed_refresh" => __("Refresh current feed"),
  918. "feed_unhide_read" => __("Un/hide read feeds"),
  919. "feed_subscribe" => __("Subscribe to feed"),
  920. "feed_edit" => __("Edit feed"),
  921. "feed_catchup" => __("Mark as read"),
  922. "feed_reverse" => __("Reverse headlines"),
  923. "feed_toggle_vgroup" => __("Toggle headline grouping"),
  924. "feed_debug_update" => __("Debug feed update"),
  925. "feed_debug_viewfeed" => __("Debug viewfeed()"),
  926. "catchup_all" => __("Mark all feeds as read"),
  927. "cat_toggle_collapse" => __("Un/collapse current category"),
  928. "toggle_cdm_expanded" => __("Toggle auto expand in combined mode"),
  929. "toggle_combined_mode" => __("Toggle combined mode")),
  930. __("Go to") => array(
  931. "goto_all" => __("All articles"),
  932. "goto_fresh" => __("Fresh"),
  933. "goto_marked" => __("Starred"),
  934. "goto_published" => __("Published"),
  935. "goto_read" => __("Recently read"),
  936. "goto_tagcloud" => __("Tag cloud"),
  937. "goto_prefs" => __("Preferences")),
  938. __("Other") => array(
  939. "create_label" => __("Create label"),
  940. "create_filter" => __("Create filter"),
  941. "collapse_sidebar" => __("Un/collapse sidebar"),
  942. "toggle_night_mode" => __("Toggle night mode"),
  943. "help_dialog" => __("Show help dialog"))
  944. );
  945. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HOTKEY_INFO) as $plugin) {
  946. $hotkeys = $plugin->hook_hotkey_info($hotkeys);
  947. }
  948. return $hotkeys;
  949. }
  950. function get_hotkeys_map() {
  951. $hotkeys = array(
  952. "k" => "next_feed",
  953. "j" => "prev_feed",
  954. "n" => "next_article",
  955. "p" => "prev_article",
  956. "(38)|Up" => "prev_article",
  957. "(40)|Down" => "next_article",
  958. "*(38)|Shift+Up" => "article_scroll_up",
  959. "*(40)|Shift+Down" => "article_scroll_down",
  960. "^(38)|Ctrl+Up" => "prev_article_noscroll",
  961. "^(40)|Ctrl+Down" => "next_article_noscroll",
  962. "/" => "search_dialog",
  963. "s" => "toggle_mark",
  964. "S" => "toggle_publ",
  965. "u" => "toggle_unread",
  966. "T" => "edit_tags",
  967. "o" => "open_in_new_window",
  968. "c p" => "catchup_below",
  969. "c n" => "catchup_above",
  970. "N" => "article_scroll_down",
  971. "P" => "article_scroll_up",
  972. "a W" => "toggle_widescreen",
  973. "a e" => "toggle_embed_original",
  974. "e" => "email_article",
  975. "a q" => "close_article",
  976. "a a" => "select_all",
  977. "a u" => "select_unread",
  978. "a U" => "select_marked",
  979. "a p" => "select_published",
  980. "a i" => "select_invert",
  981. "a n" => "select_none",
  982. "f r" => "feed_refresh",
  983. "f a" => "feed_unhide_read",
  984. "f s" => "feed_subscribe",
  985. "f e" => "feed_edit",
  986. "f q" => "feed_catchup",
  987. "f x" => "feed_reverse",
  988. "f g" => "feed_toggle_vgroup",
  989. "f D" => "feed_debug_update",
  990. "f G" => "feed_debug_viewfeed",
  991. "f C" => "toggle_combined_mode",
  992. "f c" => "toggle_cdm_expanded",
  993. "Q" => "catchup_all",
  994. "x" => "cat_toggle_collapse",
  995. "g a" => "goto_all",
  996. "g f" => "goto_fresh",
  997. "g s" => "goto_marked",
  998. "g p" => "goto_published",
  999. "g r" => "goto_read",
  1000. "g t" => "goto_tagcloud",
  1001. "g P" => "goto_prefs",
  1002. "r" => "select_article_cursor",
  1003. "c l" => "create_label",
  1004. "c f" => "create_filter",
  1005. "c s" => "collapse_sidebar",
  1006. "a N" => "toggle_night_mode",
  1007. "?" => "help_dialog",
  1008. );
  1009. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_HOTKEY_MAP) as $plugin) {
  1010. $hotkeys = $plugin->hook_hotkey_map($hotkeys);
  1011. }
  1012. $prefixes = array();
  1013. foreach (array_keys($hotkeys) as $hotkey) {
  1014. $pair = explode(" ", $hotkey, 2);
  1015. if (count($pair) > 1 && !in_array($pair[0], $prefixes)) {
  1016. array_push($prefixes, $pair[0]);
  1017. }
  1018. }
  1019. return array($prefixes, $hotkeys);
  1020. }
  1021. function make_runtime_info() {
  1022. $data = array();
  1023. $pdo = Db::pdo();
  1024. $sth = $pdo->prepare("SELECT MAX(id) AS mid, COUNT(*) AS nf FROM
  1025. ttrss_feeds WHERE owner_uid = ?");
  1026. $sth->execute([$_SESSION['uid']]);
  1027. $row = $sth->fetch();
  1028. $max_feed_id = $row['mid'];
  1029. $num_feeds = $row['nf'];
  1030. $data["max_feed_id"] = (int) $max_feed_id;
  1031. $data["num_feeds"] = (int) $num_feeds;
  1032. $data['cdm_expanded'] = get_pref('CDM_EXPANDED');
  1033. $data["labels"] = Labels::get_all_labels($_SESSION["uid"]);
  1034. if (LOG_DESTINATION == 'sql' && $_SESSION['access_level'] >= 10) {
  1035. if (DB_TYPE == 'pgsql') {
  1036. $log_interval = "created_at > NOW() - interval '1 hour'";
  1037. } else {
  1038. $log_interval = "created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)";
  1039. }
  1040. $sth = $pdo->prepare("SELECT COUNT(id) AS cid FROM ttrss_error_log WHERE $log_interval");
  1041. $sth->execute();
  1042. if ($row = $sth->fetch()) {
  1043. $data['recent_log_events'] = $row['cid'];
  1044. }
  1045. }
  1046. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  1047. $data['daemon_is_running'] = (int) file_is_locked("update_daemon.lock");
  1048. if (time() - $_SESSION["daemon_stamp_check"] > 30) {
  1049. $stamp = (int) @file_get_contents(LOCK_DIRECTORY . "/update_daemon.stamp");
  1050. if ($stamp) {
  1051. $stamp_delta = time() - $stamp;
  1052. if ($stamp_delta > 1800) {
  1053. $stamp_check = 0;
  1054. } else {
  1055. $stamp_check = 1;
  1056. $_SESSION["daemon_stamp_check"] = time();
  1057. }
  1058. $data['daemon_stamp_ok'] = $stamp_check;
  1059. $stamp_fmt = date("Y.m.d, G:i", $stamp);
  1060. $data['daemon_stamp'] = $stamp_fmt;
  1061. }
  1062. }
  1063. }
  1064. return $data;
  1065. }
  1066. function search_to_sql($search, $search_language) {
  1067. $keywords = str_getcsv(trim($search), " ");
  1068. $query_keywords = array();
  1069. $search_words = array();
  1070. $search_query_leftover = array();
  1071. $pdo = Db::pdo();
  1072. if ($search_language)
  1073. $search_language = $pdo->quote(mb_strtolower($search_language));
  1074. else
  1075. $search_language = $pdo->quote("english");
  1076. foreach ($keywords as $k) {
  1077. if (strpos($k, "-") === 0) {
  1078. $k = substr($k, 1);
  1079. $not = "NOT";
  1080. } else {
  1081. $not = "";
  1082. }
  1083. $commandpair = explode(":", mb_strtolower($k), 2);
  1084. switch ($commandpair[0]) {
  1085. case "title":
  1086. if ($commandpair[1]) {
  1087. array_push($query_keywords, "($not (LOWER(ttrss_entries.title) LIKE ".
  1088. $pdo->quote('%' . mb_strtolower($commandpair[1]) . '%') ."))");
  1089. } else {
  1090. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
  1091. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1092. array_push($search_words, $k);
  1093. }
  1094. break;
  1095. case "author":
  1096. if ($commandpair[1]) {
  1097. array_push($query_keywords, "($not (LOWER(author) LIKE ".
  1098. $pdo->quote('%' . mb_strtolower($commandpair[1]) . '%')."))");
  1099. } else {
  1100. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
  1101. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1102. array_push($search_words, $k);
  1103. }
  1104. break;
  1105. case "note":
  1106. if ($commandpair[1]) {
  1107. if ($commandpair[1] == "true")
  1108. array_push($query_keywords, "($not (note IS NOT NULL AND note != ''))");
  1109. else if ($commandpair[1] == "false")
  1110. array_push($query_keywords, "($not (note IS NULL OR note = ''))");
  1111. else
  1112. array_push($query_keywords, "($not (LOWER(note) LIKE ".
  1113. $pdo->quote('%' . mb_strtolower($commandpair[1]) . '%')."))");
  1114. } else {
  1115. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
  1116. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1117. if (!$not) array_push($search_words, $k);
  1118. }
  1119. break;
  1120. case "star":
  1121. if ($commandpair[1]) {
  1122. if ($commandpair[1] == "true")
  1123. array_push($query_keywords, "($not (marked = true))");
  1124. else
  1125. array_push($query_keywords, "($not (marked = false))");
  1126. } else {
  1127. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
  1128. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1129. if (!$not) array_push($search_words, $k);
  1130. }
  1131. break;
  1132. case "pub":
  1133. if ($commandpair[1]) {
  1134. if ($commandpair[1] == "true")
  1135. array_push($query_keywords, "($not (published = true))");
  1136. else
  1137. array_push($query_keywords, "($not (published = false))");
  1138. } else {
  1139. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
  1140. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1141. if (!$not) array_push($search_words, $k);
  1142. }
  1143. break;
  1144. case "unread":
  1145. if ($commandpair[1]) {
  1146. if ($commandpair[1] == "true")
  1147. array_push($query_keywords, "($not (unread = true))");
  1148. else
  1149. array_push($query_keywords, "($not (unread = false))");
  1150. } else {
  1151. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
  1152. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1153. if (!$not) array_push($search_words, $k);
  1154. }
  1155. break;
  1156. default:
  1157. if (strpos($k, "@") === 0) {
  1158. $user_tz_string = get_pref('USER_TIMEZONE', $_SESSION['uid']);
  1159. $orig_ts = strtotime(substr($k, 1));
  1160. $k = date("Y-m-d", convert_timestamp($orig_ts, $user_tz_string, 'UTC'));
  1161. //$k = date("Y-m-d", strtotime(substr($k, 1)));
  1162. array_push($query_keywords, "(".SUBSTRING_FOR_DATE."(updated,1,LENGTH('$k')) $not = '$k')");
  1163. } else {
  1164. if (DB_TYPE == "pgsql") {
  1165. $k = mb_strtolower($k);
  1166. array_push($search_query_leftover, $not ? "!$k" : $k);
  1167. } else {
  1168. array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER(".$pdo->quote("%$k%").")
  1169. OR UPPER(ttrss_entries.content) $not LIKE UPPER(".$pdo->quote("%$k%")."))");
  1170. }
  1171. if (!$not) array_push($search_words, $k);
  1172. }
  1173. }
  1174. }
  1175. if (count($search_query_leftover) > 0) {
  1176. if (DB_TYPE == "pgsql") {
  1177. // if there's no joiners consider this a "simple" search and
  1178. // concatenate everything with &, otherwise don't try to mess with tsquery syntax
  1179. if (preg_match("/[&|]/", implode(" " , $search_query_leftover))) {
  1180. $tsquery = $pdo->quote(implode(" ", $search_query_leftover));
  1181. } else {
  1182. $tsquery = $pdo->quote(implode(" & ", $search_query_leftover));
  1183. }
  1184. array_push($query_keywords,
  1185. "(tsvector_combined @@ to_tsquery($search_language, $tsquery))");
  1186. }
  1187. }
  1188. $search_query_part = implode("AND", $query_keywords);
  1189. return array($search_query_part, $search_words);
  1190. }
  1191. function iframe_whitelisted($entry) {
  1192. $whitelist = array("youtube.com", "youtu.be", "vimeo.com", "player.vimeo.com");
  1193. @$src = parse_url($entry->getAttribute("src"), PHP_URL_HOST);
  1194. if ($src) {
  1195. foreach ($whitelist as $w) {
  1196. if ($src == $w || $src == "www.$w")
  1197. return true;
  1198. }
  1199. }
  1200. return false;
  1201. }
  1202. // check for locally cached (media) URLs and rewrite to local versions
  1203. // this is called separately after sanitize() and plugin render article hooks to allow
  1204. // plugins work on original source URLs used before caching
  1205. function rewrite_cached_urls($str) {
  1206. $res = trim($str); if (!$res) return '';
  1207. $doc = new DOMDocument();
  1208. $doc->loadHTML('<?xml encoding="UTF-8">' . $res);
  1209. $xpath = new DOMXPath($doc);
  1210. $entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video/source[@src]|//audio/source[@src])');
  1211. $need_saving = false;
  1212. foreach ($entries as $entry) {
  1213. if ($entry->hasAttribute('src') || $entry->hasAttribute('poster')) {
  1214. // should be already absolutized because this is called after sanitize()
  1215. $src = $entry->hasAttribute('poster') ? $entry->getAttribute('poster') : $entry->getAttribute('src');
  1216. $cached_filename = CACHE_DIR . '/images/' . sha1($src);
  1217. if (file_exists($cached_filename)) {
  1218. // this is strictly cosmetic
  1219. if ($entry->tagName == 'img') {
  1220. $suffix = ".png";
  1221. } else if ($entry->parentNode && $entry->parentNode->tagName == "picture") {
  1222. $suffix = ".png";
  1223. } else if ($entry->parentNode && $entry->parentNode->tagName == "video") {
  1224. $suffix = ".mp4";
  1225. } else if ($entry->parentNode && $entry->parentNode->tagName == "audio") {
  1226. $suffix = ".ogg";
  1227. } else {
  1228. $suffix = "";
  1229. }
  1230. $src = get_self_url_prefix() . '/public.php?op=cached_url&hash=' . sha1($src) . $suffix;
  1231. if ($entry->hasAttribute('poster'))
  1232. $entry->setAttribute('poster', $src);
  1233. else
  1234. $entry->setAttribute('src', $src);
  1235. $need_saving = true;
  1236. }
  1237. }
  1238. }
  1239. if ($need_saving) {
  1240. $doc->removeChild($doc->firstChild); //remove doctype
  1241. $res = $doc->saveHTML();
  1242. }
  1243. return $res;
  1244. }
  1245. function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false, $article_id = false) {
  1246. if (!$owner) $owner = $_SESSION["uid"];
  1247. $res = trim($str); if (!$res) return '';
  1248. libxml_use_internal_errors(true);
  1249. $doc = new DOMDocument();
  1250. $doc->loadHTML('<?xml encoding="UTF-8">' . $res);
  1251. $xpath = new DOMXPath($doc);
  1252. $rewrite_base_url = $site_url ? $site_url : get_self_url_prefix();
  1253. $entries = $xpath->query('(//a[@href]|//img[@src]|//video/source[@src]|//audio/source[@src]|//picture/source[@src])');
  1254. foreach ($entries as $entry) {
  1255. if ($entry->hasAttribute('href')) {
  1256. $entry->setAttribute('href',
  1257. rewrite_relative_url($rewrite_base_url, $entry->getAttribute('href')));
  1258. $entry->setAttribute('rel', 'noopener noreferrer');
  1259. }
  1260. if ($entry->hasAttribute('src')) {
  1261. $src = rewrite_relative_url($rewrite_base_url, $entry->getAttribute('src'));
  1262. // cache stuff has gone to rewrite_cached_urls()
  1263. $entry->setAttribute('src', $src);
  1264. }
  1265. if ($entry->nodeName == 'img') {
  1266. $entry->setAttribute('referrerpolicy', 'no-referrer');
  1267. $entry->removeAttribute('width');
  1268. $entry->removeAttribute('height');
  1269. if ($entry->hasAttribute('src')) {
  1270. $is_https_url = parse_url($entry->getAttribute('src'), PHP_URL_SCHEME) === 'https';
  1271. if (is_prefix_https() && !$is_https_url) {
  1272. if ($entry->hasAttribute('srcset')) {
  1273. $entry->removeAttribute('srcset');
  1274. }
  1275. if ($entry->hasAttribute('sizes')) {
  1276. $entry->removeAttribute('sizes');
  1277. }
  1278. }
  1279. }
  1280. }
  1281. if ($entry->hasAttribute('src') &&
  1282. ($owner && get_pref("STRIP_IMAGES", $owner)) || $force_remove_images || $_SESSION["bw_limit"]) {
  1283. $p = $doc->createElement('p');
  1284. $a = $doc->createElement('a');
  1285. $a->setAttribute('href', $entry->getAttribute('src'));
  1286. $a->appendChild(new DOMText($entry->getAttribute('src')));
  1287. $a->setAttribute('target', '_blank');
  1288. $a->setAttribute('rel', 'noopener noreferrer');
  1289. $p->appendChild($a);
  1290. if ($entry->nodeName == 'source') {
  1291. if ($entry->parentNode && $entry->parentNode->parentNode)
  1292. $entry->parentNode->parentNode->replaceChild($p, $entry->parentNode);
  1293. } else if ($entry->nodeName == 'img') {
  1294. if ($entry->parentNode)
  1295. $entry->parentNode->replaceChild($p, $entry);
  1296. }
  1297. }
  1298. if (strtolower($entry->nodeName) == "a") {
  1299. $entry->setAttribute("target", "_blank");
  1300. $entry->setAttribute("rel", "noopener noreferrer");
  1301. }
  1302. }
  1303. $entries = $xpath->query('//iframe');
  1304. foreach ($entries as $entry) {
  1305. if (!iframe_whitelisted($entry)) {
  1306. $entry->setAttribute('sandbox', 'allow-scripts');
  1307. } else {
  1308. if (is_prefix_https()) {
  1309. $entry->setAttribute("src",
  1310. str_replace("http://", "https://",
  1311. $entry->getAttribute("src")));
  1312. }
  1313. }
  1314. }
  1315. $allowed_elements = array('a', 'abbr', 'address', 'acronym', 'audio', 'article', 'aside',
  1316. 'b', 'bdi', 'bdo', 'big', 'blockquote', 'body', 'br',
  1317. 'caption', 'cite', 'center', 'code', 'col', 'colgroup',
  1318. 'data', 'dd', 'del', 'details', 'description', 'dfn', 'div', 'dl', 'font',
  1319. 'dt', 'em', 'footer', 'figure', 'figcaption',
  1320. 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'html', 'i',
  1321. 'img', 'ins', 'kbd', 'li', 'main', 'mark', 'nav', 'noscript',
  1322. 'ol', 'p', 'picture', 'pre', 'q', 'ruby', 'rp', 'rt', 's', 'samp', 'section',
  1323. 'small', 'source', 'span', 'strike', 'strong', 'sub', 'summary',
  1324. 'sup', 'table', 'tbody', 'td', 'tfoot', 'th', 'thead', 'time',
  1325. 'tr', 'track', 'tt', 'u', 'ul', 'var', 'wbr', 'video', 'xml:namespace' );
  1326. if ($_SESSION['hasSandbox']) $allowed_elements[] = 'iframe';
  1327. $disallowed_attributes = array('id', 'style', 'class');
  1328. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SANITIZE) as $plugin) {
  1329. $retval = $plugin->hook_sanitize($doc, $site_url, $allowed_elements, $disallowed_attributes, $article_id);
  1330. if (is_array($retval)) {
  1331. $doc = $retval[0];
  1332. $allowed_elements = $retval[1];
  1333. $disallowed_attributes = $retval[2];
  1334. } else {
  1335. $doc = $retval;
  1336. }
  1337. }
  1338. $doc->removeChild($doc->firstChild); //remove doctype
  1339. $doc = strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes);
  1340. if ($highlight_words) {
  1341. foreach ($highlight_words as $word) {
  1342. // http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph
  1343. $elements = $xpath->query("//*/text()");
  1344. foreach ($elements as $child) {
  1345. $fragment = $doc->createDocumentFragment();
  1346. $text = $child->textContent;
  1347. while (($pos = mb_stripos($text, $word)) !== false) {
  1348. $fragment->appendChild(new DomText(mb_substr($text, 0, $pos)));
  1349. $word = mb_substr($text, $pos, mb_strlen($word));
  1350. $highlight = $doc->createElement('span');
  1351. $highlight->appendChild(new DomText($word));
  1352. $highlight->setAttribute('class', 'highlight');
  1353. $fragment->appendChild($highlight);
  1354. $text = mb_substr($text, $pos + mb_strlen($word));
  1355. }
  1356. if (!empty($text)) $fragment->appendChild(new DomText($text));
  1357. $child->parentNode->replaceChild($fragment, $child);
  1358. }
  1359. }
  1360. }
  1361. $res = $doc->saveHTML();
  1362. /* strip everything outside of <body>...</body> */
  1363. $res_frag = array();
  1364. if (preg_match('/<body>(.*)<\/body>/is', $res, $res_frag)) {
  1365. return $res_frag[1];
  1366. } else {
  1367. return $res;
  1368. }
  1369. }
  1370. function strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes) {
  1371. $xpath = new DOMXPath($doc);
  1372. $entries = $xpath->query('//*');
  1373. foreach ($entries as $entry) {
  1374. if (!in_array($entry->nodeName, $allowed_elements)) {
  1375. $entry->parentNode->removeChild($entry);
  1376. }
  1377. if ($entry->hasAttributes()) {
  1378. $attrs_to_remove = array();
  1379. foreach ($entry->attributes as $attr) {
  1380. if (strpos($attr->nodeName, 'on') === 0) {
  1381. array_push($attrs_to_remove, $attr);
  1382. }
  1383. if (strpos($attr->nodeName, "data-") === 0) {
  1384. array_push($attrs_to_remove, $attr);
  1385. }
  1386. if ($attr->nodeName == 'href' && stripos($attr->value, 'javascript:') === 0) {
  1387. array_push($attrs_to_remove, $attr);
  1388. }
  1389. if (in_array($attr->nodeName, $disallowed_attributes)) {
  1390. array_push($attrs_to_remove, $attr);
  1391. }
  1392. }
  1393. foreach ($attrs_to_remove as $attr) {
  1394. $entry->removeAttributeNode($attr);
  1395. }
  1396. }
  1397. }
  1398. return $doc;
  1399. }
  1400. function trim_array($array) {
  1401. $tmp = $array;
  1402. array_walk($tmp, 'trim');
  1403. return $tmp;
  1404. }
  1405. function render_login_form() {
  1406. header('Cache-Control: public');
  1407. require_once "login_form.php";
  1408. exit;
  1409. }
  1410. function T_sprintf() {
  1411. $args = func_get_args();
  1412. return vsprintf(__(array_shift($args)), $args);
  1413. }
  1414. function print_checkpoint($n, $s) {
  1415. $ts = microtime(true);
  1416. echo sprintf("<!-- CP[$n] %.4f seconds -->\n", $ts - $s);
  1417. return $ts;
  1418. }
  1419. function is_server_https() {
  1420. return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https';
  1421. }
  1422. function is_prefix_https() {
  1423. return parse_url(SELF_URL_PATH, PHP_URL_SCHEME) == 'https';
  1424. }
  1425. // this returns SELF_URL_PATH sans ending slash
  1426. function get_self_url_prefix() {
  1427. if (strrpos(SELF_URL_PATH, "/") === strlen(SELF_URL_PATH)-1) {
  1428. return substr(SELF_URL_PATH, 0, strlen(SELF_URL_PATH)-1);
  1429. } else {
  1430. return SELF_URL_PATH;
  1431. }
  1432. }
  1433. function encrypt_password($pass, $salt = '', $mode2 = false) {
  1434. if ($salt && $mode2) {
  1435. return "MODE2:" . hash('sha256', $salt . $pass);
  1436. } else if ($salt) {
  1437. return "SHA1X:" . sha1("$salt:$pass");
  1438. } else {
  1439. return "SHA1:" . sha1($pass);
  1440. }
  1441. } // function encrypt_password
  1442. function init_plugins() {
  1443. PluginHost::getInstance()->load(PLUGINS, PluginHost::KIND_ALL);
  1444. return true;
  1445. }
  1446. function build_url($parts) {
  1447. return $parts['scheme'] . "://" . $parts['host'] . $parts['path'];
  1448. }
  1449. function cleanup_url_path($path) {
  1450. $path = str_replace("/./", "/", $path);
  1451. $path = str_replace("//", "/", $path);
  1452. return $path;
  1453. }
  1454. /**
  1455. * Converts a (possibly) relative URL to a absolute one.
  1456. *
  1457. * @param string $url Base URL (i.e. from where the document is)
  1458. * @param string $rel_url Possibly relative URL in the document
  1459. *
  1460. * @return string Absolute URL
  1461. */
  1462. function rewrite_relative_url($url, $rel_url) {
  1463. if (strpos($rel_url, "://") !== false) {
  1464. return $rel_url;
  1465. } else if (strpos($rel_url, "//") === 0) {
  1466. # protocol-relative URL (rare but they exist)
  1467. return $rel_url;
  1468. } else if (preg_match("/^[a-z]+:/i", $rel_url)) {
  1469. # magnet:, feed:, etc
  1470. return $rel_url;
  1471. } else if (strpos($rel_url, "/") === 0) {
  1472. $parts = parse_url($url);
  1473. $parts['path'] = $rel_url;
  1474. $parts['path'] = cleanup_url_path($parts['path']);
  1475. return build_url($parts);
  1476. } else {
  1477. $parts = parse_url($url);
  1478. if (!isset($parts['path'])) {
  1479. $parts['path'] = '/';
  1480. }
  1481. $dir = $parts['path'];
  1482. if (substr($dir, -1) !== '/') {
  1483. $dir = dirname($parts['path']);
  1484. $dir !== '/' && $dir .= '/';
  1485. }
  1486. $parts['path'] = $dir . $rel_url;
  1487. $parts['path'] = cleanup_url_path($parts['path']);
  1488. return build_url($parts);
  1489. }
  1490. }
  1491. function cleanup_tags($days = 14, $limit = 1000) {
  1492. $days = (int) $days;
  1493. if (DB_TYPE == "pgsql") {
  1494. $interval_query = "date_updated < NOW() - INTERVAL '$days days'";
  1495. } else if (DB_TYPE == "mysql") {
  1496. $interval_query = "date_updated < DATE_SUB(NOW(), INTERVAL $days DAY)";
  1497. }
  1498. $tags_deleted = 0;
  1499. $pdo = Db::pdo();
  1500. while ($limit > 0) {
  1501. $limit_part = 500;
  1502. $sth = $pdo->prepare("SELECT ttrss_tags.id AS id
  1503. FROM ttrss_tags, ttrss_user_entries, ttrss_entries
  1504. WHERE post_int_id = int_id AND $interval_query AND
  1505. ref_id = ttrss_entries.id AND tag_cache != '' LIMIT ?");
  1506. $sth->bindValue(1, $limit_part, PDO::PARAM_INT);
  1507. $sth->execute();
  1508. $ids = array();
  1509. while ($line = $sth->fetch()) {
  1510. array_push($ids, $line['id']);
  1511. }
  1512. if (count($ids) > 0) {
  1513. $ids = join(",", $ids);
  1514. $usth = $pdo->query("DELETE FROM ttrss_tags WHERE id IN ($ids)");
  1515. $tags_deleted = $usth->rowCount();
  1516. } else {
  1517. break;
  1518. }
  1519. $limit -= $limit_part;
  1520. }
  1521. return $tags_deleted;
  1522. }
  1523. function print_user_stylesheet() {
  1524. $value = get_pref('USER_STYLESHEET');
  1525. if ($value) {
  1526. print "<style type=\"text/css\">";
  1527. print str_replace("<br/>", "\n", $value);
  1528. print "</style>";
  1529. }
  1530. }
  1531. function filter_to_sql($filter, $owner_uid) {
  1532. $query = array();
  1533. $pdo = Db::pdo();
  1534. if (DB_TYPE == "pgsql")
  1535. $reg_qpart = "~";
  1536. else
  1537. $reg_qpart = "REGEXP";
  1538. foreach ($filter["rules"] AS $rule) {
  1539. $rule['reg_exp'] = str_replace('/', '\/', $rule["reg_exp"]);
  1540. $regexp_valid = preg_match('/' . $rule['reg_exp'] . '/',
  1541. $rule['reg_exp']) !== FALSE;
  1542. if ($regexp_valid) {
  1543. $rule['reg_exp'] = $pdo->quote($rule['reg_exp']);
  1544. switch ($rule["type"]) {
  1545. case "title":
  1546. $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
  1547. $rule['reg_exp'] . "')";
  1548. break;
  1549. case "content":
  1550. $qpart = "LOWER(ttrss_entries.content) $reg_qpart LOWER('".
  1551. $rule['reg_exp'] . "')";
  1552. break;
  1553. case "both":
  1554. $qpart = "LOWER(ttrss_entries.title) $reg_qpart LOWER('".
  1555. $rule['reg_exp'] . "') OR LOWER(" .
  1556. "ttrss_entries.content) $reg_qpart LOWER('" . $rule['reg_exp'] . "')";
  1557. break;
  1558. case "tag":
  1559. $qpart = "LOWER(ttrss_user_entries.tag_cache) $reg_qpart LOWER('".
  1560. $rule['reg_exp'] . "')";
  1561. break;
  1562. case "link":
  1563. $qpart = "LOWER(ttrss_entries.link) $reg_qpart LOWER('".
  1564. $rule['reg_exp'] . "')";
  1565. break;
  1566. case "author":
  1567. $qpart = "LOWER(ttrss_entries.author) $reg_qpart LOWER('".
  1568. $rule['reg_exp'] . "')";
  1569. break;
  1570. }
  1571. if (isset($rule['inverse'])) $qpart = "NOT ($qpart)";
  1572. if (isset($rule["feed_id"]) && $rule["feed_id"] > 0) {
  1573. $qpart .= " AND feed_id = " . $pdo->quote($rule["feed_id"]);
  1574. }
  1575. if (isset($rule["cat_id"])) {
  1576. if ($rule["cat_id"] > 0) {
  1577. $children = Feeds::getChildCategories($rule["cat_id"], $owner_uid);
  1578. array_push($children, $rule["cat_id"]);
  1579. $children = array_map("intval", $children);
  1580. $children = join(",", $children);
  1581. $cat_qpart = "cat_id IN ($children)";
  1582. } else {
  1583. $cat_qpart = "cat_id IS NULL";
  1584. }
  1585. $qpart .= " AND $cat_qpart";
  1586. }
  1587. $qpart .= " AND feed_id IS NOT NULL";
  1588. array_push($query, "($qpart)");
  1589. }
  1590. }
  1591. if (count($query) > 0) {
  1592. $fullquery = "(" . join($filter["match_any_rule"] ? "OR" : "AND", $query) . ")";
  1593. } else {
  1594. $fullquery = "(false)";
  1595. }
  1596. if ($filter['inverse']) $fullquery = "(NOT $fullquery)";
  1597. return $fullquery;
  1598. }
  1599. if (!function_exists('gzdecode')) {
  1600. function gzdecode($string) { // no support for 2nd argument
  1601. return file_get_contents('compress.zlib://data:who/cares;base64,'.
  1602. base64_encode($string));
  1603. }
  1604. }
  1605. function get_random_bytes($length) {
  1606. if (function_exists('openssl_random_pseudo_bytes')) {
  1607. return openssl_random_pseudo_bytes($length);
  1608. } else {
  1609. $output = "";
  1610. for ($i = 0; $i < $length; $i++)
  1611. $output .= chr(mt_rand(0, 255));
  1612. return $output;
  1613. }
  1614. }
  1615. function read_stdin() {
  1616. $fp = fopen("php://stdin", "r");
  1617. if ($fp) {
  1618. $line = trim(fgets($fp));
  1619. fclose($fp);
  1620. return $line;
  1621. }
  1622. return null;
  1623. }
  1624. function implements_interface($class, $interface) {
  1625. return in_array($interface, class_implements($class));
  1626. }
  1627. function T_js_decl($s1, $s2) {
  1628. if ($s1 && $s2) {
  1629. $s1 = preg_replace("/\n/", "", $s1);
  1630. $s2 = preg_replace("/\n/", "", $s2);
  1631. $s1 = preg_replace("/\"/", "\\\"", $s1);
  1632. $s2 = preg_replace("/\"/", "\\\"", $s2);
  1633. return "T_messages[\"$s1\"] = \"$s2\";\n";
  1634. }
  1635. }
  1636. function init_js_translations() {
  1637. print 'var T_messages = new Object();
  1638. function __(msg) {
  1639. if (T_messages[msg]) {
  1640. return T_messages[msg];
  1641. } else {
  1642. return msg;
  1643. }
  1644. }
  1645. function ngettext(msg1, msg2, n) {
  1646. return __((parseInt(n) > 1) ? msg2 : msg1);
  1647. }';
  1648. global $text_domains;
  1649. foreach (array_keys($text_domains) as $domain) {
  1650. $l10n = _get_reader($domain);
  1651. for ($i = 0; $i < $l10n->total; $i++) {
  1652. $orig = $l10n->get_original_string($i);
  1653. if(strpos($orig, "\000") !== FALSE) { // Plural forms
  1654. $key = explode(chr(0), $orig);
  1655. print T_js_decl($key[0], _ngettext($key[0], $key[1], 1)); // Singular
  1656. print T_js_decl($key[1], _ngettext($key[0], $key[1], 2)); // Plural
  1657. } else {
  1658. $translation = _dgettext($domain,$orig);
  1659. print T_js_decl($orig, $translation);
  1660. }
  1661. }
  1662. }
  1663. }
  1664. function get_theme_path($theme) {
  1665. if ($theme == "default.php")
  1666. return "css/default.css";
  1667. $check = "themes/$theme";
  1668. if (file_exists($check)) return $check;
  1669. $check = "themes.local/$theme";
  1670. if (file_exists($check)) return $check;
  1671. }
  1672. function theme_exists($theme) {
  1673. return file_exists("themes/$theme") || file_exists("themes.local/$theme");
  1674. }
  1675. /**
  1676. * @SuppressWarnings(unused)
  1677. */
  1678. function error_json($code) {
  1679. require_once "errors.php";
  1680. @$message = $ERRORS[$code];
  1681. return json_encode(array("error" =>
  1682. array("code" => $code, "message" => $message)));
  1683. }
  1684. /*function abs_to_rel_path($dir) {
  1685. $tmp = str_replace(dirname(__DIR__), "", $dir);
  1686. if (strlen($tmp) > 0 && substr($tmp, 0, 1) == "/") $tmp = substr($tmp, 1);
  1687. return $tmp;
  1688. }*/
  1689. function get_upload_error_message($code) {
  1690. $errors = array(
  1691. 0 => __('There is no error, the file uploaded with success'),
  1692. 1 => __('The uploaded file exceeds the upload_max_filesize directive in php.ini'),
  1693. 2 => __('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'),
  1694. 3 => __('The uploaded file was only partially uploaded'),
  1695. 4 => __('No file was uploaded'),
  1696. 6 => __('Missing a temporary folder'),
  1697. 7 => __('Failed to write file to disk.'),
  1698. 8 => __('A PHP extension stopped the file upload.'),
  1699. );
  1700. return $errors[$code];
  1701. }
  1702. function base64_img($filename) {
  1703. if (file_exists($filename)) {
  1704. $ext = pathinfo($filename, PATHINFO_EXTENSION);
  1705. return "data:image/$ext;base64," . base64_encode(file_get_contents($filename));
  1706. } else {
  1707. return "";
  1708. }
  1709. }
  1710. /* this is essentially a wrapper for readfile() which allows plugins to hook
  1711. output with httpd-specific "fast" implementation i.e. X-Sendfile or whatever else
  1712. hook function should return true if request was handled (or at least attempted to)
  1713. note that this can be called without user context so the plugin to handle this
  1714. should be loaded systemwide in config.php */
  1715. function send_local_file($filename) {
  1716. if (file_exists($filename)) {
  1717. if (is_writable($filename)) touch($filename);
  1718. $tmppluginhost = new PluginHost();
  1719. $tmppluginhost->load(PLUGINS, PluginHost::KIND_SYSTEM);
  1720. $tmppluginhost->load_data();
  1721. foreach ($tmppluginhost->get_hooks(PluginHost::HOOK_SEND_LOCAL_FILE) as $plugin) {
  1722. if ($plugin->hook_send_local_file($filename)) return true;
  1723. }
  1724. $mimetype = mime_content_type($filename);
  1725. // this is hardly ideal but 1) only media is cached in images/ and 2) seemingly only mp4
  1726. // video files are detected as octet-stream by mime_content_type()
  1727. if ($mimetype == "application/octet-stream")
  1728. $mimetype = "video/mp4";
  1729. header("Content-type: $mimetype");
  1730. $stamp = gmdate("D, d M Y H:i:s", filemtime($filename)) . " GMT";
  1731. header("Last-Modified: $stamp", true);
  1732. return readfile($filename);
  1733. } else {
  1734. return false;
  1735. }
  1736. }
  1737. function check_mysql_tables() {
  1738. $pdo = Db::pdo();
  1739. $sth = $pdo->prepare("SELECT engine, table_name FROM information_schema.tables WHERE
  1740. table_schema = ? AND table_name LIKE 'ttrss_%' AND engine != 'InnoDB'");
  1741. $sth->execute([DB_NAME]);
  1742. $bad_tables = [];
  1743. while ($line = $sth->fetch()) {
  1744. array_push($bad_tables, $line);
  1745. }
  1746. return $bad_tables;
  1747. }
  1748. function validate_field($string, $allowed, $default = "") {
  1749. if (in_array($string, $allowed))
  1750. return $string;
  1751. else
  1752. return $default;
  1753. }
  1754. function arr_qmarks($arr) {
  1755. return str_repeat('?,', count($arr) - 1) . '?';
  1756. }
  1757. function get_scripts_timestamp() {
  1758. $files = glob("js/*.js");
  1759. $ts = 0;
  1760. foreach ($files as $file) {
  1761. $file_ts = filemtime($file);
  1762. if ($file_ts > $ts) $ts = $file_ts;
  1763. }
  1764. return $ts;
  1765. }