Entry.php 11 KB

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