lib_rss.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. if (!function_exists('json_decode')) {
  3. require_once('JSON.php');
  4. function json_decode($var) {
  5. $JSON = new Services_JSON;
  6. return (array)($JSON->decode($var));
  7. }
  8. }
  9. if (!function_exists('json_encode')) {
  10. require_once('JSON.php');
  11. function json_encode($var) {
  12. $JSON = new Services_JSON;
  13. return $JSON->encodeUnsafe($var);
  14. }
  15. }
  16. //<Auto-loading>
  17. function classAutoloader($class) {
  18. if (strpos($class, 'FreshRSS') === 0) {
  19. $components = explode('_', $class);
  20. switch (count($components)) {
  21. case 1:
  22. include(APP_PATH . '/' . $components[0] . '.php');
  23. return;
  24. case 2:
  25. include(APP_PATH . '/Models/' . $components[1] . '.php');
  26. return;
  27. case 3: //Controllers, Exceptions
  28. include(APP_PATH . '/' . $components[2] . 's/' . $components[1] . $components[2] . '.php');
  29. return;
  30. }
  31. } elseif (strpos($class, 'Minz') === 0) {
  32. include(LIB_PATH . '/' . str_replace('_', '/', $class) . '.php');
  33. } elseif (strpos($class, 'SimplePie') === 0) {
  34. include(LIB_PATH . '/SimplePie/' . str_replace('_', '/', $class) . '.php');
  35. }
  36. }
  37. spl_autoload_register('classAutoloader');
  38. //</Auto-loading>
  39. function checkUrl($url) {
  40. if (empty ($url)) {
  41. return '';
  42. }
  43. if (!preg_match ('#^https?://#i', $url)) {
  44. $url = 'http://' . $url;
  45. }
  46. if (filter_var($url, FILTER_VALIDATE_URL) ||
  47. (version_compare(PHP_VERSION, '5.3.3', '<') && (strpos($url, '-') > 0) && //PHP bug #51192
  48. ($url === filter_var($url, FILTER_SANITIZE_URL)))) {
  49. return $url;
  50. } else {
  51. return false;
  52. }
  53. }
  54. // vérifie qu'on est connecté
  55. function is_logged () {
  56. return Minz_Session::param ('mail') != false;
  57. }
  58. // vérifie que le système d'authentification est configuré
  59. function login_is_conf ($conf) {
  60. return $conf->mailLogin () != false;
  61. }
  62. // tiré de Shaarli de Seb Sauvage //Format RFC 4648 base64url
  63. function small_hash ($txt) {
  64. $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
  65. return strtr ($t, '+/', '-_');
  66. }
  67. function formatBytes($bytes, $precision = 2, $system = 'IEC') {
  68. if ($system === 'IEC') {
  69. $base = 1024;
  70. $units = array('B', 'KiB', 'MiB', 'GiB', 'TiB');
  71. } elseif ($system === 'SI') {
  72. $base = 1000;
  73. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  74. }
  75. $bytes = max(intval($bytes), 0);
  76. $pow = $bytes === 0 ? 0 : floor(log($bytes) / log($base));
  77. $pow = min($pow, count($units) - 1);
  78. $bytes /= pow($base, $pow);
  79. return round($bytes, $precision) . ' ' . $units[$pow];
  80. }
  81. function timestamptodate ($t, $hour = true) {
  82. $month = Minz_Translate::t (date('M', $t));
  83. if ($hour) {
  84. $date = Minz_Translate::t ('format_date_hour', $month);
  85. } else {
  86. $date = Minz_Translate::t ('format_date', $month);
  87. }
  88. return @date ($date, $t);
  89. }
  90. function sortEntriesByDate ($entry1, $entry2) {
  91. return $entry2->date (true) - $entry1->date (true);
  92. }
  93. function sortReverseEntriesByDate ($entry1, $entry2) {
  94. return $entry1->date (true) - $entry2->date (true);
  95. }
  96. function get_domain ($url) {
  97. return parse_url($url, PHP_URL_HOST);
  98. }
  99. function opml_export ($cats) {
  100. $txt = '';
  101. foreach ($cats as $cat) {
  102. $txt .= '<outline text="' . $cat['name'] . '">' . "\n";
  103. foreach ($cat['feeds'] as $feed) {
  104. $txt .= "\t" . '<outline text="' . $feed->name () . '" type="rss" xmlUrl="' . $feed->url () . '" htmlUrl="' . $feed->website () . '" description="' . htmlspecialchars($feed->description(), ENT_COMPAT, 'UTF-8') . '" />' . "\n";
  105. }
  106. $txt .= '</outline>' . "\n";
  107. }
  108. return $txt;
  109. }
  110. function html_only_entity_decode($text) {
  111. static $htmlEntitiesOnly = null;
  112. if ($htmlEntitiesOnly === null) {
  113. $htmlEntitiesOnly = array_flip(array_diff(
  114. get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8'), //Decode HTML entities
  115. get_html_translation_table(HTML_SPECIALCHARS, ENT_NOQUOTES, 'UTF-8') //Preserve XML entities
  116. ));
  117. }
  118. return strtr($text, $htmlEntitiesOnly);
  119. }
  120. function sanitizeHTML($data) {
  121. static $simplePie = null;
  122. if ($simplePie == null) {
  123. $simplePie = new SimplePie();
  124. }
  125. return html_only_entity_decode($simplePie->sanitize->sanitize($data, SIMPLEPIE_CONSTRUCT_MAYBE_HTML));
  126. }
  127. function opml_import ($xml) {
  128. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  129. $dom = new DOMDocument();
  130. $dom->recover = true;
  131. $dom->strictErrorChecking = false;
  132. $dom->loadXML($xml);
  133. $dom->encoding = 'UTF-8';
  134. $opml = simplexml_import_dom($dom);
  135. if (!$opml) {
  136. throw new FreshRSS_Opml_Exception ();
  137. }
  138. $catDAO = new FreshRSS_CategoryDAO();
  139. $catDAO->checkDefault();
  140. $defCat = $catDAO->getDefault();
  141. $categories = array ();
  142. $feeds = array ();
  143. foreach ($opml->body->outline as $outline) {
  144. if (!isset ($outline['xmlUrl'])) {
  145. // Catégorie
  146. $title = '';
  147. if (isset ($outline['text'])) {
  148. $title = (string) $outline['text'];
  149. } elseif (isset ($outline['title'])) {
  150. $title = (string) $outline['title'];
  151. }
  152. if ($title) {
  153. // Permet d'éviter les soucis au niveau des id :
  154. // ceux-ci sont générés en fonction de la date,
  155. // un flux pourrait être dans une catégorie X avec l'id Y
  156. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  157. // Y ne sera pas ajouté et le flux non plus vu que l'id
  158. // de sa catégorie n'exisera pas
  159. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  160. $catDAO = new FreshRSS_CategoryDAO ();
  161. $cat = $catDAO->searchByName ($title);
  162. if ($cat === false) {
  163. $cat = new FreshRSS_Category ($title);
  164. $values = array (
  165. 'name' => $cat->name (),
  166. 'color' => $cat->color ()
  167. );
  168. $cat->_id ($catDAO->addCategory ($values));
  169. }
  170. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  171. }
  172. } else {
  173. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  174. $feeds[] = getFeed ($outline, $defCat->id());
  175. }
  176. }
  177. return array ($categories, $feeds);
  178. }
  179. /**
  180. * import all feeds of a given outline tag
  181. */
  182. function getFeedsOutline ($outline, $cat_id) {
  183. $feeds = array ();
  184. foreach ($outline->children () as $child) {
  185. if (isset ($child['xmlUrl'])) {
  186. $feeds[] = getFeed ($child, $cat_id);
  187. } else {
  188. $feeds = array_merge(
  189. $feeds,
  190. getFeedsOutline ($child, $cat_id)
  191. );
  192. }
  193. }
  194. return $feeds;
  195. }
  196. function getFeed ($outline, $cat_id) {
  197. $url = (string) $outline['xmlUrl'];
  198. $url = htmlspecialchars($url, ENT_COMPAT, 'UTF-8');
  199. $title = '';
  200. if (isset ($outline['text'])) {
  201. $title = (string) $outline['text'];
  202. } elseif (isset ($outline['title'])) {
  203. $title = (string) $outline['title'];
  204. }
  205. $title = htmlspecialchars($title, ENT_COMPAT, 'UTF-8');
  206. $feed = new FreshRSS_Feed ($url);
  207. $feed->_category ($cat_id);
  208. $feed->_name ($title);
  209. if (isset($outline['htmlUrl'])) {
  210. $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_COMPAT, 'UTF-8'));
  211. }
  212. if (isset($outline['description'])) {
  213. $feed->_description(sanitizeHTML((string)$outline['description']));
  214. }
  215. return $feed;
  216. }
  217. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  218. function get_content_by_parsing ($url, $path) {
  219. require_once (LIB_PATH . '/lib_phpQuery.php');
  220. $html = file_get_contents ($url);
  221. if ($html) {
  222. $doc = phpQuery::newDocument ($html);
  223. $content = $doc->find ($path);
  224. $content->find ('*')->removeAttr ('style')
  225. ->removeAttr ('id')
  226. ->removeAttr ('class')
  227. ->removeAttr ('onload')
  228. ->removeAttr ('target');
  229. $content->removeAttr ('style')
  230. ->removeAttr ('id')
  231. ->removeAttr ('class')
  232. ->removeAttr ('onload')
  233. ->removeAttr ('target');
  234. return $content->__toString ();
  235. } else {
  236. throw new Exception ();
  237. }
  238. }
  239. /**
  240. * Add support of image lazy loading
  241. * Move content from src attribute to data-original
  242. * @param content is the text we want to parse
  243. */
  244. function lazyimg($content) {
  245. return preg_replace(
  246. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  247. '<img$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  248. $content
  249. );
  250. }
  251. function uTimeString() {
  252. $t = @gettimeofday();
  253. return $t['sec'] . str_pad($t['usec'], 6, '0');
  254. }
  255. function uSecString() {
  256. $t = @gettimeofday();
  257. return str_pad($t['usec'], 6, '0');
  258. }
  259. function invalidateHttpCache() {
  260. file_put_contents(DATA_PATH . '/touch.txt', uTimeString());
  261. }