Entry.php 7.8 KB

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