4
0

lib_rss.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 opml_import ($xml) {
  121. $xml = html_only_entity_decode($xml); //!\ Assume UTF-8
  122. $dom = new DOMDocument();
  123. $dom->recover = true;
  124. $dom->strictErrorChecking = false;
  125. $dom->loadXML($xml);
  126. $dom->encoding = 'UTF-8';
  127. $opml = simplexml_import_dom($dom);
  128. if (!$opml) {
  129. throw new FreshRSS_Opml_Exception ();
  130. }
  131. $catDAO = new FreshRSS_CategoryDAO();
  132. $catDAO->checkDefault();
  133. $defCat = $catDAO->getDefault();
  134. $categories = array ();
  135. $feeds = array ();
  136. foreach ($opml->body->outline as $outline) {
  137. if (!isset ($outline['xmlUrl'])) {
  138. // Catégorie
  139. $title = '';
  140. if (isset ($outline['text'])) {
  141. $title = (string) $outline['text'];
  142. } elseif (isset ($outline['title'])) {
  143. $title = (string) $outline['title'];
  144. }
  145. if ($title) {
  146. // Permet d'éviter les soucis au niveau des id :
  147. // ceux-ci sont générés en fonction de la date,
  148. // un flux pourrait être dans une catégorie X avec l'id Y
  149. // alors qu'il existe déjà la catégorie X mais avec l'id Z
  150. // Y ne sera pas ajouté et le flux non plus vu que l'id
  151. // de sa catégorie n'exisera pas
  152. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  153. $catDAO = new FreshRSS_CategoryDAO ();
  154. $cat = $catDAO->searchByName ($title);
  155. if ($cat === false) {
  156. $cat = new FreshRSS_Category ($title);
  157. $values = array (
  158. 'name' => $cat->name (),
  159. 'color' => $cat->color ()
  160. );
  161. $cat->_id ($catDAO->addCategory ($values));
  162. }
  163. $feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
  164. }
  165. } else {
  166. // Flux rss sans catégorie, on récupère l'ajoute dans la catégorie par défaut
  167. $feeds[] = getFeed ($outline, $defCat->id());
  168. }
  169. }
  170. return array ($categories, $feeds);
  171. }
  172. /**
  173. * import all feeds of a given outline tag
  174. */
  175. function getFeedsOutline ($outline, $cat_id) {
  176. $feeds = array ();
  177. foreach ($outline->children () as $child) {
  178. if (isset ($child['xmlUrl'])) {
  179. $feeds[] = getFeed ($child, $cat_id);
  180. } else {
  181. $feeds = array_merge(
  182. $feeds,
  183. getFeedsOutline ($child, $cat_id)
  184. );
  185. }
  186. }
  187. return $feeds;
  188. }
  189. function getFeed ($outline, $cat_id) {
  190. $url = (string) $outline['xmlUrl'];
  191. $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  192. $title = '';
  193. if (isset ($outline['text'])) {
  194. $title = (string) $outline['text'];
  195. } elseif (isset ($outline['title'])) {
  196. $title = (string) $outline['title'];
  197. }
  198. $title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
  199. $feed = new FreshRSS_Feed ($url);
  200. $feed->_category ($cat_id);
  201. $feed->_name ($title);
  202. if (isset($outline['htmlUrl'])) {
  203. $feed->_website(htmlspecialchars((string)$outline['htmlUrl'], ENT_QUOTES, 'UTF-8'));
  204. }
  205. if (isset($outline['description'])) {
  206. $feed->_description(htmlspecialchars((string)$outline['description'], ENT_QUOTES, 'UTF-8'));
  207. }
  208. return $feed;
  209. }
  210. /* permet de récupérer le contenu d'un article pour un flux qui n'est pas complet */
  211. function get_content_by_parsing ($url, $path) {
  212. require_once (LIB_PATH . '/lib_phpQuery.php');
  213. $html = file_get_contents ($url);
  214. if ($html) {
  215. $doc = phpQuery::newDocument ($html);
  216. $content = $doc->find ($path);
  217. $content->find ('*')->removeAttr ('style')
  218. ->removeAttr ('id')
  219. ->removeAttr ('class')
  220. ->removeAttr ('onload')
  221. ->removeAttr ('target');
  222. $content->removeAttr ('style')
  223. ->removeAttr ('id')
  224. ->removeAttr ('class')
  225. ->removeAttr ('onload')
  226. ->removeAttr ('target');
  227. return $content->__toString ();
  228. } else {
  229. throw new Exception ();
  230. }
  231. }
  232. /**
  233. * Add support of image lazy loading
  234. * Move content from src attribute to data-original
  235. * @param content is the text we want to parse
  236. */
  237. function lazyimg($content) {
  238. return preg_replace(
  239. '/<img([^>]+?)src=[\'"]([^"\']+)[\'"]([^>]*)>/i',
  240. '<img$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
  241. $content
  242. );
  243. }
  244. function uTimeString() {
  245. $t = @gettimeofday();
  246. return $t['sec'] . str_pad($t['usec'], 6, '0');
  247. }
  248. function uSecString() {
  249. $t = @gettimeofday();
  250. return str_pad($t['usec'], 6, '0');
  251. }
  252. function invalidateHttpCache() {
  253. file_put_contents(DATA_PATH . '/touch.txt', uTimeString());
  254. }