Entry.php 12 KB

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