From 9fe7f992d9323f4509d7fa7c0f895c22141dcc06 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind1991@gmail.com>
Date: Tue, 6 Jul 2010 12:50:37 +0200
Subject: [PATCH] Initial work on transering files between servers

---
 files/api.php       |  2 ++
 files/pull.php      | 11 ++++++++
 inc/lib_connect.php | 67 +++++++++++++++++++++++++++++++++++++++++++++
 inc/lib_files.php   | 30 ++++++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100644 files/pull.php

diff --git a/files/api.php b/files/api.php
index 014bbb56bbc..fa94a512547 100755
--- a/files/api.php
+++ b/files/api.php
@@ -72,6 +72,8 @@ if($arguments['action']){
 				echo 'false';
 			}
 			break;
+		case 'pull':
+			return OC_FILES::pull($arguments['source'],$arguments['token'],$arguments['dir'],$arguments['file']);
 	}
 }
 
diff --git a/files/pull.php b/files/pull.php
new file mode 100644
index 00000000000..1cc82425845
--- /dev/null
+++ b/files/pull.php
@@ -0,0 +1,11 @@
+<?php
+$token=$_GET['token'];
+
+$file=sys_get_temp_dir().'/'.'remoteCloudFile'.$token;
+if(file_exists($file) and is_readable($file) and is_writable($file)){
+	readfile($file);
+	unlink($file);
+}else{
+	header("HTTP/1.0 404 Not Found");
+}
+?>
\ No newline at end of file
diff --git a/inc/lib_connect.php b/inc/lib_connect.php
index ddf03eb6e74..0c56d1a01d5 100644
--- a/inc/lib_connect.php
+++ b/inc/lib_connect.php
@@ -78,6 +78,7 @@ class OC_REMOTE_CLOUD{
 		$result=trim(curl_exec($ch));
 		$info=curl_getinfo($ch);
 		$httpCode=$info['http_code'];
+		curl_close($ch);
 		if($httpCode==200 or $httpCode==0){
 			return json_decode($result,$assoc);
 		}else{
@@ -130,6 +131,48 @@ class OC_REMOTE_CLOUD{
 		}
 		return $this->apiCall('getfiles',array('dir'=>$dir),true);
 	}
+	
+	/**
+	* get a remove file and save it in a temporary file and return the path of the temporary file
+	* @param string $dir
+	* @param string $file
+	* @return string
+	*/
+	public function getFile($dir, $file){
+		if(!$this->connected){
+			return false;
+		}
+		$ch=curl_init();
+		if(!$this->cookiefile){
+			$this->cookiefile=sys_get_temp_dir().'/remoteCloudCookie'.uniqid();
+		}
+		$tmpfile=tempnam(sys_get_temp_dir(),'remoteCloudFile');
+		$fp=fopen($tmpfile,'w+');
+		$url=$this->path.="/files/api.php?action=get&dir=$dir&file=$file";
+		curl_setopt($ch,CURLOPT_URL,$url);
+		curl_setopt($ch, CURLOPT_COOKIEFILE,$this->cookiefile); 
+		curl_setopt($ch, CURLOPT_COOKIEJAR,$this->cookiefile); 
+		curl_setopt($ch, CURLOPT_FILE, $fp);
+		curl_exec($ch);
+		fclose($fp);
+		curl_close($ch);
+		return $tmpfile;
+	}
+	
+	public function sendFile($sourceDir,$sourceFile,$targetDir,$targetFile){
+		global $WEBROOT;
+		$source=$sourceDir.'/'.$sourceFile;
+		$tmp=OC_FILESYSTEM::toTmpFile($source);
+		$token=sha1(uniqid().$source);
+		$file=sys_get_temp_dir().'/'.'remoteCloudFile'.$token;
+		rename($tmp,$file);
+		if((isset($CONFIG_HTTPFORCESSL) and $CONFIG_HTTPFORCESSL) or isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') { 
+			$url = "https://". $_SERVER['SERVER_NAME'] . $WEBROOT;
+		}else{
+			$url = "http://". $_SERVER['SERVER_NAME'] . $WEBROOT;
+		}
+		return $this->apiCall('pull',array('dir'=>$targetDir,'file'=>$targetFile,'token'=>$token,'source'=>$url),true);
+	}
 }
 
 function OC_CONNECT_TEST($path,$user,$password){
@@ -146,6 +189,30 @@ function OC_CONNECT_TEST($path,$user,$password){
 				foreach($files as $file){
 					echo "{$file['type']} {$file['name']}: {$file['size']} bytes<br/>";
 				}
+				echo 'getting file "'.$file['name'].'"...';
+				$size=$file['size'];
+				$file=$remote->getFile('',$file['name']);
+				if(file_exists($file)){
+					$newSize=filesize($file);
+					if($size!=$newSize){
+						echo "fail<br/>Error: $newSize bytes received, $size expected.";
+						echo '<br/><br/>Recieved file:<br/>';
+						readfile($file);
+						unlink($file);
+						return;
+					}
+					OC_FILESYSTEM::fromTmpFile($file,'/remoteFile');
+					echo 'done<br/>';
+					echo 'sending file "burning_avatar.png"...';
+					$res=$remote->sendFile('','burning_avatar.png','','burning_avatar.png');
+					if($res){
+						echo 'done<br/>';
+					}else{
+						echo 'fail<br/>';
+					}
+				}else{
+					echo 'fail<br/>';
+				}
 			}else{
 				echo 'fail<br/>';
 			}
diff --git a/inc/lib_files.php b/inc/lib_files.php
index 1702ef20de6..20d40669d77 100755
--- a/inc/lib_files.php
+++ b/inc/lib_files.php
@@ -242,6 +242,36 @@ class OC_FILES {
 	static function getMimeType($path){
 		return OC_FILESYSTEM::getMimeType($path);
 	}
+	
+	/**
+	* pull a file from a remote server
+	* @param  string  source
+	* @param  string  token
+	* @param  string  dir
+	* @param  string  file
+	* @return string  guessed mime type
+	*/
+	static function pull($source,$token,$dir,$file){
+		$tmpfile=tempnam(sys_get_temp_dir(),'remoteCloudFile');
+		$fp=fopen($tmpfile,'w+');
+		$url=$source.="/files/pull.php?token=$token";
+		$ch=curl_init();
+		curl_setopt($ch,CURLOPT_URL,$url);
+		curl_setopt($ch,CURLOPT_POST,count($parameters));
+		curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
+		curl_setopt($ch, CURLOPT_FILE, $fp);
+		curl_exec($ch);
+		fclose($fp);
+		$info=curl_getinfo($ch);
+		$httpCode=$info['http_code'];
+		curl_close($ch);
+		if($httpCode==200 or $httpCode==0){
+			OC_FILESYSTEM::fromTmpFile($tmpfile,$dir.'/'.$file);
+			return true;
+		}else{
+			return false;
+		}
+	}
 }
 
 function zipAddDir($dir,$zip,$internalDir=''){
-- 
GitLab