Entry.php 8.1 KB

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