Completed
Pull Request — stable9 (#4226)
by Lukas
11:11
created
apps/dav/command/createaddressbook.php 2 patches
Unused Use Statements   -5 removed lines patch added patch discarded remove patch
@@ -22,11 +22,6 @@
 block discarded – undo
22 22
 namespace OCA\DAV\Command;
23 23
 
24 24
 use OCA\DAV\CardDAV\CardDavBackend;
25
-use OCA\DAV\Connector\Sabre\Principal;
26
-use OCP\IConfig;
27
-use OCP\IDBConnection;
28
-use OCP\IGroupManager;
29
-use OCP\ILogger;
30 25
 use OCP\IUserManager;
31 26
 use Symfony\Component\Console\Command\Command;
32 27
 use Symfony\Component\Console\Input\InputArgument;
Please login to merge, or discard this patch.
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -36,43 +36,43 @@
 block discarded – undo
36 36
 
37 37
 class CreateAddressBook extends Command {
38 38
 
39
-	/** @var IUserManager */
40
-	private $userManager;
39
+    /** @var IUserManager */
40
+    private $userManager;
41 41
 
42
-	/** @var CardDavBackend */
43
-	private $cardDavBackend;
42
+    /** @var CardDavBackend */
43
+    private $cardDavBackend;
44 44
 
45
-	/**
46
-	 * @param IUserManager $userManager
47
-	 * @param CardDavBackend $cardDavBackend
48
-	 */
49
-	function __construct(IUserManager $userManager,
50
-						 CardDavBackend $cardDavBackend
51
-	) {
52
-		parent::__construct();
53
-		$this->userManager = $userManager;
54
-		$this->cardDavBackend = $cardDavBackend;
55
-	}
45
+    /**
46
+     * @param IUserManager $userManager
47
+     * @param CardDavBackend $cardDavBackend
48
+     */
49
+    function __construct(IUserManager $userManager,
50
+                            CardDavBackend $cardDavBackend
51
+    ) {
52
+        parent::__construct();
53
+        $this->userManager = $userManager;
54
+        $this->cardDavBackend = $cardDavBackend;
55
+    }
56 56
 
57
-	protected function configure() {
58
-		$this
59
-				->setName('dav:create-addressbook')
60
-				->setDescription('Create a dav addressbook')
61
-				->addArgument('user',
62
-						InputArgument::REQUIRED,
63
-						'User for whom the addressbook will be created')
64
-				->addArgument('name',
65
-						InputArgument::REQUIRED,
66
-						'Name of the addressbook');
67
-	}
57
+    protected function configure() {
58
+        $this
59
+                ->setName('dav:create-addressbook')
60
+                ->setDescription('Create a dav addressbook')
61
+                ->addArgument('user',
62
+                        InputArgument::REQUIRED,
63
+                        'User for whom the addressbook will be created')
64
+                ->addArgument('name',
65
+                        InputArgument::REQUIRED,
66
+                        'Name of the addressbook');
67
+    }
68 68
 
69
-	protected function execute(InputInterface $input, OutputInterface $output) {
70
-		$user = $input->getArgument('user');
71
-		if (!$this->userManager->userExists($user)) {
72
-			throw new \InvalidArgumentException("User <$user> in unknown.");
73
-		}
69
+    protected function execute(InputInterface $input, OutputInterface $output) {
70
+        $user = $input->getArgument('user');
71
+        if (!$this->userManager->userExists($user)) {
72
+            throw new \InvalidArgumentException("User <$user> in unknown.");
73
+        }
74 74
 
75
-		$name = $input->getArgument('name');
76
-		$this->cardDavBackend->createAddressBook("principals/users/$user", $name, []);
77
-	}
75
+        $name = $input->getArgument('name');
76
+        $this->cardDavBackend->createAddressBook("principals/users/$user", $name, []);
77
+    }
78 78
 }
Please login to merge, or discard this patch.
apps/dav/command/syncsystemaddressbook.php 2 patches
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -21,10 +21,8 @@
 block discarded – undo
21 21
 namespace OCA\DAV\Command;
22 22
 
23 23
 use OCA\DAV\CardDAV\SyncService;
24
-use OCP\IUserManager;
25 24
 use Symfony\Component\Console\Command\Command;
26 25
 use Symfony\Component\Console\Helper\ProgressBar;
27
-use Symfony\Component\Console\Input\InputArgument;
28 26
 use Symfony\Component\Console\Input\InputInterface;
29 27
 use Symfony\Component\Console\Output\OutputInterface;
30 28
 
Please login to merge, or discard this patch.
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -31,36 +31,36 @@
 block discarded – undo
31 31
 
32 32
 class SyncSystemAddressBook extends Command {
33 33
 
34
-	/** @var SyncService */
35
-	private $syncService;
34
+    /** @var SyncService */
35
+    private $syncService;
36 36
 
37
-	/**
38
-	 * @param SyncService $syncService
39
-	 */
40
-	function __construct(SyncService $syncService) {
41
-		parent::__construct();
42
-		$this->syncService = $syncService;
43
-	}
37
+    /**
38
+     * @param SyncService $syncService
39
+     */
40
+    function __construct(SyncService $syncService) {
41
+        parent::__construct();
42
+        $this->syncService = $syncService;
43
+    }
44 44
 
45
-	protected function configure() {
46
-		$this
47
-			->setName('dav:sync-system-addressbook')
48
-			->setDescription('Synchronizes users to the system addressbook');
49
-	}
45
+    protected function configure() {
46
+        $this
47
+            ->setName('dav:sync-system-addressbook')
48
+            ->setDescription('Synchronizes users to the system addressbook');
49
+    }
50 50
 
51
-	/**
52
-	 * @param InputInterface $input
53
-	 * @param OutputInterface $output
54
-	 */
55
-	protected function execute(InputInterface $input, OutputInterface $output) {
56
-		$output->writeln('Syncing users ...');
57
-		$progress = new ProgressBar($output);
58
-		$progress->start();
59
-		$this->syncService->syncInstance(function() use ($progress) {
60
-			$progress->advance();
61
-		});
51
+    /**
52
+     * @param InputInterface $input
53
+     * @param OutputInterface $output
54
+     */
55
+    protected function execute(InputInterface $input, OutputInterface $output) {
56
+        $output->writeln('Syncing users ...');
57
+        $progress = new ProgressBar($output);
58
+        $progress->start();
59
+        $this->syncService->syncInstance(function() use ($progress) {
60
+            $progress->advance();
61
+        });
62 62
 
63
-		$progress->finish();
64
-		$output->writeln('');
65
-	}
63
+        $progress->finish();
64
+        $output->writeln('');
65
+    }
66 66
 }
Please login to merge, or discard this patch.
apps/dav/lib/caldav/calendar.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -22,7 +22,6 @@
 block discarded – undo
22 22
 namespace OCA\DAV\CalDAV;
23 23
 
24 24
 use OCA\DAV\DAV\Sharing\IShareable;
25
-use Sabre\CalDAV\Backend\BackendInterface;
26 25
 use Sabre\DAV\Exception\Forbidden;
27 26
 use Sabre\DAV\PropPatch;
28 27
 
Please login to merge, or discard this patch.
Indentation   +120 added lines, -120 removed lines patch added patch discarded remove patch
@@ -30,135 +30,135 @@
 block discarded – undo
30 30
 
31 31
 class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
32 32
 
33
-	/**
34
-	 * Updates the list of shares.
35
-	 *
36
-	 * The first array is a list of people that are to be added to the
37
-	 * resource.
38
-	 *
39
-	 * Every element in the add array has the following properties:
40
-	 *   * href - A url. Usually a mailto: address
41
-	 *   * commonName - Usually a first and last name, or false
42
-	 *   * summary - A description of the share, can also be false
43
-	 *   * readOnly - A boolean value
44
-	 *
45
-	 * Every element in the remove array is just the address string.
46
-	 *
47
-	 * @param array $add
48
-	 * @param array $remove
49
-	 * @return void
50
-	 */
51
-	function updateShares(array $add, array $remove) {
52
-		/** @var CalDavBackend $calDavBackend */
53
-		$calDavBackend = $this->caldavBackend;
54
-		$calDavBackend->updateShares($this, $add, $remove);
55
-	}
33
+    /**
34
+     * Updates the list of shares.
35
+     *
36
+     * The first array is a list of people that are to be added to the
37
+     * resource.
38
+     *
39
+     * Every element in the add array has the following properties:
40
+     *   * href - A url. Usually a mailto: address
41
+     *   * commonName - Usually a first and last name, or false
42
+     *   * summary - A description of the share, can also be false
43
+     *   * readOnly - A boolean value
44
+     *
45
+     * Every element in the remove array is just the address string.
46
+     *
47
+     * @param array $add
48
+     * @param array $remove
49
+     * @return void
50
+     */
51
+    function updateShares(array $add, array $remove) {
52
+        /** @var CalDavBackend $calDavBackend */
53
+        $calDavBackend = $this->caldavBackend;
54
+        $calDavBackend->updateShares($this, $add, $remove);
55
+    }
56 56
 
57
-	/**
58
-	 * Returns the list of people whom this resource is shared with.
59
-	 *
60
-	 * Every element in this array should have the following properties:
61
-	 *   * href - Often a mailto: address
62
-	 *   * commonName - Optional, for example a first + last name
63
-	 *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
64
-	 *   * readOnly - boolean
65
-	 *   * summary - Optional, a description for the share
66
-	 *
67
-	 * @return array
68
-	 */
69
-	function getShares() {
70
-		/** @var CalDavBackend $calDavBackend */
71
-		$calDavBackend = $this->caldavBackend;
72
-		return $calDavBackend->getShares($this->getResourceId());
73
-	}
57
+    /**
58
+     * Returns the list of people whom this resource is shared with.
59
+     *
60
+     * Every element in this array should have the following properties:
61
+     *   * href - Often a mailto: address
62
+     *   * commonName - Optional, for example a first + last name
63
+     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
64
+     *   * readOnly - boolean
65
+     *   * summary - Optional, a description for the share
66
+     *
67
+     * @return array
68
+     */
69
+    function getShares() {
70
+        /** @var CalDavBackend $calDavBackend */
71
+        $calDavBackend = $this->caldavBackend;
72
+        return $calDavBackend->getShares($this->getResourceId());
73
+    }
74 74
 
75
-	/**
76
-	 * @return int
77
-	 */
78
-	public function getResourceId() {
79
-		return $this->calendarInfo['id'];
80
-	}
75
+    /**
76
+     * @return int
77
+     */
78
+    public function getResourceId() {
79
+        return $this->calendarInfo['id'];
80
+    }
81 81
 
82
-	function getACL() {
83
-		$acl =  [
84
-			[
85
-				'privilege' => '{DAV:}read',
86
-				'principal' => $this->getOwner(),
87
-				'protected' => true,
88
-			]];
89
-		if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
90
-			$acl[] = [
91
-				'privilege' => '{DAV:}write',
92
-				'principal' => $this->getOwner(),
93
-				'protected' => true,
94
-			];
95
-		}
96
-		if ($this->getOwner() !== parent::getOwner()) {
97
-			$acl[] =  [
98
-					'privilege' => '{DAV:}read',
99
-					'principal' => parent::getOwner(),
100
-					'protected' => true,
101
-				];
102
-			if ($this->canWrite()) {
103
-				$acl[] = [
104
-					'privilege' => '{DAV:}write',
105
-					'principal' => parent::getOwner(),
106
-					'protected' => true,
107
-				];
108
-			}
109
-		}
82
+    function getACL() {
83
+        $acl =  [
84
+            [
85
+                'privilege' => '{DAV:}read',
86
+                'principal' => $this->getOwner(),
87
+                'protected' => true,
88
+            ]];
89
+        if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
90
+            $acl[] = [
91
+                'privilege' => '{DAV:}write',
92
+                'principal' => $this->getOwner(),
93
+                'protected' => true,
94
+            ];
95
+        }
96
+        if ($this->getOwner() !== parent::getOwner()) {
97
+            $acl[] =  [
98
+                    'privilege' => '{DAV:}read',
99
+                    'principal' => parent::getOwner(),
100
+                    'protected' => true,
101
+                ];
102
+            if ($this->canWrite()) {
103
+                $acl[] = [
104
+                    'privilege' => '{DAV:}write',
105
+                    'principal' => parent::getOwner(),
106
+                    'protected' => true,
107
+                ];
108
+            }
109
+        }
110 110
 
111
-		/** @var CalDavBackend $calDavBackend */
112
-		$calDavBackend = $this->caldavBackend;
113
-		return $calDavBackend->applyShareAcl($this->getResourceId(), $acl);
114
-	}
111
+        /** @var CalDavBackend $calDavBackend */
112
+        $calDavBackend = $this->caldavBackend;
113
+        return $calDavBackend->applyShareAcl($this->getResourceId(), $acl);
114
+    }
115 115
 
