Completed
Pull Request — master (#7363)
by Björn
14:32
created
lib/private/Files/Storage/Flysystem.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -54,6 +54,9 @@
 block discarded – undo
54 54
 		$this->flysystem->addPlugin(new GetWithMetadata());
55 55
 	}
56 56
 
57
+	/**
58
+	 * @param string $path
59
+	 */
57 60
 	protected function buildPath($path) {
58 61
 		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
59 62
 		return ltrim($fullPath, '/');
Please login to merge, or discard this patch.
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -35,223 +35,223 @@
 block discarded – undo
35 35
  * To use: subclass and call $this->buildFlysystem with the flysystem adapter of choice
36 36
  */
37 37
 abstract class Flysystem extends Common {
38
-	/**
39
-	 * @var Filesystem
40
-	 */
41
-	protected $flysystem;
38
+    /**
39
+     * @var Filesystem
40
+     */
41
+    protected $flysystem;
42 42
 
43
-	/**
44
-	 * @var string
45
-	 */
46
-	protected $root = '';
43
+    /**
44
+     * @var string
45
+     */
46
+    protected $root = '';
47 47
 
48
-	/**
49
-	 * Initialize the storage backend with a flyssytem adapter
50
-	 *
51
-	 * @param \League\Flysystem\AdapterInterface $adapter
52
-	 */
53
-	protected function buildFlySystem(AdapterInterface $adapter) {
54
-		$this->flysystem = new Filesystem($adapter);
55
-		$this->flysystem->addPlugin(new GetWithMetadata());
56
-	}
48
+    /**
49
+     * Initialize the storage backend with a flyssytem adapter
50
+     *
51
+     * @param \League\Flysystem\AdapterInterface $adapter
52
+     */
53
+    protected function buildFlySystem(AdapterInterface $adapter) {
54
+        $this->flysystem = new Filesystem($adapter);
55
+        $this->flysystem->addPlugin(new GetWithMetadata());
56
+    }
57 57
 
58
-	protected function buildPath($path) {
59
-		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
60
-		return ltrim($fullPath, '/');
61
-	}
58
+    protected function buildPath($path) {
59
+        $fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
60
+        return ltrim($fullPath, '/');
61
+    }
62 62
 
63
-	/**
64
-	 * {@inheritdoc}
65
-	 */
66
-	public function file_get_contents($path) {
67
-		return $this->flysystem->read($this->buildPath($path));
68
-	}
63
+    /**
64
+     * {@inheritdoc}
65
+     */
66
+    public function file_get_contents($path) {
67
+        return $this->flysystem->read($this->buildPath($path));
68
+    }
69 69
 
70
-	/**
71
-	 * {@inheritdoc}
72
-	 */
73
-	public function file_put_contents($path, $data) {
74
-		return $this->flysystem->put($this->buildPath($path), $data);
75
-	}
70
+    /**
71
+     * {@inheritdoc}
72
+     */
73
+    public function file_put_contents($path, $data) {
74
+        return $this->flysystem->put($this->buildPath($path), $data);
75
+    }
76 76
 
77
-	/**
78
-	 * {@inheritdoc}
79
-	 */
80
-	public function file_exists($path) {
81
-		return $this->flysystem->has($this->buildPath($path));
82
-	}
77
+    /**
78
+     * {@inheritdoc}
79
+     */
80
+    public function file_exists($path) {
81
+        return $this->flysystem->has($this->buildPath($path));
82
+    }
83 83
 
84
-	/**
85
-	 * {@inheritdoc}
86
-	 */
87
-	public function unlink($path) {
88
-		if ($this->is_dir($path)) {
89
-			return $this->rmdir($path);
90
-		}
91
-		try {
92
-			return $this->flysystem->delete($this->buildPath($path));
93
-		} catch (FileNotFoundException $e) {
94
-			return false;
95
-		}
96
-	}
84
+    /**
85
+     * {@inheritdoc}
86
+     */
87
+    public function unlink($path) {
88
+        if ($this->is_dir($path)) {
89
+            return $this->rmdir($path);
90
+        }
91
+        try {
92
+            return $this->flysystem->delete($this->buildPath($path));
93
+        } catch (FileNotFoundException $e) {
94
+            return false;
95
+        }
96
+    }
97 97
 
98
-	/**
99
-	 * {@inheritdoc}
100
-	 */
101
-	public function rename($source, $target) {
102
-		if ($this->file_exists($target)) {
103
-			$this->unlink($target);
104
-		}
105
-		return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target));
106
-	}
98
+    /**
99
+     * {@inheritdoc}
100
+     */
101
+    public function rename($source, $target) {
102
+        if ($this->file_exists($target)) {
103
+            $this->unlink($target);
104
+        }
105
+        return $this->flysystem->rename($this->buildPath($source), $this->buildPath($target));
106
+    }
107 107
 
108
-	/**
109
-	 * {@inheritdoc}
110
-	 */
111
-	public function copy($source, $target) {
112
-		if ($this->file_exists($target)) {
113
-			$this->unlink($target);
114
-		}
115
-		return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target));
116
-	}
108
+    /**
109
+     * {@inheritdoc}
110
+     */
111
+    public function copy($source, $target) {
112
+        if ($this->file_exists($target)) {
113
+            $this->unlink($target);
114
+        }
115
+        return $this->flysystem->copy($this->buildPath($source), $this->buildPath($target));
116
+    }
117 117
 
118
-	/**
119
-	 * {@inheritdoc}
120
-	 */
121
-	public function filesize($path) {
122
-		if ($this->is_dir($path)) {
123
-			return 0;
124
-		} else {
125
-			return $this->flysystem->getSize($this->buildPath($path));
126
-		}
127
-	}
118
+    /**
119
+     * {@inheritdoc}
120
+     */
121
+    public function filesize($path) {
122
+        if ($this->is_dir($path)) {
123
+            return 0;
124
+        } else {
125
+            return $this->flysystem->getSize($this->buildPath($path));
126
+        }
127
+    }
128 128
 
129
-	/**
130
-	 * {@inheritdoc}
131
-	 */
132
-	public function mkdir($path) {
133
-		if ($this->file_exists($path)) {
134
-			return false;
135
-		}
136
-		return $this->flysystem->createDir($this->buildPath($path));
137
-	}
129
+    /**
130
+     * {@inheritdoc}
131
+     */
132
+    public function mkdir($path) {
133
+        if ($this->file_exists($path)) {
134
+            return false;
135
+        }
136
+        return $this->flysystem->createDir($this->buildPath($path));
137
+    }
138 138
 
139
-	/**
140
-	 * {@inheritdoc}
141
-	 */
142
-	public function filemtime($path) {
143
-		return $this->flysystem->getTimestamp($this->buildPath($path));
144
-	}
139
+    /**
140
+     * {@inheritdoc}
141
+     */
142
+    public function filemtime($path) {
143
+        return $this->flysystem->getTimestamp($this->buildPath($path));
144
+    }
145 145
 
146
-	/**
147
-	 * {@inheritdoc}
148
-	 */
149
-	public function rmdir($path) {
150
-		try {
151
-			return @$this->flysystem->deleteDir($this->buildPath($path));
152
-		} catch (FileNotFoundException $e) {
153
-			return false;
154
-		}
155
-	}
146
+    /**
147
+     * {@inheritdoc}
148
+     */
149
+    public function rmdir($path) {
150
+        try {
151
+            return @$this->flysystem->deleteDir($this->buildPath($path));
152
+        } catch (FileNotFoundException $e) {
153
+            return false;
154
+        }
155
+    }
156 156
 
157
-	/**
158
-	 * {@inheritdoc}
159
-	 */
160
-	public function opendir($path) {
161
-		try {
162
-			$content = $this->flysystem->listContents($this->buildPath($path));
163
-		} catch (FileNotFoundException $e) {
164
-			return false;
165
-		}
166
-		$names = array_map(function ($object) {
167
-			return $object['basename'];
168
-		}, $content);
169
-		return IteratorDirectory::wrap($names);
170
-	}
157
+    /**
158
+     * {@inheritdoc}
159
+     */
160
+    public function opendir($path) {
161
+        try {
162
+            $content = $this->flysystem->listContents($this->buildPath($path));
163
+        } catch (FileNotFoundException $e) {
164
+            return false;
165
+        }
166
+        $names = array_map(function ($object) {
167
+            return $object['basename'];
168
+        }, $content);
169
+        return IteratorDirectory::wrap($names);
170
+    }
171 171
 
172
-	/**
173
-	 * {@inheritdoc}
174
-	 */
175
-	public function fopen($path, $mode) {
176
-		$fullPath = $this->buildPath($path);
177
-		$useExisting = true;
178
-		switch ($mode) {
179
-			case 'r':
180
-			case 'rb':
181
-				try {
182
-					return $this->flysystem->readStream($fullPath);
183
-				} catch (FileNotFoundException $e) {
184
-					return false;
185
-				}
186
-			case 'w':
187
-			case 'w+':
188
-			case 'wb':
189
-			case 'wb+':
190
-				$useExisting = false;
191
-			case 'a':
192
-			case 'ab':
193
-			case 'r+':
194
-			case 'a+':
195
-			case 'x':
196
-			case 'x+':
197
-			case 'c':
198
-			case 'c+':
199
-				//emulate these
200
-				if ($useExisting and $this->file_exists($path)) {
201
-					if (!$this->isUpdatable($path)) {
202
-						return false;
203
-					}
204
-					$tmpFile = $this->getCachedFile($path);
205
-				} else {
206
-					if (!$this->isCreatable(dirname($path))) {
207
-						return false;
208
-					}
209
-					$tmpFile = \OCP\Files::tmpFile();
210
-				}
211
-				$source = fopen($tmpFile, $mode);
212
-				return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
213
-					$this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214
-					unlink($tmpFile);
215
-				});
216
-		}
217
-		return false;
218
-	}
172
+    /**
173
+     * {@inheritdoc}
174
+     */
175
+    public function fopen($path, $mode) {
176
+        $fullPath = $this->buildPath($path);
177
+        $useExisting = true;
178
+        switch ($mode) {
179
+            case 'r':
180
+            case 'rb':
181
+                try {
182
+                    return $this->flysystem->readStream($fullPath);
183
+                } catch (FileNotFoundException $e) {
184
+                    return false;
185
+                }
186
+            case 'w':
187
+            case 'w+':
188
+            case 'wb':
189
+            case 'wb+':
190
+                $useExisting = false;
191
+            case 'a':
192
+            case 'ab':
193
+            case 'r+':
194
+            case 'a+':
195
+            case 'x':
196
+            case 'x+':
197
+            case 'c':
198
+            case 'c+':
199
+                //emulate these
200
+                if ($useExisting and $this->file_exists($path)) {
201
+                    if (!$this->isUpdatable($path)) {
202
+                        return false;
203
+                    }
204
+                    $tmpFile = $this->getCachedFile($path);
205
+                } else {
206
+                    if (!$this->isCreatable(dirname($path))) {
207
+                        return false;
208
+                    }
209
+                    $tmpFile = \OCP\Files::tmpFile();
210
+                }
211
+                $source = fopen($tmpFile, $mode);
212
+                return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
213
+                    $this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214
+                    unlink($tmpFile);
215
+                });
216
+        }
217
+        return false;
218
+    }
219 219
 
220
-	/**
221
-	 * {@inheritdoc}
222
-	 */
223
-	public function touch($path, $mtime = null) {
224
-		if ($this->file_exists($path)) {
225
-			return false;
226
-		} else {
227
-			$this->file_put_contents($path, '');
228
-			return true;
229
-		}
230
-	}
220
+    /**
221
+     * {@inheritdoc}
222
+     */
223
+    public function touch($path, $mtime = null) {
224
+        if ($this->file_exists($path)) {
225
+            return false;
226
+        } else {
227
+            $this->file_put_contents($path, '');
228
+            return true;
229
+        }
230
+    }
231 231
 
232
-	/**
233
-	 * {@inheritdoc}
234
-	 */
235
-	public function stat($path) {
236
-		$info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']);
237
-		return [
238
-			'mtime' => $info['timestamp'],
239
-			'size' => $info['size']
240
-		];
241
-	}
232
+    /**
233
+     * {@inheritdoc}
234
+     */
235
+    public function stat($path) {
236
+        $info = $this->flysystem->getWithMetadata($this->buildPath($path), ['timestamp', 'size']);
237
+        return [
238
+            'mtime' => $info['timestamp'],
239
+            'size' => $info['size']
240
+        ];
241
+    }
242 242
 
243
-	/**
244
-	 * {@inheritdoc}
245
-	 */
246
-	public function filetype($path) {
247
-		if ($path === '' or $path === '/' or $path === '.') {
248
-			return 'dir';
249
-		}
250
-		try {
251
-			$info = $this->flysystem->getMetadata($this->buildPath($path));
252
-		} catch (FileNotFoundException $e) {
253
-			return false;
254
-		}
255
-		return $info['type'];
256
-	}
243
+    /**
244
+     * {@inheritdoc}
245
+     */
246
+    public function filetype($path) {
247
+        if ($path === '' or $path === '/' or $path === '.') {
248
+            return 'dir';
249
+        }
250
+        try {
251
+            $info = $this->flysystem->getMetadata($this->buildPath($path));
252
+        } catch (FileNotFoundException $e) {
253
+            return false;
254
+        }
255
+        return $info['type'];
256
+    }
257 257
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 	}
57 57
 
58 58
 	protected function buildPath($path) {
59
-		$fullPath = \OC\Files\Filesystem::normalizePath($this->root . '/' . $path);
59
+		$fullPath = \OC\Files\Filesystem::normalizePath($this->root.'/'.$path);
60 60
 		return ltrim($fullPath, '/');
61 61
 	}
62 62
 
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 		} catch (FileNotFoundException $e) {
164 164
 			return false;
165 165
 		}
166
-		$names = array_map(function ($object) {
166
+		$names = array_map(function($object) {
167 167
 			return $object['basename'];
168 168
 		}, $content);
169 169
 		return IteratorDirectory::wrap($names);
@@ -209,7 +209,7 @@  discard block
 block discarded – undo
209 209
 					$tmpFile = \OCP\Files::tmpFile();
210 210
 				}
211 211
 				$source = fopen($tmpFile, $mode);
212
-				return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath) {
212
+				return CallbackWrapper::wrap($source, null, null, function() use ($tmpFile, $fullPath) {
213 213
 					$this->flysystem->putStream($fullPath, fopen($tmpFile, 'r'));
214 214
 					unlink($tmpFile);
215 215
 				});
Please login to merge, or discard this patch.
lib/private/Files/Storage/Wrapper/Quota.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -159,7 +159,7 @@
 block discarded – undo
159 159
 	 * Checks whether the given path is a part file
160 160
 	 *
161 161
 	 * @param string $path Path that may identify a .part file
162
-	 * @return string File path without .part extension
162
+	 * @return boolean File path without .part extension
163 163
 	 * @note this is needed for reusing keys
164 164
 	 */
165 165
 	private function isPartFile($path) {
Please login to merge, or discard this patch.
Indentation   +168 added lines, -168 removed lines patch added patch discarded remove patch
@@ -31,172 +31,172 @@
 block discarded – undo
31 31
 
32 32
 class Quota extends Wrapper {
33 33
 
34
-	/**
35
-	 * @var int $quota
36
-	 */
37
-	protected $quota;
38
-
39
-	/**
40
-	 * @var string $sizeRoot
41
-	 */
42
-	protected $sizeRoot;
43
-
44
-	/**
45
-	 * @param array $parameters
46
-	 */
47
-	public function __construct($parameters) {
48
-		$this->storage = $parameters['storage'];
49
-		$this->quota = $parameters['quota'];
50
-		$this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
51
-	}
52
-
53
-	/**
54
-	 * @return int quota value
55
-	 */
56
-	public function getQuota() {
57
-		return $this->quota;
58
-	}
59
-
60
-	/**
61
-	 * @param string $path
62
-	 * @param \OC\Files\Storage\Storage $storage
63
-	 */
64
-	protected function getSize($path, $storage = null) {
65
-		if (is_null($storage)) {
66
-			$cache = $this->getCache();
67
-		} else {
68
-			$cache = $storage->getCache();
69
-		}
70
-		$data = $cache->get($path);
71
-		if ($data instanceof ICacheEntry and isset($data['size'])) {
72
-			return $data['size'];
73
-		} else {
74
-			return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
75
-		}
76
-	}
77
-
78
-	/**
79
-	 * Get free space as limited by the quota
80
-	 *
81
-	 * @param string $path
82
-	 * @return int
83
-	 */
84
-	public function free_space($path) {
85
-		if ($this->quota < 0) {
86
-			return $this->storage->free_space($path);
87
-		} else {
88
-			$used = $this->getSize($this->sizeRoot);
89
-			if ($used < 0) {
90
-				return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
91
-			} else {
92
-				$free = $this->storage->free_space($path);
93
-				$quotaFree = max($this->quota - $used, 0);
94
-				// if free space is known
95
-				if ($free >= 0) {
96
-					$free = min($free, $quotaFree);
97
-				} else {
98
-					$free = $quotaFree;
99
-				}
100
-				return $free;
101
-			}
102
-		}
103
-	}
104
-
105
-	/**
106
-	 * see http://php.net/manual/en/function.file_put_contents.php
107
-	 *
108
-	 * @param string $path
109
-	 * @param string $data
110
-	 * @return bool
111
-	 */
112
-	public function file_put_contents($path, $data) {
113
-		$free = $this->free_space('');
114
-		if ($free < 0 or strlen($data) < $free) {
115
-			return $this->storage->file_put_contents($path, $data);
116
-		} else {
117
-			return false;
118
-		}
119
-	}
120
-
121
-	/**
122
-	 * see http://php.net/manual/en/function.copy.php
123
-	 *
124
-	 * @param string $source
125
-	 * @param string $target
126
-	 * @return bool
127
-	 */
128
-	public function copy($source, $target) {
129
-		$free = $this->free_space('');
130
-		if ($free < 0 or $this->getSize($source) < $free) {
131
-			return $this->storage->copy($source, $target);
132
-		} else {
133
-			return false;
134
-		}
135
-	}
136
-
137
-	/**
138
-	 * see http://php.net/manual/en/function.fopen.php
139
-	 *
140
-	 * @param string $path
141
-	 * @param string $mode
142
-	 * @return resource
143
-	 */
144
-	public function fopen($path, $mode) {
145
-		$source = $this->storage->fopen($path, $mode);
146
-
147
-		// don't apply quota for part files
148
-		if (!$this->isPartFile($path)) {
149
-			$free = $this->free_space('');
150
-			if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
151
-				// only apply quota for files, not metadata, trash or others
152
-				if (strpos(ltrim($path, '/'), 'files/') === 0) {
153
-					return \OC\Files\Stream\Quota::wrap($source, $free);
154
-				}
155
-			}
156
-		}
157
-		return $source;
158
-	}
159
-
160
-	/**
161
-	 * Checks whether the given path is a part file
162
-	 *
163
-	 * @param string $path Path that may identify a .part file
164
-	 * @return string File path without .part extension
165
-	 * @note this is needed for reusing keys
166
-	 */
167
-	private function isPartFile($path) {
168
-		$extension = pathinfo($path, PATHINFO_EXTENSION);
169
-
170
-		return ($extension === 'part');
171
-	}
172
-
173
-	/**
174
-	 * @param IStorage $sourceStorage
175
-	 * @param string $sourceInternalPath
176
-	 * @param string $targetInternalPath
177
-	 * @return bool
178
-	 */
179
-	public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
180
-		$free = $this->free_space('');
181
-		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
182
-			return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
183
-		} else {
184
-			return false;
185
-		}
186
-	}
187
-
188
-	/**
189
-	 * @param IStorage $sourceStorage
190
-	 * @param string $sourceInternalPath
191
-	 * @param string $targetInternalPath
192
-	 * @return bool
193
-	 */
194
-	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
195
-		$free = $this->free_space('');
196
-		if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
197
-			return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
198
-		} else {
199
-			return false;
200
-		}
201
-	}
34
+    /**
35
+     * @var int $quota
36
+     */
37
+    protected $quota;
38
+
39
+    /**
40
+     * @var string $sizeRoot
41
+     */
42
+    protected $sizeRoot;
43
+
44
+    /**
45
+     * @param array $parameters
46
+     */
47
+    public function __construct($parameters) {
48
+        $this->storage = $parameters['storage'];
49
+        $this->quota = $parameters['quota'];
50
+        $this->sizeRoot = isset($parameters['root']) ? $parameters['root'] : '';
51
+    }
52
+
53
+    /**
54
+     * @return int quota value
55
+     */
56
+    public function getQuota() {
57
+        return $this->quota;
58
+    }
59
+
60
+    /**
61
+     * @param string $path
62
+     * @param \OC\Files\Storage\Storage $storage
63
+     */
64
+    protected function getSize($path, $storage = null) {
65
+        if (is_null($storage)) {
66
+            $cache = $this->getCache();
67
+        } else {
68
+            $cache = $storage->getCache();
69
+        }
70
+        $data = $cache->get($path);
71
+        if ($data instanceof ICacheEntry and isset($data['size'])) {
72
+            return $data['size'];
73
+        } else {
74
+            return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
75
+        }
76
+    }
77
+
78
+    /**
79
+     * Get free space as limited by the quota
80
+     *
81
+     * @param string $path
82
+     * @return int
83
+     */
84
+    public function free_space($path) {
85
+        if ($this->quota < 0) {
86
+            return $this->storage->free_space($path);
87
+        } else {
88
+            $used = $this->getSize($this->sizeRoot);
89
+            if ($used < 0) {
90
+                return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED;
91
+            } else {
92
+                $free = $this->storage->free_space($path);
93
+                $quotaFree = max($this->quota - $used, 0);
94
+                // if free space is known
95
+                if ($free >= 0) {
96
+                    $free = min($free, $quotaFree);
97
+                } else {
98
+                    $free = $quotaFree;
99
+                }
100
+                return $free;
101
+            }
102
+        }
103
+    }
104
+
105
+    /**
106
+     * see http://php.net/manual/en/function.file_put_contents.php
107
+     *
108
+     * @param string $path
109
+     * @param string $data
110
+     * @return bool
111
+     */
112
+    public function file_put_contents($path, $data) {
113
+        $free = $this->free_space('');
114
+        if ($free < 0 or strlen($data) < $free) {
115
+            return $this->storage->file_put_contents($path, $data);
116
+        } else {
117
+            return false;
118
+        }
119
+    }
120
+
121
+    /**
122
+     * see http://php.net/manual/en/function.copy.php
123
+     *
124
+     * @param string $source
125
+     * @param string $target
126
+     * @return bool
127
+     */
128
+    public function copy($source, $target) {
129
+        $free = $this->free_space('');
130
+        if ($free < 0 or $this->getSize($source) < $free) {
131
+            return $this->storage->copy($source, $target);
132
+        } else {
133
+            return false;
134
+        }
135
+    }
136
+
137
+    /**
138
+     * see http://php.net/manual/en/function.fopen.php
139
+     *
140
+     * @param string $path
141
+     * @param string $mode
142
+     * @return resource
143
+     */
144
+    public function fopen($path, $mode) {
145
+        $source = $this->storage->fopen($path, $mode);
146
+
147
+        // don't apply quota for part files
148
+        if (!$this->isPartFile($path)) {
149
+            $free = $this->free_space('');
150
+            if ($source && $free >= 0 && $mode !== 'r' && $mode !== 'rb') {
151
+                // only apply quota for files, not metadata, trash or others
152
+                if (strpos(ltrim($path, '/'), 'files/') === 0) {
153
+                    return \OC\Files\Stream\Quota::wrap($source, $free);
154
+                }
155
+            }
156
+        }
157
+        return $source;
158
+    }
159
+
160
+    /**
161
+     * Checks whether the given path is a part file
162
+     *
163
+     * @param string $path Path that may identify a .part file
164
+     * @return string File path without .part extension
165
+     * @note this is needed for reusing keys
166
+     */
167
+    private function isPartFile($path) {
168
+        $extension = pathinfo($path, PATHINFO_EXTENSION);
169
+
170
+        return ($extension === 'part');
171
+    }
172
+
173
+    /**
174
+     * @param IStorage $sourceStorage
175
+     * @param string $sourceInternalPath
176
+     * @param string $targetInternalPath
177
+     * @return bool
178
+     */
179
+    public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
180
+        $free = $this->free_space('');
181
+        if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
182
+            return $this->storage->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
183
+        } else {
184
+            return false;
185
+        }
186
+    }
187
+
188
+    /**
189
+     * @param IStorage $sourceStorage
190
+     * @param string $sourceInternalPath
191
+     * @param string $targetInternalPath
192
+     * @return bool
193
+     */
194
+    public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
195
+        $free = $this->free_space('');
196
+        if ($free < 0 or $this->getSize($sourceInternalPath, $sourceStorage) < $free) {
197
+            return $this->storage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
198
+        } else {
199
+            return false;
200
+        }
201
+    }
202 202
 }
