1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<?php
class FreshRSS_importExport_Controller extends Minz_ActionController {
public function firstAction() {
if (!$this->view->loginOk) {
Minz_Error::error (
403,
array ('error' => array (Minz_Translate::t ('access_denied')))
);
}
require_once(LIB_PATH . '/lib_opml.php');
}
public function indexAction() {
$catDAO = new FreshRSS_CategoryDAO();
$this->view->categories = $catDAO->listCategories();
$feedDAO = new FreshRSS_FeedDAO();
$this->view->feeds = $feedDAO->listFeeds();
// au niveau de la vue, permet de ne pas voir un flux sélectionné dans la liste
$this->view->flux = false;
Minz_View::prependTitle(Minz_Translate::t('import_export') . ' · ');
}
public function importAction() {
if (Minz_Request::isPost() && $_FILES['file']['error'] == 0) {
invalidateHttpCache();
// on parse le fichier OPML pour récupérer les catégories et les flux associés
try {
list ($categories, $feeds) = opml_import (
file_get_contents ($_FILES['file']['tmp_name'])
);
// On redirige vers le controller feed qui va se charger d'insérer les flux en BDD
// les flux sont mis au préalable dans des variables de Request
Minz_Request::_param ('categories', $categories);
Minz_Request::_param ('feeds', $feeds);
Minz_Request::forward (array ('c' => 'feed', 'a' => 'massiveImport'));
} catch (FreshRSS_Opml_Exception $e) {
Minz_Log::record ($e->getMessage (), Minz_Log::WARNING);
$notif = array (
'type' => 'bad',
'content' => Minz_Translate::t ('bad_opml_file')
);
Minz_Session::_param ('notification', $notif);
Minz_Request::forward (array (
'c' => 'configure',
'a' => 'importExport'
), true);
}
}
}
public function exportAction() {
if (Minz_Request::isPost()) {
$this->view->_useLayout (false);
$export_opml = Minz_Request::param('export_opml', false);
$export_starred = Minz_Request::param('export_starred', false);
$export_feeds = Minz_Request::param('export_feeds', false);
// code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
$file = tempnam('tmp', 'zip');
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);
// Stuff with content
if ($export_opml) {
$zip->addFromString('feeds.opml', $this->generate_opml());
}
if ($export_starred) {
$zip->addFromString('starred.json', $this->generate_articles('starred'));
}
$feedDAO = new FreshRSS_FeedDAO ();
foreach ($export_feeds as $feed_id) {
$feed = $feedDAO->searchById($feed_id);
$zip->addFromString(
'feed_' . $feed->category() . '_' . $feed->id() . '.json',
$this->generate_articles('feed', $feed)
);
}
// Close and send to user
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="freshrss_export.zip"');
readfile($file);
unlink($file);
}
}
private function generate_opml() {
$feedDAO = new FreshRSS_FeedDAO ();
$catDAO = new FreshRSS_CategoryDAO ();
$list = array ();
foreach ($catDAO->listCategories () as $key => $cat) {
$list[$key]['name'] = $cat->name ();
$list[$key]['feeds'] = $feedDAO->listByCategory ($cat->id ());
}
$this->view->categories = $list;
return $this->view->helperToString('export/opml');
}
private function generate_articles($type, $feed = NULL) {
$entryDAO = new FreshRSS_EntryDAO();
$catDAO = new FreshRSS_CategoryDAO();
$this->view->categories = $catDAO->listCategories();
if ($type == 'starred') {
$this->view->list_title = Minz_Translate::t("starred_list");
$this->view->type = 'starred';
$this->view->entries = $entryDAO->listWhere(
's', '', 'all', 'ASC',
$entryDAO->countUnreadReadFavorites()['all']
);
} elseif ($type == 'feed' && !is_null($feed)) {
$this->view->list_title = Minz_Translate::t("feed_list", $feed->name());
$this->view->type = 'feed/' . $feed->id();
$this->view->entries = $entryDAO->listWhere(
'f', $feed->id(), 'all', 'ASC',
$this->view->conf->posts_per_page
);
$this->view->feed = $feed;
}
return $this->view->helperToString('export/articles');
}
}
|