4
0

Entry.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. class FreshRSS_Entry extends Minz_Model {
  3. const STATE_READ = 1;
  4. const STATE_NOT_READ = 2;
  5. const STATE_ALL = 3;
  6. const STATE_FAVORITE = 4;
  7. const STATE_NOT_FAVORITE = 8;
  8. /**
  9. * @var string
  10. */
  11. private $id = '0';
  12. /**
  13. * @var string
  14. */
  15. private $guid;
  16. private $title;
  17. private $authors;
  18. private $content;
  19. private $link;
  20. private $date;
  21. private $date_added = 0; //In microseconds
  22. /**
  23. * @var string
  24. */
  25. private $hash = '';
  26. /**
  27. * @var bool|null
  28. */
  29. private $is_read;
  30. private $is_favorite;
  31. /**
  32. * @var int
  33. */
  34. private $feedId;
  35. /**
  36. * @var FreshRSS_Feed|null
  37. */
  38. private $feed;
  39. private $tags;
  40. public function __construct(int $feedId = 0, string $guid = '', string $title = '', string $authors = '', string $content = '',
  41. string $link = '', $pubdate = 0, bool $is_read = false, bool $is_favorite = false, string $tags = '') {
  42. $this->_title($title);
  43. $this->_authors($authors);
  44. $this->_content($content);
  45. $this->_link($link);
  46. $this->_date($pubdate);
  47. $this->_isRead($is_read);
  48. $this->_isFavorite($is_favorite);
  49. $this->_feedId($feedId);
  50. $this->_tags($tags);
  51. $this->_guid($guid);
  52. }
  53. public function id(): string {
  54. return $this->id;
  55. }
  56. public function guid(): string {
  57. return $this->guid;
  58. }
  59. public function title(): string {
  60. return $this->title == '' ? $this->guid() : $this->title;
  61. }
  62. public function author(): string {
  63. //Deprecated
  64. return $this->authors(true);
  65. }
  66. public function authors(bool $asString = false) {
  67. if ($asString) {
  68. return $this->authors == null ? '' : ';' . implode('; ', $this->authors);
  69. } else {
  70. return $this->authors;
  71. }
  72. }
  73. public function content(): string {
  74. return $this->content;
  75. }
  76. public function enclosures(bool $searchBodyImages = false): array {
  77. $results = [];
  78. try {
  79. $searchEnclosures = strpos($this->content, '<p class="enclosure-content') !== false;
  80. $searchBodyImages &= (stripos($this->content, '<img') !== false);
  81. $xpath = null;
  82. if ($searchEnclosures || $searchBodyImages) {
  83. $dom = new DOMDocument();
  84. $dom->loadHTML('<?xml version="1.0" encoding="UTF-8" ?>' . $this->content, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
  85. $xpath = new DOMXpath($dom);
  86. }
  87. if ($searchEnclosures) {
  88. $enclosures = $xpath->query('//div[@class="enclosure"]/p[@class="enclosure-content"]/*[@src]');
  89. foreach ($enclosures as $enclosure) {
  90. $results[] = [
  91. 'url' => $enclosure->getAttribute('src'),
  92. 'type' => $enclosure->getAttribute('data-type'),
  93. 'length' => $enclosure->getAttribute('data-length'),
  94. ];
  95. }
  96. }
  97. if ($searchBodyImages) {
  98. $images = $xpath->query('//img');
  99. foreach ($images as $img) {
  100. $src = $img->getAttribute('src');
  101. if ($src == null) {
  102. $src = $img->getAttribute('data-src');
  103. }
  104. if ($src != null) {
  105. $results[] = [
  106. 'url' => $src,
  107. 'alt' => $img->getAttribute('alt'),
  108. ];
  109. }
  110. }
  111. }
  112. return $results;
  113. } catch (Exception $ex) {
  114. return $results;
  115. }
  116. }
  117. /**
  118. * @return array<string,string>|null
  119. */
  120. public function thumbnail() {
  121. foreach ($this->enclosures(true) as $enclosure) {
  122. if (!empty($enclosure['url']) && empty($enclosure['type'])) {
  123. return $enclosure;
  124. }
  125. }
  126. return null;
  127. }
  128. public function link(): string {
  129. return $this->link;
  130. }
  131. public function date(bool $raw = false) {
  132. if ($raw) {
  133. return $this->date;
  134. }
  135. return timestamptodate($this->date);
  136. }
  137. public function machineReadableDate(): string {
  138. return @date (DATE_ATOM, $this->date);
  139. }
  140. public function dateAdded(bool $raw = false, bool $microsecond = false) {
  141. if ($raw) {
  142. if ($microsecond) {
  143. return $this->date_added;
  144. } else {
  145. return intval(substr($this->date_added, 0, -6));
  146. }
  147. } else {
  148. $date = intval(substr($this->date_added, 0, -6));
  149. return timestamptodate($date);
  150. }
  151. }
  152. public function isRead() {
  153. return $this->is_read;
  154. }
  155. public function isFavorite() {
  156. return $this->is_favorite;
  157. }
  158. public function feed($object = false) {
  159. if ($object) {
  160. if ($this->feed == null) {
  161. $feedDAO = FreshRSS_Factory::createFeedDao();
  162. $this->feed = $feedDAO->searchById($this->feedId);
  163. }
  164. return $this->feed;
  165. } else {
  166. return $this->feedId;
  167. }
  168. }
  169. public function tags($asString = false) {
  170. if ($asString) {
  171. return $this->tags == null ? '' : '#' . implode(' #', $this->tags);
  172. } else {
  173. return $this->tags;
  174. }
  175. }
  176. public function hash(): string {
  177. if ($this->hash == '') {
  178. //Do not include $this->date because it may be automatically generated when lacking
  179. $this->hash = md5($this->link . $this->title . $this->authors(true) . $this->content . $this->tags(true));
  180. }
  181. return $this->hash;
  182. }
  183. public function _hash(string $value) {
  184. $value = trim($value);
  185. if (ctype_xdigit($value)) {
  186. $this->hash = substr($value, 0, 32);
  187. }
  188. return $this->hash;
  189. }
  190. public function _id($value) {
  191. $this->id = $value;
  192. if ($this->date_added == 0) {
  193. $this->date_added = $value;
  194. }
  195. }
  196. public function _guid(string $value) {
  197. if ($value == '') {
  198. $value = $this->link;
  199. if ($value == '') {
  200. $value = $this->hash();
  201. }
  202. }
  203. $this->guid = $value;
  204. }
  205. public function _title(string $value) {
  206. $this->hash = '';
  207. $this->title = trim($value);
  208. }
  209. public function _author(string $value) {
  210. //Deprecated
  211. $this->_authors($value);
  212. }
  213. public function _authors($value) {
  214. $this->hash = '';
  215. if (!is_array($value)) {
  216. if (strpos($value, ';') !== false) {
  217. $value = preg_split('/\s*[;]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  218. } else {
  219. $value = preg_split('/\s*[,]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  220. }
  221. }
  222. $this->authors = $value;
  223. }
  224. public function _content(string $value) {
  225. $this->hash = '';
  226. $this->content = $value;
  227. }
  228. public function _link(string $value) {
  229. $this->hash = '';
  230. $this->link = $value;
  231. }
  232. public function _date($value) {
  233. $this->hash = '';
  234. $value = intval($value);
  235. $this->date = $value > 1 ? $value : time();
  236. }
  237. public function _dateAdded($value, bool $microsecond = false) {
  238. if ($microsecond) {
  239. $this->date_added = $value;
  240. } else {
  241. $this->date_added = $value * 1000000;
  242. }
  243. }
  244. public function _isRead($value) {
  245. $this->is_read = $value === null ? null : (bool)$value;
  246. }
  247. public function _isFavorite($value) {
  248. $this->is_favorite = $value;
  249. }
  250. public function _feed($value) {
  251. if ($value != null) {
  252. $this->feed = $value;
  253. $this->feedId = $this->feed->id();
  254. }
  255. }
  256. private function _feedId($value) {
  257. $this->feed = null;
  258. $this->feedId = intval($value);
  259. }
  260. public function _tags($value) {
  261. $this->hash = '';
  262. if (!is_array($value)) {
  263. $value = preg_split('/\s*[#,]\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
  264. }
  265. $this->tags = $value;
  266. }
  267. public function matches(FreshRSS_BooleanSearch $booleanSearch): bool {
  268. if (count($booleanSearch->searches()) <= 0) {
  269. return true;
  270. }
  271. foreach ($booleanSearch->searches() as $filter) {
  272. $ok = true;
  273. if ($filter->getMinDate()) {
  274. $ok &= strnatcmp($this->id, $filter->getMinDate() . '000000') >= 0;
  275. }
  276. if ($ok && $filter->getNotMinDate()) {
  277. $ok &= strnatcmp($this->id, $filter->getNotMinDate() . '000000') < 0;
  278. }
  279. if ($ok && $filter->getMaxDate()) {
  280. $ok &= strnatcmp($this->id, $filter->getMaxDate() . '000000') <= 0;
  281. }
  282. if ($ok && $filter->getNotMaxDate()) {
  283. $ok &= strnatcmp($this->id, $filter->getNotMaxDate() . '000000') > 0;
  284. }
  285. if ($ok && $filter->getMinPubdate()) {
  286. $ok &= $this->date >= $filter->getMinPubdate();
  287. }
  288. if ($ok && $filter->getNotMinPubdate()) {
  289. $ok &= $this->date < $filter->getNotMinPubdate();
  290. }
  291. if ($ok && $filter->getMaxPubdate()) {
  292. $ok &= $this->date <= $filter->getMaxPubdate();
  293. }
  294. if ($ok && $filter->getNotMaxPubdate()) {
  295. $ok &= $this->date > $filter->getNotMaxPubdate();
  296. }
  297. if ($ok && $filter->getFeedIds()) {
  298. $ok &= in_array($this->feedId, $filter->getFeedIds());
  299. }
  300. if ($ok && $filter->getNotFeedIds()) {
  301. $ok &= !in_array($this->feedId, $filter->getFeedIds());
  302. }
  303. if ($ok && $filter->getAuthor()) {
  304. foreach ($filter->getAuthor() as $author) {
  305. $ok &= stripos(implode(';', $this->authors), $author) !== false;
  306. }
  307. }
  308. if ($ok && $filter->getNotAuthor()) {
  309. foreach ($filter->getNotAuthor() as $author) {
  310. $ok &= stripos(implode(';', $this->authors), $author) === false;
  311. }
  312. }
  313. if ($ok && $filter->getIntitle()) {
  314. foreach ($filter->getIntitle() as $title) {
  315. $ok &= stripos($this->title, $title) !== false;
  316. }
  317. }
  318. if ($ok && $filter->getNotIntitle()) {
  319. foreach ($filter->getNotIntitle() as $title) {
  320. $ok &= stripos($this->title, $title) === false;
  321. }
  322. }
  323. if ($ok && $filter->getTags()) {
  324. foreach ($filter->getTags() as $tag2) {
  325. $found = false;
  326. foreach ($this->tags as $tag1) {
  327. if (strcasecmp($tag1, $tag2) === 0) {
  328. $found = true;
  329. }
  330. }
  331. $ok &= $found;
  332. }
  333. }
  334. if ($ok && $filter->getNotTags()) {
  335. foreach ($filter->getNotTags() as $tag2) {
  336. $found = false;
  337. foreach ($this->tags as $tag1) {
  338. if (strcasecmp($tag1, $tag2) === 0) {
  339. $found = true;
  340. }
  341. }
  342. $ok &= !$found;
  343. }
  344. }
  345. if ($ok && $filter->getInurl()) {
  346. foreach ($filter->getInurl() as $url) {
  347. $ok &= stripos($this->link, $url) !== false;
  348. }
  349. }
  350. if ($ok && $filter->getNotInurl()) {
  351. foreach ($filter->getNotInurl() as $url) {
  352. $ok &= stripos($this->link, $url) === false;
  353. }
  354. }
  355. if ($ok && $filter->getSearch()) {
  356. foreach ($filter->getSearch() as $needle) {
  357. $ok &= (stripos($this->title, $needle) !== false || stripos($this->content, $needle) !== false);
  358. }
  359. }
  360. if ($ok && $filter->getNotSearch()) {
  361. foreach ($filter->getNotSearch() as $needle) {
  362. $ok &= (stripos($this->title, $needle) === false && stripos($this->content, $needle) === false);
  363. }
  364. }
  365. if ($ok) {
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. public function applyFilterActions(array $titlesAsRead = []) {
  372. if ($this->feed != null) {
  373. if ($this->feed->attributes('read_upon_reception') ||
  374. ($this->feed->attributes('read_upon_reception') === null && FreshRSS_Context::$user_conf->mark_when['reception'])) {
  375. $this->_isRead(true);
  376. }
  377. if (isset($titlesAsRead[$this->title()])) {
  378. Minz_Log::debug('Mark title as read: ' . $this->title());
  379. $this->_isRead(true);
  380. }
  381. foreach ($this->feed->filterActions() as $filterAction) {
  382. if ($this->matches($filterAction->booleanSearch())) {
  383. foreach ($filterAction->actions() as $action) {
  384. switch ($action) {
  385. case 'read':
  386. $this->_isRead(true);
  387. break;
  388. case 'star':
  389. $this->_isFavorite(true);
  390. break;
  391. case 'label':
  392. //TODO: Implement more actions
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }
  400. public function isDay(int $day, int $today): bool {
  401. $date = $this->dateAdded(true);
  402. switch ($day) {
  403. case FreshRSS_Days::TODAY:
  404. $tomorrow = $today + 86400;
  405. return $date >= $today && $date < $tomorrow;
  406. case FreshRSS_Days::YESTERDAY:
  407. $yesterday = $today - 86400;
  408. return $date >= $yesterday && $date < $today;
  409. case FreshRSS_Days::BEFORE_YESTERDAY:
  410. $yesterday = $today - 86400;
  411. return $date < $yesterday;
  412. default:
  413. return false;
  414. }
  415. }
  416. public static function getContentByParsing(string $url, string $path, array $attributes = array(), int $maxRedirs = 3): string {
  417. $limits = FreshRSS_Context::$system_conf->limits;
  418. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  419. if (FreshRSS_Context::$system_conf->simplepie_syslog_enabled) {
  420. syslog(LOG_INFO, 'FreshRSS GET ' . SimplePie_Misc::url_remove_credentials($url));
  421. }
  422. $ch = curl_init();
  423. curl_setopt_array($ch, [
  424. CURLOPT_URL => $url,
  425. CURLOPT_REFERER => SimplePie_Misc::url_remove_credentials($url),
  426. CURLOPT_HTTPHEADER => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
  427. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  428. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  429. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  430. //CURLOPT_FAILONERROR => true;
  431. CURLOPT_MAXREDIRS => 4,
  432. CURLOPT_RETURNTRANSFER => true,
  433. CURLOPT_FOLLOWLOCATION => true,
  434. CURLOPT_ENCODING => '', //Enable all encodings
  435. ]);
  436. curl_setopt_array($ch, FreshRSS_Context::$system_conf->curl_options);
  437. if (isset($attributes['curl_params']) && is_array($attributes['curl_params'])) {
  438. curl_setopt_array($ch, $attributes['curl_params']);
  439. }
  440. if (isset($attributes['ssl_verify'])) {
  441. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
  442. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
  443. if (!$attributes['ssl_verify']) {
  444. curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'DEFAULT@SECLEVEL=1');
  445. }
  446. }
  447. $html = curl_exec($ch);
  448. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  449. $c_error = curl_error($ch);
  450. curl_close($ch);
  451. if ($c_status != 200 || $c_error != '') {
  452. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  453. }
  454. if (is_string($html) && strlen($html) > 0) {
  455. require_once(LIB_PATH . '/lib_phpQuery.php');
  456. /**
  457. * @var phpQueryObject @doc
  458. */
  459. $doc = phpQuery::newDocument($html);
  460. if ($maxRedirs > 0) {
  461. //Follow any HTML redirection
  462. /**
  463. * @var phpQueryObject @metas
  464. */
  465. $metas = $doc->find('meta[http-equiv][content]');
  466. foreach ($metas as $meta) {
  467. if (strtolower(trim($meta->getAttribute('http-equiv'))) === 'refresh') {
  468. $refresh = preg_replace('/^[0-9.; ]*\s*(url\s*=)?\s*/i', '', trim($meta->getAttribute('content')));
  469. $refresh = SimplePie_Misc::absolutize_url($refresh, $url);
  470. if ($refresh != false && $refresh !== $url) {
  471. phpQuery::unloadDocuments();
  472. return self::getContentByParsing($refresh, $path, $attributes, $maxRedirs - 1);
  473. }
  474. }
  475. }
  476. }
  477. /**
  478. * @var phpQueryObject @content
  479. */
  480. $content = $doc->find($path);
  481. $html = trim(sanitizeHTML($content->__toString(), $url));
  482. phpQuery::unloadDocuments();
  483. return $html;
  484. } else {
  485. throw new Exception();
  486. }
  487. }
  488. public function loadCompleteContent(bool $force = false): bool {
  489. // Gestion du contenu
  490. // Trying to fetch full article content even when feeds do not propose it
  491. $feed = $this->feed(true);
  492. if ($feed != null && trim($feed->pathEntries()) != '') {
  493. $entryDAO = FreshRSS_Factory::createEntryDao();
  494. $entry = $force ? null : $entryDAO->searchByGuid($this->feedId, $this->guid);
  495. if ($entry) {
  496. // l’article existe déjà en BDD, en se contente de recharger ce contenu
  497. $this->content = $entry->content();
  498. } else {
  499. try {
  500. // l’article n’est pas en BDD, on va le chercher sur le site
  501. $fullContent = self::getContentByParsing(
  502. htmlspecialchars_decode($this->link(), ENT_QUOTES),
  503. $feed->pathEntries(),
  504. $feed->attributes()
  505. );
  506. if ('' !== $fullContent) {
  507. $fullContent = "<!-- FULLCONTENT start //-->{$fullContent}<!-- FULLCONTENT end //-->";
  508. $originalContent = preg_replace('#<!-- FULLCONTENT start //-->.*<!-- FULLCONTENT end //-->#s', '', $this->content());
  509. switch ($feed->attributes('content_action')) {
  510. case 'prepend':
  511. $this->content = $fullContent . $originalContent;
  512. break;
  513. case 'append':
  514. $this->content = $originalContent . $fullContent;
  515. break;
  516. case 'replace':
  517. default:
  518. $this->content = $fullContent;
  519. break;
  520. }
  521. return true;
  522. }
  523. } catch (Exception $e) {
  524. // rien à faire, on garde l’ancien contenu(requête a échoué)
  525. Minz_Log::warning($e->getMessage());
  526. }
  527. }
  528. }
  529. return false;
  530. }
  531. public function toArray(): array {
  532. return array(
  533. 'id' => $this->id(),
  534. 'guid' => $this->guid(),
  535. 'title' => $this->title(),
  536. 'author' => $this->authors(true),
  537. 'content' => $this->content(),
  538. 'link' => $this->link(),
  539. 'date' => $this->date(true),
  540. 'hash' => $this->hash(),
  541. 'is_read' => $this->isRead(),
  542. 'is_favorite' => $this->isFavorite(),
  543. 'id_feed' => $this->feed(),
  544. 'tags' => $this->tags(true),
  545. );
  546. }
  547. }