Entry.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 isDay($day, $today) {
  181. $date = $this->dateAdded(true);
  182. switch ($day) {
  183. case FreshRSS_Days::TODAY:
  184. $tomorrow = $today + 86400;
  185. return $date >= $today && $date < $tomorrow;
  186. case FreshRSS_Days::YESTERDAY:
  187. $yesterday = $today - 86400;
  188. return $date >= $yesterday && $date < $today;
  189. case FreshRSS_Days::BEFORE_YESTERDAY:
  190. $yesterday = $today - 86400;
  191. return $date < $yesterday;
  192. default:
  193. return false;
  194. }
  195. }
  196. private static function get_content_by_parsing($url, $path, $attributes = array()) {
  197. require_once(LIB_PATH . '/lib_phpQuery.php');
  198. $system_conf = Minz_Configuration::get('system');
  199. $limits = $system_conf->limits;
  200. $feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
  201. if ($system_conf->simplepie_syslog_enabled) {
  202. prepareSyslog();
  203. syslog(LOG_INFO, 'FreshRSS GET ' . SimplePie_Misc::url_remove_credentials($url));
  204. }
  205. $ch = curl_init();
  206. curl_setopt_array($ch, array(
  207. CURLOPT_URL => $url,
  208. CURLOPT_REFERER => SimplePie_Misc::url_remove_credentials($url),
  209. CURLOPT_HTTPHEADER => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
  210. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  211. CURLOPT_CONNECTTIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  212. CURLOPT_TIMEOUT => $feed_timeout > 0 ? $feed_timeout : $limits['timeout'],
  213. //CURLOPT_FAILONERROR => true;
  214. CURLOPT_MAXREDIRS => 4,
  215. CURLOPT_RETURNTRANSFER => true,
  216. ));
  217. if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
  218. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
  219. }
  220. if (defined('CURLOPT_ENCODING')) {
  221. curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
  222. }
  223. curl_setopt_array($ch, $system_conf->curl_options);
  224. if (isset($attributes['ssl_verify'])) {
  225. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
  226. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
  227. }
  228. $html = curl_exec($ch);
  229. $c_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  230. $c_error = curl_error($ch);
  231. curl_close($ch);
  232. if ($c_status != 200 || $c_error != '') {
  233. Minz_Log::warning('Error fetching content: HTTP code ' . $c_status . ': ' . $c_error . ' ' . $url);
  234. }
  235. if ($html) {
  236. $doc = phpQuery::newDocument($html);
  237. $content = $doc->find($path);
  238. foreach (pq('img[data-src]') as $img) {
  239. $imgP = pq($img);
  240. $dataSrc = $imgP->attr('data-src');
  241. if (strlen($dataSrc) > 4) {
  242. $imgP->attr('src', $dataSrc);
  243. $imgP->removeAttr('data-src');
  244. }
  245. }
  246. return trim(sanitizeHTML($content->__toString(), $url));
  247. } else {
  248. throw new Exception();
  249. }
  250. }
  251. public function loadCompleteContent() {
  252. // Gestion du contenu
  253. // On cherche à récupérer les articles en entier... même si le flux ne le propose pas
  254. $feed = $this->feed(true);
  255. if ($feed != null && trim($feed->pathEntries()) != '') {
  256. $entryDAO = FreshRSS_Factory::createEntryDao();
  257. $entry = $entryDAO->searchByGuid($this->feedId, $this->guid);
  258. if ($entry) {
  259. // l'article existe déjà en BDD, en se contente de recharger ce contenu
  260. $this->content = $entry->content();
  261. } else {
  262. try {
  263. // l'article n'est pas en BDD, on va le chercher sur le site
  264. $fullContent = self::get_content_by_parsing(
  265. htmlspecialchars_decode($this->link(), ENT_QUOTES),
  266. $feed->pathEntries(),
  267. $feed->attributes()
  268. );
  269. if ($fullContent != '') {
  270. $this->content = $fullContent;
  271. }
  272. } catch (Exception $e) {
  273. // rien à faire, on garde l'ancien contenu(requête a échoué)
  274. Minz_Log::warning($e->getMessage());
  275. }
  276. }
  277. }
  278. }
  279. public function toArray() {
  280. return array(
  281. 'id' => $this->id(),
  282. 'guid' => $this->guid(),
  283. 'title' => $this->title(),
  284. 'author' => $this->authors(true),
  285. 'content' => $this->content(),
  286. 'link' => $this->link(),
  287. 'date' => $this->date(true),
  288. 'hash' => $this->hash(),
  289. 'is_read' => $this->isRead(),
  290. 'is_favorite' => $this->isFavorite(),
  291. 'id_feed' => $this->feed(),
  292. 'tags' => $this->tags(true),
  293. );
  294. }
  295. }