Entry.php 15 KB

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