2014-07-23 15:52:50 +02:00
|
|
|
<?php
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* Controller to handle every import and export actions.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
class FreshRSS_importExport_Controller extends Minz_ActionController {
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action is called before every other action in that class. It is
|
|
|
|
* the common boiler plate for every action. It is triggered by the
|
|
|
|
* underlying framework.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function firstAction() {
|
2015-02-08 18:55:48 +01:00
|
|
|
if (!FreshRSS_Auth::hasAccess()) {
|
|
|
|
Minz_Error::error(403);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
require_once(LIB_PATH . '/lib_opml.php');
|
|
|
|
|
|
|
|
$this->catDAO = new FreshRSS_CategoryDAO();
|
|
|
|
$this->entryDAO = FreshRSS_Factory::createEntryDao();
|
|
|
|
$this->feedDAO = FreshRSS_Factory::createFeedDao();
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action displays the main page for import / export system.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function indexAction() {
|
|
|
|
$this->view->feeds = $this->feedDAO->listFeeds();
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_View::prependTitle(_t('sub.import_export.title') . ' · ');
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action handles import action.
|
|
|
|
*
|
|
|
|
* It must be reached by a POST request.
|
|
|
|
*
|
|
|
|
* Parameter is:
|
|
|
|
* - file (default: nothing!)
|
|
|
|
* Available file types are: zip, json or xml.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function importAction() {
|
2014-09-21 12:12:35 +02:00
|
|
|
if (!Minz_Request::isPost()) {
|
|
|
|
Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
$file = $_FILES['file'];
|
|
|
|
$status_file = $file['error'];
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
if ($status_file !== 0) {
|
|
|
|
Minz_Log::error('File cannot be uploaded. Error code: ' . $status_file);
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Request::bad(_t('feedback.import_export.file_cannot_be_uploaded'),
|
2014-09-21 12:12:35 +02:00
|
|
|
array('c' => 'importExport', 'a' => 'index'));
|
|
|
|
}
|
|
|
|
|
|
|
|
@set_time_limit(300);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
$type_file = $this->guessFileType($file['name']);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
$list_files = array(
|
|
|
|
'opml' => array(),
|
|
|
|
'json_starred' => array(),
|
|
|
|
'json_feed' => array()
|
|
|
|
);
|
|
|
|
|
|
|
|
// We try to list all files according to their type
|
|
|
|
$list = array();
|
|
|
|
if ($type_file === 'zip' && extension_loaded('zip')) {
|
|
|
|
$zip = zip_open($file['tmp_name']);
|
|
|
|
|
|
|
|
if (!is_resource($zip)) {
|
|
|
|
// zip_open cannot open file: something is wrong
|
|
|
|
Minz_Log::error('Zip archive cannot be imported. Error code: ' . $zip);
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Request::bad(_t('feedback.import_export.zip_error'),
|
2014-09-21 12:12:35 +02:00
|
|
|
array('c' => 'importExport', 'a' => 'index'));
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
while (($zipfile = zip_read($zip)) !== false) {
|
|
|
|
if (!is_resource($zipfile)) {
|
|
|
|
// zip_entry() can also return an error code!
|
|
|
|
Minz_Log::error('Zip file cannot be imported. Error code: ' . $zipfile);
|
|
|
|
} else {
|
|
|
|
$type_zipfile = $this->guessFileType(zip_entry_name($zipfile));
|
2014-07-23 15:52:50 +02:00
|
|
|
if ($type_file !== 'unknown') {
|
|
|
|
$list_files[$type_zipfile][] = zip_entry_read(
|
|
|
|
$zipfile,
|
|
|
|
zip_entry_filesize($zipfile)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
zip_close($zip);
|
|
|
|
} elseif ($type_file === 'zip') {
|
|
|
|
// Zip extension is not loaded
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Request::bad(_t('feedback.import_export.no_zip_extension'),
|
2014-09-21 12:12:35 +02:00
|
|
|
array('c' => 'importExport', 'a' => 'index'));
|
|
|
|
} elseif ($type_file !== 'unknown') {
|
|
|
|
$list_files[$type_file][] = file_get_contents($file['tmp_name']);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
// Import file contents.
|
|
|
|
// OPML first(so categories and feeds are imported)
|
|
|
|
// Starred articles then so the "favourite" status is already set
|
|
|
|
// And finally all other files.
|
|
|
|
$error = false;
|
|
|
|
foreach ($list_files['opml'] as $opml_file) {
|
|
|
|
$error = $this->importOpml($opml_file);
|
|
|
|
}
|
|
|
|
foreach ($list_files['json_starred'] as $article_file) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$error = $this->importJson($article_file, true);
|
2014-09-21 12:12:35 +02:00
|
|
|
}
|
|
|
|
foreach ($list_files['json_feed'] as $article_file) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$error = $this->importJson($article_file);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
// And finally, we get import status and redirect to the home page
|
|
|
|
Minz_Session::_param('actualize_feeds', true);
|
2015-02-08 18:55:48 +01:00
|
|
|
$content_notif = $error === true ? _t('feedback.import_export.feeds_imported_with_errors') :
|
|
|
|
_t('feedback.import_export.feeds_imported');
|
2014-09-21 12:12:35 +02:00
|
|
|
Minz_Request::good($content_notif);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method tries to guess the file type based on its name.
|
|
|
|
*
|
|
|
|
* Itis a *very* basic guess file type function. Only based on filename.
|
|
|
|
* That's could be improved but should be enough for what we have to do.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
private function guessFileType($filename) {
|
|
|
|
if (substr_compare($filename, '.zip', -4) === 0) {
|
|
|
|
return 'zip';
|
|
|
|
} elseif (substr_compare($filename, '.opml', -5) === 0 ||
|
|
|
|
substr_compare($filename, '.xml', -4) === 0) {
|
|
|
|
return 'opml';
|
2014-09-21 12:12:35 +02:00
|
|
|
} elseif (substr_compare($filename, '.json', -5) === 0 &&
|
|
|
|
strpos($filename, 'starred') !== false) {
|
2014-07-23 15:52:50 +02:00
|
|
|
return 'json_starred';
|
2014-09-27 10:10:43 +02:00
|
|
|
} elseif (substr_compare($filename, '.json', -5) === 0) {
|
2014-07-23 15:52:50 +02:00
|
|
|
return 'json_feed';
|
|
|
|
} else {
|
|
|
|
return 'unknown';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method parses and imports an OPML file.
|
|
|
|
*
|
|
|
|
* @param string $opml_file the OPML file content.
|
|
|
|
* @return boolean true if an error occured, false else.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
private function importOpml($opml_file) {
|
|
|
|
$opml_array = array();
|
|
|
|
try {
|
2015-02-08 18:55:48 +01:00
|
|
|
$opml_array = libopml_parse_string($opml_file, false);
|
2014-07-23 15:52:50 +02:00
|
|
|
} catch (LibOPML_Exception $e) {
|
|
|
|
Minz_Log::warning($e->getMessage());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->catDAO->checkDefault();
|
|
|
|
|
|
|
|
return $this->addOpmlElements($opml_array['body']);
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method imports an OPML file based on its body.
|
|
|
|
*
|
|
|
|
* @param array $opml_elements an OPML element (body or outline).
|
|
|
|
* @param string $parent_cat the name of the parent category.
|
|
|
|
* @return boolean true if an error occured, false else.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
private function addOpmlElements($opml_elements, $parent_cat = null) {
|
|
|
|
$error = false;
|
2015-02-08 18:55:48 +01:00
|
|
|
|
|
|
|
$nb_feeds = count($this->feedDAO->listFeeds());
|
|
|
|
$nb_cats = count($this->catDAO->listCategories(false));
|
|
|
|
$limits = FreshRSS_Context::$system_conf->limits;
|
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
foreach ($opml_elements as $elt) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$is_error = false;
|
2014-07-23 15:52:50 +02:00
|
|
|
if (isset($elt['xmlUrl'])) {
|
2015-02-08 18:55:48 +01:00
|
|
|
// If xmlUrl exists, it means it is a feed
|
|
|
|
if ($nb_feeds >= $limits['max_feeds']) {
|
|
|
|
Minz_Log::warning(_t('feedback.sub.feed.over_max',
|
|
|
|
$limits['max_feeds']));
|
|
|
|
$is_error = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_error = $this->addFeedOpml($elt, $parent_cat);
|
|
|
|
if (!$is_error) {
|
|
|
|
$nb_feeds += 1;
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
} else {
|
2015-02-08 18:55:48 +01:00
|
|
|
// No xmlUrl? It should be a category!
|
|
|
|
$limit_reached = ($nb_cats >= $limits['max_categories']);
|
|
|
|
if ($limit_reached) {
|
|
|
|
Minz_Log::warning(_t('feedback.sub.category.over_max',
|
|
|
|
$limits['max_categories']));
|
|
|
|
}
|
|
|
|
|
|
|
|
$is_error = $this->addCategoryOpml($elt, $parent_cat, $limit_reached);
|
|
|
|
if (!$is_error) {
|
|
|
|
$nb_cats += 1;
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
if (!$error && $is_error) {
|
2014-07-23 15:52:50 +02:00
|
|
|
// oops: there is at least one error!
|
2015-02-08 18:55:48 +01:00
|
|
|
$error = $is_error;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method imports an OPML feed element.
|
|
|
|
*
|
|
|
|
* @param array $feed_elt an OPML element (must be a feed element).
|
|
|
|
* @param string $parent_cat the name of the parent category.
|
|
|
|
* @return boolean true if an error occured, false else.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
private function addFeedOpml($feed_elt, $parent_cat) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$default_cat = $this->catDAO->getDefault();
|
2014-07-23 15:52:50 +02:00
|
|
|
if (is_null($parent_cat)) {
|
|
|
|
// This feed has no parent category so we get the default one
|
2015-02-08 18:55:48 +01:00
|
|
|
$parent_cat = $default_cat->name();
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$cat = $this->catDAO->searchByName($parent_cat);
|
2015-02-08 18:55:48 +01:00
|
|
|
if (is_null($cat)) {
|
|
|
|
// If there is not $cat, it means parent category does not exist in
|
|
|
|
// database.
|
|
|
|
// If it happens, take the default category.
|
|
|
|
$cat = $default_cat;
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We get different useful information
|
2014-09-21 12:12:35 +02:00
|
|
|
$url = Minz_Helper::htmlspecialchars_utf8($feed_elt['xmlUrl']);
|
|
|
|
$name = Minz_Helper::htmlspecialchars_utf8($feed_elt['text']);
|
2014-07-23 15:52:50 +02:00
|
|
|
$website = '';
|
|
|
|
if (isset($feed_elt['htmlUrl'])) {
|
2014-09-21 12:12:35 +02:00
|
|
|
$website = Minz_Helper::htmlspecialchars_utf8($feed_elt['htmlUrl']);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
$description = '';
|
|
|
|
if (isset($feed_elt['description'])) {
|
2014-09-21 12:12:35 +02:00
|
|
|
$description = Minz_Helper::htmlspecialchars_utf8($feed_elt['description']);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$error = false;
|
|
|
|
try {
|
|
|
|
// Create a Feed object and add it in DB
|
|
|
|
$feed = new FreshRSS_Feed($url);
|
|
|
|
$feed->_category($cat->id());
|
|
|
|
$feed->_name($name);
|
|
|
|
$feed->_website($website);
|
|
|
|
$feed->_description($description);
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
// Call the extension hook
|
|
|
|
$feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
|
|
|
|
if (!is_null($feed)) {
|
|
|
|
// addFeedObject checks if feed is already in DB so nothing else to
|
|
|
|
// check here
|
|
|
|
$id = $this->feedDAO->addFeedObject($feed);
|
|
|
|
$error = ($id === false);
|
|
|
|
} else {
|
|
|
|
$error = true;
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
} catch (FreshRSS_Feed_Exception $e) {
|
|
|
|
Minz_Log::warning($e->getMessage());
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method imports an OPML category element.
|
|
|
|
*
|
|
|
|
* @param array $cat_elt an OPML element (must be a category element).
|
|
|
|
* @param string $parent_cat the name of the parent category.
|
|
|
|
* @param boolean $cat_limit_reached indicates if category limit has been reached.
|
|
|
|
* if yes, category is not added (but we try for feeds!)
|
|
|
|
* @return boolean true if an error occured, false else.
|
|
|
|
*/
|
|
|
|
private function addCategoryOpml($cat_elt, $parent_cat, $cat_limit_reached) {
|
2014-07-23 15:52:50 +02:00
|
|
|
// Create a new Category object
|
2014-09-21 12:12:35 +02:00
|
|
|
$cat = new FreshRSS_Category(Minz_Helper::htmlspecialchars_utf8($cat_elt['text']));
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$error = true;
|
|
|
|
if (!$cat_limit_reached) {
|
|
|
|
$id = $this->catDAO->addCategoryObject($cat);
|
|
|
|
$error = ($id === false);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
if (isset($cat_elt['@outlines'])) {
|
|
|
|
// Our cat_elt contains more categories or more feeds, so we
|
|
|
|
// add them recursively.
|
|
|
|
// Note: FreshRSS does not support yet category arborescence
|
|
|
|
$res = $this->addOpmlElements($cat_elt['@outlines'], $cat->name());
|
|
|
|
if (!$error && $res) {
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method import a JSON-based file (Google Reader format).
|
|
|
|
*
|
|
|
|
* @param string $article_file the JSON file content.
|
|
|
|
* @param boolean $starred true if articles from the file must be starred.
|
|
|
|
* @return boolean true if an error occured, false else.
|
|
|
|
*/
|
|
|
|
private function importJson($article_file, $starred = false) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$article_object = json_decode($article_file, true);
|
|
|
|
if (is_null($article_object)) {
|
|
|
|
Minz_Log::warning('Try to import a non-JSON file');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$is_read = FreshRSS_Context::$user_conf->mark_when['reception'] ? 1 : 0;
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$google_compliant = strpos($article_object['id'], 'com.google') !== false;
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
$error = false;
|
2014-09-27 10:10:43 +02:00
|
|
|
$article_to_feed = array();
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$nb_feeds = count($this->feedDAO->listFeeds());
|
|
|
|
$limits = FreshRSS_Context::$system_conf->limits;
|
|
|
|
|
2014-09-27 10:10:43 +02:00
|
|
|
// First, we check feeds of articles are in DB (and add them if needed).
|
2014-07-23 15:52:50 +02:00
|
|
|
foreach ($article_object['items'] as $item) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$key = $google_compliant ? 'htmlUrl' : 'feedUrl';
|
|
|
|
$feed = new FreshRSS_Feed($item['origin'][$key]);
|
|
|
|
$feed = $this->feedDAO->searchByUrl($feed->url());
|
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
if (is_null($feed)) {
|
2015-02-08 18:55:48 +01:00
|
|
|
// Feed does not exist in DB,we should to try to add it.
|
|
|
|
if ($nb_feeds >= $limits['max_feeds']) {
|
|
|
|
// Oops, no more place!
|
|
|
|
Minz_Log::warning(_t('feedback.sub.feed.over_max', $limits['max_feeds']));
|
|
|
|
} else {
|
|
|
|
$feed = $this->addFeedJson($item['origin'], $google_compliant);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($feed)) {
|
|
|
|
// Still null? It means something went wrong.
|
|
|
|
$error = true;
|
|
|
|
} else {
|
|
|
|
// Nice! Increase the counter.
|
|
|
|
$nb_feeds += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($feed)) {
|
2014-09-27 10:10:43 +02:00
|
|
|
$article_to_feed[$item['id']] = $feed->id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, articles are imported.
|
|
|
|
$prepared_statement = $this->entryDAO->addEntryPrepare();
|
|
|
|
$this->entryDAO->beginTransaction();
|
|
|
|
foreach ($article_object['items'] as $item) {
|
|
|
|
if (!isset($article_to_feed[$item['id']])) {
|
2015-02-08 18:55:48 +01:00
|
|
|
// Related feed does not exist for this entry, do nothing.
|
2014-07-23 15:52:50 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-27 10:10:43 +02:00
|
|
|
$feed_id = $article_to_feed[$item['id']];
|
2014-07-23 15:52:50 +02:00
|
|
|
$author = isset($item['author']) ? $item['author'] : '';
|
|
|
|
$key_content = ($google_compliant && !isset($item['content'])) ?
|
|
|
|
'summary' : 'content';
|
|
|
|
$tags = $item['categories'];
|
|
|
|
if ($google_compliant) {
|
2015-02-08 18:55:48 +01:00
|
|
|
// Remove tags containing "/state/com.google" which are useless.
|
2014-07-23 15:52:50 +02:00
|
|
|
$tags = array_filter($tags, function($var) {
|
|
|
|
return strpos($var, '/state/com.google') === false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$entry = new FreshRSS_Entry(
|
2014-09-27 10:10:43 +02:00
|
|
|
$feed_id, $item['id'], $item['title'], $author,
|
2014-07-23 15:52:50 +02:00
|
|
|
$item[$key_content]['content'], $item['alternate'][0]['href'],
|
|
|
|
$item['published'], $is_read, $starred
|
|
|
|
);
|
2014-09-27 10:10:43 +02:00
|
|
|
$entry->_id(min(time(), $entry->date(true)) . uSecString());
|
2014-07-23 15:52:50 +02:00
|
|
|
$entry->_tags($tags);
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
$entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry);
|
|
|
|
if (is_null($entry)) {
|
|
|
|
// An extension has returned a null value, there is nothing to insert.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-27 10:10:43 +02:00
|
|
|
$values = $entry->toArray();
|
|
|
|
$id = $this->entryDAO->addEntry($values, $prepared_statement);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
if (!$error && ($id === false)) {
|
|
|
|
$error = true;
|
|
|
|
}
|
|
|
|
}
|
2014-09-27 10:10:43 +02:00
|
|
|
$this->entryDAO->commit();
|
2014-07-23 15:52:50 +02:00
|
|
|
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method import a JSON-based feed (Google Reader format).
|
|
|
|
*
|
|
|
|
* @param array $origin represents a feed.
|
|
|
|
* @param boolean $google_compliant takes care of some specific values if true.
|
|
|
|
* @return FreshRSS_Feed if feed is in database at the end of the process,
|
|
|
|
* else null.
|
|
|
|
*/
|
|
|
|
private function addFeedJson($origin, $google_compliant) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$default_cat = $this->catDAO->getDefault();
|
|
|
|
|
|
|
|
$return = null;
|
|
|
|
$key = $google_compliant ? 'htmlUrl' : 'feedUrl';
|
|
|
|
$url = $origin[$key];
|
|
|
|
$name = $origin['title'];
|
|
|
|
$website = $origin['htmlUrl'];
|
2014-09-21 12:12:35 +02:00
|
|
|
|
2014-07-23 15:52:50 +02:00
|
|
|
try {
|
2015-02-08 18:55:48 +01:00
|
|
|
// Create a Feed object and add it in database.
|
2014-07-23 15:52:50 +02:00
|
|
|
$feed = new FreshRSS_Feed($url);
|
|
|
|
$feed->_category($default_cat->id());
|
|
|
|
$feed->_name($name);
|
|
|
|
$feed->_website($website);
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
// Call the extension hook
|
|
|
|
$feed = Minz_ExtensionManager::callHook('feed_before_insert', $feed);
|
|
|
|
if (!is_null($feed)) {
|
|
|
|
// addFeedObject checks if feed is already in DB so nothing else to
|
|
|
|
// check here.
|
|
|
|
$id = $this->feedDAO->addFeedObject($feed);
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
if ($id !== false) {
|
|
|
|
$feed->_id($id);
|
|
|
|
$return = $feed;
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
} catch (FreshRSS_Feed_Exception $e) {
|
|
|
|
Minz_Log::warning($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This action handles export action.
|
|
|
|
*
|
|
|
|
* This action must be reached by a POST request.
|
|
|
|
*
|
|
|
|
* Parameters are:
|
|
|
|
* - export_opml (default: false)
|
|
|
|
* - export_starred (default: false)
|
|
|
|
* - export_feeds (default: array()) a list of feed ids
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
public function exportAction() {
|
2014-09-21 12:12:35 +02:00
|
|
|
if (!Minz_Request::isPost()) {
|
|
|
|
Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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', array());
|
|
|
|
|
|
|
|
$export_files = array();
|
|
|
|
if ($export_opml) {
|
|
|
|
$export_files['feeds.opml'] = $this->generateOpml();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($export_starred) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$export_files['starred.json'] = $this->generateEntries('starred');
|
2014-09-21 12:12:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($export_feeds as $feed_id) {
|
|
|
|
$feed = $this->feedDAO->searchById($feed_id);
|
|
|
|
if ($feed) {
|
|
|
|
$filename = 'feed_' . $feed->category() . '_'
|
|
|
|
. $feed->id() . '.json';
|
2015-02-08 18:55:48 +01:00
|
|
|
$export_files[$filename] = $this->generateEntries('feed', $feed);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
2014-09-21 12:12:35 +02:00
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
|
2014-09-21 12:12:35 +02:00
|
|
|
$nb_files = count($export_files);
|
|
|
|
if ($nb_files > 1) {
|
|
|
|
// If there are more than 1 file to export, we need a zip archive.
|
|
|
|
try {
|
|
|
|
$this->exportZip($export_files);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
# Oops, there is no Zip extension!
|
2015-02-08 18:55:48 +01:00
|
|
|
Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
|
2014-09-21 12:12:35 +02:00
|
|
|
array('c' => 'importExport', 'a' => 'index'));
|
|
|
|
}
|
|
|
|
} elseif ($nb_files === 1) {
|
|
|
|
// Only one file? Guess its type and export it.
|
|
|
|
$filename = key($export_files);
|
|
|
|
$type = $this->guessFileType($filename);
|
|
|
|
$this->exportFile('freshrss_' . $filename, $export_files[$filename], $type);
|
|
|
|
} else {
|
2015-02-08 18:55:48 +01:00
|
|
|
// Nothing to do...
|
2014-09-21 12:12:35 +02:00
|
|
|
Minz_Request::forward(array('c' => 'importExport', 'a' => 'index'), true);
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method returns the OPML file based on user subscriptions.
|
|
|
|
*
|
|
|
|
* @return string the OPML file content.
|
|
|
|
*/
|
2014-07-23 15:52:50 +02:00
|
|
|
private function generateOpml() {
|
|
|
|
$list = array();
|
|
|
|
foreach ($this->catDAO->listCategories() as $key => $cat) {
|
|
|
|
$list[$key]['name'] = $cat->name();
|
|
|
|
$list[$key]['feeds'] = $this->feedDAO->listByCategory($cat->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->view->categories = $list;
|
|
|
|
return $this->view->helperToString('export/opml');
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method returns a JSON file content.
|
|
|
|
*
|
|
|
|
* @param string $type must be "starred" or "feed"
|
|
|
|
* @param FreshRSS_Feed $feed feed of which we want to get entries.
|
|
|
|
* @return string the JSON file content.
|
|
|
|
*/
|
|
|
|
private function generateEntries($type, $feed = NULL) {
|
2014-07-23 15:52:50 +02:00
|
|
|
$this->view->categories = $this->catDAO->listCategories();
|
|
|
|
|
|
|
|
if ($type == 'starred') {
|
2015-02-08 18:55:48 +01:00
|
|
|
$this->view->list_title = _t('sub.import_export.starred_list');
|
2014-07-23 15:52:50 +02:00
|
|
|
$this->view->type = 'starred';
|
|
|
|
$unread_fav = $this->entryDAO->countUnreadReadFavorites();
|
|
|
|
$this->view->entries = $this->entryDAO->listWhere(
|
2015-02-08 18:55:48 +01:00
|
|
|
's', '', FreshRSS_Entry::STATE_ALL, 'ASC', $unread_fav['all']
|
2014-07-23 15:52:50 +02:00
|
|
|
);
|
|
|
|
} elseif ($type == 'feed' && !is_null($feed)) {
|
2015-02-08 18:55:48 +01:00
|
|
|
$this->view->list_title = _t('sub.import_export.feed_list', $feed->name());
|
2014-07-23 15:52:50 +02:00
|
|
|
$this->view->type = 'feed/' . $feed->id();
|
|
|
|
$this->view->entries = $this->entryDAO->listWhere(
|
|
|
|
'f', $feed->id(), FreshRSS_Entry::STATE_ALL, 'ASC',
|
2015-02-08 18:55:48 +01:00
|
|
|
FreshRSS_Context::$user_conf->posts_per_page
|
2014-07-23 15:52:50 +02:00
|
|
|
);
|
|
|
|
$this->view->feed = $feed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->view->helperToString('export/articles');
|
|
|
|
}
|
2014-09-21 12:12:35 +02:00
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method zips a list of files and returns it by HTTP.
|
|
|
|
*
|
|
|
|
* @param array $files list of files where key is filename and value the content.
|
|
|
|
* @throws Exception if Zip extension is not loaded.
|
|
|
|
*/
|
2014-09-21 12:12:35 +02:00
|
|
|
private function exportZip($files) {
|
|
|
|
if (!extension_loaded('zip')) {
|
|
|
|
throw new Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
// From https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
|
|
|
|
$zip_file = tempnam('tmp', 'zip');
|
|
|
|
$zip = new ZipArchive();
|
|
|
|
$zip->open($zip_file, ZipArchive::OVERWRITE);
|
|
|
|
|
|
|
|
foreach ($files as $filename => $content) {
|
|
|
|
$zip->addFromString($filename, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close and send to user
|
|
|
|
$zip->close();
|
|
|
|
header('Content-Type: application/zip');
|
|
|
|
header('Content-Length: ' . filesize($zip_file));
|
|
|
|
header('Content-Disposition: attachment; filename="freshrss_export.zip"');
|
|
|
|
readfile($zip_file);
|
|
|
|
unlink($zip_file);
|
|
|
|
}
|
|
|
|
|
2015-02-08 18:55:48 +01:00
|
|
|
/**
|
|
|
|
* This method returns a single file (OPML or JSON) by HTTP.
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $content
|
|
|
|
* @param string $type the file type (opml, json_feed or json_starred).
|
|
|
|
* If equals to unknown, nothing happens.
|
|
|
|
*/
|
2014-09-21 12:12:35 +02:00
|
|
|
private function exportFile($filename, $content, $type) {
|
|
|
|
if ($type === 'unknown') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content_type = '';
|
|
|
|
if ($type === 'opml') {
|
|
|
|
$content_type = "text/opml";
|
|
|
|
} elseif ($type === 'json_feed' || $type === 'json_starred') {
|
|
|
|
$content_type = "text/json";
|
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: ' . $content_type . '; charset=utf-8');
|
|
|
|
header('Content-disposition: attachment; filename=' . $filename);
|
|
|
|
print($content);
|
|
|
|
}
|
2014-07-23 15:52:50 +02:00
|
|
|
}
|