Please login to merge, or discard this patch.
lib/private/L10N/L10N.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -176,7 +176,7 @@
 block discarded – undo
176 176
 	 * Returns an associative array with all translations
177 177
 	 *
178 178
 	 * Called by \OC_L10N_String
179
-	 * @return array
179
+	 * @return string[]
180 180
 	 */
181 181
 	public function getTranslations() {
182 182
 		return $this->translations;
Please login to merge, or discard this patch.
Indentation   +192 added lines, -192 removed lines patch added patch discarded remove patch
@@ -29,196 +29,196 @@
 block discarded – undo
29 29
 
30 30
 class L10N implements IL10N {
31 31
 
32
-	/** @var IFactory */
33
-	protected $factory;
34
-
35
-	/** @var string App of this object */
36
-	protected $app;
37
-
38
-	/** @var string Language of this object */
39
-	protected $lang;
40
-
41
-	/** @var string Plural forms (string) */
42
-	private $pluralFormString = 'nplurals=2; plural=(n != 1);';
43
-
44
-	/** @var string Plural forms (function) */
45
-	private $pluralFormFunction = null;
46
-
47
-	/** @var string[] */
48
-	private $translations = [];
49
-
50
-	/**
51
-	 * @param IFactory $factory
52
-	 * @param string $app
53
-	 * @param string $lang
54
-	 * @param array $files
55
-	 */
56
-	public function __construct(IFactory $factory, $app, $lang, array $files) {
57
-		$this->factory = $factory;
58
-		$this->app = $app;
59
-		$this->lang = $lang;
60
-
61
-		$this->translations = [];
62
-		foreach ($files as $languageFile) {
63
-			$this->load($languageFile);
64
-		}
65
-	}
66
-
67
-	/**
68
-	 * The code (en, de, ...) of the language that is used for this instance
69
-	 *
70
-	 * @return string language
71
-	 */
72
-	public function getLanguageCode() {
73
-		return $this->lang;
74
-	}
75
-
76
-	/**
77
-	 * Translating
78
-	 * @param string $text The text we need a translation for
79
-	 * @param array $parameters default:array() Parameters for sprintf
80
-	 * @return string Translation or the same text
81
-	 *
82
-	 * Returns the translation. If no translation is found, $text will be
83
-	 * returned.
84
-	 */
85
-	public function t($text, $parameters = array()) {
86
-		return (string) new L10NString($this, $text, $parameters);
87
-	}
88
-
89
-	/**
90
-	 * Translating
91
-	 * @param string $text_singular the string to translate for exactly one object
92
-	 * @param string $text_plural the string to translate for n objects
93
-	 * @param integer $count Number of objects
94
-	 * @param array $parameters default:array() Parameters for sprintf
95
-	 * @return string Translation or the same text
96
-	 *
97
-	 * Returns the translation. If no translation is found, $text will be
98
-	 * returned. %n will be replaced with the number of objects.
99
-	 *
100
-	 * The correct plural is determined by the plural_forms-function
101
-	 * provided by the po file.
102
-	 *
103
-	 */
104
-	public function n($text_singular, $text_plural, $count, $parameters = array()) {
105
-		$identifier = "_${text_singular}_::_${text_plural}_";
106
-		if (isset($this->translations[$identifier])) {
107
-			return (string) new L10NString($this, $identifier, $parameters, $count);
108
-		} else {
109
-			if ($count === 1) {
110
-				return (string) new L10NString($this, $text_singular, $parameters, $count);
111
-			} else {
112
-				return (string) new L10NString($this, $text_plural, $parameters, $count);
113
-			}
114
-		}
115
-	}
116
-
117
-	/**
118
-	 * Localization
119
-	 * @param string $type Type of localization
120
-	 * @param \DateTime|int|string $data parameters for this localization
121
-	 * @param array $options
122
-	 * @return string|int|false
123
-	 *
124
-	 * Returns the localized data.
125
-	 *
126
-	 * Implemented types:
127
-	 *  - date
128
-	 *    - Creates a date
129
-	 *    - params: timestamp (int/string)
130
-	 *  - datetime
131
-	 *    - Creates date and time
132
-	 *    - params: timestamp (int/string)
133
-	 *  - time
134
-	 *    - Creates a time
135
-	 *    - params: timestamp (int/string)
136
-	 *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
137
-	 *  - jsdate: Returns the short JS date format
138
-	 */
139
-	public function l($type, $data = null, $options = array()) {
140
-		// Use the language of the instance
141
-		$locale = $this->getLanguageCode();
142
-		if ($locale === 'sr@latin') {
143
-			$locale = 'sr_latn';
144
-		}
145
-
146
-		if ($type === 'firstday') {
147
-			return (int) Calendar::getFirstWeekday($locale);
148
-		}
149
-		if ($type === 'jsdate') {
150
-			return (string) Calendar::getDateFormat('short', $locale);
151
-		}
152
-
153
-		$value = new \DateTime();
154
-		if ($data instanceof \DateTime) {
155
-			$value = $data;
156
-		} else if (is_string($data) && !is_numeric($data)) {
157
-			$data = strtotime($data);
158
-			$value->setTimestamp($data);
159
-		} else if ($data !== null) {
160
-			$value->setTimestamp($data);
161
-		}
162
-
163
-		$options = array_merge(array('width' => 'long'), $options);
164
-		$width = $options['width'];
165
-		switch ($type) {
166
-			case 'date':
167
-				return (string) Calendar::formatDate($value, $width, $locale);
168
-			case 'datetime':
169
-				return (string) Calendar::formatDatetime($value, $width, $locale);
170
-			case 'time':
171
-				return (string) Calendar::formatTime($value, $width, $locale);
172
-			case 'weekdayName':
173
-				return (string) Calendar::getWeekdayName($value, $width, $locale);
174
-			default:
175
-				return false;
176
-		}
177
-	}
178
-
179
-	/**
180
-	 * Returns an associative array with all translations
181
-	 *
182
-	 * Called by \OC_L10N_String
183
-	 * @return array
184
-	 */
185
-	public function getTranslations() {
186
-		return $this->translations;
187
-	}
188
-
189
-	/**
190
-	 * Returnsed function accepts the argument $n
191
-	 *
192
-	 * Called by \OC_L10N_String
193
-	 * @return string the plural form function
194
-	 */
195
-	public function getPluralFormFunction() {
196
-		if (is_null($this->pluralFormFunction)) {
197
-			$lang = $this->getLanguageCode();
198
-			$this->pluralFormFunction = function($n) use ($lang) {
199
-				return PluralizationRules::get($n, $lang);
200
-			};
201
-		}
202
-
203
-		return $this->pluralFormFunction;
204
-	}
205
-
206
-	/**
207
-	 * @param $translationFile
208
-	 * @return bool
209
-	 */
210
-	protected function load($translationFile) {
211
-		$json = json_decode(file_get_contents($translationFile), true);
212
-		if (!is_array($json)) {
213
-			$jsonError = json_last_error();
214
-			\OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
215
-			return false;
216
-		}
217
-
218
-		if (!empty($json['pluralForm'])) {
219
-			$this->pluralFormString = $json['pluralForm'];
220
-		}
221
-		$this->translations = array_merge($this->translations, $json['translations']);
222
-		return true;
223
-	}
32
+    /** @var IFactory */
33
+    protected $factory;
34
+
35
+    /** @var string App of this object */
36
+    protected $app;
37
+
38
+    /** @var string Language of this object */
39
+    protected $lang;
40
+
41
+    /** @var string Plural forms (string) */
42
+    private $pluralFormString = 'nplurals=2; plural=(n != 1);';
43
+
44
+    /** @var string Plural forms (function) */
45
+    private $pluralFormFunction = null;
46
+
47
+    /** @var string[] */
48
+    private $translations = [];
49
+
50
+    /**
51
+     * @param IFactory $factory
52
+     * @param string $app
53
+     * @param string $lang
54
+     * @param array $files
55
+     */
56
+    public function __construct(IFactory $factory, $app, $lang, array $files) {
57
+        $this->factory = $factory;
58
+        $this->app = $app;
59
+        $this->lang = $lang;
60
+
61
+        $this->translations = [];
62
+        foreach ($files as $languageFile) {
63
+            $this->load($languageFile);
64
+        }
65
+    }
66
+
67
+    /**
68
+     * The code (en, de, ...) of the language that is used for this instance
69
+     *
70
+     * @return string language
71
+     */
72
+    public function getLanguageCode() {
73
+        return $this->lang;
74
+    }
75
+
76
+    /**
77
+     * Translating
78
+     * @param string $text The text we need a translation for
79
+     * @param array $parameters default:array() Parameters for sprintf
80
+     * @return string Translation or the same text
81
+     *
82
+     * Returns the translation. If no translation is found, $text will be
83
+     * returned.
84
+     */
85
+    public function t($text, $parameters = array()) {
86
+        return (string) new L10NString($this, $text, $parameters);
87
+    }
88
+
89
+    /**
90
+     * Translating
91
+     * @param string $text_singular the string to translate for exactly one object
92
+     * @param string $text_plural the string to translate for n objects
93
+     * @param integer $count Number of objects
94
+     * @param array $parameters default:array() Parameters for sprintf
95
+     * @return string Translation or the same text
96
+     *
97
+     * Returns the translation. If no translation is found, $text will be
98
+     * returned. %n will be replaced with the number of objects.
99
+     *
100
+     * The correct plural is determined by the plural_forms-function
101
+     * provided by the po file.
102
+     *
103
+     */
104
+    public function n($text_singular, $text_plural, $count, $parameters = array()) {
105
+        $identifier = "_${text_singular}_::_${text_plural}_";
106
+        if (isset($this->translations[$identifier])) {
107
+            return (string) new L10NString($this, $identifier, $parameters, $count);
108
+        } else {
109
+            if ($count === 1) {
110
+                return (string) new L10NString($this, $text_singular, $parameters, $count);
111
+            } else {
112
+                return (string) new L10NString($this, $text_plural, $parameters, $count);
113
+            }
114
+        }
115
+    }
116
+
117
+    /**
118
+     * Localization
119
+     * @param string $type Type of localization
120
+     * @param \DateTime|int|string $data parameters for this localization
121
+     * @param array $options
122
+     * @return string|int|false
123
+     *
124
+     * Returns the localized data.
125
+     *
126
+     * Implemented types:
127
+     *  - date
128
+     *    - Creates a date
129
+     *    - params: timestamp (int/string)
130
+     *  - datetime
131
+     *    - Creates date and time
132
+     *    - params: timestamp (int/string)
133
+     *  - time
134
+     *    - Creates a time
135
+     *    - params: timestamp (int/string)
136
+     *  - firstday: Returns the first day of the week (0 sunday - 6 saturday)
137
+     *  - jsdate: Returns the short JS date format
138
+     */
139
+    public function l($type, $data = null, $options = array()) {
140
+        // Use the language of the instance
141
+        $locale = $this->getLanguageCode();
142
+        if ($locale === 'sr@latin') {
143
+            $locale = 'sr_latn';
144
+        }
145
+
146
+        if ($type === 'firstday') {
147
+            return (int) Calendar::getFirstWeekday($locale);
148
+        }
149
+        if ($type === 'jsdate') {
150
+            return (string) Calendar::getDateFormat('short', $locale);
151
+        }
152
+
153
+        $value = new \DateTime();
154
+        if ($data instanceof \DateTime) {
155
+            $value = $data;
156
+        } else if (is_string($data) && !is_numeric($data)) {
157
+            $data = strtotime($data);
158
+            $value->setTimestamp($data);
159
+        } else if ($data !== null) {
160
+            $value->setTimestamp($data);
161
+        }
162
+
163
+        $options = array_merge(array('width' => 'long'), $options);
164
+        $width = $options['width'];
165
+        switch ($type) {
166
+            case 'date':
167
+                return (string) Calendar::formatDate($value, $width, $locale);
168
+            case 'datetime':
169
+                return (string) Calendar::formatDatetime($value, $width, $locale);
170
+            case 'time':
171
+                return (string) Calendar::formatTime($value, $width, $locale);
172
+            case 'weekdayName':
173
+                return (string) Calendar::getWeekdayName($value, $width, $locale);
174
+            default:
175
+                return false;
176
+        }
177
+    }
178
+
179
+    /**
180
+     * Returns an associative array with all translations
181
+     *
182
+     * Called by \OC_L10N_String
183
+     * @return array
184
+     */
185
+    public function getTranslations() {
186
+        return $this->translations;
187
+    }
188
+
189
+    /**
190
+     * Returnsed function accepts the argument $n
191
+     *
192
+     * Called by \OC_L10N_String
193
+     * @return string the plural form function
194
+     */
195
+    public function getPluralFormFunction() {
196
+        if (is_null($this->pluralFormFunction)) {
197
+            $lang = $this->getLanguageCode();
198
+            $this->pluralFormFunction = function($n) use ($lang) {
199
+                return PluralizationRules::get($n, $lang);
200
+            };
201
+        }
202
+
203
+        return $this->pluralFormFunction;
204
+    }
205
+
206
+    /**
207
+     * @param $translationFile
208
+     * @return bool
209
+     */
210
+    protected function load($translationFile) {
211
+        $json = json_decode(file_get_contents($translationFile), true);
212
+        if (!is_array($json)) {
213
+            $jsonError = json_last_error();
214
+            \OC::$server->getLogger()->warning("Failed to load $translationFile - json error code: $jsonError", ['app' => 'l10n']);
215
+            return false;
216
+        }
217
+
218
+        if (!empty($json['pluralForm'])) {
219
+            $this->pluralFormString = $json['pluralForm'];
220
+        }
221
+        $this->translations = array_merge($this->translations, $json['translations']);
222
+        return true;
223
+    }
224 224
 }
Please login to merge, or discard this patch.
lib/private/legacy/api.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -331,7 +331,7 @@
 block discarded – undo
331 331
 
332 332
 	/**
333 333
 	 * http basic auth
334
-	 * @return string|false (username, or false on failure)
334
+	 * @return string (username, or false on failure)
335 335
 	 */
336 336
 	private static function loginUser() {
337 337
 		if(self::$isLoggedIn === true) {
Please login to merge, or discard this patch.
Indentation   +459 added lines, -459 removed lines patch added patch discarded remove patch
@@ -37,463 +37,463 @@
 block discarded – undo
37 37
 
38 38
 class OC_API {
39 39
 
40
-	/**
41
-	 * API authentication levels
42
-	 */
43
-
44
-	/** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
-	const GUEST_AUTH = 0;
46
-
47
-	/** @deprecated Use \OCP\API::USER_AUTH instead */
48
-	const USER_AUTH = 1;
49
-
50
-	/** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
-	const SUBADMIN_AUTH = 2;
52
-
53
-	/** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
-	const ADMIN_AUTH = 3;
55
-
56
-	/**
57
-	 * API Response Codes
58
-	 */
59
-
60
-	/** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
-	const RESPOND_UNAUTHORISED = 997;
62
-
63
-	/** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
-	const RESPOND_SERVER_ERROR = 996;
65
-
66
-	/** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
-	const RESPOND_NOT_FOUND = 998;
68
-
69
-	/** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
-	const RESPOND_UNKNOWN_ERROR = 999;
71
-
72
-	/**
73
-	 * api actions
74
-	 */
75
-	protected static $actions = array();
76
-	private static $logoutRequired = false;
77
-	private static $isLoggedIn = false;
78
-
79
-	/**
80
-	 * registers an api call
81
-	 * @param string $method the http method
82
-	 * @param string $url the url to match
83
-	 * @param callable $action the function to run
84
-	 * @param string $app the id of the app registering the call
85
-	 * @param int $authLevel the level of authentication required for the call
86
-	 * @param array $defaults
87
-	 * @param array $requirements
88
-	 */
89
-	public static function register($method, $url, $action, $app,
90
-				$authLevel = API::USER_AUTH,
91
-				$defaults = array(),
92
-				$requirements = array()) {
93
-		$name = strtolower($method).$url;
94
-		$name = str_replace(array('/', '{', '}'), '_', $name);
95
-		if(!isset(self::$actions[$name])) {
96
-			$oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
-			OC::$server->getRouter()->useCollection('ocs');
98
-			OC::$server->getRouter()->create($name, $url)
99
-				->method($method)
100
-				->defaults($defaults)
101
-				->requirements($requirements)
102
-				->action('OC_API', 'call');
103
-			self::$actions[$name] = array();
104
-			OC::$server->getRouter()->useCollection($oldCollection);
105
-		}
106
-		self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
-	}
108
-
109
-	/**
110
-	 * handles an api call
111
-	 * @param array $parameters
112
-	 */
113
-	public static function call($parameters) {
114
-		$request = \OC::$server->getRequest();
115
-		$method = $request->getMethod();
116
-
117
-		// Prepare the request variables
118
-		if($method === 'PUT') {
119
-			$parameters['_put'] = $request->getParams();
120
-		} else if($method === 'DELETE') {
121
-			$parameters['_delete'] = $request->getParams();
122
-		}
123
-		$name = $parameters['_route'];
124
-		// Foreach registered action
125
-		$responses = array();
126
-		$appManager = \OC::$server->getAppManager();
127
-		foreach(self::$actions[$name] as $action) {
128
-			// Check authentication and availability
129
-			if(!self::isAuthorised($action)) {
130
-				$responses[] = array(
131
-					'app' => $action['app'],
132
-					'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
133
-					'shipped' => $appManager->isShipped($action['app']),
134
-					);
135
-				continue;
136
-			}
137
-			if(!is_callable($action['action'])) {
138
-				$responses[] = array(
139
-					'app' => $action['app'],
140
-					'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
141
-					'shipped' => $appManager->isShipped($action['app']),
142
-					);
143
-				continue;
144
-			}
145
-			// Run the action
146
-			$responses[] = array(
147
-				'app' => $action['app'],
148
-				'response' => call_user_func($action['action'], $parameters),
149
-				'shipped' => $appManager->isShipped($action['app']),
150
-				);
151
-		}
152
-		$response = self::mergeResponses($responses);
153
-		$format = self::requestedFormat();
154
-		if (self::$logoutRequired) {
155
-			\OC::$server->getUserSession()->logout();
156
-		}
157
-
158
-		self::respond($response, $format);
159
-	}
160
-
161
-	/**
162
-	 * merge the returned result objects into one response
163
-	 * @param array $responses
164
-	 * @return \OC\OCS\Result
165
-	 */
166
-	public static function mergeResponses($responses) {
167
-		// Sort into shipped and third-party
168
-		$shipped = array(
169
-			'succeeded' => array(),
170
-			'failed' => array(),
171
-			);
172
-		$thirdparty = array(
173
-			'succeeded' => array(),
174
-			'failed' => array(),
175
-			);
176
-
177
-		foreach($responses as $response) {
178
-			if($response['shipped'] || ($response['app'] === 'core')) {
179
-				if($response['response']->succeeded()) {
180
-					$shipped['succeeded'][$response['app']] = $response;
181
-				} else {
182
-					$shipped['failed'][$response['app']] = $response;
183
-				}
184
-			} else {
185
-				if($response['response']->succeeded()) {
186
-					$thirdparty['succeeded'][$response['app']] = $response;
187
-				} else {
188
-					$thirdparty['failed'][$response['app']] = $response;
189
-				}
190
-			}
191
-		}
192
-
193
-		// Remove any error responses if there is one shipped response that succeeded
194
-		if(!empty($shipped['failed'])) {
195
-			// Which shipped response do we use if they all failed?
196
-			// They may have failed for different reasons (different status codes)
197
-			// Which response code should we return?
198
-			// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199
-			// Merge failed responses if more than one
200
-			$data = array();
201
-			foreach($shipped['failed'] as $failure) {
202
-				$data = array_merge_recursive($data, $failure['response']->getData());
203
-			}
204
-			$picked = reset($shipped['failed']);
205
-			$code = $picked['response']->getStatusCode();
206
-			$meta = $picked['response']->getMeta();
207
-			$headers = $picked['response']->getHeaders();
208
-			$response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
209
-			return $response;
210
-		} elseif(!empty($shipped['succeeded'])) {
211
-			$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
-		} elseif(!empty($thirdparty['failed'])) {
213
-			// Merge failed responses if more than one
214
-			$data = array();
215
-			foreach($thirdparty['failed'] as $failure) {
216
-				$data = array_merge_recursive($data, $failure['response']->getData());
217
-			}
218
-			$picked = reset($thirdparty['failed']);
219
-			$code = $picked['response']->getStatusCode();
220
-			$meta = $picked['response']->getMeta();
221
-			$headers = $picked['response']->getHeaders();
222
-			$response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
223
-			return $response;
224
-		} else {
225
-			$responses = $thirdparty['succeeded'];
226
-		}
227
-		// Merge the successful responses
228
-		$data = [];
229
-		$codes = [];
230
-		$header = [];
231
-
232
-		foreach($responses as $response) {
233
-			if($response['shipped']) {
234
-				$data = array_merge_recursive($response['response']->getData(), $data);
235
-			} else {
236
-				$data = array_merge_recursive($data, $response['response']->getData());
237
-			}
238
-			$header = array_merge_recursive($header, $response['response']->getHeaders());
239
-			$codes[] = ['code' => $response['response']->getStatusCode(),
240
-				'meta' => $response['response']->getMeta()];
241
-		}
242
-
243
-		// Use any non 100 status codes
244
-		$statusCode = 100;
245
-		$statusMessage = null;
246
-		foreach($codes as $code) {
247
-			if($code['code'] != 100) {
248
-				$statusCode = $code['code'];
249
-				$statusMessage = $code['meta']['message'];
250
-				break;
251
-			}
252
-		}
253
-
254
-		return new \OC\OCS\Result($data, $statusCode, $statusMessage, $header);
255
-	}
256
-
257
-	/**
258
-	 * authenticate the api call
259
-	 * @param array $action the action details as supplied to OC_API::register()
260
-	 * @return bool
261
-	 */
262
-	private static function isAuthorised($action) {
263
-		$level = $action['authlevel'];
264
-		switch($level) {
265
-			case API::GUEST_AUTH:
266
-				// Anyone can access
267
-				return true;
268
-			case API::USER_AUTH:
269
-				// User required
270
-				return self::loginUser();
271
-			case API::SUBADMIN_AUTH:
272
-				// Check for subadmin
273
-				$user = self::loginUser();
274
-				if(!$user) {
275
-					return false;
276
-				} else {
277
-					$userObject = \OC::$server->getUserSession()->getUser();
278
-					if($userObject === null) {
279
-						return false;
280
-					}
281
-					$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282
-					$admin = OC_User::isAdminUser($user);
283
-					if($isSubAdmin || $admin) {
284
-						return true;
285
-					} else {
286
-						return false;
287
-					}
288
-				}
289
-			case API::ADMIN_AUTH:
290
-				// Check for admin
291
-				$user = self::loginUser();
292
-				if(!$user) {
293
-					return false;
294
-				} else {
295
-					return OC_User::isAdminUser($user);
296
-				}
297
-			default:
298
-				// oops looks like invalid level supplied
299
-				return false;
300
-		}
301
-	}
302
-
303
-	/**
304
-	 * http basic auth
305
-	 * @return string|false (username, or false on failure)
306
-	 */
307
-	private static function loginUser() {
308
-		if(self::$isLoggedIn === true) {
309
-			return \OC_User::getUser();
310
-		}
311
-
312
-		// reuse existing login
313
-		$loggedIn = \OC::$server->getUserSession()->isLoggedIn();
314
-		if ($loggedIn === true) {
315
-			if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
316
-				// Do not allow access to OCS until the 2FA challenge was solved successfully
317
-				return false;
318
-			}
319
-			$ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
320
-			if ($ocsApiRequest) {
321
-
322
-				// initialize the user's filesystem
323
-				\OC_Util::setupFS(\OC_User::getUser());
324
-				self::$isLoggedIn = true;
325
-
326
-				return OC_User::getUser();
327
-			}
328
-			return false;
329
-		}
330
-
331
-		// basic auth - because OC_User::login will create a new session we shall only try to login
332
-		// if user and pass are set
333
-		$userSession = \OC::$server->getUserSession();
334
-		$request = \OC::$server->getRequest();
335
-		try {
336
-			if ($userSession->tryTokenLogin($request)
337
-				|| $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
338
-				self::$logoutRequired = true;
339
-			} else {
340
-				return false;
341
-			}
342
-			// initialize the user's filesystem
343
-			\OC_Util::setupFS(\OC_User::getUser());
344
-			self::$isLoggedIn = true;
345
-
346
-			return \OC_User::getUser();
347
-		} catch (\OC\User\LoginException $e) {
348
-			return false;
349
-		}
350
-	}
351
-
352
-	/**
353
-	 * respond to a call
354
-	 * @param \OC\OCS\Result $result
355
-	 * @param string $format the format xml|json
356
-	 */
357
-	public static function respond($result, $format='xml') {
358
-		$request = \OC::$server->getRequest();
359
-
360
-		// Send 401 headers if unauthorised
361
-		if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362
-			// If request comes from JS return dummy auth request
363
-			if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364
-				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365
-			} else {
366
-				header('WWW-Authenticate: Basic realm="Authorisation Required"');
367
-			}
368
-			header('HTTP/1.0 401 Unauthorized');
369
-		}
370
-
371
-		foreach($result->getHeaders() as $name => $value) {
372
-			header($name . ': ' . $value);
373
-		}
374
-
375
-		$meta = $result->getMeta();
376
-		$data = $result->getData();
377
-		if (self::isV2($request)) {
378
-			$statusCode = self::mapStatusCodes($result->getStatusCode());
379
-			if (!is_null($statusCode)) {
380
-				$meta['statuscode'] = $statusCode;
381
-				OC_Response::setStatus($statusCode);
382
-			}
383
-		}
384
-
385
-		self::setContentType($format);
386
-		$body = self::renderResult($format, $meta, $data);
387
-		echo $body;
388
-	}
389
-
390
-	/**
391
-	 * @param XMLWriter $writer
392
-	 */
393
-	private static function toXML($array, $writer) {
394
-		foreach($array as $k => $v) {
395
-			if ($k[0] === '@') {
396
-				$writer->writeAttribute(substr($k, 1), $v);
397
-				continue;
398
-			} else if (is_numeric($k)) {
399
-				$k = 'element';
400
-			}
401
-			if(is_array($v)) {
402
-				$writer->startElement($k);
403
-				self::toXML($v, $writer);
404
-				$writer->endElement();
405
-			} else {
406
-				$writer->writeElement($k, $v);
407
-			}
408
-		}
409
-	}
410
-
411
-	/**
412
-	 * @return string
413
-	 */
414
-	public static function requestedFormat() {
415
-		$formats = array('json', 'xml');
416
-
417
-		$format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
418
-		return $format;
419
-	}
420
-
421
-	/**
422
-	 * Based on the requested format the response content type is set
423
-	 * @param string $format
424
-	 */
425
-	public static function setContentType($format = null) {
426
-		$format = is_null($format) ? self::requestedFormat() : $format;
427
-		if ($format === 'xml') {
428
-			header('Content-type: text/xml; charset=UTF-8');
429
-			return;
430
-		}
431
-
432
-		if ($format === 'json') {
433
-			header('Content-Type: application/json; charset=utf-8');
434
-			return;
435
-		}
436
-
437
-		header('Content-Type: application/octet-stream; charset=utf-8');
438
-	}
439
-
440
-	/**
441
-	 * @param \OCP\IRequest $request
442
-	 * @return bool
443
-	 */
444
-	protected static function isV2(\OCP\IRequest $request) {
445
-		$script = $request->getScriptName();
446
-
447
-		return substr($script, -11) === '/ocs/v2.php';
448
-	}
449
-
450
-	/**
451
-	 * @param integer $sc
452
-	 * @return int
453
-	 */
454
-	public static function mapStatusCodes($sc) {
455
-		switch ($sc) {
456
-			case API::RESPOND_NOT_FOUND:
457
-				return Http::STATUS_NOT_FOUND;
458
-			case API::RESPOND_SERVER_ERROR:
459
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
460
-			case API::RESPOND_UNKNOWN_ERROR:
461
-				return Http::STATUS_INTERNAL_SERVER_ERROR;
462
-			case API::RESPOND_UNAUTHORISED:
463
-				// already handled for v1
464
-				return null;
465
-			case 100:
466
-				return Http::STATUS_OK;
467
-		}
468
-		// any 2xx, 4xx and 5xx will be used as is
469
-		if ($sc >= 200 && $sc < 600) {
470
-			return $sc;
471
-		}
472
-
473
-		return Http::STATUS_BAD_REQUEST;
474
-	}
475
-
476
-	/**
477
-	 * @param string $format
478
-	 * @return string
479
-	 */
480
-	public static function renderResult($format, $meta, $data) {
481
-		$response = array(
482
-			'ocs' => array(
483
-				'meta' => $meta,
484
-				'data' => $data,
485
-			),
486
-		);
487
-		if ($format == 'json') {
488
-			return OC_JSON::encode($response);
489
-		}
490
-
491
-		$writer = new XMLWriter();
492
-		$writer->openMemory();
493
-		$writer->setIndent(true);
494
-		$writer->startDocument();
495
-		self::toXML($response, $writer);
496
-		$writer->endDocument();
497
-		return $writer->outputMemory(true);
498
-	}
40
+    /**
41
+     * API authentication levels
42
+     */
43
+
44
+    /** @deprecated Use \OCP\API::GUEST_AUTH instead */
45
+    const GUEST_AUTH = 0;
46
+
47
+    /** @deprecated Use \OCP\API::USER_AUTH instead */
48
+    const USER_AUTH = 1;
49
+
50
+    /** @deprecated Use \OCP\API::SUBADMIN_AUTH instead */
51
+    const SUBADMIN_AUTH = 2;
52
+
53
+    /** @deprecated Use \OCP\API::ADMIN_AUTH instead */
54
+    const ADMIN_AUTH = 3;
55
+
56
+    /**
57
+     * API Response Codes
58
+     */
59
+
60
+    /** @deprecated Use \OCP\API::RESPOND_UNAUTHORISED instead */
61
+    const RESPOND_UNAUTHORISED = 997;
62
+
63
+    /** @deprecated Use \OCP\API::RESPOND_SERVER_ERROR instead */
64
+    const RESPOND_SERVER_ERROR = 996;
65
+
66
+    /** @deprecated Use \OCP\API::RESPOND_NOT_FOUND instead */
67
+    const RESPOND_NOT_FOUND = 998;
68
+
69
+    /** @deprecated Use \OCP\API::RESPOND_UNKNOWN_ERROR instead */
70
+    const RESPOND_UNKNOWN_ERROR = 999;
71
+
72
+    /**
73
+     * api actions
74
+     */
75
+    protected static $actions = array();
76
+    private static $logoutRequired = false;
77
+    private static $isLoggedIn = false;
78
+
79
+    /**
80
+     * registers an api call
81
+     * @param string $method the http method
82
+     * @param string $url the url to match
83
+     * @param callable $action the function to run
84
+     * @param string $app the id of the app registering the call
85
+     * @param int $authLevel the level of authentication required for the call
86
+     * @param array $defaults
87
+     * @param array $requirements
88
+     */
89
+    public static function register($method, $url, $action, $app,
90
+                $authLevel = API::USER_AUTH,
91
+                $defaults = array(),
92
+                $requirements = array()) {
93
+        $name = strtolower($method).$url;
94
+        $name = str_replace(array('/', '{', '}'), '_', $name);
95
+        if(!isset(self::$actions[$name])) {
96
+            $oldCollection = OC::$server->getRouter()->getCurrentCollection();
97
+            OC::$server->getRouter()->useCollection('ocs');
98
+            OC::$server->getRouter()->create($name, $url)
99
+                ->method($method)
100
+                ->defaults($defaults)
101
+                ->requirements($requirements)
102
+                ->action('OC_API', 'call');
103
+            self::$actions[$name] = array();
104
+            OC::$server->getRouter()->useCollection($oldCollection);
105
+        }
106
+        self::$actions[$name][] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
107
+    }
108
+
109
+    /**
110
+     * handles an api call
111
+     * @param array $parameters
112
+     */
113
+    public static function call($parameters) {
114
+        $request = \OC::$server->getRequest();
115
+        $method = $request->getMethod();
116
+
117
+        // Prepare the request variables
118
+        if($method === 'PUT') {
119
+            $parameters['_put'] = $request->getParams();
120
+        } else if($method === 'DELETE') {
121
+            $parameters['_delete'] = $request->getParams();
122
+        }
123
+        $name = $parameters['_route'];
124
+        // Foreach registered action
125
+        $responses = array();
126
+        $appManager = \OC::$server->getAppManager();
127
+        foreach(self::$actions[$name] as $action) {
128
+            // Check authentication and availability
129
+            if(!self::isAuthorised($action)) {
130
+                $responses[] = array(
131
+                    'app' => $action['app'],
132
+                    'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
133
+                    'shipped' => $appManager->isShipped($action['app']),
134
+                    );
135
+                continue;
136
+            }
137
+            if(!is_callable($action['action'])) {
138
+                $responses[] = array(
139
+                    'app' => $action['app'],
140
+                    'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
141
+                    'shipped' => $appManager->isShipped($action['app']),
142
+                    );
143
+                continue;
144
+            }
145
+            // Run the action
146
+            $responses[] = array(
147
+                'app' => $action['app'],
148
+                'response' => call_user_func($action['action'], $parameters),
149
+                'shipped' => $appManager->isShipped($action['app']),
150
+                );
151
+        }
152
+        $response = self::mergeResponses($responses);
153
+        $format = self::requestedFormat();
154
+        if (self::$logoutRequired) {
155
+            \OC::$server->getUserSession()->logout();
156
+        }
157
+
158
+        self::respond($response, $format);
159
+    }
160
+
161
+    /**
162
+     * merge the returned result objects into one response
163
+     * @param array $responses
164
+     * @return \OC\OCS\Result
165
+     */
166
+    public static function mergeResponses($responses) {
167
+        // Sort into shipped and third-party
168
+        $shipped = array(
169
+            'succeeded' => array(),
170
+            'failed' => array(),
171
+            );
172
+        $thirdparty = array(
173
+            'succeeded' => array(),
174
+            'failed' => array(),
175
+            );
176
+
177
+        foreach($responses as $response) {
178
+            if($response['shipped'] || ($response['app'] === 'core')) {
179
+                if($response['response']->succeeded()) {
180
+                    $shipped['succeeded'][$response['app']] = $response;
181
+                } else {
182
+                    $shipped['failed'][$response['app']] = $response;
183
+                }
184
+            } else {
185
+                if($response['response']->succeeded()) {
186
+                    $thirdparty['succeeded'][$response['app']] = $response;
187
+                } else {
188
+                    $thirdparty['failed'][$response['app']] = $response;
189
+                }
190
+            }
191
+        }
192
+
193
+        // Remove any error responses if there is one shipped response that succeeded
194
+        if(!empty($shipped['failed'])) {
195
+            // Which shipped response do we use if they all failed?
196
+            // They may have failed for different reasons (different status codes)
197
+            // Which response code should we return?
198
+            // Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199
+            // Merge failed responses if more than one
200
+            $data = array();
201
+            foreach($shipped['failed'] as $failure) {
202
+                $data = array_merge_recursive($data, $failure['response']->getData());
203
+            }
204
+            $picked = reset($shipped['failed']);
205
+            $code = $picked['response']->getStatusCode();
206
+            $meta = $picked['response']->getMeta();
207
+            $headers = $picked['response']->getHeaders();
208
+            $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
209
+            return $response;
210
+        } elseif(!empty($shipped['succeeded'])) {
211
+            $responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
+        } elseif(!empty($thirdparty['failed'])) {
213
+            // Merge failed responses if more than one
214
+            $data = array();
215
+            foreach($thirdparty['failed'] as $failure) {
216
+                $data = array_merge_recursive($data, $failure['response']->getData());
217
+            }
218
+            $picked = reset($thirdparty['failed']);
219
+            $code = $picked['response']->getStatusCode();
220
+            $meta = $picked['response']->getMeta();
221
+            $headers = $picked['response']->getHeaders();
222
+            $response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
223
+            return $response;
224
+        } else {
225
+            $responses = $thirdparty['succeeded'];
226
+        }
227
+        // Merge the successful responses
228
+        $data = [];
229
+        $codes = [];
230
+        $header = [];
231
+
232
+        foreach($responses as $response) {
233
+            if($response['shipped']) {
234
+                $data = array_merge_recursive($response['response']->getData(), $data);
235
+            } else {
236
+                $data = array_merge_recursive($data, $response['response']->getData());
237
+            }
238
+            $header = array_merge_recursive($header, $response['response']->getHeaders());
239
+            $codes[] = ['code' => $response['response']->getStatusCode(),
240
+                'meta' => $response['response']->getMeta()];
241
+        }
242
+
243
+        // Use any non 100 status codes
244
+        $statusCode = 100;
245
+        $statusMessage = null;
246
+        foreach($codes as $code) {
247
+            if($code['code'] != 100) {
248
+                $statusCode = $code['code'];
249
+                $statusMessage = $code['meta']['message'];
250
+                break;
251
+            }
252
+        }
253
+
254
+        return new \OC\OCS\Result($data, $statusCode, $statusMessage, $header);
255
+    }
256
+
257
+    /**
258
+     * authenticate the api call
259
+     * @param array $action the action details as supplied to OC_API::register()
260
+     * @return bool
261
+     */
262
+    private static function isAuthorised($action) {
263
+        $level = $action['authlevel'];
264
+        switch($level) {
265
+            case API::GUEST_AUTH:
266
+                // Anyone can access
267
+                return true;
268
+            case API::USER_AUTH:
269
+                // User required
270
+                return self::loginUser();
271
+            case API::SUBADMIN_AUTH:
272
+                // Check for subadmin
273
+                $user = self::loginUser();
274
+                if(!$user) {
275
+                    return false;
276
+                } else {
277
+                    $userObject = \OC::$server->getUserSession()->getUser();
278
+                    if($userObject === null) {
279
+                        return false;
280
+                    }
281
+                    $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282
+                    $admin = OC_User::isAdminUser($user);
283
+                    if($isSubAdmin || $admin) {
284
+                        return true;
285
+                    } else {
286
+                        return false;
287
+                    }
288
+                }
289
+            case API::ADMIN_AUTH:
290
+                // Check for admin
291
+                $user = self::loginUser();
292
+                if(!$user) {
293
+                    return false;
294
+                } else {
295
+                    return OC_User::isAdminUser($user);
296
+                }
297
+            default:
298
+                // oops looks like invalid level supplied
299
+                return false;
300
+        }
301
+    }
302
+
303
+    /**
304
+     * http basic auth
305
+     * @return string|false (username, or false on failure)
306
+     */
307
+    private static function loginUser() {
308
+        if(self::$isLoggedIn === true) {
309
+            return \OC_User::getUser();
310
+        }
311
+
312
+        // reuse existing login
313
+        $loggedIn = \OC::$server->getUserSession()->isLoggedIn();
314
+        if ($loggedIn === true) {
315
+            if (\OC::$server->getTwoFactorAuthManager()->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
316
+                // Do not allow access to OCS until the 2FA challenge was solved successfully
317
+                return false;
318
+            }
319
+            $ocsApiRequest = isset($_SERVER['HTTP_OCS_APIREQUEST']) ? $_SERVER['HTTP_OCS_APIREQUEST'] === 'true' : false;
320
+            if ($ocsApiRequest) {
321
+
322
+                // initialize the user's filesystem
323
+                \OC_Util::setupFS(\OC_User::getUser());
324
+                self::$isLoggedIn = true;
325
+
326
+                return OC_User::getUser();
327
+            }
328
+            return false;
329
+        }
330
+
331
+        // basic auth - because OC_User::login will create a new session we shall only try to login
332
+        // if user and pass are set
333
+        $userSession = \OC::$server->getUserSession();
334
+        $request = \OC::$server->getRequest();
335
+        try {
336
+            if ($userSession->tryTokenLogin($request)
337
+                || $userSession->tryBasicAuthLogin($request, \OC::$server->getBruteForceThrottler())) {
338
+                self::$logoutRequired = true;
339
+            } else {
340
+                return false;
341
+            }
342
+            // initialize the user's filesystem
343
+            \OC_Util::setupFS(\OC_User::getUser());
344
+            self::$isLoggedIn = true;
345
+
346
+            return \OC_User::getUser();
347
+        } catch (\OC\User\LoginException $e) {
348
+            return false;
349
+        }
350
+    }
351
+
352
+    /**
353
+     * respond to a call
354
+     * @param \OC\OCS\Result $result
355
+     * @param string $format the format xml|json
356
+     */
357
+    public static function respond($result, $format='xml') {
358
+        $request = \OC::$server->getRequest();
359
+
360
+        // Send 401 headers if unauthorised
361
+        if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362
+            // If request comes from JS return dummy auth request
363
+            if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364
+                header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365
+            } else {
366
+                header('WWW-Authenticate: Basic realm="Authorisation Required"');
367
+            }
368
+            header('HTTP/1.0 401 Unauthorized');
369
+        }
370
+
371
+        foreach($result->getHeaders() as $name => $value) {
372
+            header($name . ': ' . $value);
373
+        }
374
+
375
+        $meta = $result->getMeta();
376
+        $data = $result->getData();
377
+        if (self::isV2($request)) {
378
+            $statusCode = self::mapStatusCodes($result->getStatusCode());
379
+            if (!is_null($statusCode)) {
380
+                $meta['statuscode'] = $statusCode;
381
+                OC_Response::setStatus($statusCode);
382
+            }
383
+        }
384
+
385
+        self::setContentType($format);
386
+        $body = self::renderResult($format, $meta, $data);
387
+        echo $body;
388
+    }
389
+
390
+    /**
391
+     * @param XMLWriter $writer
392
+     */
393
+    private static function toXML($array, $writer) {
394
+        foreach($array as $k => $v) {
395
+            if ($k[0] === '@') {
396
+                $writer->writeAttribute(substr($k, 1), $v);
397
+                continue;
398
+            } else if (is_numeric($k)) {
399
+                $k = 'element';
400
+            }
401
+            if(is_array($v)) {
402
+                $writer->startElement($k);
403
+                self::toXML($v, $writer);
404
+                $writer->endElement();
405
+            } else {
406
+                $writer->writeElement($k, $v);
407
+            }
408
+        }
409
+    }
410
+
411
+    /**
412
+     * @return string
413
+     */
414
+    public static function requestedFormat() {
415
+        $formats = array('json', 'xml');
416
+
417
+        $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
418
+        return $format;
419
+    }
420
+
421
+    /**
422
+     * Based on the requested format the response content type is set
423
+     * @param string $format
424
+     */
425
+    public static function setContentType($format = null) {
426
+        $format = is_null($format) ? self::requestedFormat() : $format;
427
+        if ($format === 'xml') {
428
+            header('Content-type: text/xml; charset=UTF-8');
429
+            return;
430
+        }
431
+
432
+        if ($format === 'json') {
433
+            header('Content-Type: application/json; charset=utf-8');
434
+            return;
435
+        }
436
+
437
+        header('Content-Type: application/octet-stream; charset=utf-8');
438
+    }
439
+
440
+    /**
441
+     * @param \OCP\IRequest $request
442
+     * @return bool
443
+     */
444
+    protected static function isV2(\OCP\IRequest $request) {
445
+        $script = $request->getScriptName();
446
+
447
+        return substr($script, -11) === '/ocs/v2.php';
448
+    }
449
+
450
+    /**
451
+     * @param integer $sc
452
+     * @return int
453
+     */
454
+    public static function mapStatusCodes($sc) {
455
+        switch ($sc) {
456
+            case API::RESPOND_NOT_FOUND:
457
+                return Http::STATUS_NOT_FOUND;
458
+            case API::RESPOND_SERVER_ERROR:
459
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
460
+            case API::RESPOND_UNKNOWN_ERROR:
461
+                return Http::STATUS_INTERNAL_SERVER_ERROR;
462
+            case API::RESPOND_UNAUTHORISED:
463
+                // already handled for v1
464
+                return null;
465
+            case 100:
466
+                return Http::STATUS_OK;
467
+        }
468
+        // any 2xx, 4xx and 5xx will be used as is
469
+        if ($sc >= 200 && $sc < 600) {
470
+            return $sc;
471
+        }
472
+
473
+        return Http::STATUS_BAD_REQUEST;
474
+    }
475
+
476
+    /**
477
+     * @param string $format
478
+     * @return string
479
+     */
480
+    public static function renderResult($format, $meta, $data) {
481
+        $response = array(
482
+            'ocs' => array(
483
+                'meta' => $meta,
484
+                'data' => $data,
485
+            ),
486
+        );
487
+        if ($format == 'json') {
488
+            return OC_JSON::encode($response);
489
+        }
490
+
491
+        $writer = new XMLWriter();
492
+        $writer->openMemory();
493
+        $writer->setIndent(true);
494
+        $writer->startDocument();
495
+        self::toXML($response, $writer);
496
+        $writer->endDocument();
497
+        return $writer->outputMemory(true);
498
+    }
499 499
 }
