Entry.php 13 KB

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