importExportController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. class FreshRSS_importExport_Controller extends Minz_ActionController {
  3. public function firstAction() {
  4. if (!$this->view->loginOk) {
  5. Minz_Error::error(
  6. 403,
  7. array('error' => array(Minz_Translate::t('access_denied')))
  8. );
  9. }
  10. require_once(LIB_PATH . '/lib_opml.php');
  11. $this->catDAO = new FreshRSS_CategoryDAO();
  12. $this->entryDAO = new FreshRSS_EntryDAO();
  13. $this->feedDAO = new FreshRSS_FeedDAO();
  14. }
  15. public function indexAction() {
  16. $this->view->categories = $this->catDAO->listCategories();
  17. $this->view->feeds = $this->feedDAO->listFeeds();
  18. // au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste
  19. $this->view->flux = false;
  20. Minz_View::prependTitle(Minz_Translate::t('import_export') . ' · ');
  21. }
  22. public function importAction() {
  23. if (Minz_Request::isPost() && $_FILES['file']['error'] == 0) {
  24. @set_time_limit(300);
  25. $file = $_FILES['file'];
  26. $type_file = $this->guess_file_type($file['name']);
  27. $list_files = array(
  28. 'opml' => array(),
  29. 'json_starred' => array(),
  30. 'json_feed' => array()
  31. );
  32. // We try to list all files according to their type
  33. // A zip file is first opened and then its files are listed
  34. $list = array();
  35. if ($type_file === 'zip') {
  36. $zip = zip_open($file['tmp_name']);
  37. while (($zipfile = zip_read($zip)) !== false) {
  38. $type_zipfile = $this->guess_file_type(zip_entry_name($zipfile));
  39. if ($type_file !== 'unknown') {
  40. $list_files[$type_zipfile][] = zip_entry_read(
  41. $zipfile,
  42. zip_entry_filesize($zipfile)
  43. );
  44. }
  45. }
  46. zip_close($zip);
  47. } elseif ($type_file !== 'unknown') {
  48. $list_files[$type_file][] = file_get_contents($file['tmp_name']);
  49. }
  50. // Import different files.
  51. // OPML first(so categories and feeds are imported)
  52. // Starred articles then so the "favourite" status is already set
  53. // And finally all other files.
  54. $error = false;
  55. foreach ($list_files['opml'] as $opml_file) {
  56. $error = $this->import_opml($opml_file);
  57. }
  58. foreach ($list_files['json_starred'] as $article_file) {
  59. $error = $this->import_articles($article_file, true);
  60. }
  61. foreach ($list_files['json_feed'] as $article_file) {
  62. $error = $this->import_articles($article_file);
  63. }
  64. // And finally, we get import status and redirect to the home page
  65. $notif = null;
  66. if ($error === true) {
  67. $notif = array(
  68. 'type' => 'good',
  69. 'content' => Minz_Translate::t('feeds_imported_with_errors')
  70. );
  71. } else {
  72. $notif = array(
  73. 'type' => 'good',
  74. 'content' => Minz_Translate::t('feeds_imported')
  75. );
  76. }
  77. Minz_Session::_param('notification', $notif);
  78. Minz_Session::_param('actualize_feeds', true);
  79. Minz_Request::forward(array(
  80. 'c' => 'index',
  81. 'a' => 'index'
  82. ), true);
  83. }
  84. // What are you doing? you have to call this controller
  85. // with a POST request!
  86. Minz_Request::forward(array(
  87. 'c' => 'importExport',
  88. 'a' => 'index'
  89. ));
  90. }
  91. private function guess_file_type($filename) {
  92. // A *very* basic guess file type function. Only based on filename
  93. // That's could be improved but should be enough, at least for a first
  94. // implementation.
  95. // TODO: improve this function?
  96. if (substr_compare($filename, '.zip', -4) === 0) {
  97. return 'zip';
  98. } elseif (substr_compare($filename, '.opml', -5) === 0) {
  99. return 'opml';
  100. } elseif (strcmp($filename, 'starred.json') === 0) {
  101. return 'json_starred';
  102. } elseif (substr_compare($filename, '.json', -5) === 0 &&
  103. strpos($filename, 'feed_') === 0) {
  104. return 'json_feed';
  105. } else {
  106. return 'unknown';
  107. }
  108. }
  109. private function import_opml($opml_file) {
  110. $categories = array();
  111. $feeds = array();
  112. try {
  113. list($categories, $feeds) = opml_import($opml_file);
  114. } catch (FreshRSS_Opml_Exception $e) {
  115. Minz_Log::warning($e->getMessage());
  116. return true;
  117. }
  118. $this->catDAO->checkDefault();
  119. // on ajoute les catégories en masse dans une fonction à part
  120. $this->addCategories($categories);
  121. // on calcule la date des articles les plus anciens qu'on accepte
  122. $nb_month_old = $this->view->conf->old_entries;
  123. $date_min = time() -(3600 * 24 * 30 * $nb_month_old);
  124. // la variable $error permet de savoir si une erreur est survenue
  125. // Le but est de ne pas arrêter l'import même en cas d'erreur
  126. // L'utilisateur sera mis au courant s'il y a eu des erreurs, mais
  127. // ne connaîtra pas les détails. Ceux-ci seront toutefois logguées
  128. $error = false;
  129. foreach ($feeds as $feed) {
  130. try {
  131. $values = array(
  132. 'id' => $feed->id(),
  133. 'url' => $feed->url(),
  134. 'category' => $feed->category(),
  135. 'name' => $feed->name(),
  136. 'website' => $feed->website(),
  137. 'description' => $feed->description(),
  138. 'lastUpdate' => 0,
  139. 'httpAuth' => $feed->httpAuth()
  140. );
  141. // ajout du flux que s'il n'est pas déjà en BDD
  142. if (!$this->feedDAO->searchByUrl($values['url'])) {
  143. $id = $this->feedDAO->addFeed($values);
  144. if ($id) {
  145. $feed->_id($id);
  146. $feed->faviconPrepare();
  147. } else {
  148. $error = true;
  149. }
  150. }
  151. } catch (FreshRSS_Feed_Exception $e) {
  152. $error = true;
  153. Minz_Log::record($e->getMessage(), Minz_Log::WARNING);
  154. }
  155. }
  156. return $error;
  157. }
  158. private function addCategories($categories) {
  159. foreach ($categories as $cat) {
  160. if (!$this->catDAO->searchByName($cat->name())) {
  161. $values = array(
  162. 'id' => $cat->id(),
  163. 'name' => $cat->name(),
  164. );
  165. $this->catDAO->addCategory($values);
  166. }
  167. }
  168. }
  169. private function import_articles($article_file, $starred = false) {
  170. $article_object = json_decode($article_file, true);
  171. if (is_null($article_object)) {
  172. Minz_Log::warning('Try to import a non-JSON file');
  173. return true;
  174. }
  175. $google_compliant = (strpos($article_object['id'], 'com.google') !== false);
  176. foreach ($article_object['items'] as $item) {
  177. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  178. $feed = $this->feedDAO->searchByUrl($item['origin'][$key]);
  179. if (is_null($feed)) {
  180. $feed = new FreshRSS_Feed($item['origin'][$key]);
  181. $feed->_name ($item['origin']['title']);
  182. $feed->_website ($item['origin']['htmlUrl']);
  183. $error = $this->addFeed($feed); // TODO
  184. if ($error) {
  185. continue;
  186. }
  187. }
  188. $author = isset($item['author']) ? $item['author'] : '';
  189. $key_content = $google_compliant && !isset($item['content']) ? 'summary' : 'content';
  190. $tags = $item['categories'];
  191. if ($google_compliant) {
  192. $tags = array_filter($tags, function($var) {
  193. return strpos($var, '/state/com.google') === false;
  194. });
  195. }
  196. $entry = new FreshRSS_Entry(
  197. $feed->id(), $item['id'], $item['title'], $author,
  198. $item[$key_content]['content'], $item['alternate'][0]['href'],
  199. $item['published'], false, $starred, $tags
  200. );
  201. Minz_Log::debug(print_r($entry, true)); // TODO
  202. }
  203. }
  204. public function exportAction() {
  205. if (Minz_Request::isPost()) {
  206. $this->view->_useLayout(false);
  207. $export_opml = Minz_Request::param('export_opml', false);
  208. $export_starred = Minz_Request::param('export_starred', false);
  209. $export_feeds = Minz_Request::param('export_feeds', false);
  210. // code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  211. $file = tempnam('tmp', 'zip');
  212. $zip = new ZipArchive();
  213. $zip->open($file, ZipArchive::OVERWRITE);
  214. // Stuff with content
  215. if ($export_opml) {
  216. $zip->addFromString('feeds.opml', $this->generate_opml());
  217. }
  218. if ($export_starred) {
  219. $zip->addFromString('starred.json', $this->generate_articles('starred'));
  220. }
  221. foreach ($export_feeds as $feed_id) {
  222. $feed = $this->feedDAO->searchById($feed_id);
  223. $zip->addFromString(
  224. 'feed_' . $feed->category() . '_' . $feed->id() . '.json',
  225. $this->generate_articles('feed', $feed)
  226. );
  227. }
  228. // Close and send to user
  229. $zip->close();
  230. header('Content-Type: application/zip');
  231. header('Content-Length: ' . filesize($file));
  232. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  233. readfile($file);
  234. unlink($file);
  235. }
  236. }
  237. private function generate_opml() {
  238. $list = array();
  239. foreach ($this->catDAO->listCategories() as $key => $cat) {
  240. $list[$key]['name'] = $cat->name();
  241. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  242. }
  243. $this->view->categories = $list;
  244. return $this->view->helperToString('export/opml');
  245. }
  246. private function generate_articles($type, $feed = NULL) {
  247. $this->view->categories = $this->catDAO->listCategories();
  248. if ($type == 'starred') {
  249. $this->view->list_title = Minz_Translate::t("starred_list");
  250. $this->view->type = 'starred';
  251. $this->view->entries = $this->entryDAO->listWhere(
  252. 's', '', 'all', 'ASC',
  253. $entryDAO->countUnreadReadFavorites()['all']
  254. );
  255. } elseif ($type == 'feed' && !is_null($feed)) {
  256. $this->view->list_title = Minz_Translate::t("feed_list", $feed->name());
  257. $this->view->type = 'feed/' . $feed->id();
  258. $this->view->entries = $this->entryDAO->listWhere(
  259. 'f', $feed->id(), 'all', 'ASC',
  260. $this->view->conf->posts_per_page
  261. );
  262. $this->view->feed = $feed;
  263. }
  264. return $this->view->helperToString('export/articles');
  265. }
  266. }