From 773d7b119d20d5962817cdd057bf68c8cb39d529 Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <schiessle@owncloud.com>
Date: Fri, 3 Aug 2012 13:52:41 +0200
Subject: [PATCH] OCS api calls cleanup

---
 apps/files_encryption/lib/keymanager.php |  28 ++--
 lib/ocs.php                              | 172 +++++++++--------------
 2 files changed, 83 insertions(+), 117 deletions(-)

diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php
index 6e3dcaf0ad9..825a3f78fcd 100644
--- a/apps/files_encryption/lib/keymanager.php
+++ b/apps/files_encryption/lib/keymanager.php
@@ -32,11 +32,11 @@ class Keymanager {
 	/**
 	 * @brief retrieve private key from a user
 	 * 
-	 * @param string user name
 	 * @return string private key or false
 	 */
-	public static function getPrivateKey( $user ) {
+	public static function getPrivateKey() {
 
+		$user = \OCP\User::getUser();
 		$view = new \OC_FilesystemView( '/' . $user . '/' . 'files_encryption' );
 		
 		return $view->file_get_contents( '/' . $user.'.private.key' );
@@ -91,17 +91,16 @@ class Keymanager {
 	 * @brief retrieve file encryption key
 	 *
 	 * @param string file name
-	 * @param string user name of the file owner
 	 * @return string file key or false
 	 */
-	public static function getFileKey( $userId, $path ) {
+	public static function getFileKey( $path ) {
 		
 		$keypath = ltrim( $path, '/' );
-		$user = $userId;
+		$user = \OCP\User::getUser();
 
 		// update $keypath and $user if path point to a file shared by someone else
 		$query = \OC_DB::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
-		$result = $query->execute( array ('/'.$userId.'/files/'.$keypath, $userId));
+		$result = $query->execute( array ('/'.$user.'/files/'.$keypath, $user));
 		if ($row = $result->fetchRow()){
 			$keypath = $row['source'];
 			$keypath_parts=explode('/',$keypath);
@@ -114,16 +113,16 @@ class Keymanager {
 	}	
 	
 	/**
-	 * @brief store private key from a user
+	 * @brief store private key from the user
 	 *
-	 * @param string user name
 	 * @param string key
 	 * @return bool true/false
 	 */
-	public static function setPrivateKey($user, $key) {
+	public static function setPrivateKey($key) {
 
 		\OC_FileProxy::$enabled = false;
 		
+		$user = \OCP\User::getUser();
 		$view = new \OC_FilesystemView('/'.$user.'/files_encryption');
 		if (!$view->file_exists('')) $view->mkdir('');
 		$result = $view->file_put_contents($user.'.private.key', $key);
@@ -135,19 +134,18 @@ class Keymanager {
 	
 	
 	/**
-	 * @brief store public key from a user
+	 * @brief store public key of the user
 	 *
-	 * @param string user name
 	 * @param string key
 	 * @return bool true/false
 	 */
-	public static function setPublicKey($user, $key) {
+	public static function setPublicKey($key) {
 		
 		\OC_FileProxy::$enabled = false;
 		
 		$view = new \OC_FilesystemView('/public-keys');
 		if (!$view->file_exists('')) $view->mkdir('');
-		$result = $view->file_put_contents($user.'.public.key', $key);
+		$result = $view->file_put_contents(\OCP\User::getUser().'.public.key', $key);
 		
 		\OC_FileProxy::$enabled = true;
 		
@@ -157,16 +155,16 @@ class Keymanager {
 	/**
 	 * @brief store file encryption key
 	 *
-	 * @param string $userId name of the file owner
 	 * @param string $path relative path of the file, including filename
 	 * @param string $key
 	 * @return bool true/false
 	 */
-	public static function setFileKey( $user, $path, $key, $view, $dbClassName, $fileProxyClassName ) {
+	public static function setFileKey( $path, $key, $view, $dbClassName, $fileProxyClassName ) {
 
 		$fileProxyClassName::$enabled = false;
 
 		$targetpath = ltrim(  $path, '/'  );
+		$user = \OCP\User::getUser();
 		
 		// update $keytarget and $user if key belongs to a file shared by someone else
 		$query = $dbClassName::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
diff --git a/lib/ocs.php b/lib/ocs.php
index 6617beb8066..97314d71ced 100644
--- a/lib/ocs.php
+++ b/lib/ocs.php
@@ -174,34 +174,29 @@ class OC_OCS {
 			OC_OCS::publicKeyGet($format,$file);
 
 		//keysetpublic
-		}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'publickey')){
-				$user=$ex[$paracount-3];
+		}elseif(($method=='post') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'publickey')){
 				$key = self::readData('post', 'key', 'string');
-				OC_OCS::publicKeySet($format,$user, $key);
+				OC_OCS::publicKeySet($format, $key);
 		
 		// keygetprivate 
-		}elseif(($method=='get') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'privatekey')){
-			$user=$ex[$paracount-3];
-			OC_OCS::privateKeyGet($format,$user);
+		}elseif(($method=='get') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'privatekey')){
+			OC_OCS::privateKeyGet($format);
 		
 		//keysetprivate
-		}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'privatekey')){
-				$user=$ex[$paracount-3];
+		}elseif(($method=='post') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'privatekey')){
 				$key = self::readData('post', 'key', 'string');
-				OC_OCS::privateKeySet($format,$user, $key);
-				
+				OC_OCS::privateKeySet($format, $key);
+			
 		// keygetfiles
-		}elseif(($method=='get') and ($ex[$paracount-7] == 'v1.php') and ($ex[$paracount-6]=='cloud') and ($ex[$paracount-5] == 'user') and ($ex[$paracount-3] == 'filekey')){
-			$user=$ex[$paracount-4];
-			$file = urldecode($ex[$paracount-2]);
-			OC_OCS::fileKeyGet($format,$user, $file);
+		}elseif(($method=='get') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'file') and ($ex[$paracount-2] == 'filekey')){
+			$file = urldecode($ex[$paracount-3]);
+			OC_OCS::fileKeyGet($format, $file);
 		
 		//keysetfiles
-		}elseif(($method=='post') and ($ex[$paracount-6] == 'v1.php') and ($ex[$paracount-5]=='cloud') and ($ex[$paracount-4] == 'user') and ($ex[$paracount-2] == 'filekey')){
-			$user=$ex[$paracount-3];
+		}elseif(($method=='post') and ($ex[$paracount-4] == 'v1.php') and ($ex[$paracount-3]=='cloud') and ($ex[$paracount-2] == 'filekey')){
 			$key = self::readData('post', 'key', 'string');
 			$file = self::readData('post', 'file', 'string');
-			OC_OCS::fileKeySet($format,$user, $file, $key);
+			OC_OCS::fileKeySet($format, $file, $key);
 
 // add more calls here
 // please document all the call in the draft spec
@@ -669,7 +664,7 @@ class OC_OCS {
         }
 
         /**
-        * get the public key of a user
+        * get the public key from all users associated with a given file
         * @param string $format
         * @param string $file
         * @return string xml/json list of public keys
@@ -692,130 +687,103 @@ class OC_OCS {
 
         /**
          * set the public key of a user
-         * @param string $format
-         * @param string $user
+         * @param string $format
          * @param string $key
          * @return string xml/json
          */
-        private static function publicKeySet($format, $user, $key) {
+        private static function publicKeySet($format, $key) {
         	$login=OC_OCS::checkpassword();
-        	if(($login==$user)) {
-        		if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
-        			if (($key = OCA_Encryption\Keymanager::setPublicKey($user, $key))) {
-        				echo self::generateXml('', 'ok', 100, '');
-        			} else {
-        				echo self::generateXml('', 'fail', 404, 'could not add your public key to the key storage');
-        			}
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        		if (OCA_Encryption\Keymanager::setPublicKey($key)) {
+        			echo self::generateXml('', 'ok', 100, '');
         		} else {
-        			echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        			echo self::generateXml('', 'fail', 404, 'could not add your public key to the key storage');
         		}
-        	}else{
-        		echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
         	}
         }
         	
         /**
         * get the private key of a user
         * @param string $format
-        * @param string $user
         * @return string xml/json
         */
-        private static function privateKeyGet($format, $user) {
-        	$login=OC_OCS::checkpassword();
-        	if(($login==$user)) {
-        		if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
-        			if (($key = OCA_Encryption\Keymanager::getPrivateKey($user))) {
-        				$xml=array();
-        				$xml['key']=$key;
-        				$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
-        				echo($txt);
-        			} else {
-        				echo self::generateXml('', 'fail', 404, 'private key does not exist');
-        			}
-        		} else {
-        			echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        private static function privateKeyGet($format) {
+        	$login=OC_OCS::checkpassword();
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        		if (($key = OCA_Encryption\Keymanager::getPrivateKey())) {
+        			$xml=array();
+        			$xml['key']=$key;
+        			$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
+        			echo($txt);
+        		} else {
+        			echo self::generateXml('', 'fail', 404, 'private key does not exist');
         		}
-        	}else{
-        		echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
         	}
         }
 		
 		/**
 		 * set the private key of a user
-		 * @param string $format
-		 * @param string $user
+		 * @param string $format
 		 * @param string $key
 		 * @return string xml/json
 		 */
-        private static function privateKeySet($format, $user, $key) {
+        private static function privateKeySet($format, $key) {
         	$login=OC_OCS::checkpassword();
-        	if(($login==$user)) {
-        		if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
-        			if (($key = OCA_Encryption\Keymanager::setPrivateKey($user, $key))) {
-        				echo self::generateXml('', 'ok', 100, '');
-        			} else {
-        				echo self::generateXml('', 'fail', 404, 'could not add your private key to the key storage');
-        			}
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        		if (($key = OCA_Encryption\Keymanager::setPrivateKey($key))) {
+        			echo self::generateXml('', 'ok', 100, '');
         		} else {
-        			echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        			echo self::generateXml('', 'fail', 404, 'could not add your private key to the key storage');
         		}
-        	}else{
-        		echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
         	}
         }
 
 		/**
 		 * get the encryption key of a file
-		 * @param string $format
-		 * @param string $user
+		 * @param string $format
 		 * @param string $file
 		 * @return string xml/json
 		 */
-		private static function fileKeyGet($format, $user, $file) {
-			$login=OC_OCS::checkpassword();
-			if(($login==$user)) {
-				if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
-					if (($key = OCA_Encryption\Keymanager::getFileKey($user, $file))) {
-						$xml=array();
-						$xml['key']=$key;					
-						$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
-						echo($txt);
-					} else {
-						echo self::generateXml('', 'fail', 404, 'file key does not exist');
-					}
-				} else {
-					echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
-				}
-			}else{
-				echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
-			}
-		}
+        private static function fileKeyGet($format, $file) {
+        	$login=OC_OCS::checkpassword();
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        		if (($key = OCA_Encryption\Keymanager::getFileKey($file))) {
+        			$xml=array();
+        			$xml['key']=$key;
+        			$txt=OC_OCS::generatexml($format, 'ok', 100, '', $xml, 'cloud', '', 1, 0, 0);
+        			echo($txt);
+        		} else {
+        			echo self::generateXml('', 'fail', 404, 'file key does not exist');
+        		}
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        	}
+        }
 		
 		/**
 		 * set the encryption keyn of a file
-		 * @param string $format
-		 * @param string $user
+		 * @param string $format
 		 * @param string $file
 		 * @param string $key
 		 * @return string xml/json
 		 */
-		private static function fileKeySet($format, $user, $file, $key) {
-			$login=OC_OCS::checkpassword();
-			if(($login==$user)) {
-				if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
-					if (($key = OCA_Encryption\Keymanager::setFileKey($user, $file, $key))) {
-						echo self::generateXml('', 'ok', 100, '');
-						return true;
-					} else {
-						echo self::generateXml('', 'fail', 404, 'could not write key file');
-					}
-				} else {
-					echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
-				}
-			}else{
-				echo self::generateXml('', 'fail', 300, 'You don´t have permission to access this ressource.');
-			}
-			return false;
-		}
+        private static function fileKeySet($format, $file, $key) {
+        	$login=OC_OCS::checkpassword();
+        	if(OC_App::isEnabled('files_encryption') && OCA_Encryption\Crypt::mode($user) === 'client') {
+        		if (($key = OCA_Encryption\Keymanager::setFileKey($file, $key))) {
+        			echo self::generateXml('', 'ok', 100, '');
+        		} else {
+        			echo self::generateXml('', 'fail', 404, 'could not write key file');
+        		}
+        	} else {
+        		echo self::generateXml('', 'fail', 300, 'Client side encryption not enabled for user ' . $user);
+        	}
+        }
 
 }
-- 
GitLab