116
-	function getChildACL() {
117
-		return $this->getACL();
118
-	}
116
+    function getChildACL() {
117
+        return $this->getACL();
118
+    }
119 119
 
120
-	function getOwner() {
121
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
122
-			return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
123
-		}
124
-		return parent::getOwner();
125
-	}
120
+    function getOwner() {
121
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
122
+            return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
123
+        }
124
+        return parent::getOwner();
125
+    }
126 126
 
127
-	function delete() {
128
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
129
-			$principal = 'principal:' . parent::getOwner();
130
-			$shares = $this->getShares();
131
-			$shares = array_filter($shares, function($share) use ($principal){
132
-				return $share['href'] === $principal;
133
-			});
134
-			if (empty($shares)) {
135
-				throw new Forbidden();
136
-			}
127
+    function delete() {
128
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
129
+            $principal = 'principal:' . parent::getOwner();
130
+            $shares = $this->getShares();
131
+            $shares = array_filter($shares, function($share) use ($principal){
132
+                return $share['href'] === $principal;
133
+            });
134
+            if (empty($shares)) {
135
+                throw new Forbidden();
136
+            }
137 137
 
138
-			/** @var CalDavBackend $calDavBackend */
139
-			$calDavBackend = $this->caldavBackend;
140
-			$calDavBackend->updateShares($this, [], [
141
-				'href' => $principal
142
-			]);
143
-			return;
144
-		}
145
-		parent::delete();
146
-	}
138
+            /** @var CalDavBackend $calDavBackend */
139
+            $calDavBackend = $this->caldavBackend;
140
+            $calDavBackend->updateShares($this, [], [
141
+                'href' => $principal
142
+            ]);
143
+            return;
144
+        }
145
+        parent::delete();
146
+    }
147 147
 
148
-	function propPatch(PropPatch $propPatch) {
149
-		$mutations = $propPatch->getMutations();
150
-		// If this is a shared calendar, the user can only change the enabled property, to hide it.
151
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) && (sizeof($mutations) !== 1 || !isset($mutations['{http://owncloud.org/ns}calendar-enabled']))) {
152
-			throw new Forbidden();
153
-		}
154
-		parent::propPatch($propPatch);
155
-	}
148
+    function propPatch(PropPatch $propPatch) {
149
+        $mutations = $propPatch->getMutations();
150
+        // If this is a shared calendar, the user can only change the enabled property, to hide it.
151
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) && (sizeof($mutations) !== 1 || !isset($mutations['{http://owncloud.org/ns}calendar-enabled']))) {
152
+            throw new Forbidden();
153
+        }
154
+        parent::propPatch($propPatch);
155
+    }
156 156
 
157
-	private function canWrite() {
158
-		if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
159
-			return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
160
-		}
161
-		return true;
162
-	}
157
+    private function canWrite() {
158
+        if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
159
+            return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
160
+        }
161
+        return true;
162
+    }
163 163
 
164 164
 }
Please login to merge, or discard this patch.
apps/dav/lib/connector/sabre/checksumlist.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -21,7 +21,6 @@
 block discarded – undo
21 21
 namespace OCA\DAV\Connector\Sabre;
22 22
 
23 23
 use Sabre\Xml\XmlSerializable;
24
-use Sabre\Xml\Element;
25 24
 use Sabre\Xml\Writer;
26 25
 
27 26
 /**
Please login to merge, or discard this patch.
Indentation   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -32,41 +32,41 @@
 block discarded – undo
32 32
  * checksum name.
33 33
  */
34 34
 class ChecksumList implements XmlSerializable {
35
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
35
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
36 36
 
37
-	/** @var string[] of TYPE:CHECKSUM */
38
-	private $checksums;
37
+    /** @var string[] of TYPE:CHECKSUM */
38
+    private $checksums;
39 39
 
40
-	/**
41
-	 * @param string $checksum
42
-	 */
43
-	public function __construct($checksum) {
44
-		$this->checksums = explode(',', $checksum);
45
-	}
40
+    /**
41
+     * @param string $checksum
42
+     */
43
+    public function __construct($checksum) {
44
+        $this->checksums = explode(',', $checksum);
45
+    }
46 46
 
47
-	/**
48
-	 * The xmlSerialize metod is called during xml writing.
49
-	 *
50
-	 * Use the $writer argument to write its own xml serialization.
51
-	 *
52
-	 * An important note: do _not_ create a parent element. Any element
53
-	 * implementing XmlSerializble should only ever write what's considered
54
-	 * its 'inner xml'.
55
-	 *
56
-	 * The parent of the current element is responsible for writing a
57
-	 * containing element.
58
-	 *
59
-	 * This allows serializers to be re-used for different element names.
60
-	 *
61
-	 * If you are opening new elements, you must also close them again.
62
-	 *
63
-	 * @param Writer $writer
64
-	 * @return void
65
-	 */
66
-	function xmlSerialize(Writer $writer) {
47
+    /**
48
+     * The xmlSerialize metod is called during xml writing.
49
+     *
50
+     * Use the $writer argument to write its own xml serialization.
51
+     *
52
+     * An important note: do _not_ create a parent element. Any element
53
+     * implementing XmlSerializble should only ever write what's considered
54
+     * its 'inner xml'.
55
+     *
56
+     * The parent of the current element is responsible for writing a
57
+     * containing element.
58
+     *
59
+     * This allows serializers to be re-used for different element names.
60
+     *
61
+     * If you are opening new elements, you must also close them again.
62
+     *
63
+     * @param Writer $writer
64
+     * @return void
65
+     */
66
+    function xmlSerialize(Writer $writer) {
67 67
 
68
-		foreach ($this->checksums as $checksum) {
69
-			$writer->writeElement('{' . self::NS_OWNCLOUD . '}checksum', $checksum);
70
-		}
71
-	}
68
+        foreach ($this->checksums as $checksum) {
69
+            $writer->writeElement('{' . self::NS_OWNCLOUD . '}checksum', $checksum);
70
+        }
71
+    }
72 72
 }
Please login to merge, or discard this patch.
apps/dav/lib/connector/sabre/davaclplugin.php 2 patches
Unused Use Statements   -3 removed lines patch added patch discarded remove patch
@@ -23,14 +23,11 @@
 block discarded – undo
23 23
 namespace OCA\DAV\Connector\Sabre;
24 24
 
25 25
 use Sabre\DAV\Exception\NotFound;
26
-use Sabre\DAV\IFile;
27 26
 use Sabre\DAV\INode;
28 27
 use \Sabre\DAV\PropFind;
29 28
 use \Sabre\DAV\PropPatch;
30
-use Sabre\DAVACL\Exception\NeedPrivileges;
31 29
 use \Sabre\HTTP\RequestInterface;
32 30
 use \Sabre\HTTP\ResponseInterface;
33
-use Sabre\HTTP\URLUtil;
34 31
 
35 32
 /**
36 33
  * Class DavAclPlugin is a wrapper around \Sabre\DAVACL\Plugin that returns 404
Please login to merge, or discard this patch.
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -41,33 +41,33 @@
 block discarded – undo
41 41
  * @package OCA\DAV\Connector\Sabre
42 42
  */
43 43
 class DavAclPlugin extends \Sabre\DAVACL\Plugin {
44
-	public function __construct() {
45
-		$this->hideNodesFromListings = true;
46
-	}
44
+    public function __construct() {
45
+        $this->hideNodesFromListings = true;
46
+    }
47 47
 
48
-	function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
49
-		$access = parent::checkPrivileges($uri, $privileges, $recursion, false);
50
-		if($access === false) {
51
-			/** @var INode $node */
52
-			$node = $this->server->tree->getNodeForPath($uri);
48
+    function checkPrivileges($uri, $privileges, $recursion = self::R_PARENT, $throwExceptions = true) {
49
+        $access = parent::checkPrivileges($uri, $privileges, $recursion, false);
50
+        if($access === false) {
51
+            /** @var INode $node */
52
+            $node = $this->server->tree->getNodeForPath($uri);
53 53
 
54
-			switch(get_class($node)) {
55
-				case 'OCA\DAV\CardDAV\AddressBook':
56
-					$type = 'Addressbook';
57
-					break;
58
-				default:
59
-					$type = 'Node';
60
-					break;
61
-			}
62
-			throw new NotFound(
63
-				sprintf(
64
-					"%s with name '%s' could not be found",
65
-					$type,
66
-					$node->getName()
67
-				)
68
-			);
69
-		}
54
+            switch(get_class($node)) {
55
+                case 'OCA\DAV\CardDAV\AddressBook':
56
+                    $type = 'Addressbook';
57
+                    break;
58
+                default:
59
+                    $type = 'Node';
60
+                    break;
61
+            }
62
+            throw new NotFound(
63
+                sprintf(
64
+                    "%s with name '%s' could not be found",
65
+                    $type,
66
+                    $node->getName()
67
+                )
68
+            );
69
+        }
70 70
 
71
-		return $access;
72
-	}
71
+        return $access;
72
+    }
73 73
 }
Please login to merge, or discard this patch.
apps/dav/lib/dav/sharing/plugin.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,6 @@
 block discarded – undo
24 24
 use OCA\DAV\Connector\Sabre\Auth;