Please login to merge, or discard this patch.
Spacing   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
 				$requirements = array()) {
93 93
 		$name = strtolower($method).$url;
94 94
 		$name = str_replace(array('/', '{', '}'), '_', $name);
95
-		if(!isset(self::$actions[$name])) {
95
+		if (!isset(self::$actions[$name])) {
96 96
 			$oldCollection = OC::$server->getRouter()->getCurrentCollection();
97 97
 			OC::$server->getRouter()->useCollection('ocs');
98 98
 			OC::$server->getRouter()->create($name, $url)
@@ -115,18 +115,18 @@  discard block
 block discarded – undo
115 115
 		$method = $request->getMethod();
116 116
 
117 117
 		// Prepare the request variables
118
-		if($method === 'PUT') {
118
+		if ($method === 'PUT') {
119 119
 			$parameters['_put'] = $request->getParams();
120
-		} else if($method === 'DELETE') {
120
+		} else if ($method === 'DELETE') {
121 121
 			$parameters['_delete'] = $request->getParams();
122 122
 		}
123 123
 		$name = $parameters['_route'];
124 124
 		// Foreach registered action
125 125
 		$responses = array();
126 126
 		$appManager = \OC::$server->getAppManager();
127
-		foreach(self::$actions[$name] as $action) {
127
+		foreach (self::$actions[$name] as $action) {
128 128
 			// Check authentication and availability
129
-			if(!self::isAuthorised($action)) {
129
+			if (!self::isAuthorised($action)) {
130 130
 				$responses[] = array(
131 131
 					'app' => $action['app'],
132 132
 					'response' => new \OC\OCS\Result(null, API::RESPOND_UNAUTHORISED, 'Unauthorised'),
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 					);
135 135
 				continue;
136 136
 			}
137
-			if(!is_callable($action['action'])) {
137
+			if (!is_callable($action['action'])) {
138 138
 				$responses[] = array(
139 139
 					'app' => $action['app'],
140 140
 					'response' => new \OC\OCS\Result(null, API::RESPOND_NOT_FOUND, 'Api method not found'),
@@ -174,15 +174,15 @@  discard block
 block discarded – undo
174 174
 			'failed' => array(),
175 175
 			);
176 176
 
177
-		foreach($responses as $response) {
178
-			if($response['shipped'] || ($response['app'] === 'core')) {
179
-				if($response['response']->succeeded()) {
177
+		foreach ($responses as $response) {
178
+			if ($response['shipped'] || ($response['app'] === 'core')) {
179
+				if ($response['response']->succeeded()) {
180 180
 					$shipped['succeeded'][$response['app']] = $response;
181 181
 				} else {
182 182
 					$shipped['failed'][$response['app']] = $response;
183 183
 				}
184 184
 			} else {
185
-				if($response['response']->succeeded()) {
185
+				if ($response['response']->succeeded()) {
186 186
 					$thirdparty['succeeded'][$response['app']] = $response;
187 187
 				} else {
188 188
 					$thirdparty['failed'][$response['app']] = $response;
@@ -191,14 +191,14 @@  discard block
 block discarded – undo
191 191
 		}
192 192
 
193 193
 		// Remove any error responses if there is one shipped response that succeeded
194
-		if(!empty($shipped['failed'])) {
194
+		if (!empty($shipped['failed'])) {
195 195
 			// Which shipped response do we use if they all failed?
196 196
 			// They may have failed for different reasons (different status codes)
197 197
 			// Which response code should we return?
198 198
 			// Maybe any that are not \OCP\API::RESPOND_SERVER_ERROR
199 199
 			// Merge failed responses if more than one
200 200
 			$data = array();
201
-			foreach($shipped['failed'] as $failure) {
201
+			foreach ($shipped['failed'] as $failure) {
202 202
 				$data = array_merge_recursive($data, $failure['response']->getData());
203 203
 			}
204 204
 			$picked = reset($shipped['failed']);
@@ -207,12 +207,12 @@  discard block
 block discarded – undo
207 207
 			$headers = $picked['response']->getHeaders();
208 208
 			$response = new \OC\OCS\Result($data, $code, $meta['message'], $headers);
209 209
 			return $response;
210
-		} elseif(!empty($shipped['succeeded'])) {
210
+		} elseif (!empty($shipped['succeeded'])) {
211 211
 			$responses = array_merge($shipped['succeeded'], $thirdparty['succeeded']);
212
-		} elseif(!empty($thirdparty['failed'])) {
212
+		} elseif (!empty($thirdparty['failed'])) {
213 213
 			// Merge failed responses if more than one
214 214
 			$data = array();
215
-			foreach($thirdparty['failed'] as $failure) {
215
+			foreach ($thirdparty['failed'] as $failure) {
216 216
 				$data = array_merge_recursive($data, $failure['response']->getData());
217 217
 			}
218 218
 			$picked = reset($thirdparty['failed']);
@@ -229,8 +229,8 @@  discard block
 block discarded – undo
229 229
 		$codes = [];
230 230
 		$header = [];
231 231
 
232
-		foreach($responses as $response) {
233
-			if($response['shipped']) {
232
+		foreach ($responses as $response) {
233
+			if ($response['shipped']) {
234 234
 				$data = array_merge_recursive($response['response']->getData(), $data);
235 235
 			} else {
236 236
 				$data = array_merge_recursive($data, $response['response']->getData());
@@ -243,8 +243,8 @@  discard block
 block discarded – undo
243 243
 		// Use any non 100 status codes
244 244
 		$statusCode = 100;
245 245
 		$statusMessage = null;
246
-		foreach($codes as $code) {
247
-			if($code['code'] != 100) {
246
+		foreach ($codes as $code) {
247
+			if ($code['code'] != 100) {
248 248
 				$statusCode = $code['code'];
249 249
 				$statusMessage = $code['meta']['message'];
250 250
 				break;
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	 */
262 262
 	private static function isAuthorised($action) {
263 263
 		$level = $action['authlevel'];
264
-		switch($level) {
264
+		switch ($level) {
265 265
 			case API::GUEST_AUTH:
266 266
 				// Anyone can access
267 267
 				return true;
@@ -271,16 +271,16 @@  discard block
 block discarded – undo
271 271
 			case API::SUBADMIN_AUTH:
272 272
 				// Check for subadmin
273 273
 				$user = self::loginUser();
274
-				if(!$user) {
274
+				if (!$user) {
275 275
 					return false;
276 276
 				} else {
277 277
 					$userObject = \OC::$server->getUserSession()->getUser();
278
-					if($userObject === null) {
278
+					if ($userObject === null) {
279 279
 						return false;
280 280
 					}
281 281
 					$isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
282 282
 					$admin = OC_User::isAdminUser($user);
283
-					if($isSubAdmin || $admin) {
283
+					if ($isSubAdmin || $admin) {
284 284
 						return true;
285 285
 					} else {
286 286
 						return false;
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 			case API::ADMIN_AUTH:
290 290
 				// Check for admin
291 291
 				$user = self::loginUser();
292
-				if(!$user) {
292
+				if (!$user) {
293 293
 					return false;
294 294
 				} else {
295 295
 					return OC_User::isAdminUser($user);
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
 	 * @return string|false (username, or false on failure)
306 306
 	 */
307 307
 	private static function loginUser() {
308
-		if(self::$isLoggedIn === true) {
308
+		if (self::$isLoggedIn === true) {
309 309
 			return \OC_User::getUser();
310 310
 		}
311 311
 
@@ -354,13 +354,13 @@  discard block
 block discarded – undo
354 354
 	 * @param \OC\OCS\Result $result
355 355
 	 * @param string $format the format xml|json
356 356
 	 */
357
-	public static function respond($result, $format='xml') {
357
+	public static function respond($result, $format = 'xml') {
358 358
 		$request = \OC::$server->getRequest();
359 359
 
360 360
 		// Send 401 headers if unauthorised
361
-		if($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
361
+		if ($result->getStatusCode() === API::RESPOND_UNAUTHORISED) {
362 362
 			// If request comes from JS return dummy auth request
363
-			if($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
363
+			if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') {
364 364
 				header('WWW-Authenticate: DummyBasic realm="Authorisation Required"');
365 365
 			} else {
366 366
 				header('WWW-Authenticate: Basic realm="Authorisation Required"');
@@ -368,8 +368,8 @@  discard block
 block discarded – undo
368 368
 			header('HTTP/1.0 401 Unauthorized');
369 369
 		}
370 370
 
371
-		foreach($result->getHeaders() as $name => $value) {
372
-			header($name . ': ' . $value);
371
+		foreach ($result->getHeaders() as $name => $value) {
372
+			header($name.': '.$value);
373 373
 		}
374 374
 
375 375
 		$meta = $result->getMeta();
@@ -391,14 +391,14 @@  discard block
 block discarded – undo
391 391
 	 * @param XMLWriter $writer
392 392
 	 */
393 393
 	private static function toXML($array, $writer) {
394
-		foreach($array as $k => $v) {
394
+		foreach ($array as $k => $v) {
395 395
 			if ($k[0] === '@') {
396 396
 				$writer->writeAttribute(substr($k, 1), $v);
397 397
 				continue;
398 398
 			} else if (is_numeric($k)) {
399 399
 				$k = 'element';
400 400
 			}
401
-			if(is_array($v)) {
401
+			if (is_array($v)) {
402 402
 				$writer->startElement($k);
403 403
 				self::toXML($v, $writer);
404 404
 				$writer->endElement();
Please login to merge, or discard this patch.
lib/private/legacy/eventsource.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -88,7 +88,7 @@
 block discarded – undo
88 88
 	 * send a message to the client
89 89
 	 *
90 90
 	 * @param string $type
91
-	 * @param mixed $data
91
+	 * @param string $data
92 92
 	 *
93 93
 	 * @throws \BadMethodCallException
94 94
 	 * if only one parameter is given, a typeless message will be send with that parameter as data
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 		header('X-Accel-Buffering: no');
61 61
 		$this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62 62
 		if ($this->fallback) {
63
-			$this->fallBackId = (int)$_GET['fallback_id'];
63
+			$this->fallBackId = (int) $_GET['fallback_id'];
64 64
 			/**
65 65
 			 * FIXME: The default content-security-policy of ownCloud forbids inline
66 66
 			 * JavaScript for security reasons. IE starting on Windows 10 will
@@ -73,11 +73,11 @@  discard block
 block discarded – undo
73 73
 			 */
74 74
 			header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75 75
 			header("Content-Type: text/html");
76
-			echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
76
+			echo str_repeat('<span></span>'.PHP_EOL, 10); //dummy data to keep IE happy
77 77
 		} else {
78 78
 			header("Content-Type: text/event-stream");
79 79
 		}
80
-		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
80
+		if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81 81
 			header('Location: '.\OC::$WEBROOT);
82 82
 			exit();
83 83
 		}
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
 	 */
101 101
 	public function send($type, $data = null) {
102 102
 		if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
103
-			throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
103
+			throw new BadMethodCallException('Type needs to be alphanumeric ('.$type.')');
104 104
 		}
105 105
 		$this->init();
106 106
 		if (is_null($data)) {
@@ -109,13 +109,13 @@  discard block
 block discarded – undo
109 109
 		}
110 110
 		if ($this->fallback) {
111 111
 			$response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
112
-				. $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
112
+				. $this->fallBackId.',"'.$type.'",'.OCP\JSON::encode($data).')</script>'.PHP_EOL;
113 113
 			echo $response;
114 114
 		} else {
115 115
 			if ($type) {
116
-				echo 'event: ' . $type . PHP_EOL;
116
+				echo 'event: '.$type.PHP_EOL;
117 117
 			}
118
-			echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
118
+			echo 'data: '.OCP\JSON::encode($data).PHP_EOL;
119 119
 		}
120 120
 		echo PHP_EOL;
121 121
 		flush();
Please login to merge, or discard this patch.
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -33,99 +33,99 @@
 block discarded – undo
33 33
  * use server side events with caution, to many open requests can hang the server
34 34
  */
35 35
 class OC_EventSource implements \OCP\IEventSource {
36
-	/**
37
-	 * @var bool
38
-	 */
39
-	private $fallback;
36
+    /**
37
+     * @var bool
38
+     */
39
+    private $fallback;
40 40
 
41
-	/**
42
-	 * @var int
43
-	 */
44
-	private $fallBackId = 0;
41
+    /**
42
+     * @var int
43
+     */
44
+    private $fallBackId = 0;
45 45
 
46
-	/**
47
-	 * @var bool
48
-	 */
49
-	private $started = false;
46
+    /**
47
+     * @var bool
48
+     */
49
+    private $started = false;
50 50
 
51
-	protected function init() {
52
-		if ($this->started) {
53
-			return;
54
-		}
55
-		$this->started = true;
51
+    protected function init() {
52
+        if ($this->started) {
53
+            return;
54
+        }
55
+        $this->started = true;
56 56
 
57
-		// prevent php output buffering, caching and nginx buffering
58
-		OC_Util::obEnd();
59
-		header('Cache-Control: no-cache');
60
-		header('X-Accel-Buffering: no');
61
-		$this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62
-		if ($this->fallback) {
63
-			$this->fallBackId = (int)$_GET['fallback_id'];
64
-			/**
65
-			 * FIXME: The default content-security-policy of ownCloud forbids inline
66
-			 * JavaScript for security reasons. IE starting on Windows 10 will
67
-			 * however also obey the CSP which will break the event source fallback.
68
-			 *
69
-			 * As a workaround thus we set a custom policy which allows the execution
70
-			 * of inline JavaScript.
71
-			 *
72
-			 * @link https://github.com/owncloud/core/issues/14286
73
-			 */
74
-			header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75
-			header("Content-Type: text/html");
76
-			echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
77
-		} else {
78
-			header("Content-Type: text/event-stream");
79
-		}
80
-		if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81
-			header('Location: '.\OC::$WEBROOT);
82
-			exit();
83
-		}
84
-		if (!\OC::$server->getRequest()->passesCSRFCheck()) {
85
-			$this->send('error', 'Possible CSRF attack. Connection will be closed.');
86
-			$this->close();
87
-			exit();
88
-		}
89
-		flush();
90
-	}
57
+        // prevent php output buffering, caching and nginx buffering
58
+        OC_Util::obEnd();
59
+        header('Cache-Control: no-cache');
60
+        header('X-Accel-Buffering: no');
61
+        $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true';
62
+        if ($this->fallback) {
63
+            $this->fallBackId = (int)$_GET['fallback_id'];
64
+            /**
65
+             * FIXME: The default content-security-policy of ownCloud forbids inline
66
+             * JavaScript for security reasons. IE starting on Windows 10 will
67
+             * however also obey the CSP which will break the event source fallback.
68
+             *
69
+             * As a workaround thus we set a custom policy which allows the execution
70
+             * of inline JavaScript.
71
+             *
72
+             * @link https://github.com/owncloud/core/issues/14286
73
+             */
74
+            header("Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'");
75
+            header("Content-Type: text/html");
76
+            echo str_repeat('<span></span>' . PHP_EOL, 10); //dummy data to keep IE happy
77
+        } else {
78
+            header("Content-Type: text/event-stream");
79
+        }
80
+        if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
81
+            header('Location: '.\OC::$WEBROOT);
82
+            exit();
83
+        }
84
+        if (!\OC::$server->getRequest()->passesCSRFCheck()) {
85
+            $this->send('error', 'Possible CSRF attack. Connection will be closed.');
86
+            $this->close();
87
+            exit();
88
+        }
89
+        flush();
90
+    }
91 91
 
92
-	/**
93
-	 * send a message to the client
94
-	 *
95
-	 * @param string $type
96
-	 * @param mixed $data
97
-	 *
98
-	 * @throws \BadMethodCallException
99
-	 * if only one parameter is given, a typeless message will be send with that parameter as data
100
-	 * @suppress PhanDeprecatedFunction
101
-	 */
102
-	public function send($type, $data = null) {
103
-		if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
104
-			throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
105
-		}
106
-		$this->init();
107
-		if (is_null($data)) {
108
-			$data = $type;
109
-			$type = null;
110
-		}
111
-		if ($this->fallback) {
112
-			$response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
113
-				. $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
114
-			echo $response;
115
-		} else {
116
-			if ($type) {
117
-				echo 'event: ' . $type . PHP_EOL;
118
-			}
119
-			echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
120
-		}
121
-		echo PHP_EOL;
122
-		flush();
123
-	}
92
+    /**
93
+     * send a message to the client
94
+     *
95
+     * @param string $type
96
+     * @param mixed $data
97
+     *
98
+     * @throws \BadMethodCallException
99
+     * if only one parameter is given, a typeless message will be send with that parameter as data
100
+     * @suppress PhanDeprecatedFunction
101
+     */
102
+    public function send($type, $data = null) {
103
+        if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
104
+            throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')');
105
+        }
106
+        $this->init();
107
+        if (is_null($data)) {
108
+            $data = $type;
109
+            $type = null;
110
+        }
111
+        if ($this->fallback) {
112
+            $response = '<script type="text/javascript">window.parent.OC.EventSource.fallBackCallBack('
113
+                . $this->fallBackId . ',"' . $type . '",' . OCP\JSON::encode($data) . ')</script>' . PHP_EOL;
114
+            echo $response;
115
+        } else {
116
+            if ($type) {
117
+                echo 'event: ' . $type . PHP_EOL;
118
+            }
119
+            echo 'data: ' . OCP\JSON::encode($data) . PHP_EOL;
120
+        }
121
+        echo PHP_EOL;
122
+        flush();
123
+    }
124 124
 
125
-	/**
126
-	 * close the connection of the event source
127
-	 */
128
-	public function close() {
129
-		$this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
130
-	}
125
+    /**
126
+     * close the connection of the event source
127
+     */
128
+    public function close() {
129
+        $this->send('__internal__', 'close'); //server side closing can be an issue, let the client do it
130
+    }
131 131
 }
Please login to merge, or discard this patch.
lib/private/Memcache/Memcached.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -155,7 +155,7 @@
 block discarded – undo
155 155
 	 * Set a value in the cache if it's not already stored
156 156
 	 *
157 157
 	 * @param string $key
158
-	 * @param mixed $value
158
+	 * @param integer $value
159 159
 	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
160 160
 	 * @return bool
161 161
 	 * @throws \Exception
Please login to merge, or discard this patch.
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -33,193 +33,193 @@
 block discarded – undo
33 33
 use OCP\IMemcache;
34 34
 
35 35
 class Memcached extends Cache implements IMemcache {
36
-	use CASTrait;
37
-
38
-	/**
39
-	 * @var \Memcached $cache
40
-	 */
41
-	private static $cache = null;
42
-
43
-	use CADTrait;
44
-
45
-	public function __construct($prefix = '') {
46
-		parent::__construct($prefix);
47
-		if (is_null(self::$cache)) {
48
-			self::$cache = new \Memcached();
49
-
50
-			$defaultOptions = [
51
-				\Memcached::OPT_CONNECT_TIMEOUT => 50,
52
-				\Memcached::OPT_RETRY_TIMEOUT =>   50,
53
-				\Memcached::OPT_SEND_TIMEOUT =>    50,
54
-				\Memcached::OPT_RECV_TIMEOUT =>    50,
55
-				\Memcached::OPT_POLL_TIMEOUT =>    50,
56
-
57
-				// Enable compression
58
-				\Memcached::OPT_COMPRESSION =>          true,
59
-
60
-				// Turn on consistent hashing
61
-				\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
-
63
-				// Enable Binary Protocol
64
-				//\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
-			];
66
-			// by default enable igbinary serializer if available
67
-			if (\Memcached::HAVE_IGBINARY) {
68
-				$defaultOptions[\Memcached::OPT_SERIALIZER] =
69
-					\Memcached::SERIALIZER_IGBINARY;
70
-			}
71
-			$options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
-			if (is_array($options)) {
73
-				$options = $options + $defaultOptions;
74
-				self::$cache->setOptions($options);
75
-			} else {
76
-				throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
-			}
78
-
79
-			$servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
-			if (!$servers) {
81
-				$server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
-				if ($server) {
83
-					$servers = [$server];
84
-				} else {
85
-					$servers = [['localhost', 11211]];
86
-				}
87
-			}
88
-			self::$cache->addServers($servers);
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
-	 */
95
-	protected function getNameSpace() {
96
-		return $this->prefix;
97
-	}
98
-
99
-	public function get($key) {
100
-		$result = self::$cache->get($this->getNameSpace() . $key);
101
-		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
-			return null;
103
-		} else {
104
-			return $result;
105
-		}
106
-	}
107
-
108
-	public function set($key, $value, $ttl = 0) {
109
-		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
111
-		} else {
112
-			$result = self::$cache->set($this->getNameSpace() . $key, $value);
113
-		}
114
-		if ($result !== true) {
115
-			$this->verifyReturnCode();
116
-		}
117
-		return $result;
118
-	}
119
-
120
-	public function hasKey($key) {
121
-		self::$cache->get($this->getNameSpace() . $key);
122
-		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
-	}
124
-
125
-	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNameSpace() . $key);
127
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
-			$this->verifyReturnCode();
129
-		}
130
-		return $result;
131
-	}
132
-
133
-	public function clear($prefix = '') {
134
-		$prefix = $this->getNameSpace() . $prefix;
135
-		$allKeys = self::$cache->getAllKeys();
136
-		if ($allKeys === false) {
137
-			// newer Memcached doesn't like getAllKeys(), flush everything
138
-			self::$cache->flush();
139
-			return true;
140
-		}
141
-		$keys = array();
142
-		$prefixLength = strlen($prefix);
143
-		foreach ($allKeys as $key) {
144
-			if (substr($key, 0, $prefixLength) === $prefix) {
145
-				$keys[] = $key;
146
-			}
147
-		}
148
-		if (method_exists(self::$cache, 'deleteMulti')) {
149
-			self::$cache->deleteMulti($keys);
150
-		} else {
151
-			foreach ($keys as $key) {
152
-				self::$cache->delete($key);
153
-			}
154
-		}
155
-		return true;
156
-	}
157
-
158
-	/**
159
-	 * Set a value in the cache if it's not already stored
160
-	 *
161
-	 * @param string $key
162
-	 * @param mixed $value
163
-	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
-	 * @return bool
165
-	 * @throws \Exception
166
-	 */
167
-	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
-			$this->verifyReturnCode();
171
-		}
172
-		return $result;
173
-	}
174
-
175
-	/**
176
-	 * Increase a stored number
177
-	 *
178
-	 * @param string $key
179
-	 * @param int $step
180
-	 * @return int | bool
181
-	 */
182
-	public function inc($key, $step = 1) {
183
-		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
185
-
186
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
-			return false;
188
-		}
189
-
190
-		return $result;
191
-	}
192
-
193
-	/**
194
-	 * Decrease a stored number
195
-	 *
196
-	 * @param string $key
197
-	 * @param int $step
198
-	 * @return int | bool
199
-	 */
200
-	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
-
203
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
-			return false;
205
-		}
206
-
207
-		return $result;
208
-	}
209
-
210
-	static public function isAvailable() {
211
-		return extension_loaded('memcached');
212
-	}
213
-
214
-	/**
215
-	 * @throws \Exception
216
-	 */
217
-	private function verifyReturnCode() {
218
-		$code = self::$cache->getResultCode();
219
-		if ($code === \Memcached::RES_SUCCESS) {
220
-			return;
221
-		}
222
-		$message = self::$cache->getResultMessage();
223
-		throw new \Exception("Error $code interacting with memcached : $message");
224
-	}
36
+    use CASTrait;
37
+
38
+    /**
39
+     * @var \Memcached $cache
40
+     */
41
+    private static $cache = null;
42
+
43
+    use CADTrait;
44
+
45
+    public function __construct($prefix = '') {
46
+        parent::__construct($prefix);
47
+        if (is_null(self::$cache)) {
48
+            self::$cache = new \Memcached();
49
+
50
+            $defaultOptions = [
51
+                \Memcached::OPT_CONNECT_TIMEOUT => 50,
52
+                \Memcached::OPT_RETRY_TIMEOUT =>   50,
53
+                \Memcached::OPT_SEND_TIMEOUT =>    50,
54
+                \Memcached::OPT_RECV_TIMEOUT =>    50,
55
+                \Memcached::OPT_POLL_TIMEOUT =>    50,
56
+
57
+                // Enable compression
58
+                \Memcached::OPT_COMPRESSION =>          true,
59
+
60
+                // Turn on consistent hashing
61
+                \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
+
63
+                // Enable Binary Protocol
64
+                //\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
+            ];
66
+            // by default enable igbinary serializer if available
67
+            if (\Memcached::HAVE_IGBINARY) {
68
+                $defaultOptions[\Memcached::OPT_SERIALIZER] =
69
+                    \Memcached::SERIALIZER_IGBINARY;
70
+            }
71
+            $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
+            if (is_array($options)) {
73
+                $options = $options + $defaultOptions;
74
+                self::$cache->setOptions($options);
75
+            } else {
76
+                throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
+            }
78
+
79
+            $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
+            if (!$servers) {
81
+                $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
+                if ($server) {
83
+                    $servers = [$server];
84
+                } else {
85
+                    $servers = [['localhost', 11211]];
86
+                }
87
+            }
88
+            self::$cache->addServers($servers);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
+     */
95
+    protected function getNameSpace() {
96
+        return $this->prefix;
97
+    }
98
+
99
+    public function get($key) {
100
+        $result = self::$cache->get($this->getNameSpace() . $key);
101
+        if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
+            return null;
103
+        } else {
104
+            return $result;
105
+        }
106
+    }
107
+
108
+    public function set($key, $value, $ttl = 0) {
109
+        if ($ttl > 0) {
110
+            $result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
111
+        } else {
112
+            $result = self::$cache->set($this->getNameSpace() . $key, $value);
113
+        }
114
+        if ($result !== true) {
115
+            $this->verifyReturnCode();
116
+        }
117
+        return $result;
118
+    }
119
+
120
+    public function hasKey($key) {
121
+        self::$cache->get($this->getNameSpace() . $key);
122
+        return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
+    }
124
+
125
+    public function remove($key) {
126
+        $result= self::$cache->delete($this->getNameSpace() . $key);
127
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
+            $this->verifyReturnCode();
129
+        }
130
+        return $result;
131
+    }
132
+
133
+    public function clear($prefix = '') {
134
+        $prefix = $this->getNameSpace() . $prefix;
135
+        $allKeys = self::$cache->getAllKeys();
136
+        if ($allKeys === false) {
137
+            // newer Memcached doesn't like getAllKeys(), flush everything
138
+            self::$cache->flush();
139
+            return true;
140
+        }
141
+        $keys = array();
142
+        $prefixLength = strlen($prefix);
143
+        foreach ($allKeys as $key) {
144
+            if (substr($key, 0, $prefixLength) === $prefix) {
145
+                $keys[] = $key;
146
+            }
147
+        }
148
+        if (method_exists(self::$cache, 'deleteMulti')) {
149
+            self::$cache->deleteMulti($keys);
150
+        } else {
151
+            foreach ($keys as $key) {
152
+                self::$cache->delete($key);
153
+            }
154
+        }
155
+        return true;
156
+    }
157
+
158
+    /**
159
+     * Set a value in the cache if it's not already stored
160
+     *
161
+     * @param string $key
162
+     * @param mixed $value
163
+     * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
+     * @return bool
165
+     * @throws \Exception
166
+     */
167
+    public function add($key, $value, $ttl = 0) {
168
+        $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
+            $this->verifyReturnCode();
171
+        }
172
+        return $result;
173
+    }
174
+
175
+    /**
176
+     * Increase a stored number
177
+     *
178
+     * @param string $key
179
+     * @param int $step
180
+     * @return int | bool
181
+     */
182
+    public function inc($key, $step = 1) {
183
+        $this->add($key, 0);
184
+        $result = self::$cache->increment($this->getPrefix() . $key, $step);
185
+
186
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
+            return false;
188
+        }
189
+
190
+        return $result;
191
+    }
192
+
193
+    /**
194
+     * Decrease a stored number
195
+     *
196
+     * @param string $key
197
+     * @param int $step
198
+     * @return int | bool
199
+     */
200
+    public function dec($key, $step = 1) {
201
+        $result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
+
203
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
+            return false;
205
+        }
206
+
207
+        return $result;
208
+    }
209
+
210
+    static public function isAvailable() {
211
+        return extension_loaded('memcached');
212
+    }
213
+
214
+    /**
215
+     * @throws \Exception
216
+     */
217
+    private function verifyReturnCode() {
218
+        $code = self::$cache->getResultCode();
219
+        if ($code === \Memcached::RES_SUCCESS) {
220
+            return;
221
+        }
222
+        $message = self::$cache->getResultMessage();
223
+        throw new \Exception("Error $code interacting with memcached : $message");
224
+    }
225 225
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	}
98 98
 
99 99
 	public function get($key) {
100
-		$result = self::$cache->get($this->getNameSpace() . $key);
100
+		$result = self::$cache->get($this->getNameSpace().$key);
101 101
 		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102 102
 			return null;
103 103
 		} else {
@@ -107,9 +107,9 @@  discard block
 block discarded – undo
107 107
 
108 108
 	public function set($key, $value, $ttl = 0) {
109 109
 		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNameSpace() . $key, $value, $ttl);
110
+			$result = self::$cache->set($this->getNameSpace().$key, $value, $ttl);
111 111
 		} else {
112
-			$result = self::$cache->set($this->getNameSpace() . $key, $value);
112
+			$result = self::$cache->set($this->getNameSpace().$key, $value);
113 113
 		}
114 114
 		if ($result !== true) {
115 115
 			$this->verifyReturnCode();
@@ -118,12 +118,12 @@  discard block
 block discarded – undo
118 118
 	}
119 119
 
120 120
 	public function hasKey($key) {
121
-		self::$cache->get($this->getNameSpace() . $key);
121
+		self::$cache->get($this->getNameSpace().$key);
122 122
 		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123 123
 	}
124 124
 
125 125
 	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNameSpace() . $key);
126
+		$result = self::$cache->delete($this->getNameSpace().$key);
127 127
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128 128
 			$this->verifyReturnCode();
129 129
 		}
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 	}
132 132
 
133 133
 	public function clear($prefix = '') {
134
-		$prefix = $this->getNameSpace() . $prefix;
134
+		$prefix = $this->getNameSpace().$prefix;
135 135
 		$allKeys = self::$cache->getAllKeys();
136 136
 		if ($allKeys === false) {
137 137
 			// newer Memcached doesn't like getAllKeys(), flush everything
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 	 * @throws \Exception
166 166
 	 */
167 167
 	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
168
+		$result = self::$cache->add($this->getPrefix().$key, $value, $ttl);
169 169
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170 170
 			$this->verifyReturnCode();
171 171
 		}
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 	 */
182 182
 	public function inc($key, $step = 1) {
183 183
 		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
184
+		$result = self::$cache->increment($this->getPrefix().$key, $step);
185 185
 
186 186
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187 187
 			return false;
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
 	 * @return int | bool
199 199
 	 */
200 200
 	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
201
+		$result = self::$cache->decrement($this->getPrefix().$key, $step);
202 202
 
203 203
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204 204
 			return false;
Please login to merge, or discard this patch.
lib/private/Repair.php 4 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 	 * Returns expensive repair steps to be run on the
140 140
 	 * command line with a special option.
141 141
 	 *
142
-	 * @return IRepairStep[]
142
+	 * @return OldGroupMembershipShares[]
143 143
 	 */
144 144
 	public static function getExpensiveRepairSteps() {
145 145
 		return [
@@ -216,7 +216,6 @@  discard block
 block discarded – undo
216 216
 	}
217 217
 
218 218
 	/**
219
-	 * @param int $max
220 219
 	 */
221 220
 	public function finishProgress() {
222 221
 		// for now just emit as we did in the past
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@
 block discarded – undo
58 58
 use Symfony\Component\EventDispatcher\EventDispatcher;
59 59
 use Symfony\Component\EventDispatcher\GenericEvent;
60 60
 
61
-class Repair implements IOutput{
61
+class Repair implements IOutput {
62 62
 	/* @var IRepairStep[] */
63 63
 	private $repairSteps;
64 64
 	/** @var EventDispatcher */
Please login to merge, or discard this patch.
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -30,8 +30,6 @@
 block discarded – undo
30 30
 
31 31
 namespace OC;
32 32
 
33
-use OC\App\AppStore\Bundles\BundleFetcher;
34
-use OC\Files\AppData\Factory;
35 33
 use OC\Repair\CleanTags;
36 34
 use OC\Repair\ClearFrontendCaches;
37 35
 use OC\Repair\Collation;
Please login to merge, or discard this patch.
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -55,167 +55,167 @@
 block discarded – undo
55 55
 use Symfony\Component\EventDispatcher\GenericEvent;
56 56
 
57 57
 class Repair implements IOutput{
58
-	/* @var IRepairStep[] */
59
-	private $repairSteps;
60
-	/** @var EventDispatcher */
61
-	private $dispatcher;
62
-	/** @var string */
63
-	private $currentStep;
64
-
65
-	/**
66
-	 * Creates a new repair step runner
67
-	 *
68
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
69
-	 * @param EventDispatcher $dispatcher
70
-	 */
71
-	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
72
-		$this->repairSteps = $repairSteps;
73
-		$this->dispatcher = $dispatcher;
74
-	}
75
-
76
-	/**
77
-	 * Run a series of repair steps for common problems
78
-	 */
79
-	public function run() {
80
-		if (count($this->repairSteps) === 0) {
81
-			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
82
-			return;
83
-		}
84
-		// run each repair step
85
-		foreach ($this->repairSteps as $step) {
86
-			$this->currentStep = $step->getName();
87
-			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
88
-			$step->run($this);
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * Add repair step
94
-	 *
95
-	 * @param IRepairStep|string $repairStep repair step
96
-	 * @throws \Exception
97
-	 */
98
-	public function addStep($repairStep) {
99
-		if (is_string($repairStep)) {
100
-			try {
101
-				$s = \OC::$server->query($repairStep);
102
-			} catch (QueryException $e) {
103
-				if (class_exists($repairStep)) {
104
-					$s = new $repairStep();
105
-				} else {
106
-					throw new \Exception("Repair step '$repairStep' is unknown");
107
-				}
108
-			}
109
-
110
-			if ($s instanceof IRepairStep) {
111
-				$this->repairSteps[] = $s;
112
-			} else {
113
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
114
-			}
115
-		} else {
116
-			$this->repairSteps[] = $repairStep;
117
-		}
118
-	}
119
-
120
-	/**
121
-	 * Returns the default repair steps to be run on the
122
-	 * command line or after an upgrade.
123
-	 *
124
-	 * @return IRepairStep[]
125
-	 */
126
-	public static function getRepairSteps() {
127
-		return [
128
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
129
-			new RepairMimeTypes(\OC::$server->getConfig()),
130
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
131
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
132
-			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
133
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
134
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
135
-			new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
136
-			new AddLogRotateJob(\OC::$server->getJobList()),
137
-			new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class))
138
-		];
139
-	}
140
-
141
-	/**
142
-	 * Returns expensive repair steps to be run on the
143
-	 * command line with a special option.
144
-	 *
145
-	 * @return IRepairStep[]
146
-	 */
147
-	public static function getExpensiveRepairSteps() {
148
-		return [
149
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
150
-		];
151
-	}
152
-
153
-	/**
154
-	 * Returns the repair steps to be run before an
155
-	 * upgrade.
156
-	 *
157
-	 * @return IRepairStep[]
158
-	 */
159
-	public static function getBeforeUpgradeRepairSteps() {
160
-		$connection = \OC::$server->getDatabaseConnection();
161
-		$config = \OC::$server->getConfig();
162
-		$steps = [
163
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
164
-			new SqliteAutoincrement($connection),
165
-			new SaveAccountsTableData($connection, $config),
166
-			new DropAccountTermsTable($connection),
167
-		];
168
-
169
-		return $steps;
170
-	}
171
-
172
-	/**
173
-	 * @param string $scope
174
-	 * @param string $method
175
-	 * @param array $arguments
176
-	 */
177
-	public function emit($scope, $method, array $arguments = []) {
178
-		if (!is_null($this->dispatcher)) {
179
-			$this->dispatcher->dispatch("$scope::$method",
180
-				new GenericEvent("$scope::$method", $arguments));
181
-		}
182
-	}
183
-
184
-	public function info($string) {
185
-		// for now just emit as we did in the past
186
-		$this->emit('\OC\Repair', 'info', array($string));
187
-	}
188
-
189
-	/**
190
-	 * @param string $message
191
-	 */
192
-	public function warning($message) {
193
-		// for now just emit as we did in the past
194
-		$this->emit('\OC\Repair', 'warning', [$message]);
195
-	}
196
-
197
-	/**
198
-	 * @param int $max
199
-	 */
200
-	public function startProgress($max = 0) {
201
-		// for now just emit as we did in the past
202
-		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
203
-	}
204
-
205
-	/**
206
-	 * @param int $step
207
-	 * @param string $description
208
-	 */
209
-	public function advance($step = 1, $description = '') {
210
-		// for now just emit as we did in the past
211
-		$this->emit('\OC\Repair', 'advance', [$step, $description]);
212
-	}
213
-
214
-	/**
215
-	 * @param int $max
216
-	 */
217
-	public function finishProgress() {
218
-		// for now just emit as we did in the past
219
-		$this->emit('\OC\Repair', 'finishProgress', []);
220
-	}
58
+    /* @var IRepairStep[] */
59
+    private $repairSteps;
60
+    /** @var EventDispatcher */
61
+    private $dispatcher;
62
+    /** @var string */
63
+    private $currentStep;
64
+
65
+    /**
66
+     * Creates a new repair step runner
67
+     *
68
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
69
+     * @param EventDispatcher $dispatcher
70
+     */
71
+    public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
72
+        $this->repairSteps = $repairSteps;
73
+        $this->dispatcher = $dispatcher;
74
+    }
75
+
76
+    /**
77
+     * Run a series of repair steps for common problems
78
+     */
79
+    public function run() {
80
+        if (count($this->repairSteps) === 0) {
81
+            $this->emit('\OC\Repair', 'info', array('No repair steps available'));
82
+            return;
83
+        }
84
+        // run each repair step
85
+        foreach ($this->repairSteps as $step) {
86
+            $this->currentStep = $step->getName();
87
+            $this->emit('\OC\Repair', 'step', [$this->currentStep]);
88
+            $step->run($this);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * Add repair step
94
+     *
95
+     * @param IRepairStep|string $repairStep repair step
96
+     * @throws \Exception
97
+     */
98
+    public function addStep($repairStep) {
99
+        if (is_string($repairStep)) {
100
+            try {
101
+                $s = \OC::$server->query($repairStep);
102
+            } catch (QueryException $e) {
103
+                if (class_exists($repairStep)) {
104
+                    $s = new $repairStep();
105
+                } else {
106
+                    throw new \Exception("Repair step '$repairStep' is unknown");
107
+                }
108
+            }
109
+
110
+            if ($s instanceof IRepairStep) {
111
+                $this->repairSteps[] = $s;
112
+            } else {
113
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
114
+            }
115
+        } else {
116
+            $this->repairSteps[] = $repairStep;
117
+        }
118
+    }
119
+
120
+    /**
121
+     * Returns the default repair steps to be run on the
122
+     * command line or after an upgrade.
123
+     *
124
+     * @return IRepairStep[]
125
+     */
126
+    public static function getRepairSteps() {
127
+        return [
128
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
129
+            new RepairMimeTypes(\OC::$server->getConfig()),
130
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
131
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
132
+            new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
133
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
134
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
135
+            new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
136
+            new AddLogRotateJob(\OC::$server->getJobList()),
137
+            new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class))
138
+        ];
139
+    }
140
+
141
+    /**
142
+     * Returns expensive repair steps to be run on the
143
+     * command line with a special option.
144
+     *
145
+     * @return IRepairStep[]
146
+     */
147
+    public static function getExpensiveRepairSteps() {
148
+        return [
149
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
150
+        ];
151
+    }
152
+
153
+    /**
154
+     * Returns the repair steps to be run before an
155
+     * upgrade.
156
+     *
157
+     * @return IRepairStep[]
158
+     */
159
+    public static function getBeforeUpgradeRepairSteps() {
160
+        $connection = \OC::$server->getDatabaseConnection();
161
+        $config = \OC::$server->getConfig();
162
+        $steps = [
163
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
164
+            new SqliteAutoincrement($connection),
165
+            new SaveAccountsTableData($connection, $config),
166
+            new DropAccountTermsTable($connection),
167
+        ];
168
+
169
+        return $steps;
170
+    }
171
+
172
+    /**
173
+     * @param string $scope
174
+     * @param string $method
175
+     * @param array $arguments
176
+     */
177
+    public function emit($scope, $method, array $arguments = []) {
178
+        if (!is_null($this->dispatcher)) {
179
+            $this->dispatcher->dispatch("$scope::$method",
180
+                new GenericEvent("$scope::$method", $arguments));
181
+        }
182
+    }
183
+
184
+    public function info($string) {
185
+        // for now just emit as we did in the past
186
+        $this->emit('\OC\Repair', 'info', array($string));
187
+    }
188
+
189
+    /**
190
+     * @param string $message
191
+     */
192
+    public function warning($message) {
193
+        // for now just emit as we did in the past
194
+        $this->emit('\OC\Repair', 'warning', [$message]);
195
+    }
196
+
197
+    /**
198
+     * @param int $max
199
+     */
200
+    public function startProgress($max = 0) {
201
+        // for now just emit as we did in the past
202
+        $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
203
+    }
204
+
205
+    /**
206
+     * @param int $step
207
+     * @param string $description
208
+     */
209
+    public function advance($step = 1, $description = '') {
210
+        // for now just emit as we did in the past
211
+        $this->emit('\OC\Repair', 'advance', [$step, $description]);
212
+    }
213
+
214
+    /**
215
+     * @param int $max
216
+     */
217
+    public function finishProgress() {
218
+        // for now just emit as we did in the past
219
+        $this->emit('\OC\Repair', 'finishProgress', []);
220
+    }
221 221
 }
Please login to merge, or discard this patch.
lib/private/Session/CryptoSessionData.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -176,7 +176,7 @@
 block discarded – undo
176 176
 
177 177
 	/**
178 178
 	 * @param mixed $offset
179
-	 * @return mixed
179
+	 * @return string|null
180 180
 	 */
181 181
 	public function offsetGet($offset) {
182 182
 		return $this->get($offset);
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
 	public function __destruct() {
67 67
 		try {
68 68
 			$this->close();
69
-		} catch (SessionNotAvailableException $e){
69
+		} catch (SessionNotAvailableException $e) {
70 70
 			// This exception can occur if session is already closed
71 71
 			// So it is safe to ignore it and let the garbage collector to proceed
72 72
 		}
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
 	 * @return string|null Either the value or null
103 103
 	 */
104 104
 	public function get($key) {
105
-		if(isset($this->sessionValues[$key])) {
105
+		if (isset($this->sessionValues[$key])) {
106 106
 			return $this->sessionValues[$key];
107 107
 		}
108 108
 
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 	 * Close the session and release the lock, also writes all changed data in batch
169 169
 	 */
170 170
 	public function close() {
171
-		if($this->isModified) {
171
+		if ($this->isModified) {
172 172
 			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
173 173
 			$this->session->set(self::encryptedSessionName, $encryptedValue);
174 174
 			$this->isModified = false;
Please login to merge, or discard this patch.
Indentation   +172 added lines, -172 removed lines patch added patch discarded remove patch
@@ -36,176 +36,176 @@
 block discarded – undo
36 36
  * @package OC\Session
37 37
  */
38 38
 class CryptoSessionData implements \ArrayAccess, ISession {
39
-	/** @var ISession */
40
-	protected $session;
41
-	/** @var \OCP\Security\ICrypto */
42
-	protected $crypto;
43
-	/** @var string */
44
-	protected $passphrase;
45
-	/** @var array */
46
-	protected $sessionValues;
47
-	/** @var bool */
48
-	protected $isModified = false;
49
-	CONST encryptedSessionName = 'encrypted_session_data';
50
-
51
-	/**
52
-	 * @param ISession $session
53
-	 * @param ICrypto $crypto
54
-	 * @param string $passphrase
55
-	 */
56
-	public function __construct(ISession $session,
57
-								ICrypto $crypto,
58
-								$passphrase) {
59
-		$this->crypto = $crypto;
60
-		$this->session = $session;
61
-		$this->passphrase = $passphrase;
62
-		$this->initializeSession();
63
-	}
64
-
65
-	/**
66
-	 * Close session if class gets destructed
67
-	 */
68
-	public function __destruct() {
69
-		try {
70
-			$this->close();
71
-		} catch (SessionNotAvailableException $e){
72
-			// This exception can occur if session is already closed
73
-			// So it is safe to ignore it and let the garbage collector to proceed
74
-		}
75
-	}
76
-
77
-	protected function initializeSession() {
78
-		$encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
79
-		try {
80
-			$this->sessionValues = json_decode(
81
-				$this->crypto->decrypt($encryptedSessionData, $this->passphrase),
82
-				true
83
-			);
84
-		} catch (\Exception $e) {
85
-			$this->sessionValues = [];
86
-		}
87
-	}
88
-
89
-	/**
90
-	 * Set a value in the session
91
-	 *
92
-	 * @param string $key
93
-	 * @param mixed $value
94
-	 */
95
-	public function set($key, $value) {
96
-		$this->sessionValues[$key] = $value;
97
-		$this->isModified = true;
98
-	}
99
-
100
-	/**
101
-	 * Get a value from the session
102
-	 *
103
-	 * @param string $key
104
-	 * @return string|null Either the value or null
105
-	 */
106
-	public function get($key) {
107
-		if(isset($this->sessionValues[$key])) {
108
-			return $this->sessionValues[$key];
109
-		}
110
-
111
-		return null;
112
-	}
113
-
114
-	/**
115
-	 * Check if a named key exists in the session
116
-	 *
117
-	 * @param string $key
118
-	 * @return bool
119
-	 */
120
-	public function exists($key) {
121
-		return isset($this->sessionValues[$key]);
122
-	}
123
-
124
-	/**
125
-	 * Remove a $key/$value pair from the session
126
-	 *
127
-	 * @param string $key
128
-	 */
129
-	public function remove($key) {
130
-		$this->isModified = true;
131
-		unset($this->sessionValues[$key]);
132
-		$this->session->remove(self::encryptedSessionName);
133
-	}
134
-
135
-	/**
136
-	 * Reset and recreate the session
137
-	 */
138
-	public function clear() {
139
-		$requesttoken = $this->get('requesttoken');
140
-		$this->sessionValues = [];
141
-		if ($requesttoken !== null) {
142
-			$this->set('requesttoken', $requesttoken);
143
-		}
144
-		$this->isModified = true;
145
-		$this->session->clear();
146
-	}
147
-
148
-	/**
149
-	 * Wrapper around session_regenerate_id
150
-	 *
151
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
152
-	 * @return void
153
-	 */
154
-	public function regenerateId($deleteOldSession = true) {
155
-		$this->session->regenerateId($deleteOldSession);
156
-	}
157
-
158
-	/**
159
-	 * Wrapper around session_id
160
-	 *
161
-	 * @return string
162
-	 * @throws SessionNotAvailableException
163
-	 * @since 9.1.0
164
-	 */
165
-	public function getId() {
166
-		return $this->session->getId();
167
-	}
168
-
169
-	/**
170
-	 * Close the session and release the lock, also writes all changed data in batch
171
-	 */
172
-	public function close() {
173
-		if($this->isModified) {
174
-			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
175
-			$this->session->set(self::encryptedSessionName, $encryptedValue);
176
-			$this->isModified = false;
177
-		}
178
-		$this->session->close();
179
-	}
180
-
181
-	/**
182
-	 * @param mixed $offset
183
-	 * @return bool
184
-	 */
185
-	public function offsetExists($offset) {
186
-		return $this->exists($offset);
187
-	}
188
-
189
-	/**
190
-	 * @param mixed $offset
191
-	 * @return mixed
192
-	 */
193
-	public function offsetGet($offset) {
194
-		return $this->get($offset);
195
-	}
196
-
197
-	/**
198
-	 * @param mixed $offset
199
-	 * @param mixed $value
200
-	 */
201
-	public function offsetSet($offset, $value) {
202
-		$this->set($offset, $value);
203
-	}
204
-
205
-	/**
206
-	 * @param mixed $offset
207
-	 */
208
-	public function offsetUnset($offset) {
209
-		$this->remove($offset);
210
-	}
39
+    /** @var ISession */
40
+    protected $session;
41
+    /** @var \OCP\Security\ICrypto */
42
+    protected $crypto;
43
+    /** @var string */
44
+    protected $passphrase;
45
+    /** @var array */
46
+    protected $sessionValues;
47
+    /** @var bool */
48
+    protected $isModified = false;
49
+    CONST encryptedSessionName = 'encrypted_session_data';
50
+
51
+    /**
52
+     * @param ISession $session
53
+     * @param ICrypto $crypto
54
+     * @param string $passphrase
55
+     */
56
+    public function __construct(ISession $session,
57
+                                ICrypto $crypto,
58
+                                $passphrase) {
59
+        $this->crypto = $crypto;
60
+        $this->session = $session;
61
+        $this->passphrase = $passphrase;
62
+        $this->initializeSession();
63
+    }
64
+
65
+    /**
66
+     * Close session if class gets destructed
67
+     */
68
+    public function __destruct() {
69
+        try {
70
+            $this->close();
71
+        } catch (SessionNotAvailableException $e){
72
+            // This exception can occur if session is already closed
73
+            // So it is safe to ignore it and let the garbage collector to proceed
74
+        }
75
+    }
76
+
77
+    protected function initializeSession() {
78
+        $encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
79
+        try {
80
+            $this->sessionValues = json_decode(
81
+                $this->crypto->decrypt($encryptedSessionData, $this->passphrase),
82
+                true
83
+            );
84
+        } catch (\Exception $e) {
85
+            $this->sessionValues = [];
86
+        }
87
+    }
88
+
89
+    /**
90
+     * Set a value in the session
91
+     *
92
+     * @param string $key
93
+     * @param mixed $value
94
+     */
95
+    public function set($key, $value) {
96
+        $this->sessionValues[$key] = $value;
97
+        $this->isModified = true;
98
+    }
99
+
100
+    /**
101
+     * Get a value from the session
102
+     *
103
+     * @param string $key
104
+     * @return string|null Either the value or null
105
+     */
106
+    public function get($key) {
107
+        if(isset($this->sessionValues[$key])) {
108
+            return $this->sessionValues[$key];
109
+        }
110
+
111
+        return null;
112
+    }
113
+
114
+    /**
115
+     * Check if a named key exists in the session
116
+     *
117
+     * @param string $key
118
+     * @return bool
119
+     */
120
+    public function exists($key) {
121
+        return isset($this->sessionValues[$key]);
122
+    }
123
+
124
+    /**
125
+     * Remove a $key/$value pair from the session
126
+     *
127
+     * @param string $key
128
+     */
129
+    public function remove($key) {
130
+        $this->isModified = true;
131
+        unset($this->sessionValues[$key]);
132
+        $this->session->remove(self::encryptedSessionName);
133
+    }
134
+
135
+    /**
136
+     * Reset and recreate the session
137
+     */
138
+    public function clear() {
139
+        $requesttoken = $this->get('requesttoken');
140
+        $this->sessionValues = [];
141
+        if ($requesttoken !== null) {
142
+            $this->set('requesttoken', $requesttoken);
143
+        }
144
+        $this->isModified = true;
145
+        $this->session->clear();
146
+    }
147
+
148
+    /**
149
+     * Wrapper around session_regenerate_id
150
+     *
151
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
152
+     * @return void
153
+     */
154
+    public function regenerateId($deleteOldSession = true) {
155
+        $this->session->regenerateId($deleteOldSession);
156
+    }
157
+
158
+    /**
159
+     * Wrapper around session_id
160
+     *
161
+     * @return string
162
+     * @throws SessionNotAvailableException
163
+     * @since 9.1.0
164
+     */
165
+    public function getId() {
166
+        return $this->session->getId();
167
+    }
168
+
169
+    /**
170
+     * Close the session and release the lock, also writes all changed data in batch
171
+     */
172
+    public function close() {
173
+        if($this->isModified) {
174
+            $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
175
+            $this->session->set(self::encryptedSessionName, $encryptedValue);
176
+            $this->isModified = false;
177
+        }
178
+        $this->session->close();
179
+    }
180
+
181
+    /**
182
+     * @param mixed $offset
183
+     * @return bool
184
+     */
185
+    public function offsetExists($offset) {
186
+        return $this->exists($offset);
187
+    }
188
+
189
+    /**
190
+     * @param mixed $offset
191
+     * @return mixed
192
+     */
193
+    public function offsetGet($offset) {
194
+        return $this->get($offset);
195
+    }
196
+
197
+    /**
198
+     * @param mixed $offset
199
+     * @param mixed $value
200
+     */
201
+    public function offsetSet($offset, $value) {
202
+        $this->set($offset, $value);
203
+    }
204
+
205
+    /**
206
+     * @param mixed $offset
207
+     */
208
+    public function offsetUnset($offset) {
209
+        $this->remove($offset);
210
+    }
211 211
 }
Please login to merge, or discard this patch.
lib/private/Tags.php 3 patches
Doc Comments   +10 added lines, -2 removed lines patch added patch discarded remove patch
@@ -742,11 +742,19 @@  discard block
 block discarded – undo
742 742
 	}
743 743
 
744 744
 	// case-insensitive array_search
745
+
746
+	/**
747
+	 * @param string $needle
748
+	 */
745 749
 	protected function array_searchi($needle, $haystack, $mem='getName') {
746 750
 		if(!is_array($haystack)) {
747 751
 			return false;
748 752
 		}
749 753
 		return array_search(strtolower($needle), array_map(
754
+
755
+			/**
756
+			 * @param string $tag
757
+			 */
750 758
 			function($tag) use($mem) {
751 759
 				return strtolower(call_user_func(array($tag, $mem)));
752 760
 			}, $haystack)
@@ -771,7 +779,7 @@  discard block
 block discarded – undo
771 779
 	* Get a tag by its name.
772 780
 	*
773 781
 	* @param string $name The tag name.
774
-	* @return integer|bool The tag object's offset within the $this->tags
782
+	* @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags
775 783
 	*                      array or false if it doesn't exist.
776 784
 	*/
777 785
 	private function getTagByName($name) {
@@ -782,7 +790,7 @@  discard block
 block discarded – undo
782 790
 	* Get a tag by its ID.
783 791
 	*
784 792
 	* @param string $id The tag ID to look for.
785
-	* @return integer|bool The tag object's offset within the $this->tags
793
+	* @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags
786 794
 	*                      array or false if it doesn't exist.
787 795
 	*/
788 796
 	private function getTagById($id) {
Please login to merge, or discard this patch.
Indentation   +802 added lines, -802 removed lines patch added patch discarded remove patch
@@ -50,806 +50,806 @@
 block discarded – undo
50 50
 
51 51
 class Tags implements \OCP\ITags {
52 52
 
53
-	/**
54
-	 * Tags
55
-	 *
56
-	 * @var array
57
-	 */
58
-	private $tags = array();
59
-
60
-	/**
61
-	 * Used for storing objectid/categoryname pairs while rescanning.
62
-	 *
63
-	 * @var array
64
-	 */
65
-	private static $relations = array();
66
-
67
-	/**
68
-	 * Type
69
-	 *
70
-	 * @var string
71
-	 */
72
-	private $type;
73
-
74
-	/**
75
-	 * User
76
-	 *
77
-	 * @var string
78
-	 */
79
-	private $user;
80
-
81
-	/**
82
-	 * Are we including tags for shared items?
83
-	 *
84
-	 * @var bool
85
-	 */
86
-	private $includeShared = false;
87
-
88
-	/**
89
-	 * The current user, plus any owners of the items shared with the current
90
-	 * user, if $this->includeShared === true.
91
-	 *
92
-	 * @var array
93
-	 */
94
-	private $owners = array();
95
-
96
-	/**
97
-	 * The Mapper we're using to communicate our Tag objects to the database.
98
-	 *
99
-	 * @var TagMapper
100
-	 */
101
-	private $mapper;
102
-
103
-	/**
104
-	 * The sharing backend for objects of $this->type. Required if
105
-	 * $this->includeShared === true to determine ownership of items.
106
-	 *
107
-	 * @var \OCP\Share_Backend
108
-	 */
109
-	private $backend;
110
-
111
-	const TAG_TABLE = '*PREFIX*vcategory';
112
-	const RELATION_TABLE = '*PREFIX*vcategory_to_object';
113
-
114
-	const TAG_FAVORITE = '_$!<Favorite>!$_';
115
-
116
-	/**
117
-	* Constructor.
118
-	*
119
-	* @param TagMapper $mapper Instance of the TagMapper abstraction layer.
120
-	* @param string $user The user whose data the object will operate on.
121
-	* @param string $type The type of items for which tags will be loaded.
122
-	* @param array $defaultTags Tags that should be created at construction.
123
-	* @param boolean $includeShared Whether to include tags for items shared with this user by others.
124
-	*/
125
-	public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) {
126
-		$this->mapper = $mapper;
127
-		$this->user = $user;
128
-		$this->type = $type;
129
-		$this->includeShared = $includeShared;
130
-		$this->owners = array($this->user);
131
-		if ($this->includeShared) {
132
-			$this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true));
133
-			$this->backend = \OC\Share\Share::getBackend($this->type);
134
-		}
135
-		$this->tags = $this->mapper->loadTags($this->owners, $this->type);
136
-
137
-		if(count($defaultTags) > 0 && count($this->tags) === 0) {
138
-			$this->addMultiple($defaultTags, true);
139
-		}
140
-	}
141
-
142
-	/**
143
-	* Check if any tags are saved for this type and user.
144
-	*
145
-	* @return boolean
146
-	*/
147
-	public function isEmpty() {
148
-		return count($this->tags) === 0;
149
-	}
150
-
151
-	/**
152
-	* Returns an array mapping a given tag's properties to its values:
153
-	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
154
-	*
155
-	* @param string $id The ID of the tag that is going to be mapped
156
-	* @return array|false
157
-	*/
158
-	public function getTag($id) {
159
-		$key = $this->getTagById($id);
160
-		if ($key !== false) {
161
-			return $this->tagMap($this->tags[$key]);
162
-		}
163
-		return false;
164
-	}
165
-
166
-	/**
167
-	* Get the tags for a specific user.
168
-	*
169
-	* This returns an array with maps containing each tag's properties:
170
-	* [
171
-	* 	['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
172
-	* 	['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
173
-	* ]
174
-	*
175
-	* @return array
176
-	*/
177
-	public function getTags() {
178
-		if(!count($this->tags)) {
179
-			return array();
180
-		}
181
-
182
-		usort($this->tags, function($a, $b) {
183
-			return strnatcasecmp($a->getName(), $b->getName());
184
-		});
185
-		$tagMap = array();
186
-
187
-		foreach($this->tags as $tag) {
188
-			if($tag->getName() !== self::TAG_FAVORITE) {
189
-				$tagMap[] = $this->tagMap($tag);
190
-			}
191
-		}
192
-		return $tagMap;
193
-
194
-	}
195
-
196
-	/**
197
-	* Return only the tags owned by the given user, omitting any tags shared
198
-	* by other users.
199
-	*
200
-	* @param string $user The user whose tags are to be checked.
201
-	* @return array An array of Tag objects.
202
-	*/
203
-	public function getTagsForUser($user) {
204
-		return array_filter($this->tags,
205
-			function($tag) use($user) {
206
-				return $tag->getOwner() === $user;
207
-			}
208
-		);
209
-	}
210
-
211
-	/**
212
-	 * Get the list of tags for the given ids.
213
-	 *
214
-	 * @param array $objIds array of object ids
215
-	 * @return array|boolean of tags id as key to array of tag names
216
-	 * or false if an error occurred
217
-	 */
218
-	public function getTagsForObjects(array $objIds) {
219
-		$entries = array();
220
-
221
-		try {
222
-			$conn = \OC::$server->getDatabaseConnection();
223
-			$chunks = array_chunk($objIds, 900, false);
224
-			foreach ($chunks as $chunk) {
225
-				$result = $conn->executeQuery(
226
-					'SELECT `category`, `categoryid`, `objid` ' .
227
-					'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
228
-					'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
229
-					array($this->user, $this->type, $chunk),
230
-					array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
231
-				);
232
-				while ($row = $result->fetch()) {
233
-					$objId = (int)$row['objid'];
234
-					if (!isset($entries[$objId])) {
235
-						$entries[$objId] = array();
236
-					}
237
-					$entries[$objId][] = $row['category'];
238
-				}
239
-				if (\OCP\DB::isError($result)) {
240
-					\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
241
-					return false;
242
-				}
243
-			}
244
-		} catch(\Exception $e) {
245
-			\OC::$server->getLogger()->logException($e, [
246
-				'message' => __METHOD__,
247
-				'level' => \OCP\Util::ERROR,
248
-				'app' => 'core',
249
-			]);
250
-			return false;
251
-		}
252
-
253
-		return $entries;
254
-	}
255
-
256
-	/**
257
-	* Get the a list if items tagged with $tag.
258
-	*
259
-	* Throws an exception if the tag could not be found.
260
-	*
261
-	* @param string $tag Tag id or name.
262
-	* @return array|false An array of object ids or false on error.
263
-	* @throws \Exception
264
-	*/
265
-	public function getIdsForTag($tag) {
266
-		$result = null;
267
-		$tagId = false;
268
-		if(is_numeric($tag)) {
269
-			$tagId = $tag;
270
-		} elseif(is_string($tag)) {
271
-			$tag = trim($tag);
272
-			if($tag === '') {
273
-				\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
274
-				return false;
275
-			}
276
-			$tagId = $this->getTagId($tag);
277
-		}
278
-
279
-		if($tagId === false) {
280
-			$l10n = \OC::$server->getL10N('core');
281
-			throw new \Exception(
282
-				$l10n->t('Could not find category "%s"', [$tag])
283
-			);
284
-		}
285
-
286
-		$ids = array();
287
-		$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
288
-			. '` WHERE `categoryid` = ?';
289
-
290
-		try {
291
-			$stmt = \OCP\DB::prepare($sql);
292
-			$result = $stmt->execute(array($tagId));
293
-			if (\OCP\DB::isError($result)) {
294
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
295
-				return false;
296
-			}
297
-		} catch(\Exception $e) {
298
-			\OC::$server->getLogger()->logException($e, [
299
-				'message' => __METHOD__,
300
-				'level' => \OCP\Util::ERROR,
301
-				'app' => 'core',
302
-			]);
303
-			return false;
304
-		}
305
-
306
-		if(!is_null($result)) {
307
-			while( $row = $result->fetchRow()) {
308
-				$id = (int)$row['objid'];
309
-
310
-				if ($this->includeShared) {
311
-					// We have to check if we are really allowed to access the
312
-					// items that are tagged with $tag. To that end, we ask the
313
-					// corresponding sharing backend if the item identified by $id
314
-					// is owned by any of $this->owners.
315
-					foreach ($this->owners as $owner) {
316
-						if ($this->backend->isValidSource($id, $owner)) {
317
-							$ids[] = $id;
318
-							break;
319
-						}
320
-					}
321
-				} else {
322
-					$ids[] = $id;
323
-				}
324
-			}
325
-		}
326
-
327
-		return $ids;
328
-	}
329
-
330
-	/**
331
-	* Checks whether a tag is saved for the given user,
332
-	* disregarding the ones shared with him or her.
333
-	*
334
-	* @param string $name The tag name to check for.
335
-	* @param string $user The user whose tags are to be checked.
336
-	* @return bool
337
-	*/
338
-	public function userHasTag($name, $user) {
339
-		$key = $this->array_searchi($name, $this->getTagsForUser($user));
340
-		return ($key !== false) ? $this->tags[$key]->getId() : false;
341
-	}
342
-
343
-	/**
344
-	* Checks whether a tag is saved for or shared with the current user.
345
-	*
346
-	* @param string $name The tag name to check for.
347
-	* @return bool
348
-	*/
349
-	public function hasTag($name) {
350
-		return $this->getTagId($name) !== false;
351
-	}
352
-
353
-	/**
354
-	* Add a new tag.
355
-	*
356
-	* @param string $name A string with a name of the tag
357
-	* @return false|int the id of the added tag or false on error.
358
-	*/
359
-	public function add($name) {
360
-		$name = trim($name);
361
-
362
-		if($name === '') {
363
-			\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
364
-			return false;
365
-		}
366
-		if($this->userHasTag($name, $this->user)) {
367
-			\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
368
-			return false;
369
-		}
370
-		try {
371
-			$tag = new Tag($this->user, $this->type, $name);
372
-			$tag = $this->mapper->insert($tag);
373
-			$this->tags[] = $tag;
374
-		} catch(\Exception $e) {
375
-			\OC::$server->getLogger()->logException($e, [
376
-				'message' => __METHOD__,
377
-				'level' => \OCP\Util::ERROR,
378
-				'app' => 'core',
379
-			]);
380
-			return false;
381
-		}
382
-		\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
383
-		return $tag->getId();
384
-	}
385
-
386
-	/**
387
-	* Rename tag.
388
-	*
389
-	* @param string|integer $from The name or ID of the existing tag
390
-	* @param string $to The new name of the tag.
391
-	* @return bool
392
-	*/
393
-	public function rename($from, $to) {
394
-		$from = trim($from);
395
-		$to = trim($to);
396
-
397
-		if($to === '' || $from === '') {
398
-			\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
399
-			return false;
400
-		}
401
-
402
-		if (is_numeric($from)) {
403
-			$key = $this->getTagById($from);
404
-		} else {
405
-			$key = $this->getTagByName($from);
406
-		}
407
-		if($key === false) {
408
-			\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
409
-			return false;
410
-		}
411
-		$tag = $this->tags[$key];
412
-
413
-		if($this->userHasTag($to, $tag->getOwner())) {
414
-			\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
415
-			return false;
416
-		}
417
-
418
-		try {
419
-			$tag->setName($to);
420
-			$this->tags[$key] = $this->mapper->update($tag);
421
-		} catch(\Exception $e) {
422
-			\OC::$server->getLogger()->logException($e, [
423
-				'message' => __METHOD__,
424
-				'level' => \OCP\Util::ERROR,
425
-				'app' => 'core',
426
-			]);
427
-			return false;
428
-		}
429
-		return true;
430
-	}
431
-
432
-	/**
433
-	* Add a list of new tags.
434
-	*
435
-	* @param string[] $names A string with a name or an array of strings containing
436
-	* the name(s) of the tag(s) to add.
437
-	* @param bool $sync When true, save the tags
438
-	* @param int|null $id int Optional object id to add to this|these tag(s)
439
-	* @return bool Returns false on error.
440
-	*/
441
-	public function addMultiple($names, $sync=false, $id = null) {
442
-		if(!is_array($names)) {
443
-			$names = array($names);
444
-		}
445
-		$names = array_map('trim', $names);
446
-		array_filter($names);
447
-
448
-		$newones = array();
449
-		foreach($names as $name) {
450
-			if(!$this->hasTag($name) && $name !== '') {
451
-				$newones[] = new Tag($this->user, $this->type, $name);
452
-			}
453
-			if(!is_null($id) ) {
454
-				// Insert $objectid, $categoryid  pairs if not exist.
455
-				self::$relations[] = array('objid' => $id, 'tag' => $name);
456
-			}
457
-		}
458
-		$this->tags = array_merge($this->tags, $newones);
459
-		if($sync === true) {
460
-			$this->save();
461
-		}
462
-
463
-		return true;
464
-	}
465
-
466
-	/**
467
-	 * Save the list of tags and their object relations
468
-	 */
469
-	protected function save() {
470
-		if(is_array($this->tags)) {
471
-			foreach($this->tags as $tag) {
472
-				try {
473
-					if (!$this->mapper->tagExists($tag)) {
474
-						$this->mapper->insert($tag);
475
-					}
476
-				} catch(\Exception $e) {
477
-					\OC::$server->getLogger()->logException($e, [
478
-						'message' => __METHOD__,
479
-						'level' => \OCP\Util::ERROR,
480
-						'app' => 'core',
481
-					]);
482
-				}
483
-			}
484
-
485
-			// reload tags to get the proper ids.
486
-			$this->tags = $this->mapper->loadTags($this->owners, $this->type);
487
-			\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
488
-				\OCP\Util::DEBUG);
489
-			// Loop through temporarily cached objectid/tagname pairs
490
-			// and save relations.
491
-			$tags = $this->tags;
492
-			// For some reason this is needed or array_search(i) will return 0..?
493
-			ksort($tags);
494
-			foreach(self::$relations as $relation) {
495
-				$tagId = $this->getTagId($relation['tag']);
496
-				\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
497
-				if($tagId) {
498
-					try {
499
-						\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
500
-							array(
501
-								'objid' => $relation['objid'],
502
-								'categoryid' => $tagId,
503
-								'type' => $this->type,
504
-								));
505
-					} catch(\Exception $e) {
506
-						\OC::$server->getLogger()->logException($e, [
507
-							'message' => __METHOD__,
508
-							'level' => \OCP\Util::ERROR,
509
-							'app' => 'core',
510
-						]);
511
-					}
512
-				}
513
-			}
514
-			self::$relations = array(); // reset
515
-		} else {
516
-			\OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
517
-				. print_r($this->tags, true), \OCP\Util::ERROR);
518
-		}
519
-	}
520
-
521
-	/**
522
-	* Delete tags and tag/object relations for a user.
523
-	*
524
-	* For hooking up on post_deleteUser
525
-	*
526
-	* @param array $arguments
527
-	*/
528
-	public static function post_deleteUser($arguments) {
529
-		// Find all objectid/tagId pairs.
530
-		$result = null;
531
-		try {
532
-			$stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
533
-				. 'WHERE `uid` = ?');
534
-			$result = $stmt->execute(array($arguments['uid']));
535
-			if (\OCP\DB::isError($result)) {
536
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
537
-			}
538
-		} catch(\Exception $e) {
539
-			\OC::$server->getLogger()->logException($e, [
540
-				'message' => __METHOD__,
541
-				'level' => \OCP\Util::ERROR,
542
-				'app' => 'core',
543
-			]);
544
-		}
545
-
546
-		if(!is_null($result)) {
547
-			try {
548
-				$stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
549
-					. 'WHERE `categoryid` = ?');
550
-				while( $row = $result->fetchRow()) {
551
-					try {
552
-						$stmt->execute(array($row['id']));
553
-					} catch(\Exception $e) {
554
-						\OC::$server->getLogger()->logException($e, [
555
-							'message' => __METHOD__,
556
-							'level' => \OCP\Util::ERROR,
557
-							'app' => 'core',
558
-						]);
559
-					}
560
-				}
561
-			} catch(\Exception $e) {
562
-				\OC::$server->getLogger()->logException($e, [
563
-					'message' => __METHOD__,
564
-					'level' => \OCP\Util::ERROR,
565
-					'app' => 'core',
566
-				]);
567
-			}
568
-		}
569
-		try {
570
-			$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
571
-				. 'WHERE `uid` = ?');
572
-			$result = $stmt->execute(array($arguments['uid']));
573
-			if (\OCP\DB::isError($result)) {
574
-				\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
575
-			}
576
-		} catch(\Exception $e) {
577
-			\OC::$server->getLogger()->logException($e, [
578
-				'message' => __METHOD__,
579
-				'level' => \OCP\Util::ERROR,
580
-				'app' => 'core',
581
-			]);
582
-		}
583
-	}
584
-
585
-	/**
586
-	* Delete tag/object relations from the db
587
-	*
588
-	* @param array $ids The ids of the objects
589
-	* @return boolean Returns false on error.
590
-	*/
591
-	public function purgeObjects(array $ids) {
592
-		if(count($ids) === 0) {
593
-			// job done ;)
594
-			return true;
595
-		}
596
-		$updates = $ids;
597
-		try {
598
-			$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
599
-			$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
600
-			$query .= 'AND `type`= ?';
601
-			$updates[] = $this->type;
602
-			$stmt = \OCP\DB::prepare($query);
603
-			$result = $stmt->execute($updates);
604
-			if (\OCP\DB::isError($result)) {
605
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
606
-				return false;
607
-			}
608
-		} catch(\Exception $e) {
609
-			\OC::$server->getLogger()->logException($e, [
610
-				'message' => __METHOD__,
611
-				'level' => \OCP\Util::ERROR,
612
-				'app' => 'core',
613
-			]);
614
-			return false;
615
-		}
616
-		return true;
617
-	}
618
-
619
-	/**
620
-	* Get favorites for an object type
621
-	*
622
-	* @return array|false An array of object ids.
623
-	*/
624
-	public function getFavorites() {
625
-		try {
626
-			return $this->getIdsForTag(self::TAG_FAVORITE);
627
-		} catch(\Exception $e) {
628
-			\OC::$server->getLogger()->logException($e, [
629
-				'message' => __METHOD__,
630
-				'level' => \OCP\Util::ERROR,
631
-				'app' => 'core',
632
-			]);
633
-			return array();
634
-		}
635
-	}
636
-
637
-	/**
638
-	* Add an object to favorites
639
-	*
640
-	* @param int $objid The id of the object
641
-	* @return boolean
642
-	*/
643
-	public function addToFavorites($objid) {
644
-		if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
645
-			$this->add(self::TAG_FAVORITE);
646
-		}
647
-		return $this->tagAs($objid, self::TAG_FAVORITE);
648
-	}
649
-
650
-	/**
651
-	* Remove an object from favorites
652
-	*
653
-	* @param int $objid The id of the object
654
-	* @return boolean
655
-	*/
656
-	public function removeFromFavorites($objid) {
657
-		return $this->unTag($objid, self::TAG_FAVORITE);
658
-	}
659
-
660
-	/**
661
-	* Creates a tag/object relation.
662
-	*
663
-	* @param int $objid The id of the object
664
-	* @param string $tag The id or name of the tag
665
-	* @return boolean Returns false on error.
666
-	*/
667
-	public function tagAs($objid, $tag) {
668
-		if(is_string($tag) && !is_numeric($tag)) {
669
-			$tag = trim($tag);
670
-			if($tag === '') {
671
-				\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
672
-				return false;
673
-			}
674
-			if(!$this->hasTag($tag)) {
675
-				$this->add($tag);
676
-			}
677
-			$tagId =  $this->getTagId($tag);
678
-		} else {
679
-			$tagId = $tag;
680
-		}
681
-		try {
682
-			\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
683
-				array(
684
-					'objid' => $objid,
685
-					'categoryid' => $tagId,
686
-					'type' => $this->type,
687
-				));
688
-		} catch(\Exception $e) {
689
-			\OC::$server->getLogger()->logException($e, [
690
-				'message' => __METHOD__,
691
-				'level' => \OCP\Util::ERROR,
692
-				'app' => 'core',
693
-			]);
694
-			return false;
695
-		}
696
-		return true;
697
-	}
698
-
699
-	/**
700
-	* Delete single tag/object relation from the db
701
-	*
702
-	* @param int $objid The id of the object
703
-	* @param string $tag The id or name of the tag
704
-	* @return boolean
705
-	*/
706
-	public function unTag($objid, $tag) {
707
-		if(is_string($tag) && !is_numeric($tag)) {
708
-			$tag = trim($tag);
709
-			if($tag === '') {
710
-				\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
711
-				return false;
712
-			}
713
-			$tagId =  $this->getTagId($tag);
714
-		} else {
715
-			$tagId = $tag;
716
-		}
717
-
718
-		try {
719
-			$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
720
-					. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
721
-			$stmt = \OCP\DB::prepare($sql);
722
-			$stmt->execute(array($objid, $tagId, $this->type));
723
-		} catch(\Exception $e) {
724
-			\OC::$server->getLogger()->logException($e, [
725
-				'message' => __METHOD__,
726
-				'level' => \OCP\Util::ERROR,
727
-				'app' => 'core',
728
-			]);
729
-			return false;
730
-		}
731
-		return true;
732
-	}
733
-
734
-	/**
735
-	* Delete tags from the database.
736
-	*
737
-	* @param string[]|integer[] $names An array of tags (names or IDs) to delete
738
-	* @return bool Returns false on error
739
-	*/
740
-	public function delete($names) {
741
-		if(!is_array($names)) {
742
-			$names = array($names);
743
-		}
744
-
745
-		$names = array_map('trim', $names);
746
-		array_filter($names);
747
-
748
-		\OCP\Util::writeLog('core', __METHOD__ . ', before: '
749
-			. print_r($this->tags, true), \OCP\Util::DEBUG);
750
-		foreach($names as $name) {
751
-			$id = null;
752
-
753
-			if (is_numeric($name)) {
754
-				$key = $this->getTagById($name);
755
-			} else {
756
-				$key = $this->getTagByName($name);
757
-			}
758
-			if ($key !== false) {
759
-				$tag = $this->tags[$key];
760
-				$id = $tag->getId();
761
-				unset($this->tags[$key]);
762
-				$this->mapper->delete($tag);
763
-			} else {
764
-				\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
765
-					. ': not found.', \OCP\Util::ERROR);
766
-			}
767
-			if(!is_null($id) && $id !== false) {
768
-				try {
769
-					$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
770
-							. 'WHERE `categoryid` = ?';
771
-					$stmt = \OCP\DB::prepare($sql);
772
-					$result = $stmt->execute(array($id));
773
-					if (\OCP\DB::isError($result)) {
774
-						\OCP\Util::writeLog('core',
775
-							__METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
776
-							\OCP\Util::ERROR);
777
-						return false;
778
-					}
779
-				} catch(\Exception $e) {
780
-					\OC::$server->getLogger()->logException($e, [
781
-						'message' => __METHOD__,
782
-						'level' => \OCP\Util::ERROR,
783
-						'app' => 'core',
784
-					]);
785
-					return false;
786
-				}
787
-			}
788
-		}
789
-		return true;
790
-	}
791
-
792
-	// case-insensitive array_search
793
-	protected function array_searchi($needle, $haystack, $mem='getName') {
794
-		if(!is_array($haystack)) {
795
-			return false;
796
-		}
797
-		return array_search(strtolower($needle), array_map(
798
-			function($tag) use($mem) {
799
-				return strtolower(call_user_func(array($tag, $mem)));
800
-			}, $haystack)
801
-		);
802
-	}
803
-
804
-	/**
805
-	* Get a tag's ID.
806
-	*
807
-	* @param string $name The tag name to look for.
808
-	* @return string|bool The tag's id or false if no matching tag is found.
809
-	*/
810
-	private function getTagId($name) {
811
-		$key = $this->array_searchi($name, $this->tags);
812
-		if ($key !== false) {
813
-			return $this->tags[$key]->getId();
814
-		}
815
-		return false;
816
-	}
817
-
818
-	/**
819
-	* Get a tag by its name.
820
-	*
821
-	* @param string $name The tag name.
822
-	* @return integer|bool The tag object's offset within the $this->tags
823
-	*                      array or false if it doesn't exist.
824
-	*/
825
-	private function getTagByName($name) {
826
-		return $this->array_searchi($name, $this->tags, 'getName');
827
-	}
828
-
829
-	/**
830
-	* Get a tag by its ID.
831
-	*
832
-	* @param string $id The tag ID to look for.
833
-	* @return integer|bool The tag object's offset within the $this->tags
834
-	*                      array or false if it doesn't exist.
835
-	*/
836
-	private function getTagById($id) {
837
-		return $this->array_searchi($id, $this->tags, 'getId');
838
-	}
839
-
840
-	/**
841
-	* Returns an array mapping a given tag's properties to its values:
842
-	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
843
-	*
844
-	* @param Tag $tag The tag that is going to be mapped
845
-	* @return array
846
-	*/
847
-	private function tagMap(Tag $tag) {
848
-		return array(
849
-			'id'    => $tag->getId(),
850
-			'name'  => $tag->getName(),
851
-			'owner' => $tag->getOwner(),
852
-			'type'  => $tag->getType()
853
-		);
854
-	}
53
+    /**
54
+     * Tags
55
+     *
56
+     * @var array
57
+     */
58
+    private $tags = array();
59
+
60
+    /**
61
+     * Used for storing objectid/categoryname pairs while rescanning.
62
+     *
63
+     * @var array
64
+     */
65
+    private static $relations = array();
66
+
67
+    /**
68
+     * Type
69
+     *
70
+     * @var string
71
+     */
72
+    private $type;
73
+
74
+    /**
75
+     * User
76
+     *
77
+     * @var string
78
+     */
79
+    private $user;
80
+
81
+    /**
82
+     * Are we including tags for shared items?
83
+     *
84
+     * @var bool
85
+     */
86
+    private $includeShared = false;
87
+
88
+    /**
89
+     * The current user, plus any owners of the items shared with the current
90
+     * user, if $this->includeShared === true.
91
+     *
92
+     * @var array
93
+     */
94
+    private $owners = array();
95
+
96
+    /**
97
+     * The Mapper we're using to communicate our Tag objects to the database.
98
+     *
99
+     * @var TagMapper
100
+     */
101
+    private $mapper;
102
+
103
+    /**
104
+     * The sharing backend for objects of $this->type. Required if
105
+     * $this->includeShared === true to determine ownership of items.
106
+     *
107
+     * @var \OCP\Share_Backend
108
+     */
109
+    private $backend;
110
+
111
+    const TAG_TABLE = '*PREFIX*vcategory';
112
+    const RELATION_TABLE = '*PREFIX*vcategory_to_object';
113
+
114
+    const TAG_FAVORITE = '_$!<Favorite>!$_';
115
+
116
+    /**
117
+     * Constructor.
118
+     *
119
+     * @param TagMapper $mapper Instance of the TagMapper abstraction layer.
120
+     * @param string $user The user whose data the object will operate on.
121
+     * @param string $type The type of items for which tags will be loaded.
122
+     * @param array $defaultTags Tags that should be created at construction.
123
+     * @param boolean $includeShared Whether to include tags for items shared with this user by others.
124
+     */
125
+    public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) {
126
+        $this->mapper = $mapper;
127
+        $this->user = $user;
128
+        $this->type = $type;
129
+        $this->includeShared = $includeShared;
130
+        $this->owners = array($this->user);
131
+        if ($this->includeShared) {
132
+            $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true));
133
+            $this->backend = \OC\Share\Share::getBackend($this->type);
134
+        }
135
+        $this->tags = $this->mapper->loadTags($this->owners, $this->type);
136
+
137
+        if(count($defaultTags) > 0 && count($this->tags) === 0) {
138
+            $this->addMultiple($defaultTags, true);
139
+        }
140
+    }
141
+
142
+    /**
143
+     * Check if any tags are saved for this type and user.
144
+     *
145
+     * @return boolean
146
+     */
147
+    public function isEmpty() {
148
+        return count($this->tags) === 0;
149
+    }
150
+
151
+    /**
152
+     * Returns an array mapping a given tag's properties to its values:
153
+     * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
154
+     *
155
+     * @param string $id The ID of the tag that is going to be mapped
156
+     * @return array|false
157
+     */
158
+    public function getTag($id) {
159
+        $key = $this->getTagById($id);
160
+        if ($key !== false) {
161
+            return $this->tagMap($this->tags[$key]);
162
+        }
163
+        return false;
164
+    }
165
+
166
+    /**
167
+     * Get the tags for a specific user.
168
+     *
169
+     * This returns an array with maps containing each tag's properties:
170
+     * [
171
+     * 	['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
172
+     * 	['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
173
+     * ]
174
+     *
175
+     * @return array
176
+     */
177
+    public function getTags() {
178
+        if(!count($this->tags)) {
179
+            return array();
180
+        }
181
+
182
+        usort($this->tags, function($a, $b) {
183
+            return strnatcasecmp($a->getName(), $b->getName());
184
+        });
185
+        $tagMap = array();
186
+
187
+        foreach($this->tags as $tag) {
188
+            if($tag->getName() !== self::TAG_FAVORITE) {
189
+                $tagMap[] = $this->tagMap($tag);
190
+            }
191
+        }
192
+        return $tagMap;
193
+
194
+    }
195
+
196
+    /**
197
+     * Return only the tags owned by the given user, omitting any tags shared
198
+     * by other users.
199
+     *
200
+     * @param string $user The user whose tags are to be checked.
201
+     * @return array An array of Tag objects.
202
+     */
203
+    public function getTagsForUser($user) {
204
+        return array_filter($this->tags,
205
+            function($tag) use($user) {
206
+                return $tag->getOwner() === $user;
207
+            }
208
+        );
209
+    }
210
+
211
+    /**
212
+     * Get the list of tags for the given ids.
213
+     *
214
+     * @param array $objIds array of object ids
215
+     * @return array|boolean of tags id as key to array of tag names
216
+     * or false if an error occurred
217
+     */
218
+    public function getTagsForObjects(array $objIds) {
219
+        $entries = array();
220
+
221
+        try {
222
+            $conn = \OC::$server->getDatabaseConnection();
223
+            $chunks = array_chunk($objIds, 900, false);
224
+            foreach ($chunks as $chunk) {
225
+                $result = $conn->executeQuery(
226
+                    'SELECT `category`, `categoryid`, `objid` ' .
227
+                    'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
228
+                    'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
229
+                    array($this->user, $this->type, $chunk),
230
+                    array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
231
+                );
232
+                while ($row = $result->fetch()) {
233
+                    $objId = (int)$row['objid'];
234
+                    if (!isset($entries[$objId])) {
235
+                        $entries[$objId] = array();
236
+                    }
237
+                    $entries[$objId][] = $row['category'];
238
+                }
239
+                if (\OCP\DB::isError($result)) {
240
+                    \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
241
+                    return false;
242
+                }
243
+            }
244
+        } catch(\Exception $e) {
245
+            \OC::$server->getLogger()->logException($e, [
246
+                'message' => __METHOD__,
247
+                'level' => \OCP\Util::ERROR,
248
+                'app' => 'core',
249
+            ]);
250
+            return false;
251
+        }
252
+
253
+        return $entries;
254
+    }
255
+
256
+    /**
257
+     * Get the a list if items tagged with $tag.
258
+     *
259
+     * Throws an exception if the tag could not be found.
260
+     *
261
+     * @param string $tag Tag id or name.
262
+     * @return array|false An array of object ids or false on error.
263
+     * @throws \Exception
264
+     */
265
+    public function getIdsForTag($tag) {
266
+        $result = null;
267
+        $tagId = false;
268
+        if(is_numeric($tag)) {
269
+            $tagId = $tag;
270
+        } elseif(is_string($tag)) {
271
+            $tag = trim($tag);
272
+            if($tag === '') {
273
+                \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
274
+                return false;
275
+            }
276
+            $tagId = $this->getTagId($tag);
277
+        }
278
+
279
+        if($tagId === false) {
280
+            $l10n = \OC::$server->getL10N('core');
281
+            throw new \Exception(
282
+                $l10n->t('Could not find category "%s"', [$tag])
283
+            );
284
+        }
285
+
286
+        $ids = array();
287
+        $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
288
+            . '` WHERE `categoryid` = ?';
289
+
290
+        try {
291
+            $stmt = \OCP\DB::prepare($sql);
292
+            $result = $stmt->execute(array($tagId));
293
+            if (\OCP\DB::isError($result)) {
294
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
295
+                return false;
296
+            }
297
+        } catch(\Exception $e) {
298
+            \OC::$server->getLogger()->logException($e, [
299
+                'message' => __METHOD__,
300
+                'level' => \OCP\Util::ERROR,
301
+                'app' => 'core',
302
+            ]);
303
+            return false;
304
+        }
305
+
306
+        if(!is_null($result)) {
307
+            while( $row = $result->fetchRow()) {
308
+                $id = (int)$row['objid'];
309
+
310
+                if ($this->includeShared) {
311
+                    // We have to check if we are really allowed to access the
312
+                    // items that are tagged with $tag. To that end, we ask the
313
+                    // corresponding sharing backend if the item identified by $id
314
+                    // is owned by any of $this->owners.
315
+                    foreach ($this->owners as $owner) {
316
+                        if ($this->backend->isValidSource($id, $owner)) {
317
+                            $ids[] = $id;
318
+                            break;
319
+                        }
320
+                    }
321
+                } else {
322
+                    $ids[] = $id;
323
+                }
324
+            }
325
+        }
326
+
327
+        return $ids;
328
+    }
329
+
330
+    /**
331
+     * Checks whether a tag is saved for the given user,
332
+     * disregarding the ones shared with him or her.
333
+     *
334
+     * @param string $name The tag name to check for.
335
+     * @param string $user The user whose tags are to be checked.
336
+     * @return bool
337
+     */
338
+    public function userHasTag($name, $user) {
339
+        $key = $this->array_searchi($name, $this->getTagsForUser($user));
340
+        return ($key !== false) ? $this->tags[$key]->getId() : false;
341
+    }
342
+
343
+    /**
344
+     * Checks whether a tag is saved for or shared with the current user.
345
+     *
346
+     * @param string $name The tag name to check for.
347
+     * @return bool
348
+     */
349
+    public function hasTag($name) {
350
+        return $this->getTagId($name) !== false;
351
+    }
352
+
353
+    /**
354
+     * Add a new tag.
355
+     *
356
+     * @param string $name A string with a name of the tag
357
+     * @return false|int the id of the added tag or false on error.
358
+     */
359
+    public function add($name) {
360
+        $name = trim($name);
361
+
362
+        if($name === '') {
363
+            \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
364
+            return false;
365
+        }
366
+        if($this->userHasTag($name, $this->user)) {
367
+            \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
368
+            return false;
369
+        }
370
+        try {
371
+            $tag = new Tag($this->user, $this->type, $name);
372
+            $tag = $this->mapper->insert($tag);
373
+            $this->tags[] = $tag;
374
+        } catch(\Exception $e) {
375
+            \OC::$server->getLogger()->logException($e, [
376
+                'message' => __METHOD__,
377
+                'level' => \OCP\Util::ERROR,
378
+                'app' => 'core',
379
+            ]);
380
+            return false;
381
+        }
382
+        \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
383
+        return $tag->getId();
384
+    }
385
+
386
+    /**
387
+     * Rename tag.
388
+     *
389
+     * @param string|integer $from The name or ID of the existing tag
390
+     * @param string $to The new name of the tag.
391
+     * @return bool
392
+     */
393
+    public function rename($from, $to) {
394
+        $from = trim($from);
395
+        $to = trim($to);
396
+
397
+        if($to === '' || $from === '') {
398
+            \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
399
+            return false;
400
+        }
401
+
402
+        if (is_numeric($from)) {
403
+            $key = $this->getTagById($from);
404
+        } else {
405
+            $key = $this->getTagByName($from);
406
+        }
407
+        if($key === false) {
408
+            \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
409
+            return false;
410
+        }
411
+        $tag = $this->tags[$key];
412
+
413
+        if($this->userHasTag($to, $tag->getOwner())) {
414
+            \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
415
+            return false;
416
+        }
417
+
418
+        try {
419
+            $tag->setName($to);
420
+            $this->tags[$key] = $this->mapper->update($tag);
421
+        } catch(\Exception $e) {
422
+            \OC::$server->getLogger()->logException($e, [
423
+                'message' => __METHOD__,
424
+                'level' => \OCP\Util::ERROR,
425
+                'app' => 'core',
426
+            ]);
427
+            return false;
428
+        }
429
+        return true;
430
+    }
431
+
432
+    /**
433
+     * Add a list of new tags.
434
+     *
435
+     * @param string[] $names A string with a name or an array of strings containing
436
+     * the name(s) of the tag(s) to add.
437
+     * @param bool $sync When true, save the tags
438
+     * @param int|null $id int Optional object id to add to this|these tag(s)
439
+     * @return bool Returns false on error.
440
+     */
441
+    public function addMultiple($names, $sync=false, $id = null) {
442
+        if(!is_array($names)) {
443
+            $names = array($names);
444
+        }
445
+        $names = array_map('trim', $names);
446
+        array_filter($names);
447
+
448
+        $newones = array();
449
+        foreach($names as $name) {
450
+            if(!$this->hasTag($name) && $name !== '') {
451
+                $newones[] = new Tag($this->user, $this->type, $name);
452
+            }
453
+            if(!is_null($id) ) {
454
+                // Insert $objectid, $categoryid  pairs if not exist.
455
+                self::$relations[] = array('objid' => $id, 'tag' => $name);
456
+            }
457
+        }
458
+        $this->tags = array_merge($this->tags, $newones);
459
+        if($sync === true) {
460
+            $this->save();
461
+        }
462
+
463
+        return true;
464
+    }
465
+
466
+    /**
467
+     * Save the list of tags and their object relations
468
+     */
469
+    protected function save() {
470
+        if(is_array($this->tags)) {
471
+            foreach($this->tags as $tag) {
472
+                try {
473
+                    if (!$this->mapper->tagExists($tag)) {
474
+                        $this->mapper->insert($tag);
475
+                    }
476
+                } catch(\Exception $e) {
477
+                    \OC::$server->getLogger()->logException($e, [
478
+                        'message' => __METHOD__,
479
+                        'level' => \OCP\Util::ERROR,
480
+                        'app' => 'core',
481
+                    ]);
482
+                }
483
+            }
484
+
485
+            // reload tags to get the proper ids.
486
+            $this->tags = $this->mapper->loadTags($this->owners, $this->type);
487
+            \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
488
+                \OCP\Util::DEBUG);
489
+            // Loop through temporarily cached objectid/tagname pairs
490
+            // and save relations.
491
+            $tags = $this->tags;
492
+            // For some reason this is needed or array_search(i) will return 0..?
493
+            ksort($tags);
494
+            foreach(self::$relations as $relation) {
495
+                $tagId = $this->getTagId($relation['tag']);
496
+                \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
497
+                if($tagId) {
498
+                    try {
499
+                        \OCP\DB::insertIfNotExist(self::RELATION_TABLE,
500
+                            array(
501
+                                'objid' => $relation['objid'],
502
+                                'categoryid' => $tagId,
503
+                                'type' => $this->type,
504
+                                ));
505
+                    } catch(\Exception $e) {
506
+                        \OC::$server->getLogger()->logException($e, [
507
+                            'message' => __METHOD__,
508
+                            'level' => \OCP\Util::ERROR,
509
+                            'app' => 'core',
510
+                        ]);
511
+                    }
512
+                }
513
+            }
514
+            self::$relations = array(); // reset
515
+        } else {
516
+            \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
517
+                . print_r($this->tags, true), \OCP\Util::ERROR);
518
+        }
519
+    }
520
+
521
+    /**
522
+     * Delete tags and tag/object relations for a user.
523
+     *
524
+     * For hooking up on post_deleteUser
525
+     *
526
+     * @param array $arguments
527
+     */
528
+    public static function post_deleteUser($arguments) {
529
+        // Find all objectid/tagId pairs.
530
+        $result = null;
531
+        try {
532
+            $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
533
+                . 'WHERE `uid` = ?');
534
+            $result = $stmt->execute(array($arguments['uid']));
535
+            if (\OCP\DB::isError($result)) {
536
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
537
+            }
538
+        } catch(\Exception $e) {
539
+            \OC::$server->getLogger()->logException($e, [
540
+                'message' => __METHOD__,
541
+                'level' => \OCP\Util::ERROR,
542
+                'app' => 'core',
543
+            ]);
544
+        }
545
+
546
+        if(!is_null($result)) {
547
+            try {
548
+                $stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
549
+                    . 'WHERE `categoryid` = ?');
550
+                while( $row = $result->fetchRow()) {
551
+                    try {
552
+                        $stmt->execute(array($row['id']));
553
+                    } catch(\Exception $e) {
554
+                        \OC::$server->getLogger()->logException($e, [
555
+                            'message' => __METHOD__,
556
+                            'level' => \OCP\Util::ERROR,
557
+                            'app' => 'core',
558
+                        ]);
559
+                    }
560
+                }
561
+            } catch(\Exception $e) {
562
+                \OC::$server->getLogger()->logException($e, [
563
+                    'message' => __METHOD__,
564
+                    'level' => \OCP\Util::ERROR,
565
+                    'app' => 'core',
566
+                ]);
567
+            }
568
+        }
569
+        try {
570
+            $stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
571
+                . 'WHERE `uid` = ?');
572
+            $result = $stmt->execute(array($arguments['uid']));
573
+            if (\OCP\DB::isError($result)) {
574
+                \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
575
+            }
576
+        } catch(\Exception $e) {
577
+            \OC::$server->getLogger()->logException($e, [
578
+                'message' => __METHOD__,
579
+                'level' => \OCP\Util::ERROR,
580
+                'app' => 'core',
581
+            ]);
582
+        }
583
+    }
584
+
585
+    /**
586
+     * Delete tag/object relations from the db
587
+     *
588
+     * @param array $ids The ids of the objects
589
+     * @return boolean Returns false on error.
590
+     */
591
+    public function purgeObjects(array $ids) {
592
+        if(count($ids) === 0) {
593
+            // job done ;)
594
+            return true;
595
+        }
596
+        $updates = $ids;
597
+        try {
598
+            $query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
599
+            $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
600
+            $query .= 'AND `type`= ?';
601
+            $updates[] = $this->type;
602
+            $stmt = \OCP\DB::prepare($query);
603
+            $result = $stmt->execute($updates);
604
+            if (\OCP\DB::isError($result)) {
605
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
606
+                return false;
607
+            }
608
+        } catch(\Exception $e) {
609
+            \OC::$server->getLogger()->logException($e, [
610
+                'message' => __METHOD__,
611
+                'level' => \OCP\Util::ERROR,
612
+                'app' => 'core',
613
+            ]);
614
+            return false;
615
+        }
616
+        return true;
617
+    }
618
+
619
+    /**
620
+     * Get favorites for an object type
621
+     *
622
+     * @return array|false An array of object ids.
623
+     */
624
+    public function getFavorites() {
625
+        try {
626
+            return $this->getIdsForTag(self::TAG_FAVORITE);
627
+        } catch(\Exception $e) {
628
+            \OC::$server->getLogger()->logException($e, [
629
+                'message' => __METHOD__,
630
+                'level' => \OCP\Util::ERROR,
631
+                'app' => 'core',
632
+            ]);
633
+            return array();
634
+        }
635
+    }
636
+
637
+    /**
638
+     * Add an object to favorites
639
+     *
640
+     * @param int $objid The id of the object
641
+     * @return boolean
642
+     */
643
+    public function addToFavorites($objid) {
644
+        if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
645
+            $this->add(self::TAG_FAVORITE);
646
+        }
647
+        return $this->tagAs($objid, self::TAG_FAVORITE);
648
+    }
649
+
650
+    /**
651
+     * Remove an object from favorites
652
+     *
653
+     * @param int $objid The id of the object
654
+     * @return boolean
655
+     */
656
+    public function removeFromFavorites($objid) {
657
+        return $this->unTag($objid, self::TAG_FAVORITE);
658
+    }
659
+
660
+    /**
661
+     * Creates a tag/object relation.
662
+     *
663
+     * @param int $objid The id of the object
664
+     * @param string $tag The id or name of the tag
665
+     * @return boolean Returns false on error.
666
+     */
667
+    public function tagAs($objid, $tag) {
668
+        if(is_string($tag) && !is_numeric($tag)) {
669
+            $tag = trim($tag);
670
+            if($tag === '') {
671
+                \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
672
+                return false;
673
+            }
674
+            if(!$this->hasTag($tag)) {
675
+                $this->add($tag);
676
+            }
677
+            $tagId =  $this->getTagId($tag);
678
+        } else {
679
+            $tagId = $tag;
680
+        }
681
+        try {
682
+            \OCP\DB::insertIfNotExist(self::RELATION_TABLE,
683
+                array(
684
+                    'objid' => $objid,
685
+                    'categoryid' => $tagId,
686
+                    'type' => $this->type,
687
+                ));
688
+        } catch(\Exception $e) {
689
+            \OC::$server->getLogger()->logException($e, [
690
+                'message' => __METHOD__,
691
+                'level' => \OCP\Util::ERROR,
692
+                'app' => 'core',
693
+            ]);
694
+            return false;
695
+        }
696
+        return true;
697
+    }
698
+
699
+    /**
700
+     * Delete single tag/object relation from the db
701
+     *
702
+     * @param int $objid The id of the object
703
+     * @param string $tag The id or name of the tag
704
+     * @return boolean
705
+     */
706
+    public function unTag($objid, $tag) {
707
+        if(is_string($tag) && !is_numeric($tag)) {
708
+            $tag = trim($tag);
709
+            if($tag === '') {
710
+                \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
711
+                return false;
712
+            }
713
+            $tagId =  $this->getTagId($tag);
714
+        } else {
715
+            $tagId = $tag;
716
+        }
717
+
718
+        try {
719
+            $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
720
+                    . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
721
+            $stmt = \OCP\DB::prepare($sql);
722
+            $stmt->execute(array($objid, $tagId, $this->type));
723
+        } catch(\Exception $e) {
724
+            \OC::$server->getLogger()->logException($e, [
725
+                'message' => __METHOD__,
726
+                'level' => \OCP\Util::ERROR,
727
+                'app' => 'core',
728
+            ]);
729
+            return false;
730
+        }
731
+        return true;
732
+    }
733
+
734
+    /**
735
+     * Delete tags from the database.
736
+     *
737
+     * @param string[]|integer[] $names An array of tags (names or IDs) to delete
738
+     * @return bool Returns false on error
739
+     */
740
+    public function delete($names) {
741
+        if(!is_array($names)) {
742
+            $names = array($names);
743
+        }
744
+
745
+        $names = array_map('trim', $names);
746
+        array_filter($names);
747
+
748
+        \OCP\Util::writeLog('core', __METHOD__ . ', before: '
749
+            . print_r($this->tags, true), \OCP\Util::DEBUG);
750
+        foreach($names as $name) {
751
+            $id = null;
752
+
753
+            if (is_numeric($name)) {
754
+                $key = $this->getTagById($name);
755
+            } else {
756
+                $key = $this->getTagByName($name);
757
+            }
758
+            if ($key !== false) {
759
+                $tag = $this->tags[$key];
760
+                $id = $tag->getId();
761
+                unset($this->tags[$key]);
762
+                $this->mapper->delete($tag);
763
+            } else {
764
+                \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
765
+                    . ': not found.', \OCP\Util::ERROR);
766
+            }
767
+            if(!is_null($id) && $id !== false) {
768
+                try {
769
+                    $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
770
+                            . 'WHERE `categoryid` = ?';
771
+                    $stmt = \OCP\DB::prepare($sql);
772
+                    $result = $stmt->execute(array($id));
773
+                    if (\OCP\DB::isError($result)) {
774
+                        \OCP\Util::writeLog('core',
775
+                            __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
776
+                            \OCP\Util::ERROR);
777
+                        return false;
778
+                    }
779
+                } catch(\Exception $e) {
780
+                    \OC::$server->getLogger()->logException($e, [
781
+                        'message' => __METHOD__,
782
+                        'level' => \OCP\Util::ERROR,
783
+                        'app' => 'core',
784
+                    ]);
785
+                    return false;
786
+                }
787
+            }
788
+        }
789
+        return true;
790
+    }
791
+
792
+    // case-insensitive array_search
793
+    protected function array_searchi($needle, $haystack, $mem='getName') {
794
+        if(!is_array($haystack)) {
795
+            return false;
796
+        }
797
+        return array_search(strtolower($needle), array_map(
798
+            function($tag) use($mem) {
799
+                return strtolower(call_user_func(array($tag, $mem)));
800
+            }, $haystack)
801
+        );
802
+    }
803
+
804
+    /**
805
+     * Get a tag's ID.
806
+     *
807
+     * @param string $name The tag name to look for.
808
+     * @return string|bool The tag's id or false if no matching tag is found.
809
+     */
810
+    private function getTagId($name) {
811
+        $key = $this->array_searchi($name, $this->tags);
812
+        if ($key !== false) {
813
+            return $this->tags[$key]->getId();
814
+        }
815
+        return false;
816
+    }
817
+
818
+    /**
819
+     * Get a tag by its name.
820
+     *
821
+     * @param string $name The tag name.
822
+     * @return integer|bool The tag object's offset within the $this->tags
823
+     *                      array or false if it doesn't exist.
824
+     */
825
+    private function getTagByName($name) {
826
+        return $this->array_searchi($name, $this->tags, 'getName');
827
+    }
828
+
829
+    /**
830
+     * Get a tag by its ID.
831
+     *
832
+     * @param string $id The tag ID to look for.
833
+     * @return integer|bool The tag object's offset within the $this->tags
834
+     *                      array or false if it doesn't exist.
835
+     */
836
+    private function getTagById($id) {
837
+        return $this->array_searchi($id, $this->tags, 'getId');
838
+    }
839
+
840
+    /**
841
+     * Returns an array mapping a given tag's properties to its values:
842
+     * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
843
+     *
844
+     * @param Tag $tag The tag that is going to be mapped
845
+     * @return array
846
+     */
847
+    private function tagMap(Tag $tag) {
848
+        return array(
849
+            'id'    => $tag->getId(),
850
+            'name'  => $tag->getName(),
851
+            'owner' => $tag->getOwner(),
852
+            'type'  => $tag->getType()
853
+        );
854
+    }
855 855
 }
Please login to merge, or discard this patch.
Spacing   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 		}
135 135
 		$this->tags = $this->mapper->loadTags($this->owners, $this->type);
136 136
 
137
-		if(count($defaultTags) > 0 && count($this->tags) === 0) {
137
+		if (count($defaultTags) > 0 && count($this->tags) === 0) {
138 138
 			$this->addMultiple($defaultTags, true);
139 139
 		}
140 140
 	}
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
 	* @return array
176 176
 	*/
177 177
 	public function getTags() {
178
-		if(!count($this->tags)) {
178
+		if (!count($this->tags)) {
179 179
 			return array();
180 180
 		}
181 181
 
@@ -184,8 +184,8 @@  discard block
 block discarded – undo
184 184
 		});
185 185
 		$tagMap = array();
186 186
 
187
-		foreach($this->tags as $tag) {
188
-			if($tag->getName() !== self::TAG_FAVORITE) {
187
+		foreach ($this->tags as $tag) {
188
+			if ($tag->getName() !== self::TAG_FAVORITE) {
189 189
 				$tagMap[] = $this->tagMap($tag);
190 190
 			}
191 191
 		}
@@ -223,25 +223,25 @@  discard block
 block discarded – undo
223 223
 			$chunks = array_chunk($objIds, 900, false);
224 224
 			foreach ($chunks as $chunk) {
225 225
 				$result = $conn->executeQuery(
226
-					'SELECT `category`, `categoryid`, `objid` ' .
227
-					'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
226
+					'SELECT `category`, `categoryid`, `objid` '.
227
+					'FROM `'.self::RELATION_TABLE.'` r, `'.self::TAG_TABLE.'` '.
228 228
 					'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
229 229
 					array($this->user, $this->type, $chunk),
230 230
 					array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
231 231
 				);
232 232
 				while ($row = $result->fetch()) {
233
-					$objId = (int)$row['objid'];
233
+					$objId = (int) $row['objid'];
234 234
 					if (!isset($entries[$objId])) {
235 235
 						$entries[$objId] = array();
236 236
 					}
237 237
 					$entries[$objId][] = $row['category'];
238 238
 				}
239 239
 				if (\OCP\DB::isError($result)) {
240
-					\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
240
+					\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
241 241
 					return false;
242 242
 				}
243 243
 			}
244
-		} catch(\Exception $e) {
244
+		} catch (\Exception $e) {
245 245
 			\OC::$server->getLogger()->logException($e, [
246 246
 				'message' => __METHOD__,
247 247
 				'level' => \OCP\Util::ERROR,
@@ -265,18 +265,18 @@  discard block
 block discarded – undo
265 265
 	public function getIdsForTag($tag) {
266 266
 		$result = null;
267 267
 		$tagId = false;
268
-		if(is_numeric($tag)) {
268
+		if (is_numeric($tag)) {
269 269
 			$tagId = $tag;
270
-		} elseif(is_string($tag)) {
270
+		} elseif (is_string($tag)) {
271 271
 			$tag = trim($tag);
272
-			if($tag === '') {
272
+			if ($tag === '') {
273 273
 				\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
274 274
 				return false;
275 275
 			}
276 276
 			$tagId = $this->getTagId($tag);
277 277
 		}
278 278
 
279
-		if($tagId === false) {
279
+		if ($tagId === false) {
280 280
 			$l10n = \OC::$server->getL10N('core');
281 281
 			throw new \Exception(
282 282
 				$l10n->t('Could not find category "%s"', [$tag])
@@ -284,17 +284,17 @@  discard block
 block discarded – undo
284 284
 		}
285 285
 
286 286
 		$ids = array();
287
-		$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
287
+		$sql = 'SELECT `objid` FROM `'.self::RELATION_TABLE
288 288
 			. '` WHERE `categoryid` = ?';
289 289
 
290 290
 		try {
291 291
 			$stmt = \OCP\DB::prepare($sql);
292 292
 			$result = $stmt->execute(array($tagId));
293 293
 			if (\OCP\DB::isError($result)) {
294
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
294
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
295 295
 				return false;
296 296
 			}
297
-		} catch(\Exception $e) {
297
+		} catch (\Exception $e) {
298 298
 			\OC::$server->getLogger()->logException($e, [
299 299
 				'message' => __METHOD__,
300 300
 				'level' => \OCP\Util::ERROR,
@@ -303,9 +303,9 @@  discard block
 block discarded – undo
303 303
 			return false;
304 304
 		}
305 305
 
306
-		if(!is_null($result)) {
307
-			while( $row = $result->fetchRow()) {
308
-				$id = (int)$row['objid'];
306
+		if (!is_null($result)) {
307
+			while ($row = $result->fetchRow()) {
308
+				$id = (int) $row['objid'];
309 309
 
310 310
 				if ($this->includeShared) {
311 311
 					// We have to check if we are really allowed to access the
@@ -359,19 +359,19 @@  discard block
 block discarded – undo
359 359
 	public function add($name) {
360 360
 		$name = trim($name);
361 361
 
362
-		if($name === '') {
362
+		if ($name === '') {
363 363
 			\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
364 364
 			return false;
365 365
 		}
366
-		if($this->userHasTag($name, $this->user)) {
367
-			\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
366
+		if ($this->userHasTag($name, $this->user)) {
367
+			\OCP\Util::writeLog('core', __METHOD__.', name: '.$name.' exists already', \OCP\Util::DEBUG);
368 368
 			return false;
369 369
 		}
370 370
 		try {
371 371
 			$tag = new Tag($this->user, $this->type, $name);
372 372
 			$tag = $this->mapper->insert($tag);
373 373
 			$this->tags[] = $tag;
374
-		} catch(\Exception $e) {
374
+		} catch (\Exception $e) {
375 375
 			\OC::$server->getLogger()->logException($e, [
376 376
 				'message' => __METHOD__,
377 377
 				'level' => \OCP\Util::ERROR,
@@ -379,7 +379,7 @@  discard block
 block discarded – undo
379 379
 			]);
380 380
 			return false;
381 381
 		}
382
-		\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
382
+		\OCP\Util::writeLog('core', __METHOD__.', id: '.$tag->getId(), \OCP\Util::DEBUG);
383 383
 		return $tag->getId();
384 384
 	}
385 385
 
@@ -394,7 +394,7 @@  discard block
 block discarded – undo
394 394
 		$from = trim($from);
395 395
 		$to = trim($to);
396 396
 
397
-		if($to === '' || $from === '') {
397
+		if ($to === '' || $from === '') {
398 398
 			\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
399 399
 			return false;
400 400
 		}
@@ -404,21 +404,21 @@  discard block
 block discarded – undo
404 404
 		} else {
405 405
 			$key = $this->getTagByName($from);
406 406
 		}
407
-		if($key === false) {
408
-			\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
407
+		if ($key === false) {
408
+			\OCP\Util::writeLog('core', __METHOD__.', tag: '.$from.' does not exist', \OCP\Util::DEBUG);
409 409
 			return false;
410 410
 		}
411 411
 		$tag = $this->tags[$key];
412 412
 
413
-		if($this->userHasTag($to, $tag->getOwner())) {
414
-			\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
413
+		if ($this->userHasTag($to, $tag->getOwner())) {
414
+			\OCP\Util::writeLog('core', __METHOD__.', A tag named '.$to.' already exists for user '.$tag->getOwner().'.', \OCP\Util::DEBUG);
415 415
 			return false;
416 416
 		}
417 417
 
418 418
 		try {
419 419
 			$tag->setName($to);
420 420
 			$this->tags[$key] = $this->mapper->update($tag);
421
-		} catch(\Exception $e) {
421
+		} catch (\Exception $e) {
422 422
 			\OC::$server->getLogger()->logException($e, [
423 423
 				'message' => __METHOD__,
424 424
 				'level' => \OCP\Util::ERROR,
@@ -438,25 +438,25 @@  discard block
 block discarded – undo
438 438
 	* @param int|null $id int Optional object id to add to this|these tag(s)
439 439
 	* @return bool Returns false on error.
440 440
 	*/
441
-	public function addMultiple($names, $sync=false, $id = null) {
442
-		if(!is_array($names)) {
441
+	public function addMultiple($names, $sync = false, $id = null) {
442
+		if (!is_array($names)) {
443 443
 			$names = array($names);
444 444
 		}
445 445
 		$names = array_map('trim', $names);
446 446
 		array_filter($names);
447 447
 
448 448
 		$newones = array();
449
-		foreach($names as $name) {
450
-			if(!$this->hasTag($name) && $name !== '') {
449
+		foreach ($names as $name) {
450
+			if (!$this->hasTag($name) && $name !== '') {
451 451
 				$newones[] = new Tag($this->user, $this->type, $name);
452 452
 			}
453
-			if(!is_null($id) ) {
453
+			if (!is_null($id)) {
454 454
 				// Insert $objectid, $categoryid  pairs if not exist.
455 455
 				self::$relations[] = array('objid' => $id, 'tag' => $name);
456 456
 			}
457 457
 		}
458 458
 		$this->tags = array_merge($this->tags, $newones);
459
-		if($sync === true) {
459
+		if ($sync === true) {
460 460
 			$this->save();
461 461
 		}
462 462
 
@@ -467,13 +467,13 @@  discard block
 block discarded – undo
467 467
 	 * Save the list of tags and their object relations
468 468
 	 */
469 469
 	protected function save() {
470
-		if(is_array($this->tags)) {
471
-			foreach($this->tags as $tag) {
470
+		if (is_array($this->tags)) {
471
+			foreach ($this->tags as $tag) {
472 472
 				try {
473 473
 					if (!$this->mapper->tagExists($tag)) {
474 474
 						$this->mapper->insert($tag);
475 475
 					}
476
-				} catch(\Exception $e) {
476
+				} catch (\Exception $e) {
477 477
 					\OC::$server->getLogger()->logException($e, [
478 478
 						'message' => __METHOD__,
479 479
 						'level' => \OCP\Util::ERROR,
@@ -484,17 +484,17 @@  discard block
 block discarded – undo
484 484
 
485 485
 			// reload tags to get the proper ids.
486 486
 			$this->tags = $this->mapper->loadTags($this->owners, $this->type);
487
-			\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
487
+			\OCP\Util::writeLog('core', __METHOD__.', tags: '.print_r($this->tags, true),
488 488
 				\OCP\Util::DEBUG);
489 489
 			// Loop through temporarily cached objectid/tagname pairs
490 490
 			// and save relations.
491 491
 			$tags = $this->tags;
492 492
 			// For some reason this is needed or array_search(i) will return 0..?
493 493
 			ksort($tags);
494
-			foreach(self::$relations as $relation) {
494
+			foreach (self::$relations as $relation) {
495 495
 				$tagId = $this->getTagId($relation['tag']);
496
-				\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
497
-				if($tagId) {
496
+				\OCP\Util::writeLog('core', __METHOD__.'catid, '.$relation['tag'].' '.$tagId, \OCP\Util::DEBUG);
497
+				if ($tagId) {
498 498
 					try {
499 499
 						\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
500 500
 							array(
@@ -502,7 +502,7 @@  discard block
 block discarded – undo
502 502
 								'categoryid' => $tagId,
503 503
 								'type' => $this->type,
504 504
 								));
505
-					} catch(\Exception $e) {
505
+					} catch (\Exception $e) {
506 506
 						\OC::$server->getLogger()->logException($e, [
507 507
 							'message' => __METHOD__,
508 508
 							'level' => \OCP\Util::ERROR,
@@ -529,13 +529,13 @@  discard block
 block discarded – undo
529 529
 		// Find all objectid/tagId pairs.
530 530
 		$result = null;
531 531
 		try {
532
-			$stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
532
+			$stmt = \OCP\DB::prepare('SELECT `id` FROM `'.self::TAG_TABLE.'` '
533 533
 				. 'WHERE `uid` = ?');
534 534
 			$result = $stmt->execute(array($arguments['uid']));
535 535
 			if (\OCP\DB::isError($result)) {
536
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
536
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
537 537
 			}
538
-		} catch(\Exception $e) {
538
+		} catch (\Exception $e) {
539 539
 			\OC::$server->getLogger()->logException($e, [
540 540
 				'message' => __METHOD__,
541 541
 				'level' => \OCP\Util::ERROR,
@@ -543,14 +543,14 @@  discard block
 block discarded – undo
543 543
 			]);
544 544
 		}
545 545
 
546
-		if(!is_null($result)) {
546
+		if (!is_null($result)) {
547 547
 			try {
548
-				$stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
548
+				$stmt = \OCP\DB::prepare('DELETE FROM `'.self::RELATION_TABLE.'` '
549 549
 					. 'WHERE `categoryid` = ?');
550
-				while( $row = $result->fetchRow()) {
550
+				while ($row = $result->fetchRow()) {
551 551
 					try {
552 552
 						$stmt->execute(array($row['id']));
553
-					} catch(\Exception $e) {
553
+					} catch (\Exception $e) {
554 554
 						\OC::$server->getLogger()->logException($e, [
555 555
 							'message' => __METHOD__,
556 556
 							'level' => \OCP\Util::ERROR,
@@ -558,7 +558,7 @@  discard block
 block discarded – undo
558 558
 						]);
559 559
 					}
560 560
 				}
561
-			} catch(\Exception $e) {
561
+			} catch (\Exception $e) {
562 562
 				\OC::$server->getLogger()->logException($e, [
563 563
 					'message' => __METHOD__,
564 564
 					'level' => \OCP\Util::ERROR,
@@ -567,13 +567,13 @@  discard block
 block discarded – undo
567 567
 			}
568 568
 		}
569 569
 		try {
570
-			$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
570
+			$stmt = \OCP\DB::prepare('DELETE FROM `'.self::TAG_TABLE.'` '
571 571
 				. 'WHERE `uid` = ?');
572 572
 			$result = $stmt->execute(array($arguments['uid']));
573 573
 			if (\OCP\DB::isError($result)) {
574
-				\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
574
+				\OCP\Util::writeLog('core', __METHOD__.', DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
575 575
 			}
576
-		} catch(\Exception $e) {
576
+		} catch (\Exception $e) {
577 577
 			\OC::$server->getLogger()->logException($e, [
578 578
 				'message' => __METHOD__,
579 579
 				'level' => \OCP\Util::ERROR,
@@ -589,23 +589,23 @@  discard block
 block discarded – undo
589 589
 	* @return boolean Returns false on error.
590 590
 	*/
591 591
 	public function purgeObjects(array $ids) {
592
-		if(count($ids) === 0) {
592
+		if (count($ids) === 0) {
593 593
 			// job done ;)
594 594
 			return true;
595 595
 		}
596 596
 		$updates = $ids;
597 597
 		try {
598
-			$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
599
-			$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
598
+			$query = 'DELETE FROM `'.self::RELATION_TABLE.'` ';
599
+			$query .= 'WHERE `objid` IN ('.str_repeat('?,', count($ids) - 1).'?) ';
600 600
 			$query .= 'AND `type`= ?';
601 601
 			$updates[] = $this->type;
602 602
 			$stmt = \OCP\DB::prepare($query);
603 603
 			$result = $stmt->execute($updates);
604 604
 			if (\OCP\DB::isError($result)) {
605
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
605
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
606 606
 				return false;
607 607
 			}
608
-		} catch(\Exception $e) {
608
+		} catch (\Exception $e) {
609 609
 			\OC::$server->getLogger()->logException($e, [
610 610
 				'message' => __METHOD__,
611 611
 				'level' => \OCP\Util::ERROR,
@@ -624,7 +624,7 @@  discard block
 block discarded – undo
624 624
 	public function getFavorites() {
625 625
 		try {
626 626
 			return $this->getIdsForTag(self::TAG_FAVORITE);
627
-		} catch(\Exception $e) {
627
+		} catch (\Exception $e) {
628 628
 			\OC::$server->getLogger()->logException($e, [
629 629
 				'message' => __METHOD__,
630 630
 				'level' => \OCP\Util::ERROR,
@@ -641,7 +641,7 @@  discard block
 block discarded – undo
641 641
 	* @return boolean
642 642
 	*/
643 643
 	public function addToFavorites($objid) {
644
-		if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
644
+		if (!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
645 645
 			$this->add(self::TAG_FAVORITE);
646 646
 		}
647 647
 		return $this->tagAs($objid, self::TAG_FAVORITE);
@@ -665,16 +665,16 @@  discard block
 block discarded – undo
665 665
 	* @return boolean Returns false on error.
666 666
 	*/
667 667
 	public function tagAs($objid, $tag) {
668
-		if(is_string($tag) && !is_numeric($tag)) {
668
+		if (is_string($tag) && !is_numeric($tag)) {
669 669
 			$tag = trim($tag);
670
-			if($tag === '') {
670
+			if ($tag === '') {
671 671
 				\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
672 672
 				return false;
673 673
 			}
674
-			if(!$this->hasTag($tag)) {
674
+			if (!$this->hasTag($tag)) {
675 675
 				$this->add($tag);
676 676
 			}
677
-			$tagId =  $this->getTagId($tag);
677
+			$tagId = $this->getTagId($tag);
678 678
 		} else {
679 679
 			$tagId = $tag;
680 680
 		}
@@ -685,7 +685,7 @@  discard block
 block discarded – undo
685 685
 					'categoryid' => $tagId,
686 686
 					'type' => $this->type,
687 687
 				));
688
-		} catch(\Exception $e) {
688
+		} catch (\Exception $e) {
689 689
 			\OC::$server->getLogger()->logException($e, [
690 690
 				'message' => __METHOD__,
691 691
 				'level' => \OCP\Util::ERROR,
@@ -704,23 +704,23 @@  discard block
 block discarded – undo
704 704
 	* @return boolean
705 705
 	*/
706 706
 	public function unTag($objid, $tag) {
707
-		if(is_string($tag) && !is_numeric($tag)) {
707
+		if (is_string($tag) && !is_numeric($tag)) {
708 708
 			$tag = trim($tag);
709
-			if($tag === '') {
709
+			if ($tag === '') {
710 710
 				\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
711 711
 				return false;
712 712
 			}
713
-			$tagId =  $this->getTagId($tag);
713
+			$tagId = $this->getTagId($tag);
714 714
 		} else {
715 715
 			$tagId = $tag;
716 716
 		}
717 717
 
718 718
 		try {
719
-			$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
719
+			$sql = 'DELETE FROM `'.self::RELATION_TABLE.'` '
720 720
 					. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
721 721
 			$stmt = \OCP\DB::prepare($sql);
722 722
 			$stmt->execute(array($objid, $tagId, $this->type));
723
-		} catch(\Exception $e) {
723
+		} catch (\Exception $e) {
724 724
 			\OC::$server->getLogger()->logException($e, [
725 725
 				'message' => __METHOD__,
726 726
 				'level' => \OCP\Util::ERROR,
@@ -738,16 +738,16 @@  discard block
 block discarded – undo
738 738
 	* @return bool Returns false on error
739 739
 	*/
740 740
 	public function delete($names) {
741
-		if(!is_array($names)) {
741
+		if (!is_array($names)) {
742 742
 			$names = array($names);
743 743
 		}
744 744
 
745 745
 		$names = array_map('trim', $names);
746 746
 		array_filter($names);
747 747
 
748
-		\OCP\Util::writeLog('core', __METHOD__ . ', before: '
748
+		\OCP\Util::writeLog('core', __METHOD__.', before: '
749 749
 			. print_r($this->tags, true), \OCP\Util::DEBUG);
750
-		foreach($names as $name) {
750
+		foreach ($names as $name) {
751 751
 			$id = null;
752 752
 
753 753
 			if (is_numeric($name)) {
@@ -761,22 +761,22 @@  discard block
 block discarded – undo
761 761
 				unset($this->tags[$key]);
762 762
 				$this->mapper->delete($tag);
763 763
 			} else {
764
-				\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
764
+				\OCP\Util::writeLog('core', __METHOD__.'Cannot delete tag '.$name
765 765
 					. ': not found.', \OCP\Util::ERROR);
766 766
 			}
767
-			if(!is_null($id) && $id !== false) {
767
+			if (!is_null($id) && $id !== false) {
768 768
 				try {
769
-					$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
769
+					$sql = 'DELETE FROM `'.self::RELATION_TABLE.'` '
770 770
 							. 'WHERE `categoryid` = ?';
771 771
 					$stmt = \OCP\DB::prepare($sql);
772 772
 					$result = $stmt->execute(array($id));
773 773
 					if (\OCP\DB::isError($result)) {
774 774
 						\OCP\Util::writeLog('core',
775
-							__METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
775
+							__METHOD__.'DB error: '.\OCP\DB::getErrorMessage(),
776 776
 							\OCP\Util::ERROR);
777 777
 						return false;
778 778
 					}
779
-				} catch(\Exception $e) {
779
+				} catch (\Exception $e) {
780 780
 					\OC::$server->getLogger()->logException($e, [
781 781
 						'message' => __METHOD__,
782 782
 						'level' => \OCP\Util::ERROR,
@@ -790,8 +790,8 @@  discard block
 block discarded – undo
790 790
 	}
791 791
 
792 792
 	// case-insensitive array_search
793
-	protected function array_searchi($needle, $haystack, $mem='getName') {
794
-		if(!is_array($haystack)) {
793
+	protected function array_searchi($needle, $haystack, $mem = 'getName') {
794
+		if (!is_array($haystack)) {
795 795
 			return false;
796 796
 		}
797 797
 		return array_search(strtolower($needle), array_map(
Please login to merge, or discard this patch.