importExportController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. substr_compare($filename, '.xml', -4) === 0) {
  100. return 'opml';
  101. } elseif (strcmp($filename, 'starred.json') === 0) {
  102. return 'json_starred';
  103. } elseif (substr_compare($filename, '.json', -5) === 0 &&
  104. strpos($filename, 'feed_') === 0) {
  105. return 'json_feed';
  106. } else {
  107. return 'unknown';
  108. }
  109. }
  110. private function import_opml($opml_file) {
  111. $opml_array = array();
  112. try {
  113. $opml_array = libopml_parse_string($opml_file);
  114. } catch (LibOPML_Exception $e) {
  115. Minz_Log::warning($e->getMessage());
  116. return true;
  117. }
  118. $this->catDAO->checkDefault();
  119. return $this->addOpmlElements($opml_array['body']);
  120. }
  121. private function addOpmlElements($opml_elements, $parent_cat = null) {
  122. $error = false;
  123. foreach ($opml_elements as $elt) {
  124. $res = false;
  125. if (isset($elt['xmlUrl'])) {
  126. $res = $this->addFeedOpml($elt, $parent_cat);
  127. } else {
  128. $res = $this->addCategoryOpml($elt, $parent_cat);
  129. }
  130. if (!$error && $res) {
  131. // oops: there is at least one error!
  132. $error = $res;
  133. }
  134. }
  135. return $error;
  136. }
  137. private function addFeedOpml($feed_elt, $parent_cat) {
  138. if (is_null($parent_cat)) {
  139. // This feed has no parent category so we get the default one
  140. $parent_cat = $catDAO->getDefault()->name();
  141. }
  142. $cat = $this->catDAO->searchByName($parent_cat);
  143. if (!$cat) {
  144. return true;
  145. }
  146. // We get different useful information
  147. $url = html_chars_utf8($feed_elt['xmlUrl']);
  148. $name = html_chars_utf8($feed_elt['text']);
  149. $website = '';
  150. if (isset($feed_elt['htmlUrl'])) {
  151. $website = html_chars_utf8($feed_elt['htmlUrl']);
  152. }
  153. $description = '';
  154. if (isset($feed_elt['description'])) {
  155. $description = html_chars_utf8($feed_elt['description']);
  156. }
  157. $error = false;
  158. try {
  159. // Create a Feed object and add it in DB
  160. $feed = new FreshRSS_Feed($url);
  161. $feed->_category($cat->id());
  162. $feed->_name($name);
  163. $feed->_website($website);
  164. $feed->_description($description);
  165. // addFeedObject checks if feed is already in DB so nothing else to
  166. // check here
  167. $id = $this->feedDAO->addFeedObject($feed);
  168. $error = ($id === false);
  169. } catch (FreshRSS_Feed_Exception $e) {
  170. Minz_Log::record($e->getMessage(), Minz_Log::WARNING);
  171. $error = true;
  172. }
  173. return $error;
  174. }
  175. private function addCategoryOpml($cat_elt, $parent_cat) {
  176. // Create a new Category object
  177. $cat = new FreshRSS_Category(html_chars_utf8($cat_elt['text']));
  178. $id = $this->catDAO->addCategoryObject($cat);
  179. $error = ($id === false);
  180. if (isset($cat_elt['@outlines'])) {
  181. // Our cat_elt contains more categories or more feeds, so we
  182. // add them recursively.
  183. // Note: FreshRSS does not support yet category arborescence
  184. $res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
  185. if (!$error && $res) {
  186. $error = true;
  187. }
  188. }
  189. return $error;
  190. }
  191. private function import_articles($article_file, $starred = false) {
  192. $article_object = json_decode($article_file, true);
  193. if (is_null($article_object)) {
  194. Minz_Log::warning('Try to import a non-JSON file');
  195. return true;
  196. }
  197. $google_compliant = (strpos($article_object['id'], 'com.google') !== false);
  198. foreach ($article_object['items'] as $item) {
  199. $key = $google_compliant ? 'htmlUrl' : 'feedUrl';
  200. $feed = $this->feedDAO->searchByUrl($item['origin'][$key]);
  201. if (is_null($feed)) {
  202. $feed = new FreshRSS_Feed($item['origin'][$key]);
  203. $feed->_name ($item['origin']['title']);
  204. $feed->_website ($item['origin']['htmlUrl']);
  205. $error = $this->addFeed($feed); // TODO
  206. if ($error) {
  207. continue;
  208. }
  209. }
  210. $author = isset($item['author']) ? $item['author'] : '';
  211. $key_content = $google_compliant && !isset($item['content']) ? 'summary' : 'content';
  212. $tags = $item['categories'];
  213. if ($google_compliant) {
  214. $tags = array_filter($tags, function($var) {
  215. return strpos($var, '/state/com.google') === false;
  216. });
  217. }
  218. $entry = new FreshRSS_Entry(
  219. $feed->id(), $item['id'], $item['title'], $author,
  220. $item[$key_content]['content'], $item['alternate'][0]['href'],
  221. $item['published'], false, $starred, $tags
  222. );
  223. Minz_Log::debug(print_r($entry, true)); // TODO
  224. }
  225. }
  226. public function exportAction() {
  227. if (Minz_Request::isPost()) {
  228. $this->view->_useLayout(false);
  229. $export_opml = Minz_Request::param('export_opml', false);
  230. $export_starred = Minz_Request::param('export_starred', false);
  231. $export_feeds = Minz_Request::param('export_feeds', false);
  232. // code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
  233. $file = tempnam('tmp', 'zip');
  234. $zip = new ZipArchive();
  235. $zip->open($file, ZipArchive::OVERWRITE);
  236. // Stuff with content
  237. if ($export_opml) {
  238. $zip->addFromString('feeds.opml', $this->generate_opml());
  239. }
  240. if ($export_starred) {
  241. $zip->addFromString('starred.json', $this->generate_articles('starred'));
  242. }
  243. foreach ($export_feeds as $feed_id) {
  244. $feed = $this->feedDAO->searchById($feed_id);
  245. $zip->addFromString(
  246. 'feed_' . $feed->category() . '_' . $feed->id() . '.json',
  247. $this->generate_articles('feed', $feed)
  248. );
  249. }
  250. // Close and send to user
  251. $zip->close();
  252. header('Content-Type: application/zip');
  253. header('Content-Length: ' . filesize($file));
  254. header('Content-Disposition: attachment; filename="freshrss_export.zip"');
  255. readfile($file);
  256. unlink($file);
  257. }
  258. }
  259. private function generate_opml() {
  260. $list = array();
  261. foreach ($this->catDAO->listCategories() as $key => $cat) {
  262. $list[$key]['name'] = $cat->name();
  263. $list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
  264. }
  265. $this->view->categories = $list;
  266. return $this->view->helperToString('export/opml');
  267. }
  268. private function generate_articles($type, $feed = NULL) {
  269. $this->view->categories = $this->catDAO->listCategories();
  270. if ($type == 'starred') {
  271. $this->view->list_title = Minz_Translate::t("starred_list");
  272. $this->view->type = 'starred';
  273. $this->view->entries = $this->entryDAO->listWhere(
  274. 's', '', 'all', 'ASC',
  275. $this->entryDAO->countUnreadReadFavorites()['all']
  276. );
  277. } elseif ($type == 'feed' && !is_null($feed)) {
  278. $this->view->list_title = Minz_Translate::t("feed_list", $feed->name());
  279. $this->view->type = 'feed/' . $feed->id();
  280. $this->view->entries = $this->entryDAO->listWhere(
  281. 'f', $feed->id(), 'all', 'ASC',
  282. $this->view->conf->posts_per_page
  283. );
  284. $this->view->feed = $feed;
  285. }
  286. return $this->view->helperToString('export/articles');
  287. }
  288. }