25 25
 use OCA\DAV\DAV\Sharing\Xml\Invite;
26 26
 use OCP\IRequest;
27
-use Sabre\DAV\Exception\BadRequest;
28 27
 use Sabre\DAV\Exception\NotFound;
29 28
 use Sabre\DAV\INode;
30 29
 use Sabre\DAV\PropFind;
Please login to merge, or discard this patch.
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -36,166 +36,166 @@
 block discarded – undo
36 36
 
37 37
 class Plugin extends ServerPlugin {
38 38
 
39
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
40
-
41
-	/** @var Auth */
42
-	private $auth;
43
-
44
-	/** @var IRequest */
45
-	private $request;
46
-
47
-	/**
48
-	 * Plugin constructor.
49
-	 *
50
-	 * @param Auth $authBackEnd
51
-	 * @param IRequest $request
52
-	 */
53
-	public function __construct(Auth $authBackEnd, IRequest $request) {
54
-		$this->auth = $authBackEnd;
55
-		$this->request = $request;
56
-	}
57
-
58
-	/**
59
-	 * Reference to SabreDAV server object.
60
-	 *
61
-	 * @var \Sabre\DAV\Server
62
-	 */
63
-	protected $server;
64
-
65
-	/**
66
-	 * This method should return a list of server-features.
67
-	 *
68
-	 * This is for example 'versioning' and is added to the DAV: header
69
-	 * in an OPTIONS response.
70
-	 *
71
-	 * @return string[]
72
-	 */
73
-	function getFeatures() {
74
-		return ['oc-resource-sharing'];
75
-	}
76
-
77
-	/**
78
-	 * Returns a plugin name.
79
-	 *
80
-	 * Using this name other plugins will be able to access other plugins
81
-	 * using Sabre\DAV\Server::getPlugin
82
-	 *
83
-	 * @return string
84
-	 */
85
-	function getPluginName() {
86
-		return 'oc-resource-sharing';
87
-	}
88
-
89
-	/**
90
-	 * This initializes the plugin.
91
-	 *
92
-	 * This function is called by Sabre\DAV\Server, after
93
-	 * addPlugin is called.
94
-	 *
95
-	 * This method should set up the required event subscriptions.
96
-	 *
97
-	 * @param Server $server
98
-	 * @return void
99
-	 */
100
-	function initialize(Server $server) {
101
-		$this->server = $server;
102
-		$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\ShareRequest';
103
-		$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\Invite';
104
-
105
-		$this->server->on('method:POST', [$this, 'httpPost']);
106
-		$this->server->on('propFind',    [$this, 'propFind']);
107
-	}
108
-
109
-	/**
110
-	 * We intercept this to handle POST requests on a dav resource.
111
-	 *
112
-	 * @param RequestInterface $request
113
-	 * @param ResponseInterface $response
114
-	 * @return null|false
115
-	 */
116
-	function httpPost(RequestInterface $request, ResponseInterface $response) {
117
-
118
-		$path = $request->getPath();
119
-
120
-		// Only handling xml
121
-		$contentType = $request->getHeader('Content-Type');
122
-		if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false)
123
-			return;
124
-
125
-		// Making sure the node exists
126
-		try {
127
-			$node = $this->server->tree->getNodeForPath($path);
128
-		} catch (NotFound $e) {
129
-			return;
130
-		}
131
-
132
-		$requestBody = $request->getBodyAsString();
133
-
134
-		// If this request handler could not deal with this POST request, it
135
-		// will return 'null' and other plugins get a chance to handle the
136
-		// request.
137
-		//
138
-		// However, we already requested the full body. This is a problem,
139
-		// because a body can only be read once. This is why we preemptively
140
-		// re-populated the request body with the existing data.
141
-		$request->setBody($requestBody);
142
-
143
-		$message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
144
-
145
-		switch ($documentType) {
146
-
147
-			// Dealing with the 'share' document, which modified invitees on a
148
-			// calendar.
149
-			case '{' . self::NS_OWNCLOUD . '}share' :
150
-
151
-				// We can only deal with IShareableCalendar objects
152
-				if (!$node instanceof IShareable) {
153
-					return;
154
-				}
155
-
156
-				$this->server->transactionType = 'post-oc-resource-share';
157
-
158
-				// Getting ACL info
159
-				$acl = $this->server->getPlugin('acl');
160
-
161
-				// If there's no ACL support, we allow everything
162
-				if ($acl) {
163
-					/** @var \Sabre\DAVACL\Plugin $acl */
164
-					$acl->checkPrivileges($path, '{DAV:}write');
165
-				}
166
-
167
-				$node->updateShares($message->set, $message->remove);
168
-
169
-				$response->setStatus(200);
170
-				// Adding this because sending a response body may cause issues,
171
-				// and I wanted some type of indicator the response was handled.
172
-				$response->setHeader('X-Sabre-Status', 'everything-went-well');
173
-
174
-				// Breaking the event chain
175
-				return false;
176
-		}
177
-	}
178
-
179
-	/**
180
-	 * This event is triggered when properties are requested for a certain
181
-	 * node.
182
-	 *
183
-	 * This allows us to inject any properties early.
184
-	 *
185
-	 * @param PropFind $propFind
186
-	 * @param INode $node
187
-	 * @return void
188
-	 */
189
-	function propFind(PropFind $propFind, INode $node) {
190
-		if ($node instanceof IShareable) {
191
-
192
-			$propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function() use ($node) {
193
-				return new Invite(
194
-					$node->getShares()
195
-				);
196
-			});
197
-
198
-		}
199
-	}
39
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
40
+
41
+    /** @var Auth */
42
+    private $auth;
43
+
44
+    /** @var IRequest */
45
+    private $request;
46
+
47
+    /**
48
+     * Plugin constructor.
49
+     *
50
+     * @param Auth $authBackEnd
51
+     * @param IRequest $request
52
+     */
53
+    public function __construct(Auth $authBackEnd, IRequest $request) {
54
+        $this->auth = $authBackEnd;
55
+        $this->request = $request;
56
+    }
57
+
58
+    /**
59
+     * Reference to SabreDAV server object.
60
+     *
61
+     * @var \Sabre\DAV\Server
62
+     */
63
+    protected $server;
64
+
65
+    /**
66
+     * This method should return a list of server-features.
67
+     *
68
+     * This is for example 'versioning' and is added to the DAV: header
69
+     * in an OPTIONS response.
70
+     *
71
+     * @return string[]
72
+     */
73
+    function getFeatures() {
74
+        return ['oc-resource-sharing'];
75
+    }
76
+
77
+    /**
78
+     * Returns a plugin name.
79
+     *
80
+     * Using this name other plugins will be able to access other plugins
81
+     * using Sabre\DAV\Server::getPlugin
82
+     *
83
+     * @return string
84
+     */
85
+    function getPluginName() {
86
+        return 'oc-resource-sharing';
87
+    }
88
+
89
+    /**
90
+     * This initializes the plugin.
91
+     *
92
+     * This function is called by Sabre\DAV\Server, after
93
+     * addPlugin is called.
94
+     *
95
+     * This method should set up the required event subscriptions.
96
+     *
97
+     * @param Server $server
98
+     * @return void
99
+     */
100
+    function initialize(Server $server) {
101
+        $this->server = $server;
102
+        $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\ShareRequest';
103
+        $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\Invite';
104
+
105
+        $this->server->on('method:POST', [$this, 'httpPost']);
106
+        $this->server->on('propFind',    [$this, 'propFind']);
107
+    }
108
+
109
+    /**
110
+     * We intercept this to handle POST requests on a dav resource.
111
+     *
112
+     * @param RequestInterface $request
113
+     * @param ResponseInterface $response
114
+     * @return null|false
115
+     */
116
+    function httpPost(RequestInterface $request, ResponseInterface $response) {
117
+
118
+        $path = $request->getPath();
119
+
120
+        // Only handling xml
121
+        $contentType = $request->getHeader('Content-Type');
122
+        if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false)
123
+            return;
124
+
125
+        // Making sure the node exists
126
+        try {
127
+            $node = $this->server->tree->getNodeForPath($path);
128
+        } catch (NotFound $e) {
129
+            return;
130
+        }
131
+
132
+        $requestBody = $request->getBodyAsString();
133
+
134
+        // If this request handler could not deal with this POST request, it
135
+        // will return 'null' and other plugins get a chance to handle the
136
+        // request.
137
+        //
138
+        // However, we already requested the full body. This is a problem,
139
+        // because a body can only be read once. This is why we preemptively
140
+        // re-populated the request body with the existing data.
141
+        $request->setBody($requestBody);
142
+
143
+        $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
144
+
145
+        switch ($documentType) {
146
+
147
+            // Dealing with the 'share' document, which modified invitees on a
148
+            // calendar.
149
+            case '{' . self::NS_OWNCLOUD . '}share' :
150
+
151
+                // We can only deal with IShareableCalendar objects
152
+                if (!$node instanceof IShareable) {
153
+                    return;
154
+                }
155
+
156
+                $this->server->transactionType = 'post-oc-resource-share';
157
+
158
+                // Getting ACL info
159
+                $acl = $this->server->getPlugin('acl');
160
+
161
+                // If there's no ACL support, we allow everything
162
+                if ($acl) {
163
+                    /** @var \Sabre\DAVACL\Plugin $acl */
164
+                    $acl->checkPrivileges($path, '{DAV:}write');
165
+                }
166
+
167
+                $node->updateShares($message->set, $message->remove);
168
+
169
+                $response->setStatus(200);
170
+                // Adding this because sending a response body may cause issues,
171
+                // and I wanted some type of indicator the response was handled.
172
+                $response->setHeader('X-Sabre-Status', 'everything-went-well');
173
+
174
+                // Breaking the event chain
175
+                return false;
176
+        }
177
+    }
178
+
179
+    /**
180
+     * This event is triggered when properties are requested for a certain
181
+     * node.
182
+     *
183
+     * This allows us to inject any properties early.
184
+     *
185
+     * @param PropFind $propFind
186
+     * @param INode $node
187
+     * @return void
188
+     */
189
+    function propFind(PropFind $propFind, INode $node) {
190
+        if ($node instanceof IShareable) {
191
+
192
+            $propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function() use ($node) {
193
+                return new Invite(
194
+                    $node->getShares()
195
+                );
196
+            });
197
+
198
+        }
199
+    }
200 200
 
201 201
 }
Please login to merge, or discard this patch.
apps/dav/lib/migration/addressbookadapter.php 2 patches
Unused Use Statements   -4 removed lines patch added patch discarded remove patch
@@ -22,10 +22,6 @@
 block discarded – undo
22 22
 namespace OCA\Dav\Migration;
23 23
 
24 24
 use OCP\IDBConnection;
25
-use Symfony\Component\Console\Command\Command;
26
-use Symfony\Component\Console\Input\InputArgument;
27
-use Symfony\Component\Console\Input\InputInterface;
28
-use Symfony\Component\Console\Output\OutputInterface;
29 25
 
