summaryrefslogtreecommitdiffstats
path: root/apps/remoteStorage/lib_remoteStorage.php
blob: c1765640c5ddc08a961da051d10c0d5d22986779 (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
<?php

class OC_remoteStorage {
	public static function getValidTokens($ownCloudUser, $category) {
		$query=OCP\DB::prepare("SELECT token,appUrl,category FROM *PREFIX*authtoken WHERE user=? LIMIT 100");
		$result=$query->execute(array($ownCloudUser));
		$ret = array();
		while($row=$result->fetchRow()){
			if(in_array($category, explode(',', $row['category']))) {
				$ret[$row['token']]=true;
			}
		}
		return $ret;
	}

  public static function getTokenFor($appUrl, $categories) {
		$user=OCP\USER::getUser();
		$query=OCP\DB::prepare("SELECT token FROM *PREFIX*authtoken WHERE user=? AND appUrl=? AND category=? LIMIT 1");
		$result=$query->execute(array($user, $appUrl, $categories));
		if($row=$result->fetchRow()) {
			return base64_encode('remoteStorage:'.$row['token']);
		} else {
			return false;
		}
	}

	public static function getAllTokens() {
		$user=OCP\USER::getUser();
		$query=OCP\DB::prepare("SELECT token,appUrl,category FROM *PREFIX*authtoken WHERE user=? LIMIT 100");
		$result=$query->execute(array($user));
		$ret = array();
		while($row=$result->fetchRow()){
			$ret[$row['token']] = array(
				'appUrl' => $row['appUrl'],
				'categories' => $row['category'],
			);
		}
		return $ret;
	}

	public static function deleteToken($token) {
		$user=OCP\USER::getUser();
		$query=OCP\DB::prepare("DELETE FROM *PREFIX*authtoken WHERE token=? AND user=?");
		$query->execute(array($token,$user));
		return 'unknown';//how can we see if any rows were affected?
	}
	private static function addToken($token, $appUrl, $categories){
		$user=OCP\USER::getUser();
		$query=OCP\DB::prepare("INSERT INTO *PREFIX*authtoken (`token`,`appUrl`,`user`,`category`) VALUES(?,?,?,?)");
		$query->execute(array($token,$appUrl,$user,$categories));
	}
	public static function createCategories($appUrl, $categories) {
		$token=uniqid();
		OC_Util::setupFS(OCP\USER::getUser());
		self::addToken($token, $appUrl, $categories);
		foreach(explode(',', $categories) as $category) {
			//TODO: input checking on $category
			$scopePathParts = array('remoteStorage', $category);
			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 base64_encode('remoteStorage:'.$token);
	}
}