Просмотр исходного кода

Fix filenames for files exported with CLI (#2932)

Filenames were created with the username of the current user. However,
when we export the files with the CLI, the current user is "_".

This commit makes the username always required in the `exportFile`
method so we make sure to always manipulate a real value. Consequently,
the filenames can be formatted correctly.

Obviously, this has absolutely no impacts since the CLI doesn't consider
the HTTP headers. It just makes things a bit more clear. It's a first
step to remove the concept of "default user".
Marien Fressinaud 6 лет назад
Родитель
Сommit
8f188ffa84

+ 27 - 11
app/Controllers/importExportController.php

@@ -720,7 +720,21 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		return $return;
 	}
 
-	public function exportFile($export_opml = true, $export_starred = false, $export_labelled = false, $export_feeds = array(), $maxFeedEntries = 50, $username = null) {
+	/**
+	 * Export the files of a user and send them to the user by HTTP
+	 *
+	 * @param string $username
+	 * @param boolean $export_opml
+	 * @param boolean $export_starred
+	 * @param boolean $export_labelled
+	 * @param array|boolean $export_feeds The list of feeds ids to export, true to export all the feeds
+	 * @param integer $maxFeedEntries Limit the number of entries to export (default is 50)
+	 *
+	 * @throws FreshRSS_ZipMissing_Exception if we try to export more than two files and the zip extension doesn't exist
+	 *
+	 * @return integer The number of exported files
+	 */
+	public function exportFile($username, $export_opml = true, $export_starred = false, $export_labelled = false, $export_feeds = array(), $maxFeedEntries = 50) {
 		require_once(LIB_PATH . '/lib_opml.php');
 
 		$this->catDAO = new FreshRSS_CategoryDAO($username);
@@ -761,8 +775,9 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		$nb_files = count($export_files);
 		if ($nb_files > 1) {
 			// If there are more than 1 file to export, we need a ZIP archive.
+			$filename = 'freshrss_' . $username . '_' . $day . '_export.zip';
 			try {
-				$this->sendZip($export_files);
+				$this->sendZip($filename, $export_files);
 			} catch (Exception $e) {
 				throw new FreshRSS_ZipMissing_Exception($e);
 			}
@@ -770,7 +785,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 			// Only one file? Guess its type and export it.
 			$filename = key($export_files);
 			$type = self::guessFileType($filename);
-			$this->sendFile('freshrss_' . Minz_Session::param('currentUser', '_') . '_' . $filename, $export_files[$filename], $type);
+			$this->sendFile('freshrss_' . $username . '_' . $filename, $export_files[$filename], $type);
 		}
 		return $nb_files;
 	}
@@ -794,11 +809,12 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		$nb_files = 0;
 		try {
 			$nb_files = $this->exportFile(
-					Minz_Request::param('export_opml', false),
-					Minz_Request::param('export_starred', false),
-					Minz_Request::param('export_labelled', false),
-					Minz_Request::param('export_feeds', array())
-				);
+				Minz_Session::param('currentUser'),
+				Minz_Request::param('export_opml', false),
+				Minz_Request::param('export_starred', false),
+				Minz_Request::param('export_labelled', false),
+				Minz_Request::param('export_feeds', array())
+			);
 		} catch (FreshRSS_ZipMissing_Exception $zme) {
 			# Oops, there is no ZIP extension!
 			Minz_Request::bad(_t('feedback.import_export.export_no_zip_extension'),
@@ -862,10 +878,11 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 	/**
 	 * This method zips a list of files and returns it by HTTP.
 	 *
+	 * @param string $export_filename The name of the file to export
 	 * @param array $files list of files where key is filename and value the content.
 	 * @throws Exception if Zip extension is not loaded.
 	 */
-	private function sendZip($files) {
+	private function sendZip($export_filename, $files) {
 		if (!extension_loaded('zip')) {
 			throw new Exception();
 		}
@@ -883,8 +900,7 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		$zip->close();
 		header('Content-Type: application/zip');
 		header('Content-Length: ' . filesize($zip_file));
-		$day = date('Y-m-d');
-		header('Content-Disposition: attachment; filename="freshrss_' . Minz_Session::param('currentUser', '_') . '_' . $day . '_export.zip"');
+		header('Content-Disposition: attachment; filename="' . $export_filename . '"');
 		readfile($zip_file);
 		unlink($zip_file);
 	}

+ 1 - 1
cli/export-opml-for-user.php

@@ -19,7 +19,7 @@ fwrite(STDERR, 'FreshRSS exporting OPML for user “' . $username . "”…\n");
 $importController = new FreshRSS_importExport_Controller();
 
 $ok = false;
-$ok = $importController->exportFile(true, false, false, array(), 0, $username);
+$ok = $importController->exportFile($username, true, false, false, array(), 0);
 
 invalidateHttpCache($username);
 

+ 2 - 3
cli/export-zip-for-user.php

@@ -20,10 +20,9 @@ fwrite(STDERR, 'FreshRSS exporting ZIP for user “' . $username . "”…\n");
 $importController = new FreshRSS_importExport_Controller();
 
 $ok = false;
+$number_entries = empty($options['max-feed-entries']) ? 100 : intval($options['max-feed-entries']);
 try {
-	$ok = $importController->exportFile(true, true, true, true,
-		empty($options['max-feed-entries']) ? 100 : intval($options['max-feed-entries']),
-		$username);
+	$ok = $importController->exportFile($username, true, true, true, true, $number_entries);
 } catch (FreshRSS_ZipMissing_Exception $zme) {
 	fail('FreshRSS error: Lacking php-zip extension!');
 }