30 26
 class AddressBookAdapter {
31 27
 
Please login to merge, or discard this patch.
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -30,78 +30,78 @@
 block discarded – undo
30 30
 
31 31
 class AddressBookAdapter {
32 32
 
33
-	/** @var \OCP\IDBConnection */
34
-	protected $dbConnection;
33
+    /** @var \OCP\IDBConnection */
34
+    protected $dbConnection;
35 35
 
36
-	/** @var string */
37
-	private $sourceBookTable;
36
+    /** @var string */
37
+    private $sourceBookTable;
38 38
 
39
-	/** @var string */
40
-	private $sourceCardsTable;
39
+    /** @var string */
40
+    private $sourceCardsTable;
41 41
 
42
-	/**
43
-	 * @param IDBConnection $dbConnection
44
-	 * @param string $sourceBookTable
45
-	 * @param string $sourceCardsTable
46
-	 */
47
-	function __construct(IDBConnection $dbConnection,
48
-						 $sourceBookTable = 'contacts_addressbooks',
49
-						 $sourceCardsTable = 'contacts_cards') {
50
-		$this->dbConnection = $dbConnection;
51
-		$this->sourceBookTable = $sourceBookTable;
52
-		$this->sourceCardsTable = $sourceCardsTable;
53
-	}
42
+    /**
43
+     * @param IDBConnection $dbConnection
44
+     * @param string $sourceBookTable
45
+     * @param string $sourceCardsTable
46
+     */
47
+    function __construct(IDBConnection $dbConnection,
48
+                            $sourceBookTable = 'contacts_addressbooks',
49
+                            $sourceCardsTable = 'contacts_cards') {
50
+        $this->dbConnection = $dbConnection;
51
+        $this->sourceBookTable = $sourceBookTable;
52
+        $this->sourceCardsTable = $sourceCardsTable;
53
+    }
54 54
 
55
-	/**
56
-	 * @param string $user
57
-	 * @param \Closure $callBack
58
-	 */
59
-	public function foreachBook($user, \Closure $callBack) {
60
-		// get all addressbooks of that user
61
-		$query = $this->dbConnection->getQueryBuilder();
62
-		$stmt = $query->select('*')->from($this->sourceBookTable)
63
-			->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
64
-			->execute();
55
+    /**
56
+     * @param string $user
57
+     * @param \Closure $callBack
58
+     */
59
+    public function foreachBook($user, \Closure $callBack) {
60
+        // get all addressbooks of that user
61
+        $query = $this->dbConnection->getQueryBuilder();
62
+        $stmt = $query->select('*')->from($this->sourceBookTable)
63
+            ->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
64
+            ->execute();
65 65
 
66
-		while($row = $stmt->fetch()) {
67
-			$callBack($row);
68
-		}
69
-	}
66
+        while($row = $stmt->fetch()) {
67
+            $callBack($row);
68
+        }
69
+    }
70 70
 
71
-	public function setup() {
72
-		if (!$this->dbConnection->tableExists($this->sourceBookTable)) {
73
-			throw new NothingToDoException('Contacts tables are missing');
74
-		}
75
-	}
71
+    public function setup() {
72
+        if (!$this->dbConnection->tableExists($this->sourceBookTable)) {
73
+            throw new NothingToDoException('Contacts tables are missing');
74
+        }
75
+    }
76 76
 
77
-	/**
78
-	 * @param int $addressBookId
79
-	 * @param \Closure $callBack
80
-	 */
81
-	public function foreachCard($addressBookId, \Closure $callBack) {
82
-		$query = $this->dbConnection->getQueryBuilder();
83
-		$stmt = $query->select('*')->from($this->sourceCardsTable)
84
-			->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
85
-			->execute();
77
+    /**
78
+     * @param int $addressBookId
79
+     * @param \Closure $callBack
80
+     */
81
+    public function foreachCard($addressBookId, \Closure $callBack) {
82
+        $query = $this->dbConnection->getQueryBuilder();
83
+        $stmt = $query->select('*')->from($this->sourceCardsTable)
84
+            ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
85
+            ->execute();
86 86
 
87
-		while($row = $stmt->fetch()) {
88
-			$callBack($row);
89
-		}
90
-	}
87
+        while($row = $stmt->fetch()) {
88
+            $callBack($row);
89
+        }
90
+    }
91 91
 
92
-	/**
93
-	 * @param int $addressBookId
94
-	 * @return array
95
-	 */
96
-	public function getShares($addressBookId) {
97
-		$query = $this->dbConnection->getQueryBuilder();
98
-		$shares = $query->select('*')->from('share')
99
-			->where($query->expr()->eq('item_source', $query->createNamedParameter($addressBookId)))
100
-			->andWhere($query->expr()->eq('item_type', $query->expr()->literal('addressbook')))
101
-			->andWhere($query->expr()->in('share_type', [ $query->expr()->literal(0), $query->expr()->literal(1)]))
102
-			->execute()
103
-			->fetchAll();
92
+    /**
93
+     * @param int $addressBookId
94
+     * @return array
95
+     */
96
+    public function getShares($addressBookId) {
97
+        $query = $this->dbConnection->getQueryBuilder();
98
+        $shares = $query->select('*')->from('share')
99
+            ->where($query->expr()->eq('item_source', $query->createNamedParameter($addressBookId)))
100
+            ->andWhere($query->expr()->eq('item_type', $query->expr()->literal('addressbook')))
101
+            ->andWhere($query->expr()->in('share_type', [ $query->expr()->literal(0), $query->expr()->literal(1)]))
102
+            ->execute()
103
+            ->fetchAll();
104 104
 
105
-		return $shares;
106
-	}
105
+        return $shares;
106
+    }
107 107
 }
Please login to merge, or discard this patch.
apps/dav/lib/server.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,6 @@
 block discarded – undo
24 24
 namespace OCA\DAV;
25 25
 
26 26
 use OCA\DAV\CalDAV\Schedule\IMipPlugin;
27
-use OCA\DAV\Connector\FedAuth;
28 27
 use OCA\DAV\Connector\Sabre\Auth;
29 28
 use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin;
30 29
 use OCA\DAV\Connector\Sabre\DavAclPlugin;
Please login to merge, or discard this patch.
Indentation   +113 added lines, -113 removed lines patch added patch discarded remove patch
@@ -39,117 +39,117 @@
 block discarded – undo
39 39
 
40 40
 class Server {
41 41
 
42
-	/** @var IRequest */
43
-	private $request;
44
-
45
-	public function __construct(IRequest $request, $baseUri) {
46
-		$this->request = $request;
47
-		$this->baseUri = $baseUri;
48
-		$logger = \OC::$server->getLogger();
49
-		$mailer = \OC::$server->getMailer();
50
-		$dispatcher = \OC::$server->getEventDispatcher();
51
-
52
-		$root = new RootCollection();
53
-		$this->server = new \OCA\DAV\Connector\Sabre\Server($root);
54
-
55
-		// Backends
56
-		$authBackend = new Auth(
57
-			\OC::$server->getSession(),
58
-			\OC::$server->getUserSession(),
59
-			\OC::$server->getRequest()
60
-		);
61
-
62
-		// Set URL explicitly due to reverse-proxy situations
63
-		$this->server->httpRequest->setUrl($this->request->getRequestUri());
64
-		$this->server->setBaseUri($this->baseUri);
65
-
66
-		$this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
67
-		$authPlugin = new Plugin($authBackend, 'ownCloud');
68
-		$this->server->addPlugin($authPlugin);
69
-
70
-		// allow setup of additional auth backends
71
-		$event = new SabrePluginEvent($this->server);
72
-		$dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
73
-
74
-		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
75
-		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
76
-		$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
77
-		$this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
78
-
79
-		// acl
80
-		$acl = new DavAclPlugin();
81
-		$acl->defaultUsernamePath = 'principals/users';
82
-		$this->server->addPlugin($acl);
83
-
84
-		// calendar plugins
85
-		$this->server->addPlugin(new \Sabre\CalDAV\Plugin());
86
-		$this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
87
-		$this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
88
-		$this->server->addPlugin(new IMipPlugin($mailer, $logger));
89
-		$this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
90
-		$this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
91
-		$this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
92
-
93
-		// addressbook plugins
94
-		$this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
95
-		$this->server->addPlugin(new VCFExportPlugin());
96
-
97
-		// system tags plugins
98
-		$this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(
99
-			\OC::$server->getSystemTagManager(),
100
-			\OC::$server->getGroupManager(),
101
-			\OC::$server->getUserSession()
102
-		));
103
-
104
-		// comments plugin
105
-		$this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin(
106
-			\OC::$server->getCommentsManager(),
107
-			\OC::$server->getUserSession()
108
-		));
109
-
110
-		// Some WebDAV clients do require Class 2 WebDAV support (locking), since
111
-		// we do not provide locking we emulate it using a fake locking plugin.
112
-		if($request->isUserAgent([
113
-			'/WebDAVFS/',
114
-			'/Microsoft Office OneNote 2013/',
115
-		])) {
116
-			$this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
117
-		}
118
-
119
-		if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
120
-			$this->server->addPlugin(new BrowserErrorPagePlugin());
121
-		}
122
-
123
-		// wait with registering these until auth is handled and the filesystem is setup
124
-		$this->server->on('beforeMethod', function () {
125
-			// custom properties plugin must be the last one
126
-			$user = \OC::$server->getUserSession()->getUser();
127
-			if (!is_null($user)) {
128
-				$view = \OC\Files\Filesystem::getView();
129
-				$this->server->addPlugin(new FilesPlugin(
130
-					$this->server->tree,
131
-					$view,
132
-					$this->request
133
-				));
134
-
135
-				$this->server->addPlugin(
136
-					new \Sabre\DAV\PropertyStorage\Plugin(
137
-						new CustomPropertiesBackend(
138
-							$this->server->tree,
139
-							\OC::$server->getDatabaseConnection(),
140
-							\OC::$server->getUserSession()->getUser()
141
-						)
142
-					)
143
-				);
144
-
145
-				$this->server->addPlugin(
146
-					new \OCA\DAV\Connector\Sabre\QuotaPlugin($view)
147
-				);
148
-			}
149
-		});
150
-	}
151
-
152
-	public function exec() {
153
-		$this->server->exec();
154
-	}
42
+    /** @var IRequest */
43
+    private $request;
44
+
45
+    public function __construct(IRequest $request, $baseUri) {
46
+        $this->request = $request;
47
+        $this->baseUri = $baseUri;
48
+        $logger = \OC::$server->getLogger();
49
+        $mailer = \OC::$server->getMailer();
50
+        $dispatcher = \OC::$server->getEventDispatcher();
51
+
52
+        $root = new RootCollection();
53
+        $this->server = new \OCA\DAV\Connector\Sabre\Server($root);
54
+
55
+        // Backends
56
+        $authBackend = new Auth(
57
+            \OC::$server->getSession(),
58
+            \OC::$server->getUserSession(),
59
+            \OC::$server->getRequest()
60
+        );
61
+
62
+        // Set URL explicitly due to reverse-proxy situations
63
+        $this->server->httpRequest->setUrl($this->request->getRequestUri());
64
+        $this->server->setBaseUri($this->baseUri);
65
+
66
+        $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig()));
67
+        $authPlugin = new Plugin($authBackend, 'ownCloud');
68
+        $this->server->addPlugin($authPlugin);
69
+
70
+        // allow setup of additional auth backends
71
+        $event = new SabrePluginEvent($this->server);
72
+        $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event);
73
+
74
+        $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin());
75
+        $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger));
76
+        $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin());
77
+        $this->server->addPlugin(new \Sabre\DAV\Sync\Plugin());
78
+
79
+        // acl
80
+        $acl = new DavAclPlugin();
81
+        $acl->defaultUsernamePath = 'principals/users';
82
+        $this->server->addPlugin($acl);
83
+
84
+        // calendar plugins
85
+        $this->server->addPlugin(new \Sabre\CalDAV\Plugin());
86
+        $this->server->addPlugin(new \Sabre\CalDAV\ICSExportPlugin());
87
+        $this->server->addPlugin(new \Sabre\CalDAV\Schedule\Plugin());
88
+        $this->server->addPlugin(new IMipPlugin($mailer, $logger));
89
+        $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin());
90
+        $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin());
91
+        $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest()));
92
+
93
+        // addressbook plugins
94
+        $this->server->addPlugin(new \OCA\DAV\CardDAV\Plugin());
95
+        $this->server->addPlugin(new VCFExportPlugin());
96
+
97
+        // system tags plugins
98
+        $this->server->addPlugin(new \OCA\DAV\SystemTag\SystemTagPlugin(
99
+            \OC::$server->getSystemTagManager(),
100
+            \OC::$server->getGroupManager(),
101
+            \OC::$server->getUserSession()
102
+        ));
103
+
104
+        // comments plugin
105
+        $this->server->addPlugin(new \OCA\DAV\Comments\CommentsPlugin(
106
+            \OC::$server->getCommentsManager(),
107
+            \OC::$server->getUserSession()
108
+        ));
109
+
110
+        // Some WebDAV clients do require Class 2 WebDAV support (locking), since
111
+        // we do not provide locking we emulate it using a fake locking plugin.
112
+        if($request->isUserAgent([
113
+            '/WebDAVFS/',
114
+            '/Microsoft Office OneNote 2013/',
115
+        ])) {
116
+            $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\FakeLockerPlugin());
117
+        }
118
+
119
+        if (BrowserErrorPagePlugin::isBrowserRequest($request)) {
120
+            $this->server->addPlugin(new BrowserErrorPagePlugin());
121
+        }
122
+
123
+        // wait with registering these until auth is handled and the filesystem is setup
124
+        $this->server->on('beforeMethod', function () {
125
+            // custom properties plugin must be the last one
126
+            $user = \OC::$server->getUserSession()->getUser();
127
+            if (!is_null($user)) {
128
+                $view = \OC\Files\Filesystem::getView();
129
+                $this->server->addPlugin(new FilesPlugin(
130
+                    $this->server->tree,
131
+                    $view,
132
+                    $this->request
133
+                ));
134
+
135
+                $this->server->addPlugin(
136
+                    new \Sabre\DAV\PropertyStorage\Plugin(
137
+                        new CustomPropertiesBackend(
138
+                            $this->server->tree,
139
+                            \OC::$server->getDatabaseConnection(),
140
+                            \OC::$server->getUserSession()->getUser()
141
+                        )
142
+                    )
143
+                );
144
+
145
+                $this->server->addPlugin(
146
+                    new \OCA\DAV\Connector\Sabre\QuotaPlugin($view)
147
+                );
148
+            }
149
+        });
150
+    }
151
+
152
+    public function exec() {
153
+        $this->server->exec();
154
+    }
155 155
 }
