Entry.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 $author;
  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 = '', $author = '', $content = '',
  22. $link = '', $pubdate = 0, $is_read = false, $is_favorite = false, $tags = '') {
  23. $this->_title($title);
  24. $this->_author($author);
  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. $tags = mb_strcut($tags, 0, 1023, 'UTF-8');
  32. $this->_tags(preg_split('/[\s#]/', $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. return $this->author === null ? '' : $this->author;
  46. }
  47. public function content() {
  48. return $this->content;
  49. }
  50. public function link() {
  51. return $this->link;
  52. }
  53. public function date($raw = false) {
  54. if ($raw) {
  55. return $this->date;
  56. } else {
  57. return timestamptodate($this->date);
  58. }
  59. }
  60. public function dateAdded($raw = false) {
  61. $date = intval(substr($this->id, 0, -6));
  62. if ($raw) {
  63. return $date;
  64. } else {
  65. return timestamptodate($date);
  66. }
  67. }
  68. public function isRead() {
  69. return $this->is_read;
  70. }
  71. public function isFavorite() {
  72. return $this->is_favorite;
  73. }
  74. public function feed($object = false) {
  75. if ($object) {
  76. if ($this->feed == null) {
  77. $feedDAO = FreshRSS_Factory::createFeedDao();
  78. $this->feed = $feedDAO->searchById($this->feedId);
  79. }
  80. return $this->feed;
  81. } else {
  82. return $this->feedId;
  83. }
  84. }
  85. public function tags($inString = false) {
  86. if ($inString) {
  87. return empty($this->tags) ? '' : '#' . implode(' #', $this->tags);
  88. } else {
  89. return $this->tags;
  90. }
  91. }
  92. public function hash() {
  93. if ($this->hash === null) {
  94. //Do not include $this->date because it may be automatically generated when lacking
  95. $this->hash = md5($this->link . $this->title . $this->author . $this->content . $this->tags(true));
  96. }
  97. return $this->hash;
  98. }
  99. public function _hash($value) {
  100. $value = trim($value);
  101. if (ctype_xdigit($value)) {
  102. $this->hash = substr($value, 0, 32);
  103. }
  104. return $this->hash;
  105. }
  106. public function _id($value) {
  107. $this->id = $value;
  108. }
  109. public function _guid($value) {
  110. if ($value == '') {
  111. $value = $this->link;
  112. if ($value == '') {
  113. $value = $this->hash();
  114. }
  115. }
  116. $this->guid = $value;
  117. }
  118. public function _title($value) {
  119. $this->hash = null;
  120. $this->title = mb_strcut($value, 0, 255, 'UTF-8');
  121. }
  122. public function _author($value) {
  123. $this->hash = null;
  124. $this->author = mb_strcut($value, 0, 255, 'UTF-8');
  125. }
  126. public function _content($value) {
  127. $this->hash = null;
  128. $this->content = $value;
  129. }
  130. public function _link($value) {
  131. $this->hash = null;
  132. $this->link = $value;
  133. }
  134. public function _date($value) {
  135. $this->hash = null;
  136. $value = intval($value);
  137. $this->date = $value > 1 ? $value : time();
  138. }
  139. public function _isRead($value) {
  140. $this->is_read = $value === null ? null : (bool)$value;
  141. }
  142. public function _isFavorite($value) {
  143. $this->is_favorite = $value;
  144. }
  145. public function _feed($value) {
  146. if ($value != null) {
  147. $this->feed = $value;
  148. $this->feedId = $this->feed->id();
  149. }
  150. }
  151. private function _feedId($value) {
  152. $this->feed = null;
  153. $this->feedId = $value;
  154. }
  155. public function _tags($value) {
  156. $this->hash = null;
  157. if (!is_array($value)) {
  158. $value = array($value);
  159. }
  160. foreach ($value as $key => $t) {
  161. if (!$t) {
  162. unset($value[$key]);
  163. }
  164. }
  165. $this->tags = $value;
  166. }
  167. public function isDay($day, $today) {
  168. $date = $this->dateAdded(true);
  169. switch ($day) {
  170. case FreshRSS_Days::TODAY:
  171. $tomorrow = $today + 86400;
  172. return $date >= $today && $date < $tomorrow;
  173. case FreshRSS_Days::YESTERDAY:
  174. $yesterday = $today - 86400;
  175. return $date >= $yesterday && $date < $today;
  176. case FreshRSS_Days::BEFORE_YESTERDAY:
  177. $yesterday = $today - 86400;
  178. return $date < $yesterday;
  179. default:
  180. return false;
  181. }
  182. }
  183. private static function get_content_by_parsing($url, $path, $attributes = array()) {
  184. require_once(LIB_PATH . '/lib_phpQuery.php');
  185. $system_conf = Minz_Configuration::get('system');
  186. $limits = $system_conf->limits;
  187. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  188. if ($system_conf->simplepie_syslog_enabled) {
  189. syslog(LOG_INFO, 'FreshRSS GET ' . SimplePie_Misc::url_remove_credentials($url));
  190. }
  191. $ch = curl_init();
  192. curl_setopt_array($ch, array(
  193. CURLOPT_URL => $url,
  194. CURLOPT_REFERER => SimplePie_Misc::url_remove_credentials($url),
  195. CURLOPT_HTTPHEADER => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
  196. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  197. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  198. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  199. //CURLOPT_FAILONERROR => true;
  200. CURLOPT_MAXREDIRS => 4,
  201. CURLOPT_RETURNTRANSFER => true,
  202. ));
  203. if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
  204. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
  205. }
  206. if (defined('CURLOPT_ENCODING')) {
  207. curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
  208. }
  209. curl_setopt_array($ch, $system_conf->curl_options);
  210. if (isset($attributes['ssl_verify'])) {
  211. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
  212. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
  213. }
  214. $html = curl_exec($ch);
  215. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  216. $c_error = curl_error($ch);
  217. curl_close($ch);
  218. if ($c_status != 200 || $c_error != '') {
  219. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  220. }
  221. if ($html) {
  222. $doc = phpQuery::newDocument($html);
  223. $content = $doc->find($path);
  224. foreach (pq('img[data-src]') as $img) {
  225. $imgP = pq($img);
  226. $dataSrc = $imgP->attr('data-src');
  227. if (strlen($dataSrc) > 4) {
  228. $imgP->attr('src', $dataSrc);
  229. $imgP->removeAttr('data-src');
  230. }
  231. }
  232. return trim(sanitizeHTML($content->__toString(), $url));
  233. } else {
  234. throw new Exception();
  235. }
  236. }
  237. public function loadCompleteContent() {
  238. // Gestion du contenu
  239. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  240. $feed = $this->feed(true);
  241. if ($feed != null && trim($feed->pathEntries()) != '') {
  242. $entryDAO = FreshRSS_Factory::createEntryDao();
  243. $entry = $entryDAO->searchByGuid($this->feedId, $this->guid);
  244. if ($entry) {
  245. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  246. $this->content = $entry->content();
  247. } else {
  248. try {
  249. // l'article n'est pas en BDD, on va le chercher sur le site
  250. $fullContent = self::get_content_by_parsing(
  251. htmlspecialchars_decode($this->link(), ENT_QUOTES),
  252. $feed->pathEntries(),
  253. $feed->attributes()
  254. );
  255. if ($fullContent != '') {
  256. $this->content = $fullContent;
  257. }
  258. } catch (Exception $e) {
  259. // rien à faire, on garde l'ancien contenu(requête a échoué)
  260. Minz_Log::warning($e->getMessage());
  261. }
  262. }
  263. }
  264. }
  265. public function toArray() {
  266. return array(
  267. 'id' => $this->id(),
  268. 'guid' => $this->guid(),
  269. 'title' => $this->title(),
  270. 'author' => $this->author(),
  271. 'content' => $this->content(),
  272. 'link' => $this->link(),
  273. 'date' => $this->date(true),
  274. 'hash' => $this->hash(),
  275. 'is_read' => $this->isRead(),
  276. 'is_favorite' => $this->isFavorite(),
  277. 'id_feed' => $this->feed(),
  278. 'tags' => $this->tags(true),
  279. );
  280. }
  281. }