Entry.php 14 KB

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