rssfuncs.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. <?php
  2. define_default('DAEMON_UPDATE_LOGIN_LIMIT', 30);
  3. define_default('DAEMON_FEED_LIMIT', 500);
  4. define_default('DAEMON_SLEEP_INTERVAL', 120);
  5. function update_feedbrowser_cache() {
  6. $result = db_query("SELECT feed_url, site_url, title, COUNT(id) AS subscribers
  7. FROM ttrss_feeds WHERE (SELECT COUNT(id) = 0 FROM ttrss_feeds AS tf
  8. WHERE tf.feed_url = ttrss_feeds.feed_url
  9. AND (private IS true OR auth_login != '' OR auth_pass != '' OR feed_url LIKE '%:%@%/%'))
  10. GROUP BY feed_url, site_url, title ORDER BY subscribers DESC LIMIT 1000");
  11. db_query("BEGIN");
  12. db_query("DELETE FROM ttrss_feedbrowser_cache");
  13. $count = 0;
  14. while ($line = db_fetch_assoc($result)) {
  15. $subscribers = db_escape_string($line["subscribers"]);
  16. $feed_url = db_escape_string($line["feed_url"]);
  17. $title = db_escape_string($line["title"]);
  18. $site_url = db_escape_string($line["site_url"]);
  19. $tmp_result = db_query("SELECT subscribers FROM
  20. ttrss_feedbrowser_cache WHERE feed_url = '$feed_url'");
  21. if (db_num_rows($tmp_result) == 0) {
  22. db_query("INSERT INTO ttrss_feedbrowser_cache
  23. (feed_url, site_url, title, subscribers) VALUES ('$feed_url',
  24. '$site_url', '$title', '$subscribers')");
  25. ++$count;
  26. }
  27. }
  28. db_query("COMMIT");
  29. return $count;
  30. }
  31. /**
  32. * Update a feed batch.
  33. * Used by daemons to update n feeds by run.
  34. * Only update feed needing a update, and not being processed
  35. * by another process.
  36. *
  37. * @param mixed $link Database link
  38. * @param integer $limit Maximum number of feeds in update batch. Default to DAEMON_FEED_LIMIT.
  39. * @param boolean $from_http Set to true if you call this function from http to disable cli specific code.
  40. * @param boolean $debug Set to false to disable debug output. Default to true.
  41. * @return void
  42. */
  43. function update_daemon_common($limit = DAEMON_FEED_LIMIT, $from_http = false, $debug = true) {
  44. // Process all other feeds using last_updated and interval parameters
  45. $schema_version = get_schema_version();
  46. if ($schema_version != SCHEMA_VERSION) {
  47. die("Schema version is wrong, please upgrade the database.\n");
  48. }
  49. define('PREFS_NO_CACHE', true);
  50. // Test if the user has loggued in recently. If not, it does not update its feeds.
  51. if (!SINGLE_USER_MODE && DAEMON_UPDATE_LOGIN_LIMIT > 0) {
  52. if (DB_TYPE == "pgsql") {
  53. $login_thresh_qpart = "AND ttrss_users.last_login >= NOW() - INTERVAL '".DAEMON_UPDATE_LOGIN_LIMIT." days'";
  54. } else {
  55. $login_thresh_qpart = "AND ttrss_users.last_login >= DATE_SUB(NOW(), INTERVAL ".DAEMON_UPDATE_LOGIN_LIMIT." DAY)";
  56. }
  57. } else {
  58. $login_thresh_qpart = "";
  59. }
  60. // Test if the feed need a update (update interval exceded).
  61. if (DB_TYPE == "pgsql") {
  62. $update_limit_qpart = "AND ((
  63. ttrss_feeds.update_interval = 0
  64. AND ttrss_user_prefs.value != '-1'
  65. AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_user_prefs.value || ' minutes') AS INTERVAL)
  66. ) OR (
  67. ttrss_feeds.update_interval > 0
  68. AND ttrss_feeds.last_updated < NOW() - CAST((ttrss_feeds.update_interval || ' minutes') AS INTERVAL)
  69. ) OR ttrss_feeds.last_updated IS NULL
  70. OR last_updated = '1970-01-01 00:00:00')";
  71. } else {
  72. $update_limit_qpart = "AND ((
  73. ttrss_feeds.update_interval = 0
  74. AND ttrss_user_prefs.value != '-1'
  75. AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL CONVERT(ttrss_user_prefs.value, SIGNED INTEGER) MINUTE)
  76. ) OR (
  77. ttrss_feeds.update_interval > 0
  78. AND ttrss_feeds.last_updated < DATE_SUB(NOW(), INTERVAL ttrss_feeds.update_interval MINUTE)
  79. ) OR ttrss_feeds.last_updated IS NULL
  80. OR last_updated = '1970-01-01 00:00:00')";
  81. }
  82. // Test if feed is currently being updated by another process.
  83. if (DB_TYPE == "pgsql") {
  84. $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '10 minutes')";
  85. } else {
  86. $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 10 MINUTE))";
  87. }
  88. // Test if there is a limit to number of updated feeds
  89. $query_limit = "";
  90. if($limit) $query_limit = sprintf("LIMIT %d", $limit);
  91. $query = "SELECT DISTINCT ttrss_feeds.feed_url, ttrss_feeds.last_updated
  92. FROM
  93. ttrss_feeds, ttrss_users, ttrss_user_prefs
  94. WHERE
  95. ttrss_feeds.owner_uid = ttrss_users.id
  96. AND ttrss_users.id = ttrss_user_prefs.owner_uid
  97. AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
  98. $login_thresh_qpart $update_limit_qpart
  99. $updstart_thresh_qpart
  100. ORDER BY last_updated $query_limit";
  101. // We search for feed needing update.
  102. $result = db_query($query);
  103. if($debug) _debug(sprintf("Scheduled %d feeds to update...", db_num_rows($result)));
  104. // Here is a little cache magic in order to minimize risk of double feed updates.
  105. $feeds_to_update = array();
  106. while ($line = db_fetch_assoc($result)) {
  107. array_push($feeds_to_update, db_escape_string($line['feed_url']));
  108. }
  109. // We update the feed last update started date before anything else.
  110. // There is no lag due to feed contents downloads
  111. // It prevent an other process to update the same feed.
  112. if(count($feeds_to_update) > 0) {
  113. $feeds_quoted = array();
  114. foreach ($feeds_to_update as $feed) {
  115. array_push($feeds_quoted, "'" . db_escape_string($feed) . "'");
  116. }
  117. db_query(sprintf("UPDATE ttrss_feeds SET last_update_started = NOW()
  118. WHERE feed_url IN (%s)", implode(',', $feeds_quoted)));
  119. }
  120. $nf = 0;
  121. // For each feed, we call the feed update function.
  122. foreach ($feeds_to_update as $feed) {
  123. if($debug) _debug("Base feed: $feed");
  124. //update_rss_feed($line["id"], true);
  125. // since we have the data cached, we can deal with other feeds with the same url
  126. $tmp_result = db_query("SELECT DISTINCT ttrss_feeds.id,last_updated,ttrss_feeds.owner_uid
  127. FROM ttrss_feeds, ttrss_users, ttrss_user_prefs WHERE
  128. ttrss_user_prefs.owner_uid = ttrss_feeds.owner_uid AND
  129. ttrss_users.id = ttrss_user_prefs.owner_uid AND
  130. ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL' AND
  131. feed_url = '".db_escape_string($feed)."' AND
  132. (ttrss_feeds.update_interval > 0 OR
  133. ttrss_user_prefs.value != '-1')
  134. $login_thresh_qpart
  135. ORDER BY ttrss_feeds.id $query_limit");
  136. if (db_num_rows($tmp_result) > 0) {
  137. while ($tline = db_fetch_assoc($tmp_result)) {
  138. if($debug) _debug(" => " . $tline["last_updated"] . ", " . $tline["id"] . " " . $tline["owner_uid"]);
  139. update_rss_feed($tline["id"], true);
  140. _debug_suppress(false);
  141. ++$nf;
  142. }
  143. }
  144. }
  145. require_once "digest.php";
  146. // Send feed digests by email if needed.
  147. send_headlines_digests($debug);
  148. return $nf;
  149. } // function update_daemon_common
  150. // ignore_daemon is not used
  151. function update_rss_feed($feed, $ignore_daemon = false, $no_cache = false) {
  152. $debug_enabled = defined('DAEMON_EXTENDED_DEBUG') || $_REQUEST['xdebug'];
  153. _debug_suppress(!$debug_enabled);
  154. _debug("start", $debug_enabled);
  155. $result = db_query("SELECT id,update_interval,auth_login,
  156. feed_url,auth_pass,cache_images,last_updated,
  157. mark_unread_on_update, owner_uid,
  158. pubsub_state, auth_pass_encrypted,
  159. (SELECT max(date_entered) FROM
  160. ttrss_entries, ttrss_user_entries where ref_id = id AND feed_id = '$feed') AS last_article_timestamp
  161. FROM ttrss_feeds WHERE id = '$feed'");
  162. if (db_num_rows($result) == 0) {
  163. _debug("feed $feed NOT FOUND/SKIPPED", $debug_enabled);
  164. return false;
  165. }
  166. $last_updated = db_fetch_result($result, 0, "last_updated");
  167. $last_article_timestamp = @strtotime(db_fetch_result($result, 0, "last_article_timestamp"));
  168. if (defined('_DISABLE_HTTP_304'))
  169. $last_article_timestamp = 0;
  170. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  171. $mark_unread_on_update = sql_bool_to_bool(db_fetch_result($result,
  172. 0, "mark_unread_on_update"));
  173. $pubsub_state = db_fetch_result($result, 0, "pubsub_state");
  174. $auth_pass_encrypted = sql_bool_to_bool(db_fetch_result($result,
  175. 0, "auth_pass_encrypted"));
  176. db_query("UPDATE ttrss_feeds SET last_update_started = NOW()
  177. WHERE id = '$feed'");
  178. $auth_login = db_fetch_result($result, 0, "auth_login");
  179. $auth_pass = db_fetch_result($result, 0, "auth_pass");
  180. if ($auth_pass_encrypted) {
  181. require_once "crypt.php";
  182. $auth_pass = decrypt_string($auth_pass);
  183. }
  184. $cache_images = sql_bool_to_bool(db_fetch_result($result, 0, "cache_images"));
  185. $fetch_url = db_fetch_result($result, 0, "feed_url");
  186. $feed = db_escape_string($feed);
  187. $date_feed_processed = date('Y-m-d H:i');
  188. $cache_filename = CACHE_DIR . "/simplepie/" . sha1($fetch_url) . ".xml";
  189. $pluginhost = new PluginHost();
  190. $pluginhost->set_debug($debug_enabled);
  191. $user_plugins = get_pref("_ENABLED_PLUGINS", $owner_uid);
  192. $pluginhost->load(PLUGINS, PluginHost::KIND_ALL);
  193. $pluginhost->load($user_plugins, PluginHost::KIND_USER, $owner_uid);
  194. $pluginhost->load_data();
  195. $rss = false;
  196. $rss_hash = false;
  197. $force_refetch = isset($_REQUEST["force_refetch"]);
  198. if (file_exists($cache_filename) &&
  199. is_readable($cache_filename) &&
  200. !$auth_login && !$auth_pass &&
  201. filemtime($cache_filename) > time() - 30) {
  202. _debug("using local cache.", $debug_enabled);
  203. @$feed_data = file_get_contents($cache_filename);
  204. if ($feed_data) {
  205. $rss_hash = sha1($feed_data);
  206. }
  207. } else {
  208. _debug("local cache will not be used for this feed", $debug_enabled);
  209. }
  210. if (!$rss) {
  211. foreach ($pluginhost->get_hooks(PluginHost::HOOK_FETCH_FEED) as $plugin) {
  212. $feed_data = $plugin->hook_fetch_feed($feed_data, $fetch_url, $owner_uid, $feed);
  213. }
  214. if (!$feed_data) {
  215. _debug("fetching [$fetch_url]...", $debug_enabled);
  216. _debug("If-Modified-Since: ".gmdate('D, d M Y H:i:s \G\M\T', $last_article_timestamp), $debug_enabled);
  217. $feed_data = fetch_file_contents($fetch_url, false,
  218. $auth_login, $auth_pass, false,
  219. $no_cache ? FEED_FETCH_NO_CACHE_TIMEOUT : FEED_FETCH_TIMEOUT,
  220. $force_refetch ? 0 : $last_article_timestamp);
  221. global $fetch_curl_used;
  222. if (!$fetch_curl_used) {
  223. $tmp = @gzdecode($feed_data);
  224. if ($tmp) $feed_data = $tmp;
  225. }
  226. $feed_data = trim($feed_data);
  227. _debug("fetch done.", $debug_enabled);
  228. /* if ($feed_data) {
  229. $error = verify_feed_xml($feed_data);
  230. if ($error) {
  231. _debug("error verifying XML, code: " . $error->code, $debug_enabled);
  232. if ($error->code == 26) {
  233. _debug("got error 26, trying to decode entities...", $debug_enabled);
  234. $feed_data = html_entity_decode($feed_data, ENT_COMPAT, 'UTF-8');
  235. $error = verify_feed_xml($feed_data);
  236. if ($error) $feed_data = '';
  237. }
  238. }
  239. } */
  240. }
  241. if (!$feed_data) {
  242. global $fetch_last_error;
  243. global $fetch_last_error_code;
  244. _debug("unable to fetch: $fetch_last_error [$fetch_last_error_code]", $debug_enabled);
  245. $error_escaped = '';
  246. // If-Modified-Since
  247. if ($fetch_last_error_code != 304) {
  248. $error_escaped = db_escape_string($fetch_last_error);
  249. } else {
  250. _debug("source claims data not modified, nothing to do.", $debug_enabled);
  251. }
  252. db_query(
  253. "UPDATE ttrss_feeds SET last_error = '$error_escaped',
  254. last_updated = NOW() WHERE id = '$feed'");
  255. return;
  256. }
  257. }
  258. foreach ($pluginhost->get_hooks(PluginHost::HOOK_FEED_FETCHED) as $plugin) {
  259. $feed_data = $plugin->hook_feed_fetched($feed_data, $fetch_url, $owner_uid, $feed);
  260. }
  261. // set last update to now so if anything *simplepie* crashes later we won't be
  262. // continuously failing on the same feed
  263. //db_query("UPDATE ttrss_feeds SET last_updated = NOW() WHERE id = '$feed'");
  264. if (!$rss) {
  265. $rss = new FeedParser($feed_data);
  266. $rss->init();
  267. }
  268. require_once "lib/languagedetect/LanguageDetect.php";
  269. $lang = new Text_LanguageDetect();
  270. $lang->setNameMode(2);
  271. // print_r($rss);
  272. $feed = db_escape_string($feed);
  273. if (!$rss->error()) {
  274. // cache data for later
  275. if (!$auth_pass && !$auth_login && is_writable(CACHE_DIR . "/simplepie")) {
  276. $new_rss_hash = sha1($rss_data);
  277. if ($new_rss_hash != $rss_hash && count($rss->get_items()) > 0 ) {
  278. _debug("saving $cache_filename", $debug_enabled);
  279. @file_put_contents($cache_filename, $feed_data);
  280. }
  281. }
  282. // We use local pluginhost here because we need to load different per-user feed plugins
  283. $pluginhost->run_hooks(PluginHost::HOOK_FEED_PARSED, "hook_feed_parsed", $rss);
  284. _debug("processing feed data...", $debug_enabled);
  285. // db_query("BEGIN");
  286. if (DB_TYPE == "pgsql") {
  287. $favicon_interval_qpart = "favicon_last_checked < NOW() - INTERVAL '12 hour'";
  288. } else {
  289. $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)";
  290. }
  291. $result = db_query("SELECT title,site_url,owner_uid,favicon_avg_color,
  292. (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS
  293. favicon_needs_check
  294. FROM ttrss_feeds WHERE id = '$feed'");
  295. $registered_title = db_fetch_result($result, 0, "title");
  296. $orig_site_url = db_fetch_result($result, 0, "site_url");
  297. $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0,
  298. "favicon_needs_check"));
  299. $favicon_avg_color = db_fetch_result($result, 0, "favicon_avg_color");
  300. $owner_uid = db_fetch_result($result, 0, "owner_uid");
  301. $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
  302. _debug("site_url: $site_url", $debug_enabled);
  303. _debug("feed_title: " . $rss->get_title(), $debug_enabled);
  304. if ($favicon_needs_check || $force_refetch) {
  305. /* terrible hack: if we crash on floicon shit here, we won't check
  306. * the icon avgcolor again (unless the icon got updated) */
  307. $favicon_file = ICONS_DIR . "/$feed.ico";
  308. $favicon_modified = @filemtime($favicon_file);
  309. _debug("checking favicon...", $debug_enabled);
  310. check_feed_favicon($site_url, $feed);
  311. $favicon_modified_new = @filemtime($favicon_file);
  312. if ($favicon_modified_new > $favicon_modified)
  313. $favicon_avg_color = '';
  314. if (file_exists($favicon_file) && function_exists("imagecreatefromstring") && $favicon_avg_color == '') {
  315. require_once "colors.php";
  316. db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE
  317. id = '$feed'");
  318. $favicon_color = db_escape_string(
  319. calculate_avg_color($favicon_file));
  320. $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'";
  321. } else if ($favicon_avg_color == 'fail') {
  322. _debug("floicon failed on this file, not trying to recalculate avg color", $debug_enabled);
  323. }
  324. db_query("UPDATE ttrss_feeds SET favicon_last_checked = NOW()
  325. $favicon_colorstring
  326. WHERE id = '$feed'");
  327. }
  328. if (!$registered_title || $registered_title == "[Unknown]") {
  329. $feed_title = db_escape_string($rss->get_title());
  330. if ($feed_title) {
  331. _debug("registering title: $feed_title", $debug_enabled);
  332. db_query("UPDATE ttrss_feeds SET
  333. title = '$feed_title' WHERE id = '$feed'");
  334. }
  335. }
  336. if ($site_url && $orig_site_url != $site_url) {
  337. db_query("UPDATE ttrss_feeds SET
  338. site_url = '$site_url' WHERE id = '$feed'");
  339. }
  340. _debug("loading filters & labels...", $debug_enabled);
  341. $filters = load_filters($feed, $owner_uid);
  342. $labels = get_all_labels($owner_uid);
  343. _debug("" . count($filters) . " filters loaded.", $debug_enabled);
  344. $items = $rss->get_items();
  345. if (!is_array($items)) {
  346. _debug("no articles found.", $debug_enabled);
  347. db_query("UPDATE ttrss_feeds
  348. SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
  349. return; // no articles
  350. }
  351. if ($pubsub_state != 2 && PUBSUBHUBBUB_ENABLED) {
  352. _debug("checking for PUSH hub...", $debug_enabled);
  353. $feed_hub_url = false;
  354. $links = $rss->get_links('hub');
  355. if ($links && is_array($links)) {
  356. foreach ($links as $l) {
  357. $feed_hub_url = $l;
  358. break;
  359. }
  360. }
  361. _debug("feed hub url: $feed_hub_url", $debug_enabled);
  362. if ($feed_hub_url && function_exists('curl_init') &&
  363. !ini_get("open_basedir")) {
  364. require_once 'lib/pubsubhubbub/subscriber.php';
  365. $callback_url = get_self_url_prefix() .
  366. "/public.php?op=pubsub&id=$feed";
  367. $s = new Subscriber($feed_hub_url, $callback_url);
  368. $rc = $s->subscribe($fetch_url);
  369. _debug("feed hub url found, subscribe request sent.", $debug_enabled);
  370. db_query("UPDATE ttrss_feeds SET pubsub_state = 1
  371. WHERE id = '$feed'");
  372. }
  373. }
  374. _debug("processing articles...", $debug_enabled);
  375. foreach ($items as $item) {
  376. if ($_REQUEST['xdebug'] == 3) {
  377. print_r($item);
  378. }
  379. $entry_guid = $item->get_id();
  380. if (!$entry_guid) $entry_guid = $item->get_link();
  381. if (!$entry_guid) $entry_guid = make_guid_from_title($item->get_title());
  382. _debug("f_guid $entry_guid", $debug_enabled);
  383. if (!$entry_guid) continue;
  384. $entry_guid = "$owner_uid,$entry_guid";
  385. $entry_guid_hashed = db_escape_string('SHA1:' . sha1($entry_guid));
  386. _debug("guid $entry_guid / $entry_guid_hashed", $debug_enabled);
  387. $entry_timestamp = "";
  388. $entry_timestamp = $item->get_date();
  389. _debug("orig date: " . $item->get_date(), $debug_enabled);
  390. if ($entry_timestamp == -1 || !$entry_timestamp || $entry_timestamp > time()) {
  391. $entry_timestamp = time();
  392. $no_orig_date = 'true';
  393. } else {
  394. $no_orig_date = 'false';
  395. }
  396. $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
  397. _debug("date $entry_timestamp [$entry_timestamp_fmt]", $debug_enabled);
  398. // $entry_title = html_entity_decode($item->get_title(), ENT_COMPAT, 'UTF-8');
  399. // $entry_title = decode_numeric_entities($entry_title);
  400. $entry_title = $item->get_title();
  401. $entry_link = rewrite_relative_url($site_url, $item->get_link());
  402. _debug("title $entry_title", $debug_enabled);
  403. _debug("link $entry_link", $debug_enabled);
  404. if (!$entry_title) $entry_title = date("Y-m-d H:i:s", $entry_timestamp);;
  405. $entry_content = $item->get_content();
  406. if (!$entry_content) $entry_content = $item->get_description();
  407. if ($_REQUEST["xdebug"] == 2) {
  408. print "content: ";
  409. print $entry_content;
  410. print "\n";
  411. }
  412. $entry_language = $lang->detect($entry_title . " " . $entry_content, 1);
  413. if (count($entry_language) > 0) {
  414. $entry_language = array_keys($entry_language);
  415. $entry_language = db_escape_string(substr($entry_language[0], 0, 2));
  416. _debug("detected language: $entry_language", $debug_enabled);
  417. } else{
  418. $entry_language = "";
  419. }
  420. $entry_comments = $item->get_comments_url();
  421. $entry_author = $item->get_author();
  422. $entry_guid = db_escape_string(mb_substr($entry_guid, 0, 245));
  423. $entry_comments = db_escape_string(mb_substr(trim($entry_comments), 0, 245));
  424. $entry_author = db_escape_string(mb_substr(trim($entry_author), 0, 245));
  425. $num_comments = (int) $item->get_comments_count();
  426. _debug("author $entry_author", $debug_enabled);
  427. _debug("num_comments: $num_comments", $debug_enabled);
  428. _debug("looking for tags...", $debug_enabled);
  429. // parse <category> entries into tags
  430. $additional_tags = array();
  431. $additional_tags_src = $item->get_categories();
  432. if (is_array($additional_tags_src)) {
  433. foreach ($additional_tags_src as $tobj) {
  434. array_push($additional_tags, $tobj);
  435. }
  436. }
  437. $entry_tags = array_unique($additional_tags);
  438. for ($i = 0; $i < count($entry_tags); $i++)
  439. $entry_tags[$i] = mb_strtolower($entry_tags[$i], 'utf-8');
  440. _debug("tags found: " . join(",", $entry_tags), $debug_enabled);
  441. _debug("done collecting data.", $debug_enabled);
  442. // TODO: less memory-hungry implementation
  443. _debug("applying plugin filters..", $debug_enabled);
  444. // FIXME not sure if owner_uid is a good idea here, we may have a base entry without user entry (?)
  445. $result = db_query("SELECT plugin_data,title,content,link,tag_cache,author FROM ttrss_entries, ttrss_user_entries
  446. WHERE ref_id = id AND (guid = '".db_escape_string($entry_guid)."' OR guid = '$entry_guid_hashed') AND owner_uid = $owner_uid");
  447. if (db_num_rows($result) != 0) {
  448. $entry_plugin_data = db_fetch_result($result, 0, "plugin_data");
  449. $stored_article = array("title" => db_fetch_result($result, 0, "title"),
  450. "content" => db_fetch_result($result, 0, "content"),
  451. "link" => db_fetch_result($result, 0, "link"),
  452. "tags" => explode(",", db_fetch_result($result, 0, "tag_cache")),
  453. "author" => db_fetch_result($result, 0, "author"));
  454. } else {
  455. $entry_plugin_data = "";
  456. $stored_article = array();
  457. }
  458. $article = array("owner_uid" => $owner_uid, // read only
  459. "guid" => $entry_guid, // read only
  460. "title" => $entry_title,
  461. "content" => $entry_content,
  462. "link" => $entry_link,
  463. "tags" => $entry_tags,
  464. "plugin_data" => $entry_plugin_data,
  465. "author" => $entry_author,
  466. "stored" => $stored_article,
  467. "feed" => array("id" => $feed,
  468. "fetch_url" => $fetch_url,
  469. "site_url" => $site_url)
  470. );
  471. foreach ($pluginhost->get_hooks(PluginHost::HOOK_ARTICLE_FILTER) as $plugin) {
  472. $article = $plugin->hook_article_filter($article);
  473. }
  474. $entry_tags = $article["tags"];
  475. $entry_guid = db_escape_string($entry_guid);
  476. $entry_title = db_escape_string($article["title"]);
  477. $entry_author = db_escape_string($article["author"]);
  478. $entry_link = db_escape_string($article["link"]);
  479. $entry_plugin_data = db_escape_string($article["plugin_data"]);
  480. $entry_content = $article["content"]; // escaped below
  481. _debug("plugin data: $entry_plugin_data", $debug_enabled);
  482. if ($cache_images && is_writable(CACHE_DIR . '/images'))
  483. cache_images($entry_content, $site_url, $debug_enabled);
  484. $entry_content = db_escape_string($entry_content, false);
  485. $content_hash = "SHA1:" . sha1($entry_content);
  486. db_query("BEGIN");
  487. $result = db_query("SELECT id FROM ttrss_entries
  488. WHERE (guid = '$entry_guid' OR guid = '$entry_guid_hashed')");
  489. if (db_num_rows($result) == 0) {
  490. _debug("base guid [$entry_guid] not found", $debug_enabled);
  491. // base post entry does not exist, create it
  492. $result = db_query(
  493. "INSERT INTO ttrss_entries
  494. (title,
  495. guid,
  496. link,
  497. updated,
  498. content,
  499. content_hash,
  500. no_orig_date,
  501. date_updated,
  502. date_entered,
  503. comments,
  504. num_comments,
  505. plugin_data,
  506. lang,
  507. author)
  508. VALUES
  509. ('$entry_title',
  510. '$entry_guid_hashed',
  511. '$entry_link',
  512. '$entry_timestamp_fmt',
  513. '$entry_content',
  514. '$content_hash',
  515. $no_orig_date,
  516. NOW(),
  517. '$date_feed_processed',
  518. '$entry_comments',
  519. '$num_comments',
  520. '$entry_plugin_data',
  521. '$entry_language',
  522. '$entry_author')");
  523. $article_labels = array();
  524. } else {
  525. // we keep encountering the entry in feeds, so we need to
  526. // update date_updated column so that we don't get horrible
  527. // dupes when the entry gets purged and reinserted again e.g.
  528. // in the case of SLOW SLOW OMG SLOW updating feeds
  529. $base_entry_id = db_fetch_result($result, 0, "id");
  530. db_query("UPDATE ttrss_entries SET date_updated = NOW()
  531. WHERE id = '$base_entry_id'");
  532. $article_labels = get_article_labels($base_entry_id, $owner_uid);
  533. }
  534. // now it should exist, if not - bad luck then
  535. $result = db_query("SELECT
  536. id,content_hash,no_orig_date,title,plugin_data,guid,
  537. ".SUBSTRING_FOR_DATE."(date_updated,1,19) as date_updated,
  538. ".SUBSTRING_FOR_DATE."(updated,1,19) as updated,
  539. num_comments
  540. FROM
  541. ttrss_entries
  542. WHERE guid = '$entry_guid' OR guid = '$entry_guid_hashed'");
  543. $entry_ref_id = 0;
  544. $entry_int_id = 0;
  545. if (db_num_rows($result) == 1) {
  546. _debug("base guid found, checking for user record", $debug_enabled);
  547. // this will be used below in update handler
  548. $orig_content_hash = db_fetch_result($result, 0, "content_hash");
  549. $orig_title = db_fetch_result($result, 0, "title");
  550. $orig_num_comments = db_fetch_result($result, 0, "num_comments");
  551. $orig_date_updated = strtotime(db_fetch_result($result,
  552. 0, "date_updated"));
  553. $orig_plugin_data = db_fetch_result($result, 0, "plugin_data");
  554. $ref_id = db_fetch_result($result, 0, "id");
  555. $entry_ref_id = $ref_id;
  556. /* $stored_guid = db_fetch_result($result, 0, "guid");
  557. if ($stored_guid != $entry_guid_hashed) {
  558. if ($debug_enabled) _debug("upgrading compat guid to hashed one", $debug_enabled);
  559. db_query("UPDATE ttrss_entries SET guid = '$entry_guid_hashed' WHERE
  560. id = '$ref_id'");
  561. } */
  562. // check for user post link to main table
  563. // do we allow duplicate posts with same GUID in different feeds?
  564. if (get_pref("ALLOW_DUPLICATE_POSTS", $owner_uid, false)) {
  565. $dupcheck_qpart = "AND (feed_id = '$feed' OR feed_id IS NULL)";
  566. } else {
  567. $dupcheck_qpart = "";
  568. }
  569. /* Collect article tags here so we could filter by them: */
  570. $article_filters = get_article_filters($filters, $entry_title,
  571. $entry_content, $entry_link, $entry_timestamp, $entry_author,
  572. $entry_tags);
  573. if ($debug_enabled) {
  574. _debug("article filters: ", $debug_enabled);
  575. if (count($article_filters) != 0) {
  576. print_r($article_filters);
  577. }
  578. }
  579. if (find_article_filter($article_filters, "filter")) {
  580. db_query("COMMIT"); // close transaction in progress
  581. continue;
  582. }
  583. $score = calculate_article_score($article_filters);
  584. _debug("initial score: $score", $debug_enabled);
  585. $query = "SELECT ref_id, int_id FROM ttrss_user_entries WHERE
  586. ref_id = '$ref_id' AND owner_uid = '$owner_uid'
  587. $dupcheck_qpart";
  588. // if ($_REQUEST["xdebug"]) print "$query\n";
  589. $result = db_query($query);
  590. // okay it doesn't exist - create user entry
  591. if (db_num_rows($result) == 0) {
  592. _debug("user record not found, creating...", $debug_enabled);
  593. if ($score >= -500 && !find_article_filter($article_filters, 'catchup')) {
  594. $unread = 'true';
  595. $last_read_qpart = 'NULL';
  596. } else {
  597. $unread = 'false';
  598. $last_read_qpart = 'NOW()';
  599. }
  600. if (find_article_filter($article_filters, 'mark') || $score > 1000) {
  601. $marked = 'true';
  602. } else {
  603. $marked = 'false';
  604. }
  605. if (find_article_filter($article_filters, 'publish')) {
  606. $published = 'true';
  607. } else {
  608. $published = 'false';
  609. }
  610. // N-grams
  611. if (DB_TYPE == "pgsql" and defined('_NGRAM_TITLE_DUPLICATE_THRESHOLD')) {
  612. $result = db_query("SELECT COUNT(*) AS similar FROM
  613. ttrss_entries,ttrss_user_entries
  614. WHERE ref_id = id AND updated >= NOW() - INTERVAL '7 day'
  615. AND similarity(title, '$entry_title') >= "._NGRAM_TITLE_DUPLICATE_THRESHOLD."
  616. AND owner_uid = $owner_uid");
  617. $ngram_similar = db_fetch_result($result, 0, "similar");
  618. _debug("N-gram similar results: $ngram_similar", $debug_enabled);
  619. if ($ngram_similar > 0) {
  620. $unread = 'false';
  621. }
  622. }
  623. $last_marked = ($marked == 'true') ? 'NOW()' : 'NULL';
  624. $last_published = ($published == 'true') ? 'NOW()' : 'NULL';
  625. $result = db_query(
  626. "INSERT INTO ttrss_user_entries
  627. (ref_id, owner_uid, feed_id, unread, last_read, marked,
  628. published, score, tag_cache, label_cache, uuid,
  629. last_marked, last_published)
  630. VALUES ('$ref_id', '$owner_uid', '$feed', $unread,
  631. $last_read_qpart, $marked, $published, '$score', '', '',
  632. '', $last_marked, $last_published)");
  633. if (PUBSUBHUBBUB_HUB && $published == 'true') {
  634. $rss_link = get_self_url_prefix() .
  635. "/public.php?op=rss&id=-2&key=" .
  636. get_feed_access_key(-2, false, $owner_uid);
  637. $p = new Publisher(PUBSUBHUBBUB_HUB);
  638. $pubsub_result = $p->publish_update($rss_link);
  639. }
  640. $result = db_query(
  641. "SELECT int_id FROM ttrss_user_entries WHERE
  642. ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND
  643. feed_id = '$feed' LIMIT 1");
  644. if (db_num_rows($result) == 1) {
  645. $entry_int_id = db_fetch_result($result, 0, "int_id");
  646. }
  647. } else {
  648. _debug("user record FOUND", $debug_enabled);
  649. $entry_ref_id = db_fetch_result($result, 0, "ref_id");
  650. $entry_int_id = db_fetch_result($result, 0, "int_id");
  651. }
  652. _debug("RID: $entry_ref_id, IID: $entry_int_id", $debug_enabled);
  653. $post_needs_update = false;
  654. $update_insignificant = false;
  655. if ($orig_num_comments != $num_comments) {
  656. $post_needs_update = true;
  657. $update_insignificant = true;
  658. }
  659. if ($entry_plugin_data != $orig_plugin_data) {
  660. $post_needs_update = true;
  661. $update_insignificant = true;
  662. }
  663. if ($content_hash != $orig_content_hash) {
  664. $post_needs_update = true;
  665. $update_insignificant = false;
  666. }
  667. if (db_escape_string($orig_title) != $entry_title) {
  668. $post_needs_update = true;
  669. $update_insignificant = false;
  670. }
  671. // if post needs update, update it and mark all user entries
  672. // linking to this post as updated
  673. if ($post_needs_update) {
  674. if (defined('DAEMON_EXTENDED_DEBUG')) {
  675. _debug("post $entry_guid_hashed needs update...", $debug_enabled);
  676. }
  677. // print "<!-- post $orig_title needs update : $post_needs_update -->";
  678. db_query("UPDATE ttrss_entries
  679. SET title = '$entry_title', content = '$entry_content',
  680. content_hash = '$content_hash',
  681. updated = '$entry_timestamp_fmt',
  682. num_comments = '$num_comments',
  683. plugin_data = '$entry_plugin_data'
  684. WHERE id = '$ref_id'");
  685. if (!$update_insignificant) {
  686. if ($mark_unread_on_update) {
  687. db_query("UPDATE ttrss_user_entries
  688. SET last_read = null, unread = true WHERE ref_id = '$ref_id'");
  689. }
  690. }
  691. }
  692. }
  693. db_query("COMMIT");
  694. _debug("assigning labels...", $debug_enabled);
  695. assign_article_to_label_filters($entry_ref_id, $article_filters,
  696. $owner_uid, $article_labels);
  697. _debug("looking for enclosures...", $debug_enabled);
  698. // enclosures
  699. $enclosures = array();
  700. $encs = $item->get_enclosures();
  701. if (is_array($encs)) {
  702. foreach ($encs as $e) {
  703. $e_item = array(
  704. $e->link, $e->type, $e->length, $e->title);
  705. array_push($enclosures, $e_item);
  706. }
  707. }
  708. if ($debug_enabled) {
  709. _debug("article enclosures:", $debug_enabled);
  710. print_r($enclosures);
  711. }
  712. db_query("BEGIN");
  713. // debugging
  714. // db_query("DELETE FROM ttrss_enclosures WHERE post_id = '$entry_ref_id'");
  715. foreach ($enclosures as $enc) {
  716. $enc_url = db_escape_string($enc[0]);
  717. $enc_type = db_escape_string($enc[1]);
  718. $enc_dur = db_escape_string($enc[2]);
  719. $enc_title = db_escape_string($enc[3]);
  720. $result = db_query("SELECT id FROM ttrss_enclosures
  721. WHERE content_url = '$enc_url' AND post_id = '$entry_ref_id'");
  722. if (db_num_rows($result) == 0) {
  723. db_query("INSERT INTO ttrss_enclosures
  724. (content_url, content_type, title, duration, post_id) VALUES
  725. ('$enc_url', '$enc_type', '$enc_title', '$enc_dur', '$entry_ref_id')");
  726. }
  727. }
  728. db_query("COMMIT");
  729. // check for manual tags (we have to do it here since they're loaded from filters)
  730. foreach ($article_filters as $f) {
  731. if ($f["type"] == "tag") {
  732. $manual_tags = trim_array(explode(",", $f["param"]));
  733. foreach ($manual_tags as $tag) {
  734. if (tag_is_valid($tag)) {
  735. array_push($entry_tags, $tag);
  736. }
  737. }
  738. }
  739. }
  740. // Skip boring tags
  741. $boring_tags = trim_array(explode(",", mb_strtolower(get_pref(
  742. 'BLACKLISTED_TAGS', $owner_uid, ''), 'utf-8')));
  743. $filtered_tags = array();
  744. $tags_to_cache = array();
  745. if ($entry_tags && is_array($entry_tags)) {
  746. foreach ($entry_tags as $tag) {
  747. if (array_search($tag, $boring_tags) === false) {
  748. array_push($filtered_tags, $tag);
  749. }
  750. }
  751. }
  752. $filtered_tags = array_unique($filtered_tags);
  753. if ($debug_enabled) {
  754. _debug("filtered article tags:", $debug_enabled);
  755. print_r($filtered_tags);
  756. }
  757. // Save article tags in the database
  758. if (count($filtered_tags) > 0) {
  759. db_query("BEGIN");
  760. foreach ($filtered_tags as $tag) {
  761. $tag = sanitize_tag($tag);
  762. $tag = db_escape_string($tag);
  763. if (!tag_is_valid($tag)) continue;
  764. $result = db_query("SELECT id FROM ttrss_tags
  765. WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND
  766. owner_uid = '$owner_uid' LIMIT 1");
  767. if ($result && db_num_rows($result) == 0) {
  768. db_query("INSERT INTO ttrss_tags
  769. (owner_uid,tag_name,post_int_id)
  770. VALUES ('$owner_uid','$tag', '$entry_int_id')");
  771. }
  772. array_push($tags_to_cache, $tag);
  773. }
  774. /* update the cache */
  775. $tags_to_cache = array_unique($tags_to_cache);
  776. $tags_str = db_escape_string(join(",", $tags_to_cache));
  777. db_query("UPDATE ttrss_user_entries
  778. SET tag_cache = '$tags_str' WHERE ref_id = '$entry_ref_id'
  779. AND owner_uid = $owner_uid");
  780. db_query("COMMIT");
  781. }
  782. if (get_pref("AUTO_ASSIGN_LABELS", $owner_uid, false)) {
  783. _debug("auto-assigning labels...", $debug_enabled);
  784. foreach ($labels as $label) {
  785. $caption = preg_quote($label["caption"]);
  786. if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($entry_content) . " $entry_title")) {
  787. if (!labels_contains_caption($article_labels, $caption)) {
  788. label_add_article($entry_ref_id, $caption, $owner_uid);
  789. }
  790. }
  791. }
  792. }
  793. _debug("article processed", $debug_enabled);
  794. }
  795. _debug("purging feed...", $debug_enabled);
  796. purge_feed($feed, 0, $debug_enabled);
  797. db_query("UPDATE ttrss_feeds
  798. SET last_updated = NOW(), last_error = '' WHERE id = '$feed'");
  799. // db_query("COMMIT");
  800. } else {
  801. $error_msg = db_escape_string(mb_substr($rss->error(), 0, 245));
  802. _debug("error fetching feed: $error_msg", $debug_enabled);
  803. db_query(
  804. "UPDATE ttrss_feeds SET last_error = '$error_msg',
  805. last_updated = NOW() WHERE id = '$feed'");
  806. }
  807. unset($rss);
  808. _debug("done", $debug_enabled);
  809. }
  810. function cache_images($html, $site_url, $debug) {
  811. $cache_dir = CACHE_DIR . "/images";
  812. libxml_use_internal_errors(true);
  813. $charset_hack = '<head>
  814. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  815. </head>';
  816. $doc = new DOMDocument();
  817. $doc->loadHTML($charset_hack . $html);
  818. $xpath = new DOMXPath($doc);
  819. $entries = $xpath->query('(//img[@src])');
  820. foreach ($entries as $entry) {
  821. if ($entry->hasAttribute('src')) {
  822. $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
  823. $local_filename = CACHE_DIR . "/images/" . sha1($src) . ".png";
  824. if ($debug) _debug("cache_images: downloading: $src to $local_filename");
  825. if (!file_exists($local_filename)) {
  826. $file_content = fetch_file_contents($src);
  827. if ($file_content && strlen($file_content) > 1024) {
  828. file_put_contents($local_filename, $file_content);
  829. }
  830. }
  831. /* if (file_exists($local_filename)) {
  832. $entry->setAttribute('src', SELF_URL_PATH . '/image.php?url=' .
  833. base64_encode($src));
  834. } */
  835. }
  836. }
  837. //$node = $doc->getElementsByTagName('body')->item(0);
  838. //return $doc->saveXML($node);
  839. }
  840. function expire_error_log($debug) {
  841. if ($debug) _debug("Removing old error log entries...");
  842. if (DB_TYPE == "pgsql") {
  843. db_query("DELETE FROM ttrss_error_log
  844. WHERE created_at < NOW() - INTERVAL '7 days'");
  845. } else {
  846. db_query("DELETE FROM ttrss_error_log
  847. WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY)");
  848. }
  849. }
  850. function expire_lock_files($debug) {
  851. //if ($debug) _debug("Removing old lock files...");
  852. $num_deleted = 0;
  853. if (is_writable(LOCK_DIRECTORY)) {
  854. $files = glob(LOCK_DIRECTORY . "/*.lock");
  855. if ($files) {
  856. foreach ($files as $file) {
  857. if (!file_is_locked(basename($file)) && time() - filemtime($file) > 86400*2) {
  858. unlink($file);
  859. ++$num_deleted;
  860. }
  861. }
  862. }
  863. }
  864. if ($debug) _debug("Removed $num_deleted old lock files.");
  865. }
  866. function expire_cached_files($debug) {
  867. foreach (array("simplepie", "images", "export", "upload") as $dir) {
  868. $cache_dir = CACHE_DIR . "/$dir";
  869. // if ($debug) _debug("Expiring $cache_dir");
  870. $num_deleted = 0;
  871. if (is_writable($cache_dir)) {
  872. $files = glob("$cache_dir/*");
  873. if ($files) {
  874. foreach ($files as $file) {
  875. if (time() - filemtime($file) > 86400*7) {
  876. unlink($file);
  877. ++$num_deleted;
  878. }
  879. }
  880. }
  881. }
  882. if ($debug) _debug("$cache_dir: removed $num_deleted files.");
  883. }
  884. }
  885. /**
  886. * Source: http://www.php.net/manual/en/function.parse-url.php#104527
  887. * Returns the url query as associative array
  888. *
  889. * @param string query
  890. * @return array params
  891. */
  892. function convertUrlQuery($query) {
  893. $queryParts = explode('&', $query);
  894. $params = array();
  895. foreach ($queryParts as $param) {
  896. $item = explode('=', $param);
  897. $params[$item[0]] = $item[1];
  898. }
  899. return $params;
  900. }
  901. function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
  902. $matches = array();
  903. foreach ($filters as $filter) {
  904. $match_any_rule = $filter["match_any_rule"];
  905. $inverse = $filter["inverse"];
  906. $filter_match = false;
  907. foreach ($filter["rules"] as $rule) {
  908. $match = false;
  909. $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
  910. $rule_inverse = $rule["inverse"];
  911. if (!$reg_exp)
  912. continue;
  913. switch ($rule["type"]) {
  914. case "title":
  915. $match = @preg_match("/$reg_exp/i", $title);
  916. break;
  917. case "content":
  918. // we don't need to deal with multiline regexps
  919. $content = preg_replace("/[\r\n\t]/", "", $content);
  920. $match = @preg_match("/$reg_exp/i", $content);
  921. break;
  922. case "both":
  923. // we don't need to deal with multiline regexps
  924. $content = preg_replace("/[\r\n\t]/", "", $content);
  925. $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $content));
  926. break;
  927. case "link":
  928. $match = @preg_match("/$reg_exp/i", $link);
  929. break;
  930. case "author":
  931. $match = @preg_match("/$reg_exp/i", $author);
  932. break;
  933. case "tag":
  934. foreach ($tags as $tag) {
  935. if (@preg_match("/$reg_exp/i", $tag)) {
  936. $match = true;
  937. break;
  938. }
  939. }
  940. break;
  941. }
  942. if ($rule_inverse) $match = !$match;
  943. if ($match_any_rule) {
  944. if ($match) {
  945. $filter_match = true;
  946. break;
  947. }
  948. } else {
  949. $filter_match = $match;
  950. if (!$match) {
  951. break;
  952. }
  953. }
  954. }
  955. if ($inverse) $filter_match = !$filter_match;
  956. if ($filter_match) {
  957. foreach ($filter["actions"] AS $action) {
  958. array_push($matches, $action);
  959. // if Stop action encountered, perform no further processing
  960. if ($action["type"] == "stop") return $matches;
  961. }
  962. }
  963. }
  964. return $matches;
  965. }
  966. function find_article_filter($filters, $filter_name) {
  967. foreach ($filters as $f) {
  968. if ($f["type"] == $filter_name) {
  969. return $f;
  970. };
  971. }
  972. return false;
  973. }
  974. function find_article_filters($filters, $filter_name) {
  975. $results = array();
  976. foreach ($filters as $f) {
  977. if ($f["type"] == $filter_name) {
  978. array_push($results, $f);
  979. };
  980. }
  981. return $results;
  982. }
  983. function calculate_article_score($filters) {
  984. $score = 0;
  985. foreach ($filters as $f) {
  986. if ($f["type"] == "score") {
  987. $score += $f["param"];
  988. };
  989. }
  990. return $score;
  991. }
  992. function labels_contains_caption($labels, $caption) {
  993. foreach ($labels as $label) {
  994. if ($label[1] == $caption) {
  995. return true;
  996. }
  997. }
  998. return false;
  999. }
  1000. function assign_article_to_label_filters($id, $filters, $owner_uid, $article_labels) {
  1001. foreach ($filters as $f) {
  1002. if ($f["type"] == "label") {
  1003. if (!labels_contains_caption($article_labels, $f["param"])) {
  1004. label_add_article($id, $f["param"], $owner_uid);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. function make_guid_from_title($title) {
  1010. return preg_replace("/[ \"\',.:;]/", "-",
  1011. mb_strtolower(strip_tags($title), 'utf-8'));
  1012. }
  1013. /* function verify_feed_xml($feed_data) {
  1014. libxml_use_internal_errors(true);
  1015. $doc = new DOMDocument();
  1016. $doc->loadXML($feed_data);
  1017. $error = libxml_get_last_error();
  1018. libxml_clear_errors();
  1019. return $error;
  1020. } */
  1021. function housekeeping_common($debug) {
  1022. expire_cached_files($debug);
  1023. expire_lock_files($debug);
  1024. expire_error_log($debug);
  1025. $count = update_feedbrowser_cache();
  1026. _debug("Feedbrowser updated, $count feeds processed.");
  1027. purge_orphans( true);
  1028. $rc = cleanup_tags( 14, 50000);
  1029. _debug("Cleaned $rc cached tags.");
  1030. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_HOUSE_KEEPING, "hook_house_keeping", "");
  1031. }
  1032. ?>