Entry.php 12 KB

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