From 18617711401e2a9caf302eeb836b958a82fe2a7a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 29 Apr 2011 21:55:19 +0000 Subject: [PATCH] moved functionality into unhosted.php class --- www/unhosted/callback.html | 21 +++++++------ www/unhosted/config.js | 6 ++-- www/unhosted/do.php | 10 +++++++ www/unhosted/login.html | 2 +- www/unhosted/register.html | 54 +++++++++++++++++++++------------- www/unhosted/register.php | 10 ++++++- www/unhosted/unhosted.php | 60 +++++++++++++++++++++++++++++++++----- 7 files changed, 120 insertions(+), 43 deletions(-) create mode 100644 www/unhosted/do.php diff --git a/www/unhosted/callback.html b/www/unhosted/callback.html index 4234d9ade..315ea5c55 100644 --- a/www/unhosted/callback.html +++ b/www/unhosted/callback.html @@ -2,7 +2,6 @@ - diff --git a/www/unhosted/config.js b/www/unhosted/config.js index fa2031d0c..4443911b4 100644 --- a/www/unhosted/config.js +++ b/www/unhosted/config.js @@ -3,11 +3,11 @@ var appBaseUrl = "http://dev.unhosted.org"; var config = { appUrl: appBaseUrl + "/", - walletUrl: appBaseUrl + "/unhosted/wallet.php", + doUrl: appBaseUrl + "/unhosted/do.php", loginUrl: appBaseUrl + "/unhosted/login.html", registerUrl: appBaseUrl + "/unhosted/register.html", - walletRegisterUrl: appBaseUrl + "/unhosted/register.php", callbackUrl: appBaseUrl+ "/unhosted/callback.html", appName: "My Favourite Sandwich", - dataScope: "www.myfavouritesandwich.org" + dataScope: "recipes", + homeDomain: "dev.unhosted.org" } diff --git a/www/unhosted/do.php b/www/unhosted/do.php new file mode 100644 index 000000000..08d4677c6 --- /dev/null +++ b/www/unhosted/do.php @@ -0,0 +1,10 @@ +registerHosted();break; + case "register_wallet": echo $unhostedAccount->registerWallet($_GET["dav_base_url"], $_GET["dav_token"]); break; + case "add_app": echo $unhostedAccount->addApp($_GET["scope"]);break; + case "get_wallet": echo $unhostedAccount->getWallet($_GET["scope"]);break; +} diff --git a/www/unhosted/login.html b/www/unhosted/login.html index 84818d776..568432348 100644 --- a/www/unhosted/login.html +++ b/www/unhosted/login.html @@ -22,7 +22,7 @@ function takeMeIn(userAddress, pwd, havePwd) { if(havePwd) { xhr = new XMLHttpRequest(); - xhr.open("GET", config.walletUrl+"?user_name="+userAddress+"&pwd="+pwd, true); + xhr.open("GET", config.doUrl+"?action=get_wallet&user_adress="+userAddress+"&pwd="+pwd, true); xhr.onreadystatechange = function() { var wallet; if(xhr.readyState == 4) { diff --git a/www/unhosted/register.html b/www/unhosted/register.html index ee5c486a0..5027c55e1 100644 --- a/www/unhosted/register.html +++ b/www/unhosted/register.html @@ -14,14 +14,24 @@ diff --git a/www/unhosted/register.php b/www/unhosted/register.php index 3693446ae..2a3a0dceb 100644 --- a/www/unhosted/register.php +++ b/www/unhosted/register.php @@ -21,7 +21,15 @@ function createUser($userName, $userDomain, $pwd) { )); } } - +function getUserWallet($userAddress) { + $token = getToken($userAddress); + return json_encode(array( + "userAddress" => $userName . '@' . $userDomain, + "davBaseUrl" => UnhostedSettings::domain, + "davAuth" => $davAuth, + "cryptoPwd" => null + )); +} if($_GET["userAddress"]) { list($userName, $userDomain) = explode("@", $_GET["userAddress"]); echo(createUser($userName, $userDomain, $_GET["pwd"])); diff --git a/www/unhosted/unhosted.php b/www/unhosted/unhosted.php index ee89abcee..89509cf77 100644 --- a/www/unhosted/unhosted.php +++ b/www/unhosted/unhosted.php @@ -1,12 +1,26 @@ userAddress = $userAddress; + list($this->userName, $this->userDomain) = explode("@", $userAddress); + $this->pwd = $pwd; + } + private function createUserDir() { + $userDomainDir = UnhostedSettings::davDir . $this->userDomain . '/'; + $userDir = $userDomainDir . strtolower($this->userName); + if(is_dir($userDir)) { + return false; + } + mkdir($userDomainDir); + mkdir($userDir); + file_put_contents($userDir."/.htpasswd", sha1($this->pwd)); + return true; + } + private function createDav($scope) { $token = base64_encode(mt_rand()); - $davDir = UnhostedSettings::davDir . "$userDomain/$userName/".$scope; + $davDir = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/".$scope; `if [ ! -d $davDir ] ; then mkdir $davDir ; fi`; `echo "" > $davDir/.htaccess`; `echo " AuthType Basic" >> $davDir/.htaccess`; @@ -14,8 +28,38 @@ function registerScope($userAddress, $pwd, $scope) { `echo " Require valid-user" >> $davDir/.htaccess`; `echo " AuthUserFile $davDir/.htpasswd" >> $davDir/.htaccess`; `echo "" >> $davDir/.htaccess`; - `htpasswd -bc $davDir/.htpasswd {$userAddress} $token`; + `htpasswd -bc $davDir/.htpasswd {{$this->userAddress} $token`; return $token; } - return null; + private function createWallet($davBaseUrl, $davToken, $cryptoPwd) { + $wallet = json_encode(array( + "userAddress" => $userAddress, + "davBaseUrl" => $davBaseUrl, + "davAuth" => base64_encode($userAddress .':'. $davToken), + "cryptoPwd" => $cryptoPwd + )); + file_put_content($davDir.'/wallet_'.sha1($this->pwd), $wallet); + return $wallet; + } + public function getWallet($scope) { + $davDir = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/".$scope; + return file_get_content($davDir.'/wallet_'.sha1($this->pwd)); + + } + public function createHostedUser() { + createUserDir(); + $davToken = createDav(UnhostedSettings::homeScope); + return createWallet(UnhostedSettings::homeDavBaseUrl, $davToken, null); + } + public function createWalletAccount($davBaseUrl, $davToken) { + $cryptoPwd = mtrand(); + return createWallet($davBaseUrl, $davToken, $cryptoPwd); + } + public function addApp($scope) { + $pwdFile = UnhostedSettings::davDir . "{$this->userDomain}/{$this->userName}/.htpasswd"; + if(file_exists($pwdFile) && sha1($this->pwd)==file_get_contents($pwdFile)) { + return createDav($scope); + } + return null; + } }