From d20eea9761238e0569f538c6f8b1bb553068bf7b Mon Sep 17 00:00:00 2001
From: Tom Needham <needham.thomas@gmail.com>
Date: Sat, 31 Mar 2012 22:41:43 +0000
Subject: [PATCH] Use ajax to download file, OC_Dialogs for errors

---
 apps/user_migrate/ajax/export.php        | 63 ++++++++++++++++++++++++
 apps/user_migrate/appinfo/app.php        |  1 +
 apps/user_migrate/js/export.js           | 27 ++++++++++
 apps/user_migrate/settings.php           | 22 ++-------
 apps/user_migrate/templates/settings.php | 14 +++---
 lib/migrate.php                          |  4 +-
 6 files changed, 102 insertions(+), 29 deletions(-)
 create mode 100644 apps/user_migrate/ajax/export.php
 create mode 100644 apps/user_migrate/js/export.js

diff --git a/apps/user_migrate/ajax/export.php b/apps/user_migrate/ajax/export.php
new file mode 100644
index 0000000000..ef947c610f
--- /dev/null
+++ b/apps/user_migrate/ajax/export.php
@@ -0,0 +1,63 @@
+<?php
+
+/**
+ * ownCloud - user_migrate
+ *
+ * @author Tom Needham
+ * @copyright 2012 Tom Needham tom@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+// Init owncloud
+require_once('../../../lib/base.php');
+
+// Check if we are a user
+OC_JSON::checkLoggedIn();
+OC_Util::checkAppEnabled('user_migrate');
+	OC_JSON::error();	
+	die();
+// Which operation
+if( $_GET['operation']=='create' ){
+$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] :  OC_User::getUser();
+if( $uid != OC_User::getUser() ){
+    // Needs to be admin to export someone elses account
+	OC_JSON::error();	
+	die();
+}
+// Create the export zip
+if( !$path = OC_Migrate::export( $uid ) ){
+	// Error
+	OC_JSON::error();
+	die();
+} else {
+	// Save path in session
+	$_SESSION['ocuserexportpath'] = $path;
+}
+OC_JSON::success();
+die();
+} else if( $_GET['operation']=='download' ){
+	// Download the export
+	$path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false;
+	if( !$path ){
+		die();	
+	}
+	header("Content-Type: application/zip");
+	header("Content-Disposition: attachment; filename=" . basename($path));
+	header("Content-Length: " . filesize($path));
+	@ob_end_clean();
+	readfile($path);
+	unlink( $path );
+	$_SESSION['ocuserexportpath'] = '';	
+}
diff --git a/apps/user_migrate/appinfo/app.php b/apps/user_migrate/appinfo/app.php
index 18ea8f52b2..a59b6dd705 100644
--- a/apps/user_migrate/appinfo/app.php
+++ b/apps/user_migrate/appinfo/app.php
@@ -23,6 +23,7 @@
 
 OC_APP::registerPersonal( 'user_migrate', 'settings' );
 OC_APP::registerAdmin( 'user_migrate', 'admin' );
+OC_Util::addScript( 'user_migrate', 'export');
 
 // add settings page to navigation
 $entry = array(
diff --git a/apps/user_migrate/js/export.js b/apps/user_migrate/js/export.js
new file mode 100644
index 0000000000..0e1e396f65
--- /dev/null
+++ b/apps/user_migrate/js/export.js
@@ -0,0 +1,27 @@
+$(document).ready(function(){
+    // Do the export
+	$('#exportbtn').click(function(){
+		// Show loader
+		$('.loading').show();
+		$.getJSON(
+			OC.filePath('user_migrate','ajax','export.php'),
+			{operation:'create'},
+			function(result){
+				if(result.status == 'success'){
+					// Download the file
+					window.location = OC.filePath('user_migrate','ajax','export.php?operation=download')	;
+					$('.loading').hide();
+					$('#exportbtn').val(t('user_migrate', 'Export'));
+				} else {
+					// Cancel loading
+					$('#exportbtn').html('Failed');
+					// Show Dialog	
+					OC.dialogs.alert(t('user_migrate', 'Something went wrong while the export file was being generated'), t('user_migrate', 'An error has occurred'), function(){ 
+						$('#exportbtn').html(t('user_migrate', 'Export')+'<img style="display: none;" class="loading" src="'+OC.filePath('core','img','loading.git')+'" />'); 
+					});
+				}
+			}
+		// End ajax
+		);
+	});
+});
\ No newline at end of file
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php
index 38eee990b4..62f347b355 100644
--- a/apps/user_migrate/settings.php
+++ b/apps/user_migrate/settings.php
@@ -24,22 +24,6 @@
  */
 OC_Util::checkAppEnabled('user_migrate');
 
-if (isset($_POST['user_export'])) {
-	// Create the export zip
-	if( !$path = OC_Migrate::export() ){
-		// Error
-		die('error');	
-	} else {
-		// Download it
-		header("Content-Type: application/zip");
-		header("Content-Disposition: attachment; filename=" . basename($path));
-		header("Content-Length: " . filesize($path));
-		@ob_end_clean();
-		readfile($path);
-		unlink( $path );		
-	}
-} else {
-	// fill template
-	$tmpl = new OC_Template('user_migrate', 'settings');
-	return $tmpl->fetchPage();
-}
\ No newline at end of file
+// fill template
+$tmpl = new OC_Template('user_migrate', 'settings');
+return $tmpl->fetchPage();
\ No newline at end of file
diff --git a/apps/user_migrate/templates/settings.php b/apps/user_migrate/templates/settings.php
index 389de563a6..5f4857de5f 100644
--- a/apps/user_migrate/templates/settings.php
+++ b/apps/user_migrate/templates/settings.php
@@ -1,8 +1,6 @@
-<form id="export" action="#" method="post">
-    <fieldset class="personalblock">
-        <legend><strong><?php echo $l->t('Export your user account');?></strong></legend>
-        <p><?php echo $l->t('This will create a compressed file that contains your ownCloud account.');?>
-        </p>
-        <input type="submit" name="user_export" value="Export" />
-    </fieldset>
-</form>
\ No newline at end of file
+<fieldset class="personalblock">
+	<legend><strong><?php echo $l->t('Export your user account');?></strong></legend>
+	<p><?php echo $l->t('This will create a compressed file that contains your ownCloud account.');?>
+	</p>
+	<button id="exportbtn">Export<img style="display: none;" class="loading" src="<?php echo OC_Helper::linkTo('core', 'img/loading.gif'); ?>" /></button>
+</fieldset>
diff --git a/lib/migrate.php b/lib/migrate.php
index fe5ac45600..db3852fb83 100644
--- a/lib/migrate.php
+++ b/lib/migrate.php
@@ -73,12 +73,12 @@ class OC_Migrate{
 	
 	/**
 	 * @breif exports a user, or owncloud instance
+	 * @param optional $uid string user id of user to export if export type is user, defaults to current
 	 * @param ootional $type string type of export, defualts to user
 	 * @param otional $path string path to zip output folder
-	 * @param optional $uid string user id of user to export if export type is user, defaults to current
 	 * @return false on error, path to zip on success 
 	 */
-	 public static function export( $type='user', $path=null, $uid=null ){
+	 public static function export( $uid=null, $type='user', $path=null ){
 		$datadir = OC_Config::getValue( 'datadirectory' );
 	 	// Validate export type
 	 	$types = array( 'user', 'instance', 'system', 'userfiles' );
-- 
GitLab