Please login to merge, or discard this patch.
apps/encryption/lib/keymanager.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -32,7 +32,6 @@
 block discarded – undo
32 32
 use OCA\Encryption\Crypto\Crypt;
33 33
 use OCP\Encryption\Keys\IStorage;
34 34
 use OCP\IConfig;
35
-use OCP\IDBConnection;
36 35
 use OCP\ILogger;
37 36
 use OCP\IUserSession;
38 37
 
Please login to merge, or discard this patch.
Indentation   +653 added lines, -653 removed lines patch added patch discarded remove patch
@@ -40,658 +40,658 @@
 block discarded – undo
40 40
 
41 41
 class KeyManager {
42 42
 
43
-	/**
44
-	 * @var Session
45
-	 */
46
-	protected $session;
47
-	/**
48
-	 * @var IStorage
49
-	 */
50
-	private $keyStorage;
51
-	/**
52
-	 * @var Crypt
53
-	 */
54
-	private $crypt;
55
-	/**
56
-	 * @var string
57
-	 */
58
-	private $recoveryKeyId;
59
-	/**
60
-	 * @var string
61
-	 */
62
-	private $publicShareKeyId;
63
-	/**
64
-	 * @var string
65
-	 */
66
-	private $masterKeyId;
67
-	/**
68
-	 * @var string UserID
69
-	 */
70
-	private $keyId;
71
-	/**
72
-	 * @var string
73
-	 */
74
-	private $publicKeyId = 'publicKey';
75
-	/**
76
-	 * @var string
77
-	 */
78
-	private $privateKeyId = 'privateKey';
79
-
80
-	/**
81
-	 * @var string
82
-	 */
83
-	private $shareKeyId = 'shareKey';
84
-
85
-	/**
86
-	 * @var string
87
-	 */
88
-	private $fileKeyId = 'fileKey';
89
-	/**
90
-	 * @var IConfig
91
-	 */
92
-	private $config;
93
-	/**
94
-	 * @var ILogger
95
-	 */
96
-	private $log;
97
-	/**
98
-	 * @var Util
99
-	 */
100
-	private $util;
101
-
102
-	/**
103
-	 * @param IStorage $keyStorage
104
-	 * @param Crypt $crypt
105
-	 * @param IConfig $config
106
-	 * @param IUserSession $userSession
107
-	 * @param Session $session
108
-	 * @param ILogger $log
109
-	 * @param Util $util
110
-	 */
111
-	public function __construct(
112
-		IStorage $keyStorage,
113
-		Crypt $crypt,
114
-		IConfig $config,
115
-		IUserSession $userSession,
116
-		Session $session,
117
-		ILogger $log,
118
-		Util $util
119
-	) {
120
-
121
-		$this->util = $util;
122
-		$this->session = $session;
123
-		$this->keyStorage = $keyStorage;
124
-		$this->crypt = $crypt;
125
-		$this->config = $config;
126
-		$this->log = $log;
127
-
128
-		$this->recoveryKeyId = $this->config->getAppValue('encryption',
129
-			'recoveryKeyId');
130
-		if (empty($this->recoveryKeyId)) {
131
-			$this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
132
-			$this->config->setAppValue('encryption',
133
-				'recoveryKeyId',
134
-				$this->recoveryKeyId);
135
-		}
136
-
137
-		$this->publicShareKeyId = $this->config->getAppValue('encryption',
138
-			'publicShareKeyId');
139
-		if (empty($this->publicShareKeyId)) {
140
-			$this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
141
-			$this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
142
-		}
143
-
144
-		$this->masterKeyId = $this->config->getAppValue('encryption',
145
-			'masterKeyId');
146
-		if (empty($this->masterKeyId)) {
147
-			$this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
148
-			$this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
149
-		}
150
-
151
-		$this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
152
-		$this->log = $log;
153
-	}
154
-
155
-	/**
156
-	 * check if key pair for public link shares exists, if not we create one
157
-	 */
158
-	public function validateShareKey() {
159
-		$shareKey = $this->getPublicShareKey();
160
-		if (empty($shareKey)) {
161
-			$keyPair = $this->crypt->createKeyPair();
162
-
163
-			// Save public key
164
-			$this->keyStorage->setSystemUserKey(
165
-				$this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
166
-				Encryption::ID);
167
-
168
-			// Encrypt private key empty passphrase
169
-			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
170
-			$header = $this->crypt->generateHeader();
171
-			$this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
172
-		}
173
-	}
174
-
175
-	/**
176
-	 * check if a key pair for the master key exists, if not we create one
177
-	 */
178
-	public function validateMasterKey() {
179
-		$masterKey = $this->getPublicMasterKey();
180
-		if (empty($masterKey)) {
181
-			$keyPair = $this->crypt->createKeyPair();
182
-
183
-			// Save public key
184
-			$this->keyStorage->setSystemUserKey(
185
-				$this->masterKeyId . '.publicKey', $keyPair['publicKey'],
186
-				Encryption::ID);
187
-
188
-			// Encrypt private key with system password
189
-			$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
190
-			$header = $this->crypt->generateHeader();
191
-			$this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
192
-		}
193
-	}
194
-
195
-	/**
196
-	 * @return bool
197
-	 */
198
-	public function recoveryKeyExists() {
199
-		$key = $this->getRecoveryKey();
200
-		return (!empty($key));
201
-	}
202
-
203
-	/**
204
-	 * get recovery key
205
-	 *
206
-	 * @return string
207
-	 */
208
-	public function getRecoveryKey() {
209
-		return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
210
-	}
211
-
212
-	/**
213
-	 * get recovery key ID
214
-	 *
215
-	 * @return string
216
-	 */
217
-	public function getRecoveryKeyId() {
218
-		return $this->recoveryKeyId;
219
-	}
220
-
221
-	/**
222
-	 * @param string $password
223
-	 * @return bool
224
-	 */
225
-	public function checkRecoveryPassword($password) {
226
-		$recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
227
-		$decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password);
228
-
229
-		if ($decryptedRecoveryKey) {
230
-			return true;
231
-		}
232
-		return false;
233
-	}
234
-
235
-	/**
236
-	 * @param string $uid
237
-	 * @param string $password
238
-	 * @param string $keyPair
239
-	 * @return bool
240
-	 */
241
-	public function storeKeyPair($uid, $password, $keyPair) {
242
-		// Save Public Key
243
-		$this->setPublicKey($uid, $keyPair['publicKey']);
244
-
245
-		$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid);
246
-
247
-		$header = $this->crypt->generateHeader();
248
-
249
-		if ($encryptedKey) {
250
-			$this->setPrivateKey($uid, $header . $encryptedKey);
251
-			return true;
252
-		}
253
-		return false;
254
-	}
255
-
256
-	/**
257
-	 * @param string $password
258
-	 * @param array $keyPair
259
-	 * @return bool
260
-	 */
261
-	public function setRecoveryKey($password, $keyPair) {
262
-		// Save Public Key
263
-		$this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
264
-			'.publicKey',
265
-			$keyPair['publicKey'],
266
-			Encryption::ID);
267
-
268
-		$encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password);
269
-		$header = $this->crypt->generateHeader();
270
-
271
-		if ($encryptedKey) {
272
-			$this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
273
-			return true;
274
-		}
275
-		return false;
276
-	}
277
-
278
-	/**
279
-	 * @param $userId
280
-	 * @param $key
281
-	 * @return bool
282
-	 */
283
-	public function setPublicKey($userId, $key) {
284
-		return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
285
-	}
286
-
287
-	/**
288
-	 * @param $userId
289
-	 * @param string $key
290
-	 * @return bool
291
-	 */
292
-	public function setPrivateKey($userId, $key) {
293
-		return $this->keyStorage->setUserKey($userId,
294
-			$this->privateKeyId,
295
-			$key,
296
-			Encryption::ID);
297
-	}
298
-
299
-	/**
300
-	 * write file key to key storage
301
-	 *
302
-	 * @param string $path
303
-	 * @param string $key
304
-	 * @return boolean
305
-	 */
306
-	public function setFileKey($path, $key) {
307
-		return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
308
-	}
309
-
310
-	/**
311
-	 * set all file keys (the file key and the corresponding share keys)
312
-	 *
313
-	 * @param string $path
314
-	 * @param array $keys
315
-	 */
316
-	public function setAllFileKeys($path, $keys) {
317
-		$this->setFileKey($path, $keys['data']);
318
-		foreach ($keys['keys'] as $uid => $keyFile) {
319
-			$this->setShareKey($path, $uid, $keyFile);
320
-		}
321
-	}
322
-
323
-	/**
324
-	 * write share key to the key storage
325
-	 *
326
-	 * @param string $path
327
-	 * @param string $uid
328
-	 * @param string $key
329
-	 * @return boolean
330
-	 */
331
-	public function setShareKey($path, $uid, $key) {
332
-		$keyId = $uid . '.' . $this->shareKeyId;
333
-		return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
334
-	}
335
-
336
-	/**
337
-	 * Decrypt private key and store it
338
-	 *
339
-	 * @param string $uid userid
340
-	 * @param string $passPhrase users password
341
-	 * @return boolean
342
-	 */
343
-	public function init($uid, $passPhrase) {
344
-
345
-		$this->session->setStatus(Session::INIT_EXECUTED);
346
-
347
-
348
-		try {
349
-			if($this->util->isMasterKeyEnabled()) {
350
-				$uid = $this->getMasterKeyId();
351
-				$passPhrase = $this->getMasterKeyPassword();
352
-				$privateKey = $this->getSystemPrivateKey($uid);
353
-			} else {
354
-				$privateKey = $this->getPrivateKey($uid);
355
-			}
356
-			$privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid);
357
-		} catch (PrivateKeyMissingException $e) {
358
-			return false;
359
-		} catch (DecryptionFailedException $e) {
360
-			return false;
361
-		} catch (\Exception $e) {
362
-			$this->log->warning(
363
-				'Could not decrypt the private key from user "' . $uid . '"" during login. ' .
364
-				'Assume password change on the user back-end. Error message: '
365
-				. $e->getMessage()
366
-			);
367
-			return false;
368
-		}
369
-
370
-		if ($privateKey) {
371
-			$this->session->setPrivateKey($privateKey);
372
-			$this->session->setStatus(Session::INIT_SUCCESSFUL);
373
-			return true;
374
-		}
375
-
376
-		return false;
377
-	}
378
-
379
-	/**
380
-	 * @param $userId
381
-	 * @return string
382
-	 * @throws PrivateKeyMissingException
383
-	 */
384
-	public function getPrivateKey($userId) {
385
-		$privateKey = $this->keyStorage->getUserKey($userId,
386
-			$this->privateKeyId, Encryption::ID);
387
-
388
-		if (strlen($privateKey) !== 0) {
389
-			return $privateKey;
390
-		}
391
-		throw new PrivateKeyMissingException($userId);
392
-	}
393
-
394
-	/**
395
-	 * @param string $path
396
-	 * @param $uid
397
-	 * @return string
398
-	 */
399
-	public function getFileKey($path, $uid) {
400
-		$encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
401
-
402
-		if (is_null($uid)) {
403
-			$uid = $this->getPublicShareKeyId();
404
-			$shareKey = $this->getShareKey($path, $uid);
405
-			$privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
406
-			$privateKey = $this->crypt->decryptPrivateKey($privateKey);
407
-		} else {
408
-
409
-			if ($this->util->isMasterKeyEnabled()) {
410
-				$uid = $this->getMasterKeyId();
411
-			}
412
-
413
-			$shareKey = $this->getShareKey($path, $uid);
414
-			$privateKey = $this->session->getPrivateKey();
415
-		}
416
-
417
-		if ($encryptedFileKey && $shareKey && $privateKey) {
418
-			return $this->crypt->multiKeyDecrypt($encryptedFileKey,
419
-				$shareKey,
420
-				$privateKey);
421
-		}
422
-
423
-		return '';
424
-	}
425
-
426
-	/**
427
-	 * Get the current version of a file
428
-	 *
429
-	 * @param string $path
430
-	 * @param View $view
431
-	 * @return int
432
-	 */
433
-	public function getVersion($path, View $view) {
434
-		$fileInfo = $view->getFileInfo($path);
435
-		if($fileInfo === false) {
436
-			return 0;
437
-		}
438
-		return $fileInfo->getEncryptedVersion();
439
-	}
440
-
441
-	/**
442
-	 * Set the current version of a file
443
-	 *
444
-	 * @param string $path
445
-	 * @param int $version
446
-	 * @param View $view
447
-	 */
448
-	public function setVersion($path, $version, View $view) {
449
-		$fileInfo= $view->getFileInfo($path);
450
-
451
-		if($fileInfo !== false) {
452
-			$cache = $fileInfo->getStorage()->getCache();
453
-			$cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
454
-		}
455
-	}
456
-
457
-	/**
458
-	 * get the encrypted file key
459
-	 *
460
-	 * @param string $path
461
-	 * @return string
462
-	 */
463
-	public function getEncryptedFileKey($path) {
464
-		$encryptedFileKey = $this->keyStorage->getFileKey($path,
465
-			$this->fileKeyId, Encryption::ID);
466
-
467
-		return $encryptedFileKey;
468
-	}
469
-
470
-	/**
471
-	 * delete share key
472
-	 *
473
-	 * @param string $path
474
-	 * @param string $keyId
475
-	 * @return boolean
476
-	 */
477
-	public function deleteShareKey($path, $keyId) {
478
-		return $this->keyStorage->deleteFileKey(
479
-			$path,
480
-			$keyId . '.' . $this->shareKeyId,
481
-			Encryption::ID);
482
-	}
483
-
484
-
485
-	/**
486
-	 * @param $path
487
-	 * @param $uid
488
-	 * @return mixed
489
-	 */
490
-	public function getShareKey($path, $uid) {
491
-		$keyId = $uid . '.' . $this->shareKeyId;
492
-		return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
493
-	}
494
-
495
-	/**
496
-	 * check if user has a private and a public key
497
-	 *
498
-	 * @param string $userId
499
-	 * @return bool
500
-	 * @throws PrivateKeyMissingException
501
-	 * @throws PublicKeyMissingException
502
-	 */
503
-	public function userHasKeys($userId) {
504
-		$privateKey = $publicKey = true;
505
-		$exception = null;
506
-
507
-		try {
508
-			$this->getPrivateKey($userId);
509
-		} catch (PrivateKeyMissingException $e) {
510
-			$privateKey = false;
511
-			$exception = $e;
512
-		}
513
-		try {
514
-			$this->getPublicKey($userId);
515
-		} catch (PublicKeyMissingException $e) {
516
-			$publicKey = false;
517
-			$exception = $e;
518
-		}
519
-
520
-		if ($privateKey && $publicKey) {
521
-			return true;
522
-		} elseif (!$privateKey && !$publicKey) {
523
-			return false;
524
-		} else {
525
-			throw $exception;
526
-		}
527
-	}
528
-
529
-	/**
530
-	 * @param $userId
531
-	 * @return mixed
532
-	 * @throws PublicKeyMissingException
533
-	 */
534
-	public function getPublicKey($userId) {
535
-		$publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
536
-
537
-		if (strlen($publicKey) !== 0) {
538
-			return $publicKey;
539
-		}
540
-		throw new PublicKeyMissingException($userId);
541
-	}
542
-
543
-	public function getPublicShareKeyId() {
544
-		return $this->publicShareKeyId;
545
-	}
546
-
547
-	/**
548
-	 * get public key for public link shares
549
-	 *
550
-	 * @return string
551
-	 */
552
-	public function getPublicShareKey() {
553
-		return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
554
-	}
555
-
556
-	/**
557
-	 * @param string $purpose
558
-	 * @param bool $timestamp
559
-	 * @param bool $includeUserKeys
560
-	 */
561
-	public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) {
43
+    /**
44
+     * @var Session
45
+     */
46
+    protected $session;
47
+    /**
48
+     * @var IStorage
49
+     */
50
+    private $keyStorage;
51
+    /**
52
+     * @var Crypt
53
+     */
54
+    private $crypt;
55
+    /**
56
+     * @var string
57
+     */
58
+    private $recoveryKeyId;
59
+    /**
60
+     * @var string
61
+     */
62
+    private $publicShareKeyId;
63
+    /**
64
+     * @var string
65
+     */
66
+    private $masterKeyId;
67
+    /**
68
+     * @var string UserID
69
+     */
70
+    private $keyId;
71
+    /**
72
+     * @var string
73
+     */
74
+    private $publicKeyId = 'publicKey';
75
+    /**
76
+     * @var string
77
+     */
78
+    private $privateKeyId = 'privateKey';
79
+
80
+    /**
81
+     * @var string
82
+     */
83
+    private $shareKeyId = 'shareKey';
84
+
85
+    /**
86
+     * @var string
87
+     */
88
+    private $fileKeyId = 'fileKey';
89
+    /**
90
+     * @var IConfig
91
+     */
92
+    private $config;
93
+    /**
94
+     * @var ILogger
95
+     */
96
+    private $log;
97
+    /**
98
+     * @var Util
99
+     */
100
+    private $util;
101
+
102
+    /**
103
+     * @param IStorage $keyStorage
104
+     * @param Crypt $crypt
105
+     * @param IConfig $config
106
+     * @param IUserSession $userSession
107
+     * @param Session $session
108
+     * @param ILogger $log
109
+     * @param Util $util
110
+     */
111
+    public function __construct(
112
+        IStorage $keyStorage,
113
+        Crypt $crypt,
114
+        IConfig $config,
115
+        IUserSession $userSession,
116
+        Session $session,
117
+        ILogger $log,
118
+        Util $util
119
+    ) {
120
+
121
+        $this->util = $util;
122
+        $this->session = $session;
123
+        $this->keyStorage = $keyStorage;
124
+        $this->crypt = $crypt;
125
+        $this->config = $config;
126
+        $this->log = $log;
127
+
128
+        $this->recoveryKeyId = $this->config->getAppValue('encryption',
129
+            'recoveryKeyId');
130
+        if (empty($this->recoveryKeyId)) {
131
+            $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8);
132
+            $this->config->setAppValue('encryption',
133
+                'recoveryKeyId',
134
+                $this->recoveryKeyId);
135
+        }
136
+
137
+        $this->publicShareKeyId = $this->config->getAppValue('encryption',
138
+            'publicShareKeyId');
139
+        if (empty($this->publicShareKeyId)) {
140
+            $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8);
141
+            $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId);
142
+        }
143
+
144
+        $this->masterKeyId = $this->config->getAppValue('encryption',
145
+            'masterKeyId');
146
+        if (empty($this->masterKeyId)) {
147
+            $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8);
148
+            $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId);
149
+        }
150
+
151
+        $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false;
152
+        $this->log = $log;
153
+    }
154
+
155
+    /**
156
+     * check if key pair for public link shares exists, if not we create one
157
+     */
158
+    public function validateShareKey() {
159
+        $shareKey = $this->getPublicShareKey();
160
+        if (empty($shareKey)) {
161
+            $keyPair = $this->crypt->createKeyPair();
162
+
163
+            // Save public key
164
+            $this->keyStorage->setSystemUserKey(
165
+                $this->publicShareKeyId . '.publicKey', $keyPair['publicKey'],
166
+                Encryption::ID);
167
+
168
+            // Encrypt private key empty passphrase
169
+            $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], '');
170
+            $header = $this->crypt->generateHeader();
171
+            $this->setSystemPrivateKey($this->publicShareKeyId, $header . $encryptedKey);
172
+        }
173
+    }
174
+
175
+    /**
176
+     * check if a key pair for the master key exists, if not we create one
177
+     */
178
+    public function validateMasterKey() {
179
+        $masterKey = $this->getPublicMasterKey();
180
+        if (empty($masterKey)) {
181
+            $keyPair = $this->crypt->createKeyPair();
182
+
183
+            // Save public key
184
+            $this->keyStorage->setSystemUserKey(
185
+                $this->masterKeyId . '.publicKey', $keyPair['publicKey'],
186
+                Encryption::ID);
187
+
188
+            // Encrypt private key with system password
189
+            $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $this->getMasterKeyPassword(), $this->masterKeyId);
190
+            $header = $this->crypt->generateHeader();
191
+            $this->setSystemPrivateKey($this->masterKeyId, $header . $encryptedKey);
192
+        }
193
+    }
194
+
195
+    /**
196
+     * @return bool
197
+     */
198
+    public function recoveryKeyExists() {
199
+        $key = $this->getRecoveryKey();
200
+        return (!empty($key));
201
+    }
202
+
203
+    /**
204
+     * get recovery key
205
+     *
206
+     * @return string
207
+     */
208
+    public function getRecoveryKey() {
209
+        return $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.publicKey', Encryption::ID);
210
+    }
211
+
212
+    /**
213
+     * get recovery key ID
214
+     *
215
+     * @return string
216
+     */
217
+    public function getRecoveryKeyId() {
218
+        return $this->recoveryKeyId;
219
+    }
220
+
221
+    /**
222
+     * @param string $password
223
+     * @return bool
224
+     */
225
+    public function checkRecoveryPassword($password) {
226
+        $recoveryKey = $this->keyStorage->getSystemUserKey($this->recoveryKeyId . '.privateKey', Encryption::ID);
227
+        $decryptedRecoveryKey = $this->crypt->decryptPrivateKey($recoveryKey, $password);
228
+
229
+        if ($decryptedRecoveryKey) {
230
+            return true;
231
+        }
232
+        return false;
233
+    }
234
+
235
+    /**
236
+     * @param string $uid
237
+     * @param string $password
238
+     * @param string $keyPair
239
+     * @return bool
240
+     */
241
+    public function storeKeyPair($uid, $password, $keyPair) {
242
+        // Save Public Key
243
+        $this->setPublicKey($uid, $keyPair['publicKey']);
244
+
245
+        $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password, $uid);
246
+
247
+        $header = $this->crypt->generateHeader();
248
+
249
+        if ($encryptedKey) {
250
+            $this->setPrivateKey($uid, $header . $encryptedKey);
251
+            return true;
252
+        }
253
+        return false;
254
+    }
255
+
256
+    /**
257
+     * @param string $password
258
+     * @param array $keyPair
259
+     * @return bool
260
+     */
261
+    public function setRecoveryKey($password, $keyPair) {
262
+        // Save Public Key
263
+        $this->keyStorage->setSystemUserKey($this->getRecoveryKeyId().
264
+            '.publicKey',
265
+            $keyPair['publicKey'],
266
+            Encryption::ID);
267
+
268
+        $encryptedKey = $this->crypt->encryptPrivateKey($keyPair['privateKey'], $password);
269
+        $header = $this->crypt->generateHeader();
270
+
271
+        if ($encryptedKey) {
272
+            $this->setSystemPrivateKey($this->getRecoveryKeyId(), $header . $encryptedKey);
273
+            return true;
274
+        }
275
+        return false;
276
+    }
277
+
278
+    /**
279
+     * @param $userId
280
+     * @param $key
281
+     * @return bool
282
+     */
283
+    public function setPublicKey($userId, $key) {
284
+        return $this->keyStorage->setUserKey($userId, $this->publicKeyId, $key, Encryption::ID);
285
+    }
286
+
287
+    /**
288
+     * @param $userId
289
+     * @param string $key
290
+     * @return bool
291
+     */
292
+    public function setPrivateKey($userId, $key) {
293
+        return $this->keyStorage->setUserKey($userId,
294
+            $this->privateKeyId,
295
+            $key,
296
+            Encryption::ID);
297
+    }
298
+
299
+    /**
300
+     * write file key to key storage
301
+     *
302
+     * @param string $path
303
+     * @param string $key
304
+     * @return boolean
305
+     */
306
+    public function setFileKey($path, $key) {
307
+        return $this->keyStorage->setFileKey($path, $this->fileKeyId, $key, Encryption::ID);
308
+    }
309
+
310
+    /**
311
+     * set all file keys (the file key and the corresponding share keys)
312
+     *
313
+     * @param string $path
314
+     * @param array $keys
315
+     */
316
+    public function setAllFileKeys($path, $keys) {
317
+        $this->setFileKey($path, $keys['data']);
318
+        foreach ($keys['keys'] as $uid => $keyFile) {
319
+            $this->setShareKey($path, $uid, $keyFile);
320
+        }
321
+    }
322
+
323
+    /**
324
+     * write share key to the key storage
325
+     *
326
+     * @param string $path
327
+     * @param string $uid
328
+     * @param string $key
329
+     * @return boolean
330
+     */
331
+    public function setShareKey($path, $uid, $key) {
332
+        $keyId = $uid . '.' . $this->shareKeyId;
333
+        return $this->keyStorage->setFileKey($path, $keyId, $key, Encryption::ID);
334
+    }
335
+
336
+    /**
337
+     * Decrypt private key and store it
338
+     *
339
+     * @param string $uid userid
340
+     * @param string $passPhrase users password
341
+     * @return boolean
342
+     */
343
+    public function init($uid, $passPhrase) {
344
+
345
+        $this->session->setStatus(Session::INIT_EXECUTED);
346
+
347
+
348
+        try {
349
+            if($this->util->isMasterKeyEnabled()) {
350
+                $uid = $this->getMasterKeyId();
351
+                $passPhrase = $this->getMasterKeyPassword();
352
+                $privateKey = $this->getSystemPrivateKey($uid);
353
+            } else {
354
+                $privateKey = $this->getPrivateKey($uid);
355
+            }
356
+            $privateKey = $this->crypt->decryptPrivateKey($privateKey, $passPhrase, $uid);
357
+        } catch (PrivateKeyMissingException $e) {
358
+            return false;
359
+        } catch (DecryptionFailedException $e) {
360
+            return false;
361
+        } catch (\Exception $e) {
362
+            $this->log->warning(
363
+                'Could not decrypt the private key from user "' . $uid . '"" during login. ' .
364
+                'Assume password change on the user back-end. Error message: '
365
+                . $e->getMessage()
366
+            );
367
+            return false;
368
+        }
369
+
370
+        if ($privateKey) {
371
+            $this->session->setPrivateKey($privateKey);
372
+            $this->session->setStatus(Session::INIT_SUCCESSFUL);
373
+            return true;
374
+        }
375
+
376
+        return false;
377
+    }
378
+
379
+    /**
380
+     * @param $userId
381
+     * @return string
382
+     * @throws PrivateKeyMissingException
383
+     */
384
+    public function getPrivateKey($userId) {
385
+        $privateKey = $this->keyStorage->getUserKey($userId,
386
+            $this->privateKeyId, Encryption::ID);
387
+
388
+        if (strlen($privateKey) !== 0) {
389
+            return $privateKey;
390
+        }
391
+        throw new PrivateKeyMissingException($userId);
392
+    }
393
+
394
+    /**
395
+     * @param string $path
396
+     * @param $uid
397
+     * @return string
398
+     */
399
+    public function getFileKey($path, $uid) {
400
+        $encryptedFileKey = $this->keyStorage->getFileKey($path, $this->fileKeyId, Encryption::ID);
401
+
402
+        if (is_null($uid)) {
403
+            $uid = $this->getPublicShareKeyId();
404
+            $shareKey = $this->getShareKey($path, $uid);
405
+            $privateKey = $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.privateKey', Encryption::ID);
406
+            $privateKey = $this->crypt->decryptPrivateKey($privateKey);
407
+        } else {
408
+
409
+            if ($this->util->isMasterKeyEnabled()) {
410
+                $uid = $this->getMasterKeyId();
411
+            }
412
+
413
+            $shareKey = $this->getShareKey($path, $uid);
414
+            $privateKey = $this->session->getPrivateKey();
415
+        }
416
+
417
+        if ($encryptedFileKey && $shareKey && $privateKey) {
418
+            return $this->crypt->multiKeyDecrypt($encryptedFileKey,
419
+                $shareKey,
420
+                $privateKey);
421
+        }
422
+
423
+        return '';
424
+    }
425
+
426
+    /**
427
+     * Get the current version of a file
428
+     *
429
+     * @param string $path
430
+     * @param View $view
431
+     * @return int
432
+     */
433
+    public function getVersion($path, View $view) {
434
+        $fileInfo = $view->getFileInfo($path);
435
+        if($fileInfo === false) {
436
+            return 0;
437
+        }
438
+        return $fileInfo->getEncryptedVersion();
439
+    }
440
+
441
+    /**
442
+     * Set the current version of a file
443
+     *
444
+     * @param string $path
445
+     * @param int $version
446
+     * @param View $view
447
+     */
448
+    public function setVersion($path, $version, View $view) {
449
+        $fileInfo= $view->getFileInfo($path);
450
+
451
+        if($fileInfo !== false) {
452
+            $cache = $fileInfo->getStorage()->getCache();
453
+            $cache->update($fileInfo->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
454
+        }
455
+    }
456
+
457
+    /**
458
+     * get the encrypted file key
459
+     *
460
+     * @param string $path
461
+     * @return string
462
+     */
463
+    public function getEncryptedFileKey($path) {
464
+        $encryptedFileKey = $this->keyStorage->getFileKey($path,
465
+            $this->fileKeyId, Encryption::ID);
466
+
467
+        return $encryptedFileKey;
468
+    }
469
+
470
+    /**
471
+     * delete share key
472
+     *
473
+     * @param string $path
474
+     * @param string $keyId
475
+     * @return boolean
476
+     */
477
+    public function deleteShareKey($path, $keyId) {
478
+        return $this->keyStorage->deleteFileKey(
479
+            $path,
480
+            $keyId . '.' . $this->shareKeyId,
481
+            Encryption::ID);
482
+    }
483
+
484
+
485
+    /**
486
+     * @param $path
487
+     * @param $uid
488
+     * @return mixed
489
+     */
490
+    public function getShareKey($path, $uid) {
491
+        $keyId = $uid . '.' . $this->shareKeyId;
492
+        return $this->keyStorage->getFileKey($path, $keyId, Encryption::ID);
493
+    }
494
+
495
+    /**
496
+     * check if user has a private and a public key
497
+     *
498
+     * @param string $userId
499
+     * @return bool
500
+     * @throws PrivateKeyMissingException
501
+     * @throws PublicKeyMissingException
502
+     */
503
+    public function userHasKeys($userId) {
504
+        $privateKey = $publicKey = true;
505
+        $exception = null;
506
+
507
+        try {
508
+            $this->getPrivateKey($userId);
509
+        } catch (PrivateKeyMissingException $e) {
510
+            $privateKey = false;
511
+            $exception = $e;
512
+        }
513
+        try {
514
+            $this->getPublicKey($userId);
515
+        } catch (PublicKeyMissingException $e) {
516
+            $publicKey = false;
517
+            $exception = $e;
518
+        }
519
+
520
+        if ($privateKey && $publicKey) {
521
+            return true;
522
+        } elseif (!$privateKey && !$publicKey) {
523
+            return false;
524
+        } else {
525
+            throw $exception;
526
+        }
527
+    }
528
+
529
+    /**
530
+     * @param $userId
531
+     * @return mixed
532
+     * @throws PublicKeyMissingException
533
+     */
534
+    public function getPublicKey($userId) {
535
+        $publicKey = $this->keyStorage->getUserKey($userId, $this->publicKeyId, Encryption::ID);
536
+
537
+        if (strlen($publicKey) !== 0) {
538
+            return $publicKey;
539
+        }
540
+        throw new PublicKeyMissingException($userId);
541
+    }
542
+
543
+    public function getPublicShareKeyId() {
544
+        return $this->publicShareKeyId;
545
+    }
546
+
547
+    /**
548
+     * get public key for public link shares
549
+     *
550
+     * @return string
551
+     */
552
+    public function getPublicShareKey() {
553
+        return $this->keyStorage->getSystemUserKey($this->publicShareKeyId . '.publicKey', Encryption::ID);
554
+    }
555
+
556
+    /**
557
+     * @param string $purpose
558
+     * @param bool $timestamp
559
+     * @param bool $includeUserKeys
560
+     */
561
+    public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) {
562 562
 //		$backupDir = $this->keyStorage->;
563
-	}
564
-
565
-	/**
566
-	 * @param string $uid
567
-	 */
568
-	public function replaceUserKeys($uid) {
569
-		$this->backupAllKeys('password_reset');
570
-		$this->deletePublicKey($uid);
571
-		$this->deletePrivateKey($uid);
572
-	}
573
-
574
-	/**
575
-	 * @param $uid
576
-	 * @return bool
577
-	 */
578
-	public function deletePublicKey($uid) {
579
-		return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
580
-	}
581
-
582
-	/**
583
-	 * @param string $uid
584
-	 * @return bool
585
-	 */
586
-	private function deletePrivateKey($uid) {
587
-		return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
588
-	}
589
-
590
-	/**
591
-	 * @param string $path
592
-	 * @return bool
593
-	 */
594
-	public function deleteAllFileKeys($path) {
595
-		return $this->keyStorage->deleteAllFileKeys($path);
596
-	}
597
-
598
-	/**
599
-	 * @param array $userIds
600
-	 * @return array
601
-	 * @throws PublicKeyMissingException
602
-	 */
603
-	public function getPublicKeys(array $userIds) {
604
-		$keys = [];
605
-
606
-		foreach ($userIds as $userId) {
607
-			try {
608
-				$keys[$userId] = $this->getPublicKey($userId);
609
-			} catch (PublicKeyMissingException $e) {
610
-				continue;
611
-			}
612
-		}
613
-
614
-		return $keys;
615
-
616
-	}
617
-
618
-	/**
619
-	 * @param string $keyId
620
-	 * @return string returns openssl key
621
-	 */
622
-	public function getSystemPrivateKey($keyId) {
623
-		return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
624
-	}
625
-
626
-	/**
627
-	 * @param string $keyId
628
-	 * @param string $key
629
-	 * @return string returns openssl key
630
-	 */
631
-	public function setSystemPrivateKey($keyId, $key) {
632
-		return $this->keyStorage->setSystemUserKey(
633
-			$keyId . '.' . $this->privateKeyId,
634
-			$key,
635
-			Encryption::ID);
636
-	}
637
-
638
-	/**
639
-	 * add system keys such as the public share key and the recovery key
640
-	 *
641
-	 * @param array $accessList
642
-	 * @param array $publicKeys
643
-	 * @param string $uid
644
-	 * @return array
645
-	 * @throws PublicKeyMissingException
646
-	 */
647
-	public function addSystemKeys(array $accessList, array $publicKeys, $uid) {
648
-		if (!empty($accessList['public'])) {
649
-			$publicShareKey = $this->getPublicShareKey();
650
-			if (empty($publicShareKey)) {
651
-				throw new PublicKeyMissingException($this->getPublicShareKeyId());
652
-			}
653
-			$publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
654
-		}
655
-
656
-		if ($this->recoveryKeyExists() &&
657
-			$this->util->isRecoveryEnabledForUser($uid)) {
658
-
659
-			$publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
660
-		}
661
-
662
-		return $publicKeys;
663
-	}
664
-
665
-	/**
666
-	 * get master key password
667
-	 *
668
-	 * @return string
669
-	 * @throws \Exception
670
-	 */
671
-	public function getMasterKeyPassword() {
672
-		$password = $this->config->getSystemValue('secret');
673
-		if (empty($password)){
674
-			throw new \Exception('Can not get secret from instance');
675
-		}
676
-
677
-		return $password;
678
-	}
679
-
680
-	/**
681
-	 * return master key id
682
-	 *
683
-	 * @return string
684
-	 */
685
-	public function getMasterKeyId() {
686
-		return $this->masterKeyId;
687
-	}
688
-
689
-	/**
690
-	 * get public master key
691
-	 *
692
-	 * @return string
693
-	 */
694
-	public function getPublicMasterKey() {
695
-		return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
696
-	}
563
+    }
564
+
565
+    /**
566
+     * @param string $uid
567
+     */
568
+    public function replaceUserKeys($uid) {
569
+        $this->backupAllKeys('password_reset');
570
+        $this->deletePublicKey($uid);
571
+        $this->deletePrivateKey($uid);
572
+    }
573
+
574
+    /**
575
+     * @param $uid
576
+     * @return bool
577
+     */
578
+    public function deletePublicKey($uid) {
579
+        return $this->keyStorage->deleteUserKey($uid, $this->publicKeyId, Encryption::ID);
580
+    }
581
+
582
+    /**
583
+     * @param string $uid
584
+     * @return bool
585
+     */
586
+    private function deletePrivateKey($uid) {
587
+        return $this->keyStorage->deleteUserKey($uid, $this->privateKeyId, Encryption::ID);
588
+    }
589
+
590
+    /**
591
+     * @param string $path
592
+     * @return bool
593
+     */
594
+    public function deleteAllFileKeys($path) {
595
+        return $this->keyStorage->deleteAllFileKeys($path);
596
+    }
597
+
598
+    /**
599
+     * @param array $userIds
600
+     * @return array
601
+     * @throws PublicKeyMissingException
602
+     */
603
+    public function getPublicKeys(array $userIds) {
604
+        $keys = [];
605
+
606
+        foreach ($userIds as $userId) {
607
+            try {
608
+                $keys[$userId] = $this->getPublicKey($userId);
609
+            } catch (PublicKeyMissingException $e) {
610
+                continue;
611
+            }
612
+        }
613
+
614
+        return $keys;
615
+
616
+    }
617
+
618
+    /**
619
+     * @param string $keyId
620
+     * @return string returns openssl key
621
+     */
622
+    public function getSystemPrivateKey($keyId) {
623
+        return $this->keyStorage->getSystemUserKey($keyId . '.' . $this->privateKeyId, Encryption::ID);
624
+    }
625
+
626
+    /**
627
+     * @param string $keyId
628
+     * @param string $key
629
+     * @return string returns openssl key
630
+     */
631
+    public function setSystemPrivateKey($keyId, $key) {
632
+        return $this->keyStorage->setSystemUserKey(
633
+            $keyId . '.' . $this->privateKeyId,
634
+            $key,
635
+            Encryption::ID);
636
+    }
637
+
638
+    /**
639
+     * add system keys such as the public share key and the recovery key
640
+     *
641
+     * @param array $accessList
642
+     * @param array $publicKeys
643
+     * @param string $uid
644
+     * @return array
645
+     * @throws PublicKeyMissingException
646
+     */
647
+    public function addSystemKeys(array $accessList, array $publicKeys, $uid) {
648
+        if (!empty($accessList['public'])) {
649
+            $publicShareKey = $this->getPublicShareKey();
650
+            if (empty($publicShareKey)) {
651
+                throw new PublicKeyMissingException($this->getPublicShareKeyId());
652
+            }
653
+            $publicKeys[$this->getPublicShareKeyId()] = $publicShareKey;
654
+        }
655
+
656
+        if ($this->recoveryKeyExists() &&
657
+            $this->util->isRecoveryEnabledForUser($uid)) {
658
+
659
+            $publicKeys[$this->getRecoveryKeyId()] = $this->getRecoveryKey();
660
+        }
661
+
662
+        return $publicKeys;
663
+    }
664
+
665
+    /**
666
+     * get master key password
667
+     *
668
+     * @return string
669
+     * @throws \Exception
670
+     */
671
+    public function getMasterKeyPassword() {
672
+        $password = $this->config->getSystemValue('secret');
673
+        if (empty($password)){
674
+            throw new \Exception('Can not get secret from instance');
675
+        }
676
+
677
+        return $password;
678
+    }
679
+
680
+    /**
681
+     * return master key id
682
+     *
683
+     * @return string
684
+     */
685
+    public function getMasterKeyId() {
686
+        return $this->masterKeyId;
687
+    }
688
+
689
+    /**
690
+     * get public master key
691
+     *
692
+     * @return string
693
+     */
694
+    public function getPublicMasterKey() {
695
+        return $this->keyStorage->getSystemUserKey($this->masterKeyId . '.publicKey', Encryption::ID);
696
+    }
697 697
 }
Please login to merge, or discard this patch.