aboutsummaryrefslogtreecommitdiffstats
path: root/apps/remoteStorage/lib_remoteStorage.php
blob: 5677ab3c6e0149d4b90becb885b71cd696207f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

class OC_remoteStorage {
	public static function getValidTokens($ownCloudUser, $userAddress, $dataScope) {
		$query=OC_DB::prepare("SELECT token,appUrl FROM *PREFIX*authtoken WHERE user=? AND userAddress=? AND dataScope=? LIMIT 100");
		$result=$query->execute(array($ownCloudUser,$userAddress,$dataScope));
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$result->getDebugInfo().'<br />';
			if(defined("DEBUG") && DEBUG) {error_log( $entry );}
			die( $entry );
		}
		$ret = array();
		while($row=$result->fetchRow()){
			$ret[$row['token']]=$userAddress;
		}
		return $ret;
	}

	public static function getAllTokens() {
		$user=OC_User::getUser();
		$query=OC_DB::prepare("SELECT token,appUrl,userAddress,dataScope FROM *PREFIX*authtoken WHERE user=? LIMIT 100");
		$result=$query->execute(array($user));
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$result->getDebugInfo().'<br />';
			if(defined("DEBUG") && DEBUG) {error_log( $entry );}
			die( $entry );
		}
		$ret = array();
		while($row=$result->fetchRow()){
			$ret[$row['token']] = array(
				'appUrl' => $row['appurl'],
				'userAddress' => $row['useraddress'],
				'dataScope' => $row['datascope'],
			);
		}
		return $ret;
	}

	public static function deleteToken($token) {
		$user=OC_User::getUser();
		$query=OC_DB::prepare("DELETE FROM *PREFIX*authtoken WHERE token=? AND user=?");
		$result=$query->execute(array($token,$user));
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$result->getDebugInfo().'<br />';
			if(defined("DEBUG") && DEBUG) {error_log( $entry );}
			die( $entry );
		}
	}
	private static function addToken($token, $appUrl, $userAddress, $dataScope){
		$user=OC_User::getUser();
		$query=OC_DB::prepare("INSERT INTO *PREFIX*authtoken (`token`,`appUrl`,`user`,`userAddress`,`dataScope`) VALUES(?,?,?,?,?)");
		$result=$query->execute(array($token,$appUrl,$user,$userAddress,$dataScope));
		if( PEAR::isError($result)) {
			$entry = 'DB Error: "'.$result->getMessage().'"<br />';
			$entry .= 'Offending command was: '.$result->getDebugInfo().'<br />';
			if(defined("DEBUG") && DEBUG) {error_log( $entry );}
			die( $entry );
		}
	}
	public static function createDataScope($appUrl, $userAddress, $dataScope){
		$token=uniqid();
		self::addToken($token, $appUrl, $userAddress, $dataScope);
		//TODO: input checking on $userAddress and $dataScope
		list($userName, $userHost) = explode('@', $userAddress);
		OC_Util::setupFS(OC_User::getUser());
		$scopePathParts = array('remoteStorage', 'webdav', $userHost, $userName, $dataScope);
		for($i=0;$i<=count($scopePathParts);$i++){
			$thisPath = '/'.implode('/', array_slice($scopePathParts, 0, $i));
			if(!OC_Filesystem::file_exists($thisPath)) {
				OC_Filesystem::mkdir($thisPath);
			}
		}
		return $token;
	}
}