@@ -46,390 +46,390 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | |
| 48 | 48 | class Avatar implements IAvatar { |
| 49 | - /** @var ISimpleFolder */ |
|
| 50 | - private $folder; |
|
| 51 | - /** @var IL10N */ |
|
| 52 | - private $l; |
|
| 53 | - /** @var User */ |
|
| 54 | - private $user; |
|
| 55 | - /** @var ILogger */ |
|
| 56 | - private $logger; |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - private $config; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * constructor |
|
| 62 | - * |
|
| 63 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | - * @param IL10N $l |
|
| 65 | - * @param User $user |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - * @param IConfig $config |
|
| 68 | - */ |
|
| 69 | - public function __construct(ISimpleFolder $folder, |
|
| 70 | - IL10N $l, |
|
| 71 | - $user, |
|
| 72 | - ILogger $logger, |
|
| 73 | - IConfig $config) { |
|
| 74 | - $this->folder = $folder; |
|
| 75 | - $this->l = $l; |
|
| 76 | - $this->user = $user; |
|
| 77 | - $this->logger = $logger; |
|
| 78 | - $this->config = $config; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @inheritdoc |
|
| 83 | - */ |
|
| 84 | - public function get ($size = 64) { |
|
| 85 | - try { |
|
| 86 | - $file = $this->getFile($size); |
|
| 87 | - } catch (NotFoundException $e) { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $avatar = new OC_Image(); |
|
| 92 | - $avatar->loadFromData($file->getContent()); |
|
| 93 | - return $avatar; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Check if an avatar exists for the user |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - public function exists() { |
|
| 102 | - |
|
| 103 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * sets the users avatar |
|
| 108 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | - * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | - * @throws \Exception if the provided image is not valid |
|
| 111 | - * @throws NotSquareException if the image is not square |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function set ($data) { |
|
| 115 | - |
|
| 116 | - if($data instanceOf IImage) { |
|
| 117 | - $img = $data; |
|
| 118 | - $data = $img->data(); |
|
| 119 | - } else { |
|
| 120 | - $img = new OC_Image(); |
|
| 121 | - if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 122 | - $img->setResource($data); |
|
| 123 | - } elseif(is_resource($data)) { |
|
| 124 | - $img->loadFromFileHandle($data); |
|
| 125 | - } else { |
|
| 126 | - try { |
|
| 127 | - // detect if it is a path or maybe the images as string |
|
| 128 | - $result = @realpath($data); |
|
| 129 | - if ($result === false || $result === null) { |
|
| 130 | - $img->loadFromData($data); |
|
| 131 | - } else { |
|
| 132 | - $img->loadFromFile($data); |
|
| 133 | - } |
|
| 134 | - } catch (\Error $e) { |
|
| 135 | - $img->loadFromData($data); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - $type = substr($img->mimeType(), -3); |
|
| 140 | - if ($type === 'peg') { |
|
| 141 | - $type = 'jpg'; |
|
| 142 | - } |
|
| 143 | - if ($type !== 'jpg' && $type !== 'png') { |
|
| 144 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - if (!$img->valid()) { |
|
| 148 | - throw new \Exception($this->l->t('Invalid image')); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if (!($img->height() === $img->width())) { |
|
| 152 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $this->remove(); |
|
| 156 | - $file = $this->folder->newFile('avatar.'.$type); |
|
| 157 | - $file->putContent($data); |
|
| 158 | - |
|
| 159 | - try { |
|
| 160 | - $generated = $this->folder->getFile('generated'); |
|
| 161 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 162 | - $generated->delete(); |
|
| 163 | - } catch (NotFoundException $e) { |
|
| 164 | - // |
|
| 165 | - } |
|
| 166 | - $this->user->triggerChange('avatar', $file); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * remove the users avatar |
|
| 171 | - * @return void |
|
| 172 | - */ |
|
| 173 | - public function remove () { |
|
| 174 | - $avatars = $this->folder->getDirectoryListing(); |
|
| 175 | - |
|
| 176 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 177 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 178 | - |
|
| 179 | - foreach ($avatars as $avatar) { |
|
| 180 | - $avatar->delete(); |
|
| 181 | - } |
|
| 182 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 183 | - $this->user->triggerChange('avatar', ''); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @inheritdoc |
|
| 188 | - */ |
|
| 189 | - public function getFile($size) { |
|
| 190 | - try { |
|
| 191 | - $ext = $this->getExtension(); |
|
| 192 | - } catch (NotFoundException $e) { |
|
| 193 | - $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 194 | - $avatar = $this->folder->newFile('avatar.png'); |
|
| 195 | - $avatar->putContent($data); |
|
| 196 | - $ext = 'png'; |
|
| 197 | - |
|
| 198 | - $this->folder->newFile('generated'); |
|
| 199 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ($size === -1) { |
|
| 203 | - $path = 'avatar.' . $ext; |
|
| 204 | - } else { |
|
| 205 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - try { |
|
| 209 | - $file = $this->folder->getFile($path); |
|
| 210 | - } catch (NotFoundException $e) { |
|
| 211 | - if ($size <= 0) { |
|
| 212 | - throw new NotFoundException; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - if ($this->folder->fileExists('generated')) { |
|
| 216 | - $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 217 | - |
|
| 218 | - } else { |
|
| 219 | - $avatar = new OC_Image(); |
|
| 220 | - /** @var ISimpleFile $file */ |
|
| 221 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 222 | - $avatar->loadFromData($file->getContent()); |
|
| 223 | - $avatar->resize($size); |
|
| 224 | - $data = $avatar->data(); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - try { |
|
| 228 | - $file = $this->folder->newFile($path); |
|
| 229 | - $file->putContent($data); |
|
| 230 | - } catch (NotPermittedException $e) { |
|
| 231 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 232 | - throw new NotFoundException(); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 238 | - $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
| 239 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - return $file; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 247 | - * |
|
| 248 | - * @return string |
|
| 249 | - * @throws NotFoundException |
|
| 250 | - */ |
|
| 251 | - private function getExtension() { |
|
| 252 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
| 253 | - return 'jpg'; |
|
| 254 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 255 | - return 'png'; |
|
| 256 | - } |
|
| 257 | - throw new NotFoundException; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @param string $userDisplayName |
|
| 262 | - * @param int $size |
|
| 263 | - * @return string |
|
| 264 | - */ |
|
| 265 | - private function generateAvatar($userDisplayName, $size) { |
|
| 266 | - $text = strtoupper($userDisplayName[0]); |
|
| 267 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 268 | - |
|
| 269 | - $im = imagecreatetruecolor($size, $size); |
|
| 270 | - $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 271 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
| 272 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 273 | - |
|
| 274 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 275 | - |
|
| 276 | - $fontSize = $size * 0.4; |
|
| 277 | - $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 278 | - |
|
| 279 | - $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 280 | - $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 281 | - $x += 1; |
|
| 282 | - $y -= $box[7]; |
|
| 283 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 284 | - |
|
| 285 | - ob_start(); |
|
| 286 | - imagepng($im); |
|
| 287 | - $data = ob_get_contents(); |
|
| 288 | - ob_end_clean(); |
|
| 289 | - |
|
| 290 | - return $data; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param int $r |
|
| 295 | - * @param int $g |
|
| 296 | - * @param int $b |
|
| 297 | - * @return double[] Array containing h s l in [0, 1] range |
|
| 298 | - */ |
|
| 299 | - private function rgbToHsl($r, $g, $b) { |
|
| 300 | - $r /= 255.0; |
|
| 301 | - $g /= 255.0; |
|
| 302 | - $b /= 255.0; |
|
| 303 | - |
|
| 304 | - $max = max($r, $g, $b); |
|
| 305 | - $min = min($r, $g, $b); |
|
| 306 | - |
|
| 307 | - |
|
| 308 | - $h = ($max + $min) / 2.0; |
|
| 309 | - $l = ($max + $min) / 2.0; |
|
| 310 | - |
|
| 311 | - if($max === $min) { |
|
| 312 | - $h = $s = 0; // Achromatic |
|
| 313 | - } else { |
|
| 314 | - $d = $max - $min; |
|
| 315 | - $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 316 | - switch($max) { |
|
| 317 | - case $r: |
|
| 318 | - $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 319 | - break; |
|
| 320 | - case $g: |
|
| 321 | - $h = ($b - $r) / $d + 2.0; |
|
| 322 | - break; |
|
| 323 | - case $b: |
|
| 324 | - $h = ($r - $g) / $d + 4.0; |
|
| 325 | - break; |
|
| 326 | - } |
|
| 327 | - $h /= 6.0; |
|
| 328 | - } |
|
| 329 | - return [$h, $s, $l]; |
|
| 330 | - |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @param string $text |
|
| 335 | - * @return int[] Array containting r g b in the range [0, 255] |
|
| 336 | - */ |
|
| 337 | - private function avatarBackgroundColor($text) { |
|
| 338 | - $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 339 | - |
|
| 340 | - $hash = md5($hash); |
|
| 341 | - $hashChars = str_split($hash); |
|
| 342 | - |
|
| 343 | - |
|
| 344 | - // Init vars |
|
| 345 | - $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 346 | - $rgb = [0, 0, 0]; |
|
| 347 | - $sat = 0.70; |
|
| 348 | - $lum = 0.68; |
|
| 349 | - $modulo = 16; |
|
| 350 | - |
|
| 351 | - |
|
| 352 | - // Splitting evenly the string |
|
| 353 | - foreach($hashChars as $i => $char) { |
|
| 354 | - $result[$i % $modulo] .= intval($char, 16); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - // Converting our data into a usable rgb format |
|
| 358 | - // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 359 | - for($count = 1; $count < $modulo; $count++) { |
|
| 360 | - $rgb[$count%3] += (int)$result[$count]; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // Reduce values bigger than rgb requirements |
|
| 364 | - $rgb[0] %= 255; |
|
| 365 | - $rgb[1] %= 255; |
|
| 366 | - $rgb[2] %= 255; |
|
| 367 | - |
|
| 368 | - $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 369 | - |
|
| 370 | - // Classic formula to check the brightness for our eye |
|
| 371 | - // If too bright, lower the sat |
|
| 372 | - $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 373 | - if ($bright >= 200) { |
|
| 374 | - $sat = 0.60; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * @param double $h Hue in range [0, 1] |
|
| 382 | - * @param double $s Saturation in range [0, 1] |
|
| 383 | - * @param double $l Lightness in range [0, 1] |
|
| 384 | - * @return int[] Array containing r g b in the range [0, 255] |
|
| 385 | - */ |
|
| 386 | - private function hslToRgb($h, $s, $l){ |
|
| 387 | - $hue2rgb = function ($p, $q, $t){ |
|
| 388 | - if($t < 0) { |
|
| 389 | - $t += 1; |
|
| 390 | - } |
|
| 391 | - if($t > 1) { |
|
| 392 | - $t -= 1; |
|
| 393 | - } |
|
| 394 | - if($t < 1/6) { |
|
| 395 | - return $p + ($q - $p) * 6 * $t; |
|
| 396 | - } |
|
| 397 | - if($t < 1/2) { |
|
| 398 | - return $q; |
|
| 399 | - } |
|
| 400 | - if($t < 2/3) { |
|
| 401 | - return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 402 | - } |
|
| 403 | - return $p; |
|
| 404 | - }; |
|
| 405 | - |
|
| 406 | - if($s === 0){ |
|
| 407 | - $r = $l; |
|
| 408 | - $g = $l; |
|
| 409 | - $b = $l; // achromatic |
|
| 410 | - }else{ |
|
| 411 | - $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 412 | - $p = 2 * $l - $q; |
|
| 413 | - $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 414 | - $g = $hue2rgb($p, $q, $h); |
|
| 415 | - $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - public function userChanged($feature, $oldValue, $newValue) { |
|
| 422 | - // We only change the avatar on display name changes |
|
| 423 | - if ($feature !== 'displayName') { |
|
| 424 | - return; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - // If the avatar is not generated (so an uploaded image) we skip this |
|
| 428 | - if (!$this->folder->fileExists('generated')) { |
|
| 429 | - return; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - $this->remove(); |
|
| 433 | - } |
|
| 49 | + /** @var ISimpleFolder */ |
|
| 50 | + private $folder; |
|
| 51 | + /** @var IL10N */ |
|
| 52 | + private $l; |
|
| 53 | + /** @var User */ |
|
| 54 | + private $user; |
|
| 55 | + /** @var ILogger */ |
|
| 56 | + private $logger; |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + private $config; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * constructor |
|
| 62 | + * |
|
| 63 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | + * @param IL10N $l |
|
| 65 | + * @param User $user |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + * @param IConfig $config |
|
| 68 | + */ |
|
| 69 | + public function __construct(ISimpleFolder $folder, |
|
| 70 | + IL10N $l, |
|
| 71 | + $user, |
|
| 72 | + ILogger $logger, |
|
| 73 | + IConfig $config) { |
|
| 74 | + $this->folder = $folder; |
|
| 75 | + $this->l = $l; |
|
| 76 | + $this->user = $user; |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + $this->config = $config; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @inheritdoc |
|
| 83 | + */ |
|
| 84 | + public function get ($size = 64) { |
|
| 85 | + try { |
|
| 86 | + $file = $this->getFile($size); |
|
| 87 | + } catch (NotFoundException $e) { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $avatar = new OC_Image(); |
|
| 92 | + $avatar->loadFromData($file->getContent()); |
|
| 93 | + return $avatar; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Check if an avatar exists for the user |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + public function exists() { |
|
| 102 | + |
|
| 103 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * sets the users avatar |
|
| 108 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | + * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | + * @throws \Exception if the provided image is not valid |
|
| 111 | + * @throws NotSquareException if the image is not square |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function set ($data) { |
|
| 115 | + |
|
| 116 | + if($data instanceOf IImage) { |
|
| 117 | + $img = $data; |
|
| 118 | + $data = $img->data(); |
|
| 119 | + } else { |
|
| 120 | + $img = new OC_Image(); |
|
| 121 | + if (is_resource($data) && get_resource_type($data) === "gd") { |
|
| 122 | + $img->setResource($data); |
|
| 123 | + } elseif(is_resource($data)) { |
|
| 124 | + $img->loadFromFileHandle($data); |
|
| 125 | + } else { |
|
| 126 | + try { |
|
| 127 | + // detect if it is a path or maybe the images as string |
|
| 128 | + $result = @realpath($data); |
|
| 129 | + if ($result === false || $result === null) { |
|
| 130 | + $img->loadFromData($data); |
|
| 131 | + } else { |
|
| 132 | + $img->loadFromFile($data); |
|
| 133 | + } |
|
| 134 | + } catch (\Error $e) { |
|
| 135 | + $img->loadFromData($data); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + $type = substr($img->mimeType(), -3); |
|
| 140 | + if ($type === 'peg') { |
|
| 141 | + $type = 'jpg'; |
|
| 142 | + } |
|
| 143 | + if ($type !== 'jpg' && $type !== 'png') { |
|
| 144 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + if (!$img->valid()) { |
|
| 148 | + throw new \Exception($this->l->t('Invalid image')); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if (!($img->height() === $img->width())) { |
|
| 152 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $this->remove(); |
|
| 156 | + $file = $this->folder->newFile('avatar.'.$type); |
|
| 157 | + $file->putContent($data); |
|
| 158 | + |
|
| 159 | + try { |
|
| 160 | + $generated = $this->folder->getFile('generated'); |
|
| 161 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
| 162 | + $generated->delete(); |
|
| 163 | + } catch (NotFoundException $e) { |
|
| 164 | + // |
|
| 165 | + } |
|
| 166 | + $this->user->triggerChange('avatar', $file); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * remove the users avatar |
|
| 171 | + * @return void |
|
| 172 | + */ |
|
| 173 | + public function remove () { |
|
| 174 | + $avatars = $this->folder->getDirectoryListing(); |
|
| 175 | + |
|
| 176 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 177 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 178 | + |
|
| 179 | + foreach ($avatars as $avatar) { |
|
| 180 | + $avatar->delete(); |
|
| 181 | + } |
|
| 182 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 183 | + $this->user->triggerChange('avatar', ''); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @inheritdoc |
|
| 188 | + */ |
|
| 189 | + public function getFile($size) { |
|
| 190 | + try { |
|
| 191 | + $ext = $this->getExtension(); |
|
| 192 | + } catch (NotFoundException $e) { |
|
| 193 | + $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 194 | + $avatar = $this->folder->newFile('avatar.png'); |
|
| 195 | + $avatar->putContent($data); |
|
| 196 | + $ext = 'png'; |
|
| 197 | + |
|
| 198 | + $this->folder->newFile('generated'); |
|
| 199 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ($size === -1) { |
|
| 203 | + $path = 'avatar.' . $ext; |
|
| 204 | + } else { |
|
| 205 | + $path = 'avatar.' . $size . '.' . $ext; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + try { |
|
| 209 | + $file = $this->folder->getFile($path); |
|
| 210 | + } catch (NotFoundException $e) { |
|
| 211 | + if ($size <= 0) { |
|
| 212 | + throw new NotFoundException; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + if ($this->folder->fileExists('generated')) { |
|
| 216 | + $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 217 | + |
|
| 218 | + } else { |
|
| 219 | + $avatar = new OC_Image(); |
|
| 220 | + /** @var ISimpleFile $file */ |
|
| 221 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
| 222 | + $avatar->loadFromData($file->getContent()); |
|
| 223 | + $avatar->resize($size); |
|
| 224 | + $data = $avatar->data(); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + try { |
|
| 228 | + $file = $this->folder->newFile($path); |
|
| 229 | + $file->putContent($data); |
|
| 230 | + } catch (NotPermittedException $e) { |
|
| 231 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 232 | + throw new NotFoundException(); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
| 238 | + $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
| 239 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + return $file; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 247 | + * |
|
| 248 | + * @return string |
|
| 249 | + * @throws NotFoundException |
|
| 250 | + */ |
|
| 251 | + private function getExtension() { |
|
| 252 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
| 253 | + return 'jpg'; |
|
| 254 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 255 | + return 'png'; |
|
| 256 | + } |
|
| 257 | + throw new NotFoundException; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @param string $userDisplayName |
|
| 262 | + * @param int $size |
|
| 263 | + * @return string |
|
| 264 | + */ |
|
| 265 | + private function generateAvatar($userDisplayName, $size) { |
|
| 266 | + $text = strtoupper($userDisplayName[0]); |
|
| 267 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 268 | + |
|
| 269 | + $im = imagecreatetruecolor($size, $size); |
|
| 270 | + $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 271 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
| 272 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 273 | + |
|
| 274 | + $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 275 | + |
|
| 276 | + $fontSize = $size * 0.4; |
|
| 277 | + $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 278 | + |
|
| 279 | + $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 280 | + $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 281 | + $x += 1; |
|
| 282 | + $y -= $box[7]; |
|
| 283 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 284 | + |
|
| 285 | + ob_start(); |
|
| 286 | + imagepng($im); |
|
| 287 | + $data = ob_get_contents(); |
|
| 288 | + ob_end_clean(); |
|
| 289 | + |
|
| 290 | + return $data; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param int $r |
|
| 295 | + * @param int $g |
|
| 296 | + * @param int $b |
|
| 297 | + * @return double[] Array containing h s l in [0, 1] range |
|
| 298 | + */ |
|
| 299 | + private function rgbToHsl($r, $g, $b) { |
|
| 300 | + $r /= 255.0; |
|
| 301 | + $g /= 255.0; |
|
| 302 | + $b /= 255.0; |
|
| 303 | + |
|
| 304 | + $max = max($r, $g, $b); |
|
| 305 | + $min = min($r, $g, $b); |
|
| 306 | + |
|
| 307 | + |
|
| 308 | + $h = ($max + $min) / 2.0; |
|
| 309 | + $l = ($max + $min) / 2.0; |
|
| 310 | + |
|
| 311 | + if($max === $min) { |
|
| 312 | + $h = $s = 0; // Achromatic |
|
| 313 | + } else { |
|
| 314 | + $d = $max - $min; |
|
| 315 | + $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 316 | + switch($max) { |
|
| 317 | + case $r: |
|
| 318 | + $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 319 | + break; |
|
| 320 | + case $g: |
|
| 321 | + $h = ($b - $r) / $d + 2.0; |
|
| 322 | + break; |
|
| 323 | + case $b: |
|
| 324 | + $h = ($r - $g) / $d + 4.0; |
|
| 325 | + break; |
|
| 326 | + } |
|
| 327 | + $h /= 6.0; |
|
| 328 | + } |
|
| 329 | + return [$h, $s, $l]; |
|
| 330 | + |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @param string $text |
|
| 335 | + * @return int[] Array containting r g b in the range [0, 255] |
|
| 336 | + */ |
|
| 337 | + private function avatarBackgroundColor($text) { |
|
| 338 | + $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 339 | + |
|
| 340 | + $hash = md5($hash); |
|
| 341 | + $hashChars = str_split($hash); |
|
| 342 | + |
|
| 343 | + |
|
| 344 | + // Init vars |
|
| 345 | + $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 346 | + $rgb = [0, 0, 0]; |
|
| 347 | + $sat = 0.70; |
|
| 348 | + $lum = 0.68; |
|
| 349 | + $modulo = 16; |
|
| 350 | + |
|
| 351 | + |
|
| 352 | + // Splitting evenly the string |
|
| 353 | + foreach($hashChars as $i => $char) { |
|
| 354 | + $result[$i % $modulo] .= intval($char, 16); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + // Converting our data into a usable rgb format |
|
| 358 | + // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 359 | + for($count = 1; $count < $modulo; $count++) { |
|
| 360 | + $rgb[$count%3] += (int)$result[$count]; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // Reduce values bigger than rgb requirements |
|
| 364 | + $rgb[0] %= 255; |
|
| 365 | + $rgb[1] %= 255; |
|
| 366 | + $rgb[2] %= 255; |
|
| 367 | + |
|
| 368 | + $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 369 | + |
|
| 370 | + // Classic formula to check the brightness for our eye |
|
| 371 | + // If too bright, lower the sat |
|
| 372 | + $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 373 | + if ($bright >= 200) { |
|
| 374 | + $sat = 0.60; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @param double $h Hue in range [0, 1] |
|
| 382 | + * @param double $s Saturation in range [0, 1] |
|
| 383 | + * @param double $l Lightness in range [0, 1] |
|
| 384 | + * @return int[] Array containing r g b in the range [0, 255] |
|
| 385 | + */ |
|
| 386 | + private function hslToRgb($h, $s, $l){ |
|
| 387 | + $hue2rgb = function ($p, $q, $t){ |
|
| 388 | + if($t < 0) { |
|
| 389 | + $t += 1; |
|
| 390 | + } |
|
| 391 | + if($t > 1) { |
|
| 392 | + $t -= 1; |
|
| 393 | + } |
|
| 394 | + if($t < 1/6) { |
|
| 395 | + return $p + ($q - $p) * 6 * $t; |
|
| 396 | + } |
|
| 397 | + if($t < 1/2) { |
|
| 398 | + return $q; |
|
| 399 | + } |
|
| 400 | + if($t < 2/3) { |
|
| 401 | + return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 402 | + } |
|
| 403 | + return $p; |
|
| 404 | + }; |
|
| 405 | + |
|
| 406 | + if($s === 0){ |
|
| 407 | + $r = $l; |
|
| 408 | + $g = $l; |
|
| 409 | + $b = $l; // achromatic |
|
| 410 | + }else{ |
|
| 411 | + $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 412 | + $p = 2 * $l - $q; |
|
| 413 | + $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 414 | + $g = $hue2rgb($p, $q, $h); |
|
| 415 | + $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + public function userChanged($feature, $oldValue, $newValue) { |
|
| 422 | + // We only change the avatar on display name changes |
|
| 423 | + if ($feature !== 'displayName') { |
|
| 424 | + return; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + // If the avatar is not generated (so an uploaded image) we skip this |
|
| 428 | + if (!$this->folder->fileExists('generated')) { |
|
| 429 | + return; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + $this->remove(); |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | 435 | } |
@@ -43,556 +43,556 @@ discard block |
||
| 43 | 43 | * Class for basic image manipulation |
| 44 | 44 | */ |
| 45 | 45 | class OC_Image implements \OCP\IImage { |
| 46 | - /** @var false|resource */ |
|
| 47 | - protected $resource = false; // tmp resource. |
|
| 48 | - /** @var int */ |
|
| 49 | - protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | - /** @var string */ |
|
| 51 | - protected $mimeType = 'image/png'; // Default to png |
|
| 52 | - /** @var int */ |
|
| 53 | - protected $bitDepth = 24; |
|
| 54 | - /** @var null|string */ |
|
| 55 | - protected $filePath = null; |
|
| 56 | - /** @var finfo */ |
|
| 57 | - private $fileInfo; |
|
| 58 | - /** @var \OCP\ILogger */ |
|
| 59 | - private $logger; |
|
| 60 | - /** @var \OCP\IConfig */ |
|
| 61 | - private $config; |
|
| 62 | - /** @var array */ |
|
| 63 | - private $exif; |
|
| 46 | + /** @var false|resource */ |
|
| 47 | + protected $resource = false; // tmp resource. |
|
| 48 | + /** @var int */ |
|
| 49 | + protected $imageType = IMAGETYPE_PNG; // Default to png if file type isn't evident. |
|
| 50 | + /** @var string */ |
|
| 51 | + protected $mimeType = 'image/png'; // Default to png |
|
| 52 | + /** @var int */ |
|
| 53 | + protected $bitDepth = 24; |
|
| 54 | + /** @var null|string */ |
|
| 55 | + protected $filePath = null; |
|
| 56 | + /** @var finfo */ |
|
| 57 | + private $fileInfo; |
|
| 58 | + /** @var \OCP\ILogger */ |
|
| 59 | + private $logger; |
|
| 60 | + /** @var \OCP\IConfig */ |
|
| 61 | + private $config; |
|
| 62 | + /** @var array */ |
|
| 63 | + private $exif; |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Constructor. |
|
| 67 | - * |
|
| 68 | - * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | - * an imagecreate* function. |
|
| 70 | - * @param \OCP\ILogger $logger |
|
| 71 | - * @param \OCP\IConfig $config |
|
| 72 | - * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | - */ |
|
| 74 | - public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - if ($logger === null) { |
|
| 77 | - $this->logger = \OC::$server->getLogger(); |
|
| 78 | - } |
|
| 79 | - $this->config = $config; |
|
| 80 | - if ($config === null) { |
|
| 81 | - $this->config = \OC::$server->getConfig(); |
|
| 82 | - } |
|
| 65 | + /** |
|
| 66 | + * Constructor. |
|
| 67 | + * |
|
| 68 | + * @param resource|string $imageRef The path to a local file, a base64 encoded string or a resource created by |
|
| 69 | + * an imagecreate* function. |
|
| 70 | + * @param \OCP\ILogger $logger |
|
| 71 | + * @param \OCP\IConfig $config |
|
| 72 | + * @throws \InvalidArgumentException in case the $imageRef parameter is not null |
|
| 73 | + */ |
|
| 74 | + public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\IConfig $config = null) { |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + if ($logger === null) { |
|
| 77 | + $this->logger = \OC::$server->getLogger(); |
|
| 78 | + } |
|
| 79 | + $this->config = $config; |
|
| 80 | + if ($config === null) { |
|
| 81 | + $this->config = \OC::$server->getConfig(); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | - $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | - } |
|
| 84 | + if (\OC_Util::fileInfoLoaded()) { |
|
| 85 | + $this->fileInfo = new finfo(FILEINFO_MIME_TYPE); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - if ($imageRef !== null) { |
|
| 89 | - throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | - } |
|
| 91 | - } |
|
| 88 | + if ($imageRef !== null) { |
|
| 89 | + throw new \InvalidArgumentException('The first parameter in the constructor is not supported anymore. Please use any of the load* methods of the image object to load an image.'); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * Determine whether the object contains an image resource. |
|
| 95 | - * |
|
| 96 | - * @return bool |
|
| 97 | - */ |
|
| 98 | - public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | - return is_resource($this->resource); |
|
| 100 | - } |
|
| 93 | + /** |
|
| 94 | + * Determine whether the object contains an image resource. |
|
| 95 | + * |
|
| 96 | + * @return bool |
|
| 97 | + */ |
|
| 98 | + public function valid() { // apparently you can't name a method 'empty'... |
|
| 99 | + return is_resource($this->resource); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | - * |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function mimeType() { |
|
| 108 | - return $this->valid() ? $this->mimeType : ''; |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * Returns the MIME type of the image or an empty string if no image is loaded. |
|
| 104 | + * |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function mimeType() { |
|
| 108 | + return $this->valid() ? $this->mimeType : ''; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | - * |
|
| 114 | - * @return int |
|
| 115 | - */ |
|
| 116 | - public function width() { |
|
| 117 | - return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | - } |
|
| 111 | + /** |
|
| 112 | + * Returns the width of the image or -1 if no image is loaded. |
|
| 113 | + * |
|
| 114 | + * @return int |
|
| 115 | + */ |
|
| 116 | + public function width() { |
|
| 117 | + return $this->valid() ? imagesx($this->resource) : -1; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | - * |
|
| 123 | - * @return int |
|
| 124 | - */ |
|
| 125 | - public function height() { |
|
| 126 | - return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * Returns the height of the image or -1 if no image is loaded. |
|
| 122 | + * |
|
| 123 | + * @return int |
|
| 124 | + */ |
|
| 125 | + public function height() { |
|
| 126 | + return $this->valid() ? imagesy($this->resource) : -1; |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Returns the width when the image orientation is top-left. |
|
| 131 | - * |
|
| 132 | - * @return int |
|
| 133 | - */ |
|
| 134 | - public function widthTopLeft() { |
|
| 135 | - $o = $this->getOrientation(); |
|
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | - switch ($o) { |
|
| 138 | - case -1: |
|
| 139 | - case 1: |
|
| 140 | - case 2: // Not tested |
|
| 141 | - case 3: |
|
| 142 | - case 4: // Not tested |
|
| 143 | - return $this->width(); |
|
| 144 | - case 5: // Not tested |
|
| 145 | - case 6: |
|
| 146 | - case 7: // Not tested |
|
| 147 | - case 8: |
|
| 148 | - return $this->height(); |
|
| 149 | - } |
|
| 150 | - return $this->width(); |
|
| 151 | - } |
|
| 129 | + /** |
|
| 130 | + * Returns the width when the image orientation is top-left. |
|
| 131 | + * |
|
| 132 | + * @return int |
|
| 133 | + */ |
|
| 134 | + public function widthTopLeft() { |
|
| 135 | + $o = $this->getOrientation(); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 137 | + switch ($o) { |
|
| 138 | + case -1: |
|
| 139 | + case 1: |
|
| 140 | + case 2: // Not tested |
|
| 141 | + case 3: |
|
| 142 | + case 4: // Not tested |
|
| 143 | + return $this->width(); |
|
| 144 | + case 5: // Not tested |
|
| 145 | + case 6: |
|
| 146 | + case 7: // Not tested |
|
| 147 | + case 8: |
|
| 148 | + return $this->height(); |
|
| 149 | + } |
|
| 150 | + return $this->width(); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Returns the height when the image orientation is top-left. |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public function heightTopLeft() { |
|
| 159 | - $o = $this->getOrientation(); |
|
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | - switch ($o) { |
|
| 162 | - case -1: |
|
| 163 | - case 1: |
|
| 164 | - case 2: // Not tested |
|
| 165 | - case 3: |
|
| 166 | - case 4: // Not tested |
|
| 167 | - return $this->height(); |
|
| 168 | - case 5: // Not tested |
|
| 169 | - case 6: |
|
| 170 | - case 7: // Not tested |
|
| 171 | - case 8: |
|
| 172 | - return $this->width(); |
|
| 173 | - } |
|
| 174 | - return $this->height(); |
|
| 175 | - } |
|
| 153 | + /** |
|
| 154 | + * Returns the height when the image orientation is top-left. |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public function heightTopLeft() { |
|
| 159 | + $o = $this->getOrientation(); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 161 | + switch ($o) { |
|
| 162 | + case -1: |
|
| 163 | + case 1: |
|
| 164 | + case 2: // Not tested |
|
| 165 | + case 3: |
|
| 166 | + case 4: // Not tested |
|
| 167 | + return $this->height(); |
|
| 168 | + case 5: // Not tested |
|
| 169 | + case 6: |
|
| 170 | + case 7: // Not tested |
|
| 171 | + case 8: |
|
| 172 | + return $this->width(); |
|
| 173 | + } |
|
| 174 | + return $this->height(); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - /** |
|
| 178 | - * Outputs the image. |
|
| 179 | - * |
|
| 180 | - * @param string $mimeType |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function show($mimeType = null) { |
|
| 184 | - if ($mimeType === null) { |
|
| 185 | - $mimeType = $this->mimeType(); |
|
| 186 | - } |
|
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 188 | - return $this->_output(null, $mimeType); |
|
| 189 | - } |
|
| 177 | + /** |
|
| 178 | + * Outputs the image. |
|
| 179 | + * |
|
| 180 | + * @param string $mimeType |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function show($mimeType = null) { |
|
| 184 | + if ($mimeType === null) { |
|
| 185 | + $mimeType = $this->mimeType(); |
|
| 186 | + } |
|
| 187 | + header('Content-Type: ' . $mimeType); |
|
| 188 | + return $this->_output(null, $mimeType); |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | - /** |
|
| 192 | - * Saves the image. |
|
| 193 | - * |
|
| 194 | - * @param string $filePath |
|
| 195 | - * @param string $mimeType |
|
| 196 | - * @return bool |
|
| 197 | - */ |
|
| 191 | + /** |
|
| 192 | + * Saves the image. |
|
| 193 | + * |
|
| 194 | + * @param string $filePath |
|
| 195 | + * @param string $mimeType |
|
| 196 | + * @return bool |
|
| 197 | + */ |
|
| 198 | 198 | |
| 199 | - public function save($filePath = null, $mimeType = null) { |
|
| 200 | - if ($mimeType === null) { |
|
| 201 | - $mimeType = $this->mimeType(); |
|
| 202 | - } |
|
| 203 | - if ($filePath === null) { |
|
| 204 | - if ($this->filePath === null) { |
|
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | - return false; |
|
| 207 | - } else { |
|
| 208 | - $filePath = $this->filePath; |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - return $this->_output($filePath, $mimeType); |
|
| 212 | - } |
|
| 199 | + public function save($filePath = null, $mimeType = null) { |
|
| 200 | + if ($mimeType === null) { |
|
| 201 | + $mimeType = $this->mimeType(); |
|
| 202 | + } |
|
| 203 | + if ($filePath === null) { |
|
| 204 | + if ($this->filePath === null) { |
|
| 205 | + $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 206 | + return false; |
|
| 207 | + } else { |
|
| 208 | + $filePath = $this->filePath; |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + return $this->_output($filePath, $mimeType); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - /** |
|
| 215 | - * Outputs/saves the image. |
|
| 216 | - * |
|
| 217 | - * @param string $filePath |
|
| 218 | - * @param string $mimeType |
|
| 219 | - * @return bool |
|
| 220 | - * @throws Exception |
|
| 221 | - */ |
|
| 222 | - private function _output($filePath = null, $mimeType = null) { |
|
| 223 | - if ($filePath) { |
|
| 224 | - if (!file_exists(dirname($filePath))) { |
|
| 225 | - mkdir(dirname($filePath), 0777, true); |
|
| 226 | - } |
|
| 227 | - $isWritable = is_writable(dirname($filePath)); |
|
| 228 | - if (!$isWritable) { |
|
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | - return false; |
|
| 231 | - } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | - return false; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - if (!$this->valid()) { |
|
| 237 | - return false; |
|
| 238 | - } |
|
| 214 | + /** |
|
| 215 | + * Outputs/saves the image. |
|
| 216 | + * |
|
| 217 | + * @param string $filePath |
|
| 218 | + * @param string $mimeType |
|
| 219 | + * @return bool |
|
| 220 | + * @throws Exception |
|
| 221 | + */ |
|
| 222 | + private function _output($filePath = null, $mimeType = null) { |
|
| 223 | + if ($filePath) { |
|
| 224 | + if (!file_exists(dirname($filePath))) { |
|
| 225 | + mkdir(dirname($filePath), 0777, true); |
|
| 226 | + } |
|
| 227 | + $isWritable = is_writable(dirname($filePath)); |
|
| 228 | + if (!$isWritable) { |
|
| 229 | + $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 230 | + return false; |
|
| 231 | + } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
|
| 232 | + $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 233 | + return false; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + if (!$this->valid()) { |
|
| 237 | + return false; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - $imageType = $this->imageType; |
|
| 241 | - if ($mimeType !== null) { |
|
| 242 | - switch ($mimeType) { |
|
| 243 | - case 'image/gif': |
|
| 244 | - $imageType = IMAGETYPE_GIF; |
|
| 245 | - break; |
|
| 246 | - case 'image/jpeg': |
|
| 247 | - $imageType = IMAGETYPE_JPEG; |
|
| 248 | - break; |
|
| 249 | - case 'image/png': |
|
| 250 | - $imageType = IMAGETYPE_PNG; |
|
| 251 | - break; |
|
| 252 | - case 'image/x-xbitmap': |
|
| 253 | - $imageType = IMAGETYPE_XBM; |
|
| 254 | - break; |
|
| 255 | - case 'image/bmp': |
|
| 256 | - case 'image/x-ms-bmp': |
|
| 257 | - $imageType = IMAGETYPE_BMP; |
|
| 258 | - break; |
|
| 259 | - default: |
|
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | - } |
|
| 262 | - } |
|
| 240 | + $imageType = $this->imageType; |
|
| 241 | + if ($mimeType !== null) { |
|
| 242 | + switch ($mimeType) { |
|
| 243 | + case 'image/gif': |
|
| 244 | + $imageType = IMAGETYPE_GIF; |
|
| 245 | + break; |
|
| 246 | + case 'image/jpeg': |
|
| 247 | + $imageType = IMAGETYPE_JPEG; |
|
| 248 | + break; |
|
| 249 | + case 'image/png': |
|
| 250 | + $imageType = IMAGETYPE_PNG; |
|
| 251 | + break; |
|
| 252 | + case 'image/x-xbitmap': |
|
| 253 | + $imageType = IMAGETYPE_XBM; |
|
| 254 | + break; |
|
| 255 | + case 'image/bmp': |
|
| 256 | + case 'image/x-ms-bmp': |
|
| 257 | + $imageType = IMAGETYPE_BMP; |
|
| 258 | + break; |
|
| 259 | + default: |
|
| 260 | + throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - switch ($imageType) { |
|
| 265 | - case IMAGETYPE_GIF: |
|
| 266 | - $retVal = imagegif($this->resource, $filePath); |
|
| 267 | - break; |
|
| 268 | - case IMAGETYPE_JPEG: |
|
| 269 | - $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | - break; |
|
| 271 | - case IMAGETYPE_PNG: |
|
| 272 | - $retVal = imagepng($this->resource, $filePath); |
|
| 273 | - break; |
|
| 274 | - case IMAGETYPE_XBM: |
|
| 275 | - if (function_exists('imagexbm')) { |
|
| 276 | - $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | - } else { |
|
| 278 | - throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | - } |
|
| 264 | + switch ($imageType) { |
|
| 265 | + case IMAGETYPE_GIF: |
|
| 266 | + $retVal = imagegif($this->resource, $filePath); |
|
| 267 | + break; |
|
| 268 | + case IMAGETYPE_JPEG: |
|
| 269 | + $retVal = imagejpeg($this->resource, $filePath, $this->getJpegQuality()); |
|
| 270 | + break; |
|
| 271 | + case IMAGETYPE_PNG: |
|
| 272 | + $retVal = imagepng($this->resource, $filePath); |
|
| 273 | + break; |
|
| 274 | + case IMAGETYPE_XBM: |
|
| 275 | + if (function_exists('imagexbm')) { |
|
| 276 | + $retVal = imagexbm($this->resource, $filePath); |
|
| 277 | + } else { |
|
| 278 | + throw new Exception('\OC_Image::_output(): imagexbm() is not supported.'); |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | - break; |
|
| 282 | - case IMAGETYPE_WBMP: |
|
| 283 | - $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | - break; |
|
| 285 | - case IMAGETYPE_BMP: |
|
| 286 | - $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | - break; |
|
| 288 | - default: |
|
| 289 | - $retVal = imagepng($this->resource, $filePath); |
|
| 290 | - } |
|
| 291 | - return $retVal; |
|
| 292 | - } |
|
| 281 | + break; |
|
| 282 | + case IMAGETYPE_WBMP: |
|
| 283 | + $retVal = imagewbmp($this->resource, $filePath); |
|
| 284 | + break; |
|
| 285 | + case IMAGETYPE_BMP: |
|
| 286 | + $retVal = imagebmp($this->resource, $filePath, $this->bitDepth); |
|
| 287 | + break; |
|
| 288 | + default: |
|
| 289 | + $retVal = imagepng($this->resource, $filePath); |
|
| 290 | + } |
|
| 291 | + return $retVal; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Prints the image when called as $image(). |
|
| 296 | - */ |
|
| 297 | - public function __invoke() { |
|
| 298 | - return $this->show(); |
|
| 299 | - } |
|
| 294 | + /** |
|
| 295 | + * Prints the image when called as $image(). |
|
| 296 | + */ |
|
| 297 | + public function __invoke() { |
|
| 298 | + return $this->show(); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * @param resource Returns the image resource in any. |
|
| 303 | - * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | - */ |
|
| 305 | - public function setResource($resource) { |
|
| 306 | - if (get_resource_type($resource) === 'gd') { |
|
| 307 | - $this->resource = $resource; |
|
| 308 | - return; |
|
| 309 | - } |
|
| 310 | - throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | - } |
|
| 301 | + /** |
|
| 302 | + * @param resource Returns the image resource in any. |
|
| 303 | + * @throws \InvalidArgumentException in case the supplied resource does not have the type "gd" |
|
| 304 | + */ |
|
| 305 | + public function setResource($resource) { |
|
| 306 | + if (get_resource_type($resource) === 'gd') { |
|
| 307 | + $this->resource = $resource; |
|
| 308 | + return; |
|
| 309 | + } |
|
| 310 | + throw new \InvalidArgumentException('Supplied resource is not of type "gd".'); |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** |
|
| 314 | - * @return resource Returns the image resource in any. |
|
| 315 | - */ |
|
| 316 | - public function resource() { |
|
| 317 | - return $this->resource; |
|
| 318 | - } |
|
| 313 | + /** |
|
| 314 | + * @return resource Returns the image resource in any. |
|
| 315 | + */ |
|
| 316 | + public function resource() { |
|
| 317 | + return $this->resource; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - /** |
|
| 321 | - * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | - * if the data is not valid. |
|
| 323 | - */ |
|
| 324 | - public function dataMimeType() { |
|
| 325 | - if (!$this->valid()) { |
|
| 326 | - return ''; |
|
| 327 | - } |
|
| 320 | + /** |
|
| 321 | + * @return string Returns the mimetype of the data. Returns the empty string |
|
| 322 | + * if the data is not valid. |
|
| 323 | + */ |
|
| 324 | + public function dataMimeType() { |
|
| 325 | + if (!$this->valid()) { |
|
| 326 | + return ''; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - switch ($this->mimeType) { |
|
| 330 | - case 'image/png': |
|
| 331 | - case 'image/jpeg': |
|
| 332 | - case 'image/gif': |
|
| 333 | - return $this->mimeType; |
|
| 334 | - default: |
|
| 335 | - return 'image/png'; |
|
| 336 | - } |
|
| 337 | - } |
|
| 329 | + switch ($this->mimeType) { |
|
| 330 | + case 'image/png': |
|
| 331 | + case 'image/jpeg': |
|
| 332 | + case 'image/gif': |
|
| 333 | + return $this->mimeType; |
|
| 334 | + default: |
|
| 335 | + return 'image/png'; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * @return null|string Returns the raw image data. |
|
| 341 | - */ |
|
| 342 | - public function data() { |
|
| 343 | - if (!$this->valid()) { |
|
| 344 | - return null; |
|
| 345 | - } |
|
| 346 | - ob_start(); |
|
| 347 | - switch ($this->mimeType) { |
|
| 348 | - case "image/png": |
|
| 349 | - $res = imagepng($this->resource); |
|
| 350 | - break; |
|
| 351 | - case "image/jpeg": |
|
| 352 | - $quality = $this->getJpegQuality(); |
|
| 353 | - if ($quality !== null) { |
|
| 354 | - $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | - } else { |
|
| 356 | - $res = imagejpeg($this->resource); |
|
| 357 | - } |
|
| 358 | - break; |
|
| 359 | - case "image/gif": |
|
| 360 | - $res = imagegif($this->resource); |
|
| 361 | - break; |
|
| 362 | - default: |
|
| 363 | - $res = imagepng($this->resource); |
|
| 364 | - $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - if (!$res) { |
|
| 368 | - $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | - } |
|
| 370 | - return ob_get_clean(); |
|
| 371 | - } |
|
| 339 | + /** |
|
| 340 | + * @return null|string Returns the raw image data. |
|
| 341 | + */ |
|
| 342 | + public function data() { |
|
| 343 | + if (!$this->valid()) { |
|
| 344 | + return null; |
|
| 345 | + } |
|
| 346 | + ob_start(); |
|
| 347 | + switch ($this->mimeType) { |
|
| 348 | + case "image/png": |
|
| 349 | + $res = imagepng($this->resource); |
|
| 350 | + break; |
|
| 351 | + case "image/jpeg": |
|
| 352 | + $quality = $this->getJpegQuality(); |
|
| 353 | + if ($quality !== null) { |
|
| 354 | + $res = imagejpeg($this->resource, null, $quality); |
|
| 355 | + } else { |
|
| 356 | + $res = imagejpeg($this->resource); |
|
| 357 | + } |
|
| 358 | + break; |
|
| 359 | + case "image/gif": |
|
| 360 | + $res = imagegif($this->resource); |
|
| 361 | + break; |
|
| 362 | + default: |
|
| 363 | + $res = imagepng($this->resource); |
|
| 364 | + $this->logger->info('OC_Image->data. Could not guess mime-type, defaulting to png', array('app' => 'core')); |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + if (!$res) { |
|
| 368 | + $this->logger->error('OC_Image->data. Error getting image data.', array('app' => 'core')); |
|
| 369 | + } |
|
| 370 | + return ob_get_clean(); |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - /** |
|
| 374 | - * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | - */ |
|
| 376 | - public function __toString() { |
|
| 377 | - return base64_encode($this->data()); |
|
| 378 | - } |
|
| 373 | + /** |
|
| 374 | + * @return string - base64 encoded, which is suitable for embedding in a VCard. |
|
| 375 | + */ |
|
| 376 | + public function __toString() { |
|
| 377 | + return base64_encode($this->data()); |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * @return int|null |
|
| 382 | - */ |
|
| 383 | - protected function getJpegQuality() { |
|
| 384 | - $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | - if ($quality !== null) { |
|
| 386 | - $quality = min(100, max(10, (int) $quality)); |
|
| 387 | - } |
|
| 388 | - return $quality; |
|
| 389 | - } |
|
| 380 | + /** |
|
| 381 | + * @return int|null |
|
| 382 | + */ |
|
| 383 | + protected function getJpegQuality() { |
|
| 384 | + $quality = $this->config->getAppValue('preview', 'jpeg_quality', 90); |
|
| 385 | + if ($quality !== null) { |
|
| 386 | + $quality = min(100, max(10, (int) $quality)); |
|
| 387 | + } |
|
| 388 | + return $quality; |
|
| 389 | + } |
|
| 390 | 390 | |
| 391 | - /** |
|
| 392 | - * (I'm open for suggestions on better method name ;) |
|
| 393 | - * Get the orientation based on EXIF data. |
|
| 394 | - * |
|
| 395 | - * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | - */ |
|
| 397 | - public function getOrientation() { |
|
| 398 | - if ($this->exif !== null) { |
|
| 399 | - return $this->exif['Orientation']; |
|
| 400 | - } |
|
| 391 | + /** |
|
| 392 | + * (I'm open for suggestions on better method name ;) |
|
| 393 | + * Get the orientation based on EXIF data. |
|
| 394 | + * |
|
| 395 | + * @return int The orientation or -1 if no EXIF data is available. |
|
| 396 | + */ |
|
| 397 | + public function getOrientation() { |
|
| 398 | + if ($this->exif !== null) { |
|
| 399 | + return $this->exif['Orientation']; |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | - $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | - return -1; |
|
| 405 | - } |
|
| 406 | - if (!is_callable('exif_read_data')) { |
|
| 407 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | - return -1; |
|
| 409 | - } |
|
| 410 | - if (!$this->valid()) { |
|
| 411 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | - return -1; |
|
| 413 | - } |
|
| 414 | - if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | - $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | - return -1; |
|
| 417 | - } |
|
| 418 | - $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | - if (!$exif) { |
|
| 420 | - return -1; |
|
| 421 | - } |
|
| 422 | - if (!isset($exif['Orientation'])) { |
|
| 423 | - return -1; |
|
| 424 | - } |
|
| 425 | - $this->exif = $exif; |
|
| 426 | - return $exif['Orientation']; |
|
| 427 | - } |
|
| 402 | + if ($this->imageType !== IMAGETYPE_JPEG) { |
|
| 403 | + $this->logger->debug('OC_Image->fixOrientation() Image is not a JPEG.', array('app' => 'core')); |
|
| 404 | + return -1; |
|
| 405 | + } |
|
| 406 | + if (!is_callable('exif_read_data')) { |
|
| 407 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 408 | + return -1; |
|
| 409 | + } |
|
| 410 | + if (!$this->valid()) { |
|
| 411 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 412 | + return -1; |
|
| 413 | + } |
|
| 414 | + if (is_null($this->filePath) || !is_readable($this->filePath)) { |
|
| 415 | + $this->logger->debug('OC_Image->fixOrientation() No readable file path set.', array('app' => 'core')); |
|
| 416 | + return -1; |
|
| 417 | + } |
|
| 418 | + $exif = @exif_read_data($this->filePath, 'IFD0'); |
|
| 419 | + if (!$exif) { |
|
| 420 | + return -1; |
|
| 421 | + } |
|
| 422 | + if (!isset($exif['Orientation'])) { |
|
| 423 | + return -1; |
|
| 424 | + } |
|
| 425 | + $this->exif = $exif; |
|
| 426 | + return $exif['Orientation']; |
|
| 427 | + } |
|
| 428 | 428 | |
| 429 | - public function readExif($data) { |
|
| 430 | - if (!is_callable('exif_read_data')) { |
|
| 431 | - $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | - return; |
|
| 433 | - } |
|
| 434 | - if (!$this->valid()) { |
|
| 435 | - $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | - return; |
|
| 437 | - } |
|
| 429 | + public function readExif($data) { |
|
| 430 | + if (!is_callable('exif_read_data')) { |
|
| 431 | + $this->logger->debug('OC_Image->fixOrientation() Exif module not enabled.', array('app' => 'core')); |
|
| 432 | + return; |
|
| 433 | + } |
|
| 434 | + if (!$this->valid()) { |
|
| 435 | + $this->logger->debug('OC_Image->fixOrientation() No image loaded.', array('app' => 'core')); |
|
| 436 | + return; |
|
| 437 | + } |
|
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | - if (!$exif) { |
|
| 441 | - return; |
|
| 442 | - } |
|
| 443 | - if (!isset($exif['Orientation'])) { |
|
| 444 | - return; |
|
| 445 | - } |
|
| 446 | - $this->exif = $exif; |
|
| 447 | - } |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 440 | + if (!$exif) { |
|
| 441 | + return; |
|
| 442 | + } |
|
| 443 | + if (!isset($exif['Orientation'])) { |
|
| 444 | + return; |
|
| 445 | + } |
|
| 446 | + $this->exif = $exif; |
|
| 447 | + } |
|
| 448 | 448 | |
| 449 | - /** |
|
| 450 | - * (I'm open for suggestions on better method name ;) |
|
| 451 | - * Fixes orientation based on EXIF data. |
|
| 452 | - * |
|
| 453 | - * @return bool |
|
| 454 | - */ |
|
| 455 | - public function fixOrientation() { |
|
| 456 | - $o = $this->getOrientation(); |
|
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | - $rotate = 0; |
|
| 459 | - $flip = false; |
|
| 460 | - switch ($o) { |
|
| 461 | - case -1: |
|
| 462 | - return false; //Nothing to fix |
|
| 463 | - case 1: |
|
| 464 | - $rotate = 0; |
|
| 465 | - break; |
|
| 466 | - case 2: |
|
| 467 | - $rotate = 0; |
|
| 468 | - $flip = true; |
|
| 469 | - break; |
|
| 470 | - case 3: |
|
| 471 | - $rotate = 180; |
|
| 472 | - break; |
|
| 473 | - case 4: |
|
| 474 | - $rotate = 180; |
|
| 475 | - $flip = true; |
|
| 476 | - break; |
|
| 477 | - case 5: |
|
| 478 | - $rotate = 90; |
|
| 479 | - $flip = true; |
|
| 480 | - break; |
|
| 481 | - case 6: |
|
| 482 | - $rotate = 270; |
|
| 483 | - break; |
|
| 484 | - case 7: |
|
| 485 | - $rotate = 270; |
|
| 486 | - $flip = true; |
|
| 487 | - break; |
|
| 488 | - case 8: |
|
| 489 | - $rotate = 90; |
|
| 490 | - break; |
|
| 491 | - } |
|
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 493 | - imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | - } |
|
| 495 | - if ($rotate) { |
|
| 496 | - $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | - if ($res) { |
|
| 498 | - if (imagealphablending($res, true)) { |
|
| 499 | - if (imagesavealpha($res, true)) { |
|
| 500 | - imagedestroy($this->resource); |
|
| 501 | - $this->resource = $res; |
|
| 502 | - return true; |
|
| 503 | - } else { |
|
| 504 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | - return false; |
|
| 506 | - } |
|
| 507 | - } else { |
|
| 508 | - $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | - return false; |
|
| 510 | - } |
|
| 511 | - } else { |
|
| 512 | - $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | - return false; |
|
| 514 | - } |
|
| 515 | - } |
|
| 516 | - return false; |
|
| 517 | - } |
|
| 449 | + /** |
|
| 450 | + * (I'm open for suggestions on better method name ;) |
|
| 451 | + * Fixes orientation based on EXIF data. |
|
| 452 | + * |
|
| 453 | + * @return bool |
|
| 454 | + */ |
|
| 455 | + public function fixOrientation() { |
|
| 456 | + $o = $this->getOrientation(); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 458 | + $rotate = 0; |
|
| 459 | + $flip = false; |
|
| 460 | + switch ($o) { |
|
| 461 | + case -1: |
|
| 462 | + return false; //Nothing to fix |
|
| 463 | + case 1: |
|
| 464 | + $rotate = 0; |
|
| 465 | + break; |
|
| 466 | + case 2: |
|
| 467 | + $rotate = 0; |
|
| 468 | + $flip = true; |
|
| 469 | + break; |
|
| 470 | + case 3: |
|
| 471 | + $rotate = 180; |
|
| 472 | + break; |
|
| 473 | + case 4: |
|
| 474 | + $rotate = 180; |
|
| 475 | + $flip = true; |
|
| 476 | + break; |
|
| 477 | + case 5: |
|
| 478 | + $rotate = 90; |
|
| 479 | + $flip = true; |
|
| 480 | + break; |
|
| 481 | + case 6: |
|
| 482 | + $rotate = 270; |
|
| 483 | + break; |
|
| 484 | + case 7: |
|
| 485 | + $rotate = 270; |
|
| 486 | + $flip = true; |
|
| 487 | + break; |
|
| 488 | + case 8: |
|
| 489 | + $rotate = 90; |
|
| 490 | + break; |
|
| 491 | + } |
|
| 492 | + if($flip && function_exists('imageflip')) { |
|
| 493 | + imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
|
| 494 | + } |
|
| 495 | + if ($rotate) { |
|
| 496 | + $res = imagerotate($this->resource, $rotate, 0); |
|
| 497 | + if ($res) { |
|
| 498 | + if (imagealphablending($res, true)) { |
|
| 499 | + if (imagesavealpha($res, true)) { |
|
| 500 | + imagedestroy($this->resource); |
|
| 501 | + $this->resource = $res; |
|
| 502 | + return true; |
|
| 503 | + } else { |
|
| 504 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-saving', array('app' => 'core')); |
|
| 505 | + return false; |
|
| 506 | + } |
|
| 507 | + } else { |
|
| 508 | + $this->logger->debug('OC_Image->fixOrientation() Error during alpha-blending', array('app' => 'core')); |
|
| 509 | + return false; |
|
| 510 | + } |
|
| 511 | + } else { |
|
| 512 | + $this->logger->debug('OC_Image->fixOrientation() Error during orientation fixing', array('app' => 'core')); |
|
| 513 | + return false; |
|
| 514 | + } |
|
| 515 | + } |
|
| 516 | + return false; |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - /** |
|
| 520 | - * Loads an image from an open file handle. |
|
| 521 | - * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | - * |
|
| 523 | - * @param resource $handle |
|
| 524 | - * @return resource|false An image resource or false on error |
|
| 525 | - */ |
|
| 526 | - public function loadFromFileHandle($handle) { |
|
| 527 | - $contents = stream_get_contents($handle); |
|
| 528 | - if ($this->loadFromData($contents)) { |
|
| 529 | - return $this->resource; |
|
| 530 | - } |
|
| 531 | - return false; |
|
| 532 | - } |
|
| 519 | + /** |
|
| 520 | + * Loads an image from an open file handle. |
|
| 521 | + * It is the responsibility of the caller to position the pointer at the correct place and to close the handle again. |
|
| 522 | + * |
|
| 523 | + * @param resource $handle |
|
| 524 | + * @return resource|false An image resource or false on error |
|
| 525 | + */ |
|
| 526 | + public function loadFromFileHandle($handle) { |
|
| 527 | + $contents = stream_get_contents($handle); |
|
| 528 | + if ($this->loadFromData($contents)) { |
|
| 529 | + return $this->resource; |
|
| 530 | + } |
|
| 531 | + return false; |
|
| 532 | + } |
|
| 533 | 533 | |
| 534 | - /** |
|
| 535 | - * Loads an image from a local file. |
|
| 536 | - * |
|
| 537 | - * @param bool|string $imagePath The path to a local file. |
|
| 538 | - * @return bool|resource An image resource or false on error |
|
| 539 | - */ |
|
| 540 | - public function loadFromFile($imagePath = false) { |
|
| 541 | - // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | - if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | - return false; |
|
| 544 | - } |
|
| 545 | - $iType = exif_imagetype($imagePath); |
|
| 546 | - switch ($iType) { |
|
| 547 | - case IMAGETYPE_GIF: |
|
| 548 | - if (imagetypes() & IMG_GIF) { |
|
| 549 | - $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | - // Preserve transparency |
|
| 551 | - imagealphablending($this->resource, true); |
|
| 552 | - imagesavealpha($this->resource, true); |
|
| 553 | - } else { |
|
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | - } |
|
| 556 | - break; |
|
| 557 | - case IMAGETYPE_JPEG: |
|
| 558 | - if (imagetypes() & IMG_JPG) { |
|
| 559 | - if (getimagesize($imagePath) !== false) { |
|
| 560 | - $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | - } else { |
|
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | - } |
|
| 564 | - } else { |
|
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | - } |
|
| 567 | - break; |
|
| 568 | - case IMAGETYPE_PNG: |
|
| 569 | - if (imagetypes() & IMG_PNG) { |
|
| 570 | - $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | - // Preserve transparency |
|
| 572 | - imagealphablending($this->resource, true); |
|
| 573 | - imagesavealpha($this->resource, true); |
|
| 574 | - } else { |
|
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | - } |
|
| 577 | - break; |
|
| 578 | - case IMAGETYPE_XBM: |
|
| 579 | - if (imagetypes() & IMG_XPM) { |
|
| 580 | - $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | - } else { |
|
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | - } |
|
| 584 | - break; |
|
| 585 | - case IMAGETYPE_WBMP: |
|
| 586 | - if (imagetypes() & IMG_WBMP) { |
|
| 587 | - $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | - } else { |
|
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | - } |
|
| 591 | - break; |
|
| 592 | - case IMAGETYPE_BMP: |
|
| 593 | - $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | - break; |
|
| 595 | - /* |
|
| 534 | + /** |
|
| 535 | + * Loads an image from a local file. |
|
| 536 | + * |
|
| 537 | + * @param bool|string $imagePath The path to a local file. |
|
| 538 | + * @return bool|resource An image resource or false on error |
|
| 539 | + */ |
|
| 540 | + public function loadFromFile($imagePath = false) { |
|
| 541 | + // exif_imagetype throws "read error!" if file is less than 12 byte |
|
| 542 | + if (!@is_file($imagePath) || !file_exists($imagePath) || filesize($imagePath) < 12 || !is_readable($imagePath)) { |
|
| 543 | + return false; |
|
| 544 | + } |
|
| 545 | + $iType = exif_imagetype($imagePath); |
|
| 546 | + switch ($iType) { |
|
| 547 | + case IMAGETYPE_GIF: |
|
| 548 | + if (imagetypes() & IMG_GIF) { |
|
| 549 | + $this->resource = imagecreatefromgif($imagePath); |
|
| 550 | + // Preserve transparency |
|
| 551 | + imagealphablending($this->resource, true); |
|
| 552 | + imagesavealpha($this->resource, true); |
|
| 553 | + } else { |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 555 | + } |
|
| 556 | + break; |
|
| 557 | + case IMAGETYPE_JPEG: |
|
| 558 | + if (imagetypes() & IMG_JPG) { |
|
| 559 | + if (getimagesize($imagePath) !== false) { |
|
| 560 | + $this->resource = @imagecreatefromjpeg($imagePath); |
|
| 561 | + } else { |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 563 | + } |
|
| 564 | + } else { |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 566 | + } |
|
| 567 | + break; |
|
| 568 | + case IMAGETYPE_PNG: |
|
| 569 | + if (imagetypes() & IMG_PNG) { |
|
| 570 | + $this->resource = @imagecreatefrompng($imagePath); |
|
| 571 | + // Preserve transparency |
|
| 572 | + imagealphablending($this->resource, true); |
|
| 573 | + imagesavealpha($this->resource, true); |
|
| 574 | + } else { |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 576 | + } |
|
| 577 | + break; |
|
| 578 | + case IMAGETYPE_XBM: |
|
| 579 | + if (imagetypes() & IMG_XPM) { |
|
| 580 | + $this->resource = @imagecreatefromxbm($imagePath); |
|
| 581 | + } else { |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 583 | + } |
|
| 584 | + break; |
|
| 585 | + case IMAGETYPE_WBMP: |
|
| 586 | + if (imagetypes() & IMG_WBMP) { |
|
| 587 | + $this->resource = @imagecreatefromwbmp($imagePath); |
|
| 588 | + } else { |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 590 | + } |
|
| 591 | + break; |
|
| 592 | + case IMAGETYPE_BMP: |
|
| 593 | + $this->resource = $this->imagecreatefrombmp($imagePath); |
|
| 594 | + break; |
|
| 595 | + /* |
|
| 596 | 596 | case IMAGETYPE_TIFF_II: // (intel byte order) |
| 597 | 597 | break; |
| 598 | 598 | case IMAGETYPE_TIFF_MM: // (motorola byte order) |
@@ -616,581 +616,581 @@ discard block |
||
| 616 | 616 | case IMAGETYPE_PSD: |
| 617 | 617 | break; |
| 618 | 618 | */ |
| 619 | - default: |
|
| 619 | + default: |
|
| 620 | 620 | |
| 621 | - // this is mostly file created from encrypted file |
|
| 622 | - $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | - $iType = IMAGETYPE_PNG; |
|
| 624 | - $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | - break; |
|
| 626 | - } |
|
| 627 | - if ($this->valid()) { |
|
| 628 | - $this->imageType = $iType; |
|
| 629 | - $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | - $this->filePath = $imagePath; |
|
| 631 | - } |
|
| 632 | - return $this->resource; |
|
| 633 | - } |
|
| 621 | + // this is mostly file created from encrypted file |
|
| 622 | + $this->resource = imagecreatefromstring(\OC\Files\Filesystem::file_get_contents(\OC\Files\Filesystem::getLocalPath($imagePath))); |
|
| 623 | + $iType = IMAGETYPE_PNG; |
|
| 624 | + $this->logger->debug('OC_Image->loadFromFile, Default', array('app' => 'core')); |
|
| 625 | + break; |
|
| 626 | + } |
|
| 627 | + if ($this->valid()) { |
|
| 628 | + $this->imageType = $iType; |
|
| 629 | + $this->mimeType = image_type_to_mime_type($iType); |
|
| 630 | + $this->filePath = $imagePath; |
|
| 631 | + } |
|
| 632 | + return $this->resource; |
|
| 633 | + } |
|
| 634 | 634 | |
| 635 | - /** |
|
| 636 | - * Loads an image from a string of data. |
|
| 637 | - * |
|
| 638 | - * @param string $str A string of image data as read from a file. |
|
| 639 | - * @return bool|resource An image resource or false on error |
|
| 640 | - */ |
|
| 641 | - public function loadFromData($str) { |
|
| 642 | - if (is_resource($str)) { |
|
| 643 | - return false; |
|
| 644 | - } |
|
| 645 | - $this->resource = @imagecreatefromstring($str); |
|
| 646 | - if ($this->fileInfo) { |
|
| 647 | - $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | - } |
|
| 649 | - if (is_resource($this->resource)) { |
|
| 650 | - imagealphablending($this->resource, false); |
|
| 651 | - imagesavealpha($this->resource, true); |
|
| 652 | - } |
|
| 635 | + /** |
|
| 636 | + * Loads an image from a string of data. |
|
| 637 | + * |
|
| 638 | + * @param string $str A string of image data as read from a file. |
|
| 639 | + * @return bool|resource An image resource or false on error |
|
| 640 | + */ |
|
| 641 | + public function loadFromData($str) { |
|
| 642 | + if (is_resource($str)) { |
|
| 643 | + return false; |
|
| 644 | + } |
|
| 645 | + $this->resource = @imagecreatefromstring($str); |
|
| 646 | + if ($this->fileInfo) { |
|
| 647 | + $this->mimeType = $this->fileInfo->buffer($str); |
|
| 648 | + } |
|
| 649 | + if (is_resource($this->resource)) { |
|
| 650 | + imagealphablending($this->resource, false); |
|
| 651 | + imagesavealpha($this->resource, true); |
|
| 652 | + } |
|
| 653 | 653 | |
| 654 | - if (!$this->resource) { |
|
| 655 | - $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | - return false; |
|
| 657 | - } |
|
| 658 | - return $this->resource; |
|
| 659 | - } |
|
| 654 | + if (!$this->resource) { |
|
| 655 | + $this->logger->debug('OC_Image->loadFromFile, could not load', array('app' => 'core')); |
|
| 656 | + return false; |
|
| 657 | + } |
|
| 658 | + return $this->resource; |
|
| 659 | + } |
|
| 660 | 660 | |
| 661 | - /** |
|
| 662 | - * Loads an image from a base64 encoded string. |
|
| 663 | - * |
|
| 664 | - * @param string $str A string base64 encoded string of image data. |
|
| 665 | - * @return bool|resource An image resource or false on error |
|
| 666 | - */ |
|
| 667 | - public function loadFromBase64($str) { |
|
| 668 | - if (!is_string($str)) { |
|
| 669 | - return false; |
|
| 670 | - } |
|
| 671 | - $data = base64_decode($str); |
|
| 672 | - if ($data) { // try to load from string data |
|
| 673 | - $this->resource = @imagecreatefromstring($data); |
|
| 674 | - if ($this->fileInfo) { |
|
| 675 | - $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | - } |
|
| 677 | - if (!$this->resource) { |
|
| 678 | - $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - return $this->resource; |
|
| 682 | - } else { |
|
| 683 | - return false; |
|
| 684 | - } |
|
| 685 | - } |
|
| 661 | + /** |
|
| 662 | + * Loads an image from a base64 encoded string. |
|
| 663 | + * |
|
| 664 | + * @param string $str A string base64 encoded string of image data. |
|
| 665 | + * @return bool|resource An image resource or false on error |
|
| 666 | + */ |
|
| 667 | + public function loadFromBase64($str) { |
|
| 668 | + if (!is_string($str)) { |
|
| 669 | + return false; |
|
| 670 | + } |
|
| 671 | + $data = base64_decode($str); |
|
| 672 | + if ($data) { // try to load from string data |
|
| 673 | + $this->resource = @imagecreatefromstring($data); |
|
| 674 | + if ($this->fileInfo) { |
|
| 675 | + $this->mimeType = $this->fileInfo->buffer($data); |
|
| 676 | + } |
|
| 677 | + if (!$this->resource) { |
|
| 678 | + $this->logger->debug('OC_Image->loadFromBase64, could not load', array('app' => 'core')); |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + return $this->resource; |
|
| 682 | + } else { |
|
| 683 | + return false; |
|
| 684 | + } |
|
| 685 | + } |
|
| 686 | 686 | |
| 687 | - /** |
|
| 688 | - * Create a new image from file or URL |
|
| 689 | - * |
|
| 690 | - * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | - * @version 1.00 |
|
| 692 | - * @param string $fileName <p> |
|
| 693 | - * Path to the BMP image. |
|
| 694 | - * </p> |
|
| 695 | - * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | - */ |
|
| 697 | - private function imagecreatefrombmp($fileName) { |
|
| 698 | - if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | - return false; |
|
| 701 | - } |
|
| 702 | - // read file header |
|
| 703 | - $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | - // check for bitmap |
|
| 705 | - if ($meta['type'] != 19778) { |
|
| 706 | - fclose($fh); |
|
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | - return false; |
|
| 709 | - } |
|
| 710 | - // read image header |
|
| 711 | - $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | - // read additional 16bit header |
|
| 713 | - if ($meta['bits'] == 16) { |
|
| 714 | - $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | - } |
|
| 716 | - // set bytes and padding |
|
| 717 | - $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | - $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | - $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | - if ($meta['decal'] == 4) { |
|
| 721 | - $meta['decal'] = 0; |
|
| 722 | - } |
|
| 723 | - // obtain imagesize |
|
| 724 | - if ($meta['imagesize'] < 1) { |
|
| 725 | - $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | - // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | - if ($meta['imagesize'] < 1) { |
|
| 728 | - $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | - if ($meta['imagesize'] < 1) { |
|
| 730 | - fclose($fh); |
|
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | - return false; |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - } |
|
| 736 | - // calculate colors |
|
| 737 | - $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | - // read color palette |
|
| 739 | - $palette = array(); |
|
| 740 | - if ($meta['bits'] < 16) { |
|
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | - // in rare cases the color value is signed |
|
| 743 | - if ($palette[1] < 0) { |
|
| 744 | - foreach ($palette as $i => $color) { |
|
| 745 | - $palette[$i] = $color + 16777216; |
|
| 746 | - } |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - // create gd image |
|
| 750 | - $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | - if ($im == false) { |
|
| 752 | - fclose($fh); |
|
| 753 | - $this->logger->warning( |
|
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | - array('app' => 'core')); |
|
| 756 | - return false; |
|
| 757 | - } |
|
| 687 | + /** |
|
| 688 | + * Create a new image from file or URL |
|
| 689 | + * |
|
| 690 | + * @link http://www.programmierer-forum.de/function-imagecreatefrombmp-laeuft-mit-allen-bitraten-t143137.htm |
|
| 691 | + * @version 1.00 |
|
| 692 | + * @param string $fileName <p> |
|
| 693 | + * Path to the BMP image. |
|
| 694 | + * </p> |
|
| 695 | + * @return bool|resource an image resource identifier on success, <b>FALSE</b> on errors. |
|
| 696 | + */ |
|
| 697 | + private function imagecreatefrombmp($fileName) { |
|
| 698 | + if (!($fh = fopen($fileName, 'rb'))) { |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 700 | + return false; |
|
| 701 | + } |
|
| 702 | + // read file header |
|
| 703 | + $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14)); |
|
| 704 | + // check for bitmap |
|
| 705 | + if ($meta['type'] != 19778) { |
|
| 706 | + fclose($fh); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 708 | + return false; |
|
| 709 | + } |
|
| 710 | + // read image header |
|
| 711 | + $meta += unpack('Vheadersize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vcolors/Vimportant', fread($fh, 40)); |
|
| 712 | + // read additional 16bit header |
|
| 713 | + if ($meta['bits'] == 16) { |
|
| 714 | + $meta += unpack('VrMask/VgMask/VbMask', fread($fh, 12)); |
|
| 715 | + } |
|
| 716 | + // set bytes and padding |
|
| 717 | + $meta['bytes'] = $meta['bits'] / 8; |
|
| 718 | + $this->bitDepth = $meta['bits']; //remember the bit depth for the imagebmp call |
|
| 719 | + $meta['decal'] = 4 - (4 * (($meta['width'] * $meta['bytes'] / 4) - floor($meta['width'] * $meta['bytes'] / 4))); |
|
| 720 | + if ($meta['decal'] == 4) { |
|
| 721 | + $meta['decal'] = 0; |
|
| 722 | + } |
|
| 723 | + // obtain imagesize |
|
| 724 | + if ($meta['imagesize'] < 1) { |
|
| 725 | + $meta['imagesize'] = $meta['filesize'] - $meta['offset']; |
|
| 726 | + // in rare cases filesize is equal to offset so we need to read physical size |
|
| 727 | + if ($meta['imagesize'] < 1) { |
|
| 728 | + $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
|
| 729 | + if ($meta['imagesize'] < 1) { |
|
| 730 | + fclose($fh); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 732 | + return false; |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + } |
|
| 736 | + // calculate colors |
|
| 737 | + $meta['colors'] = !$meta['colors'] ? pow(2, $meta['bits']) : $meta['colors']; |
|
| 738 | + // read color palette |
|
| 739 | + $palette = array(); |
|
| 740 | + if ($meta['bits'] < 16) { |
|
| 741 | + $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | + // in rare cases the color value is signed |
|
| 743 | + if ($palette[1] < 0) { |
|
| 744 | + foreach ($palette as $i => $color) { |
|
| 745 | + $palette[$i] = $color + 16777216; |
|
| 746 | + } |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + // create gd image |
|
| 750 | + $im = imagecreatetruecolor($meta['width'], $meta['height']); |
|
| 751 | + if ($im == false) { |
|
| 752 | + fclose($fh); |
|
| 753 | + $this->logger->warning( |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 755 | + array('app' => 'core')); |
|
| 756 | + return false; |
|
| 757 | + } |
|
| 758 | 758 | |
| 759 | - $data = fread($fh, $meta['imagesize']); |
|
| 760 | - $p = 0; |
|
| 761 | - $vide = chr(0); |
|
| 762 | - $y = $meta['height'] - 1; |
|
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | - // loop through the image data beginning with the lower left corner |
|
| 765 | - while ($y >= 0) { |
|
| 766 | - $x = 0; |
|
| 767 | - while ($x < $meta['width']) { |
|
| 768 | - switch ($meta['bits']) { |
|
| 769 | - case 32: |
|
| 770 | - case 24: |
|
| 771 | - if (!($part = substr($data, $p, 3))) { |
|
| 772 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | - return $im; |
|
| 774 | - } |
|
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 776 | - break; |
|
| 777 | - case 16: |
|
| 778 | - if (!($part = substr($data, $p, 2))) { |
|
| 779 | - fclose($fh); |
|
| 780 | - $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | - return $im; |
|
| 782 | - } |
|
| 783 | - $color = @unpack('v', $part); |
|
| 784 | - $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | - break; |
|
| 786 | - case 8: |
|
| 787 | - $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 788 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | - break; |
|
| 790 | - case 4: |
|
| 791 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 792 | - $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | - break; |
|
| 795 | - case 1: |
|
| 796 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 797 | - switch (($p * 8) % 8) { |
|
| 798 | - case 0: |
|
| 799 | - $color[1] = $color[1] >> 7; |
|
| 800 | - break; |
|
| 801 | - case 1: |
|
| 802 | - $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | - break; |
|
| 804 | - case 2: |
|
| 805 | - $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | - break; |
|
| 807 | - case 3: |
|
| 808 | - $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | - break; |
|
| 810 | - case 4: |
|
| 811 | - $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | - break; |
|
| 813 | - case 5: |
|
| 814 | - $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | - break; |
|
| 816 | - case 6: |
|
| 817 | - $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | - break; |
|
| 819 | - case 7: |
|
| 820 | - $color[1] = ($color[1] & 0x1); |
|
| 821 | - break; |
|
| 822 | - } |
|
| 823 | - $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | - break; |
|
| 825 | - default: |
|
| 826 | - fclose($fh); |
|
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | - return false; |
|
| 829 | - } |
|
| 830 | - imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | - $x++; |
|
| 832 | - $p += $meta['bytes']; |
|
| 833 | - } |
|
| 834 | - $y--; |
|
| 835 | - $p += $meta['decal']; |
|
| 836 | - } |
|
| 837 | - fclose($fh); |
|
| 838 | - return $im; |
|
| 839 | - } |
|
| 759 | + $data = fread($fh, $meta['imagesize']); |
|
| 760 | + $p = 0; |
|
| 761 | + $vide = chr(0); |
|
| 762 | + $y = $meta['height'] - 1; |
|
| 763 | + $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 764 | + // loop through the image data beginning with the lower left corner |
|
| 765 | + while ($y >= 0) { |
|
| 766 | + $x = 0; |
|
| 767 | + while ($x < $meta['width']) { |
|
| 768 | + switch ($meta['bits']) { |
|
| 769 | + case 32: |
|
| 770 | + case 24: |
|
| 771 | + if (!($part = substr($data, $p, 3))) { |
|
| 772 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 773 | + return $im; |
|
| 774 | + } |
|
| 775 | + $color = @unpack('V', $part . $vide); |
|
| 776 | + break; |
|
| 777 | + case 16: |
|
| 778 | + if (!($part = substr($data, $p, 2))) { |
|
| 779 | + fclose($fh); |
|
| 780 | + $this->logger->warning($error, array('app' => 'core')); |
|
| 781 | + return $im; |
|
| 782 | + } |
|
| 783 | + $color = @unpack('v', $part); |
|
| 784 | + $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
|
| 785 | + break; |
|
| 786 | + case 8: |
|
| 787 | + $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 788 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 789 | + break; |
|
| 790 | + case 4: |
|
| 791 | + $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 792 | + $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
|
| 793 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 794 | + break; |
|
| 795 | + case 1: |
|
| 796 | + $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 797 | + switch (($p * 8) % 8) { |
|
| 798 | + case 0: |
|
| 799 | + $color[1] = $color[1] >> 7; |
|
| 800 | + break; |
|
| 801 | + case 1: |
|
| 802 | + $color[1] = ($color[1] & 0x40) >> 6; |
|
| 803 | + break; |
|
| 804 | + case 2: |
|
| 805 | + $color[1] = ($color[1] & 0x20) >> 5; |
|
| 806 | + break; |
|
| 807 | + case 3: |
|
| 808 | + $color[1] = ($color[1] & 0x10) >> 4; |
|
| 809 | + break; |
|
| 810 | + case 4: |
|
| 811 | + $color[1] = ($color[1] & 0x8) >> 3; |
|
| 812 | + break; |
|
| 813 | + case 5: |
|
| 814 | + $color[1] = ($color[1] & 0x4) >> 2; |
|
| 815 | + break; |
|
| 816 | + case 6: |
|
| 817 | + $color[1] = ($color[1] & 0x2) >> 1; |
|
| 818 | + break; |
|
| 819 | + case 7: |
|
| 820 | + $color[1] = ($color[1] & 0x1); |
|
| 821 | + break; |
|
| 822 | + } |
|
| 823 | + $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
|
| 824 | + break; |
|
| 825 | + default: |
|
| 826 | + fclose($fh); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | + return false; |
|
| 829 | + } |
|
| 830 | + imagesetpixel($im, $x, $y, $color[1]); |
|
| 831 | + $x++; |
|
| 832 | + $p += $meta['bytes']; |
|
| 833 | + } |
|
| 834 | + $y--; |
|
| 835 | + $p += $meta['decal']; |
|
| 836 | + } |
|
| 837 | + fclose($fh); |
|
| 838 | + return $im; |
|
| 839 | + } |
|
| 840 | 840 | |
| 841 | - /** |
|
| 842 | - * Resizes the image preserving ratio. |
|
| 843 | - * |
|
| 844 | - * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | - * @return bool |
|
| 846 | - */ |
|
| 847 | - public function resize($maxSize) { |
|
| 848 | - if (!$this->valid()) { |
|
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | - return false; |
|
| 851 | - } |
|
| 852 | - $widthOrig = imagesx($this->resource); |
|
| 853 | - $heightOrig = imagesy($this->resource); |
|
| 854 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 841 | + /** |
|
| 842 | + * Resizes the image preserving ratio. |
|
| 843 | + * |
|
| 844 | + * @param integer $maxSize The maximum size of either the width or height. |
|
| 845 | + * @return bool |
|
| 846 | + */ |
|
| 847 | + public function resize($maxSize) { |
|
| 848 | + if (!$this->valid()) { |
|
| 849 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 850 | + return false; |
|
| 851 | + } |
|
| 852 | + $widthOrig = imagesx($this->resource); |
|
| 853 | + $heightOrig = imagesy($this->resource); |
|
| 854 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 855 | 855 | |
| 856 | - if ($ratioOrig > 1) { |
|
| 857 | - $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | - $newWidth = $maxSize; |
|
| 859 | - } else { |
|
| 860 | - $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | - $newHeight = $maxSize; |
|
| 862 | - } |
|
| 856 | + if ($ratioOrig > 1) { |
|
| 857 | + $newHeight = round($maxSize / $ratioOrig); |
|
| 858 | + $newWidth = $maxSize; |
|
| 859 | + } else { |
|
| 860 | + $newWidth = round($maxSize * $ratioOrig); |
|
| 861 | + $newHeight = $maxSize; |
|
| 862 | + } |
|
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | - return true; |
|
| 866 | - } |
|
| 864 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 865 | + return true; |
|
| 866 | + } |
|
| 867 | 867 | |
| 868 | - /** |
|
| 869 | - * @param int $width |
|
| 870 | - * @param int $height |
|
| 871 | - * @return bool |
|
| 872 | - */ |
|
| 873 | - public function preciseResize(int $width, int $height): bool { |
|
| 874 | - if (!$this->valid()) { |
|
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | - return false; |
|
| 877 | - } |
|
| 878 | - $widthOrig = imagesx($this->resource); |
|
| 879 | - $heightOrig = imagesy($this->resource); |
|
| 880 | - $process = imagecreatetruecolor($width, $height); |
|
| 868 | + /** |
|
| 869 | + * @param int $width |
|
| 870 | + * @param int $height |
|
| 871 | + * @return bool |
|
| 872 | + */ |
|
| 873 | + public function preciseResize(int $width, int $height): bool { |
|
| 874 | + if (!$this->valid()) { |
|
| 875 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 876 | + return false; |
|
| 877 | + } |
|
| 878 | + $widthOrig = imagesx($this->resource); |
|
| 879 | + $heightOrig = imagesy($this->resource); |
|
| 880 | + $process = imagecreatetruecolor($width, $height); |
|
| 881 | 881 | |
| 882 | - if ($process == false) { |
|
| 883 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 884 | - imagedestroy($process); |
|
| 885 | - return false; |
|
| 886 | - } |
|
| 882 | + if ($process == false) { |
|
| 883 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 884 | + imagedestroy($process); |
|
| 885 | + return false; |
|
| 886 | + } |
|
| 887 | 887 | |
| 888 | - // preserve transparency |
|
| 889 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 890 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 891 | - imagealphablending($process, false); |
|
| 892 | - imagesavealpha($process, true); |
|
| 893 | - } |
|
| 888 | + // preserve transparency |
|
| 889 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 890 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 891 | + imagealphablending($process, false); |
|
| 892 | + imagesavealpha($process, true); |
|
| 893 | + } |
|
| 894 | 894 | |
| 895 | - imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 896 | - if ($process == false) { |
|
| 897 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | - imagedestroy($process); |
|
| 899 | - return false; |
|
| 900 | - } |
|
| 901 | - imagedestroy($this->resource); |
|
| 902 | - $this->resource = $process; |
|
| 903 | - return true; |
|
| 904 | - } |
|
| 895 | + imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
|
| 896 | + if ($process == false) { |
|
| 897 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | + imagedestroy($process); |
|
| 899 | + return false; |
|
| 900 | + } |
|
| 901 | + imagedestroy($this->resource); |
|
| 902 | + $this->resource = $process; |
|
| 903 | + return true; |
|
| 904 | + } |
|
| 905 | 905 | |
| 906 | - /** |
|
| 907 | - * Crops the image to the middle square. If the image is already square it just returns. |
|
| 908 | - * |
|
| 909 | - * @param int $size maximum size for the result (optional) |
|
| 910 | - * @return bool for success or failure |
|
| 911 | - */ |
|
| 912 | - public function centerCrop($size = 0) { |
|
| 913 | - if (!$this->valid()) { |
|
| 914 | - $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 915 | - return false; |
|
| 916 | - } |
|
| 917 | - $widthOrig = imagesx($this->resource); |
|
| 918 | - $heightOrig = imagesy($this->resource); |
|
| 919 | - if ($widthOrig === $heightOrig and $size == 0) { |
|
| 920 | - return true; |
|
| 921 | - } |
|
| 922 | - $ratioOrig = $widthOrig / $heightOrig; |
|
| 923 | - $width = $height = min($widthOrig, $heightOrig); |
|
| 906 | + /** |
|
| 907 | + * Crops the image to the middle square. If the image is already square it just returns. |
|
| 908 | + * |
|
| 909 | + * @param int $size maximum size for the result (optional) |
|
| 910 | + * @return bool for success or failure |
|
| 911 | + */ |
|
| 912 | + public function centerCrop($size = 0) { |
|
| 913 | + if (!$this->valid()) { |
|
| 914 | + $this->logger->error('OC_Image->centerCrop, No image loaded', array('app' => 'core')); |
|
| 915 | + return false; |
|
| 916 | + } |
|
| 917 | + $widthOrig = imagesx($this->resource); |
|
| 918 | + $heightOrig = imagesy($this->resource); |
|
| 919 | + if ($widthOrig === $heightOrig and $size == 0) { |
|
| 920 | + return true; |
|
| 921 | + } |
|
| 922 | + $ratioOrig = $widthOrig / $heightOrig; |
|
| 923 | + $width = $height = min($widthOrig, $heightOrig); |
|
| 924 | 924 | |
| 925 | - if ($ratioOrig > 1) { |
|
| 926 | - $x = ($widthOrig / 2) - ($width / 2); |
|
| 927 | - $y = 0; |
|
| 928 | - } else { |
|
| 929 | - $y = ($heightOrig / 2) - ($height / 2); |
|
| 930 | - $x = 0; |
|
| 931 | - } |
|
| 932 | - if ($size > 0) { |
|
| 933 | - $targetWidth = $size; |
|
| 934 | - $targetHeight = $size; |
|
| 935 | - } else { |
|
| 936 | - $targetWidth = $width; |
|
| 937 | - $targetHeight = $height; |
|
| 938 | - } |
|
| 939 | - $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 940 | - if ($process == false) { |
|
| 941 | - $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 942 | - imagedestroy($process); |
|
| 943 | - return false; |
|
| 944 | - } |
|
| 925 | + if ($ratioOrig > 1) { |
|
| 926 | + $x = ($widthOrig / 2) - ($width / 2); |
|
| 927 | + $y = 0; |
|
| 928 | + } else { |
|
| 929 | + $y = ($heightOrig / 2) - ($height / 2); |
|
| 930 | + $x = 0; |
|
| 931 | + } |
|
| 932 | + if ($size > 0) { |
|
| 933 | + $targetWidth = $size; |
|
| 934 | + $targetHeight = $size; |
|
| 935 | + } else { |
|
| 936 | + $targetWidth = $width; |
|
| 937 | + $targetHeight = $height; |
|
| 938 | + } |
|
| 939 | + $process = imagecreatetruecolor($targetWidth, $targetHeight); |
|
| 940 | + if ($process == false) { |
|
| 941 | + $this->logger->error('OC_Image->centerCrop, Error creating true color image', array('app' => 'core')); |
|
| 942 | + imagedestroy($process); |
|
| 943 | + return false; |
|
| 944 | + } |
|
| 945 | 945 | |
| 946 | - // preserve transparency |
|
| 947 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 948 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 949 | - imagealphablending($process, false); |
|
| 950 | - imagesavealpha($process, true); |
|
| 951 | - } |
|
| 946 | + // preserve transparency |
|
| 947 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 948 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 949 | + imagealphablending($process, false); |
|
| 950 | + imagesavealpha($process, true); |
|
| 951 | + } |
|
| 952 | 952 | |
| 953 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 954 | - if ($process == false) { |
|
| 955 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 956 | - imagedestroy($process); |
|
| 957 | - return false; |
|
| 958 | - } |
|
| 959 | - imagedestroy($this->resource); |
|
| 960 | - $this->resource = $process; |
|
| 961 | - return true; |
|
| 962 | - } |
|
| 953 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
|
| 954 | + if ($process == false) { |
|
| 955 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 956 | + imagedestroy($process); |
|
| 957 | + return false; |
|
| 958 | + } |
|
| 959 | + imagedestroy($this->resource); |
|
| 960 | + $this->resource = $process; |
|
| 961 | + return true; |
|
| 962 | + } |
|
| 963 | 963 | |
| 964 | - /** |
|
| 965 | - * Crops the image from point $x$y with dimension $wx$h. |
|
| 966 | - * |
|
| 967 | - * @param int $x Horizontal position |
|
| 968 | - * @param int $y Vertical position |
|
| 969 | - * @param int $w Width |
|
| 970 | - * @param int $h Height |
|
| 971 | - * @return bool for success or failure |
|
| 972 | - */ |
|
| 973 | - public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 974 | - if (!$this->valid()) { |
|
| 975 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 976 | - return false; |
|
| 977 | - } |
|
| 978 | - $process = imagecreatetruecolor($w, $h); |
|
| 979 | - if ($process == false) { |
|
| 980 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 981 | - imagedestroy($process); |
|
| 982 | - return false; |
|
| 983 | - } |
|
| 964 | + /** |
|
| 965 | + * Crops the image from point $x$y with dimension $wx$h. |
|
| 966 | + * |
|
| 967 | + * @param int $x Horizontal position |
|
| 968 | + * @param int $y Vertical position |
|
| 969 | + * @param int $w Width |
|
| 970 | + * @param int $h Height |
|
| 971 | + * @return bool for success or failure |
|
| 972 | + */ |
|
| 973 | + public function crop(int $x, int $y, int $w, int $h): bool { |
|
| 974 | + if (!$this->valid()) { |
|
| 975 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 976 | + return false; |
|
| 977 | + } |
|
| 978 | + $process = imagecreatetruecolor($w, $h); |
|
| 979 | + if ($process == false) { |
|
| 980 | + $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 981 | + imagedestroy($process); |
|
| 982 | + return false; |
|
| 983 | + } |
|
| 984 | 984 | |
| 985 | - // preserve transparency |
|
| 986 | - if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 987 | - imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 988 | - imagealphablending($process, false); |
|
| 989 | - imagesavealpha($process, true); |
|
| 990 | - } |
|
| 985 | + // preserve transparency |
|
| 986 | + if ($this->imageType == IMAGETYPE_GIF or $this->imageType == IMAGETYPE_PNG) { |
|
| 987 | + imagecolortransparent($process, imagecolorallocatealpha($process, 0, 0, 0, 127)); |
|
| 988 | + imagealphablending($process, false); |
|
| 989 | + imagesavealpha($process, true); |
|
| 990 | + } |
|
| 991 | 991 | |
| 992 | - imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 993 | - if ($process == false) { |
|
| 994 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 995 | - imagedestroy($process); |
|
| 996 | - return false; |
|
| 997 | - } |
|
| 998 | - imagedestroy($this->resource); |
|
| 999 | - $this->resource = $process; |
|
| 1000 | - return true; |
|
| 1001 | - } |
|
| 992 | + imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
|
| 993 | + if ($process == false) { |
|
| 994 | + $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 995 | + imagedestroy($process); |
|
| 996 | + return false; |
|
| 997 | + } |
|
| 998 | + imagedestroy($this->resource); |
|
| 999 | + $this->resource = $process; |
|
| 1000 | + return true; |
|
| 1001 | + } |
|
| 1002 | 1002 | |
| 1003 | - /** |
|
| 1004 | - * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1005 | - * |
|
| 1006 | - * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1007 | - * |
|
| 1008 | - * @param integer $maxWidth |
|
| 1009 | - * @param integer $maxHeight |
|
| 1010 | - * @return bool |
|
| 1011 | - */ |
|
| 1012 | - public function fitIn($maxWidth, $maxHeight) { |
|
| 1013 | - if (!$this->valid()) { |
|
| 1014 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1015 | - return false; |
|
| 1016 | - } |
|
| 1017 | - $widthOrig = imagesx($this->resource); |
|
| 1018 | - $heightOrig = imagesy($this->resource); |
|
| 1019 | - $ratio = $widthOrig / $heightOrig; |
|
| 1003 | + /** |
|
| 1004 | + * Resizes the image to fit within a boundary while preserving ratio. |
|
| 1005 | + * |
|
| 1006 | + * Warning: Images smaller than $maxWidth x $maxHeight will end up being scaled up |
|
| 1007 | + * |
|
| 1008 | + * @param integer $maxWidth |
|
| 1009 | + * @param integer $maxHeight |
|
| 1010 | + * @return bool |
|
| 1011 | + */ |
|
| 1012 | + public function fitIn($maxWidth, $maxHeight) { |
|
| 1013 | + if (!$this->valid()) { |
|
| 1014 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1015 | + return false; |
|
| 1016 | + } |
|
| 1017 | + $widthOrig = imagesx($this->resource); |
|
| 1018 | + $heightOrig = imagesy($this->resource); |
|
| 1019 | + $ratio = $widthOrig / $heightOrig; |
|
| 1020 | 1020 | |
| 1021 | - $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1022 | - $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1021 | + $newWidth = min($maxWidth, $ratio * $maxHeight); |
|
| 1022 | + $newHeight = min($maxHeight, $maxWidth / $ratio); |
|
| 1023 | 1023 | |
| 1024 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1025 | - return true; |
|
| 1026 | - } |
|
| 1024 | + $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1025 | + return true; |
|
| 1026 | + } |
|
| 1027 | 1027 | |
| 1028 | - /** |
|
| 1029 | - * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1030 | - * |
|
| 1031 | - * @param integer $maxWidth |
|
| 1032 | - * @param integer $maxHeight |
|
| 1033 | - * @return bool |
|
| 1034 | - */ |
|
| 1035 | - public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1036 | - if (!$this->valid()) { |
|
| 1037 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1038 | - return false; |
|
| 1039 | - } |
|
| 1040 | - $widthOrig = imagesx($this->resource); |
|
| 1041 | - $heightOrig = imagesy($this->resource); |
|
| 1028 | + /** |
|
| 1029 | + * Shrinks larger images to fit within specified boundaries while preserving ratio. |
|
| 1030 | + * |
|
| 1031 | + * @param integer $maxWidth |
|
| 1032 | + * @param integer $maxHeight |
|
| 1033 | + * @return bool |
|
| 1034 | + */ |
|
| 1035 | + public function scaleDownToFit($maxWidth, $maxHeight) { |
|
| 1036 | + if (!$this->valid()) { |
|
| 1037 | + $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1038 | + return false; |
|
| 1039 | + } |
|
| 1040 | + $widthOrig = imagesx($this->resource); |
|
| 1041 | + $heightOrig = imagesy($this->resource); |
|
| 1042 | 1042 | |
| 1043 | - if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1044 | - return $this->fitIn($maxWidth, $maxHeight); |
|
| 1045 | - } |
|
| 1043 | + if ($widthOrig > $maxWidth || $heightOrig > $maxHeight) { |
|
| 1044 | + return $this->fitIn($maxWidth, $maxHeight); |
|
| 1045 | + } |
|
| 1046 | 1046 | |
| 1047 | - return false; |
|
| 1048 | - } |
|
| 1047 | + return false; |
|
| 1048 | + } |
|
| 1049 | 1049 | |
| 1050 | - /** |
|
| 1051 | - * Destroys the current image and resets the object |
|
| 1052 | - */ |
|
| 1053 | - public function destroy() { |
|
| 1054 | - if ($this->valid()) { |
|
| 1055 | - imagedestroy($this->resource); |
|
| 1056 | - } |
|
| 1057 | - $this->resource = null; |
|
| 1058 | - } |
|
| 1050 | + /** |
|
| 1051 | + * Destroys the current image and resets the object |
|
| 1052 | + */ |
|
| 1053 | + public function destroy() { |
|
| 1054 | + if ($this->valid()) { |
|
| 1055 | + imagedestroy($this->resource); |
|
| 1056 | + } |
|
| 1057 | + $this->resource = null; |
|
| 1058 | + } |
|
| 1059 | 1059 | |
| 1060 | - public function __destruct() { |
|
| 1061 | - $this->destroy(); |
|
| 1062 | - } |
|
| 1060 | + public function __destruct() { |
|
| 1061 | + $this->destroy(); |
|
| 1062 | + } |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | 1065 | if (!function_exists('imagebmp')) { |
| 1066 | - /** |
|
| 1067 | - * Output a BMP image to either the browser or a file |
|
| 1068 | - * |
|
| 1069 | - * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1070 | - * @author legend <[email protected]> |
|
| 1071 | - * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1072 | - * @author mgutt <[email protected]> |
|
| 1073 | - * @version 1.00 |
|
| 1074 | - * @param resource $im |
|
| 1075 | - * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1076 | - * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1077 | - * @param int $compression [optional] |
|
| 1078 | - * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1079 | - */ |
|
| 1080 | - function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1081 | - if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1082 | - $bit = 24; |
|
| 1083 | - } else if ($bit == 32) { |
|
| 1084 | - $bit = 24; |
|
| 1085 | - } |
|
| 1086 | - $bits = pow(2, $bit); |
|
| 1087 | - imagetruecolortopalette($im, true, $bits); |
|
| 1088 | - $width = imagesx($im); |
|
| 1089 | - $height = imagesy($im); |
|
| 1090 | - $colorsNum = imagecolorstotal($im); |
|
| 1091 | - $rgbQuad = ''; |
|
| 1092 | - if ($bit <= 8) { |
|
| 1093 | - for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1094 | - $colors = imagecolorsforindex($im, $i); |
|
| 1095 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1096 | - } |
|
| 1097 | - $bmpData = ''; |
|
| 1098 | - if ($compression == 0 || $bit < 8) { |
|
| 1099 | - $compression = 0; |
|
| 1100 | - $extra = ''; |
|
| 1101 | - $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1102 | - if ($padding % 4 != 0) { |
|
| 1103 | - $extra = str_repeat("\0", $padding); |
|
| 1104 | - } |
|
| 1105 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1106 | - $i = 0; |
|
| 1107 | - while ($i < $width) { |
|
| 1108 | - $bin = 0; |
|
| 1109 | - $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1110 | - for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1111 | - $index = imagecolorat($im, $i, $j); |
|
| 1112 | - $bin |= $index << $k; |
|
| 1113 | - $i++; |
|
| 1114 | - } |
|
| 1115 | - $bmpData .= chr($bin); |
|
| 1116 | - } |
|
| 1117 | - $bmpData .= $extra; |
|
| 1118 | - } |
|
| 1119 | - } // RLE8 |
|
| 1120 | - else if ($compression == 1 && $bit == 8) { |
|
| 1121 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1122 | - $lastIndex = "\0"; |
|
| 1123 | - $sameNum = 0; |
|
| 1124 | - for ($i = 0; $i <= $width; $i++) { |
|
| 1125 | - $index = imagecolorat($im, $i, $j); |
|
| 1126 | - if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1127 | - if ($sameNum != 0) { |
|
| 1128 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1129 | - } |
|
| 1130 | - $lastIndex = $index; |
|
| 1131 | - $sameNum = 1; |
|
| 1132 | - } else { |
|
| 1133 | - $sameNum++; |
|
| 1134 | - } |
|
| 1135 | - } |
|
| 1136 | - $bmpData .= "\0\0"; |
|
| 1137 | - } |
|
| 1138 | - $bmpData .= "\0\1"; |
|
| 1139 | - } |
|
| 1140 | - $sizeQuad = strlen($rgbQuad); |
|
| 1141 | - $sizeData = strlen($bmpData); |
|
| 1142 | - } else { |
|
| 1143 | - $extra = ''; |
|
| 1144 | - $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1145 | - if ($padding % 4 != 0) { |
|
| 1146 | - $extra = str_repeat("\0", $padding); |
|
| 1147 | - } |
|
| 1148 | - $bmpData = ''; |
|
| 1149 | - for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1150 | - for ($i = 0; $i < $width; $i++) { |
|
| 1151 | - $index = imagecolorat($im, $i, $j); |
|
| 1152 | - $colors = imagecolorsforindex($im, $index); |
|
| 1153 | - if ($bit == 16) { |
|
| 1154 | - $bin = 0 << $bit; |
|
| 1155 | - $bin |= ($colors['red'] >> 3) << 10; |
|
| 1156 | - $bin |= ($colors['green'] >> 3) << 5; |
|
| 1157 | - $bin |= $colors['blue'] >> 3; |
|
| 1158 | - $bmpData .= pack("v", $bin); |
|
| 1159 | - } else { |
|
| 1160 | - $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1161 | - } |
|
| 1162 | - } |
|
| 1163 | - $bmpData .= $extra; |
|
| 1164 | - } |
|
| 1165 | - $sizeQuad = 0; |
|
| 1166 | - $sizeData = strlen($bmpData); |
|
| 1167 | - $colorsNum = 0; |
|
| 1168 | - } |
|
| 1169 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | - $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1171 | - if ($fileName != '') { |
|
| 1172 | - $fp = fopen($fileName, 'wb'); |
|
| 1173 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1174 | - fclose($fp); |
|
| 1175 | - return true; |
|
| 1176 | - } |
|
| 1177 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1178 | - return true; |
|
| 1179 | - } |
|
| 1066 | + /** |
|
| 1067 | + * Output a BMP image to either the browser or a file |
|
| 1068 | + * |
|
| 1069 | + * @link http://www.ugia.cn/wp-data/imagebmp.php |
|
| 1070 | + * @author legend <[email protected]> |
|
| 1071 | + * @link http://www.programmierer-forum.de/imagebmp-gute-funktion-gefunden-t143716.htm |
|
| 1072 | + * @author mgutt <[email protected]> |
|
| 1073 | + * @version 1.00 |
|
| 1074 | + * @param resource $im |
|
| 1075 | + * @param string $fileName [optional] <p>The path to save the file to.</p> |
|
| 1076 | + * @param int $bit [optional] <p>Bit depth, (default is 24).</p> |
|
| 1077 | + * @param int $compression [optional] |
|
| 1078 | + * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure. |
|
| 1079 | + */ |
|
| 1080 | + function imagebmp($im, $fileName = '', $bit = 24, $compression = 0) { |
|
| 1081 | + if (!in_array($bit, array(1, 4, 8, 16, 24, 32))) { |
|
| 1082 | + $bit = 24; |
|
| 1083 | + } else if ($bit == 32) { |
|
| 1084 | + $bit = 24; |
|
| 1085 | + } |
|
| 1086 | + $bits = pow(2, $bit); |
|
| 1087 | + imagetruecolortopalette($im, true, $bits); |
|
| 1088 | + $width = imagesx($im); |
|
| 1089 | + $height = imagesy($im); |
|
| 1090 | + $colorsNum = imagecolorstotal($im); |
|
| 1091 | + $rgbQuad = ''; |
|
| 1092 | + if ($bit <= 8) { |
|
| 1093 | + for ($i = 0; $i < $colorsNum; $i++) { |
|
| 1094 | + $colors = imagecolorsforindex($im, $i); |
|
| 1095 | + $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1096 | + } |
|
| 1097 | + $bmpData = ''; |
|
| 1098 | + if ($compression == 0 || $bit < 8) { |
|
| 1099 | + $compression = 0; |
|
| 1100 | + $extra = ''; |
|
| 1101 | + $padding = 4 - ceil($width / (8 / $bit)) % 4; |
|
| 1102 | + if ($padding % 4 != 0) { |
|
| 1103 | + $extra = str_repeat("\0", $padding); |
|
| 1104 | + } |
|
| 1105 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1106 | + $i = 0; |
|
| 1107 | + while ($i < $width) { |
|
| 1108 | + $bin = 0; |
|
| 1109 | + $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0; |
|
| 1110 | + for ($k = 8 - $bit; $k >= $limit; $k -= $bit) { |
|
| 1111 | + $index = imagecolorat($im, $i, $j); |
|
| 1112 | + $bin |= $index << $k; |
|
| 1113 | + $i++; |
|
| 1114 | + } |
|
| 1115 | + $bmpData .= chr($bin); |
|
| 1116 | + } |
|
| 1117 | + $bmpData .= $extra; |
|
| 1118 | + } |
|
| 1119 | + } // RLE8 |
|
| 1120 | + else if ($compression == 1 && $bit == 8) { |
|
| 1121 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1122 | + $lastIndex = "\0"; |
|
| 1123 | + $sameNum = 0; |
|
| 1124 | + for ($i = 0; $i <= $width; $i++) { |
|
| 1125 | + $index = imagecolorat($im, $i, $j); |
|
| 1126 | + if ($index !== $lastIndex || $sameNum > 255) { |
|
| 1127 | + if ($sameNum != 0) { |
|
| 1128 | + $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1129 | + } |
|
| 1130 | + $lastIndex = $index; |
|
| 1131 | + $sameNum = 1; |
|
| 1132 | + } else { |
|
| 1133 | + $sameNum++; |
|
| 1134 | + } |
|
| 1135 | + } |
|
| 1136 | + $bmpData .= "\0\0"; |
|
| 1137 | + } |
|
| 1138 | + $bmpData .= "\0\1"; |
|
| 1139 | + } |
|
| 1140 | + $sizeQuad = strlen($rgbQuad); |
|
| 1141 | + $sizeData = strlen($bmpData); |
|
| 1142 | + } else { |
|
| 1143 | + $extra = ''; |
|
| 1144 | + $padding = 4 - ($width * ($bit / 8)) % 4; |
|
| 1145 | + if ($padding % 4 != 0) { |
|
| 1146 | + $extra = str_repeat("\0", $padding); |
|
| 1147 | + } |
|
| 1148 | + $bmpData = ''; |
|
| 1149 | + for ($j = $height - 1; $j >= 0; $j--) { |
|
| 1150 | + for ($i = 0; $i < $width; $i++) { |
|
| 1151 | + $index = imagecolorat($im, $i, $j); |
|
| 1152 | + $colors = imagecolorsforindex($im, $index); |
|
| 1153 | + if ($bit == 16) { |
|
| 1154 | + $bin = 0 << $bit; |
|
| 1155 | + $bin |= ($colors['red'] >> 3) << 10; |
|
| 1156 | + $bin |= ($colors['green'] >> 3) << 5; |
|
| 1157 | + $bin |= $colors['blue'] >> 3; |
|
| 1158 | + $bmpData .= pack("v", $bin); |
|
| 1159 | + } else { |
|
| 1160 | + $bmpData .= pack("c*", $colors['blue'], $colors['green'], $colors['red']); |
|
| 1161 | + } |
|
| 1162 | + } |
|
| 1163 | + $bmpData .= $extra; |
|
| 1164 | + } |
|
| 1165 | + $sizeQuad = 0; |
|
| 1166 | + $sizeData = strlen($bmpData); |
|
| 1167 | + $colorsNum = 0; |
|
| 1168 | + } |
|
| 1169 | + $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | + $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
|
| 1171 | + if ($fileName != '') { |
|
| 1172 | + $fp = fopen($fileName, 'wb'); |
|
| 1173 | + fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1174 | + fclose($fp); |
|
| 1175 | + return true; |
|
| 1176 | + } |
|
| 1177 | + echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1178 | + return true; |
|
| 1179 | + } |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | if (!function_exists('exif_imagetype')) { |
| 1183 | - /** |
|
| 1184 | - * Workaround if exif_imagetype does not exist |
|
| 1185 | - * |
|
| 1186 | - * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1187 | - * @param string $fileName |
|
| 1188 | - * @return string|boolean |
|
| 1189 | - */ |
|
| 1190 | - function exif_imagetype($fileName) { |
|
| 1191 | - if (($info = getimagesize($fileName)) !== false) { |
|
| 1192 | - return $info[2]; |
|
| 1193 | - } |
|
| 1194 | - return false; |
|
| 1195 | - } |
|
| 1183 | + /** |
|
| 1184 | + * Workaround if exif_imagetype does not exist |
|
| 1185 | + * |
|
| 1186 | + * @link http://www.php.net/manual/en/function.exif-imagetype.php#80383 |
|
| 1187 | + * @param string $fileName |
|
| 1188 | + * @return string|boolean |
|
| 1189 | + */ |
|
| 1190 | + function exif_imagetype($fileName) { |
|
| 1191 | + if (($info = getimagesize($fileName)) !== false) { |
|
| 1192 | + return $info[2]; |
|
| 1193 | + } |
|
| 1194 | + return false; |
|
| 1195 | + } |
|
| 1196 | 1196 | } |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | public function widthTopLeft() { |
| 135 | 135 | $o = $this->getOrientation(); |
| 136 | - $this->logger->debug('OC_Image->widthTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 136 | + $this->logger->debug('OC_Image->widthTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 137 | 137 | switch ($o) { |
| 138 | 138 | case -1: |
| 139 | 139 | case 1: |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function heightTopLeft() { |
| 159 | 159 | $o = $this->getOrientation(); |
| 160 | - $this->logger->debug('OC_Image->heightTopLeft() Orientation: ' . $o, array('app' => 'core')); |
|
| 160 | + $this->logger->debug('OC_Image->heightTopLeft() Orientation: '.$o, array('app' => 'core')); |
|
| 161 | 161 | switch ($o) { |
| 162 | 162 | case -1: |
| 163 | 163 | case 1: |
@@ -184,7 +184,7 @@ discard block |
||
| 184 | 184 | if ($mimeType === null) { |
| 185 | 185 | $mimeType = $this->mimeType(); |
| 186 | 186 | } |
| 187 | - header('Content-Type: ' . $mimeType); |
|
| 187 | + header('Content-Type: '.$mimeType); |
|
| 188 | 188 | return $this->_output(null, $mimeType); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | } |
| 203 | 203 | if ($filePath === null) { |
| 204 | 204 | if ($this->filePath === null) { |
| 205 | - $this->logger->error(__METHOD__ . '(): called with no path.', array('app' => 'core')); |
|
| 205 | + $this->logger->error(__METHOD__.'(): called with no path.', array('app' => 'core')); |
|
| 206 | 206 | return false; |
| 207 | 207 | } else { |
| 208 | 208 | $filePath = $this->filePath; |
@@ -226,10 +226,10 @@ discard block |
||
| 226 | 226 | } |
| 227 | 227 | $isWritable = is_writable(dirname($filePath)); |
| 228 | 228 | if (!$isWritable) { |
| 229 | - $this->logger->error(__METHOD__ . '(): Directory \'' . dirname($filePath) . '\' is not writable.', array('app' => 'core')); |
|
| 229 | + $this->logger->error(__METHOD__.'(): Directory \''.dirname($filePath).'\' is not writable.', array('app' => 'core')); |
|
| 230 | 230 | return false; |
| 231 | 231 | } elseif ($isWritable && file_exists($filePath) && !is_writable($filePath)) { |
| 232 | - $this->logger->error(__METHOD__ . '(): File \'' . $filePath . '\' is not writable.', array('app' => 'core')); |
|
| 232 | + $this->logger->error(__METHOD__.'(): File \''.$filePath.'\' is not writable.', array('app' => 'core')); |
|
| 233 | 233 | return false; |
| 234 | 234 | } |
| 235 | 235 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | $imageType = IMAGETYPE_BMP; |
| 258 | 258 | break; |
| 259 | 259 | default: |
| 260 | - throw new Exception('\OC_Image::_output(): "' . $mimeType . '" is not supported when forcing a specific output format'); |
|
| 260 | + throw new Exception('\OC_Image::_output(): "'.$mimeType.'" is not supported when forcing a specific output format'); |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | return; |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | - $exif = @exif_read_data('data://image/jpeg;base64,' . base64_encode($data)); |
|
| 439 | + $exif = @exif_read_data('data://image/jpeg;base64,'.base64_encode($data)); |
|
| 440 | 440 | if (!$exif) { |
| 441 | 441 | return; |
| 442 | 442 | } |
@@ -454,7 +454,7 @@ discard block |
||
| 454 | 454 | */ |
| 455 | 455 | public function fixOrientation() { |
| 456 | 456 | $o = $this->getOrientation(); |
| 457 | - $this->logger->debug('OC_Image->fixOrientation() Orientation: ' . $o, array('app' => 'core')); |
|
| 457 | + $this->logger->debug('OC_Image->fixOrientation() Orientation: '.$o, array('app' => 'core')); |
|
| 458 | 458 | $rotate = 0; |
| 459 | 459 | $flip = false; |
| 460 | 460 | switch ($o) { |
@@ -489,7 +489,7 @@ discard block |
||
| 489 | 489 | $rotate = 90; |
| 490 | 490 | break; |
| 491 | 491 | } |
| 492 | - if($flip && function_exists('imageflip')) { |
|
| 492 | + if ($flip && function_exists('imageflip')) { |
|
| 493 | 493 | imageflip($this->resource, IMG_FLIP_HORIZONTAL); |
| 494 | 494 | } |
| 495 | 495 | if ($rotate) { |
@@ -551,7 +551,7 @@ discard block |
||
| 551 | 551 | imagealphablending($this->resource, true); |
| 552 | 552 | imagesavealpha($this->resource, true); |
| 553 | 553 | } else { |
| 554 | - $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 554 | + $this->logger->debug('OC_Image->loadFromFile, GIF images not supported: '.$imagePath, array('app' => 'core')); |
|
| 555 | 555 | } |
| 556 | 556 | break; |
| 557 | 557 | case IMAGETYPE_JPEG: |
@@ -559,10 +559,10 @@ discard block |
||
| 559 | 559 | if (getimagesize($imagePath) !== false) { |
| 560 | 560 | $this->resource = @imagecreatefromjpeg($imagePath); |
| 561 | 561 | } else { |
| 562 | - $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: ' . $imagePath, array('app' => 'core')); |
|
| 562 | + $this->logger->debug('OC_Image->loadFromFile, JPG image not valid: '.$imagePath, array('app' => 'core')); |
|
| 563 | 563 | } |
| 564 | 564 | } else { |
| 565 | - $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 565 | + $this->logger->debug('OC_Image->loadFromFile, JPG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 566 | 566 | } |
| 567 | 567 | break; |
| 568 | 568 | case IMAGETYPE_PNG: |
@@ -572,21 +572,21 @@ discard block |
||
| 572 | 572 | imagealphablending($this->resource, true); |
| 573 | 573 | imagesavealpha($this->resource, true); |
| 574 | 574 | } else { |
| 575 | - $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 575 | + $this->logger->debug('OC_Image->loadFromFile, PNG images not supported: '.$imagePath, array('app' => 'core')); |
|
| 576 | 576 | } |
| 577 | 577 | break; |
| 578 | 578 | case IMAGETYPE_XBM: |
| 579 | 579 | if (imagetypes() & IMG_XPM) { |
| 580 | 580 | $this->resource = @imagecreatefromxbm($imagePath); |
| 581 | 581 | } else { |
| 582 | - $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 582 | + $this->logger->debug('OC_Image->loadFromFile, XBM/XPM images not supported: '.$imagePath, array('app' => 'core')); |
|
| 583 | 583 | } |
| 584 | 584 | break; |
| 585 | 585 | case IMAGETYPE_WBMP: |
| 586 | 586 | if (imagetypes() & IMG_WBMP) { |
| 587 | 587 | $this->resource = @imagecreatefromwbmp($imagePath); |
| 588 | 588 | } else { |
| 589 | - $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: ' . $imagePath, array('app' => 'core')); |
|
| 589 | + $this->logger->debug('OC_Image->loadFromFile, WBMP images not supported: '.$imagePath, array('app' => 'core')); |
|
| 590 | 590 | } |
| 591 | 591 | break; |
| 592 | 592 | case IMAGETYPE_BMP: |
@@ -696,7 +696,7 @@ discard block |
||
| 696 | 696 | */ |
| 697 | 697 | private function imagecreatefrombmp($fileName) { |
| 698 | 698 | if (!($fh = fopen($fileName, 'rb'))) { |
| 699 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName, array('app' => 'core')); |
|
| 699 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName, array('app' => 'core')); |
|
| 700 | 700 | return false; |
| 701 | 701 | } |
| 702 | 702 | // read file header |
@@ -704,7 +704,7 @@ discard block |
||
| 704 | 704 | // check for bitmap |
| 705 | 705 | if ($meta['type'] != 19778) { |
| 706 | 706 | fclose($fh); |
| 707 | - $this->logger->warning('imagecreatefrombmp: Can not open ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 707 | + $this->logger->warning('imagecreatefrombmp: Can not open '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 708 | 708 | return false; |
| 709 | 709 | } |
| 710 | 710 | // read image header |
@@ -728,7 +728,7 @@ discard block |
||
| 728 | 728 | $meta['imagesize'] = @filesize($fileName) - $meta['offset']; |
| 729 | 729 | if ($meta['imagesize'] < 1) { |
| 730 | 730 | fclose($fh); |
| 731 | - $this->logger->warning('imagecreatefrombmp: Can not obtain file size of ' . $fileName . ' is not a bitmap!', array('app' => 'core')); |
|
| 731 | + $this->logger->warning('imagecreatefrombmp: Can not obtain file size of '.$fileName.' is not a bitmap!', array('app' => 'core')); |
|
| 732 | 732 | return false; |
| 733 | 733 | } |
| 734 | 734 | } |
@@ -738,7 +738,7 @@ discard block |
||
| 738 | 738 | // read color palette |
| 739 | 739 | $palette = array(); |
| 740 | 740 | if ($meta['bits'] < 16) { |
| 741 | - $palette = unpack('l' . $meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 741 | + $palette = unpack('l'.$meta['colors'], fread($fh, $meta['colors'] * 4)); |
|
| 742 | 742 | // in rare cases the color value is signed |
| 743 | 743 | if ($palette[1] < 0) { |
| 744 | 744 | foreach ($palette as $i => $color) { |
@@ -751,7 +751,7 @@ discard block |
||
| 751 | 751 | if ($im == false) { |
| 752 | 752 | fclose($fh); |
| 753 | 753 | $this->logger->warning( |
| 754 | - 'imagecreatefrombmp: imagecreatetruecolor failed for file "' . $fileName . '" with dimensions ' . $meta['width'] . 'x' . $meta['height'], |
|
| 754 | + 'imagecreatefrombmp: imagecreatetruecolor failed for file "'.$fileName.'" with dimensions '.$meta['width'].'x'.$meta['height'], |
|
| 755 | 755 | array('app' => 'core')); |
| 756 | 756 | return false; |
| 757 | 757 | } |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | $p = 0; |
| 761 | 761 | $vide = chr(0); |
| 762 | 762 | $y = $meta['height'] - 1; |
| 763 | - $error = 'imagecreatefrombmp: ' . $fileName . ' has not enough data!'; |
|
| 763 | + $error = 'imagecreatefrombmp: '.$fileName.' has not enough data!'; |
|
| 764 | 764 | // loop through the image data beginning with the lower left corner |
| 765 | 765 | while ($y >= 0) { |
| 766 | 766 | $x = 0; |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | $this->logger->warning($error, array('app' => 'core')); |
| 773 | 773 | return $im; |
| 774 | 774 | } |
| 775 | - $color = @unpack('V', $part . $vide); |
|
| 775 | + $color = @unpack('V', $part.$vide); |
|
| 776 | 776 | break; |
| 777 | 777 | case 16: |
| 778 | 778 | if (!($part = substr($data, $p, 2))) { |
@@ -784,16 +784,16 @@ discard block |
||
| 784 | 784 | $color[1] = (($color[1] & 0xf800) >> 8) * 65536 + (($color[1] & 0x07e0) >> 3) * 256 + (($color[1] & 0x001f) << 3); |
| 785 | 785 | break; |
| 786 | 786 | case 8: |
| 787 | - $color = @unpack('n', $vide . ($data[$p] ?? '')); |
|
| 787 | + $color = @unpack('n', $vide.($data[$p] ?? '')); |
|
| 788 | 788 | $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
| 789 | 789 | break; |
| 790 | 790 | case 4: |
| 791 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 791 | + $color = @unpack('n', $vide.($data[floor($p)] ?? '')); |
|
| 792 | 792 | $color[1] = ($p * 2) % 2 == 0 ? $color[1] >> 4 : $color[1] & 0x0F; |
| 793 | 793 | $color[1] = (isset($palette[$color[1] + 1])) ? $palette[$color[1] + 1] : $palette[1]; |
| 794 | 794 | break; |
| 795 | 795 | case 1: |
| 796 | - $color = @unpack('n', $vide . ($data[floor($p)] ?? '')); |
|
| 796 | + $color = @unpack('n', $vide.($data[floor($p)] ?? '')); |
|
| 797 | 797 | switch (($p * 8) % 8) { |
| 798 | 798 | case 0: |
| 799 | 799 | $color[1] = $color[1] >> 7; |
@@ -824,7 +824,7 @@ discard block |
||
| 824 | 824 | break; |
| 825 | 825 | default: |
| 826 | 826 | fclose($fh); |
| 827 | - $this->logger->warning('imagecreatefrombmp: ' . $fileName . ' has ' . $meta['bits'] . ' bits and this is not supported!', array('app' => 'core')); |
|
| 827 | + $this->logger->warning('imagecreatefrombmp: '.$fileName.' has '.$meta['bits'].' bits and this is not supported!', array('app' => 'core')); |
|
| 828 | 828 | return false; |
| 829 | 829 | } |
| 830 | 830 | imagesetpixel($im, $x, $y, $color[1]); |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | */ |
| 847 | 847 | public function resize($maxSize) { |
| 848 | 848 | if (!$this->valid()) { |
| 849 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 849 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 850 | 850 | return false; |
| 851 | 851 | } |
| 852 | 852 | $widthOrig = imagesx($this->resource); |
@@ -861,7 +861,7 @@ discard block |
||
| 861 | 861 | $newHeight = $maxSize; |
| 862 | 862 | } |
| 863 | 863 | |
| 864 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 864 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 865 | 865 | return true; |
| 866 | 866 | } |
| 867 | 867 | |
@@ -872,7 +872,7 @@ discard block |
||
| 872 | 872 | */ |
| 873 | 873 | public function preciseResize(int $width, int $height): bool { |
| 874 | 874 | if (!$this->valid()) { |
| 875 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 875 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 876 | 876 | return false; |
| 877 | 877 | } |
| 878 | 878 | $widthOrig = imagesx($this->resource); |
@@ -880,7 +880,7 @@ discard block |
||
| 880 | 880 | $process = imagecreatetruecolor($width, $height); |
| 881 | 881 | |
| 882 | 882 | if ($process == false) { |
| 883 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 883 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 884 | 884 | imagedestroy($process); |
| 885 | 885 | return false; |
| 886 | 886 | } |
@@ -894,7 +894,7 @@ discard block |
||
| 894 | 894 | |
| 895 | 895 | imagecopyresampled($process, $this->resource, 0, 0, 0, 0, $width, $height, $widthOrig, $heightOrig); |
| 896 | 896 | if ($process == false) { |
| 897 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image', array('app' => 'core')); |
|
| 897 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image', array('app' => 'core')); |
|
| 898 | 898 | imagedestroy($process); |
| 899 | 899 | return false; |
| 900 | 900 | } |
@@ -952,7 +952,7 @@ discard block |
||
| 952 | 952 | |
| 953 | 953 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $targetWidth, $targetHeight, $width, $height); |
| 954 | 954 | if ($process == false) { |
| 955 | - $this->logger->error('OC_Image->centerCrop, Error re-sampling process image ' . $width . 'x' . $height, array('app' => 'core')); |
|
| 955 | + $this->logger->error('OC_Image->centerCrop, Error re-sampling process image '.$width.'x'.$height, array('app' => 'core')); |
|
| 956 | 956 | imagedestroy($process); |
| 957 | 957 | return false; |
| 958 | 958 | } |
@@ -972,12 +972,12 @@ discard block |
||
| 972 | 972 | */ |
| 973 | 973 | public function crop(int $x, int $y, int $w, int $h): bool { |
| 974 | 974 | if (!$this->valid()) { |
| 975 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 975 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 976 | 976 | return false; |
| 977 | 977 | } |
| 978 | 978 | $process = imagecreatetruecolor($w, $h); |
| 979 | 979 | if ($process == false) { |
| 980 | - $this->logger->error(__METHOD__ . '(): Error creating true color image', array('app' => 'core')); |
|
| 980 | + $this->logger->error(__METHOD__.'(): Error creating true color image', array('app' => 'core')); |
|
| 981 | 981 | imagedestroy($process); |
| 982 | 982 | return false; |
| 983 | 983 | } |
@@ -991,7 +991,7 @@ discard block |
||
| 991 | 991 | |
| 992 | 992 | imagecopyresampled($process, $this->resource, 0, 0, $x, $y, $w, $h, $w, $h); |
| 993 | 993 | if ($process == false) { |
| 994 | - $this->logger->error(__METHOD__ . '(): Error re-sampling process image ' . $w . 'x' . $h, array('app' => 'core')); |
|
| 994 | + $this->logger->error(__METHOD__.'(): Error re-sampling process image '.$w.'x'.$h, array('app' => 'core')); |
|
| 995 | 995 | imagedestroy($process); |
| 996 | 996 | return false; |
| 997 | 997 | } |
@@ -1011,7 +1011,7 @@ discard block |
||
| 1011 | 1011 | */ |
| 1012 | 1012 | public function fitIn($maxWidth, $maxHeight) { |
| 1013 | 1013 | if (!$this->valid()) { |
| 1014 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1014 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1015 | 1015 | return false; |
| 1016 | 1016 | } |
| 1017 | 1017 | $widthOrig = imagesx($this->resource); |
@@ -1021,7 +1021,7 @@ discard block |
||
| 1021 | 1021 | $newWidth = min($maxWidth, $ratio * $maxHeight); |
| 1022 | 1022 | $newHeight = min($maxHeight, $maxWidth / $ratio); |
| 1023 | 1023 | |
| 1024 | - $this->preciseResize((int)round($newWidth), (int)round($newHeight)); |
|
| 1024 | + $this->preciseResize((int) round($newWidth), (int) round($newHeight)); |
|
| 1025 | 1025 | return true; |
| 1026 | 1026 | } |
| 1027 | 1027 | |
@@ -1034,7 +1034,7 @@ discard block |
||
| 1034 | 1034 | */ |
| 1035 | 1035 | public function scaleDownToFit($maxWidth, $maxHeight) { |
| 1036 | 1036 | if (!$this->valid()) { |
| 1037 | - $this->logger->error(__METHOD__ . '(): No image loaded', array('app' => 'core')); |
|
| 1037 | + $this->logger->error(__METHOD__.'(): No image loaded', array('app' => 'core')); |
|
| 1038 | 1038 | return false; |
| 1039 | 1039 | } |
| 1040 | 1040 | $widthOrig = imagesx($this->resource); |
@@ -1092,7 +1092,7 @@ discard block |
||
| 1092 | 1092 | if ($bit <= 8) { |
| 1093 | 1093 | for ($i = 0; $i < $colorsNum; $i++) { |
| 1094 | 1094 | $colors = imagecolorsforindex($im, $i); |
| 1095 | - $rgbQuad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; |
|
| 1095 | + $rgbQuad .= chr($colors['blue']).chr($colors['green']).chr($colors['red'])."\0"; |
|
| 1096 | 1096 | } |
| 1097 | 1097 | $bmpData = ''; |
| 1098 | 1098 | if ($compression == 0 || $bit < 8) { |
@@ -1125,7 +1125,7 @@ discard block |
||
| 1125 | 1125 | $index = imagecolorat($im, $i, $j); |
| 1126 | 1126 | if ($index !== $lastIndex || $sameNum > 255) { |
| 1127 | 1127 | if ($sameNum != 0) { |
| 1128 | - $bmpData .= chr($sameNum) . chr($lastIndex); |
|
| 1128 | + $bmpData .= chr($sameNum).chr($lastIndex); |
|
| 1129 | 1129 | } |
| 1130 | 1130 | $lastIndex = $index; |
| 1131 | 1131 | $sameNum = 1; |
@@ -1166,15 +1166,15 @@ discard block |
||
| 1166 | 1166 | $sizeData = strlen($bmpData); |
| 1167 | 1167 | $colorsNum = 0; |
| 1168 | 1168 | } |
| 1169 | - $fileHeader = 'BM' . pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1169 | + $fileHeader = 'BM'.pack('V3', 54 + $sizeQuad + $sizeData, 0, 54 + $sizeQuad); |
|
| 1170 | 1170 | $infoHeader = pack('V3v2V*', 0x28, $width, $height, 1, $bit, $compression, $sizeData, 0, 0, $colorsNum, 0); |
| 1171 | 1171 | if ($fileName != '') { |
| 1172 | 1172 | $fp = fopen($fileName, 'wb'); |
| 1173 | - fwrite($fp, $fileHeader . $infoHeader . $rgbQuad . $bmpData); |
|
| 1173 | + fwrite($fp, $fileHeader.$infoHeader.$rgbQuad.$bmpData); |
|
| 1174 | 1174 | fclose($fp); |
| 1175 | 1175 | return true; |
| 1176 | 1176 | } |
| 1177 | - echo $fileHeader . $infoHeader . $rgbQuad . $bmpData; |
|
| 1177 | + echo $fileHeader.$infoHeader.$rgbQuad.$bmpData; |
|
| 1178 | 1178 | return true; |
| 1179 | 1179 | } |
| 1180 | 1180 | } |
@@ -57,569 +57,569 @@ |
||
| 57 | 57 | * This class provides the functionality needed to install, update and remove apps |
| 58 | 58 | */ |
| 59 | 59 | class Installer { |
| 60 | - /** @var AppFetcher */ |
|
| 61 | - private $appFetcher; |
|
| 62 | - /** @var IClientService */ |
|
| 63 | - private $clientService; |
|
| 64 | - /** @var ITempManager */ |
|
| 65 | - private $tempManager; |
|
| 66 | - /** @var ILogger */ |
|
| 67 | - private $logger; |
|
| 68 | - /** @var IConfig */ |
|
| 69 | - private $config; |
|
| 70 | - /** @var array - for caching the result of app fetcher */ |
|
| 71 | - private $apps = null; |
|
| 72 | - /** @var bool|null - for caching the result of the ready status */ |
|
| 73 | - private $isInstanceReadyForUpdates = null; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param AppFetcher $appFetcher |
|
| 77 | - * @param IClientService $clientService |
|
| 78 | - * @param ITempManager $tempManager |
|
| 79 | - * @param ILogger $logger |
|
| 80 | - * @param IConfig $config |
|
| 81 | - */ |
|
| 82 | - public function __construct(AppFetcher $appFetcher, |
|
| 83 | - IClientService $clientService, |
|
| 84 | - ITempManager $tempManager, |
|
| 85 | - ILogger $logger, |
|
| 86 | - IConfig $config) { |
|
| 87 | - $this->appFetcher = $appFetcher; |
|
| 88 | - $this->clientService = $clientService; |
|
| 89 | - $this->tempManager = $tempManager; |
|
| 90 | - $this->logger = $logger; |
|
| 91 | - $this->config = $config; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Installs an app that is located in one of the app folders already |
|
| 96 | - * |
|
| 97 | - * @param string $appId App to install |
|
| 98 | - * @throws \Exception |
|
| 99 | - * @return string app ID |
|
| 100 | - */ |
|
| 101 | - public function installApp($appId) { |
|
| 102 | - $app = \OC_App::findAppInDirectories($appId); |
|
| 103 | - if($app === false) { |
|
| 104 | - throw new \Exception('App not found in any app directory'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - $basedir = $app['path'].'/'.$appId; |
|
| 108 | - $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 109 | - |
|
| 110 | - $l = \OC::$server->getL10N('core'); |
|
| 111 | - |
|
| 112 | - if(!is_array($info)) { |
|
| 113 | - throw new \Exception( |
|
| 114 | - $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 115 | - [$info['name']] |
|
| 116 | - ) |
|
| 117 | - ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $version = \OCP\Util::getVersion(); |
|
| 121 | - if (!\OC_App::isAppCompatible($version, $info)) { |
|
| 122 | - throw new \Exception( |
|
| 123 | - // TODO $l |
|
| 124 | - $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 125 | - [$info['name']] |
|
| 126 | - ) |
|
| 127 | - ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // check for required dependencies |
|
| 131 | - \OC_App::checkAppDependencies($this->config, $l, $info); |
|
| 132 | - \OC_App::registerAutoloading($appId, $basedir); |
|
| 133 | - |
|
| 134 | - //install the database |
|
| 135 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 136 | - if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
|
| 137 | - OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 138 | - } else { |
|
| 139 | - OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 140 | - } |
|
| 141 | - } else { |
|
| 142 | - $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection()); |
|
| 143 | - $ms->migrate(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 147 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 148 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - //run appinfo/install.php |
|
| 152 | - if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 153 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $appData = OC_App::getAppInfo($appId); |
|
| 157 | - OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 158 | - |
|
| 159 | - //set the installed version |
|
| 160 | - \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 161 | - \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 162 | - |
|
| 163 | - //set remote/public handlers |
|
| 164 | - foreach($info['remote'] as $name=>$path) { |
|
| 165 | - \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 166 | - } |
|
| 167 | - foreach($info['public'] as $name=>$path) { |
|
| 168 | - \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - OC_App::setAppTypes($info['id']); |
|
| 172 | - |
|
| 173 | - return $info['id']; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @brief checks whether or not an app is installed |
|
| 178 | - * @param string $app app |
|
| 179 | - * @returns bool |
|
| 180 | - * |
|
| 181 | - * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 182 | - */ |
|
| 183 | - public static function isInstalled( $app ) { |
|
| 184 | - return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Updates the specified app from the appstore |
|
| 189 | - * |
|
| 190 | - * @param string $appId |
|
| 191 | - * @return bool |
|
| 192 | - */ |
|
| 193 | - public function updateAppstoreApp($appId) { |
|
| 194 | - if($this->isUpdateAvailable($appId)) { |
|
| 195 | - try { |
|
| 196 | - $this->downloadApp($appId); |
|
| 197 | - } catch (\Exception $e) { |
|
| 198 | - $this->logger->logException($e, [ |
|
| 199 | - 'level' => \OCP\Util::ERROR, |
|
| 200 | - 'app' => 'core', |
|
| 201 | - ]); |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - return OC_App::updateApp($appId); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return false; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Downloads an app and puts it into the app directory |
|
| 212 | - * |
|
| 213 | - * @param string $appId |
|
| 214 | - * |
|
| 215 | - * @throws \Exception If the installation was not successful |
|
| 216 | - */ |
|
| 217 | - public function downloadApp($appId) { |
|
| 218 | - $appId = strtolower($appId); |
|
| 219 | - |
|
| 220 | - $apps = $this->appFetcher->get(); |
|
| 221 | - foreach($apps as $app) { |
|
| 222 | - if($app['id'] === $appId) { |
|
| 223 | - // Load the certificate |
|
| 224 | - $certificate = new X509(); |
|
| 225 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 226 | - $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 227 | - |
|
| 228 | - // Verify if the certificate has been revoked |
|
| 229 | - $crl = new X509(); |
|
| 230 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 231 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 232 | - if($crl->validateSignature() !== true) { |
|
| 233 | - throw new \Exception('Could not validate CRL signature'); |
|
| 234 | - } |
|
| 235 | - $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 236 | - $revoked = $crl->getRevoked($csn); |
|
| 237 | - if ($revoked !== false) { |
|
| 238 | - throw new \Exception( |
|
| 239 | - sprintf( |
|
| 240 | - 'Certificate "%s" has been revoked', |
|
| 241 | - $csn |
|
| 242 | - ) |
|
| 243 | - ); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 247 | - if($certificate->validateSignature() !== true) { |
|
| 248 | - throw new \Exception( |
|
| 249 | - sprintf( |
|
| 250 | - 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 251 | - $appId |
|
| 252 | - ) |
|
| 253 | - ); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - // Verify if the certificate is issued for the requested app id |
|
| 257 | - $certInfo = openssl_x509_parse($app['certificate']); |
|
| 258 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 259 | - throw new \Exception( |
|
| 260 | - sprintf( |
|
| 261 | - 'App with id %s has a cert with no CN', |
|
| 262 | - $appId |
|
| 263 | - ) |
|
| 264 | - ); |
|
| 265 | - } |
|
| 266 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 267 | - throw new \Exception( |
|
| 268 | - sprintf( |
|
| 269 | - 'App with id %s has a cert issued to %s', |
|
| 270 | - $appId, |
|
| 271 | - $certInfo['subject']['CN'] |
|
| 272 | - ) |
|
| 273 | - ); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // Download the release |
|
| 277 | - $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 278 | - $client = $this->clientService->newClient(); |
|
| 279 | - $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 280 | - |
|
| 281 | - // Check if the signature actually matches the downloaded content |
|
| 282 | - $certificate = openssl_get_publickey($app['certificate']); |
|
| 283 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 284 | - openssl_free_key($certificate); |
|
| 285 | - |
|
| 286 | - if($verified === true) { |
|
| 287 | - // Seems to match, let's proceed |
|
| 288 | - $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 289 | - $archive = new TAR($tempFile); |
|
| 290 | - |
|
| 291 | - if($archive) { |
|
| 292 | - if (!$archive->extract($extractDir)) { |
|
| 293 | - throw new \Exception( |
|
| 294 | - sprintf( |
|
| 295 | - 'Could not extract app %s', |
|
| 296 | - $appId |
|
| 297 | - ) |
|
| 298 | - ); |
|
| 299 | - } |
|
| 300 | - $allFiles = scandir($extractDir); |
|
| 301 | - $folders = array_diff($allFiles, ['.', '..']); |
|
| 302 | - $folders = array_values($folders); |
|
| 303 | - |
|
| 304 | - if(count($folders) > 1) { |
|
| 305 | - throw new \Exception( |
|
| 306 | - sprintf( |
|
| 307 | - 'Extracted app %s has more than 1 folder', |
|
| 308 | - $appId |
|
| 309 | - ) |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - // Check if appinfo/info.xml has the same app ID as well |
|
| 314 | - $loadEntities = libxml_disable_entity_loader(false); |
|
| 315 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 316 | - libxml_disable_entity_loader($loadEntities); |
|
| 317 | - if((string)$xml->id !== $appId) { |
|
| 318 | - throw new \Exception( |
|
| 319 | - sprintf( |
|
| 320 | - 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 321 | - $appId, |
|
| 322 | - (string)$xml->id |
|
| 323 | - ) |
|
| 324 | - ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Check if the version is lower than before |
|
| 328 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 329 | - $newVersion = (string)$xml->version; |
|
| 330 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 331 | - throw new \Exception( |
|
| 332 | - sprintf( |
|
| 333 | - 'App for id %s has version %s and tried to update to lower version %s', |
|
| 334 | - $appId, |
|
| 335 | - $currentVersion, |
|
| 336 | - $newVersion |
|
| 337 | - ) |
|
| 338 | - ); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 342 | - // Remove old app with the ID if existent |
|
| 343 | - OC_Helper::rmdirr($baseDir); |
|
| 344 | - // Move to app folder |
|
| 345 | - if(@mkdir($baseDir)) { |
|
| 346 | - $extractDir .= '/' . $folders[0]; |
|
| 347 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 348 | - } |
|
| 349 | - OC_Helper::copyr($extractDir, $baseDir); |
|
| 350 | - OC_Helper::rmdirr($extractDir); |
|
| 351 | - return; |
|
| 352 | - } else { |
|
| 353 | - throw new \Exception( |
|
| 354 | - sprintf( |
|
| 355 | - 'Could not extract app with ID %s to %s', |
|
| 356 | - $appId, |
|
| 357 | - $extractDir |
|
| 358 | - ) |
|
| 359 | - ); |
|
| 360 | - } |
|
| 361 | - } else { |
|
| 362 | - // Signature does not match |
|
| 363 | - throw new \Exception( |
|
| 364 | - sprintf( |
|
| 365 | - 'App with id %s has invalid signature', |
|
| 366 | - $appId |
|
| 367 | - ) |
|
| 368 | - ); |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - throw new \Exception( |
|
| 374 | - sprintf( |
|
| 375 | - 'Could not download app %s', |
|
| 376 | - $appId |
|
| 377 | - ) |
|
| 378 | - ); |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Check if an update for the app is available |
|
| 383 | - * |
|
| 384 | - * @param string $appId |
|
| 385 | - * @return string|false false or the version number of the update |
|
| 386 | - */ |
|
| 387 | - public function isUpdateAvailable($appId) { |
|
| 388 | - if ($this->isInstanceReadyForUpdates === null) { |
|
| 389 | - $installPath = OC_App::getInstallPath(); |
|
| 390 | - if ($installPath === false || $installPath === null) { |
|
| 391 | - $this->isInstanceReadyForUpdates = false; |
|
| 392 | - } else { |
|
| 393 | - $this->isInstanceReadyForUpdates = true; |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($this->isInstanceReadyForUpdates === false) { |
|
| 398 | - return false; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - if ($this->isInstalledFromGit($appId) === true) { |
|
| 402 | - return false; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - if ($this->apps === null) { |
|
| 406 | - $this->apps = $this->appFetcher->get(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - foreach($this->apps as $app) { |
|
| 410 | - if($app['id'] === $appId) { |
|
| 411 | - $currentVersion = OC_App::getAppVersion($appId); |
|
| 412 | - $newestVersion = $app['releases'][0]['version']; |
|
| 413 | - if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 414 | - return $newestVersion; |
|
| 415 | - } else { |
|
| 416 | - return false; |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - return false; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Check if app has been installed from git |
|
| 426 | - * @param string $name name of the application to remove |
|
| 427 | - * @return boolean |
|
| 428 | - * |
|
| 429 | - * The function will check if the path contains a .git folder |
|
| 430 | - */ |
|
| 431 | - private function isInstalledFromGit($appId) { |
|
| 432 | - $app = \OC_App::findAppInDirectories($appId); |
|
| 433 | - if($app === false) { |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 436 | - $basedir = $app['path'].'/'.$appId; |
|
| 437 | - return file_exists($basedir.'/.git/'); |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Check if app is already downloaded |
|
| 442 | - * @param string $name name of the application to remove |
|
| 443 | - * @return boolean |
|
| 444 | - * |
|
| 445 | - * The function will check if the app is already downloaded in the apps repository |
|
| 446 | - */ |
|
| 447 | - public function isDownloaded($name) { |
|
| 448 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 449 | - $dirToTest = $dir['path']; |
|
| 450 | - $dirToTest .= '/'; |
|
| 451 | - $dirToTest .= $name; |
|
| 452 | - $dirToTest .= '/'; |
|
| 453 | - |
|
| 454 | - if (is_dir($dirToTest)) { |
|
| 455 | - return true; |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Removes an app |
|
| 464 | - * @param string $appId ID of the application to remove |
|
| 465 | - * @return boolean |
|
| 466 | - * |
|
| 467 | - * |
|
| 468 | - * This function works as follows |
|
| 469 | - * -# call uninstall repair steps |
|
| 470 | - * -# removing the files |
|
| 471 | - * |
|
| 472 | - * The function will not delete preferences, tables and the configuration, |
|
| 473 | - * this has to be done by the function oc_app_uninstall(). |
|
| 474 | - */ |
|
| 475 | - public function removeApp($appId) { |
|
| 476 | - if($this->isDownloaded( $appId )) { |
|
| 477 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 478 | - OC_Helper::rmdirr($appDir); |
|
| 479 | - return true; |
|
| 480 | - }else{ |
|
| 481 | - \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 482 | - |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Installs the app within the bundle and marks the bundle as installed |
|
| 490 | - * |
|
| 491 | - * @param Bundle $bundle |
|
| 492 | - * @throws \Exception If app could not get installed |
|
| 493 | - */ |
|
| 494 | - public function installAppBundle(Bundle $bundle) { |
|
| 495 | - $appIds = $bundle->getAppIdentifiers(); |
|
| 496 | - foreach($appIds as $appId) { |
|
| 497 | - if(!$this->isDownloaded($appId)) { |
|
| 498 | - $this->downloadApp($appId); |
|
| 499 | - } |
|
| 500 | - $this->installApp($appId); |
|
| 501 | - $app = new OC_App(); |
|
| 502 | - $app->enable($appId); |
|
| 503 | - } |
|
| 504 | - $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true); |
|
| 505 | - $bundles[] = $bundle->getIdentifier(); |
|
| 506 | - $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles)); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - /** |
|
| 510 | - * Installs shipped apps |
|
| 511 | - * |
|
| 512 | - * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 513 | - * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 514 | - * working ownCloud at the end instead of an aborted update. |
|
| 515 | - * @return array Array of error messages (appid => Exception) |
|
| 516 | - */ |
|
| 517 | - public static function installShippedApps($softErrors = false) { |
|
| 518 | - $errors = []; |
|
| 519 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 520 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 521 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 522 | - if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 523 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 524 | - if(!Installer::isInstalled($filename)) { |
|
| 525 | - $info=OC_App::getAppInfo($filename); |
|
| 526 | - $enabled = isset($info['default_enable']); |
|
| 527 | - if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 528 | - && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 529 | - if ($softErrors) { |
|
| 530 | - try { |
|
| 531 | - Installer::installShippedApp($filename); |
|
| 532 | - } catch (HintException $e) { |
|
| 533 | - if ($e->getPrevious() instanceof TableExistsException) { |
|
| 534 | - $errors[$filename] = $e; |
|
| 535 | - continue; |
|
| 536 | - } |
|
| 537 | - throw $e; |
|
| 538 | - } |
|
| 539 | - } else { |
|
| 540 | - Installer::installShippedApp($filename); |
|
| 541 | - } |
|
| 542 | - \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 543 | - } |
|
| 544 | - } |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - } |
|
| 548 | - closedir( $dir ); |
|
| 549 | - } |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - return $errors; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * install an app already placed in the app folder |
|
| 557 | - * @param string $app id of the app to install |
|
| 558 | - * @return integer |
|
| 559 | - */ |
|
| 560 | - public static function installShippedApp($app) { |
|
| 561 | - //install the database |
|
| 562 | - $appPath = OC_App::getAppPath($app); |
|
| 563 | - \OC_App::registerAutoloading($app, $appPath); |
|
| 564 | - |
|
| 565 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 566 | - try { |
|
| 567 | - OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 568 | - } catch (TableExistsException $e) { |
|
| 569 | - throw new HintException( |
|
| 570 | - 'Failed to enable app ' . $app, |
|
| 571 | - 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
|
| 572 | - 0, $e |
|
| 573 | - ); |
|
| 574 | - } |
|
| 575 | - } else { |
|
| 576 | - $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection()); |
|
| 577 | - $ms->migrate(); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - //run appinfo/install.php |
|
| 581 | - self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 582 | - |
|
| 583 | - $info = OC_App::getAppInfo($app); |
|
| 584 | - if (is_null($info)) { |
|
| 585 | - return false; |
|
| 586 | - } |
|
| 587 | - \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 588 | - |
|
| 589 | - OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 590 | - |
|
| 591 | - $config = \OC::$server->getConfig(); |
|
| 592 | - |
|
| 593 | - $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 594 | - if (array_key_exists('ocsid', $info)) { |
|
| 595 | - $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - //set remote/public handlers |
|
| 599 | - foreach($info['remote'] as $name=>$path) { |
|
| 600 | - $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 601 | - } |
|
| 602 | - foreach($info['public'] as $name=>$path) { |
|
| 603 | - $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - OC_App::setAppTypes($info['id']); |
|
| 607 | - |
|
| 608 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 609 | - // requires that autoloading was registered for the app, |
|
| 610 | - // as happens before running the install.php some lines above |
|
| 611 | - \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - return $info['id']; |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * @param string $script |
|
| 619 | - */ |
|
| 620 | - private static function includeAppScript($script) { |
|
| 621 | - if ( file_exists($script) ){ |
|
| 622 | - include $script; |
|
| 623 | - } |
|
| 624 | - } |
|
| 60 | + /** @var AppFetcher */ |
|
| 61 | + private $appFetcher; |
|
| 62 | + /** @var IClientService */ |
|
| 63 | + private $clientService; |
|
| 64 | + /** @var ITempManager */ |
|
| 65 | + private $tempManager; |
|
| 66 | + /** @var ILogger */ |
|
| 67 | + private $logger; |
|
| 68 | + /** @var IConfig */ |
|
| 69 | + private $config; |
|
| 70 | + /** @var array - for caching the result of app fetcher */ |
|
| 71 | + private $apps = null; |
|
| 72 | + /** @var bool|null - for caching the result of the ready status */ |
|
| 73 | + private $isInstanceReadyForUpdates = null; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param AppFetcher $appFetcher |
|
| 77 | + * @param IClientService $clientService |
|
| 78 | + * @param ITempManager $tempManager |
|
| 79 | + * @param ILogger $logger |
|
| 80 | + * @param IConfig $config |
|
| 81 | + */ |
|
| 82 | + public function __construct(AppFetcher $appFetcher, |
|
| 83 | + IClientService $clientService, |
|
| 84 | + ITempManager $tempManager, |
|
| 85 | + ILogger $logger, |
|
| 86 | + IConfig $config) { |
|
| 87 | + $this->appFetcher = $appFetcher; |
|
| 88 | + $this->clientService = $clientService; |
|
| 89 | + $this->tempManager = $tempManager; |
|
| 90 | + $this->logger = $logger; |
|
| 91 | + $this->config = $config; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Installs an app that is located in one of the app folders already |
|
| 96 | + * |
|
| 97 | + * @param string $appId App to install |
|
| 98 | + * @throws \Exception |
|
| 99 | + * @return string app ID |
|
| 100 | + */ |
|
| 101 | + public function installApp($appId) { |
|
| 102 | + $app = \OC_App::findAppInDirectories($appId); |
|
| 103 | + if($app === false) { |
|
| 104 | + throw new \Exception('App not found in any app directory'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + $basedir = $app['path'].'/'.$appId; |
|
| 108 | + $info = OC_App::getAppInfo($basedir.'/appinfo/info.xml', true); |
|
| 109 | + |
|
| 110 | + $l = \OC::$server->getL10N('core'); |
|
| 111 | + |
|
| 112 | + if(!is_array($info)) { |
|
| 113 | + throw new \Exception( |
|
| 114 | + $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
|
| 115 | + [$info['name']] |
|
| 116 | + ) |
|
| 117 | + ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $version = \OCP\Util::getVersion(); |
|
| 121 | + if (!\OC_App::isAppCompatible($version, $info)) { |
|
| 122 | + throw new \Exception( |
|
| 123 | + // TODO $l |
|
| 124 | + $l->t('App "%s" cannot be installed because it is not compatible with this version of the server.', |
|
| 125 | + [$info['name']] |
|
| 126 | + ) |
|
| 127 | + ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // check for required dependencies |
|
| 131 | + \OC_App::checkAppDependencies($this->config, $l, $info); |
|
| 132 | + \OC_App::registerAutoloading($appId, $basedir); |
|
| 133 | + |
|
| 134 | + //install the database |
|
| 135 | + if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 136 | + if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
|
| 137 | + OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 138 | + } else { |
|
| 139 | + OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); |
|
| 140 | + } |
|
| 141 | + } else { |
|
| 142 | + $ms = new \OC\DB\MigrationService($info['id'], \OC::$server->getDatabaseConnection()); |
|
| 143 | + $ms->migrate(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 147 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 148 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + //run appinfo/install.php |
|
| 152 | + if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 153 | + self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $appData = OC_App::getAppInfo($appId); |
|
| 157 | + OC_App::executeRepairSteps($appId, $appData['repair-steps']['install']); |
|
| 158 | + |
|
| 159 | + //set the installed version |
|
| 160 | + \OC::$server->getConfig()->setAppValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'], false)); |
|
| 161 | + \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
|
| 162 | + |
|
| 163 | + //set remote/public handlers |
|
| 164 | + foreach($info['remote'] as $name=>$path) { |
|
| 165 | + \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
|
| 166 | + } |
|
| 167 | + foreach($info['public'] as $name=>$path) { |
|
| 168 | + \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + OC_App::setAppTypes($info['id']); |
|
| 172 | + |
|
| 173 | + return $info['id']; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @brief checks whether or not an app is installed |
|
| 178 | + * @param string $app app |
|
| 179 | + * @returns bool |
|
| 180 | + * |
|
| 181 | + * Checks whether or not an app is installed, i.e. registered in apps table. |
|
| 182 | + */ |
|
| 183 | + public static function isInstalled( $app ) { |
|
| 184 | + return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Updates the specified app from the appstore |
|
| 189 | + * |
|
| 190 | + * @param string $appId |
|
| 191 | + * @return bool |
|
| 192 | + */ |
|
| 193 | + public function updateAppstoreApp($appId) { |
|
| 194 | + if($this->isUpdateAvailable($appId)) { |
|
| 195 | + try { |
|
| 196 | + $this->downloadApp($appId); |
|
| 197 | + } catch (\Exception $e) { |
|
| 198 | + $this->logger->logException($e, [ |
|
| 199 | + 'level' => \OCP\Util::ERROR, |
|
| 200 | + 'app' => 'core', |
|
| 201 | + ]); |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + return OC_App::updateApp($appId); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return false; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Downloads an app and puts it into the app directory |
|
| 212 | + * |
|
| 213 | + * @param string $appId |
|
| 214 | + * |
|
| 215 | + * @throws \Exception If the installation was not successful |
|
| 216 | + */ |
|
| 217 | + public function downloadApp($appId) { |
|
| 218 | + $appId = strtolower($appId); |
|
| 219 | + |
|
| 220 | + $apps = $this->appFetcher->get(); |
|
| 221 | + foreach($apps as $app) { |
|
| 222 | + if($app['id'] === $appId) { |
|
| 223 | + // Load the certificate |
|
| 224 | + $certificate = new X509(); |
|
| 225 | + $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 226 | + $loadedCertificate = $certificate->loadX509($app['certificate']); |
|
| 227 | + |
|
| 228 | + // Verify if the certificate has been revoked |
|
| 229 | + $crl = new X509(); |
|
| 230 | + $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 231 | + $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 232 | + if($crl->validateSignature() !== true) { |
|
| 233 | + throw new \Exception('Could not validate CRL signature'); |
|
| 234 | + } |
|
| 235 | + $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
|
| 236 | + $revoked = $crl->getRevoked($csn); |
|
| 237 | + if ($revoked !== false) { |
|
| 238 | + throw new \Exception( |
|
| 239 | + sprintf( |
|
| 240 | + 'Certificate "%s" has been revoked', |
|
| 241 | + $csn |
|
| 242 | + ) |
|
| 243 | + ); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
|
| 247 | + if($certificate->validateSignature() !== true) { |
|
| 248 | + throw new \Exception( |
|
| 249 | + sprintf( |
|
| 250 | + 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
|
| 251 | + $appId |
|
| 252 | + ) |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + // Verify if the certificate is issued for the requested app id |
|
| 257 | + $certInfo = openssl_x509_parse($app['certificate']); |
|
| 258 | + if(!isset($certInfo['subject']['CN'])) { |
|
| 259 | + throw new \Exception( |
|
| 260 | + sprintf( |
|
| 261 | + 'App with id %s has a cert with no CN', |
|
| 262 | + $appId |
|
| 263 | + ) |
|
| 264 | + ); |
|
| 265 | + } |
|
| 266 | + if($certInfo['subject']['CN'] !== $appId) { |
|
| 267 | + throw new \Exception( |
|
| 268 | + sprintf( |
|
| 269 | + 'App with id %s has a cert issued to %s', |
|
| 270 | + $appId, |
|
| 271 | + $certInfo['subject']['CN'] |
|
| 272 | + ) |
|
| 273 | + ); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // Download the release |
|
| 277 | + $tempFile = $this->tempManager->getTemporaryFile('.tar.gz'); |
|
| 278 | + $client = $this->clientService->newClient(); |
|
| 279 | + $client->get($app['releases'][0]['download'], ['save_to' => $tempFile]); |
|
| 280 | + |
|
| 281 | + // Check if the signature actually matches the downloaded content |
|
| 282 | + $certificate = openssl_get_publickey($app['certificate']); |
|
| 283 | + $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 284 | + openssl_free_key($certificate); |
|
| 285 | + |
|
| 286 | + if($verified === true) { |
|
| 287 | + // Seems to match, let's proceed |
|
| 288 | + $extractDir = $this->tempManager->getTemporaryFolder(); |
|
| 289 | + $archive = new TAR($tempFile); |
|
| 290 | + |
|
| 291 | + if($archive) { |
|
| 292 | + if (!$archive->extract($extractDir)) { |
|
| 293 | + throw new \Exception( |
|
| 294 | + sprintf( |
|
| 295 | + 'Could not extract app %s', |
|
| 296 | + $appId |
|
| 297 | + ) |
|
| 298 | + ); |
|
| 299 | + } |
|
| 300 | + $allFiles = scandir($extractDir); |
|
| 301 | + $folders = array_diff($allFiles, ['.', '..']); |
|
| 302 | + $folders = array_values($folders); |
|
| 303 | + |
|
| 304 | + if(count($folders) > 1) { |
|
| 305 | + throw new \Exception( |
|
| 306 | + sprintf( |
|
| 307 | + 'Extracted app %s has more than 1 folder', |
|
| 308 | + $appId |
|
| 309 | + ) |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + // Check if appinfo/info.xml has the same app ID as well |
|
| 314 | + $loadEntities = libxml_disable_entity_loader(false); |
|
| 315 | + $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 316 | + libxml_disable_entity_loader($loadEntities); |
|
| 317 | + if((string)$xml->id !== $appId) { |
|
| 318 | + throw new \Exception( |
|
| 319 | + sprintf( |
|
| 320 | + 'App for id %s has a wrong app ID in info.xml: %s', |
|
| 321 | + $appId, |
|
| 322 | + (string)$xml->id |
|
| 323 | + ) |
|
| 324 | + ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Check if the version is lower than before |
|
| 328 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 329 | + $newVersion = (string)$xml->version; |
|
| 330 | + if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 331 | + throw new \Exception( |
|
| 332 | + sprintf( |
|
| 333 | + 'App for id %s has version %s and tried to update to lower version %s', |
|
| 334 | + $appId, |
|
| 335 | + $currentVersion, |
|
| 336 | + $newVersion |
|
| 337 | + ) |
|
| 338 | + ); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 342 | + // Remove old app with the ID if existent |
|
| 343 | + OC_Helper::rmdirr($baseDir); |
|
| 344 | + // Move to app folder |
|
| 345 | + if(@mkdir($baseDir)) { |
|
| 346 | + $extractDir .= '/' . $folders[0]; |
|
| 347 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 348 | + } |
|
| 349 | + OC_Helper::copyr($extractDir, $baseDir); |
|
| 350 | + OC_Helper::rmdirr($extractDir); |
|
| 351 | + return; |
|
| 352 | + } else { |
|
| 353 | + throw new \Exception( |
|
| 354 | + sprintf( |
|
| 355 | + 'Could not extract app with ID %s to %s', |
|
| 356 | + $appId, |
|
| 357 | + $extractDir |
|
| 358 | + ) |
|
| 359 | + ); |
|
| 360 | + } |
|
| 361 | + } else { |
|
| 362 | + // Signature does not match |
|
| 363 | + throw new \Exception( |
|
| 364 | + sprintf( |
|
| 365 | + 'App with id %s has invalid signature', |
|
| 366 | + $appId |
|
| 367 | + ) |
|
| 368 | + ); |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + throw new \Exception( |
|
| 374 | + sprintf( |
|
| 375 | + 'Could not download app %s', |
|
| 376 | + $appId |
|
| 377 | + ) |
|
| 378 | + ); |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Check if an update for the app is available |
|
| 383 | + * |
|
| 384 | + * @param string $appId |
|
| 385 | + * @return string|false false or the version number of the update |
|
| 386 | + */ |
|
| 387 | + public function isUpdateAvailable($appId) { |
|
| 388 | + if ($this->isInstanceReadyForUpdates === null) { |
|
| 389 | + $installPath = OC_App::getInstallPath(); |
|
| 390 | + if ($installPath === false || $installPath === null) { |
|
| 391 | + $this->isInstanceReadyForUpdates = false; |
|
| 392 | + } else { |
|
| 393 | + $this->isInstanceReadyForUpdates = true; |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($this->isInstanceReadyForUpdates === false) { |
|
| 398 | + return false; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + if ($this->isInstalledFromGit($appId) === true) { |
|
| 402 | + return false; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + if ($this->apps === null) { |
|
| 406 | + $this->apps = $this->appFetcher->get(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + foreach($this->apps as $app) { |
|
| 410 | + if($app['id'] === $appId) { |
|
| 411 | + $currentVersion = OC_App::getAppVersion($appId); |
|
| 412 | + $newestVersion = $app['releases'][0]['version']; |
|
| 413 | + if (version_compare($newestVersion, $currentVersion, '>')) { |
|
| 414 | + return $newestVersion; |
|
| 415 | + } else { |
|
| 416 | + return false; |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + return false; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Check if app has been installed from git |
|
| 426 | + * @param string $name name of the application to remove |
|
| 427 | + * @return boolean |
|
| 428 | + * |
|
| 429 | + * The function will check if the path contains a .git folder |
|
| 430 | + */ |
|
| 431 | + private function isInstalledFromGit($appId) { |
|
| 432 | + $app = \OC_App::findAppInDirectories($appId); |
|
| 433 | + if($app === false) { |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | + $basedir = $app['path'].'/'.$appId; |
|
| 437 | + return file_exists($basedir.'/.git/'); |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Check if app is already downloaded |
|
| 442 | + * @param string $name name of the application to remove |
|
| 443 | + * @return boolean |
|
| 444 | + * |
|
| 445 | + * The function will check if the app is already downloaded in the apps repository |
|
| 446 | + */ |
|
| 447 | + public function isDownloaded($name) { |
|
| 448 | + foreach(\OC::$APPSROOTS as $dir) { |
|
| 449 | + $dirToTest = $dir['path']; |
|
| 450 | + $dirToTest .= '/'; |
|
| 451 | + $dirToTest .= $name; |
|
| 452 | + $dirToTest .= '/'; |
|
| 453 | + |
|
| 454 | + if (is_dir($dirToTest)) { |
|
| 455 | + return true; |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Removes an app |
|
| 464 | + * @param string $appId ID of the application to remove |
|
| 465 | + * @return boolean |
|
| 466 | + * |
|
| 467 | + * |
|
| 468 | + * This function works as follows |
|
| 469 | + * -# call uninstall repair steps |
|
| 470 | + * -# removing the files |
|
| 471 | + * |
|
| 472 | + * The function will not delete preferences, tables and the configuration, |
|
| 473 | + * this has to be done by the function oc_app_uninstall(). |
|
| 474 | + */ |
|
| 475 | + public function removeApp($appId) { |
|
| 476 | + if($this->isDownloaded( $appId )) { |
|
| 477 | + $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 478 | + OC_Helper::rmdirr($appDir); |
|
| 479 | + return true; |
|
| 480 | + }else{ |
|
| 481 | + \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
|
| 482 | + |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Installs the app within the bundle and marks the bundle as installed |
|
| 490 | + * |
|
| 491 | + * @param Bundle $bundle |
|
| 492 | + * @throws \Exception If app could not get installed |
|
| 493 | + */ |
|
| 494 | + public function installAppBundle(Bundle $bundle) { |
|
| 495 | + $appIds = $bundle->getAppIdentifiers(); |
|
| 496 | + foreach($appIds as $appId) { |
|
| 497 | + if(!$this->isDownloaded($appId)) { |
|
| 498 | + $this->downloadApp($appId); |
|
| 499 | + } |
|
| 500 | + $this->installApp($appId); |
|
| 501 | + $app = new OC_App(); |
|
| 502 | + $app->enable($appId); |
|
| 503 | + } |
|
| 504 | + $bundles = json_decode($this->config->getAppValue('core', 'installed.bundles', json_encode([])), true); |
|
| 505 | + $bundles[] = $bundle->getIdentifier(); |
|
| 506 | + $this->config->setAppValue('core', 'installed.bundles', json_encode($bundles)); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + /** |
|
| 510 | + * Installs shipped apps |
|
| 511 | + * |
|
| 512 | + * This function installs all apps found in the 'apps' directory that should be enabled by default; |
|
| 513 | + * @param bool $softErrors When updating we ignore errors and simply log them, better to have a |
|
| 514 | + * working ownCloud at the end instead of an aborted update. |
|
| 515 | + * @return array Array of error messages (appid => Exception) |
|
| 516 | + */ |
|
| 517 | + public static function installShippedApps($softErrors = false) { |
|
| 518 | + $errors = []; |
|
| 519 | + foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 520 | + if($dir = opendir( $app_dir['path'] )) { |
|
| 521 | + while( false !== ( $filename = readdir( $dir ))) { |
|
| 522 | + if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 523 | + if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 524 | + if(!Installer::isInstalled($filename)) { |
|
| 525 | + $info=OC_App::getAppInfo($filename); |
|
| 526 | + $enabled = isset($info['default_enable']); |
|
| 527 | + if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
|
| 528 | + && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
|
| 529 | + if ($softErrors) { |
|
| 530 | + try { |
|
| 531 | + Installer::installShippedApp($filename); |
|
| 532 | + } catch (HintException $e) { |
|
| 533 | + if ($e->getPrevious() instanceof TableExistsException) { |
|
| 534 | + $errors[$filename] = $e; |
|
| 535 | + continue; |
|
| 536 | + } |
|
| 537 | + throw $e; |
|
| 538 | + } |
|
| 539 | + } else { |
|
| 540 | + Installer::installShippedApp($filename); |
|
| 541 | + } |
|
| 542 | + \OC::$server->getConfig()->setAppValue($filename, 'enabled', 'yes'); |
|
| 543 | + } |
|
| 544 | + } |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + } |
|
| 548 | + closedir( $dir ); |
|
| 549 | + } |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + return $errors; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * install an app already placed in the app folder |
|
| 557 | + * @param string $app id of the app to install |
|
| 558 | + * @return integer |
|
| 559 | + */ |
|
| 560 | + public static function installShippedApp($app) { |
|
| 561 | + //install the database |
|
| 562 | + $appPath = OC_App::getAppPath($app); |
|
| 563 | + \OC_App::registerAutoloading($app, $appPath); |
|
| 564 | + |
|
| 565 | + if(is_file("$appPath/appinfo/database.xml")) { |
|
| 566 | + try { |
|
| 567 | + OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
|
| 568 | + } catch (TableExistsException $e) { |
|
| 569 | + throw new HintException( |
|
| 570 | + 'Failed to enable app ' . $app, |
|
| 571 | + 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
|
| 572 | + 0, $e |
|
| 573 | + ); |
|
| 574 | + } |
|
| 575 | + } else { |
|
| 576 | + $ms = new \OC\DB\MigrationService($app, \OC::$server->getDatabaseConnection()); |
|
| 577 | + $ms->migrate(); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + //run appinfo/install.php |
|
| 581 | + self::includeAppScript("$appPath/appinfo/install.php"); |
|
| 582 | + |
|
| 583 | + $info = OC_App::getAppInfo($app); |
|
| 584 | + if (is_null($info)) { |
|
| 585 | + return false; |
|
| 586 | + } |
|
| 587 | + \OC_App::setupBackgroundJobs($info['background-jobs']); |
|
| 588 | + |
|
| 589 | + OC_App::executeRepairSteps($app, $info['repair-steps']['install']); |
|
| 590 | + |
|
| 591 | + $config = \OC::$server->getConfig(); |
|
| 592 | + |
|
| 593 | + $config->setAppValue($app, 'installed_version', OC_App::getAppVersion($app)); |
|
| 594 | + if (array_key_exists('ocsid', $info)) { |
|
| 595 | + $config->setAppValue($app, 'ocsid', $info['ocsid']); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + //set remote/public handlers |
|
| 599 | + foreach($info['remote'] as $name=>$path) { |
|
| 600 | + $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
|
| 601 | + } |
|
| 602 | + foreach($info['public'] as $name=>$path) { |
|
| 603 | + $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + OC_App::setAppTypes($info['id']); |
|
| 607 | + |
|
| 608 | + if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 609 | + // requires that autoloading was registered for the app, |
|
| 610 | + // as happens before running the install.php some lines above |
|
| 611 | + \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + return $info['id']; |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * @param string $script |
|
| 619 | + */ |
|
| 620 | + private static function includeAppScript($script) { |
|
| 621 | + if ( file_exists($script) ){ |
|
| 622 | + include $script; |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | 625 | } |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | public function installApp($appId) { |
| 102 | 102 | $app = \OC_App::findAppInDirectories($appId); |
| 103 | - if($app === false) { |
|
| 103 | + if ($app === false) { |
|
| 104 | 104 | throw new \Exception('App not found in any app directory'); |
| 105 | 105 | } |
| 106 | 106 | |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | $l = \OC::$server->getL10N('core'); |
| 111 | 111 | |
| 112 | - if(!is_array($info)) { |
|
| 112 | + if (!is_array($info)) { |
|
| 113 | 113 | throw new \Exception( |
| 114 | 114 | $l->t('App "%s" cannot be installed because appinfo file cannot be read.', |
| 115 | 115 | [$info['name']] |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | \OC_App::registerAutoloading($appId, $basedir); |
| 133 | 133 | |
| 134 | 134 | //install the database |
| 135 | - if(is_file($basedir.'/appinfo/database.xml')) { |
|
| 135 | + if (is_file($basedir.'/appinfo/database.xml')) { |
|
| 136 | 136 | if (\OC::$server->getConfig()->getAppValue($info['id'], 'installed_version') === null) { |
| 137 | 137 | OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); |
| 138 | 138 | } else { |
@@ -144,13 +144,13 @@ discard block |
||
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | \OC_App::setupBackgroundJobs($info['background-jobs']); |
| 147 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 147 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 148 | 148 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | //run appinfo/install.php |
| 152 | - if((!isset($data['noinstall']) or $data['noinstall']==false)) { |
|
| 153 | - self::includeAppScript($basedir . '/appinfo/install.php'); |
|
| 152 | + if ((!isset($data['noinstall']) or $data['noinstall'] == false)) { |
|
| 153 | + self::includeAppScript($basedir.'/appinfo/install.php'); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | $appData = OC_App::getAppInfo($appId); |
@@ -161,10 +161,10 @@ discard block |
||
| 161 | 161 | \OC::$server->getConfig()->setAppValue($info['id'], 'enabled', 'no'); |
| 162 | 162 | |
| 163 | 163 | //set remote/public handlers |
| 164 | - foreach($info['remote'] as $name=>$path) { |
|
| 164 | + foreach ($info['remote'] as $name=>$path) { |
|
| 165 | 165 | \OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); |
| 166 | 166 | } |
| 167 | - foreach($info['public'] as $name=>$path) { |
|
| 167 | + foreach ($info['public'] as $name=>$path) { |
|
| 168 | 168 | \OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); |
| 169 | 169 | } |
| 170 | 170 | |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | * |
| 181 | 181 | * Checks whether or not an app is installed, i.e. registered in apps table. |
| 182 | 182 | */ |
| 183 | - public static function isInstalled( $app ) { |
|
| 183 | + public static function isInstalled($app) { |
|
| 184 | 184 | return (\OC::$server->getConfig()->getAppValue($app, "installed_version", null) !== null); |
| 185 | 185 | } |
| 186 | 186 | |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | * @return bool |
| 192 | 192 | */ |
| 193 | 193 | public function updateAppstoreApp($appId) { |
| 194 | - if($this->isUpdateAvailable($appId)) { |
|
| 194 | + if ($this->isUpdateAvailable($appId)) { |
|
| 195 | 195 | try { |
| 196 | 196 | $this->downloadApp($appId); |
| 197 | 197 | } catch (\Exception $e) { |
@@ -218,18 +218,18 @@ discard block |
||
| 218 | 218 | $appId = strtolower($appId); |
| 219 | 219 | |
| 220 | 220 | $apps = $this->appFetcher->get(); |
| 221 | - foreach($apps as $app) { |
|
| 222 | - if($app['id'] === $appId) { |
|
| 221 | + foreach ($apps as $app) { |
|
| 222 | + if ($app['id'] === $appId) { |
|
| 223 | 223 | // Load the certificate |
| 224 | 224 | $certificate = new X509(); |
| 225 | - $certificate->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 225 | + $certificate->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 226 | 226 | $loadedCertificate = $certificate->loadX509($app['certificate']); |
| 227 | 227 | |
| 228 | 228 | // Verify if the certificate has been revoked |
| 229 | 229 | $crl = new X509(); |
| 230 | - $crl->loadCA(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crt')); |
|
| 231 | - $crl->loadCRL(file_get_contents(__DIR__ . '/../../resources/codesigning/root.crl')); |
|
| 232 | - if($crl->validateSignature() !== true) { |
|
| 230 | + $crl->loadCA(file_get_contents(__DIR__.'/../../resources/codesigning/root.crt')); |
|
| 231 | + $crl->loadCRL(file_get_contents(__DIR__.'/../../resources/codesigning/root.crl')); |
|
| 232 | + if ($crl->validateSignature() !== true) { |
|
| 233 | 233 | throw new \Exception('Could not validate CRL signature'); |
| 234 | 234 | } |
| 235 | 235 | $csn = $loadedCertificate['tbsCertificate']['serialNumber']->toString(); |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Verify if the certificate has been issued by the Nextcloud Code Authority CA |
| 247 | - if($certificate->validateSignature() !== true) { |
|
| 247 | + if ($certificate->validateSignature() !== true) { |
|
| 248 | 248 | throw new \Exception( |
| 249 | 249 | sprintf( |
| 250 | 250 | 'App with id %s has a certificate not issued by a trusted Code Signing Authority', |
@@ -255,7 +255,7 @@ discard block |
||
| 255 | 255 | |
| 256 | 256 | // Verify if the certificate is issued for the requested app id |
| 257 | 257 | $certInfo = openssl_x509_parse($app['certificate']); |
| 258 | - if(!isset($certInfo['subject']['CN'])) { |
|
| 258 | + if (!isset($certInfo['subject']['CN'])) { |
|
| 259 | 259 | throw new \Exception( |
| 260 | 260 | sprintf( |
| 261 | 261 | 'App with id %s has a cert with no CN', |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | ) |
| 264 | 264 | ); |
| 265 | 265 | } |
| 266 | - if($certInfo['subject']['CN'] !== $appId) { |
|
| 266 | + if ($certInfo['subject']['CN'] !== $appId) { |
|
| 267 | 267 | throw new \Exception( |
| 268 | 268 | sprintf( |
| 269 | 269 | 'App with id %s has a cert issued to %s', |
@@ -280,15 +280,15 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | // Check if the signature actually matches the downloaded content |
| 282 | 282 | $certificate = openssl_get_publickey($app['certificate']); |
| 283 | - $verified = (bool)openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 283 | + $verified = (bool) openssl_verify(file_get_contents($tempFile), base64_decode($app['releases'][0]['signature']), $certificate, OPENSSL_ALGO_SHA512); |
|
| 284 | 284 | openssl_free_key($certificate); |
| 285 | 285 | |
| 286 | - if($verified === true) { |
|
| 286 | + if ($verified === true) { |
|
| 287 | 287 | // Seems to match, let's proceed |
| 288 | 288 | $extractDir = $this->tempManager->getTemporaryFolder(); |
| 289 | 289 | $archive = new TAR($tempFile); |
| 290 | 290 | |
| 291 | - if($archive) { |
|
| 291 | + if ($archive) { |
|
| 292 | 292 | if (!$archive->extract($extractDir)) { |
| 293 | 293 | throw new \Exception( |
| 294 | 294 | sprintf( |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | $folders = array_diff($allFiles, ['.', '..']); |
| 302 | 302 | $folders = array_values($folders); |
| 303 | 303 | |
| 304 | - if(count($folders) > 1) { |
|
| 304 | + if (count($folders) > 1) { |
|
| 305 | 305 | throw new \Exception( |
| 306 | 306 | sprintf( |
| 307 | 307 | 'Extracted app %s has more than 1 folder', |
@@ -312,22 +312,22 @@ discard block |
||
| 312 | 312 | |
| 313 | 313 | // Check if appinfo/info.xml has the same app ID as well |
| 314 | 314 | $loadEntities = libxml_disable_entity_loader(false); |
| 315 | - $xml = simplexml_load_file($extractDir . '/' . $folders[0] . '/appinfo/info.xml'); |
|
| 315 | + $xml = simplexml_load_file($extractDir.'/'.$folders[0].'/appinfo/info.xml'); |
|
| 316 | 316 | libxml_disable_entity_loader($loadEntities); |
| 317 | - if((string)$xml->id !== $appId) { |
|
| 317 | + if ((string) $xml->id !== $appId) { |
|
| 318 | 318 | throw new \Exception( |
| 319 | 319 | sprintf( |
| 320 | 320 | 'App for id %s has a wrong app ID in info.xml: %s', |
| 321 | 321 | $appId, |
| 322 | - (string)$xml->id |
|
| 322 | + (string) $xml->id |
|
| 323 | 323 | ) |
| 324 | 324 | ); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | 327 | // Check if the version is lower than before |
| 328 | 328 | $currentVersion = OC_App::getAppVersion($appId); |
| 329 | - $newVersion = (string)$xml->version; |
|
| 330 | - if(version_compare($currentVersion, $newVersion) === 1) { |
|
| 329 | + $newVersion = (string) $xml->version; |
|
| 330 | + if (version_compare($currentVersion, $newVersion) === 1) { |
|
| 331 | 331 | throw new \Exception( |
| 332 | 332 | sprintf( |
| 333 | 333 | 'App for id %s has version %s and tried to update to lower version %s', |
@@ -338,12 +338,12 @@ discard block |
||
| 338 | 338 | ); |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - $baseDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 341 | + $baseDir = OC_App::getInstallPath().'/'.$appId; |
|
| 342 | 342 | // Remove old app with the ID if existent |
| 343 | 343 | OC_Helper::rmdirr($baseDir); |
| 344 | 344 | // Move to app folder |
| 345 | - if(@mkdir($baseDir)) { |
|
| 346 | - $extractDir .= '/' . $folders[0]; |
|
| 345 | + if (@mkdir($baseDir)) { |
|
| 346 | + $extractDir .= '/'.$folders[0]; |
|
| 347 | 347 | OC_Helper::copyr($extractDir, $baseDir); |
| 348 | 348 | } |
| 349 | 349 | OC_Helper::copyr($extractDir, $baseDir); |
@@ -406,8 +406,8 @@ discard block |
||
| 406 | 406 | $this->apps = $this->appFetcher->get(); |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | - foreach($this->apps as $app) { |
|
| 410 | - if($app['id'] === $appId) { |
|
| 409 | + foreach ($this->apps as $app) { |
|
| 410 | + if ($app['id'] === $appId) { |
|
| 411 | 411 | $currentVersion = OC_App::getAppVersion($appId); |
| 412 | 412 | $newestVersion = $app['releases'][0]['version']; |
| 413 | 413 | if (version_compare($newestVersion, $currentVersion, '>')) { |
@@ -430,7 +430,7 @@ discard block |
||
| 430 | 430 | */ |
| 431 | 431 | private function isInstalledFromGit($appId) { |
| 432 | 432 | $app = \OC_App::findAppInDirectories($appId); |
| 433 | - if($app === false) { |
|
| 433 | + if ($app === false) { |
|
| 434 | 434 | return false; |
| 435 | 435 | } |
| 436 | 436 | $basedir = $app['path'].'/'.$appId; |
@@ -445,7 +445,7 @@ discard block |
||
| 445 | 445 | * The function will check if the app is already downloaded in the apps repository |
| 446 | 446 | */ |
| 447 | 447 | public function isDownloaded($name) { |
| 448 | - foreach(\OC::$APPSROOTS as $dir) { |
|
| 448 | + foreach (\OC::$APPSROOTS as $dir) { |
|
| 449 | 449 | $dirToTest = $dir['path']; |
| 450 | 450 | $dirToTest .= '/'; |
| 451 | 451 | $dirToTest .= $name; |
@@ -473,11 +473,11 @@ discard block |
||
| 473 | 473 | * this has to be done by the function oc_app_uninstall(). |
| 474 | 474 | */ |
| 475 | 475 | public function removeApp($appId) { |
| 476 | - if($this->isDownloaded( $appId )) { |
|
| 477 | - $appDir = OC_App::getInstallPath() . '/' . $appId; |
|
| 476 | + if ($this->isDownloaded($appId)) { |
|
| 477 | + $appDir = OC_App::getInstallPath().'/'.$appId; |
|
| 478 | 478 | OC_Helper::rmdirr($appDir); |
| 479 | 479 | return true; |
| 480 | - }else{ |
|
| 480 | + } else { |
|
| 481 | 481 | \OCP\Util::writeLog('core', 'can\'t remove app '.$appId.'. It is not installed.', \OCP\Util::ERROR); |
| 482 | 482 | |
| 483 | 483 | return false; |
@@ -493,8 +493,8 @@ discard block |
||
| 493 | 493 | */ |
| 494 | 494 | public function installAppBundle(Bundle $bundle) { |
| 495 | 495 | $appIds = $bundle->getAppIdentifiers(); |
| 496 | - foreach($appIds as $appId) { |
|
| 497 | - if(!$this->isDownloaded($appId)) { |
|
| 496 | + foreach ($appIds as $appId) { |
|
| 497 | + if (!$this->isDownloaded($appId)) { |
|
| 498 | 498 | $this->downloadApp($appId); |
| 499 | 499 | } |
| 500 | 500 | $this->installApp($appId); |
@@ -516,13 +516,13 @@ discard block |
||
| 516 | 516 | */ |
| 517 | 517 | public static function installShippedApps($softErrors = false) { |
| 518 | 518 | $errors = []; |
| 519 | - foreach(\OC::$APPSROOTS as $app_dir) { |
|
| 520 | - if($dir = opendir( $app_dir['path'] )) { |
|
| 521 | - while( false !== ( $filename = readdir( $dir ))) { |
|
| 522 | - if( $filename[0] !== '.' and is_dir($app_dir['path']."/$filename") ) { |
|
| 523 | - if( file_exists( $app_dir['path']."/$filename/appinfo/info.xml" )) { |
|
| 524 | - if(!Installer::isInstalled($filename)) { |
|
| 525 | - $info=OC_App::getAppInfo($filename); |
|
| 519 | + foreach (\OC::$APPSROOTS as $app_dir) { |
|
| 520 | + if ($dir = opendir($app_dir['path'])) { |
|
| 521 | + while (false !== ($filename = readdir($dir))) { |
|
| 522 | + if ($filename[0] !== '.' and is_dir($app_dir['path']."/$filename")) { |
|
| 523 | + if (file_exists($app_dir['path']."/$filename/appinfo/info.xml")) { |
|
| 524 | + if (!Installer::isInstalled($filename)) { |
|
| 525 | + $info = OC_App::getAppInfo($filename); |
|
| 526 | 526 | $enabled = isset($info['default_enable']); |
| 527 | 527 | if (($enabled || in_array($filename, \OC::$server->getAppManager()->getAlwaysEnabledApps())) |
| 528 | 528 | && \OC::$server->getConfig()->getAppValue($filename, 'enabled') !== 'no') { |
@@ -545,7 +545,7 @@ discard block |
||
| 545 | 545 | } |
| 546 | 546 | } |
| 547 | 547 | } |
| 548 | - closedir( $dir ); |
|
| 548 | + closedir($dir); |
|
| 549 | 549 | } |
| 550 | 550 | } |
| 551 | 551 | |
@@ -562,12 +562,12 @@ discard block |
||
| 562 | 562 | $appPath = OC_App::getAppPath($app); |
| 563 | 563 | \OC_App::registerAutoloading($app, $appPath); |
| 564 | 564 | |
| 565 | - if(is_file("$appPath/appinfo/database.xml")) { |
|
| 565 | + if (is_file("$appPath/appinfo/database.xml")) { |
|
| 566 | 566 | try { |
| 567 | 567 | OC_DB::createDbFromStructure("$appPath/appinfo/database.xml"); |
| 568 | 568 | } catch (TableExistsException $e) { |
| 569 | 569 | throw new HintException( |
| 570 | - 'Failed to enable app ' . $app, |
|
| 570 | + 'Failed to enable app '.$app, |
|
| 571 | 571 | 'Please ask for help via one of our <a href="https://nextcloud.com/support/" target="_blank" rel="noreferrer noopener">support channels</a>.', |
| 572 | 572 | 0, $e |
| 573 | 573 | ); |
@@ -596,16 +596,16 @@ discard block |
||
| 596 | 596 | } |
| 597 | 597 | |
| 598 | 598 | //set remote/public handlers |
| 599 | - foreach($info['remote'] as $name=>$path) { |
|
| 599 | + foreach ($info['remote'] as $name=>$path) { |
|
| 600 | 600 | $config->setAppValue('core', 'remote_'.$name, $app.'/'.$path); |
| 601 | 601 | } |
| 602 | - foreach($info['public'] as $name=>$path) { |
|
| 602 | + foreach ($info['public'] as $name=>$path) { |
|
| 603 | 603 | $config->setAppValue('core', 'public_'.$name, $app.'/'.$path); |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | OC_App::setAppTypes($info['id']); |
| 607 | 607 | |
| 608 | - if(isset($info['settings']) && is_array($info['settings'])) { |
|
| 608 | + if (isset($info['settings']) && is_array($info['settings'])) { |
|
| 609 | 609 | // requires that autoloading was registered for the app, |
| 610 | 610 | // as happens before running the install.php some lines above |
| 611 | 611 | \OC::$server->getSettingsManager()->setupSettings($info['settings']); |
@@ -618,7 +618,7 @@ discard block |
||
| 618 | 618 | * @param string $script |
| 619 | 619 | */ |
| 620 | 620 | private static function includeAppScript($script) { |
| 621 | - if ( file_exists($script) ){ |
|
| 621 | + if (file_exists($script)) { |
|
| 622 | 622 | include $script; |
| 623 | 623 | } |
| 624 | 624 | } |
@@ -39,246 +39,246 @@ |
||
| 39 | 39 | use OCP\Settings\ISettings; |
| 40 | 40 | |
| 41 | 41 | class PersonalInfo implements ISettings { |
| 42 | - /** @var IConfig */ |
|
| 43 | - private $config; |
|
| 44 | - /** @var IUserManager */ |
|
| 45 | - private $userManager; |
|
| 46 | - /** @var AccountManager */ |
|
| 47 | - private $accountManager; |
|
| 48 | - /** @var IGroupManager */ |
|
| 49 | - private $groupManager; |
|
| 50 | - /** @var IAppManager */ |
|
| 51 | - private $appManager; |
|
| 52 | - /** @var IFactory */ |
|
| 53 | - private $l10nFactory; |
|
| 42 | + /** @var IConfig */ |
|
| 43 | + private $config; |
|
| 44 | + /** @var IUserManager */ |
|
| 45 | + private $userManager; |
|
| 46 | + /** @var AccountManager */ |
|
| 47 | + private $accountManager; |
|
| 48 | + /** @var IGroupManager */ |
|
| 49 | + private $groupManager; |
|
| 50 | + /** @var IAppManager */ |
|
| 51 | + private $appManager; |
|
| 52 | + /** @var IFactory */ |
|
| 53 | + private $l10nFactory; |
|
| 54 | 54 | |
| 55 | - const COMMON_LANGUAGE_CODES = [ |
|
| 56 | - 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 57 | - 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 58 | - ]; |
|
| 55 | + const COMMON_LANGUAGE_CODES = [ |
|
| 56 | + 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 57 | + 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 58 | + ]; |
|
| 59 | 59 | |
| 60 | - /** @var IL10N */ |
|
| 61 | - private $l; |
|
| 60 | + /** @var IL10N */ |
|
| 61 | + private $l; |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @param IConfig $config |
|
| 65 | - * @param IUserManager $userManager |
|
| 66 | - * @param IGroupManager $groupManager |
|
| 67 | - * @param AccountManager $accountManager |
|
| 68 | - * @param IFactory $l10nFactory |
|
| 69 | - * @param IL10N $l |
|
| 70 | - */ |
|
| 71 | - public function __construct( |
|
| 72 | - IConfig $config, |
|
| 73 | - IUserManager $userManager, |
|
| 74 | - IGroupManager $groupManager, |
|
| 75 | - AccountManager $accountManager, |
|
| 76 | - IAppManager $appManager, |
|
| 77 | - IFactory $l10nFactory, |
|
| 78 | - IL10N $l |
|
| 79 | - ) { |
|
| 80 | - $this->config = $config; |
|
| 81 | - $this->userManager = $userManager; |
|
| 82 | - $this->accountManager = $accountManager; |
|
| 83 | - $this->groupManager = $groupManager; |
|
| 84 | - $this->appManager = $appManager; |
|
| 85 | - $this->l10nFactory = $l10nFactory; |
|
| 86 | - $this->l = $l; |
|
| 87 | - } |
|
| 63 | + /** |
|
| 64 | + * @param IConfig $config |
|
| 65 | + * @param IUserManager $userManager |
|
| 66 | + * @param IGroupManager $groupManager |
|
| 67 | + * @param AccountManager $accountManager |
|
| 68 | + * @param IFactory $l10nFactory |
|
| 69 | + * @param IL10N $l |
|
| 70 | + */ |
|
| 71 | + public function __construct( |
|
| 72 | + IConfig $config, |
|
| 73 | + IUserManager $userManager, |
|
| 74 | + IGroupManager $groupManager, |
|
| 75 | + AccountManager $accountManager, |
|
| 76 | + IAppManager $appManager, |
|
| 77 | + IFactory $l10nFactory, |
|
| 78 | + IL10N $l |
|
| 79 | + ) { |
|
| 80 | + $this->config = $config; |
|
| 81 | + $this->userManager = $userManager; |
|
| 82 | + $this->accountManager = $accountManager; |
|
| 83 | + $this->groupManager = $groupManager; |
|
| 84 | + $this->appManager = $appManager; |
|
| 85 | + $this->l10nFactory = $l10nFactory; |
|
| 86 | + $this->l = $l; |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @return TemplateResponse returns the instance with all parameters set, ready to be rendered |
|
| 91 | - * @since 9.1 |
|
| 92 | - */ |
|
| 93 | - public function getForm() { |
|
| 94 | - $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing'); |
|
| 95 | - $lookupServerUploadEnabled = false; |
|
| 96 | - if($federatedFileSharingEnabled) { |
|
| 97 | - $federatedFileSharing = new Application(); |
|
| 98 | - $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 99 | - $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled(); |
|
| 100 | - } |
|
| 89 | + /** |
|
| 90 | + * @return TemplateResponse returns the instance with all parameters set, ready to be rendered |
|
| 91 | + * @since 9.1 |
|
| 92 | + */ |
|
| 93 | + public function getForm() { |
|
| 94 | + $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing'); |
|
| 95 | + $lookupServerUploadEnabled = false; |
|
| 96 | + if($federatedFileSharingEnabled) { |
|
| 97 | + $federatedFileSharing = new Application(); |
|
| 98 | + $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
|
| 99 | + $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled(); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - $uid = \OC_User::getUser(); |
|
| 103 | - $user = $this->userManager->get($uid); |
|
| 104 | - $userData = $this->accountManager->getUser($user); |
|
| 102 | + $uid = \OC_User::getUser(); |
|
| 103 | + $user = $this->userManager->get($uid); |
|
| 104 | + $userData = $this->accountManager->getUser($user); |
|
| 105 | 105 | |
| 106 | - $storageInfo = \OC_Helper::getStorageInfo('/'); |
|
| 107 | - if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) { |
|
| 108 | - $totalSpace = $this->l->t('Unlimited'); |
|
| 109 | - } else { |
|
| 110 | - $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
| 111 | - } |
|
| 106 | + $storageInfo = \OC_Helper::getStorageInfo('/'); |
|
| 107 | + if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) { |
|
| 108 | + $totalSpace = $this->l->t('Unlimited'); |
|
| 109 | + } else { |
|
| 110 | + $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - $languageParameters = $this->getLanguages($user); |
|
| 114 | - $messageParameters = $this->getMessageParameters($userData); |
|
| 113 | + $languageParameters = $this->getLanguages($user); |
|
| 114 | + $messageParameters = $this->getMessageParameters($userData); |
|
| 115 | 115 | |
| 116 | - $parameters = [ |
|
| 117 | - 'total_space' => $totalSpace, |
|
| 118 | - 'usage' => \OC_Helper::humanFileSize($storageInfo['used']), |
|
| 119 | - 'usage_relative' => round($storageInfo['relative']), |
|
| 120 | - 'quota' => $storageInfo['quota'], |
|
| 121 | - 'avatarChangeSupported' => $user->canChangeAvatar(), |
|
| 122 | - 'lookupServerUploadEnabled' => $lookupServerUploadEnabled, |
|
| 123 | - 'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'], |
|
| 124 | - 'displayNameChangeSupported' => $user->canChangeDisplayName(), |
|
| 125 | - 'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'], |
|
| 126 | - 'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'], |
|
| 127 | - 'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'], |
|
| 128 | - 'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'], |
|
| 129 | - 'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'], |
|
| 130 | - 'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'], |
|
| 131 | - 'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'], |
|
| 132 | - 'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'], |
|
| 133 | - 'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'], |
|
| 134 | - 'website' => $userData[AccountManager::PROPERTY_WEBSITE]['value'], |
|
| 135 | - 'websiteScope' => $userData[AccountManager::PROPERTY_WEBSITE]['scope'], |
|
| 136 | - 'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'], |
|
| 137 | - 'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'], |
|
| 138 | - 'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'], |
|
| 139 | - 'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'], |
|
| 140 | - 'groups' => $this->getGroups($user), |
|
| 141 | - 'passwordChangeSupported' => $user->canChangePassword(), |
|
| 142 | - ] + $messageParameters + $languageParameters; |
|
| 116 | + $parameters = [ |
|
| 117 | + 'total_space' => $totalSpace, |
|
| 118 | + 'usage' => \OC_Helper::humanFileSize($storageInfo['used']), |
|
| 119 | + 'usage_relative' => round($storageInfo['relative']), |
|
| 120 | + 'quota' => $storageInfo['quota'], |
|
| 121 | + 'avatarChangeSupported' => $user->canChangeAvatar(), |
|
| 122 | + 'lookupServerUploadEnabled' => $lookupServerUploadEnabled, |
|
| 123 | + 'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'], |
|
| 124 | + 'displayNameChangeSupported' => $user->canChangeDisplayName(), |
|
| 125 | + 'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'], |
|
| 126 | + 'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'], |
|
| 127 | + 'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'], |
|
| 128 | + 'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'], |
|
| 129 | + 'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'], |
|
| 130 | + 'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'], |
|
| 131 | + 'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'], |
|
| 132 | + 'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'], |
|
| 133 | + 'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'], |
|
| 134 | + 'website' => $userData[AccountManager::PROPERTY_WEBSITE]['value'], |
|
| 135 | + 'websiteScope' => $userData[AccountManager::PROPERTY_WEBSITE]['scope'], |
|
| 136 | + 'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'], |
|
| 137 | + 'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'], |
|
| 138 | + 'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'], |
|
| 139 | + 'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'], |
|
| 140 | + 'groups' => $this->getGroups($user), |
|
| 141 | + 'passwordChangeSupported' => $user->canChangePassword(), |
|
| 142 | + ] + $messageParameters + $languageParameters; |
|
| 143 | 143 | |
| 144 | 144 | |
| 145 | - return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, ''); |
|
| 146 | - } |
|
| 145 | + return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, ''); |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * @return string the section ID, e.g. 'sharing' |
|
| 150 | - * @since 9.1 |
|
| 151 | - */ |
|
| 152 | - public function getSection() { |
|
| 153 | - return 'personal-info'; |
|
| 154 | - } |
|
| 148 | + /** |
|
| 149 | + * @return string the section ID, e.g. 'sharing' |
|
| 150 | + * @since 9.1 |
|
| 151 | + */ |
|
| 152 | + public function getSection() { |
|
| 153 | + return 'personal-info'; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @return int whether the form should be rather on the top or bottom of |
|
| 158 | - * the admin section. The forms are arranged in ascending order of the |
|
| 159 | - * priority values. It is required to return a value between 0 and 100. |
|
| 160 | - * |
|
| 161 | - * E.g.: 70 |
|
| 162 | - * @since 9.1 |
|
| 163 | - */ |
|
| 164 | - public function getPriority() { |
|
| 165 | - return 10; |
|
| 166 | - } |
|
| 156 | + /** |
|
| 157 | + * @return int whether the form should be rather on the top or bottom of |
|
| 158 | + * the admin section. The forms are arranged in ascending order of the |
|
| 159 | + * priority values. It is required to return a value between 0 and 100. |
|
| 160 | + * |
|
| 161 | + * E.g.: 70 |
|
| 162 | + * @since 9.1 |
|
| 163 | + */ |
|
| 164 | + public function getPriority() { |
|
| 165 | + return 10; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * returns a sorted list of the user's group GIDs |
|
| 170 | - * |
|
| 171 | - * @param IUser $user |
|
| 172 | - * @return array |
|
| 173 | - */ |
|
| 174 | - private function getGroups(IUser $user) { |
|
| 175 | - $groups = array_map( |
|
| 176 | - function(IGroup $group) { |
|
| 177 | - return $group->getGID(); |
|
| 178 | - }, |
|
| 179 | - $this->groupManager->getUserGroups($user) |
|
| 180 | - ); |
|
| 181 | - sort($groups); |
|
| 168 | + /** |
|
| 169 | + * returns a sorted list of the user's group GIDs |
|
| 170 | + * |
|
| 171 | + * @param IUser $user |
|
| 172 | + * @return array |
|
| 173 | + */ |
|
| 174 | + private function getGroups(IUser $user) { |
|
| 175 | + $groups = array_map( |
|
| 176 | + function(IGroup $group) { |
|
| 177 | + return $group->getGID(); |
|
| 178 | + }, |
|
| 179 | + $this->groupManager->getUserGroups($user) |
|
| 180 | + ); |
|
| 181 | + sort($groups); |
|
| 182 | 182 | |
| 183 | - return $groups; |
|
| 184 | - } |
|
| 183 | + return $groups; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * returns the user language, common language and other languages in an |
|
| 188 | - * associative array |
|
| 189 | - * |
|
| 190 | - * @param IUser $user |
|
| 191 | - * @return array |
|
| 192 | - */ |
|
| 193 | - private function getLanguages(IUser $user) { |
|
| 194 | - $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 195 | - if($forceLanguage !== false) { |
|
| 196 | - return []; |
|
| 197 | - } |
|
| 186 | + /** |
|
| 187 | + * returns the user language, common language and other languages in an |
|
| 188 | + * associative array |
|
| 189 | + * |
|
| 190 | + * @param IUser $user |
|
| 191 | + * @return array |
|
| 192 | + */ |
|
| 193 | + private function getLanguages(IUser $user) { |
|
| 194 | + $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 195 | + if($forceLanguage !== false) { |
|
| 196 | + return []; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - $uid = $user->getUID(); |
|
| 199 | + $uid = $user->getUID(); |
|
| 200 | 200 | |
| 201 | - $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage()); |
|
| 202 | - $languageCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 201 | + $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage()); |
|
| 202 | + $languageCodes = $this->l10nFactory->findAvailableLanguages(); |
|
| 203 | 203 | |
| 204 | - $commonLanguages = []; |
|
| 205 | - $languages = []; |
|
| 204 | + $commonLanguages = []; |
|
| 205 | + $languages = []; |
|
| 206 | 206 | |
| 207 | - foreach($languageCodes as $lang) { |
|
| 208 | - $l = \OC::$server->getL10N('settings', $lang); |
|
| 209 | - // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 210 | - $potentialName = (string) $l->t('__language_name__'); |
|
| 211 | - if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 212 | - $ln = array('code' => $lang, 'name' => $potentialName); |
|
| 213 | - } elseif ($lang === 'en') { |
|
| 214 | - $ln = ['code' => $lang, 'name' => 'English (US)']; |
|
| 215 | - }else{//fallback to language code |
|
| 216 | - $ln=array('code'=>$lang, 'name'=>$lang); |
|
| 217 | - } |
|
| 207 | + foreach($languageCodes as $lang) { |
|
| 208 | + $l = \OC::$server->getL10N('settings', $lang); |
|
| 209 | + // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 210 | + $potentialName = (string) $l->t('__language_name__'); |
|
| 211 | + if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 212 | + $ln = array('code' => $lang, 'name' => $potentialName); |
|
| 213 | + } elseif ($lang === 'en') { |
|
| 214 | + $ln = ['code' => $lang, 'name' => 'English (US)']; |
|
| 215 | + }else{//fallback to language code |
|
| 216 | + $ln=array('code'=>$lang, 'name'=>$lang); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - // put appropriate languages into appropriate arrays, to print them sorted |
|
| 220 | - // used language -> common languages -> divider -> other languages |
|
| 221 | - if ($lang === $userLang) { |
|
| 222 | - $userLang = $ln; |
|
| 223 | - } elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 224 | - $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln; |
|
| 225 | - } else { |
|
| 226 | - $languages[]=$ln; |
|
| 227 | - } |
|
| 228 | - } |
|
| 219 | + // put appropriate languages into appropriate arrays, to print them sorted |
|
| 220 | + // used language -> common languages -> divider -> other languages |
|
| 221 | + if ($lang === $userLang) { |
|
| 222 | + $userLang = $ln; |
|
| 223 | + } elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 224 | + $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln; |
|
| 225 | + } else { |
|
| 226 | + $languages[]=$ln; |
|
| 227 | + } |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - // if user language is not available but set somehow: show the actual code as name |
|
| 231 | - if (!is_array($userLang)) { |
|
| 232 | - $userLang = [ |
|
| 233 | - 'code' => $userLang, |
|
| 234 | - 'name' => $userLang, |
|
| 235 | - ]; |
|
| 236 | - } |
|
| 230 | + // if user language is not available but set somehow: show the actual code as name |
|
| 231 | + if (!is_array($userLang)) { |
|
| 232 | + $userLang = [ |
|
| 233 | + 'code' => $userLang, |
|
| 234 | + 'name' => $userLang, |
|
| 235 | + ]; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - ksort($commonLanguages); |
|
| 238 | + ksort($commonLanguages); |
|
| 239 | 239 | |
| 240 | - // sort now by displayed language not the iso-code |
|
| 241 | - usort( $languages, function ($a, $b) { |
|
| 242 | - if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 243 | - // If a doesn't have a name, but b does, list b before a |
|
| 244 | - return 1; |
|
| 245 | - } |
|
| 246 | - if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 247 | - // If a does have a name, but b doesn't, list a before b |
|
| 248 | - return -1; |
|
| 249 | - } |
|
| 250 | - // Otherwise compare the names |
|
| 251 | - return strcmp($a['name'], $b['name']); |
|
| 252 | - }); |
|
| 240 | + // sort now by displayed language not the iso-code |
|
| 241 | + usort( $languages, function ($a, $b) { |
|
| 242 | + if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 243 | + // If a doesn't have a name, but b does, list b before a |
|
| 244 | + return 1; |
|
| 245 | + } |
|
| 246 | + if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 247 | + // If a does have a name, but b doesn't, list a before b |
|
| 248 | + return -1; |
|
| 249 | + } |
|
| 250 | + // Otherwise compare the names |
|
| 251 | + return strcmp($a['name'], $b['name']); |
|
| 252 | + }); |
|
| 253 | 253 | |
| 254 | - return [ |
|
| 255 | - 'activelanguage' => $userLang, |
|
| 256 | - 'commonlanguages' => $commonLanguages, |
|
| 257 | - 'languages' => $languages |
|
| 258 | - ]; |
|
| 259 | - } |
|
| 254 | + return [ |
|
| 255 | + 'activelanguage' => $userLang, |
|
| 256 | + 'commonlanguages' => $commonLanguages, |
|
| 257 | + 'languages' => $languages |
|
| 258 | + ]; |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * @param array $userData |
|
| 263 | - * @return array |
|
| 264 | - */ |
|
| 265 | - private function getMessageParameters(array $userData) { |
|
| 266 | - $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER]; |
|
| 267 | - $messageParameters = []; |
|
| 268 | - foreach ($needVerifyMessage as $property) { |
|
| 269 | - switch ($userData[$property]['verified']) { |
|
| 270 | - case AccountManager::VERIFIED: |
|
| 271 | - $message = $this->l->t('Verifying'); |
|
| 272 | - break; |
|
| 273 | - case AccountManager::VERIFICATION_IN_PROGRESS: |
|
| 274 | - $message = $this->l->t('Verifying …'); |
|
| 275 | - break; |
|
| 276 | - default: |
|
| 277 | - $message = $this->l->t('Verify'); |
|
| 278 | - } |
|
| 279 | - $messageParameters[$property . 'Message'] = $message; |
|
| 280 | - } |
|
| 281 | - return $messageParameters; |
|
| 282 | - } |
|
| 261 | + /** |
|
| 262 | + * @param array $userData |
|
| 263 | + * @return array |
|
| 264 | + */ |
|
| 265 | + private function getMessageParameters(array $userData) { |
|
| 266 | + $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER]; |
|
| 267 | + $messageParameters = []; |
|
| 268 | + foreach ($needVerifyMessage as $property) { |
|
| 269 | + switch ($userData[$property]['verified']) { |
|
| 270 | + case AccountManager::VERIFIED: |
|
| 271 | + $message = $this->l->t('Verifying'); |
|
| 272 | + break; |
|
| 273 | + case AccountManager::VERIFICATION_IN_PROGRESS: |
|
| 274 | + $message = $this->l->t('Verifying …'); |
|
| 275 | + break; |
|
| 276 | + default: |
|
| 277 | + $message = $this->l->t('Verify'); |
|
| 278 | + } |
|
| 279 | + $messageParameters[$property . 'Message'] = $message; |
|
| 280 | + } |
|
| 281 | + return $messageParameters; |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | 284 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | public function getForm() { |
| 94 | 94 | $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing'); |
| 95 | 95 | $lookupServerUploadEnabled = false; |
| 96 | - if($federatedFileSharingEnabled) { |
|
| 96 | + if ($federatedFileSharingEnabled) { |
|
| 97 | 97 | $federatedFileSharing = new Application(); |
| 98 | 98 | $shareProvider = $federatedFileSharing->getFederatedShareProvider(); |
| 99 | 99 | $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled(); |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | */ |
| 193 | 193 | private function getLanguages(IUser $user) { |
| 194 | 194 | $forceLanguage = $this->config->getSystemValue('force_language', false); |
| 195 | - if($forceLanguage !== false) { |
|
| 195 | + if ($forceLanguage !== false) { |
|
| 196 | 196 | return []; |
| 197 | 197 | } |
| 198 | 198 | |
@@ -204,16 +204,16 @@ discard block |
||
| 204 | 204 | $commonLanguages = []; |
| 205 | 205 | $languages = []; |
| 206 | 206 | |
| 207 | - foreach($languageCodes as $lang) { |
|
| 207 | + foreach ($languageCodes as $lang) { |
|
| 208 | 208 | $l = \OC::$server->getL10N('settings', $lang); |
| 209 | 209 | // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
| 210 | 210 | $potentialName = (string) $l->t('__language_name__'); |
| 211 | - if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 211 | + if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 212 | 212 | $ln = array('code' => $lang, 'name' => $potentialName); |
| 213 | 213 | } elseif ($lang === 'en') { |
| 214 | 214 | $ln = ['code' => $lang, 'name' => 'English (US)']; |
| 215 | - }else{//fallback to language code |
|
| 216 | - $ln=array('code'=>$lang, 'name'=>$lang); |
|
| 215 | + } else {//fallback to language code |
|
| 216 | + $ln = array('code'=>$lang, 'name'=>$lang); |
|
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | // put appropriate languages into appropriate arrays, to print them sorted |
@@ -221,9 +221,9 @@ discard block |
||
| 221 | 221 | if ($lang === $userLang) { |
| 222 | 222 | $userLang = $ln; |
| 223 | 223 | } elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
| 224 | - $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln; |
|
| 224 | + $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
| 225 | 225 | } else { |
| 226 | - $languages[]=$ln; |
|
| 226 | + $languages[] = $ln; |
|
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | ksort($commonLanguages); |
| 239 | 239 | |
| 240 | 240 | // sort now by displayed language not the iso-code |
| 241 | - usort( $languages, function ($a, $b) { |
|
| 241 | + usort($languages, function($a, $b) { |
|
| 242 | 242 | if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
| 243 | 243 | // If a doesn't have a name, but b does, list b before a |
| 244 | 244 | return 1; |
@@ -276,7 +276,7 @@ discard block |
||
| 276 | 276 | default: |
| 277 | 277 | $message = $this->l->t('Verify'); |
| 278 | 278 | } |
| 279 | - $messageParameters[$property . 'Message'] = $message; |
|
| 279 | + $messageParameters[$property.'Message'] = $message; |
|
| 280 | 280 | } |
| 281 | 281 | return $messageParameters; |
| 282 | 282 | } |
@@ -69,857 +69,857 @@ |
||
| 69 | 69 | |
| 70 | 70 | class Filesystem { |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * @var Mount\Manager $mounts |
|
| 74 | - */ |
|
| 75 | - private static $mounts; |
|
| 76 | - |
|
| 77 | - public static $loaded = false; |
|
| 78 | - /** |
|
| 79 | - * @var \OC\Files\View $defaultInstance |
|
| 80 | - */ |
|
| 81 | - static private $defaultInstance; |
|
| 82 | - |
|
| 83 | - static private $usersSetup = array(); |
|
| 84 | - |
|
| 85 | - static private $normalizedPathCache = null; |
|
| 86 | - |
|
| 87 | - static private $listeningForProviders = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * classname which used for hooks handling |
|
| 91 | - * used as signalclass in OC_Hooks::emit() |
|
| 92 | - */ |
|
| 93 | - const CLASSNAME = 'OC_Filesystem'; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * signalname emitted before file renaming |
|
| 97 | - * |
|
| 98 | - * @param string $oldpath |
|
| 99 | - * @param string $newpath |
|
| 100 | - */ |
|
| 101 | - const signal_rename = 'rename'; |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * signal emitted after file renaming |
|
| 105 | - * |
|
| 106 | - * @param string $oldpath |
|
| 107 | - * @param string $newpath |
|
| 108 | - */ |
|
| 109 | - const signal_post_rename = 'post_rename'; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * signal emitted before file/dir creation |
|
| 113 | - * |
|
| 114 | - * @param string $path |
|
| 115 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 116 | - */ |
|
| 117 | - const signal_create = 'create'; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * signal emitted after file/dir creation |
|
| 121 | - * |
|
| 122 | - * @param string $path |
|
| 123 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 124 | - */ |
|
| 125 | - const signal_post_create = 'post_create'; |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * signal emits before file/dir copy |
|
| 129 | - * |
|
| 130 | - * @param string $oldpath |
|
| 131 | - * @param string $newpath |
|
| 132 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 133 | - */ |
|
| 134 | - const signal_copy = 'copy'; |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * signal emits after file/dir copy |
|
| 138 | - * |
|
| 139 | - * @param string $oldpath |
|
| 140 | - * @param string $newpath |
|
| 141 | - */ |
|
| 142 | - const signal_post_copy = 'post_copy'; |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * signal emits before file/dir save |
|
| 146 | - * |
|
| 147 | - * @param string $path |
|
| 148 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 149 | - */ |
|
| 150 | - const signal_write = 'write'; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * signal emits after file/dir save |
|
| 154 | - * |
|
| 155 | - * @param string $path |
|
| 156 | - */ |
|
| 157 | - const signal_post_write = 'post_write'; |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * signal emitted before file/dir update |
|
| 161 | - * |
|
| 162 | - * @param string $path |
|
| 163 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 164 | - */ |
|
| 165 | - const signal_update = 'update'; |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * signal emitted after file/dir update |
|
| 169 | - * |
|
| 170 | - * @param string $path |
|
| 171 | - * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 172 | - */ |
|
| 173 | - const signal_post_update = 'post_update'; |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * signal emits when reading file/dir |
|
| 177 | - * |
|
| 178 | - * @param string $path |
|
| 179 | - */ |
|
| 180 | - const signal_read = 'read'; |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * signal emits when removing file/dir |
|
| 184 | - * |
|
| 185 | - * @param string $path |
|
| 186 | - */ |
|
| 187 | - const signal_delete = 'delete'; |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * parameters definitions for signals |
|
| 191 | - */ |
|
| 192 | - const signal_param_path = 'path'; |
|
| 193 | - const signal_param_oldpath = 'oldpath'; |
|
| 194 | - const signal_param_newpath = 'newpath'; |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * run - changing this flag to false in hook handler will cancel event |
|
| 198 | - */ |
|
| 199 | - const signal_param_run = 'run'; |
|
| 200 | - |
|
| 201 | - const signal_create_mount = 'create_mount'; |
|
| 202 | - const signal_delete_mount = 'delete_mount'; |
|
| 203 | - const signal_param_mount_type = 'mounttype'; |
|
| 204 | - const signal_param_users = 'users'; |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
| 208 | - */ |
|
| 209 | - private static $loader; |
|
| 210 | - |
|
| 211 | - /** @var bool */ |
|
| 212 | - private static $logWarningWhenAddingStorageWrapper = true; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @param bool $shouldLog |
|
| 216 | - * @return bool previous value |
|
| 217 | - * @internal |
|
| 218 | - */ |
|
| 219 | - public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 220 | - $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 221 | - self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 222 | - return $previousValue; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @param string $wrapperName |
|
| 227 | - * @param callable $wrapper |
|
| 228 | - * @param int $priority |
|
| 229 | - */ |
|
| 230 | - public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 231 | - if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 232 | - \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 233 | - 'wrapper' => $wrapperName, |
|
| 234 | - 'app' => 'filesystem', |
|
| 235 | - ]); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $mounts = self::getMountManager()->getAll(); |
|
| 239 | - if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 240 | - // do not re-wrap if storage with this name already existed |
|
| 241 | - return; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Returns the storage factory |
|
| 247 | - * |
|
| 248 | - * @return \OCP\Files\Storage\IStorageFactory |
|
| 249 | - */ |
|
| 250 | - public static function getLoader() { |
|
| 251 | - if (!self::$loader) { |
|
| 252 | - self::$loader = new StorageFactory(); |
|
| 253 | - } |
|
| 254 | - return self::$loader; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Returns the mount manager |
|
| 259 | - * |
|
| 260 | - * @return \OC\Files\Mount\Manager |
|
| 261 | - */ |
|
| 262 | - public static function getMountManager($user = '') { |
|
| 263 | - if (!self::$mounts) { |
|
| 264 | - \OC_Util::setupFS($user); |
|
| 265 | - } |
|
| 266 | - return self::$mounts; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * get the mountpoint of the storage object for a path |
|
| 271 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 272 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 273 | - * and doesn't take the chroot into account ) |
|
| 274 | - * |
|
| 275 | - * @param string $path |
|
| 276 | - * @return string |
|
| 277 | - */ |
|
| 278 | - static public function getMountPoint($path) { |
|
| 279 | - if (!self::$mounts) { |
|
| 280 | - \OC_Util::setupFS(); |
|
| 281 | - } |
|
| 282 | - $mount = self::$mounts->find($path); |
|
| 283 | - if ($mount) { |
|
| 284 | - return $mount->getMountPoint(); |
|
| 285 | - } else { |
|
| 286 | - return ''; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * get a list of all mount points in a directory |
|
| 292 | - * |
|
| 293 | - * @param string $path |
|
| 294 | - * @return string[] |
|
| 295 | - */ |
|
| 296 | - static public function getMountPoints($path) { |
|
| 297 | - if (!self::$mounts) { |
|
| 298 | - \OC_Util::setupFS(); |
|
| 299 | - } |
|
| 300 | - $result = array(); |
|
| 301 | - $mounts = self::$mounts->findIn($path); |
|
| 302 | - foreach ($mounts as $mount) { |
|
| 303 | - $result[] = $mount->getMountPoint(); |
|
| 304 | - } |
|
| 305 | - return $result; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * get the storage mounted at $mountPoint |
|
| 310 | - * |
|
| 311 | - * @param string $mountPoint |
|
| 312 | - * @return \OC\Files\Storage\Storage |
|
| 313 | - */ |
|
| 314 | - public static function getStorage($mountPoint) { |
|
| 315 | - if (!self::$mounts) { |
|
| 316 | - \OC_Util::setupFS(); |
|
| 317 | - } |
|
| 318 | - $mount = self::$mounts->find($mountPoint); |
|
| 319 | - return $mount->getStorage(); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @param string $id |
|
| 324 | - * @return Mount\MountPoint[] |
|
| 325 | - */ |
|
| 326 | - public static function getMountByStorageId($id) { |
|
| 327 | - if (!self::$mounts) { |
|
| 328 | - \OC_Util::setupFS(); |
|
| 329 | - } |
|
| 330 | - return self::$mounts->findByStorageId($id); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @param int $id |
|
| 335 | - * @return Mount\MountPoint[] |
|
| 336 | - */ |
|
| 337 | - public static function getMountByNumericId($id) { |
|
| 338 | - if (!self::$mounts) { |
|
| 339 | - \OC_Util::setupFS(); |
|
| 340 | - } |
|
| 341 | - return self::$mounts->findByNumericId($id); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * resolve a path to a storage and internal path |
|
| 346 | - * |
|
| 347 | - * @param string $path |
|
| 348 | - * @return array an array consisting of the storage and the internal path |
|
| 349 | - */ |
|
| 350 | - static public function resolvePath($path) { |
|
| 351 | - if (!self::$mounts) { |
|
| 352 | - \OC_Util::setupFS(); |
|
| 353 | - } |
|
| 354 | - $mount = self::$mounts->find($path); |
|
| 355 | - if ($mount) { |
|
| 356 | - return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 357 | - } else { |
|
| 358 | - return array(null, null); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - static public function init($user, $root) { |
|
| 363 | - if (self::$defaultInstance) { |
|
| 364 | - return false; |
|
| 365 | - } |
|
| 366 | - self::getLoader(); |
|
| 367 | - self::$defaultInstance = new View($root); |
|
| 368 | - |
|
| 369 | - if (!self::$mounts) { |
|
| 370 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - //load custom mount config |
|
| 374 | - self::initMountPoints($user); |
|
| 375 | - |
|
| 376 | - self::$loaded = true; |
|
| 377 | - |
|
| 378 | - return true; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - static public function initMountManager() { |
|
| 382 | - if (!self::$mounts) { |
|
| 383 | - self::$mounts = \OC::$server->getMountManager(); |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Initialize system and personal mount points for a user |
|
| 389 | - * |
|
| 390 | - * @param string $user |
|
| 391 | - * @throws \OC\User\NoUserException if the user is not available |
|
| 392 | - */ |
|
| 393 | - public static function initMountPoints($user = '') { |
|
| 394 | - if ($user == '') { |
|
| 395 | - $user = \OC_User::getUser(); |
|
| 396 | - } |
|
| 397 | - if ($user === null || $user === false || $user === '') { |
|
| 398 | - throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - if (isset(self::$usersSetup[$user])) { |
|
| 402 | - return; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - self::$usersSetup[$user] = true; |
|
| 406 | - |
|
| 407 | - $userManager = \OC::$server->getUserManager(); |
|
| 408 | - $userObject = $userManager->get($user); |
|
| 409 | - |
|
| 410 | - if (is_null($userObject)) { |
|
| 411 | - \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR); |
|
| 412 | - // reset flag, this will make it possible to rethrow the exception if called again |
|
| 413 | - unset(self::$usersSetup[$user]); |
|
| 414 | - throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - $realUid = $userObject->getUID(); |
|
| 418 | - // workaround in case of different casings |
|
| 419 | - if ($user !== $realUid) { |
|
| 420 | - $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 421 | - \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN); |
|
| 422 | - $user = $realUid; |
|
| 423 | - |
|
| 424 | - // again with the correct casing |
|
| 425 | - if (isset(self::$usersSetup[$user])) { |
|
| 426 | - return; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - self::$usersSetup[$user] = true; |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 433 | - /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 434 | - $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 435 | - |
|
| 436 | - // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 437 | - $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 438 | - |
|
| 439 | - self::getMountManager()->addMount($homeMount); |
|
| 440 | - |
|
| 441 | - \OC\Files\Filesystem::getStorage($user); |
|
| 442 | - |
|
| 443 | - // Chance to mount for other storages |
|
| 444 | - if ($userObject) { |
|
| 445 | - $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 446 | - $mounts[] = $homeMount; |
|
| 447 | - $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 451 | - } else { |
|
| 452 | - self::getMountManager()->addMount(new MountPoint( |
|
| 453 | - new NullStorage([]), |
|
| 454 | - '/' . $user |
|
| 455 | - )); |
|
| 456 | - self::getMountManager()->addMount(new MountPoint( |
|
| 457 | - new NullStorage([]), |
|
| 458 | - '/' . $user . '/files' |
|
| 459 | - )); |
|
| 460 | - } |
|
| 461 | - \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * Get mounts from mount providers that are registered after setup |
|
| 466 | - * |
|
| 467 | - * @param MountProviderCollection $mountConfigManager |
|
| 468 | - * @param IUserManager $userManager |
|
| 469 | - */ |
|
| 470 | - private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 471 | - if (!self::$listeningForProviders) { |
|
| 472 | - self::$listeningForProviders = true; |
|
| 473 | - $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 474 | - foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 475 | - $userObject = $userManager->get($user); |
|
| 476 | - if ($userObject) { |
|
| 477 | - $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 478 | - array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - }); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - /** |
|
| 486 | - * get the default filesystem view |
|
| 487 | - * |
|
| 488 | - * @return View |
|
| 489 | - */ |
|
| 490 | - static public function getView() { |
|
| 491 | - return self::$defaultInstance; |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * tear down the filesystem, removing all storage providers |
|
| 496 | - */ |
|
| 497 | - static public function tearDown() { |
|
| 498 | - self::clearMounts(); |
|
| 499 | - self::$defaultInstance = null; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * get the relative path of the root data directory for the current user |
|
| 504 | - * |
|
| 505 | - * @return string |
|
| 506 | - * |
|
| 507 | - * Returns path like /admin/files |
|
| 508 | - */ |
|
| 509 | - static public function getRoot() { |
|
| 510 | - if (!self::$defaultInstance) { |
|
| 511 | - return null; |
|
| 512 | - } |
|
| 513 | - return self::$defaultInstance->getRoot(); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * clear all mounts and storage backends |
|
| 518 | - */ |
|
| 519 | - public static function clearMounts() { |
|
| 520 | - if (self::$mounts) { |
|
| 521 | - self::$usersSetup = array(); |
|
| 522 | - self::$mounts->clear(); |
|
| 523 | - } |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 528 | - * |
|
| 529 | - * @param \OC\Files\Storage\Storage|string $class |
|
| 530 | - * @param array $arguments |
|
| 531 | - * @param string $mountpoint |
|
| 532 | - */ |
|
| 533 | - static public function mount($class, $arguments, $mountpoint) { |
|
| 534 | - if (!self::$mounts) { |
|
| 535 | - \OC_Util::setupFS(); |
|
| 536 | - } |
|
| 537 | - $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 538 | - self::$mounts->addMount($mount); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - /** |
|
| 542 | - * return the path to a local version of the file |
|
| 543 | - * we need this because we can't know if a file is stored local or not from |
|
| 544 | - * outside the filestorage and for some purposes a local file is needed |
|
| 545 | - * |
|
| 546 | - * @param string $path |
|
| 547 | - * @return string |
|
| 548 | - */ |
|
| 549 | - static public function getLocalFile($path) { |
|
| 550 | - return self::$defaultInstance->getLocalFile($path); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - /** |
|
| 554 | - * @param string $path |
|
| 555 | - * @return string |
|
| 556 | - */ |
|
| 557 | - static public function getLocalFolder($path) { |
|
| 558 | - return self::$defaultInstance->getLocalFolder($path); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * return path to file which reflects one visible in browser |
|
| 563 | - * |
|
| 564 | - * @param string $path |
|
| 565 | - * @return string |
|
| 566 | - */ |
|
| 567 | - static public function getLocalPath($path) { |
|
| 568 | - $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 569 | - $newpath = $path; |
|
| 570 | - if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 571 | - $newpath = substr($path, strlen($datadir)); |
|
| 572 | - } |
|
| 573 | - return $newpath; |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * check if the requested path is valid |
|
| 578 | - * |
|
| 579 | - * @param string $path |
|
| 580 | - * @return bool |
|
| 581 | - */ |
|
| 582 | - static public function isValidPath($path) { |
|
| 583 | - $path = self::normalizePath($path); |
|
| 584 | - if (!$path || $path[0] !== '/') { |
|
| 585 | - $path = '/' . $path; |
|
| 586 | - } |
|
| 587 | - if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 588 | - return false; |
|
| 589 | - } |
|
| 590 | - return true; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * checks if a file is blacklisted for storage in the filesystem |
|
| 595 | - * Listens to write and rename hooks |
|
| 596 | - * |
|
| 597 | - * @param array $data from hook |
|
| 598 | - */ |
|
| 599 | - static public function isBlacklisted($data) { |
|
| 600 | - if (isset($data['path'])) { |
|
| 601 | - $path = $data['path']; |
|
| 602 | - } else if (isset($data['newpath'])) { |
|
| 603 | - $path = $data['newpath']; |
|
| 604 | - } |
|
| 605 | - if (isset($path)) { |
|
| 606 | - if (self::isFileBlacklisted($path)) { |
|
| 607 | - $data['run'] = false; |
|
| 608 | - } |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - /** |
|
| 613 | - * @param string $filename |
|
| 614 | - * @return bool |
|
| 615 | - */ |
|
| 616 | - static public function isFileBlacklisted($filename) { |
|
| 617 | - $filename = self::normalizePath($filename); |
|
| 618 | - |
|
| 619 | - $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 620 | - $filename = strtolower(basename($filename)); |
|
| 621 | - return in_array($filename, $blacklist); |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * check if the directory should be ignored when scanning |
|
| 626 | - * NOTE: the special directories . and .. would cause never ending recursion |
|
| 627 | - * |
|
| 628 | - * @param String $dir |
|
| 629 | - * @return boolean |
|
| 630 | - */ |
|
| 631 | - static public function isIgnoredDir($dir) { |
|
| 632 | - if ($dir === '.' || $dir === '..') { |
|
| 633 | - return true; |
|
| 634 | - } |
|
| 635 | - return false; |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 640 | - */ |
|
| 641 | - static public function mkdir($path) { |
|
| 642 | - return self::$defaultInstance->mkdir($path); |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - static public function rmdir($path) { |
|
| 646 | - return self::$defaultInstance->rmdir($path); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - static public function is_dir($path) { |
|
| 650 | - return self::$defaultInstance->is_dir($path); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - static public function is_file($path) { |
|
| 654 | - return self::$defaultInstance->is_file($path); |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - static public function stat($path) { |
|
| 658 | - return self::$defaultInstance->stat($path); |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - static public function filetype($path) { |
|
| 662 | - return self::$defaultInstance->filetype($path); |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - static public function filesize($path) { |
|
| 666 | - return self::$defaultInstance->filesize($path); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - static public function readfile($path) { |
|
| 670 | - return self::$defaultInstance->readfile($path); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - static public function isCreatable($path) { |
|
| 674 | - return self::$defaultInstance->isCreatable($path); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - static public function isReadable($path) { |
|
| 678 | - return self::$defaultInstance->isReadable($path); |
|
| 679 | - } |
|
| 680 | - |
|
| 681 | - static public function isUpdatable($path) { |
|
| 682 | - return self::$defaultInstance->isUpdatable($path); |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - static public function isDeletable($path) { |
|
| 686 | - return self::$defaultInstance->isDeletable($path); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - static public function isSharable($path) { |
|
| 690 | - return self::$defaultInstance->isSharable($path); |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - static public function file_exists($path) { |
|
| 694 | - return self::$defaultInstance->file_exists($path); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - static public function filemtime($path) { |
|
| 698 | - return self::$defaultInstance->filemtime($path); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - static public function touch($path, $mtime = null) { |
|
| 702 | - return self::$defaultInstance->touch($path, $mtime); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * @return string |
|
| 707 | - */ |
|
| 708 | - static public function file_get_contents($path) { |
|
| 709 | - return self::$defaultInstance->file_get_contents($path); |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - static public function file_put_contents($path, $data) { |
|
| 713 | - return self::$defaultInstance->file_put_contents($path, $data); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - static public function unlink($path) { |
|
| 717 | - return self::$defaultInstance->unlink($path); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - static public function rename($path1, $path2) { |
|
| 721 | - return self::$defaultInstance->rename($path1, $path2); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - static public function copy($path1, $path2) { |
|
| 725 | - return self::$defaultInstance->copy($path1, $path2); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - static public function fopen($path, $mode) { |
|
| 729 | - return self::$defaultInstance->fopen($path, $mode); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * @return string |
|
| 734 | - */ |
|
| 735 | - static public function toTmpFile($path) { |
|
| 736 | - return self::$defaultInstance->toTmpFile($path); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - static public function fromTmpFile($tmpFile, $path) { |
|
| 740 | - return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - static public function getMimeType($path) { |
|
| 744 | - return self::$defaultInstance->getMimeType($path); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - static public function hash($type, $path, $raw = false) { |
|
| 748 | - return self::$defaultInstance->hash($type, $path, $raw); |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - static public function free_space($path = '/') { |
|
| 752 | - return self::$defaultInstance->free_space($path); |
|
| 753 | - } |
|
| 754 | - |
|
| 755 | - static public function search($query) { |
|
| 756 | - return self::$defaultInstance->search($query); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - /** |
|
| 760 | - * @param string $query |
|
| 761 | - */ |
|
| 762 | - static public function searchByMime($query) { |
|
| 763 | - return self::$defaultInstance->searchByMime($query); |
|
| 764 | - } |
|
| 765 | - |
|
| 766 | - /** |
|
| 767 | - * @param string|int $tag name or tag id |
|
| 768 | - * @param string $userId owner of the tags |
|
| 769 | - * @return FileInfo[] array or file info |
|
| 770 | - */ |
|
| 771 | - static public function searchByTag($tag, $userId) { |
|
| 772 | - return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 773 | - } |
|
| 774 | - |
|
| 775 | - /** |
|
| 776 | - * check if a file or folder has been updated since $time |
|
| 777 | - * |
|
| 778 | - * @param string $path |
|
| 779 | - * @param int $time |
|
| 780 | - * @return bool |
|
| 781 | - */ |
|
| 782 | - static public function hasUpdated($path, $time) { |
|
| 783 | - return self::$defaultInstance->hasUpdated($path, $time); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Fix common problems with a file path |
|
| 788 | - * |
|
| 789 | - * @param string $path |
|
| 790 | - * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 791 | - * @param bool $isAbsolutePath whether the given path is absolute |
|
| 792 | - * @param bool $keepUnicode true to disable unicode normalization |
|
| 793 | - * @return string |
|
| 794 | - */ |
|
| 795 | - public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 796 | - if (is_null(self::$normalizedPathCache)) { |
|
| 797 | - self::$normalizedPathCache = new CappedMemoryCache(); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - /** |
|
| 801 | - * FIXME: This is a workaround for existing classes and files which call |
|
| 802 | - * this function with another type than a valid string. This |
|
| 803 | - * conversion should get removed as soon as all existing |
|
| 804 | - * function calls have been fixed. |
|
| 805 | - */ |
|
| 806 | - $path = (string)$path; |
|
| 807 | - |
|
| 808 | - $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 809 | - |
|
| 810 | - if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 811 | - return self::$normalizedPathCache[$cacheKey]; |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - if ($path == '') { |
|
| 815 | - return '/'; |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - //normalize unicode if possible |
|
| 819 | - if (!$keepUnicode) { |
|
| 820 | - $path = \OC_Util::normalizeUnicode($path); |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - //no windows style slashes |
|
| 824 | - $path = str_replace('\\', '/', $path); |
|
| 825 | - |
|
| 826 | - //add leading slash |
|
| 827 | - if ($path[0] !== '/') { |
|
| 828 | - $path = '/' . $path; |
|
| 829 | - } |
|
| 830 | - |
|
| 831 | - // remove '/./' |
|
| 832 | - // ugly, but str_replace() can't replace them all in one go |
|
| 833 | - // as the replacement itself is part of the search string |
|
| 834 | - // which will only be found during the next iteration |
|
| 835 | - while (strpos($path, '/./') !== false) { |
|
| 836 | - $path = str_replace('/./', '/', $path); |
|
| 837 | - } |
|
| 838 | - // remove sequences of slashes |
|
| 839 | - $path = preg_replace('#/{2,}#', '/', $path); |
|
| 840 | - |
|
| 841 | - //remove trailing slash |
|
| 842 | - if ($stripTrailingSlash and strlen($path) > 1) { |
|
| 843 | - $path = rtrim($path, '/'); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - // remove trailing '/.' |
|
| 847 | - if (substr($path, -2) == '/.') { |
|
| 848 | - $path = substr($path, 0, -2); |
|
| 849 | - } |
|
| 850 | - |
|
| 851 | - $normalizedPath = $path; |
|
| 852 | - self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 853 | - |
|
| 854 | - return $normalizedPath; |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * get the filesystem info |
|
| 859 | - * |
|
| 860 | - * @param string $path |
|
| 861 | - * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 862 | - * defaults to true |
|
| 863 | - * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 864 | - */ |
|
| 865 | - public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 866 | - return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - /** |
|
| 870 | - * change file metadata |
|
| 871 | - * |
|
| 872 | - * @param string $path |
|
| 873 | - * @param array $data |
|
| 874 | - * @return int |
|
| 875 | - * |
|
| 876 | - * returns the fileid of the updated file |
|
| 877 | - */ |
|
| 878 | - public static function putFileInfo($path, $data) { |
|
| 879 | - return self::$defaultInstance->putFileInfo($path, $data); |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * get the content of a directory |
|
| 884 | - * |
|
| 885 | - * @param string $directory path under datadirectory |
|
| 886 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 887 | - * @return \OC\Files\FileInfo[] |
|
| 888 | - */ |
|
| 889 | - public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 890 | - return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - /** |
|
| 894 | - * Get the path of a file by id |
|
| 895 | - * |
|
| 896 | - * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 897 | - * |
|
| 898 | - * @param int $id |
|
| 899 | - * @throws NotFoundException |
|
| 900 | - * @return string |
|
| 901 | - */ |
|
| 902 | - public static function getPath($id) { |
|
| 903 | - return self::$defaultInstance->getPath($id); |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - /** |
|
| 907 | - * Get the owner for a file or folder |
|
| 908 | - * |
|
| 909 | - * @param string $path |
|
| 910 | - * @return string |
|
| 911 | - */ |
|
| 912 | - public static function getOwner($path) { |
|
| 913 | - return self::$defaultInstance->getOwner($path); |
|
| 914 | - } |
|
| 915 | - |
|
| 916 | - /** |
|
| 917 | - * get the ETag for a file or folder |
|
| 918 | - * |
|
| 919 | - * @param string $path |
|
| 920 | - * @return string |
|
| 921 | - */ |
|
| 922 | - static public function getETag($path) { |
|
| 923 | - return self::$defaultInstance->getETag($path); |
|
| 924 | - } |
|
| 72 | + /** |
|
| 73 | + * @var Mount\Manager $mounts |
|
| 74 | + */ |
|
| 75 | + private static $mounts; |
|
| 76 | + |
|
| 77 | + public static $loaded = false; |
|
| 78 | + /** |
|
| 79 | + * @var \OC\Files\View $defaultInstance |
|
| 80 | + */ |
|
| 81 | + static private $defaultInstance; |
|
| 82 | + |
|
| 83 | + static private $usersSetup = array(); |
|
| 84 | + |
|
| 85 | + static private $normalizedPathCache = null; |
|
| 86 | + |
|
| 87 | + static private $listeningForProviders = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * classname which used for hooks handling |
|
| 91 | + * used as signalclass in OC_Hooks::emit() |
|
| 92 | + */ |
|
| 93 | + const CLASSNAME = 'OC_Filesystem'; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * signalname emitted before file renaming |
|
| 97 | + * |
|
| 98 | + * @param string $oldpath |
|
| 99 | + * @param string $newpath |
|
| 100 | + */ |
|
| 101 | + const signal_rename = 'rename'; |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * signal emitted after file renaming |
|
| 105 | + * |
|
| 106 | + * @param string $oldpath |
|
| 107 | + * @param string $newpath |
|
| 108 | + */ |
|
| 109 | + const signal_post_rename = 'post_rename'; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * signal emitted before file/dir creation |
|
| 113 | + * |
|
| 114 | + * @param string $path |
|
| 115 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 116 | + */ |
|
| 117 | + const signal_create = 'create'; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * signal emitted after file/dir creation |
|
| 121 | + * |
|
| 122 | + * @param string $path |
|
| 123 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 124 | + */ |
|
| 125 | + const signal_post_create = 'post_create'; |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * signal emits before file/dir copy |
|
| 129 | + * |
|
| 130 | + * @param string $oldpath |
|
| 131 | + * @param string $newpath |
|
| 132 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 133 | + */ |
|
| 134 | + const signal_copy = 'copy'; |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * signal emits after file/dir copy |
|
| 138 | + * |
|
| 139 | + * @param string $oldpath |
|
| 140 | + * @param string $newpath |
|
| 141 | + */ |
|
| 142 | + const signal_post_copy = 'post_copy'; |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * signal emits before file/dir save |
|
| 146 | + * |
|
| 147 | + * @param string $path |
|
| 148 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 149 | + */ |
|
| 150 | + const signal_write = 'write'; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * signal emits after file/dir save |
|
| 154 | + * |
|
| 155 | + * @param string $path |
|
| 156 | + */ |
|
| 157 | + const signal_post_write = 'post_write'; |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * signal emitted before file/dir update |
|
| 161 | + * |
|
| 162 | + * @param string $path |
|
| 163 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 164 | + */ |
|
| 165 | + const signal_update = 'update'; |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * signal emitted after file/dir update |
|
| 169 | + * |
|
| 170 | + * @param string $path |
|
| 171 | + * @param bool $run changing this flag to false in hook handler will cancel event |
|
| 172 | + */ |
|
| 173 | + const signal_post_update = 'post_update'; |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * signal emits when reading file/dir |
|
| 177 | + * |
|
| 178 | + * @param string $path |
|
| 179 | + */ |
|
| 180 | + const signal_read = 'read'; |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * signal emits when removing file/dir |
|
| 184 | + * |
|
| 185 | + * @param string $path |
|
| 186 | + */ |
|
| 187 | + const signal_delete = 'delete'; |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * parameters definitions for signals |
|
| 191 | + */ |
|
| 192 | + const signal_param_path = 'path'; |
|
| 193 | + const signal_param_oldpath = 'oldpath'; |
|
| 194 | + const signal_param_newpath = 'newpath'; |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * run - changing this flag to false in hook handler will cancel event |
|
| 198 | + */ |
|
| 199 | + const signal_param_run = 'run'; |
|
| 200 | + |
|
| 201 | + const signal_create_mount = 'create_mount'; |
|
| 202 | + const signal_delete_mount = 'delete_mount'; |
|
| 203 | + const signal_param_mount_type = 'mounttype'; |
|
| 204 | + const signal_param_users = 'users'; |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
| 208 | + */ |
|
| 209 | + private static $loader; |
|
| 210 | + |
|
| 211 | + /** @var bool */ |
|
| 212 | + private static $logWarningWhenAddingStorageWrapper = true; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @param bool $shouldLog |
|
| 216 | + * @return bool previous value |
|
| 217 | + * @internal |
|
| 218 | + */ |
|
| 219 | + public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
|
| 220 | + $previousValue = self::$logWarningWhenAddingStorageWrapper; |
|
| 221 | + self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog; |
|
| 222 | + return $previousValue; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @param string $wrapperName |
|
| 227 | + * @param callable $wrapper |
|
| 228 | + * @param int $priority |
|
| 229 | + */ |
|
| 230 | + public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 231 | + if (self::$logWarningWhenAddingStorageWrapper) { |
|
| 232 | + \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [ |
|
| 233 | + 'wrapper' => $wrapperName, |
|
| 234 | + 'app' => 'filesystem', |
|
| 235 | + ]); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $mounts = self::getMountManager()->getAll(); |
|
| 239 | + if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) { |
|
| 240 | + // do not re-wrap if storage with this name already existed |
|
| 241 | + return; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Returns the storage factory |
|
| 247 | + * |
|
| 248 | + * @return \OCP\Files\Storage\IStorageFactory |
|
| 249 | + */ |
|
| 250 | + public static function getLoader() { |
|
| 251 | + if (!self::$loader) { |
|
| 252 | + self::$loader = new StorageFactory(); |
|
| 253 | + } |
|
| 254 | + return self::$loader; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Returns the mount manager |
|
| 259 | + * |
|
| 260 | + * @return \OC\Files\Mount\Manager |
|
| 261 | + */ |
|
| 262 | + public static function getMountManager($user = '') { |
|
| 263 | + if (!self::$mounts) { |
|
| 264 | + \OC_Util::setupFS($user); |
|
| 265 | + } |
|
| 266 | + return self::$mounts; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * get the mountpoint of the storage object for a path |
|
| 271 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 272 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 273 | + * and doesn't take the chroot into account ) |
|
| 274 | + * |
|
| 275 | + * @param string $path |
|
| 276 | + * @return string |
|
| 277 | + */ |
|
| 278 | + static public function getMountPoint($path) { |
|
| 279 | + if (!self::$mounts) { |
|
| 280 | + \OC_Util::setupFS(); |
|
| 281 | + } |
|
| 282 | + $mount = self::$mounts->find($path); |
|
| 283 | + if ($mount) { |
|
| 284 | + return $mount->getMountPoint(); |
|
| 285 | + } else { |
|
| 286 | + return ''; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * get a list of all mount points in a directory |
|
| 292 | + * |
|
| 293 | + * @param string $path |
|
| 294 | + * @return string[] |
|
| 295 | + */ |
|
| 296 | + static public function getMountPoints($path) { |
|
| 297 | + if (!self::$mounts) { |
|
| 298 | + \OC_Util::setupFS(); |
|
| 299 | + } |
|
| 300 | + $result = array(); |
|
| 301 | + $mounts = self::$mounts->findIn($path); |
|
| 302 | + foreach ($mounts as $mount) { |
|
| 303 | + $result[] = $mount->getMountPoint(); |
|
| 304 | + } |
|
| 305 | + return $result; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * get the storage mounted at $mountPoint |
|
| 310 | + * |
|
| 311 | + * @param string $mountPoint |
|
| 312 | + * @return \OC\Files\Storage\Storage |
|
| 313 | + */ |
|
| 314 | + public static function getStorage($mountPoint) { |
|
| 315 | + if (!self::$mounts) { |
|
| 316 | + \OC_Util::setupFS(); |
|
| 317 | + } |
|
| 318 | + $mount = self::$mounts->find($mountPoint); |
|
| 319 | + return $mount->getStorage(); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param string $id |
|
| 324 | + * @return Mount\MountPoint[] |
|
| 325 | + */ |
|
| 326 | + public static function getMountByStorageId($id) { |
|
| 327 | + if (!self::$mounts) { |
|
| 328 | + \OC_Util::setupFS(); |
|
| 329 | + } |
|
| 330 | + return self::$mounts->findByStorageId($id); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @param int $id |
|
| 335 | + * @return Mount\MountPoint[] |
|
| 336 | + */ |
|
| 337 | + public static function getMountByNumericId($id) { |
|
| 338 | + if (!self::$mounts) { |
|
| 339 | + \OC_Util::setupFS(); |
|
| 340 | + } |
|
| 341 | + return self::$mounts->findByNumericId($id); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * resolve a path to a storage and internal path |
|
| 346 | + * |
|
| 347 | + * @param string $path |
|
| 348 | + * @return array an array consisting of the storage and the internal path |
|
| 349 | + */ |
|
| 350 | + static public function resolvePath($path) { |
|
| 351 | + if (!self::$mounts) { |
|
| 352 | + \OC_Util::setupFS(); |
|
| 353 | + } |
|
| 354 | + $mount = self::$mounts->find($path); |
|
| 355 | + if ($mount) { |
|
| 356 | + return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 357 | + } else { |
|
| 358 | + return array(null, null); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + static public function init($user, $root) { |
|
| 363 | + if (self::$defaultInstance) { |
|
| 364 | + return false; |
|
| 365 | + } |
|
| 366 | + self::getLoader(); |
|
| 367 | + self::$defaultInstance = new View($root); |
|
| 368 | + |
|
| 369 | + if (!self::$mounts) { |
|
| 370 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + //load custom mount config |
|
| 374 | + self::initMountPoints($user); |
|
| 375 | + |
|
| 376 | + self::$loaded = true; |
|
| 377 | + |
|
| 378 | + return true; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + static public function initMountManager() { |
|
| 382 | + if (!self::$mounts) { |
|
| 383 | + self::$mounts = \OC::$server->getMountManager(); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Initialize system and personal mount points for a user |
|
| 389 | + * |
|
| 390 | + * @param string $user |
|
| 391 | + * @throws \OC\User\NoUserException if the user is not available |
|
| 392 | + */ |
|
| 393 | + public static function initMountPoints($user = '') { |
|
| 394 | + if ($user == '') { |
|
| 395 | + $user = \OC_User::getUser(); |
|
| 396 | + } |
|
| 397 | + if ($user === null || $user === false || $user === '') { |
|
| 398 | + throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session'); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + if (isset(self::$usersSetup[$user])) { |
|
| 402 | + return; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + self::$usersSetup[$user] = true; |
|
| 406 | + |
|
| 407 | + $userManager = \OC::$server->getUserManager(); |
|
| 408 | + $userObject = $userManager->get($user); |
|
| 409 | + |
|
| 410 | + if (is_null($userObject)) { |
|
| 411 | + \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR); |
|
| 412 | + // reset flag, this will make it possible to rethrow the exception if called again |
|
| 413 | + unset(self::$usersSetup[$user]); |
|
| 414 | + throw new \OC\User\NoUserException('Backends provided no user object for ' . $user); |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + $realUid = $userObject->getUID(); |
|
| 418 | + // workaround in case of different casings |
|
| 419 | + if ($user !== $realUid) { |
|
| 420 | + $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50)); |
|
| 421 | + \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN); |
|
| 422 | + $user = $realUid; |
|
| 423 | + |
|
| 424 | + // again with the correct casing |
|
| 425 | + if (isset(self::$usersSetup[$user])) { |
|
| 426 | + return; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + self::$usersSetup[$user] = true; |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + if (\OC::$server->getLockdownManager()->canAccessFilesystem()) { |
|
| 433 | + /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */ |
|
| 434 | + $mountConfigManager = \OC::$server->getMountProviderCollection(); |
|
| 435 | + |
|
| 436 | + // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers |
|
| 437 | + $homeMount = $mountConfigManager->getHomeMountForUser($userObject); |
|
| 438 | + |
|
| 439 | + self::getMountManager()->addMount($homeMount); |
|
| 440 | + |
|
| 441 | + \OC\Files\Filesystem::getStorage($user); |
|
| 442 | + |
|
| 443 | + // Chance to mount for other storages |
|
| 444 | + if ($userObject) { |
|
| 445 | + $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager()); |
|
| 446 | + $mounts[] = $homeMount; |
|
| 447 | + $mountConfigManager->registerMounts($userObject, $mounts); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + self::listenForNewMountProviders($mountConfigManager, $userManager); |
|
| 451 | + } else { |
|
| 452 | + self::getMountManager()->addMount(new MountPoint( |
|
| 453 | + new NullStorage([]), |
|
| 454 | + '/' . $user |
|
| 455 | + )); |
|
| 456 | + self::getMountManager()->addMount(new MountPoint( |
|
| 457 | + new NullStorage([]), |
|
| 458 | + '/' . $user . '/files' |
|
| 459 | + )); |
|
| 460 | + } |
|
| 461 | + \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user)); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * Get mounts from mount providers that are registered after setup |
|
| 466 | + * |
|
| 467 | + * @param MountProviderCollection $mountConfigManager |
|
| 468 | + * @param IUserManager $userManager |
|
| 469 | + */ |
|
| 470 | + private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 471 | + if (!self::$listeningForProviders) { |
|
| 472 | + self::$listeningForProviders = true; |
|
| 473 | + $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) { |
|
| 474 | + foreach (Filesystem::$usersSetup as $user => $setup) { |
|
| 475 | + $userObject = $userManager->get($user); |
|
| 476 | + if ($userObject) { |
|
| 477 | + $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader()); |
|
| 478 | + array_walk($mounts, array(self::$mounts, 'addMount')); |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + }); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + /** |
|
| 486 | + * get the default filesystem view |
|
| 487 | + * |
|
| 488 | + * @return View |
|
| 489 | + */ |
|
| 490 | + static public function getView() { |
|
| 491 | + return self::$defaultInstance; |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * tear down the filesystem, removing all storage providers |
|
| 496 | + */ |
|
| 497 | + static public function tearDown() { |
|
| 498 | + self::clearMounts(); |
|
| 499 | + self::$defaultInstance = null; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * get the relative path of the root data directory for the current user |
|
| 504 | + * |
|
| 505 | + * @return string |
|
| 506 | + * |
|
| 507 | + * Returns path like /admin/files |
|
| 508 | + */ |
|
| 509 | + static public function getRoot() { |
|
| 510 | + if (!self::$defaultInstance) { |
|
| 511 | + return null; |
|
| 512 | + } |
|
| 513 | + return self::$defaultInstance->getRoot(); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * clear all mounts and storage backends |
|
| 518 | + */ |
|
| 519 | + public static function clearMounts() { |
|
| 520 | + if (self::$mounts) { |
|
| 521 | + self::$usersSetup = array(); |
|
| 522 | + self::$mounts->clear(); |
|
| 523 | + } |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * mount an \OC\Files\Storage\Storage in our virtual filesystem |
|
| 528 | + * |
|
| 529 | + * @param \OC\Files\Storage\Storage|string $class |
|
| 530 | + * @param array $arguments |
|
| 531 | + * @param string $mountpoint |
|
| 532 | + */ |
|
| 533 | + static public function mount($class, $arguments, $mountpoint) { |
|
| 534 | + if (!self::$mounts) { |
|
| 535 | + \OC_Util::setupFS(); |
|
| 536 | + } |
|
| 537 | + $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 538 | + self::$mounts->addMount($mount); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + /** |
|
| 542 | + * return the path to a local version of the file |
|
| 543 | + * we need this because we can't know if a file is stored local or not from |
|
| 544 | + * outside the filestorage and for some purposes a local file is needed |
|
| 545 | + * |
|
| 546 | + * @param string $path |
|
| 547 | + * @return string |
|
| 548 | + */ |
|
| 549 | + static public function getLocalFile($path) { |
|
| 550 | + return self::$defaultInstance->getLocalFile($path); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + /** |
|
| 554 | + * @param string $path |
|
| 555 | + * @return string |
|
| 556 | + */ |
|
| 557 | + static public function getLocalFolder($path) { |
|
| 558 | + return self::$defaultInstance->getLocalFolder($path); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * return path to file which reflects one visible in browser |
|
| 563 | + * |
|
| 564 | + * @param string $path |
|
| 565 | + * @return string |
|
| 566 | + */ |
|
| 567 | + static public function getLocalPath($path) { |
|
| 568 | + $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files'; |
|
| 569 | + $newpath = $path; |
|
| 570 | + if (strncmp($newpath, $datadir, strlen($datadir)) == 0) { |
|
| 571 | + $newpath = substr($path, strlen($datadir)); |
|
| 572 | + } |
|
| 573 | + return $newpath; |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * check if the requested path is valid |
|
| 578 | + * |
|
| 579 | + * @param string $path |
|
| 580 | + * @return bool |
|
| 581 | + */ |
|
| 582 | + static public function isValidPath($path) { |
|
| 583 | + $path = self::normalizePath($path); |
|
| 584 | + if (!$path || $path[0] !== '/') { |
|
| 585 | + $path = '/' . $path; |
|
| 586 | + } |
|
| 587 | + if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') { |
|
| 588 | + return false; |
|
| 589 | + } |
|
| 590 | + return true; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * checks if a file is blacklisted for storage in the filesystem |
|
| 595 | + * Listens to write and rename hooks |
|
| 596 | + * |
|
| 597 | + * @param array $data from hook |
|
| 598 | + */ |
|
| 599 | + static public function isBlacklisted($data) { |
|
| 600 | + if (isset($data['path'])) { |
|
| 601 | + $path = $data['path']; |
|
| 602 | + } else if (isset($data['newpath'])) { |
|
| 603 | + $path = $data['newpath']; |
|
| 604 | + } |
|
| 605 | + if (isset($path)) { |
|
| 606 | + if (self::isFileBlacklisted($path)) { |
|
| 607 | + $data['run'] = false; |
|
| 608 | + } |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + /** |
|
| 613 | + * @param string $filename |
|
| 614 | + * @return bool |
|
| 615 | + */ |
|
| 616 | + static public function isFileBlacklisted($filename) { |
|
| 617 | + $filename = self::normalizePath($filename); |
|
| 618 | + |
|
| 619 | + $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess')); |
|
| 620 | + $filename = strtolower(basename($filename)); |
|
| 621 | + return in_array($filename, $blacklist); |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * check if the directory should be ignored when scanning |
|
| 626 | + * NOTE: the special directories . and .. would cause never ending recursion |
|
| 627 | + * |
|
| 628 | + * @param String $dir |
|
| 629 | + * @return boolean |
|
| 630 | + */ |
|
| 631 | + static public function isIgnoredDir($dir) { |
|
| 632 | + if ($dir === '.' || $dir === '..') { |
|
| 633 | + return true; |
|
| 634 | + } |
|
| 635 | + return false; |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * following functions are equivalent to their php builtin equivalents for arguments/return values. |
|
| 640 | + */ |
|
| 641 | + static public function mkdir($path) { |
|
| 642 | + return self::$defaultInstance->mkdir($path); |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + static public function rmdir($path) { |
|
| 646 | + return self::$defaultInstance->rmdir($path); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + static public function is_dir($path) { |
|
| 650 | + return self::$defaultInstance->is_dir($path); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + static public function is_file($path) { |
|
| 654 | + return self::$defaultInstance->is_file($path); |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + static public function stat($path) { |
|
| 658 | + return self::$defaultInstance->stat($path); |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + static public function filetype($path) { |
|
| 662 | + return self::$defaultInstance->filetype($path); |
|
| 663 | + } |
|
| 664 | + |
|
| 665 | + static public function filesize($path) { |
|
| 666 | + return self::$defaultInstance->filesize($path); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + static public function readfile($path) { |
|
| 670 | + return self::$defaultInstance->readfile($path); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + static public function isCreatable($path) { |
|
| 674 | + return self::$defaultInstance->isCreatable($path); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + static public function isReadable($path) { |
|
| 678 | + return self::$defaultInstance->isReadable($path); |
|
| 679 | + } |
|
| 680 | + |
|
| 681 | + static public function isUpdatable($path) { |
|
| 682 | + return self::$defaultInstance->isUpdatable($path); |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + static public function isDeletable($path) { |
|
| 686 | + return self::$defaultInstance->isDeletable($path); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + static public function isSharable($path) { |
|
| 690 | + return self::$defaultInstance->isSharable($path); |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + static public function file_exists($path) { |
|
| 694 | + return self::$defaultInstance->file_exists($path); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + static public function filemtime($path) { |
|
| 698 | + return self::$defaultInstance->filemtime($path); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + static public function touch($path, $mtime = null) { |
|
| 702 | + return self::$defaultInstance->touch($path, $mtime); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * @return string |
|
| 707 | + */ |
|
| 708 | + static public function file_get_contents($path) { |
|
| 709 | + return self::$defaultInstance->file_get_contents($path); |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + static public function file_put_contents($path, $data) { |
|
| 713 | + return self::$defaultInstance->file_put_contents($path, $data); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + static public function unlink($path) { |
|
| 717 | + return self::$defaultInstance->unlink($path); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + static public function rename($path1, $path2) { |
|
| 721 | + return self::$defaultInstance->rename($path1, $path2); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + static public function copy($path1, $path2) { |
|
| 725 | + return self::$defaultInstance->copy($path1, $path2); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + static public function fopen($path, $mode) { |
|
| 729 | + return self::$defaultInstance->fopen($path, $mode); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * @return string |
|
| 734 | + */ |
|
| 735 | + static public function toTmpFile($path) { |
|
| 736 | + return self::$defaultInstance->toTmpFile($path); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + static public function fromTmpFile($tmpFile, $path) { |
|
| 740 | + return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + static public function getMimeType($path) { |
|
| 744 | + return self::$defaultInstance->getMimeType($path); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + static public function hash($type, $path, $raw = false) { |
|
| 748 | + return self::$defaultInstance->hash($type, $path, $raw); |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + static public function free_space($path = '/') { |
|
| 752 | + return self::$defaultInstance->free_space($path); |
|
| 753 | + } |
|
| 754 | + |
|
| 755 | + static public function search($query) { |
|
| 756 | + return self::$defaultInstance->search($query); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + /** |
|
| 760 | + * @param string $query |
|
| 761 | + */ |
|
| 762 | + static public function searchByMime($query) { |
|
| 763 | + return self::$defaultInstance->searchByMime($query); |
|
| 764 | + } |
|
| 765 | + |
|
| 766 | + /** |
|
| 767 | + * @param string|int $tag name or tag id |
|
| 768 | + * @param string $userId owner of the tags |
|
| 769 | + * @return FileInfo[] array or file info |
|
| 770 | + */ |
|
| 771 | + static public function searchByTag($tag, $userId) { |
|
| 772 | + return self::$defaultInstance->searchByTag($tag, $userId); |
|
| 773 | + } |
|
| 774 | + |
|
| 775 | + /** |
|
| 776 | + * check if a file or folder has been updated since $time |
|
| 777 | + * |
|
| 778 | + * @param string $path |
|
| 779 | + * @param int $time |
|
| 780 | + * @return bool |
|
| 781 | + */ |
|
| 782 | + static public function hasUpdated($path, $time) { |
|
| 783 | + return self::$defaultInstance->hasUpdated($path, $time); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Fix common problems with a file path |
|
| 788 | + * |
|
| 789 | + * @param string $path |
|
| 790 | + * @param bool $stripTrailingSlash whether to strip the trailing slash |
|
| 791 | + * @param bool $isAbsolutePath whether the given path is absolute |
|
| 792 | + * @param bool $keepUnicode true to disable unicode normalization |
|
| 793 | + * @return string |
|
| 794 | + */ |
|
| 795 | + public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
|
| 796 | + if (is_null(self::$normalizedPathCache)) { |
|
| 797 | + self::$normalizedPathCache = new CappedMemoryCache(); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + /** |
|
| 801 | + * FIXME: This is a workaround for existing classes and files which call |
|
| 802 | + * this function with another type than a valid string. This |
|
| 803 | + * conversion should get removed as soon as all existing |
|
| 804 | + * function calls have been fixed. |
|
| 805 | + */ |
|
| 806 | + $path = (string)$path; |
|
| 807 | + |
|
| 808 | + $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
|
| 809 | + |
|
| 810 | + if (isset(self::$normalizedPathCache[$cacheKey])) { |
|
| 811 | + return self::$normalizedPathCache[$cacheKey]; |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + if ($path == '') { |
|
| 815 | + return '/'; |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + //normalize unicode if possible |
|
| 819 | + if (!$keepUnicode) { |
|
| 820 | + $path = \OC_Util::normalizeUnicode($path); |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + //no windows style slashes |
|
| 824 | + $path = str_replace('\\', '/', $path); |
|
| 825 | + |
|
| 826 | + //add leading slash |
|
| 827 | + if ($path[0] !== '/') { |
|
| 828 | + $path = '/' . $path; |
|
| 829 | + } |
|
| 830 | + |
|
| 831 | + // remove '/./' |
|
| 832 | + // ugly, but str_replace() can't replace them all in one go |
|
| 833 | + // as the replacement itself is part of the search string |
|
| 834 | + // which will only be found during the next iteration |
|
| 835 | + while (strpos($path, '/./') !== false) { |
|
| 836 | + $path = str_replace('/./', '/', $path); |
|
| 837 | + } |
|
| 838 | + // remove sequences of slashes |
|
| 839 | + $path = preg_replace('#/{2,}#', '/', $path); |
|
| 840 | + |
|
| 841 | + //remove trailing slash |
|
| 842 | + if ($stripTrailingSlash and strlen($path) > 1) { |
|
| 843 | + $path = rtrim($path, '/'); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + // remove trailing '/.' |
|
| 847 | + if (substr($path, -2) == '/.') { |
|
| 848 | + $path = substr($path, 0, -2); |
|
| 849 | + } |
|
| 850 | + |
|
| 851 | + $normalizedPath = $path; |
|
| 852 | + self::$normalizedPathCache[$cacheKey] = $normalizedPath; |
|
| 853 | + |
|
| 854 | + return $normalizedPath; |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * get the filesystem info |
|
| 859 | + * |
|
| 860 | + * @param string $path |
|
| 861 | + * @param boolean $includeMountPoints whether to add mountpoint sizes, |
|
| 862 | + * defaults to true |
|
| 863 | + * @return \OC\Files\FileInfo|bool False if file does not exist |
|
| 864 | + */ |
|
| 865 | + public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 866 | + return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + /** |
|
| 870 | + * change file metadata |
|
| 871 | + * |
|
| 872 | + * @param string $path |
|
| 873 | + * @param array $data |
|
| 874 | + * @return int |
|
| 875 | + * |
|
| 876 | + * returns the fileid of the updated file |
|
| 877 | + */ |
|
| 878 | + public static function putFileInfo($path, $data) { |
|
| 879 | + return self::$defaultInstance->putFileInfo($path, $data); |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * get the content of a directory |
|
| 884 | + * |
|
| 885 | + * @param string $directory path under datadirectory |
|
| 886 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 887 | + * @return \OC\Files\FileInfo[] |
|
| 888 | + */ |
|
| 889 | + public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 890 | + return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + /** |
|
| 894 | + * Get the path of a file by id |
|
| 895 | + * |
|
| 896 | + * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
|
| 897 | + * |
|
| 898 | + * @param int $id |
|
| 899 | + * @throws NotFoundException |
|
| 900 | + * @return string |
|
| 901 | + */ |
|
| 902 | + public static function getPath($id) { |
|
| 903 | + return self::$defaultInstance->getPath($id); |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + /** |
|
| 907 | + * Get the owner for a file or folder |
|
| 908 | + * |
|
| 909 | + * @param string $path |
|
| 910 | + * @return string |
|
| 911 | + */ |
|
| 912 | + public static function getOwner($path) { |
|
| 913 | + return self::$defaultInstance->getOwner($path); |
|
| 914 | + } |
|
| 915 | + |
|
| 916 | + /** |
|
| 917 | + * get the ETag for a file or folder |
|
| 918 | + * |
|
| 919 | + * @param string $path |
|
| 920 | + * @return string |
|
| 921 | + */ |
|
| 922 | + static public function getETag($path) { |
|
| 923 | + return self::$defaultInstance->getETag($path); |
|
| 924 | + } |
|
| 925 | 925 | } |
@@ -80,2084 +80,2084 @@ |
||
| 80 | 80 | * \OC\Files\Storage\Storage object |
| 81 | 81 | */ |
| 82 | 82 | class View { |
| 83 | - /** @var string */ |
|
| 84 | - private $fakeRoot = ''; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @var \OCP\Lock\ILockingProvider |
|
| 88 | - */ |
|
| 89 | - protected $lockingProvider; |
|
| 90 | - |
|
| 91 | - private $lockingEnabled; |
|
| 92 | - |
|
| 93 | - private $updaterEnabled = true; |
|
| 94 | - |
|
| 95 | - /** @var \OC\User\Manager */ |
|
| 96 | - private $userManager; |
|
| 97 | - |
|
| 98 | - /** @var \OCP\ILogger */ |
|
| 99 | - private $logger; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @param string $root |
|
| 103 | - * @throws \Exception If $root contains an invalid path |
|
| 104 | - */ |
|
| 105 | - public function __construct($root = '') { |
|
| 106 | - if (is_null($root)) { |
|
| 107 | - throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 108 | - } |
|
| 109 | - if (!Filesystem::isValidPath($root)) { |
|
| 110 | - throw new \Exception(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->fakeRoot = $root; |
|
| 114 | - $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 115 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 116 | - $this->userManager = \OC::$server->getUserManager(); |
|
| 117 | - $this->logger = \OC::$server->getLogger(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - public function getAbsolutePath($path = '/') { |
|
| 121 | - if ($path === null) { |
|
| 122 | - return null; |
|
| 123 | - } |
|
| 124 | - $this->assertPathLength($path); |
|
| 125 | - if ($path === '') { |
|
| 126 | - $path = '/'; |
|
| 127 | - } |
|
| 128 | - if ($path[0] !== '/') { |
|
| 129 | - $path = '/' . $path; |
|
| 130 | - } |
|
| 131 | - return $this->fakeRoot . $path; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * change the root to a fake root |
|
| 136 | - * |
|
| 137 | - * @param string $fakeRoot |
|
| 138 | - * @return boolean|null |
|
| 139 | - */ |
|
| 140 | - public function chroot($fakeRoot) { |
|
| 141 | - if (!$fakeRoot == '') { |
|
| 142 | - if ($fakeRoot[0] !== '/') { |
|
| 143 | - $fakeRoot = '/' . $fakeRoot; |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - $this->fakeRoot = $fakeRoot; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * get the fake root |
|
| 151 | - * |
|
| 152 | - * @return string |
|
| 153 | - */ |
|
| 154 | - public function getRoot() { |
|
| 155 | - return $this->fakeRoot; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * get path relative to the root of the view |
|
| 160 | - * |
|
| 161 | - * @param string $path |
|
| 162 | - * @return string |
|
| 163 | - */ |
|
| 164 | - public function getRelativePath($path) { |
|
| 165 | - $this->assertPathLength($path); |
|
| 166 | - if ($this->fakeRoot == '') { |
|
| 167 | - return $path; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 171 | - return '/'; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - // missing slashes can cause wrong matches! |
|
| 175 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 176 | - |
|
| 177 | - if (strpos($path, $root) !== 0) { |
|
| 178 | - return null; |
|
| 179 | - } else { |
|
| 180 | - $path = substr($path, strlen($this->fakeRoot)); |
|
| 181 | - if (strlen($path) === 0) { |
|
| 182 | - return '/'; |
|
| 183 | - } else { |
|
| 184 | - return $path; |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * get the mountpoint of the storage object for a path |
|
| 191 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 192 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 193 | - * and does not take the chroot into account ) |
|
| 194 | - * |
|
| 195 | - * @param string $path |
|
| 196 | - * @return string |
|
| 197 | - */ |
|
| 198 | - public function getMountPoint($path) { |
|
| 199 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * get the mountpoint of the storage object for a path |
|
| 204 | - * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 205 | - * returned mountpoint is relative to the absolute root of the filesystem |
|
| 206 | - * and does not take the chroot into account ) |
|
| 207 | - * |
|
| 208 | - * @param string $path |
|
| 209 | - * @return \OCP\Files\Mount\IMountPoint |
|
| 210 | - */ |
|
| 211 | - public function getMount($path) { |
|
| 212 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * resolve a path to a storage and internal path |
|
| 217 | - * |
|
| 218 | - * @param string $path |
|
| 219 | - * @return array an array consisting of the storage and the internal path |
|
| 220 | - */ |
|
| 221 | - public function resolvePath($path) { |
|
| 222 | - $a = $this->getAbsolutePath($path); |
|
| 223 | - $p = Filesystem::normalizePath($a); |
|
| 224 | - return Filesystem::resolvePath($p); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * return the path to a local version of the file |
|
| 229 | - * we need this because we can't know if a file is stored local or not from |
|
| 230 | - * outside the filestorage and for some purposes a local file is needed |
|
| 231 | - * |
|
| 232 | - * @param string $path |
|
| 233 | - * @return string |
|
| 234 | - */ |
|
| 235 | - public function getLocalFile($path) { |
|
| 236 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 237 | - $path = $this->getAbsolutePath($path); |
|
| 238 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 239 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 240 | - return $storage->getLocalFile($internalPath); |
|
| 241 | - } else { |
|
| 242 | - return null; |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param string $path |
|
| 248 | - * @return string |
|
| 249 | - */ |
|
| 250 | - public function getLocalFolder($path) { |
|
| 251 | - $parent = substr($path, 0, strrpos($path, '/')); |
|
| 252 | - $path = $this->getAbsolutePath($path); |
|
| 253 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 254 | - if (Filesystem::isValidPath($parent) and $storage) { |
|
| 255 | - return $storage->getLocalFolder($internalPath); |
|
| 256 | - } else { |
|
| 257 | - return null; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * the following functions operate with arguments and return values identical |
|
| 263 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 264 | - * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 265 | - */ |
|
| 266 | - public function mkdir($path) { |
|
| 267 | - return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * remove mount point |
|
| 272 | - * |
|
| 273 | - * @param \OC\Files\Mount\MoveableMount $mount |
|
| 274 | - * @param string $path relative to data/ |
|
| 275 | - * @return boolean |
|
| 276 | - */ |
|
| 277 | - protected function removeMount($mount, $path) { |
|
| 278 | - if ($mount instanceof MoveableMount) { |
|
| 279 | - // cut of /user/files to get the relative path to data/user/files |
|
| 280 | - $pathParts = explode('/', $path, 4); |
|
| 281 | - $relPath = '/' . $pathParts[3]; |
|
| 282 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 283 | - \OC_Hook::emit( |
|
| 284 | - Filesystem::CLASSNAME, "umount", |
|
| 285 | - array(Filesystem::signal_param_path => $relPath) |
|
| 286 | - ); |
|
| 287 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 288 | - $result = $mount->removeMount(); |
|
| 289 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 290 | - if ($result) { |
|
| 291 | - \OC_Hook::emit( |
|
| 292 | - Filesystem::CLASSNAME, "post_umount", |
|
| 293 | - array(Filesystem::signal_param_path => $relPath) |
|
| 294 | - ); |
|
| 295 | - } |
|
| 296 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 297 | - return $result; |
|
| 298 | - } else { |
|
| 299 | - // do not allow deleting the storage's root / the mount point |
|
| 300 | - // because for some storages it might delete the whole contents |
|
| 301 | - // but isn't supposed to work that way |
|
| 302 | - return false; |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - public function disableCacheUpdate() { |
|
| 307 | - $this->updaterEnabled = false; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - public function enableCacheUpdate() { |
|
| 311 | - $this->updaterEnabled = true; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 315 | - if ($this->updaterEnabled) { |
|
| 316 | - if (is_null($time)) { |
|
| 317 | - $time = time(); |
|
| 318 | - } |
|
| 319 | - $storage->getUpdater()->update($internalPath, $time); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 324 | - if ($this->updaterEnabled) { |
|
| 325 | - $storage->getUpdater()->remove($internalPath); |
|
| 326 | - } |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 330 | - if ($this->updaterEnabled) { |
|
| 331 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 332 | - } |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @param string $path |
|
| 337 | - * @return bool|mixed |
|
| 338 | - */ |
|
| 339 | - public function rmdir($path) { |
|
| 340 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 341 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 342 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 343 | - return $this->removeMount($mount, $absolutePath); |
|
| 344 | - } |
|
| 345 | - if ($this->is_dir($path)) { |
|
| 346 | - $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 347 | - } else { |
|
| 348 | - $result = false; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 352 | - $storage = $mount->getStorage(); |
|
| 353 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 354 | - $storage->getUpdater()->remove($internalPath); |
|
| 355 | - } |
|
| 356 | - return $result; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @param string $path |
|
| 361 | - * @return resource |
|
| 362 | - */ |
|
| 363 | - public function opendir($path) { |
|
| 364 | - return $this->basicOperation('opendir', $path, array('read')); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * @param string $path |
|
| 369 | - * @return bool|mixed |
|
| 370 | - */ |
|
| 371 | - public function is_dir($path) { |
|
| 372 | - if ($path == '/') { |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - return $this->basicOperation('is_dir', $path); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * @param string $path |
|
| 380 | - * @return bool|mixed |
|
| 381 | - */ |
|
| 382 | - public function is_file($path) { |
|
| 383 | - if ($path == '/') { |
|
| 384 | - return false; |
|
| 385 | - } |
|
| 386 | - return $this->basicOperation('is_file', $path); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * @param string $path |
|
| 391 | - * @return mixed |
|
| 392 | - */ |
|
| 393 | - public function stat($path) { |
|
| 394 | - return $this->basicOperation('stat', $path); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * @param string $path |
|
| 399 | - * @return mixed |
|
| 400 | - */ |
|
| 401 | - public function filetype($path) { |
|
| 402 | - return $this->basicOperation('filetype', $path); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * @param string $path |
|
| 407 | - * @return mixed |
|
| 408 | - */ |
|
| 409 | - public function filesize($path) { |
|
| 410 | - return $this->basicOperation('filesize', $path); |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * @param string $path |
|
| 415 | - * @return bool|mixed |
|
| 416 | - * @throws \OCP\Files\InvalidPathException |
|
| 417 | - */ |
|
| 418 | - public function readfile($path) { |
|
| 419 | - $this->assertPathLength($path); |
|
| 420 | - @ob_end_clean(); |
|
| 421 | - $handle = $this->fopen($path, 'rb'); |
|
| 422 | - if ($handle) { |
|
| 423 | - $chunkSize = 8192; // 8 kB chunks |
|
| 424 | - while (!feof($handle)) { |
|
| 425 | - echo fread($handle, $chunkSize); |
|
| 426 | - flush(); |
|
| 427 | - } |
|
| 428 | - fclose($handle); |
|
| 429 | - return $this->filesize($path); |
|
| 430 | - } |
|
| 431 | - return false; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * @param string $path |
|
| 436 | - * @param int $from |
|
| 437 | - * @param int $to |
|
| 438 | - * @return bool|mixed |
|
| 439 | - * @throws \OCP\Files\InvalidPathException |
|
| 440 | - * @throws \OCP\Files\UnseekableException |
|
| 441 | - */ |
|
| 442 | - public function readfilePart($path, $from, $to) { |
|
| 443 | - $this->assertPathLength($path); |
|
| 444 | - @ob_end_clean(); |
|
| 445 | - $handle = $this->fopen($path, 'rb'); |
|
| 446 | - if ($handle) { |
|
| 447 | - $chunkSize = 8192; // 8 kB chunks |
|
| 448 | - $startReading = true; |
|
| 449 | - |
|
| 450 | - if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 451 | - // forward file handle via chunked fread because fseek seem to have failed |
|
| 452 | - |
|
| 453 | - $end = $from + 1; |
|
| 454 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 455 | - $len = $from - ftell($handle); |
|
| 456 | - if ($len > $chunkSize) { |
|
| 457 | - $len = $chunkSize; |
|
| 458 | - } |
|
| 459 | - $result = fread($handle, $len); |
|
| 460 | - |
|
| 461 | - if ($result === false) { |
|
| 462 | - $startReading = false; |
|
| 463 | - break; |
|
| 464 | - } |
|
| 465 | - } |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - if ($startReading) { |
|
| 469 | - $end = $to + 1; |
|
| 470 | - while (!feof($handle) && ftell($handle) < $end) { |
|
| 471 | - $len = $end - ftell($handle); |
|
| 472 | - if ($len > $chunkSize) { |
|
| 473 | - $len = $chunkSize; |
|
| 474 | - } |
|
| 475 | - echo fread($handle, $len); |
|
| 476 | - flush(); |
|
| 477 | - } |
|
| 478 | - return ftell($handle) - $from; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 482 | - } |
|
| 483 | - return false; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * @param string $path |
|
| 488 | - * @return mixed |
|
| 489 | - */ |
|
| 490 | - public function isCreatable($path) { |
|
| 491 | - return $this->basicOperation('isCreatable', $path); |
|
| 492 | - } |
|
| 493 | - |
|
| 494 | - /** |
|
| 495 | - * @param string $path |
|
| 496 | - * @return mixed |
|
| 497 | - */ |
|
| 498 | - public function isReadable($path) { |
|
| 499 | - return $this->basicOperation('isReadable', $path); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * @param string $path |
|
| 504 | - * @return mixed |
|
| 505 | - */ |
|
| 506 | - public function isUpdatable($path) { |
|
| 507 | - return $this->basicOperation('isUpdatable', $path); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * @param string $path |
|
| 512 | - * @return bool|mixed |
|
| 513 | - */ |
|
| 514 | - public function isDeletable($path) { |
|
| 515 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 516 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 517 | - if ($mount->getInternalPath($absolutePath) === '') { |
|
| 518 | - return $mount instanceof MoveableMount; |
|
| 519 | - } |
|
| 520 | - return $this->basicOperation('isDeletable', $path); |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * @param string $path |
|
| 525 | - * @return mixed |
|
| 526 | - */ |
|
| 527 | - public function isSharable($path) { |
|
| 528 | - return $this->basicOperation('isSharable', $path); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - /** |
|
| 532 | - * @param string $path |
|
| 533 | - * @return bool|mixed |
|
| 534 | - */ |
|
| 535 | - public function file_exists($path) { |
|
| 536 | - if ($path == '/') { |
|
| 537 | - return true; |
|
| 538 | - } |
|
| 539 | - return $this->basicOperation('file_exists', $path); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * @param string $path |
|
| 544 | - * @return mixed |
|
| 545 | - */ |
|
| 546 | - public function filemtime($path) { |
|
| 547 | - return $this->basicOperation('filemtime', $path); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * @param string $path |
|
| 552 | - * @param int|string $mtime |
|
| 553 | - * @return bool |
|
| 554 | - */ |
|
| 555 | - public function touch($path, $mtime = null) { |
|
| 556 | - if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 557 | - $mtime = strtotime($mtime); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - $hooks = array('touch'); |
|
| 561 | - |
|
| 562 | - if (!$this->file_exists($path)) { |
|
| 563 | - $hooks[] = 'create'; |
|
| 564 | - $hooks[] = 'write'; |
|
| 565 | - } |
|
| 566 | - $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 567 | - if (!$result) { |
|
| 568 | - // If create file fails because of permissions on external storage like SMB folders, |
|
| 569 | - // check file exists and return false if not. |
|
| 570 | - if (!$this->file_exists($path)) { |
|
| 571 | - return false; |
|
| 572 | - } |
|
| 573 | - if (is_null($mtime)) { |
|
| 574 | - $mtime = time(); |
|
| 575 | - } |
|
| 576 | - //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 577 | - $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 578 | - } |
|
| 579 | - return true; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * @param string $path |
|
| 584 | - * @return mixed |
|
| 585 | - */ |
|
| 586 | - public function file_get_contents($path) { |
|
| 587 | - return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * @param bool $exists |
|
| 592 | - * @param string $path |
|
| 593 | - * @param bool $run |
|
| 594 | - */ |
|
| 595 | - protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 596 | - if (!$exists) { |
|
| 597 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 598 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 599 | - Filesystem::signal_param_run => &$run, |
|
| 600 | - )); |
|
| 601 | - } else { |
|
| 602 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 603 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 604 | - Filesystem::signal_param_run => &$run, |
|
| 605 | - )); |
|
| 606 | - } |
|
| 607 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 608 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 609 | - Filesystem::signal_param_run => &$run, |
|
| 610 | - )); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * @param bool $exists |
|
| 615 | - * @param string $path |
|
| 616 | - */ |
|
| 617 | - protected function emit_file_hooks_post($exists, $path) { |
|
| 618 | - if (!$exists) { |
|
| 619 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 620 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 621 | - )); |
|
| 622 | - } else { |
|
| 623 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 624 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 625 | - )); |
|
| 626 | - } |
|
| 627 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 628 | - Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 629 | - )); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * @param string $path |
|
| 634 | - * @param mixed $data |
|
| 635 | - * @return bool|mixed |
|
| 636 | - * @throws \Exception |
|
| 637 | - */ |
|
| 638 | - public function file_put_contents($path, $data) { |
|
| 639 | - if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 640 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 641 | - if (Filesystem::isValidPath($path) |
|
| 642 | - and !Filesystem::isFileBlacklisted($path) |
|
| 643 | - ) { |
|
| 644 | - $path = $this->getRelativePath($absolutePath); |
|
| 645 | - |
|
| 646 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 647 | - |
|
| 648 | - $exists = $this->file_exists($path); |
|
| 649 | - $run = true; |
|
| 650 | - if ($this->shouldEmitHooks($path)) { |
|
| 651 | - $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 652 | - } |
|
| 653 | - if (!$run) { |
|
| 654 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 655 | - return false; |
|
| 656 | - } |
|
| 657 | - |
|
| 658 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 659 | - |
|
| 660 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 661 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 662 | - $target = $storage->fopen($internalPath, 'w'); |
|
| 663 | - if ($target) { |
|
| 664 | - list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 665 | - fclose($target); |
|
| 666 | - fclose($data); |
|
| 667 | - |
|
| 668 | - $this->writeUpdate($storage, $internalPath); |
|
| 669 | - |
|
| 670 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 671 | - |
|
| 672 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 673 | - $this->emit_file_hooks_post($exists, $path); |
|
| 674 | - } |
|
| 675 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 676 | - return $result; |
|
| 677 | - } else { |
|
| 678 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 679 | - return false; |
|
| 680 | - } |
|
| 681 | - } else { |
|
| 682 | - return false; |
|
| 683 | - } |
|
| 684 | - } else { |
|
| 685 | - $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); |
|
| 686 | - return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * @param string $path |
|
| 692 | - * @return bool|mixed |
|
| 693 | - */ |
|
| 694 | - public function unlink($path) { |
|
| 695 | - if ($path === '' || $path === '/') { |
|
| 696 | - // do not allow deleting the root |
|
| 697 | - return false; |
|
| 698 | - } |
|
| 699 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 700 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 701 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 702 | - if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 703 | - return $this->removeMount($mount, $absolutePath); |
|
| 704 | - } |
|
| 705 | - if ($this->is_dir($path)) { |
|
| 706 | - $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 707 | - } else { |
|
| 708 | - $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 709 | - } |
|
| 710 | - if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 711 | - $storage = $mount->getStorage(); |
|
| 712 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 713 | - $storage->getUpdater()->remove($internalPath); |
|
| 714 | - return true; |
|
| 715 | - } else { |
|
| 716 | - return $result; |
|
| 717 | - } |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - /** |
|
| 721 | - * @param string $directory |
|
| 722 | - * @return bool|mixed |
|
| 723 | - */ |
|
| 724 | - public function deleteAll($directory) { |
|
| 725 | - return $this->rmdir($directory); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - /** |
|
| 729 | - * Rename/move a file or folder from the source path to target path. |
|
| 730 | - * |
|
| 731 | - * @param string $path1 source path |
|
| 732 | - * @param string $path2 target path |
|
| 733 | - * |
|
| 734 | - * @return bool|mixed |
|
| 735 | - */ |
|
| 736 | - public function rename($path1, $path2) { |
|
| 737 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 738 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 739 | - $result = false; |
|
| 740 | - if ( |
|
| 741 | - Filesystem::isValidPath($path2) |
|
| 742 | - and Filesystem::isValidPath($path1) |
|
| 743 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 744 | - ) { |
|
| 745 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 746 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 747 | - $exists = $this->file_exists($path2); |
|
| 748 | - |
|
| 749 | - if ($path1 == null or $path2 == null) { |
|
| 750 | - return false; |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 754 | - try { |
|
| 755 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 756 | - |
|
| 757 | - $run = true; |
|
| 758 | - if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 759 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 760 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 761 | - } elseif ($this->shouldEmitHooks($path1)) { |
|
| 762 | - \OC_Hook::emit( |
|
| 763 | - Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 764 | - array( |
|
| 765 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 766 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 767 | - Filesystem::signal_param_run => &$run |
|
| 768 | - ) |
|
| 769 | - ); |
|
| 770 | - } |
|
| 771 | - if ($run) { |
|
| 772 | - $this->verifyPath(dirname($path2), basename($path2)); |
|
| 773 | - |
|
| 774 | - $manager = Filesystem::getMountManager(); |
|
| 775 | - $mount1 = $this->getMount($path1); |
|
| 776 | - $mount2 = $this->getMount($path2); |
|
| 777 | - $storage1 = $mount1->getStorage(); |
|
| 778 | - $storage2 = $mount2->getStorage(); |
|
| 779 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 780 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 781 | - |
|
| 782 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 783 | - try { |
|
| 784 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 785 | - |
|
| 786 | - if ($internalPath1 === '') { |
|
| 787 | - if ($mount1 instanceof MoveableMount) { |
|
| 788 | - if ($this->isTargetAllowed($absolutePath2)) { |
|
| 789 | - /** |
|
| 790 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 791 | - */ |
|
| 792 | - $sourceMountPoint = $mount1->getMountPoint(); |
|
| 793 | - $result = $mount1->moveMount($absolutePath2); |
|
| 794 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 795 | - } else { |
|
| 796 | - $result = false; |
|
| 797 | - } |
|
| 798 | - } else { |
|
| 799 | - $result = false; |
|
| 800 | - } |
|
| 801 | - // moving a file/folder within the same mount point |
|
| 802 | - } elseif ($storage1 === $storage2) { |
|
| 803 | - if ($storage1) { |
|
| 804 | - $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 805 | - } else { |
|
| 806 | - $result = false; |
|
| 807 | - } |
|
| 808 | - // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 809 | - } else { |
|
| 810 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 814 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 815 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 816 | - } else if ($result) { |
|
| 817 | - if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 818 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 819 | - } |
|
| 820 | - } |
|
| 821 | - } catch(\Exception $e) { |
|
| 822 | - throw $e; |
|
| 823 | - } finally { |
|
| 824 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 825 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 829 | - if ($this->shouldEmitHooks()) { |
|
| 830 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 831 | - } |
|
| 832 | - } elseif ($result) { |
|
| 833 | - if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 834 | - \OC_Hook::emit( |
|
| 835 | - Filesystem::CLASSNAME, |
|
| 836 | - Filesystem::signal_post_rename, |
|
| 837 | - array( |
|
| 838 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 839 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 840 | - ) |
|
| 841 | - ); |
|
| 842 | - } |
|
| 843 | - } |
|
| 844 | - } |
|
| 845 | - } catch(\Exception $e) { |
|
| 846 | - throw $e; |
|
| 847 | - } finally { |
|
| 848 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 849 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - return $result; |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - /** |
|
| 856 | - * Copy a file/folder from the source path to target path |
|
| 857 | - * |
|
| 858 | - * @param string $path1 source path |
|
| 859 | - * @param string $path2 target path |
|
| 860 | - * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 861 | - * |
|
| 862 | - * @return bool|mixed |
|
| 863 | - */ |
|
| 864 | - public function copy($path1, $path2, $preserveMtime = false) { |
|
| 865 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 866 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 867 | - $result = false; |
|
| 868 | - if ( |
|
| 869 | - Filesystem::isValidPath($path2) |
|
| 870 | - and Filesystem::isValidPath($path1) |
|
| 871 | - and !Filesystem::isFileBlacklisted($path2) |
|
| 872 | - ) { |
|
| 873 | - $path1 = $this->getRelativePath($absolutePath1); |
|
| 874 | - $path2 = $this->getRelativePath($absolutePath2); |
|
| 875 | - |
|
| 876 | - if ($path1 == null or $path2 == null) { |
|
| 877 | - return false; |
|
| 878 | - } |
|
| 879 | - $run = true; |
|
| 880 | - |
|
| 881 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 882 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 883 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 884 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 885 | - |
|
| 886 | - try { |
|
| 887 | - |
|
| 888 | - $exists = $this->file_exists($path2); |
|
| 889 | - if ($this->shouldEmitHooks()) { |
|
| 890 | - \OC_Hook::emit( |
|
| 891 | - Filesystem::CLASSNAME, |
|
| 892 | - Filesystem::signal_copy, |
|
| 893 | - array( |
|
| 894 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 895 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 896 | - Filesystem::signal_param_run => &$run |
|
| 897 | - ) |
|
| 898 | - ); |
|
| 899 | - $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 900 | - } |
|
| 901 | - if ($run) { |
|
| 902 | - $mount1 = $this->getMount($path1); |
|
| 903 | - $mount2 = $this->getMount($path2); |
|
| 904 | - $storage1 = $mount1->getStorage(); |
|
| 905 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 906 | - $storage2 = $mount2->getStorage(); |
|
| 907 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 908 | - |
|
| 909 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 910 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 911 | - |
|
| 912 | - if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 913 | - if ($storage1) { |
|
| 914 | - $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 915 | - } else { |
|
| 916 | - $result = false; |
|
| 917 | - } |
|
| 918 | - } else { |
|
| 919 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - $this->writeUpdate($storage2, $internalPath2); |
|
| 923 | - |
|
| 924 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 925 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 926 | - |
|
| 927 | - if ($this->shouldEmitHooks() && $result !== false) { |
|
| 928 | - \OC_Hook::emit( |
|
| 929 | - Filesystem::CLASSNAME, |
|
| 930 | - Filesystem::signal_post_copy, |
|
| 931 | - array( |
|
| 932 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 933 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 934 | - ) |
|
| 935 | - ); |
|
| 936 | - $this->emit_file_hooks_post($exists, $path2); |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - } |
|
| 940 | - } catch (\Exception $e) { |
|
| 941 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 942 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 943 | - throw $e; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - $this->unlockFile($path2, $lockTypePath2); |
|
| 947 | - $this->unlockFile($path1, $lockTypePath1); |
|
| 948 | - |
|
| 949 | - } |
|
| 950 | - return $result; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - /** |
|
| 954 | - * @param string $path |
|
| 955 | - * @param string $mode 'r' or 'w' |
|
| 956 | - * @return resource |
|
| 957 | - */ |
|
| 958 | - public function fopen($path, $mode) { |
|
| 959 | - $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 960 | - $hooks = array(); |
|
| 961 | - switch ($mode) { |
|
| 962 | - case 'r': |
|
| 963 | - $hooks[] = 'read'; |
|
| 964 | - break; |
|
| 965 | - case 'r+': |
|
| 966 | - case 'w+': |
|
| 967 | - case 'x+': |
|
| 968 | - case 'a+': |
|
| 969 | - $hooks[] = 'read'; |
|
| 970 | - $hooks[] = 'write'; |
|
| 971 | - break; |
|
| 972 | - case 'w': |
|
| 973 | - case 'x': |
|
| 974 | - case 'a': |
|
| 975 | - $hooks[] = 'write'; |
|
| 976 | - break; |
|
| 977 | - default: |
|
| 978 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - if ($mode !== 'r' && $mode !== 'w') { |
|
| 982 | - \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 983 | - } |
|
| 984 | - |
|
| 985 | - return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - /** |
|
| 989 | - * @param string $path |
|
| 990 | - * @return bool|string |
|
| 991 | - * @throws \OCP\Files\InvalidPathException |
|
| 992 | - */ |
|
| 993 | - public function toTmpFile($path) { |
|
| 994 | - $this->assertPathLength($path); |
|
| 995 | - if (Filesystem::isValidPath($path)) { |
|
| 996 | - $source = $this->fopen($path, 'r'); |
|
| 997 | - if ($source) { |
|
| 998 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 999 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1000 | - file_put_contents($tmpFile, $source); |
|
| 1001 | - return $tmpFile; |
|
| 1002 | - } else { |
|
| 1003 | - return false; |
|
| 1004 | - } |
|
| 1005 | - } else { |
|
| 1006 | - return false; |
|
| 1007 | - } |
|
| 1008 | - } |
|
| 1009 | - |
|
| 1010 | - /** |
|
| 1011 | - * @param string $tmpFile |
|
| 1012 | - * @param string $path |
|
| 1013 | - * @return bool|mixed |
|
| 1014 | - * @throws \OCP\Files\InvalidPathException |
|
| 1015 | - */ |
|
| 1016 | - public function fromTmpFile($tmpFile, $path) { |
|
| 1017 | - $this->assertPathLength($path); |
|
| 1018 | - if (Filesystem::isValidPath($path)) { |
|
| 1019 | - |
|
| 1020 | - // Get directory that the file is going into |
|
| 1021 | - $filePath = dirname($path); |
|
| 1022 | - |
|
| 1023 | - // Create the directories if any |
|
| 1024 | - if (!$this->file_exists($filePath)) { |
|
| 1025 | - $result = $this->createParentDirectories($filePath); |
|
| 1026 | - if ($result === false) { |
|
| 1027 | - return false; |
|
| 1028 | - } |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - $source = fopen($tmpFile, 'r'); |
|
| 1032 | - if ($source) { |
|
| 1033 | - $result = $this->file_put_contents($path, $source); |
|
| 1034 | - // $this->file_put_contents() might have already closed |
|
| 1035 | - // the resource, so we check it, before trying to close it |
|
| 1036 | - // to avoid messages in the error log. |
|
| 1037 | - if (is_resource($source)) { |
|
| 1038 | - fclose($source); |
|
| 1039 | - } |
|
| 1040 | - unlink($tmpFile); |
|
| 1041 | - return $result; |
|
| 1042 | - } else { |
|
| 1043 | - return false; |
|
| 1044 | - } |
|
| 1045 | - } else { |
|
| 1046 | - return false; |
|
| 1047 | - } |
|
| 1048 | - } |
|
| 1049 | - |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * @param string $path |
|
| 1053 | - * @return mixed |
|
| 1054 | - * @throws \OCP\Files\InvalidPathException |
|
| 1055 | - */ |
|
| 1056 | - public function getMimeType($path) { |
|
| 1057 | - $this->assertPathLength($path); |
|
| 1058 | - return $this->basicOperation('getMimeType', $path); |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * @param string $type |
|
| 1063 | - * @param string $path |
|
| 1064 | - * @param bool $raw |
|
| 1065 | - * @return bool|null|string |
|
| 1066 | - */ |
|
| 1067 | - public function hash($type, $path, $raw = false) { |
|
| 1068 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1069 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1070 | - if (Filesystem::isValidPath($path)) { |
|
| 1071 | - $path = $this->getRelativePath($absolutePath); |
|
| 1072 | - if ($path == null) { |
|
| 1073 | - return false; |
|
| 1074 | - } |
|
| 1075 | - if ($this->shouldEmitHooks($path)) { |
|
| 1076 | - \OC_Hook::emit( |
|
| 1077 | - Filesystem::CLASSNAME, |
|
| 1078 | - Filesystem::signal_read, |
|
| 1079 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1080 | - ); |
|
| 1081 | - } |
|
| 1082 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1083 | - if ($storage) { |
|
| 1084 | - return $storage->hash($type, $internalPath, $raw); |
|
| 1085 | - } |
|
| 1086 | - } |
|
| 1087 | - return null; |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - /** |
|
| 1091 | - * @param string $path |
|
| 1092 | - * @return mixed |
|
| 1093 | - * @throws \OCP\Files\InvalidPathException |
|
| 1094 | - */ |
|
| 1095 | - public function free_space($path = '/') { |
|
| 1096 | - $this->assertPathLength($path); |
|
| 1097 | - $result = $this->basicOperation('free_space', $path); |
|
| 1098 | - if ($result === null) { |
|
| 1099 | - throw new InvalidPathException(); |
|
| 1100 | - } |
|
| 1101 | - return $result; |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - /** |
|
| 1105 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1106 | - * |
|
| 1107 | - * @param string $operation |
|
| 1108 | - * @param string $path |
|
| 1109 | - * @param array $hooks (optional) |
|
| 1110 | - * @param mixed $extraParam (optional) |
|
| 1111 | - * @return mixed |
|
| 1112 | - * @throws \Exception |
|
| 1113 | - * |
|
| 1114 | - * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1115 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1116 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1117 | - */ |
|
| 1118 | - private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1119 | - $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1120 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1121 | - if (Filesystem::isValidPath($path) |
|
| 1122 | - and !Filesystem::isFileBlacklisted($path) |
|
| 1123 | - ) { |
|
| 1124 | - $path = $this->getRelativePath($absolutePath); |
|
| 1125 | - if ($path == null) { |
|
| 1126 | - return false; |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1130 | - // always a shared lock during pre-hooks so the hook can read the file |
|
| 1131 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1132 | - } |
|
| 1133 | - |
|
| 1134 | - $run = $this->runHooks($hooks, $path); |
|
| 1135 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1136 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1137 | - if ($run and $storage) { |
|
| 1138 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1139 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1140 | - } |
|
| 1141 | - try { |
|
| 1142 | - if (!is_null($extraParam)) { |
|
| 1143 | - $result = $storage->$operation($internalPath, $extraParam); |
|
| 1144 | - } else { |
|
| 1145 | - $result = $storage->$operation($internalPath); |
|
| 1146 | - } |
|
| 1147 | - } catch (\Exception $e) { |
|
| 1148 | - if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1149 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1150 | - } else if (in_array('read', $hooks)) { |
|
| 1151 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1152 | - } |
|
| 1153 | - throw $e; |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - if ($result && in_array('delete', $hooks) and $result) { |
|
| 1157 | - $this->removeUpdate($storage, $internalPath); |
|
| 1158 | - } |
|
| 1159 | - if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1160 | - $this->writeUpdate($storage, $internalPath); |
|
| 1161 | - } |
|
| 1162 | - if ($result && in_array('touch', $hooks)) { |
|
| 1163 | - $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1167 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - $unlockLater = false; |
|
| 1171 | - if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1172 | - $unlockLater = true; |
|
| 1173 | - // make sure our unlocking callback will still be called if connection is aborted |
|
| 1174 | - ignore_user_abort(true); |
|
| 1175 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1176 | - if (in_array('write', $hooks)) { |
|
| 1177 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1178 | - } else if (in_array('read', $hooks)) { |
|
| 1179 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1180 | - } |
|
| 1181 | - }); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1185 | - if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1186 | - $this->runHooks($hooks, $path, true); |
|
| 1187 | - } |
|
| 1188 | - } |
|
| 1189 | - |
|
| 1190 | - if (!$unlockLater |
|
| 1191 | - && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1192 | - ) { |
|
| 1193 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1194 | - } |
|
| 1195 | - return $result; |
|
| 1196 | - } else { |
|
| 1197 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1198 | - } |
|
| 1199 | - } |
|
| 1200 | - return null; |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - /** |
|
| 1204 | - * get the path relative to the default root for hook usage |
|
| 1205 | - * |
|
| 1206 | - * @param string $path |
|
| 1207 | - * @return string |
|
| 1208 | - */ |
|
| 1209 | - private function getHookPath($path) { |
|
| 1210 | - if (!Filesystem::getView()) { |
|
| 1211 | - return $path; |
|
| 1212 | - } |
|
| 1213 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1214 | - } |
|
| 1215 | - |
|
| 1216 | - private function shouldEmitHooks($path = '') { |
|
| 1217 | - if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1218 | - return false; |
|
| 1219 | - } |
|
| 1220 | - if (!Filesystem::$loaded) { |
|
| 1221 | - return false; |
|
| 1222 | - } |
|
| 1223 | - $defaultRoot = Filesystem::getRoot(); |
|
| 1224 | - if ($defaultRoot === null) { |
|
| 1225 | - return false; |
|
| 1226 | - } |
|
| 1227 | - if ($this->fakeRoot === $defaultRoot) { |
|
| 1228 | - return true; |
|
| 1229 | - } |
|
| 1230 | - $fullPath = $this->getAbsolutePath($path); |
|
| 1231 | - |
|
| 1232 | - if ($fullPath === $defaultRoot) { |
|
| 1233 | - return true; |
|
| 1234 | - } |
|
| 1235 | - |
|
| 1236 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - /** |
|
| 1240 | - * @param string[] $hooks |
|
| 1241 | - * @param string $path |
|
| 1242 | - * @param bool $post |
|
| 1243 | - * @return bool |
|
| 1244 | - */ |
|
| 1245 | - private function runHooks($hooks, $path, $post = false) { |
|
| 1246 | - $relativePath = $path; |
|
| 1247 | - $path = $this->getHookPath($path); |
|
| 1248 | - $prefix = ($post) ? 'post_' : ''; |
|
| 1249 | - $run = true; |
|
| 1250 | - if ($this->shouldEmitHooks($relativePath)) { |
|
| 1251 | - foreach ($hooks as $hook) { |
|
| 1252 | - if ($hook != 'read') { |
|
| 1253 | - \OC_Hook::emit( |
|
| 1254 | - Filesystem::CLASSNAME, |
|
| 1255 | - $prefix . $hook, |
|
| 1256 | - array( |
|
| 1257 | - Filesystem::signal_param_run => &$run, |
|
| 1258 | - Filesystem::signal_param_path => $path |
|
| 1259 | - ) |
|
| 1260 | - ); |
|
| 1261 | - } elseif (!$post) { |
|
| 1262 | - \OC_Hook::emit( |
|
| 1263 | - Filesystem::CLASSNAME, |
|
| 1264 | - $prefix . $hook, |
|
| 1265 | - array( |
|
| 1266 | - Filesystem::signal_param_path => $path |
|
| 1267 | - ) |
|
| 1268 | - ); |
|
| 1269 | - } |
|
| 1270 | - } |
|
| 1271 | - } |
|
| 1272 | - return $run; |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - /** |
|
| 1276 | - * check if a file or folder has been updated since $time |
|
| 1277 | - * |
|
| 1278 | - * @param string $path |
|
| 1279 | - * @param int $time |
|
| 1280 | - * @return bool |
|
| 1281 | - */ |
|
| 1282 | - public function hasUpdated($path, $time) { |
|
| 1283 | - return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1284 | - } |
|
| 1285 | - |
|
| 1286 | - /** |
|
| 1287 | - * @param string $ownerId |
|
| 1288 | - * @return \OC\User\User |
|
| 1289 | - */ |
|
| 1290 | - private function getUserObjectForOwner($ownerId) { |
|
| 1291 | - $owner = $this->userManager->get($ownerId); |
|
| 1292 | - if ($owner instanceof IUser) { |
|
| 1293 | - return $owner; |
|
| 1294 | - } else { |
|
| 1295 | - return new User($ownerId, null); |
|
| 1296 | - } |
|
| 1297 | - } |
|
| 1298 | - |
|
| 1299 | - /** |
|
| 1300 | - * Get file info from cache |
|
| 1301 | - * |
|
| 1302 | - * If the file is not in cached it will be scanned |
|
| 1303 | - * If the file has changed on storage the cache will be updated |
|
| 1304 | - * |
|
| 1305 | - * @param \OC\Files\Storage\Storage $storage |
|
| 1306 | - * @param string $internalPath |
|
| 1307 | - * @param string $relativePath |
|
| 1308 | - * @return ICacheEntry|bool |
|
| 1309 | - */ |
|
| 1310 | - private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1311 | - $cache = $storage->getCache($internalPath); |
|
| 1312 | - $data = $cache->get($internalPath); |
|
| 1313 | - $watcher = $storage->getWatcher($internalPath); |
|
| 1314 | - |
|
| 1315 | - try { |
|
| 1316 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1317 | - if (!$data || $data['size'] === -1) { |
|
| 1318 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1319 | - if (!$storage->file_exists($internalPath)) { |
|
| 1320 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1321 | - return false; |
|
| 1322 | - } |
|
| 1323 | - $scanner = $storage->getScanner($internalPath); |
|
| 1324 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1325 | - $data = $cache->get($internalPath); |
|
| 1326 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1327 | - } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1328 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1329 | - $watcher->update($internalPath, $data); |
|
| 1330 | - $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1331 | - $data = $cache->get($internalPath); |
|
| 1332 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1333 | - } |
|
| 1334 | - } catch (LockedException $e) { |
|
| 1335 | - // if the file is locked we just use the old cache info |
|
| 1336 | - } |
|
| 1337 | - |
|
| 1338 | - return $data; |
|
| 1339 | - } |
|
| 1340 | - |
|
| 1341 | - /** |
|
| 1342 | - * get the filesystem info |
|
| 1343 | - * |
|
| 1344 | - * @param string $path |
|
| 1345 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1346 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1347 | - * defaults to true |
|
| 1348 | - * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1349 | - */ |
|
| 1350 | - public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1351 | - $this->assertPathLength($path); |
|
| 1352 | - if (!Filesystem::isValidPath($path)) { |
|
| 1353 | - return false; |
|
| 1354 | - } |
|
| 1355 | - if (Cache\Scanner::isPartialFile($path)) { |
|
| 1356 | - return $this->getPartFileInfo($path); |
|
| 1357 | - } |
|
| 1358 | - $relativePath = $path; |
|
| 1359 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1360 | - |
|
| 1361 | - $mount = Filesystem::getMountManager()->find($path); |
|
| 1362 | - if (!$mount) { |
|
| 1363 | - return false; |
|
| 1364 | - } |
|
| 1365 | - $storage = $mount->getStorage(); |
|
| 1366 | - $internalPath = $mount->getInternalPath($path); |
|
| 1367 | - if ($storage) { |
|
| 1368 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1369 | - |
|
| 1370 | - if (!$data instanceof ICacheEntry) { |
|
| 1371 | - return false; |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1375 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1376 | - } |
|
| 1377 | - |
|
| 1378 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1379 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1380 | - |
|
| 1381 | - if ($data and isset($data['fileid'])) { |
|
| 1382 | - if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1383 | - //add the sizes of other mount points to the folder |
|
| 1384 | - $extOnly = ($includeMountPoints === 'ext'); |
|
| 1385 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1386 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1387 | - $subStorage = $mount->getStorage(); |
|
| 1388 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1389 | - })); |
|
| 1390 | - } |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - return $info; |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - return false; |
|
| 1397 | - } |
|
| 1398 | - |
|
| 1399 | - /** |
|
| 1400 | - * get the content of a directory |
|
| 1401 | - * |
|
| 1402 | - * @param string $directory path under datadirectory |
|
| 1403 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1404 | - * @return FileInfo[] |
|
| 1405 | - */ |
|
| 1406 | - public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1407 | - $this->assertPathLength($directory); |
|
| 1408 | - if (!Filesystem::isValidPath($directory)) { |
|
| 1409 | - return []; |
|
| 1410 | - } |
|
| 1411 | - $path = $this->getAbsolutePath($directory); |
|
| 1412 | - $path = Filesystem::normalizePath($path); |
|
| 1413 | - $mount = $this->getMount($directory); |
|
| 1414 | - if (!$mount) { |
|
| 1415 | - return []; |
|
| 1416 | - } |
|
| 1417 | - $storage = $mount->getStorage(); |
|
| 1418 | - $internalPath = $mount->getInternalPath($path); |
|
| 1419 | - if ($storage) { |
|
| 1420 | - $cache = $storage->getCache($internalPath); |
|
| 1421 | - $user = \OC_User::getUser(); |
|
| 1422 | - |
|
| 1423 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1424 | - |
|
| 1425 | - if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1426 | - return []; |
|
| 1427 | - } |
|
| 1428 | - |
|
| 1429 | - $folderId = $data['fileid']; |
|
| 1430 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1431 | - |
|
| 1432 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1433 | - /** |
|
| 1434 | - * @var \OC\Files\FileInfo[] $files |
|
| 1435 | - */ |
|
| 1436 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1437 | - if ($sharingDisabled) { |
|
| 1438 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1439 | - } |
|
| 1440 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1441 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1442 | - }, $contents); |
|
| 1443 | - |
|
| 1444 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1445 | - $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1446 | - $dirLength = strlen($path); |
|
| 1447 | - foreach ($mounts as $mount) { |
|
| 1448 | - $mountPoint = $mount->getMountPoint(); |
|
| 1449 | - $subStorage = $mount->getStorage(); |
|
| 1450 | - if ($subStorage) { |
|
| 1451 | - $subCache = $subStorage->getCache(''); |
|
| 1452 | - |
|
| 1453 | - $rootEntry = $subCache->get(''); |
|
| 1454 | - if (!$rootEntry) { |
|
| 1455 | - $subScanner = $subStorage->getScanner(''); |
|
| 1456 | - try { |
|
| 1457 | - $subScanner->scanFile(''); |
|
| 1458 | - } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1459 | - continue; |
|
| 1460 | - } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1461 | - continue; |
|
| 1462 | - } catch (\Exception $e) { |
|
| 1463 | - // sometimes when the storage is not available it can be any exception |
|
| 1464 | - \OC::$server->getLogger()->logException($e, [ |
|
| 1465 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1466 | - 'level' => \OCP\Util::ERROR, |
|
| 1467 | - 'app' => 'lib', |
|
| 1468 | - ]); |
|
| 1469 | - continue; |
|
| 1470 | - } |
|
| 1471 | - $rootEntry = $subCache->get(''); |
|
| 1472 | - } |
|
| 1473 | - |
|
| 1474 | - if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1475 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1476 | - if ($pos = strpos($relativePath, '/')) { |
|
| 1477 | - //mountpoint inside subfolder add size to the correct folder |
|
| 1478 | - $entryName = substr($relativePath, 0, $pos); |
|
| 1479 | - foreach ($files as &$entry) { |
|
| 1480 | - if ($entry->getName() === $entryName) { |
|
| 1481 | - $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1482 | - } |
|
| 1483 | - } |
|
| 1484 | - } else { //mountpoint in this folder, add an entry for it |
|
| 1485 | - $rootEntry['name'] = $relativePath; |
|
| 1486 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1487 | - $permissions = $rootEntry['permissions']; |
|
| 1488 | - // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1489 | - // for shared files/folders we use the permissions given by the owner |
|
| 1490 | - if ($mount instanceof MoveableMount) { |
|
| 1491 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1492 | - } else { |
|
| 1493 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1494 | - } |
|
| 1495 | - |
|
| 1496 | - //remove any existing entry with the same name |
|
| 1497 | - foreach ($files as $i => $file) { |
|
| 1498 | - if ($file['name'] === $rootEntry['name']) { |
|
| 1499 | - unset($files[$i]); |
|
| 1500 | - break; |
|
| 1501 | - } |
|
| 1502 | - } |
|
| 1503 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1504 | - |
|
| 1505 | - // if sharing was disabled for the user we remove the share permissions |
|
| 1506 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1507 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1508 | - } |
|
| 1509 | - |
|
| 1510 | - $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1511 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1512 | - } |
|
| 1513 | - } |
|
| 1514 | - } |
|
| 1515 | - } |
|
| 1516 | - |
|
| 1517 | - if ($mimetype_filter) { |
|
| 1518 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1519 | - if (strpos($mimetype_filter, '/')) { |
|
| 1520 | - return $file->getMimetype() === $mimetype_filter; |
|
| 1521 | - } else { |
|
| 1522 | - return $file->getMimePart() === $mimetype_filter; |
|
| 1523 | - } |
|
| 1524 | - }); |
|
| 1525 | - } |
|
| 1526 | - |
|
| 1527 | - return $files; |
|
| 1528 | - } else { |
|
| 1529 | - return []; |
|
| 1530 | - } |
|
| 1531 | - } |
|
| 1532 | - |
|
| 1533 | - /** |
|
| 1534 | - * change file metadata |
|
| 1535 | - * |
|
| 1536 | - * @param string $path |
|
| 1537 | - * @param array|\OCP\Files\FileInfo $data |
|
| 1538 | - * @return int |
|
| 1539 | - * |
|
| 1540 | - * returns the fileid of the updated file |
|
| 1541 | - */ |
|
| 1542 | - public function putFileInfo($path, $data) { |
|
| 1543 | - $this->assertPathLength($path); |
|
| 1544 | - if ($data instanceof FileInfo) { |
|
| 1545 | - $data = $data->getData(); |
|
| 1546 | - } |
|
| 1547 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1548 | - /** |
|
| 1549 | - * @var \OC\Files\Storage\Storage $storage |
|
| 1550 | - * @var string $internalPath |
|
| 1551 | - */ |
|
| 1552 | - list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1553 | - if ($storage) { |
|
| 1554 | - $cache = $storage->getCache($path); |
|
| 1555 | - |
|
| 1556 | - if (!$cache->inCache($internalPath)) { |
|
| 1557 | - $scanner = $storage->getScanner($internalPath); |
|
| 1558 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1559 | - } |
|
| 1560 | - |
|
| 1561 | - return $cache->put($internalPath, $data); |
|
| 1562 | - } else { |
|
| 1563 | - return -1; |
|
| 1564 | - } |
|
| 1565 | - } |
|
| 1566 | - |
|
| 1567 | - /** |
|
| 1568 | - * search for files with the name matching $query |
|
| 1569 | - * |
|
| 1570 | - * @param string $query |
|
| 1571 | - * @return FileInfo[] |
|
| 1572 | - */ |
|
| 1573 | - public function search($query) { |
|
| 1574 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1575 | - } |
|
| 1576 | - |
|
| 1577 | - /** |
|
| 1578 | - * search for files with the name matching $query |
|
| 1579 | - * |
|
| 1580 | - * @param string $query |
|
| 1581 | - * @return FileInfo[] |
|
| 1582 | - */ |
|
| 1583 | - public function searchRaw($query) { |
|
| 1584 | - return $this->searchCommon('search', array($query)); |
|
| 1585 | - } |
|
| 1586 | - |
|
| 1587 | - /** |
|
| 1588 | - * search for files by mimetype |
|
| 1589 | - * |
|
| 1590 | - * @param string $mimetype |
|
| 1591 | - * @return FileInfo[] |
|
| 1592 | - */ |
|
| 1593 | - public function searchByMime($mimetype) { |
|
| 1594 | - return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - /** |
|
| 1598 | - * search for files by tag |
|
| 1599 | - * |
|
| 1600 | - * @param string|int $tag name or tag id |
|
| 1601 | - * @param string $userId owner of the tags |
|
| 1602 | - * @return FileInfo[] |
|
| 1603 | - */ |
|
| 1604 | - public function searchByTag($tag, $userId) { |
|
| 1605 | - return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1606 | - } |
|
| 1607 | - |
|
| 1608 | - /** |
|
| 1609 | - * @param string $method cache method |
|
| 1610 | - * @param array $args |
|
| 1611 | - * @return FileInfo[] |
|
| 1612 | - */ |
|
| 1613 | - private function searchCommon($method, $args) { |
|
| 1614 | - $files = array(); |
|
| 1615 | - $rootLength = strlen($this->fakeRoot); |
|
| 1616 | - |
|
| 1617 | - $mount = $this->getMount(''); |
|
| 1618 | - $mountPoint = $mount->getMountPoint(); |
|
| 1619 | - $storage = $mount->getStorage(); |
|
| 1620 | - if ($storage) { |
|
| 1621 | - $cache = $storage->getCache(''); |
|
| 1622 | - |
|
| 1623 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1624 | - foreach ($results as $result) { |
|
| 1625 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1626 | - $internalPath = $result['path']; |
|
| 1627 | - $path = $mountPoint . $result['path']; |
|
| 1628 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1629 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1630 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1631 | - } |
|
| 1632 | - } |
|
| 1633 | - |
|
| 1634 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1635 | - foreach ($mounts as $mount) { |
|
| 1636 | - $mountPoint = $mount->getMountPoint(); |
|
| 1637 | - $storage = $mount->getStorage(); |
|
| 1638 | - if ($storage) { |
|
| 1639 | - $cache = $storage->getCache(''); |
|
| 1640 | - |
|
| 1641 | - $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1642 | - $results = call_user_func_array(array($cache, $method), $args); |
|
| 1643 | - if ($results) { |
|
| 1644 | - foreach ($results as $result) { |
|
| 1645 | - $internalPath = $result['path']; |
|
| 1646 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1647 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1648 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1649 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1650 | - } |
|
| 1651 | - } |
|
| 1652 | - } |
|
| 1653 | - } |
|
| 1654 | - } |
|
| 1655 | - return $files; |
|
| 1656 | - } |
|
| 1657 | - |
|
| 1658 | - /** |
|
| 1659 | - * Get the owner for a file or folder |
|
| 1660 | - * |
|
| 1661 | - * @param string $path |
|
| 1662 | - * @return string the user id of the owner |
|
| 1663 | - * @throws NotFoundException |
|
| 1664 | - */ |
|
| 1665 | - public function getOwner($path) { |
|
| 1666 | - $info = $this->getFileInfo($path); |
|
| 1667 | - if (!$info) { |
|
| 1668 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1669 | - } |
|
| 1670 | - return $info->getOwner()->getUID(); |
|
| 1671 | - } |
|
| 1672 | - |
|
| 1673 | - /** |
|
| 1674 | - * get the ETag for a file or folder |
|
| 1675 | - * |
|
| 1676 | - * @param string $path |
|
| 1677 | - * @return string |
|
| 1678 | - */ |
|
| 1679 | - public function getETag($path) { |
|
| 1680 | - /** |
|
| 1681 | - * @var Storage\Storage $storage |
|
| 1682 | - * @var string $internalPath |
|
| 1683 | - */ |
|
| 1684 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1685 | - if ($storage) { |
|
| 1686 | - return $storage->getETag($internalPath); |
|
| 1687 | - } else { |
|
| 1688 | - return null; |
|
| 1689 | - } |
|
| 1690 | - } |
|
| 1691 | - |
|
| 1692 | - /** |
|
| 1693 | - * Get the path of a file by id, relative to the view |
|
| 1694 | - * |
|
| 1695 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1696 | - * |
|
| 1697 | - * @param int $id |
|
| 1698 | - * @throws NotFoundException |
|
| 1699 | - * @return string |
|
| 1700 | - */ |
|
| 1701 | - public function getPath($id) { |
|
| 1702 | - $id = (int)$id; |
|
| 1703 | - $manager = Filesystem::getMountManager(); |
|
| 1704 | - $mounts = $manager->findIn($this->fakeRoot); |
|
| 1705 | - $mounts[] = $manager->find($this->fakeRoot); |
|
| 1706 | - // reverse the array so we start with the storage this view is in |
|
| 1707 | - // which is the most likely to contain the file we're looking for |
|
| 1708 | - $mounts = array_reverse($mounts); |
|
| 1709 | - foreach ($mounts as $mount) { |
|
| 1710 | - /** |
|
| 1711 | - * @var \OC\Files\Mount\MountPoint $mount |
|
| 1712 | - */ |
|
| 1713 | - if ($mount->getStorage()) { |
|
| 1714 | - $cache = $mount->getStorage()->getCache(); |
|
| 1715 | - $internalPath = $cache->getPathById($id); |
|
| 1716 | - if (is_string($internalPath)) { |
|
| 1717 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1718 | - if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1719 | - return $path; |
|
| 1720 | - } |
|
| 1721 | - } |
|
| 1722 | - } |
|
| 1723 | - } |
|
| 1724 | - throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1725 | - } |
|
| 1726 | - |
|
| 1727 | - /** |
|
| 1728 | - * @param string $path |
|
| 1729 | - * @throws InvalidPathException |
|
| 1730 | - */ |
|
| 1731 | - private function assertPathLength($path) { |
|
| 1732 | - $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1733 | - // Check for the string length - performed using isset() instead of strlen() |
|
| 1734 | - // because isset() is about 5x-40x faster. |
|
| 1735 | - if (isset($path[$maxLen])) { |
|
| 1736 | - $pathLen = strlen($path); |
|
| 1737 | - throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1738 | - } |
|
| 1739 | - } |
|
| 1740 | - |
|
| 1741 | - /** |
|
| 1742 | - * check if it is allowed to move a mount point to a given target. |
|
| 1743 | - * It is not allowed to move a mount point into a different mount point or |
|
| 1744 | - * into an already shared folder |
|
| 1745 | - * |
|
| 1746 | - * @param string $target path |
|
| 1747 | - * @return boolean |
|
| 1748 | - */ |
|
| 1749 | - private function isTargetAllowed($target) { |
|
| 1750 | - |
|
| 1751 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1752 | - if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1753 | - \OCP\Util::writeLog('files', |
|
| 1754 | - 'It is not allowed to move one mount point into another one', |
|
| 1755 | - \OCP\Util::DEBUG); |
|
| 1756 | - return false; |
|
| 1757 | - } |
|
| 1758 | - |
|
| 1759 | - // note: cannot use the view because the target is already locked |
|
| 1760 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1761 | - if ($fileId === -1) { |
|
| 1762 | - // target might not exist, need to check parent instead |
|
| 1763 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1764 | - } |
|
| 1765 | - |
|
| 1766 | - // check if any of the parents were shared by the current owner (include collections) |
|
| 1767 | - $shares = \OCP\Share::getItemShared( |
|
| 1768 | - 'folder', |
|
| 1769 | - $fileId, |
|
| 1770 | - \OCP\Share::FORMAT_NONE, |
|
| 1771 | - null, |
|
| 1772 | - true |
|
| 1773 | - ); |
|
| 1774 | - |
|
| 1775 | - if (count($shares) > 0) { |
|
| 1776 | - \OCP\Util::writeLog('files', |
|
| 1777 | - 'It is not allowed to move one mount point into a shared folder', |
|
| 1778 | - \OCP\Util::DEBUG); |
|
| 1779 | - return false; |
|
| 1780 | - } |
|
| 1781 | - |
|
| 1782 | - return true; |
|
| 1783 | - } |
|
| 1784 | - |
|
| 1785 | - /** |
|
| 1786 | - * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1787 | - * |
|
| 1788 | - * @param string $path |
|
| 1789 | - * @return \OCP\Files\FileInfo |
|
| 1790 | - */ |
|
| 1791 | - private function getPartFileInfo($path) { |
|
| 1792 | - $mount = $this->getMount($path); |
|
| 1793 | - $storage = $mount->getStorage(); |
|
| 1794 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1795 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1796 | - return new FileInfo( |
|
| 1797 | - $this->getAbsolutePath($path), |
|
| 1798 | - $storage, |
|
| 1799 | - $internalPath, |
|
| 1800 | - [ |
|
| 1801 | - 'fileid' => null, |
|
| 1802 | - 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1803 | - 'name' => basename($path), |
|
| 1804 | - 'etag' => null, |
|
| 1805 | - 'size' => $storage->filesize($internalPath), |
|
| 1806 | - 'mtime' => $storage->filemtime($internalPath), |
|
| 1807 | - 'encrypted' => false, |
|
| 1808 | - 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1809 | - ], |
|
| 1810 | - $mount, |
|
| 1811 | - $owner |
|
| 1812 | - ); |
|
| 1813 | - } |
|
| 1814 | - |
|
| 1815 | - /** |
|
| 1816 | - * @param string $path |
|
| 1817 | - * @param string $fileName |
|
| 1818 | - * @throws InvalidPathException |
|
| 1819 | - */ |
|
| 1820 | - public function verifyPath($path, $fileName) { |
|
| 1821 | - try { |
|
| 1822 | - /** @type \OCP\Files\Storage $storage */ |
|
| 1823 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1824 | - $storage->verifyPath($internalPath, $fileName); |
|
| 1825 | - } catch (ReservedWordException $ex) { |
|
| 1826 | - $l = \OC::$server->getL10N('lib'); |
|
| 1827 | - throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1828 | - } catch (InvalidCharacterInPathException $ex) { |
|
| 1829 | - $l = \OC::$server->getL10N('lib'); |
|
| 1830 | - throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1831 | - } catch (FileNameTooLongException $ex) { |
|
| 1832 | - $l = \OC::$server->getL10N('lib'); |
|
| 1833 | - throw new InvalidPathException($l->t('File name is too long')); |
|
| 1834 | - } catch (InvalidDirectoryException $ex) { |
|
| 1835 | - $l = \OC::$server->getL10N('lib'); |
|
| 1836 | - throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1837 | - } catch (EmptyFileNameException $ex) { |
|
| 1838 | - $l = \OC::$server->getL10N('lib'); |
|
| 1839 | - throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1840 | - } |
|
| 1841 | - } |
|
| 1842 | - |
|
| 1843 | - /** |
|
| 1844 | - * get all parent folders of $path |
|
| 1845 | - * |
|
| 1846 | - * @param string $path |
|
| 1847 | - * @return string[] |
|
| 1848 | - */ |
|
| 1849 | - private function getParents($path) { |
|
| 1850 | - $path = trim($path, '/'); |
|
| 1851 | - if (!$path) { |
|
| 1852 | - return []; |
|
| 1853 | - } |
|
| 1854 | - |
|
| 1855 | - $parts = explode('/', $path); |
|
| 1856 | - |
|
| 1857 | - // remove the single file |
|
| 1858 | - array_pop($parts); |
|
| 1859 | - $result = array('/'); |
|
| 1860 | - $resultPath = ''; |
|
| 1861 | - foreach ($parts as $part) { |
|
| 1862 | - if ($part) { |
|
| 1863 | - $resultPath .= '/' . $part; |
|
| 1864 | - $result[] = $resultPath; |
|
| 1865 | - } |
|
| 1866 | - } |
|
| 1867 | - return $result; |
|
| 1868 | - } |
|
| 1869 | - |
|
| 1870 | - /** |
|
| 1871 | - * Returns the mount point for which to lock |
|
| 1872 | - * |
|
| 1873 | - * @param string $absolutePath absolute path |
|
| 1874 | - * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1875 | - * is mounted directly on the given path, false otherwise |
|
| 1876 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1877 | - */ |
|
| 1878 | - private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1879 | - $results = []; |
|
| 1880 | - $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1881 | - if (!$mount) { |
|
| 1882 | - return $results; |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - if ($useParentMount) { |
|
| 1886 | - // find out if something is mounted directly on the path |
|
| 1887 | - $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1888 | - if ($internalPath === '') { |
|
| 1889 | - // resolve the parent mount instead |
|
| 1890 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1891 | - } |
|
| 1892 | - } |
|
| 1893 | - |
|
| 1894 | - return $mount; |
|
| 1895 | - } |
|
| 1896 | - |
|
| 1897 | - /** |
|
| 1898 | - * Lock the given path |
|
| 1899 | - * |
|
| 1900 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1901 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1902 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1903 | - * |
|
| 1904 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1905 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1906 | - */ |
|
| 1907 | - private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1908 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1909 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1910 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1911 | - return false; |
|
| 1912 | - } |
|
| 1913 | - |
|
| 1914 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1915 | - if ($mount) { |
|
| 1916 | - try { |
|
| 1917 | - $storage = $mount->getStorage(); |
|
| 1918 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1919 | - $storage->acquireLock( |
|
| 1920 | - $mount->getInternalPath($absolutePath), |
|
| 1921 | - $type, |
|
| 1922 | - $this->lockingProvider |
|
| 1923 | - ); |
|
| 1924 | - } |
|
| 1925 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1926 | - // rethrow with the a human-readable path |
|
| 1927 | - throw new \OCP\Lock\LockedException( |
|
| 1928 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1929 | - $e |
|
| 1930 | - ); |
|
| 1931 | - } |
|
| 1932 | - } |
|
| 1933 | - |
|
| 1934 | - return true; |
|
| 1935 | - } |
|
| 1936 | - |
|
| 1937 | - /** |
|
| 1938 | - * Change the lock type |
|
| 1939 | - * |
|
| 1940 | - * @param string $path the path of the file to lock, relative to the view |
|
| 1941 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1942 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1943 | - * |
|
| 1944 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1945 | - * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1946 | - */ |
|
| 1947 | - public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1948 | - $path = Filesystem::normalizePath($path); |
|
| 1949 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1950 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1951 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1952 | - return false; |
|
| 1953 | - } |
|
| 1954 | - |
|
| 1955 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1956 | - if ($mount) { |
|
| 1957 | - try { |
|
| 1958 | - $storage = $mount->getStorage(); |
|
| 1959 | - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1960 | - $storage->changeLock( |
|
| 1961 | - $mount->getInternalPath($absolutePath), |
|
| 1962 | - $type, |
|
| 1963 | - $this->lockingProvider |
|
| 1964 | - ); |
|
| 1965 | - } |
|
| 1966 | - } catch (\OCP\Lock\LockedException $e) { |
|
| 1967 | - try { |
|
| 1968 | - // rethrow with the a human-readable path |
|
| 1969 | - throw new \OCP\Lock\LockedException( |
|
| 1970 | - $this->getPathRelativeToFiles($absolutePath), |
|
| 1971 | - $e |
|
| 1972 | - ); |
|
| 1973 | - } catch (\InvalidArgumentException $e) { |
|
| 1974 | - throw new \OCP\Lock\LockedException( |
|
| 1975 | - $absolutePath, |
|
| 1976 | - $e |
|
| 1977 | - ); |
|
| 1978 | - } |
|
| 1979 | - } |
|
| 1980 | - } |
|
| 1981 | - |
|
| 1982 | - return true; |
|
| 1983 | - } |
|
| 1984 | - |
|
| 1985 | - /** |
|
| 1986 | - * Unlock the given path |
|
| 1987 | - * |
|
| 1988 | - * @param string $path the path of the file to unlock, relative to the view |
|
| 1989 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1990 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1991 | - * |
|
| 1992 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 1993 | - */ |
|
| 1994 | - private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 1995 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 1996 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1997 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 1998 | - return false; |
|
| 1999 | - } |
|
| 2000 | - |
|
| 2001 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2002 | - if ($mount) { |
|
| 2003 | - $storage = $mount->getStorage(); |
|
| 2004 | - if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2005 | - $storage->releaseLock( |
|
| 2006 | - $mount->getInternalPath($absolutePath), |
|
| 2007 | - $type, |
|
| 2008 | - $this->lockingProvider |
|
| 2009 | - ); |
|
| 2010 | - } |
|
| 2011 | - } |
|
| 2012 | - |
|
| 2013 | - return true; |
|
| 2014 | - } |
|
| 2015 | - |
|
| 2016 | - /** |
|
| 2017 | - * Lock a path and all its parents up to the root of the view |
|
| 2018 | - * |
|
| 2019 | - * @param string $path the path of the file to lock relative to the view |
|
| 2020 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2021 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2022 | - * |
|
| 2023 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2024 | - */ |
|
| 2025 | - public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2026 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2027 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2028 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2029 | - return false; |
|
| 2030 | - } |
|
| 2031 | - |
|
| 2032 | - $this->lockPath($path, $type, $lockMountPoint); |
|
| 2033 | - |
|
| 2034 | - $parents = $this->getParents($path); |
|
| 2035 | - foreach ($parents as $parent) { |
|
| 2036 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2037 | - } |
|
| 2038 | - |
|
| 2039 | - return true; |
|
| 2040 | - } |
|
| 2041 | - |
|
| 2042 | - /** |
|
| 2043 | - * Unlock a path and all its parents up to the root of the view |
|
| 2044 | - * |
|
| 2045 | - * @param string $path the path of the file to lock relative to the view |
|
| 2046 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2047 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2048 | - * |
|
| 2049 | - * @return bool False if the path is excluded from locking, true otherwise |
|
| 2050 | - */ |
|
| 2051 | - public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2052 | - $absolutePath = $this->getAbsolutePath($path); |
|
| 2053 | - $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2054 | - if (!$this->shouldLockFile($absolutePath)) { |
|
| 2055 | - return false; |
|
| 2056 | - } |
|
| 2057 | - |
|
| 2058 | - $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2059 | - |
|
| 2060 | - $parents = $this->getParents($path); |
|
| 2061 | - foreach ($parents as $parent) { |
|
| 2062 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2063 | - } |
|
| 2064 | - |
|
| 2065 | - return true; |
|
| 2066 | - } |
|
| 2067 | - |
|
| 2068 | - /** |
|
| 2069 | - * Only lock files in data/user/files/ |
|
| 2070 | - * |
|
| 2071 | - * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2072 | - * @return bool |
|
| 2073 | - */ |
|
| 2074 | - protected function shouldLockFile($path) { |
|
| 2075 | - $path = Filesystem::normalizePath($path); |
|
| 2076 | - |
|
| 2077 | - $pathSegments = explode('/', $path); |
|
| 2078 | - if (isset($pathSegments[2])) { |
|
| 2079 | - // E.g.: /username/files/path-to-file |
|
| 2080 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2081 | - } |
|
| 2082 | - |
|
| 2083 | - return strpos($path, '/appdata_') !== 0; |
|
| 2084 | - } |
|
| 2085 | - |
|
| 2086 | - /** |
|
| 2087 | - * Shortens the given absolute path to be relative to |
|
| 2088 | - * "$user/files". |
|
| 2089 | - * |
|
| 2090 | - * @param string $absolutePath absolute path which is under "files" |
|
| 2091 | - * |
|
| 2092 | - * @return string path relative to "files" with trimmed slashes or null |
|
| 2093 | - * if the path was NOT relative to files |
|
| 2094 | - * |
|
| 2095 | - * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2096 | - * @since 8.1.0 |
|
| 2097 | - */ |
|
| 2098 | - public function getPathRelativeToFiles($absolutePath) { |
|
| 2099 | - $path = Filesystem::normalizePath($absolutePath); |
|
| 2100 | - $parts = explode('/', trim($path, '/'), 3); |
|
| 2101 | - // "$user", "files", "path/to/dir" |
|
| 2102 | - if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2103 | - $this->logger->error( |
|
| 2104 | - '$absolutePath must be relative to "files", value is "%s"', |
|
| 2105 | - [ |
|
| 2106 | - $absolutePath |
|
| 2107 | - ] |
|
| 2108 | - ); |
|
| 2109 | - throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2110 | - } |
|
| 2111 | - if (isset($parts[2])) { |
|
| 2112 | - return $parts[2]; |
|
| 2113 | - } |
|
| 2114 | - return ''; |
|
| 2115 | - } |
|
| 2116 | - |
|
| 2117 | - /** |
|
| 2118 | - * @param string $filename |
|
| 2119 | - * @return array |
|
| 2120 | - * @throws \OC\User\NoUserException |
|
| 2121 | - * @throws NotFoundException |
|
| 2122 | - */ |
|
| 2123 | - public function getUidAndFilename($filename) { |
|
| 2124 | - $info = $this->getFileInfo($filename); |
|
| 2125 | - if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2126 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2127 | - } |
|
| 2128 | - $uid = $info->getOwner()->getUID(); |
|
| 2129 | - if ($uid != \OCP\User::getUser()) { |
|
| 2130 | - Filesystem::initMountPoints($uid); |
|
| 2131 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2132 | - try { |
|
| 2133 | - $filename = $ownerView->getPath($info['fileid']); |
|
| 2134 | - } catch (NotFoundException $e) { |
|
| 2135 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2136 | - } |
|
| 2137 | - } |
|
| 2138 | - return [$uid, $filename]; |
|
| 2139 | - } |
|
| 2140 | - |
|
| 2141 | - /** |
|
| 2142 | - * Creates parent non-existing folders |
|
| 2143 | - * |
|
| 2144 | - * @param string $filePath |
|
| 2145 | - * @return bool |
|
| 2146 | - */ |
|
| 2147 | - private function createParentDirectories($filePath) { |
|
| 2148 | - $directoryParts = explode('/', $filePath); |
|
| 2149 | - $directoryParts = array_filter($directoryParts); |
|
| 2150 | - foreach ($directoryParts as $key => $part) { |
|
| 2151 | - $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2152 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2153 | - if ($this->is_file($currentPath)) { |
|
| 2154 | - return false; |
|
| 2155 | - } |
|
| 2156 | - if (!$this->file_exists($currentPath)) { |
|
| 2157 | - $this->mkdir($currentPath); |
|
| 2158 | - } |
|
| 2159 | - } |
|
| 2160 | - |
|
| 2161 | - return true; |
|
| 2162 | - } |
|
| 83 | + /** @var string */ |
|
| 84 | + private $fakeRoot = ''; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @var \OCP\Lock\ILockingProvider |
|
| 88 | + */ |
|
| 89 | + protected $lockingProvider; |
|
| 90 | + |
|
| 91 | + private $lockingEnabled; |
|
| 92 | + |
|
| 93 | + private $updaterEnabled = true; |
|
| 94 | + |
|
| 95 | + /** @var \OC\User\Manager */ |
|
| 96 | + private $userManager; |
|
| 97 | + |
|
| 98 | + /** @var \OCP\ILogger */ |
|
| 99 | + private $logger; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @param string $root |
|
| 103 | + * @throws \Exception If $root contains an invalid path |
|
| 104 | + */ |
|
| 105 | + public function __construct($root = '') { |
|
| 106 | + if (is_null($root)) { |
|
| 107 | + throw new \InvalidArgumentException('Root can\'t be null'); |
|
| 108 | + } |
|
| 109 | + if (!Filesystem::isValidPath($root)) { |
|
| 110 | + throw new \Exception(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->fakeRoot = $root; |
|
| 114 | + $this->lockingProvider = \OC::$server->getLockingProvider(); |
|
| 115 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); |
|
| 116 | + $this->userManager = \OC::$server->getUserManager(); |
|
| 117 | + $this->logger = \OC::$server->getLogger(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + public function getAbsolutePath($path = '/') { |
|
| 121 | + if ($path === null) { |
|
| 122 | + return null; |
|
| 123 | + } |
|
| 124 | + $this->assertPathLength($path); |
|
| 125 | + if ($path === '') { |
|
| 126 | + $path = '/'; |
|
| 127 | + } |
|
| 128 | + if ($path[0] !== '/') { |
|
| 129 | + $path = '/' . $path; |
|
| 130 | + } |
|
| 131 | + return $this->fakeRoot . $path; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * change the root to a fake root |
|
| 136 | + * |
|
| 137 | + * @param string $fakeRoot |
|
| 138 | + * @return boolean|null |
|
| 139 | + */ |
|
| 140 | + public function chroot($fakeRoot) { |
|
| 141 | + if (!$fakeRoot == '') { |
|
| 142 | + if ($fakeRoot[0] !== '/') { |
|
| 143 | + $fakeRoot = '/' . $fakeRoot; |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + $this->fakeRoot = $fakeRoot; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * get the fake root |
|
| 151 | + * |
|
| 152 | + * @return string |
|
| 153 | + */ |
|
| 154 | + public function getRoot() { |
|
| 155 | + return $this->fakeRoot; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * get path relative to the root of the view |
|
| 160 | + * |
|
| 161 | + * @param string $path |
|
| 162 | + * @return string |
|
| 163 | + */ |
|
| 164 | + public function getRelativePath($path) { |
|
| 165 | + $this->assertPathLength($path); |
|
| 166 | + if ($this->fakeRoot == '') { |
|
| 167 | + return $path; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { |
|
| 171 | + return '/'; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + // missing slashes can cause wrong matches! |
|
| 175 | + $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 176 | + |
|
| 177 | + if (strpos($path, $root) !== 0) { |
|
| 178 | + return null; |
|
| 179 | + } else { |
|
| 180 | + $path = substr($path, strlen($this->fakeRoot)); |
|
| 181 | + if (strlen($path) === 0) { |
|
| 182 | + return '/'; |
|
| 183 | + } else { |
|
| 184 | + return $path; |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * get the mountpoint of the storage object for a path |
|
| 191 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 192 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 193 | + * and does not take the chroot into account ) |
|
| 194 | + * |
|
| 195 | + * @param string $path |
|
| 196 | + * @return string |
|
| 197 | + */ |
|
| 198 | + public function getMountPoint($path) { |
|
| 199 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * get the mountpoint of the storage object for a path |
|
| 204 | + * ( note: because a storage is not always mounted inside the fakeroot, the |
|
| 205 | + * returned mountpoint is relative to the absolute root of the filesystem |
|
| 206 | + * and does not take the chroot into account ) |
|
| 207 | + * |
|
| 208 | + * @param string $path |
|
| 209 | + * @return \OCP\Files\Mount\IMountPoint |
|
| 210 | + */ |
|
| 211 | + public function getMount($path) { |
|
| 212 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * resolve a path to a storage and internal path |
|
| 217 | + * |
|
| 218 | + * @param string $path |
|
| 219 | + * @return array an array consisting of the storage and the internal path |
|
| 220 | + */ |
|
| 221 | + public function resolvePath($path) { |
|
| 222 | + $a = $this->getAbsolutePath($path); |
|
| 223 | + $p = Filesystem::normalizePath($a); |
|
| 224 | + return Filesystem::resolvePath($p); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * return the path to a local version of the file |
|
| 229 | + * we need this because we can't know if a file is stored local or not from |
|
| 230 | + * outside the filestorage and for some purposes a local file is needed |
|
| 231 | + * |
|
| 232 | + * @param string $path |
|
| 233 | + * @return string |
|
| 234 | + */ |
|
| 235 | + public function getLocalFile($path) { |
|
| 236 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 237 | + $path = $this->getAbsolutePath($path); |
|
| 238 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 239 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 240 | + return $storage->getLocalFile($internalPath); |
|
| 241 | + } else { |
|
| 242 | + return null; |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param string $path |
|
| 248 | + * @return string |
|
| 249 | + */ |
|
| 250 | + public function getLocalFolder($path) { |
|
| 251 | + $parent = substr($path, 0, strrpos($path, '/')); |
|
| 252 | + $path = $this->getAbsolutePath($path); |
|
| 253 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 254 | + if (Filesystem::isValidPath($parent) and $storage) { |
|
| 255 | + return $storage->getLocalFolder($internalPath); |
|
| 256 | + } else { |
|
| 257 | + return null; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * the following functions operate with arguments and return values identical |
|
| 263 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers |
|
| 264 | + * for \OC\Files\Storage\Storage via basicOperation(). |
|
| 265 | + */ |
|
| 266 | + public function mkdir($path) { |
|
| 267 | + return $this->basicOperation('mkdir', $path, array('create', 'write')); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * remove mount point |
|
| 272 | + * |
|
| 273 | + * @param \OC\Files\Mount\MoveableMount $mount |
|
| 274 | + * @param string $path relative to data/ |
|
| 275 | + * @return boolean |
|
| 276 | + */ |
|
| 277 | + protected function removeMount($mount, $path) { |
|
| 278 | + if ($mount instanceof MoveableMount) { |
|
| 279 | + // cut of /user/files to get the relative path to data/user/files |
|
| 280 | + $pathParts = explode('/', $path, 4); |
|
| 281 | + $relPath = '/' . $pathParts[3]; |
|
| 282 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 283 | + \OC_Hook::emit( |
|
| 284 | + Filesystem::CLASSNAME, "umount", |
|
| 285 | + array(Filesystem::signal_param_path => $relPath) |
|
| 286 | + ); |
|
| 287 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 288 | + $result = $mount->removeMount(); |
|
| 289 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 290 | + if ($result) { |
|
| 291 | + \OC_Hook::emit( |
|
| 292 | + Filesystem::CLASSNAME, "post_umount", |
|
| 293 | + array(Filesystem::signal_param_path => $relPath) |
|
| 294 | + ); |
|
| 295 | + } |
|
| 296 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
|
| 297 | + return $result; |
|
| 298 | + } else { |
|
| 299 | + // do not allow deleting the storage's root / the mount point |
|
| 300 | + // because for some storages it might delete the whole contents |
|
| 301 | + // but isn't supposed to work that way |
|
| 302 | + return false; |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + public function disableCacheUpdate() { |
|
| 307 | + $this->updaterEnabled = false; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + public function enableCacheUpdate() { |
|
| 311 | + $this->updaterEnabled = true; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + protected function writeUpdate(Storage $storage, $internalPath, $time = null) { |
|
| 315 | + if ($this->updaterEnabled) { |
|
| 316 | + if (is_null($time)) { |
|
| 317 | + $time = time(); |
|
| 318 | + } |
|
| 319 | + $storage->getUpdater()->update($internalPath, $time); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + protected function removeUpdate(Storage $storage, $internalPath) { |
|
| 324 | + if ($this->updaterEnabled) { |
|
| 325 | + $storage->getUpdater()->remove($internalPath); |
|
| 326 | + } |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 330 | + if ($this->updaterEnabled) { |
|
| 331 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); |
|
| 332 | + } |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @param string $path |
|
| 337 | + * @return bool|mixed |
|
| 338 | + */ |
|
| 339 | + public function rmdir($path) { |
|
| 340 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 341 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 342 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 343 | + return $this->removeMount($mount, $absolutePath); |
|
| 344 | + } |
|
| 345 | + if ($this->is_dir($path)) { |
|
| 346 | + $result = $this->basicOperation('rmdir', $path, array('delete')); |
|
| 347 | + } else { |
|
| 348 | + $result = false; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 352 | + $storage = $mount->getStorage(); |
|
| 353 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 354 | + $storage->getUpdater()->remove($internalPath); |
|
| 355 | + } |
|
| 356 | + return $result; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @param string $path |
|
| 361 | + * @return resource |
|
| 362 | + */ |
|
| 363 | + public function opendir($path) { |
|
| 364 | + return $this->basicOperation('opendir', $path, array('read')); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * @param string $path |
|
| 369 | + * @return bool|mixed |
|
| 370 | + */ |
|
| 371 | + public function is_dir($path) { |
|
| 372 | + if ($path == '/') { |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + return $this->basicOperation('is_dir', $path); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * @param string $path |
|
| 380 | + * @return bool|mixed |
|
| 381 | + */ |
|
| 382 | + public function is_file($path) { |
|
| 383 | + if ($path == '/') { |
|
| 384 | + return false; |
|
| 385 | + } |
|
| 386 | + return $this->basicOperation('is_file', $path); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * @param string $path |
|
| 391 | + * @return mixed |
|
| 392 | + */ |
|
| 393 | + public function stat($path) { |
|
| 394 | + return $this->basicOperation('stat', $path); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * @param string $path |
|
| 399 | + * @return mixed |
|
| 400 | + */ |
|
| 401 | + public function filetype($path) { |
|
| 402 | + return $this->basicOperation('filetype', $path); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * @param string $path |
|
| 407 | + * @return mixed |
|
| 408 | + */ |
|
| 409 | + public function filesize($path) { |
|
| 410 | + return $this->basicOperation('filesize', $path); |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * @param string $path |
|
| 415 | + * @return bool|mixed |
|
| 416 | + * @throws \OCP\Files\InvalidPathException |
|
| 417 | + */ |
|
| 418 | + public function readfile($path) { |
|
| 419 | + $this->assertPathLength($path); |
|
| 420 | + @ob_end_clean(); |
|
| 421 | + $handle = $this->fopen($path, 'rb'); |
|
| 422 | + if ($handle) { |
|
| 423 | + $chunkSize = 8192; // 8 kB chunks |
|
| 424 | + while (!feof($handle)) { |
|
| 425 | + echo fread($handle, $chunkSize); |
|
| 426 | + flush(); |
|
| 427 | + } |
|
| 428 | + fclose($handle); |
|
| 429 | + return $this->filesize($path); |
|
| 430 | + } |
|
| 431 | + return false; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * @param string $path |
|
| 436 | + * @param int $from |
|
| 437 | + * @param int $to |
|
| 438 | + * @return bool|mixed |
|
| 439 | + * @throws \OCP\Files\InvalidPathException |
|
| 440 | + * @throws \OCP\Files\UnseekableException |
|
| 441 | + */ |
|
| 442 | + public function readfilePart($path, $from, $to) { |
|
| 443 | + $this->assertPathLength($path); |
|
| 444 | + @ob_end_clean(); |
|
| 445 | + $handle = $this->fopen($path, 'rb'); |
|
| 446 | + if ($handle) { |
|
| 447 | + $chunkSize = 8192; // 8 kB chunks |
|
| 448 | + $startReading = true; |
|
| 449 | + |
|
| 450 | + if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { |
|
| 451 | + // forward file handle via chunked fread because fseek seem to have failed |
|
| 452 | + |
|
| 453 | + $end = $from + 1; |
|
| 454 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 455 | + $len = $from - ftell($handle); |
|
| 456 | + if ($len > $chunkSize) { |
|
| 457 | + $len = $chunkSize; |
|
| 458 | + } |
|
| 459 | + $result = fread($handle, $len); |
|
| 460 | + |
|
| 461 | + if ($result === false) { |
|
| 462 | + $startReading = false; |
|
| 463 | + break; |
|
| 464 | + } |
|
| 465 | + } |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + if ($startReading) { |
|
| 469 | + $end = $to + 1; |
|
| 470 | + while (!feof($handle) && ftell($handle) < $end) { |
|
| 471 | + $len = $end - ftell($handle); |
|
| 472 | + if ($len > $chunkSize) { |
|
| 473 | + $len = $chunkSize; |
|
| 474 | + } |
|
| 475 | + echo fread($handle, $len); |
|
| 476 | + flush(); |
|
| 477 | + } |
|
| 478 | + return ftell($handle) - $from; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + throw new \OCP\Files\UnseekableException('fseek error'); |
|
| 482 | + } |
|
| 483 | + return false; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * @param string $path |
|
| 488 | + * @return mixed |
|
| 489 | + */ |
|
| 490 | + public function isCreatable($path) { |
|
| 491 | + return $this->basicOperation('isCreatable', $path); |
|
| 492 | + } |
|
| 493 | + |
|
| 494 | + /** |
|
| 495 | + * @param string $path |
|
| 496 | + * @return mixed |
|
| 497 | + */ |
|
| 498 | + public function isReadable($path) { |
|
| 499 | + return $this->basicOperation('isReadable', $path); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * @param string $path |
|
| 504 | + * @return mixed |
|
| 505 | + */ |
|
| 506 | + public function isUpdatable($path) { |
|
| 507 | + return $this->basicOperation('isUpdatable', $path); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * @param string $path |
|
| 512 | + * @return bool|mixed |
|
| 513 | + */ |
|
| 514 | + public function isDeletable($path) { |
|
| 515 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 516 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 517 | + if ($mount->getInternalPath($absolutePath) === '') { |
|
| 518 | + return $mount instanceof MoveableMount; |
|
| 519 | + } |
|
| 520 | + return $this->basicOperation('isDeletable', $path); |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * @param string $path |
|
| 525 | + * @return mixed |
|
| 526 | + */ |
|
| 527 | + public function isSharable($path) { |
|
| 528 | + return $this->basicOperation('isSharable', $path); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + /** |
|
| 532 | + * @param string $path |
|
| 533 | + * @return bool|mixed |
|
| 534 | + */ |
|
| 535 | + public function file_exists($path) { |
|
| 536 | + if ($path == '/') { |
|
| 537 | + return true; |
|
| 538 | + } |
|
| 539 | + return $this->basicOperation('file_exists', $path); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * @param string $path |
|
| 544 | + * @return mixed |
|
| 545 | + */ |
|
| 546 | + public function filemtime($path) { |
|
| 547 | + return $this->basicOperation('filemtime', $path); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * @param string $path |
|
| 552 | + * @param int|string $mtime |
|
| 553 | + * @return bool |
|
| 554 | + */ |
|
| 555 | + public function touch($path, $mtime = null) { |
|
| 556 | + if (!is_null($mtime) and !is_numeric($mtime)) { |
|
| 557 | + $mtime = strtotime($mtime); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + $hooks = array('touch'); |
|
| 561 | + |
|
| 562 | + if (!$this->file_exists($path)) { |
|
| 563 | + $hooks[] = 'create'; |
|
| 564 | + $hooks[] = 'write'; |
|
| 565 | + } |
|
| 566 | + $result = $this->basicOperation('touch', $path, $hooks, $mtime); |
|
| 567 | + if (!$result) { |
|
| 568 | + // If create file fails because of permissions on external storage like SMB folders, |
|
| 569 | + // check file exists and return false if not. |
|
| 570 | + if (!$this->file_exists($path)) { |
|
| 571 | + return false; |
|
| 572 | + } |
|
| 573 | + if (is_null($mtime)) { |
|
| 574 | + $mtime = time(); |
|
| 575 | + } |
|
| 576 | + //if native touch fails, we emulate it by changing the mtime in the cache |
|
| 577 | + $this->putFileInfo($path, array('mtime' => floor($mtime))); |
|
| 578 | + } |
|
| 579 | + return true; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * @param string $path |
|
| 584 | + * @return mixed |
|
| 585 | + */ |
|
| 586 | + public function file_get_contents($path) { |
|
| 587 | + return $this->basicOperation('file_get_contents', $path, array('read')); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * @param bool $exists |
|
| 592 | + * @param string $path |
|
| 593 | + * @param bool $run |
|
| 594 | + */ |
|
| 595 | + protected function emit_file_hooks_pre($exists, $path, &$run) { |
|
| 596 | + if (!$exists) { |
|
| 597 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( |
|
| 598 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 599 | + Filesystem::signal_param_run => &$run, |
|
| 600 | + )); |
|
| 601 | + } else { |
|
| 602 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( |
|
| 603 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 604 | + Filesystem::signal_param_run => &$run, |
|
| 605 | + )); |
|
| 606 | + } |
|
| 607 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( |
|
| 608 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 609 | + Filesystem::signal_param_run => &$run, |
|
| 610 | + )); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * @param bool $exists |
|
| 615 | + * @param string $path |
|
| 616 | + */ |
|
| 617 | + protected function emit_file_hooks_post($exists, $path) { |
|
| 618 | + if (!$exists) { |
|
| 619 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( |
|
| 620 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 621 | + )); |
|
| 622 | + } else { |
|
| 623 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( |
|
| 624 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 625 | + )); |
|
| 626 | + } |
|
| 627 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( |
|
| 628 | + Filesystem::signal_param_path => $this->getHookPath($path), |
|
| 629 | + )); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * @param string $path |
|
| 634 | + * @param mixed $data |
|
| 635 | + * @return bool|mixed |
|
| 636 | + * @throws \Exception |
|
| 637 | + */ |
|
| 638 | + public function file_put_contents($path, $data) { |
|
| 639 | + if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier |
|
| 640 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 641 | + if (Filesystem::isValidPath($path) |
|
| 642 | + and !Filesystem::isFileBlacklisted($path) |
|
| 643 | + ) { |
|
| 644 | + $path = $this->getRelativePath($absolutePath); |
|
| 645 | + |
|
| 646 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 647 | + |
|
| 648 | + $exists = $this->file_exists($path); |
|
| 649 | + $run = true; |
|
| 650 | + if ($this->shouldEmitHooks($path)) { |
|
| 651 | + $this->emit_file_hooks_pre($exists, $path, $run); |
|
| 652 | + } |
|
| 653 | + if (!$run) { |
|
| 654 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 655 | + return false; |
|
| 656 | + } |
|
| 657 | + |
|
| 658 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 659 | + |
|
| 660 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 661 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 662 | + $target = $storage->fopen($internalPath, 'w'); |
|
| 663 | + if ($target) { |
|
| 664 | + list (, $result) = \OC_Helper::streamCopy($data, $target); |
|
| 665 | + fclose($target); |
|
| 666 | + fclose($data); |
|
| 667 | + |
|
| 668 | + $this->writeUpdate($storage, $internalPath); |
|
| 669 | + |
|
| 670 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 671 | + |
|
| 672 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 673 | + $this->emit_file_hooks_post($exists, $path); |
|
| 674 | + } |
|
| 675 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 676 | + return $result; |
|
| 677 | + } else { |
|
| 678 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 679 | + return false; |
|
| 680 | + } |
|
| 681 | + } else { |
|
| 682 | + return false; |
|
| 683 | + } |
|
| 684 | + } else { |
|
| 685 | + $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); |
|
| 686 | + return $this->basicOperation('file_put_contents', $path, $hooks, $data); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * @param string $path |
|
| 692 | + * @return bool|mixed |
|
| 693 | + */ |
|
| 694 | + public function unlink($path) { |
|
| 695 | + if ($path === '' || $path === '/') { |
|
| 696 | + // do not allow deleting the root |
|
| 697 | + return false; |
|
| 698 | + } |
|
| 699 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 700 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 701 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 702 | + if ($mount and $mount->getInternalPath($absolutePath) === '') { |
|
| 703 | + return $this->removeMount($mount, $absolutePath); |
|
| 704 | + } |
|
| 705 | + if ($this->is_dir($path)) { |
|
| 706 | + $result = $this->basicOperation('rmdir', $path, ['delete']); |
|
| 707 | + } else { |
|
| 708 | + $result = $this->basicOperation('unlink', $path, ['delete']); |
|
| 709 | + } |
|
| 710 | + if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete |
|
| 711 | + $storage = $mount->getStorage(); |
|
| 712 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 713 | + $storage->getUpdater()->remove($internalPath); |
|
| 714 | + return true; |
|
| 715 | + } else { |
|
| 716 | + return $result; |
|
| 717 | + } |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + /** |
|
| 721 | + * @param string $directory |
|
| 722 | + * @return bool|mixed |
|
| 723 | + */ |
|
| 724 | + public function deleteAll($directory) { |
|
| 725 | + return $this->rmdir($directory); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + /** |
|
| 729 | + * Rename/move a file or folder from the source path to target path. |
|
| 730 | + * |
|
| 731 | + * @param string $path1 source path |
|
| 732 | + * @param string $path2 target path |
|
| 733 | + * |
|
| 734 | + * @return bool|mixed |
|
| 735 | + */ |
|
| 736 | + public function rename($path1, $path2) { |
|
| 737 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 738 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 739 | + $result = false; |
|
| 740 | + if ( |
|
| 741 | + Filesystem::isValidPath($path2) |
|
| 742 | + and Filesystem::isValidPath($path1) |
|
| 743 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 744 | + ) { |
|
| 745 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 746 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 747 | + $exists = $this->file_exists($path2); |
|
| 748 | + |
|
| 749 | + if ($path1 == null or $path2 == null) { |
|
| 750 | + return false; |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 754 | + try { |
|
| 755 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 756 | + |
|
| 757 | + $run = true; |
|
| 758 | + if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { |
|
| 759 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 760 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 761 | + } elseif ($this->shouldEmitHooks($path1)) { |
|
| 762 | + \OC_Hook::emit( |
|
| 763 | + Filesystem::CLASSNAME, Filesystem::signal_rename, |
|
| 764 | + array( |
|
| 765 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 766 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 767 | + Filesystem::signal_param_run => &$run |
|
| 768 | + ) |
|
| 769 | + ); |
|
| 770 | + } |
|
| 771 | + if ($run) { |
|
| 772 | + $this->verifyPath(dirname($path2), basename($path2)); |
|
| 773 | + |
|
| 774 | + $manager = Filesystem::getMountManager(); |
|
| 775 | + $mount1 = $this->getMount($path1); |
|
| 776 | + $mount2 = $this->getMount($path2); |
|
| 777 | + $storage1 = $mount1->getStorage(); |
|
| 778 | + $storage2 = $mount2->getStorage(); |
|
| 779 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 780 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 781 | + |
|
| 782 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 783 | + try { |
|
| 784 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); |
|
| 785 | + |
|
| 786 | + if ($internalPath1 === '') { |
|
| 787 | + if ($mount1 instanceof MoveableMount) { |
|
| 788 | + if ($this->isTargetAllowed($absolutePath2)) { |
|
| 789 | + /** |
|
| 790 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 |
|
| 791 | + */ |
|
| 792 | + $sourceMountPoint = $mount1->getMountPoint(); |
|
| 793 | + $result = $mount1->moveMount($absolutePath2); |
|
| 794 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); |
|
| 795 | + } else { |
|
| 796 | + $result = false; |
|
| 797 | + } |
|
| 798 | + } else { |
|
| 799 | + $result = false; |
|
| 800 | + } |
|
| 801 | + // moving a file/folder within the same mount point |
|
| 802 | + } elseif ($storage1 === $storage2) { |
|
| 803 | + if ($storage1) { |
|
| 804 | + $result = $storage1->rename($internalPath1, $internalPath2); |
|
| 805 | + } else { |
|
| 806 | + $result = false; |
|
| 807 | + } |
|
| 808 | + // moving a file/folder between storages (from $storage1 to $storage2) |
|
| 809 | + } else { |
|
| 810 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 814 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation |
|
| 815 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 816 | + } else if ($result) { |
|
| 817 | + if ($internalPath1 !== '') { // don't do a cache update for moved mounts |
|
| 818 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
|
| 819 | + } |
|
| 820 | + } |
|
| 821 | + } catch(\Exception $e) { |
|
| 822 | + throw $e; |
|
| 823 | + } finally { |
|
| 824 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 825 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { |
|
| 829 | + if ($this->shouldEmitHooks()) { |
|
| 830 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 831 | + } |
|
| 832 | + } elseif ($result) { |
|
| 833 | + if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { |
|
| 834 | + \OC_Hook::emit( |
|
| 835 | + Filesystem::CLASSNAME, |
|
| 836 | + Filesystem::signal_post_rename, |
|
| 837 | + array( |
|
| 838 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 839 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 840 | + ) |
|
| 841 | + ); |
|
| 842 | + } |
|
| 843 | + } |
|
| 844 | + } |
|
| 845 | + } catch(\Exception $e) { |
|
| 846 | + throw $e; |
|
| 847 | + } finally { |
|
| 848 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
|
| 849 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + return $result; |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + /** |
|
| 856 | + * Copy a file/folder from the source path to target path |
|
| 857 | + * |
|
| 858 | + * @param string $path1 source path |
|
| 859 | + * @param string $path2 target path |
|
| 860 | + * @param bool $preserveMtime whether to preserve mtime on the copy |
|
| 861 | + * |
|
| 862 | + * @return bool|mixed |
|
| 863 | + */ |
|
| 864 | + public function copy($path1, $path2, $preserveMtime = false) { |
|
| 865 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); |
|
| 866 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); |
|
| 867 | + $result = false; |
|
| 868 | + if ( |
|
| 869 | + Filesystem::isValidPath($path2) |
|
| 870 | + and Filesystem::isValidPath($path1) |
|
| 871 | + and !Filesystem::isFileBlacklisted($path2) |
|
| 872 | + ) { |
|
| 873 | + $path1 = $this->getRelativePath($absolutePath1); |
|
| 874 | + $path2 = $this->getRelativePath($absolutePath2); |
|
| 875 | + |
|
| 876 | + if ($path1 == null or $path2 == null) { |
|
| 877 | + return false; |
|
| 878 | + } |
|
| 879 | + $run = true; |
|
| 880 | + |
|
| 881 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); |
|
| 882 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); |
|
| 883 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; |
|
| 884 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 885 | + |
|
| 886 | + try { |
|
| 887 | + |
|
| 888 | + $exists = $this->file_exists($path2); |
|
| 889 | + if ($this->shouldEmitHooks()) { |
|
| 890 | + \OC_Hook::emit( |
|
| 891 | + Filesystem::CLASSNAME, |
|
| 892 | + Filesystem::signal_copy, |
|
| 893 | + array( |
|
| 894 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 895 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), |
|
| 896 | + Filesystem::signal_param_run => &$run |
|
| 897 | + ) |
|
| 898 | + ); |
|
| 899 | + $this->emit_file_hooks_pre($exists, $path2, $run); |
|
| 900 | + } |
|
| 901 | + if ($run) { |
|
| 902 | + $mount1 = $this->getMount($path1); |
|
| 903 | + $mount2 = $this->getMount($path2); |
|
| 904 | + $storage1 = $mount1->getStorage(); |
|
| 905 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); |
|
| 906 | + $storage2 = $mount2->getStorage(); |
|
| 907 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); |
|
| 908 | + |
|
| 909 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 910 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; |
|
| 911 | + |
|
| 912 | + if ($mount1->getMountPoint() == $mount2->getMountPoint()) { |
|
| 913 | + if ($storage1) { |
|
| 914 | + $result = $storage1->copy($internalPath1, $internalPath2); |
|
| 915 | + } else { |
|
| 916 | + $result = false; |
|
| 917 | + } |
|
| 918 | + } else { |
|
| 919 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + $this->writeUpdate($storage2, $internalPath2); |
|
| 923 | + |
|
| 924 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); |
|
| 925 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; |
|
| 926 | + |
|
| 927 | + if ($this->shouldEmitHooks() && $result !== false) { |
|
| 928 | + \OC_Hook::emit( |
|
| 929 | + Filesystem::CLASSNAME, |
|
| 930 | + Filesystem::signal_post_copy, |
|
| 931 | + array( |
|
| 932 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), |
|
| 933 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) |
|
| 934 | + ) |
|
| 935 | + ); |
|
| 936 | + $this->emit_file_hooks_post($exists, $path2); |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + } |
|
| 940 | + } catch (\Exception $e) { |
|
| 941 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 942 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 943 | + throw $e; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + $this->unlockFile($path2, $lockTypePath2); |
|
| 947 | + $this->unlockFile($path1, $lockTypePath1); |
|
| 948 | + |
|
| 949 | + } |
|
| 950 | + return $result; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + /** |
|
| 954 | + * @param string $path |
|
| 955 | + * @param string $mode 'r' or 'w' |
|
| 956 | + * @return resource |
|
| 957 | + */ |
|
| 958 | + public function fopen($path, $mode) { |
|
| 959 | + $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support |
|
| 960 | + $hooks = array(); |
|
| 961 | + switch ($mode) { |
|
| 962 | + case 'r': |
|
| 963 | + $hooks[] = 'read'; |
|
| 964 | + break; |
|
| 965 | + case 'r+': |
|
| 966 | + case 'w+': |
|
| 967 | + case 'x+': |
|
| 968 | + case 'a+': |
|
| 969 | + $hooks[] = 'read'; |
|
| 970 | + $hooks[] = 'write'; |
|
| 971 | + break; |
|
| 972 | + case 'w': |
|
| 973 | + case 'x': |
|
| 974 | + case 'a': |
|
| 975 | + $hooks[] = 'write'; |
|
| 976 | + break; |
|
| 977 | + default: |
|
| 978 | + \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + if ($mode !== 'r' && $mode !== 'w') { |
|
| 982 | + \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); |
|
| 983 | + } |
|
| 984 | + |
|
| 985 | + return $this->basicOperation('fopen', $path, $hooks, $mode); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + /** |
|
| 989 | + * @param string $path |
|
| 990 | + * @return bool|string |
|
| 991 | + * @throws \OCP\Files\InvalidPathException |
|
| 992 | + */ |
|
| 993 | + public function toTmpFile($path) { |
|
| 994 | + $this->assertPathLength($path); |
|
| 995 | + if (Filesystem::isValidPath($path)) { |
|
| 996 | + $source = $this->fopen($path, 'r'); |
|
| 997 | + if ($source) { |
|
| 998 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 999 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); |
|
| 1000 | + file_put_contents($tmpFile, $source); |
|
| 1001 | + return $tmpFile; |
|
| 1002 | + } else { |
|
| 1003 | + return false; |
|
| 1004 | + } |
|
| 1005 | + } else { |
|
| 1006 | + return false; |
|
| 1007 | + } |
|
| 1008 | + } |
|
| 1009 | + |
|
| 1010 | + /** |
|
| 1011 | + * @param string $tmpFile |
|
| 1012 | + * @param string $path |
|
| 1013 | + * @return bool|mixed |
|
| 1014 | + * @throws \OCP\Files\InvalidPathException |
|
| 1015 | + */ |
|
| 1016 | + public function fromTmpFile($tmpFile, $path) { |
|
| 1017 | + $this->assertPathLength($path); |
|
| 1018 | + if (Filesystem::isValidPath($path)) { |
|
| 1019 | + |
|
| 1020 | + // Get directory that the file is going into |
|
| 1021 | + $filePath = dirname($path); |
|
| 1022 | + |
|
| 1023 | + // Create the directories if any |
|
| 1024 | + if (!$this->file_exists($filePath)) { |
|
| 1025 | + $result = $this->createParentDirectories($filePath); |
|
| 1026 | + if ($result === false) { |
|
| 1027 | + return false; |
|
| 1028 | + } |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + $source = fopen($tmpFile, 'r'); |
|
| 1032 | + if ($source) { |
|
| 1033 | + $result = $this->file_put_contents($path, $source); |
|
| 1034 | + // $this->file_put_contents() might have already closed |
|
| 1035 | + // the resource, so we check it, before trying to close it |
|
| 1036 | + // to avoid messages in the error log. |
|
| 1037 | + if (is_resource($source)) { |
|
| 1038 | + fclose($source); |
|
| 1039 | + } |
|
| 1040 | + unlink($tmpFile); |
|
| 1041 | + return $result; |
|
| 1042 | + } else { |
|
| 1043 | + return false; |
|
| 1044 | + } |
|
| 1045 | + } else { |
|
| 1046 | + return false; |
|
| 1047 | + } |
|
| 1048 | + } |
|
| 1049 | + |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * @param string $path |
|
| 1053 | + * @return mixed |
|
| 1054 | + * @throws \OCP\Files\InvalidPathException |
|
| 1055 | + */ |
|
| 1056 | + public function getMimeType($path) { |
|
| 1057 | + $this->assertPathLength($path); |
|
| 1058 | + return $this->basicOperation('getMimeType', $path); |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * @param string $type |
|
| 1063 | + * @param string $path |
|
| 1064 | + * @param bool $raw |
|
| 1065 | + * @return bool|null|string |
|
| 1066 | + */ |
|
| 1067 | + public function hash($type, $path, $raw = false) { |
|
| 1068 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1069 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1070 | + if (Filesystem::isValidPath($path)) { |
|
| 1071 | + $path = $this->getRelativePath($absolutePath); |
|
| 1072 | + if ($path == null) { |
|
| 1073 | + return false; |
|
| 1074 | + } |
|
| 1075 | + if ($this->shouldEmitHooks($path)) { |
|
| 1076 | + \OC_Hook::emit( |
|
| 1077 | + Filesystem::CLASSNAME, |
|
| 1078 | + Filesystem::signal_read, |
|
| 1079 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) |
|
| 1080 | + ); |
|
| 1081 | + } |
|
| 1082 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1083 | + if ($storage) { |
|
| 1084 | + return $storage->hash($type, $internalPath, $raw); |
|
| 1085 | + } |
|
| 1086 | + } |
|
| 1087 | + return null; |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + /** |
|
| 1091 | + * @param string $path |
|
| 1092 | + * @return mixed |
|
| 1093 | + * @throws \OCP\Files\InvalidPathException |
|
| 1094 | + */ |
|
| 1095 | + public function free_space($path = '/') { |
|
| 1096 | + $this->assertPathLength($path); |
|
| 1097 | + $result = $this->basicOperation('free_space', $path); |
|
| 1098 | + if ($result === null) { |
|
| 1099 | + throw new InvalidPathException(); |
|
| 1100 | + } |
|
| 1101 | + return $result; |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + /** |
|
| 1105 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage |
|
| 1106 | + * |
|
| 1107 | + * @param string $operation |
|
| 1108 | + * @param string $path |
|
| 1109 | + * @param array $hooks (optional) |
|
| 1110 | + * @param mixed $extraParam (optional) |
|
| 1111 | + * @return mixed |
|
| 1112 | + * @throws \Exception |
|
| 1113 | + * |
|
| 1114 | + * This method takes requests for basic filesystem functions (e.g. reading & writing |
|
| 1115 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to |
|
| 1116 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution |
|
| 1117 | + */ |
|
| 1118 | + private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { |
|
| 1119 | + $postFix = (substr($path, -1) === '/') ? '/' : ''; |
|
| 1120 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
|
| 1121 | + if (Filesystem::isValidPath($path) |
|
| 1122 | + and !Filesystem::isFileBlacklisted($path) |
|
| 1123 | + ) { |
|
| 1124 | + $path = $this->getRelativePath($absolutePath); |
|
| 1125 | + if ($path == null) { |
|
| 1126 | + return false; |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { |
|
| 1130 | + // always a shared lock during pre-hooks so the hook can read the file |
|
| 1131 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1132 | + } |
|
| 1133 | + |
|
| 1134 | + $run = $this->runHooks($hooks, $path); |
|
| 1135 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 1136 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1137 | + if ($run and $storage) { |
|
| 1138 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1139 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1140 | + } |
|
| 1141 | + try { |
|
| 1142 | + if (!is_null($extraParam)) { |
|
| 1143 | + $result = $storage->$operation($internalPath, $extraParam); |
|
| 1144 | + } else { |
|
| 1145 | + $result = $storage->$operation($internalPath); |
|
| 1146 | + } |
|
| 1147 | + } catch (\Exception $e) { |
|
| 1148 | + if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
|
| 1149 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1150 | + } else if (in_array('read', $hooks)) { |
|
| 1151 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1152 | + } |
|
| 1153 | + throw $e; |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + if ($result && in_array('delete', $hooks) and $result) { |
|
| 1157 | + $this->removeUpdate($storage, $internalPath); |
|
| 1158 | + } |
|
| 1159 | + if ($result && in_array('write', $hooks) and $operation !== 'fopen') { |
|
| 1160 | + $this->writeUpdate($storage, $internalPath); |
|
| 1161 | + } |
|
| 1162 | + if ($result && in_array('touch', $hooks)) { |
|
| 1163 | + $this->writeUpdate($storage, $internalPath, $extraParam); |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { |
|
| 1167 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + $unlockLater = false; |
|
| 1171 | + if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { |
|
| 1172 | + $unlockLater = true; |
|
| 1173 | + // make sure our unlocking callback will still be called if connection is aborted |
|
| 1174 | + ignore_user_abort(true); |
|
| 1175 | + $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1176 | + if (in_array('write', $hooks)) { |
|
| 1177 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
|
| 1178 | + } else if (in_array('read', $hooks)) { |
|
| 1179 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1180 | + } |
|
| 1181 | + }); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + if ($this->shouldEmitHooks($path) && $result !== false) { |
|
| 1185 | + if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open |
|
| 1186 | + $this->runHooks($hooks, $path, true); |
|
| 1187 | + } |
|
| 1188 | + } |
|
| 1189 | + |
|
| 1190 | + if (!$unlockLater |
|
| 1191 | + && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) |
|
| 1192 | + ) { |
|
| 1193 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1194 | + } |
|
| 1195 | + return $result; |
|
| 1196 | + } else { |
|
| 1197 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); |
|
| 1198 | + } |
|
| 1199 | + } |
|
| 1200 | + return null; |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + /** |
|
| 1204 | + * get the path relative to the default root for hook usage |
|
| 1205 | + * |
|
| 1206 | + * @param string $path |
|
| 1207 | + * @return string |
|
| 1208 | + */ |
|
| 1209 | + private function getHookPath($path) { |
|
| 1210 | + if (!Filesystem::getView()) { |
|
| 1211 | + return $path; |
|
| 1212 | + } |
|
| 1213 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); |
|
| 1214 | + } |
|
| 1215 | + |
|
| 1216 | + private function shouldEmitHooks($path = '') { |
|
| 1217 | + if ($path && Cache\Scanner::isPartialFile($path)) { |
|
| 1218 | + return false; |
|
| 1219 | + } |
|
| 1220 | + if (!Filesystem::$loaded) { |
|
| 1221 | + return false; |
|
| 1222 | + } |
|
| 1223 | + $defaultRoot = Filesystem::getRoot(); |
|
| 1224 | + if ($defaultRoot === null) { |
|
| 1225 | + return false; |
|
| 1226 | + } |
|
| 1227 | + if ($this->fakeRoot === $defaultRoot) { |
|
| 1228 | + return true; |
|
| 1229 | + } |
|
| 1230 | + $fullPath = $this->getAbsolutePath($path); |
|
| 1231 | + |
|
| 1232 | + if ($fullPath === $defaultRoot) { |
|
| 1233 | + return true; |
|
| 1234 | + } |
|
| 1235 | + |
|
| 1236 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + /** |
|
| 1240 | + * @param string[] $hooks |
|
| 1241 | + * @param string $path |
|
| 1242 | + * @param bool $post |
|
| 1243 | + * @return bool |
|
| 1244 | + */ |
|
| 1245 | + private function runHooks($hooks, $path, $post = false) { |
|
| 1246 | + $relativePath = $path; |
|
| 1247 | + $path = $this->getHookPath($path); |
|
| 1248 | + $prefix = ($post) ? 'post_' : ''; |
|
| 1249 | + $run = true; |
|
| 1250 | + if ($this->shouldEmitHooks($relativePath)) { |
|
| 1251 | + foreach ($hooks as $hook) { |
|
| 1252 | + if ($hook != 'read') { |
|
| 1253 | + \OC_Hook::emit( |
|
| 1254 | + Filesystem::CLASSNAME, |
|
| 1255 | + $prefix . $hook, |
|
| 1256 | + array( |
|
| 1257 | + Filesystem::signal_param_run => &$run, |
|
| 1258 | + Filesystem::signal_param_path => $path |
|
| 1259 | + ) |
|
| 1260 | + ); |
|
| 1261 | + } elseif (!$post) { |
|
| 1262 | + \OC_Hook::emit( |
|
| 1263 | + Filesystem::CLASSNAME, |
|
| 1264 | + $prefix . $hook, |
|
| 1265 | + array( |
|
| 1266 | + Filesystem::signal_param_path => $path |
|
| 1267 | + ) |
|
| 1268 | + ); |
|
| 1269 | + } |
|
| 1270 | + } |
|
| 1271 | + } |
|
| 1272 | + return $run; |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + /** |
|
| 1276 | + * check if a file or folder has been updated since $time |
|
| 1277 | + * |
|
| 1278 | + * @param string $path |
|
| 1279 | + * @param int $time |
|
| 1280 | + * @return bool |
|
| 1281 | + */ |
|
| 1282 | + public function hasUpdated($path, $time) { |
|
| 1283 | + return $this->basicOperation('hasUpdated', $path, array(), $time); |
|
| 1284 | + } |
|
| 1285 | + |
|
| 1286 | + /** |
|
| 1287 | + * @param string $ownerId |
|
| 1288 | + * @return \OC\User\User |
|
| 1289 | + */ |
|
| 1290 | + private function getUserObjectForOwner($ownerId) { |
|
| 1291 | + $owner = $this->userManager->get($ownerId); |
|
| 1292 | + if ($owner instanceof IUser) { |
|
| 1293 | + return $owner; |
|
| 1294 | + } else { |
|
| 1295 | + return new User($ownerId, null); |
|
| 1296 | + } |
|
| 1297 | + } |
|
| 1298 | + |
|
| 1299 | + /** |
|
| 1300 | + * Get file info from cache |
|
| 1301 | + * |
|
| 1302 | + * If the file is not in cached it will be scanned |
|
| 1303 | + * If the file has changed on storage the cache will be updated |
|
| 1304 | + * |
|
| 1305 | + * @param \OC\Files\Storage\Storage $storage |
|
| 1306 | + * @param string $internalPath |
|
| 1307 | + * @param string $relativePath |
|
| 1308 | + * @return ICacheEntry|bool |
|
| 1309 | + */ |
|
| 1310 | + private function getCacheEntry($storage, $internalPath, $relativePath) { |
|
| 1311 | + $cache = $storage->getCache($internalPath); |
|
| 1312 | + $data = $cache->get($internalPath); |
|
| 1313 | + $watcher = $storage->getWatcher($internalPath); |
|
| 1314 | + |
|
| 1315 | + try { |
|
| 1316 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data |
|
| 1317 | + if (!$data || $data['size'] === -1) { |
|
| 1318 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1319 | + if (!$storage->file_exists($internalPath)) { |
|
| 1320 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1321 | + return false; |
|
| 1322 | + } |
|
| 1323 | + $scanner = $storage->getScanner($internalPath); |
|
| 1324 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1325 | + $data = $cache->get($internalPath); |
|
| 1326 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1327 | + } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { |
|
| 1328 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1329 | + $watcher->update($internalPath, $data); |
|
| 1330 | + $storage->getPropagator()->propagateChange($internalPath, time()); |
|
| 1331 | + $data = $cache->get($internalPath); |
|
| 1332 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); |
|
| 1333 | + } |
|
| 1334 | + } catch (LockedException $e) { |
|
| 1335 | + // if the file is locked we just use the old cache info |
|
| 1336 | + } |
|
| 1337 | + |
|
| 1338 | + return $data; |
|
| 1339 | + } |
|
| 1340 | + |
|
| 1341 | + /** |
|
| 1342 | + * get the filesystem info |
|
| 1343 | + * |
|
| 1344 | + * @param string $path |
|
| 1345 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, |
|
| 1346 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. |
|
| 1347 | + * defaults to true |
|
| 1348 | + * @return \OC\Files\FileInfo|false False if file does not exist |
|
| 1349 | + */ |
|
| 1350 | + public function getFileInfo($path, $includeMountPoints = true) { |
|
| 1351 | + $this->assertPathLength($path); |
|
| 1352 | + if (!Filesystem::isValidPath($path)) { |
|
| 1353 | + return false; |
|
| 1354 | + } |
|
| 1355 | + if (Cache\Scanner::isPartialFile($path)) { |
|
| 1356 | + return $this->getPartFileInfo($path); |
|
| 1357 | + } |
|
| 1358 | + $relativePath = $path; |
|
| 1359 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1360 | + |
|
| 1361 | + $mount = Filesystem::getMountManager()->find($path); |
|
| 1362 | + if (!$mount) { |
|
| 1363 | + return false; |
|
| 1364 | + } |
|
| 1365 | + $storage = $mount->getStorage(); |
|
| 1366 | + $internalPath = $mount->getInternalPath($path); |
|
| 1367 | + if ($storage) { |
|
| 1368 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); |
|
| 1369 | + |
|
| 1370 | + if (!$data instanceof ICacheEntry) { |
|
| 1371 | + return false; |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + if ($mount instanceof MoveableMount && $internalPath === '') { |
|
| 1375 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; |
|
| 1376 | + } |
|
| 1377 | + |
|
| 1378 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); |
|
| 1379 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); |
|
| 1380 | + |
|
| 1381 | + if ($data and isset($data['fileid'])) { |
|
| 1382 | + if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { |
|
| 1383 | + //add the sizes of other mount points to the folder |
|
| 1384 | + $extOnly = ($includeMountPoints === 'ext'); |
|
| 1385 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1386 | + $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1387 | + $subStorage = $mount->getStorage(); |
|
| 1388 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
|
| 1389 | + })); |
|
| 1390 | + } |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + return $info; |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + return false; |
|
| 1397 | + } |
|
| 1398 | + |
|
| 1399 | + /** |
|
| 1400 | + * get the content of a directory |
|
| 1401 | + * |
|
| 1402 | + * @param string $directory path under datadirectory |
|
| 1403 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
|
| 1404 | + * @return FileInfo[] |
|
| 1405 | + */ |
|
| 1406 | + public function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 1407 | + $this->assertPathLength($directory); |
|
| 1408 | + if (!Filesystem::isValidPath($directory)) { |
|
| 1409 | + return []; |
|
| 1410 | + } |
|
| 1411 | + $path = $this->getAbsolutePath($directory); |
|
| 1412 | + $path = Filesystem::normalizePath($path); |
|
| 1413 | + $mount = $this->getMount($directory); |
|
| 1414 | + if (!$mount) { |
|
| 1415 | + return []; |
|
| 1416 | + } |
|
| 1417 | + $storage = $mount->getStorage(); |
|
| 1418 | + $internalPath = $mount->getInternalPath($path); |
|
| 1419 | + if ($storage) { |
|
| 1420 | + $cache = $storage->getCache($internalPath); |
|
| 1421 | + $user = \OC_User::getUser(); |
|
| 1422 | + |
|
| 1423 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); |
|
| 1424 | + |
|
| 1425 | + if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1426 | + return []; |
|
| 1427 | + } |
|
| 1428 | + |
|
| 1429 | + $folderId = $data['fileid']; |
|
| 1430 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter |
|
| 1431 | + |
|
| 1432 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); |
|
| 1433 | + /** |
|
| 1434 | + * @var \OC\Files\FileInfo[] $files |
|
| 1435 | + */ |
|
| 1436 | + $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1437 | + if ($sharingDisabled) { |
|
| 1438 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1439 | + } |
|
| 1440 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
|
| 1441 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1442 | + }, $contents); |
|
| 1443 | + |
|
| 1444 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
|
| 1445 | + $mounts = Filesystem::getMountManager()->findIn($path); |
|
| 1446 | + $dirLength = strlen($path); |
|
| 1447 | + foreach ($mounts as $mount) { |
|
| 1448 | + $mountPoint = $mount->getMountPoint(); |
|
| 1449 | + $subStorage = $mount->getStorage(); |
|
| 1450 | + if ($subStorage) { |
|
| 1451 | + $subCache = $subStorage->getCache(''); |
|
| 1452 | + |
|
| 1453 | + $rootEntry = $subCache->get(''); |
|
| 1454 | + if (!$rootEntry) { |
|
| 1455 | + $subScanner = $subStorage->getScanner(''); |
|
| 1456 | + try { |
|
| 1457 | + $subScanner->scanFile(''); |
|
| 1458 | + } catch (\OCP\Files\StorageNotAvailableException $e) { |
|
| 1459 | + continue; |
|
| 1460 | + } catch (\OCP\Files\StorageInvalidException $e) { |
|
| 1461 | + continue; |
|
| 1462 | + } catch (\Exception $e) { |
|
| 1463 | + // sometimes when the storage is not available it can be any exception |
|
| 1464 | + \OC::$server->getLogger()->logException($e, [ |
|
| 1465 | + 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1466 | + 'level' => \OCP\Util::ERROR, |
|
| 1467 | + 'app' => 'lib', |
|
| 1468 | + ]); |
|
| 1469 | + continue; |
|
| 1470 | + } |
|
| 1471 | + $rootEntry = $subCache->get(''); |
|
| 1472 | + } |
|
| 1473 | + |
|
| 1474 | + if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { |
|
| 1475 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); |
|
| 1476 | + if ($pos = strpos($relativePath, '/')) { |
|
| 1477 | + //mountpoint inside subfolder add size to the correct folder |
|
| 1478 | + $entryName = substr($relativePath, 0, $pos); |
|
| 1479 | + foreach ($files as &$entry) { |
|
| 1480 | + if ($entry->getName() === $entryName) { |
|
| 1481 | + $entry->addSubEntry($rootEntry, $mountPoint); |
|
| 1482 | + } |
|
| 1483 | + } |
|
| 1484 | + } else { //mountpoint in this folder, add an entry for it |
|
| 1485 | + $rootEntry['name'] = $relativePath; |
|
| 1486 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; |
|
| 1487 | + $permissions = $rootEntry['permissions']; |
|
| 1488 | + // do not allow renaming/deleting the mount point if they are not shared files/folders |
|
| 1489 | + // for shared files/folders we use the permissions given by the owner |
|
| 1490 | + if ($mount instanceof MoveableMount) { |
|
| 1491 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; |
|
| 1492 | + } else { |
|
| 1493 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); |
|
| 1494 | + } |
|
| 1495 | + |
|
| 1496 | + //remove any existing entry with the same name |
|
| 1497 | + foreach ($files as $i => $file) { |
|
| 1498 | + if ($file['name'] === $rootEntry['name']) { |
|
| 1499 | + unset($files[$i]); |
|
| 1500 | + break; |
|
| 1501 | + } |
|
| 1502 | + } |
|
| 1503 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1504 | + |
|
| 1505 | + // if sharing was disabled for the user we remove the share permissions |
|
| 1506 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 1507 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
|
| 1508 | + } |
|
| 1509 | + |
|
| 1510 | + $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
|
| 1511 | + $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1512 | + } |
|
| 1513 | + } |
|
| 1514 | + } |
|
| 1515 | + } |
|
| 1516 | + |
|
| 1517 | + if ($mimetype_filter) { |
|
| 1518 | + $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1519 | + if (strpos($mimetype_filter, '/')) { |
|
| 1520 | + return $file->getMimetype() === $mimetype_filter; |
|
| 1521 | + } else { |
|
| 1522 | + return $file->getMimePart() === $mimetype_filter; |
|
| 1523 | + } |
|
| 1524 | + }); |
|
| 1525 | + } |
|
| 1526 | + |
|
| 1527 | + return $files; |
|
| 1528 | + } else { |
|
| 1529 | + return []; |
|
| 1530 | + } |
|
| 1531 | + } |
|
| 1532 | + |
|
| 1533 | + /** |
|
| 1534 | + * change file metadata |
|
| 1535 | + * |
|
| 1536 | + * @param string $path |
|
| 1537 | + * @param array|\OCP\Files\FileInfo $data |
|
| 1538 | + * @return int |
|
| 1539 | + * |
|
| 1540 | + * returns the fileid of the updated file |
|
| 1541 | + */ |
|
| 1542 | + public function putFileInfo($path, $data) { |
|
| 1543 | + $this->assertPathLength($path); |
|
| 1544 | + if ($data instanceof FileInfo) { |
|
| 1545 | + $data = $data->getData(); |
|
| 1546 | + } |
|
| 1547 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1548 | + /** |
|
| 1549 | + * @var \OC\Files\Storage\Storage $storage |
|
| 1550 | + * @var string $internalPath |
|
| 1551 | + */ |
|
| 1552 | + list($storage, $internalPath) = Filesystem::resolvePath($path); |
|
| 1553 | + if ($storage) { |
|
| 1554 | + $cache = $storage->getCache($path); |
|
| 1555 | + |
|
| 1556 | + if (!$cache->inCache($internalPath)) { |
|
| 1557 | + $scanner = $storage->getScanner($internalPath); |
|
| 1558 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); |
|
| 1559 | + } |
|
| 1560 | + |
|
| 1561 | + return $cache->put($internalPath, $data); |
|
| 1562 | + } else { |
|
| 1563 | + return -1; |
|
| 1564 | + } |
|
| 1565 | + } |
|
| 1566 | + |
|
| 1567 | + /** |
|
| 1568 | + * search for files with the name matching $query |
|
| 1569 | + * |
|
| 1570 | + * @param string $query |
|
| 1571 | + * @return FileInfo[] |
|
| 1572 | + */ |
|
| 1573 | + public function search($query) { |
|
| 1574 | + return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1575 | + } |
|
| 1576 | + |
|
| 1577 | + /** |
|
| 1578 | + * search for files with the name matching $query |
|
| 1579 | + * |
|
| 1580 | + * @param string $query |
|
| 1581 | + * @return FileInfo[] |
|
| 1582 | + */ |
|
| 1583 | + public function searchRaw($query) { |
|
| 1584 | + return $this->searchCommon('search', array($query)); |
|
| 1585 | + } |
|
| 1586 | + |
|
| 1587 | + /** |
|
| 1588 | + * search for files by mimetype |
|
| 1589 | + * |
|
| 1590 | + * @param string $mimetype |
|
| 1591 | + * @return FileInfo[] |
|
| 1592 | + */ |
|
| 1593 | + public function searchByMime($mimetype) { |
|
| 1594 | + return $this->searchCommon('searchByMime', array($mimetype)); |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + /** |
|
| 1598 | + * search for files by tag |
|
| 1599 | + * |
|
| 1600 | + * @param string|int $tag name or tag id |
|
| 1601 | + * @param string $userId owner of the tags |
|
| 1602 | + * @return FileInfo[] |
|
| 1603 | + */ |
|
| 1604 | + public function searchByTag($tag, $userId) { |
|
| 1605 | + return $this->searchCommon('searchByTag', array($tag, $userId)); |
|
| 1606 | + } |
|
| 1607 | + |
|
| 1608 | + /** |
|
| 1609 | + * @param string $method cache method |
|
| 1610 | + * @param array $args |
|
| 1611 | + * @return FileInfo[] |
|
| 1612 | + */ |
|
| 1613 | + private function searchCommon($method, $args) { |
|
| 1614 | + $files = array(); |
|
| 1615 | + $rootLength = strlen($this->fakeRoot); |
|
| 1616 | + |
|
| 1617 | + $mount = $this->getMount(''); |
|
| 1618 | + $mountPoint = $mount->getMountPoint(); |
|
| 1619 | + $storage = $mount->getStorage(); |
|
| 1620 | + if ($storage) { |
|
| 1621 | + $cache = $storage->getCache(''); |
|
| 1622 | + |
|
| 1623 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1624 | + foreach ($results as $result) { |
|
| 1625 | + if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1626 | + $internalPath = $result['path']; |
|
| 1627 | + $path = $mountPoint . $result['path']; |
|
| 1628 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1629 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1630 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1631 | + } |
|
| 1632 | + } |
|
| 1633 | + |
|
| 1634 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); |
|
| 1635 | + foreach ($mounts as $mount) { |
|
| 1636 | + $mountPoint = $mount->getMountPoint(); |
|
| 1637 | + $storage = $mount->getStorage(); |
|
| 1638 | + if ($storage) { |
|
| 1639 | + $cache = $storage->getCache(''); |
|
| 1640 | + |
|
| 1641 | + $relativeMountPoint = substr($mountPoint, $rootLength); |
|
| 1642 | + $results = call_user_func_array(array($cache, $method), $args); |
|
| 1643 | + if ($results) { |
|
| 1644 | + foreach ($results as $result) { |
|
| 1645 | + $internalPath = $result['path']; |
|
| 1646 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1647 | + $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1648 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1649 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
|
| 1650 | + } |
|
| 1651 | + } |
|
| 1652 | + } |
|
| 1653 | + } |
|
| 1654 | + } |
|
| 1655 | + return $files; |
|
| 1656 | + } |
|
| 1657 | + |
|
| 1658 | + /** |
|
| 1659 | + * Get the owner for a file or folder |
|
| 1660 | + * |
|
| 1661 | + * @param string $path |
|
| 1662 | + * @return string the user id of the owner |
|
| 1663 | + * @throws NotFoundException |
|
| 1664 | + */ |
|
| 1665 | + public function getOwner($path) { |
|
| 1666 | + $info = $this->getFileInfo($path); |
|
| 1667 | + if (!$info) { |
|
| 1668 | + throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1669 | + } |
|
| 1670 | + return $info->getOwner()->getUID(); |
|
| 1671 | + } |
|
| 1672 | + |
|
| 1673 | + /** |
|
| 1674 | + * get the ETag for a file or folder |
|
| 1675 | + * |
|
| 1676 | + * @param string $path |
|
| 1677 | + * @return string |
|
| 1678 | + */ |
|
| 1679 | + public function getETag($path) { |
|
| 1680 | + /** |
|
| 1681 | + * @var Storage\Storage $storage |
|
| 1682 | + * @var string $internalPath |
|
| 1683 | + */ |
|
| 1684 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1685 | + if ($storage) { |
|
| 1686 | + return $storage->getETag($internalPath); |
|
| 1687 | + } else { |
|
| 1688 | + return null; |
|
| 1689 | + } |
|
| 1690 | + } |
|
| 1691 | + |
|
| 1692 | + /** |
|
| 1693 | + * Get the path of a file by id, relative to the view |
|
| 1694 | + * |
|
| 1695 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file |
|
| 1696 | + * |
|
| 1697 | + * @param int $id |
|
| 1698 | + * @throws NotFoundException |
|
| 1699 | + * @return string |
|
| 1700 | + */ |
|
| 1701 | + public function getPath($id) { |
|
| 1702 | + $id = (int)$id; |
|
| 1703 | + $manager = Filesystem::getMountManager(); |
|
| 1704 | + $mounts = $manager->findIn($this->fakeRoot); |
|
| 1705 | + $mounts[] = $manager->find($this->fakeRoot); |
|
| 1706 | + // reverse the array so we start with the storage this view is in |
|
| 1707 | + // which is the most likely to contain the file we're looking for |
|
| 1708 | + $mounts = array_reverse($mounts); |
|
| 1709 | + foreach ($mounts as $mount) { |
|
| 1710 | + /** |
|
| 1711 | + * @var \OC\Files\Mount\MountPoint $mount |
|
| 1712 | + */ |
|
| 1713 | + if ($mount->getStorage()) { |
|
| 1714 | + $cache = $mount->getStorage()->getCache(); |
|
| 1715 | + $internalPath = $cache->getPathById($id); |
|
| 1716 | + if (is_string($internalPath)) { |
|
| 1717 | + $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1718 | + if (!is_null($path = $this->getRelativePath($fullPath))) { |
|
| 1719 | + return $path; |
|
| 1720 | + } |
|
| 1721 | + } |
|
| 1722 | + } |
|
| 1723 | + } |
|
| 1724 | + throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); |
|
| 1725 | + } |
|
| 1726 | + |
|
| 1727 | + /** |
|
| 1728 | + * @param string $path |
|
| 1729 | + * @throws InvalidPathException |
|
| 1730 | + */ |
|
| 1731 | + private function assertPathLength($path) { |
|
| 1732 | + $maxLen = min(PHP_MAXPATHLEN, 4000); |
|
| 1733 | + // Check for the string length - performed using isset() instead of strlen() |
|
| 1734 | + // because isset() is about 5x-40x faster. |
|
| 1735 | + if (isset($path[$maxLen])) { |
|
| 1736 | + $pathLen = strlen($path); |
|
| 1737 | + throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); |
|
| 1738 | + } |
|
| 1739 | + } |
|
| 1740 | + |
|
| 1741 | + /** |
|
| 1742 | + * check if it is allowed to move a mount point to a given target. |
|
| 1743 | + * It is not allowed to move a mount point into a different mount point or |
|
| 1744 | + * into an already shared folder |
|
| 1745 | + * |
|
| 1746 | + * @param string $target path |
|
| 1747 | + * @return boolean |
|
| 1748 | + */ |
|
| 1749 | + private function isTargetAllowed($target) { |
|
| 1750 | + |
|
| 1751 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); |
|
| 1752 | + if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { |
|
| 1753 | + \OCP\Util::writeLog('files', |
|
| 1754 | + 'It is not allowed to move one mount point into another one', |
|
| 1755 | + \OCP\Util::DEBUG); |
|
| 1756 | + return false; |
|
| 1757 | + } |
|
| 1758 | + |
|
| 1759 | + // note: cannot use the view because the target is already locked |
|
| 1760 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1761 | + if ($fileId === -1) { |
|
| 1762 | + // target might not exist, need to check parent instead |
|
| 1763 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1764 | + } |
|
| 1765 | + |
|
| 1766 | + // check if any of the parents were shared by the current owner (include collections) |
|
| 1767 | + $shares = \OCP\Share::getItemShared( |
|
| 1768 | + 'folder', |
|
| 1769 | + $fileId, |
|
| 1770 | + \OCP\Share::FORMAT_NONE, |
|
| 1771 | + null, |
|
| 1772 | + true |
|
| 1773 | + ); |
|
| 1774 | + |
|
| 1775 | + if (count($shares) > 0) { |
|
| 1776 | + \OCP\Util::writeLog('files', |
|
| 1777 | + 'It is not allowed to move one mount point into a shared folder', |
|
| 1778 | + \OCP\Util::DEBUG); |
|
| 1779 | + return false; |
|
| 1780 | + } |
|
| 1781 | + |
|
| 1782 | + return true; |
|
| 1783 | + } |
|
| 1784 | + |
|
| 1785 | + /** |
|
| 1786 | + * Get a fileinfo object for files that are ignored in the cache (part files) |
|
| 1787 | + * |
|
| 1788 | + * @param string $path |
|
| 1789 | + * @return \OCP\Files\FileInfo |
|
| 1790 | + */ |
|
| 1791 | + private function getPartFileInfo($path) { |
|
| 1792 | + $mount = $this->getMount($path); |
|
| 1793 | + $storage = $mount->getStorage(); |
|
| 1794 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); |
|
| 1795 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
|
| 1796 | + return new FileInfo( |
|
| 1797 | + $this->getAbsolutePath($path), |
|
| 1798 | + $storage, |
|
| 1799 | + $internalPath, |
|
| 1800 | + [ |
|
| 1801 | + 'fileid' => null, |
|
| 1802 | + 'mimetype' => $storage->getMimeType($internalPath), |
|
| 1803 | + 'name' => basename($path), |
|
| 1804 | + 'etag' => null, |
|
| 1805 | + 'size' => $storage->filesize($internalPath), |
|
| 1806 | + 'mtime' => $storage->filemtime($internalPath), |
|
| 1807 | + 'encrypted' => false, |
|
| 1808 | + 'permissions' => \OCP\Constants::PERMISSION_ALL |
|
| 1809 | + ], |
|
| 1810 | + $mount, |
|
| 1811 | + $owner |
|
| 1812 | + ); |
|
| 1813 | + } |
|
| 1814 | + |
|
| 1815 | + /** |
|
| 1816 | + * @param string $path |
|
| 1817 | + * @param string $fileName |
|
| 1818 | + * @throws InvalidPathException |
|
| 1819 | + */ |
|
| 1820 | + public function verifyPath($path, $fileName) { |
|
| 1821 | + try { |
|
| 1822 | + /** @type \OCP\Files\Storage $storage */ |
|
| 1823 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 1824 | + $storage->verifyPath($internalPath, $fileName); |
|
| 1825 | + } catch (ReservedWordException $ex) { |
|
| 1826 | + $l = \OC::$server->getL10N('lib'); |
|
| 1827 | + throw new InvalidPathException($l->t('File name is a reserved word')); |
|
| 1828 | + } catch (InvalidCharacterInPathException $ex) { |
|
| 1829 | + $l = \OC::$server->getL10N('lib'); |
|
| 1830 | + throw new InvalidPathException($l->t('File name contains at least one invalid character')); |
|
| 1831 | + } catch (FileNameTooLongException $ex) { |
|
| 1832 | + $l = \OC::$server->getL10N('lib'); |
|
| 1833 | + throw new InvalidPathException($l->t('File name is too long')); |
|
| 1834 | + } catch (InvalidDirectoryException $ex) { |
|
| 1835 | + $l = \OC::$server->getL10N('lib'); |
|
| 1836 | + throw new InvalidPathException($l->t('Dot files are not allowed')); |
|
| 1837 | + } catch (EmptyFileNameException $ex) { |
|
| 1838 | + $l = \OC::$server->getL10N('lib'); |
|
| 1839 | + throw new InvalidPathException($l->t('Empty filename is not allowed')); |
|
| 1840 | + } |
|
| 1841 | + } |
|
| 1842 | + |
|
| 1843 | + /** |
|
| 1844 | + * get all parent folders of $path |
|
| 1845 | + * |
|
| 1846 | + * @param string $path |
|
| 1847 | + * @return string[] |
|
| 1848 | + */ |
|
| 1849 | + private function getParents($path) { |
|
| 1850 | + $path = trim($path, '/'); |
|
| 1851 | + if (!$path) { |
|
| 1852 | + return []; |
|
| 1853 | + } |
|
| 1854 | + |
|
| 1855 | + $parts = explode('/', $path); |
|
| 1856 | + |
|
| 1857 | + // remove the single file |
|
| 1858 | + array_pop($parts); |
|
| 1859 | + $result = array('/'); |
|
| 1860 | + $resultPath = ''; |
|
| 1861 | + foreach ($parts as $part) { |
|
| 1862 | + if ($part) { |
|
| 1863 | + $resultPath .= '/' . $part; |
|
| 1864 | + $result[] = $resultPath; |
|
| 1865 | + } |
|
| 1866 | + } |
|
| 1867 | + return $result; |
|
| 1868 | + } |
|
| 1869 | + |
|
| 1870 | + /** |
|
| 1871 | + * Returns the mount point for which to lock |
|
| 1872 | + * |
|
| 1873 | + * @param string $absolutePath absolute path |
|
| 1874 | + * @param bool $useParentMount true to return parent mount instead of whatever |
|
| 1875 | + * is mounted directly on the given path, false otherwise |
|
| 1876 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks |
|
| 1877 | + */ |
|
| 1878 | + private function getMountForLock($absolutePath, $useParentMount = false) { |
|
| 1879 | + $results = []; |
|
| 1880 | + $mount = Filesystem::getMountManager()->find($absolutePath); |
|
| 1881 | + if (!$mount) { |
|
| 1882 | + return $results; |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + if ($useParentMount) { |
|
| 1886 | + // find out if something is mounted directly on the path |
|
| 1887 | + $internalPath = $mount->getInternalPath($absolutePath); |
|
| 1888 | + if ($internalPath === '') { |
|
| 1889 | + // resolve the parent mount instead |
|
| 1890 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); |
|
| 1891 | + } |
|
| 1892 | + } |
|
| 1893 | + |
|
| 1894 | + return $mount; |
|
| 1895 | + } |
|
| 1896 | + |
|
| 1897 | + /** |
|
| 1898 | + * Lock the given path |
|
| 1899 | + * |
|
| 1900 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1901 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1902 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1903 | + * |
|
| 1904 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1905 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1906 | + */ |
|
| 1907 | + private function lockPath($path, $type, $lockMountPoint = false) { |
|
| 1908 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1909 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1910 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1911 | + return false; |
|
| 1912 | + } |
|
| 1913 | + |
|
| 1914 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1915 | + if ($mount) { |
|
| 1916 | + try { |
|
| 1917 | + $storage = $mount->getStorage(); |
|
| 1918 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1919 | + $storage->acquireLock( |
|
| 1920 | + $mount->getInternalPath($absolutePath), |
|
| 1921 | + $type, |
|
| 1922 | + $this->lockingProvider |
|
| 1923 | + ); |
|
| 1924 | + } |
|
| 1925 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1926 | + // rethrow with the a human-readable path |
|
| 1927 | + throw new \OCP\Lock\LockedException( |
|
| 1928 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1929 | + $e |
|
| 1930 | + ); |
|
| 1931 | + } |
|
| 1932 | + } |
|
| 1933 | + |
|
| 1934 | + return true; |
|
| 1935 | + } |
|
| 1936 | + |
|
| 1937 | + /** |
|
| 1938 | + * Change the lock type |
|
| 1939 | + * |
|
| 1940 | + * @param string $path the path of the file to lock, relative to the view |
|
| 1941 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1942 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1943 | + * |
|
| 1944 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1945 | + * @throws \OCP\Lock\LockedException if the path is already locked |
|
| 1946 | + */ |
|
| 1947 | + public function changeLock($path, $type, $lockMountPoint = false) { |
|
| 1948 | + $path = Filesystem::normalizePath($path); |
|
| 1949 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1950 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1951 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1952 | + return false; |
|
| 1953 | + } |
|
| 1954 | + |
|
| 1955 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 1956 | + if ($mount) { |
|
| 1957 | + try { |
|
| 1958 | + $storage = $mount->getStorage(); |
|
| 1959 | + if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 1960 | + $storage->changeLock( |
|
| 1961 | + $mount->getInternalPath($absolutePath), |
|
| 1962 | + $type, |
|
| 1963 | + $this->lockingProvider |
|
| 1964 | + ); |
|
| 1965 | + } |
|
| 1966 | + } catch (\OCP\Lock\LockedException $e) { |
|
| 1967 | + try { |
|
| 1968 | + // rethrow with the a human-readable path |
|
| 1969 | + throw new \OCP\Lock\LockedException( |
|
| 1970 | + $this->getPathRelativeToFiles($absolutePath), |
|
| 1971 | + $e |
|
| 1972 | + ); |
|
| 1973 | + } catch (\InvalidArgumentException $e) { |
|
| 1974 | + throw new \OCP\Lock\LockedException( |
|
| 1975 | + $absolutePath, |
|
| 1976 | + $e |
|
| 1977 | + ); |
|
| 1978 | + } |
|
| 1979 | + } |
|
| 1980 | + } |
|
| 1981 | + |
|
| 1982 | + return true; |
|
| 1983 | + } |
|
| 1984 | + |
|
| 1985 | + /** |
|
| 1986 | + * Unlock the given path |
|
| 1987 | + * |
|
| 1988 | + * @param string $path the path of the file to unlock, relative to the view |
|
| 1989 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 1990 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 1991 | + * |
|
| 1992 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 1993 | + */ |
|
| 1994 | + private function unlockPath($path, $type, $lockMountPoint = false) { |
|
| 1995 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 1996 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 1997 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 1998 | + return false; |
|
| 1999 | + } |
|
| 2000 | + |
|
| 2001 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); |
|
| 2002 | + if ($mount) { |
|
| 2003 | + $storage = $mount->getStorage(); |
|
| 2004 | + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { |
|
| 2005 | + $storage->releaseLock( |
|
| 2006 | + $mount->getInternalPath($absolutePath), |
|
| 2007 | + $type, |
|
| 2008 | + $this->lockingProvider |
|
| 2009 | + ); |
|
| 2010 | + } |
|
| 2011 | + } |
|
| 2012 | + |
|
| 2013 | + return true; |
|
| 2014 | + } |
|
| 2015 | + |
|
| 2016 | + /** |
|
| 2017 | + * Lock a path and all its parents up to the root of the view |
|
| 2018 | + * |
|
| 2019 | + * @param string $path the path of the file to lock relative to the view |
|
| 2020 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2021 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2022 | + * |
|
| 2023 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2024 | + */ |
|
| 2025 | + public function lockFile($path, $type, $lockMountPoint = false) { |
|
| 2026 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2027 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2028 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2029 | + return false; |
|
| 2030 | + } |
|
| 2031 | + |
|
| 2032 | + $this->lockPath($path, $type, $lockMountPoint); |
|
| 2033 | + |
|
| 2034 | + $parents = $this->getParents($path); |
|
| 2035 | + foreach ($parents as $parent) { |
|
| 2036 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2037 | + } |
|
| 2038 | + |
|
| 2039 | + return true; |
|
| 2040 | + } |
|
| 2041 | + |
|
| 2042 | + /** |
|
| 2043 | + * Unlock a path and all its parents up to the root of the view |
|
| 2044 | + * |
|
| 2045 | + * @param string $path the path of the file to lock relative to the view |
|
| 2046 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 2047 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage |
|
| 2048 | + * |
|
| 2049 | + * @return bool False if the path is excluded from locking, true otherwise |
|
| 2050 | + */ |
|
| 2051 | + public function unlockFile($path, $type, $lockMountPoint = false) { |
|
| 2052 | + $absolutePath = $this->getAbsolutePath($path); |
|
| 2053 | + $absolutePath = Filesystem::normalizePath($absolutePath); |
|
| 2054 | + if (!$this->shouldLockFile($absolutePath)) { |
|
| 2055 | + return false; |
|
| 2056 | + } |
|
| 2057 | + |
|
| 2058 | + $this->unlockPath($path, $type, $lockMountPoint); |
|
| 2059 | + |
|
| 2060 | + $parents = $this->getParents($path); |
|
| 2061 | + foreach ($parents as $parent) { |
|
| 2062 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); |
|
| 2063 | + } |
|
| 2064 | + |
|
| 2065 | + return true; |
|
| 2066 | + } |
|
| 2067 | + |
|
| 2068 | + /** |
|
| 2069 | + * Only lock files in data/user/files/ |
|
| 2070 | + * |
|
| 2071 | + * @param string $path Absolute path to the file/folder we try to (un)lock |
|
| 2072 | + * @return bool |
|
| 2073 | + */ |
|
| 2074 | + protected function shouldLockFile($path) { |
|
| 2075 | + $path = Filesystem::normalizePath($path); |
|
| 2076 | + |
|
| 2077 | + $pathSegments = explode('/', $path); |
|
| 2078 | + if (isset($pathSegments[2])) { |
|
| 2079 | + // E.g.: /username/files/path-to-file |
|
| 2080 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); |
|
| 2081 | + } |
|
| 2082 | + |
|
| 2083 | + return strpos($path, '/appdata_') !== 0; |
|
| 2084 | + } |
|
| 2085 | + |
|
| 2086 | + /** |
|
| 2087 | + * Shortens the given absolute path to be relative to |
|
| 2088 | + * "$user/files". |
|
| 2089 | + * |
|
| 2090 | + * @param string $absolutePath absolute path which is under "files" |
|
| 2091 | + * |
|
| 2092 | + * @return string path relative to "files" with trimmed slashes or null |
|
| 2093 | + * if the path was NOT relative to files |
|
| 2094 | + * |
|
| 2095 | + * @throws \InvalidArgumentException if the given path was not under "files" |
|
| 2096 | + * @since 8.1.0 |
|
| 2097 | + */ |
|
| 2098 | + public function getPathRelativeToFiles($absolutePath) { |
|
| 2099 | + $path = Filesystem::normalizePath($absolutePath); |
|
| 2100 | + $parts = explode('/', trim($path, '/'), 3); |
|
| 2101 | + // "$user", "files", "path/to/dir" |
|
| 2102 | + if (!isset($parts[1]) || $parts[1] !== 'files') { |
|
| 2103 | + $this->logger->error( |
|
| 2104 | + '$absolutePath must be relative to "files", value is "%s"', |
|
| 2105 | + [ |
|
| 2106 | + $absolutePath |
|
| 2107 | + ] |
|
| 2108 | + ); |
|
| 2109 | + throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); |
|
| 2110 | + } |
|
| 2111 | + if (isset($parts[2])) { |
|
| 2112 | + return $parts[2]; |
|
| 2113 | + } |
|
| 2114 | + return ''; |
|
| 2115 | + } |
|
| 2116 | + |
|
| 2117 | + /** |
|
| 2118 | + * @param string $filename |
|
| 2119 | + * @return array |
|
| 2120 | + * @throws \OC\User\NoUserException |
|
| 2121 | + * @throws NotFoundException |
|
| 2122 | + */ |
|
| 2123 | + public function getUidAndFilename($filename) { |
|
| 2124 | + $info = $this->getFileInfo($filename); |
|
| 2125 | + if (!$info instanceof \OCP\Files\FileInfo) { |
|
| 2126 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2127 | + } |
|
| 2128 | + $uid = $info->getOwner()->getUID(); |
|
| 2129 | + if ($uid != \OCP\User::getUser()) { |
|
| 2130 | + Filesystem::initMountPoints($uid); |
|
| 2131 | + $ownerView = new View('/' . $uid . '/files'); |
|
| 2132 | + try { |
|
| 2133 | + $filename = $ownerView->getPath($info['fileid']); |
|
| 2134 | + } catch (NotFoundException $e) { |
|
| 2135 | + throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2136 | + } |
|
| 2137 | + } |
|
| 2138 | + return [$uid, $filename]; |
|
| 2139 | + } |
|
| 2140 | + |
|
| 2141 | + /** |
|
| 2142 | + * Creates parent non-existing folders |
|
| 2143 | + * |
|
| 2144 | + * @param string $filePath |
|
| 2145 | + * @return bool |
|
| 2146 | + */ |
|
| 2147 | + private function createParentDirectories($filePath) { |
|
| 2148 | + $directoryParts = explode('/', $filePath); |
|
| 2149 | + $directoryParts = array_filter($directoryParts); |
|
| 2150 | + foreach ($directoryParts as $key => $part) { |
|
| 2151 | + $currentPathElements = array_slice($directoryParts, 0, $key); |
|
| 2152 | + $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2153 | + if ($this->is_file($currentPath)) { |
|
| 2154 | + return false; |
|
| 2155 | + } |
|
| 2156 | + if (!$this->file_exists($currentPath)) { |
|
| 2157 | + $this->mkdir($currentPath); |
|
| 2158 | + } |
|
| 2159 | + } |
|
| 2160 | + |
|
| 2161 | + return true; |
|
| 2162 | + } |
|
| 2163 | 2163 | } |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | $path = '/'; |
| 127 | 127 | } |
| 128 | 128 | if ($path[0] !== '/') { |
| 129 | - $path = '/' . $path; |
|
| 129 | + $path = '/'.$path; |
|
| 130 | 130 | } |
| 131 | - return $this->fakeRoot . $path; |
|
| 131 | + return $this->fakeRoot.$path; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | public function chroot($fakeRoot) { |
| 141 | 141 | if (!$fakeRoot == '') { |
| 142 | 142 | if ($fakeRoot[0] !== '/') { |
| 143 | - $fakeRoot = '/' . $fakeRoot; |
|
| 143 | + $fakeRoot = '/'.$fakeRoot; |
|
| 144 | 144 | } |
| 145 | 145 | } |
| 146 | 146 | $this->fakeRoot = $fakeRoot; |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | // missing slashes can cause wrong matches! |
| 175 | - $root = rtrim($this->fakeRoot, '/') . '/'; |
|
| 175 | + $root = rtrim($this->fakeRoot, '/').'/'; |
|
| 176 | 176 | |
| 177 | 177 | if (strpos($path, $root) !== 0) { |
| 178 | 178 | return null; |
@@ -278,7 +278,7 @@ discard block |
||
| 278 | 278 | if ($mount instanceof MoveableMount) { |
| 279 | 279 | // cut of /user/files to get the relative path to data/user/files |
| 280 | 280 | $pathParts = explode('/', $path, 4); |
| 281 | - $relPath = '/' . $pathParts[3]; |
|
| 281 | + $relPath = '/'.$pathParts[3]; |
|
| 282 | 282 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); |
| 283 | 283 | \OC_Hook::emit( |
| 284 | 284 | Filesystem::CLASSNAME, "umount", |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | } |
| 699 | 699 | $postFix = (substr($path, -1) === '/') ? '/' : ''; |
| 700 | 700 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); |
| 701 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); |
|
| 701 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); |
|
| 702 | 702 | if ($mount and $mount->getInternalPath($absolutePath) === '') { |
| 703 | 703 | return $this->removeMount($mount, $absolutePath); |
| 704 | 704 | } |
@@ -818,7 +818,7 @@ discard block |
||
| 818 | 818 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); |
| 819 | 819 | } |
| 820 | 820 | } |
| 821 | - } catch(\Exception $e) { |
|
| 821 | + } catch (\Exception $e) { |
|
| 822 | 822 | throw $e; |
| 823 | 823 | } finally { |
| 824 | 824 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -842,7 +842,7 @@ discard block |
||
| 842 | 842 | } |
| 843 | 843 | } |
| 844 | 844 | } |
| 845 | - } catch(\Exception $e) { |
|
| 845 | + } catch (\Exception $e) { |
|
| 846 | 846 | throw $e; |
| 847 | 847 | } finally { |
| 848 | 848 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); |
@@ -975,7 +975,7 @@ discard block |
||
| 975 | 975 | $hooks[] = 'write'; |
| 976 | 976 | break; |
| 977 | 977 | default: |
| 978 | - \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); |
|
| 978 | + \OCP\Util::writeLog('core', 'invalid mode ('.$mode.') for '.$path, \OCP\Util::ERROR); |
|
| 979 | 979 | } |
| 980 | 980 | |
| 981 | 981 | if ($mode !== 'r' && $mode !== 'w') { |
@@ -1079,7 +1079,7 @@ discard block |
||
| 1079 | 1079 | array(Filesystem::signal_param_path => $this->getHookPath($path)) |
| 1080 | 1080 | ); |
| 1081 | 1081 | } |
| 1082 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1082 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1083 | 1083 | if ($storage) { |
| 1084 | 1084 | return $storage->hash($type, $internalPath, $raw); |
| 1085 | 1085 | } |
@@ -1133,7 +1133,7 @@ discard block |
||
| 1133 | 1133 | |
| 1134 | 1134 | $run = $this->runHooks($hooks, $path); |
| 1135 | 1135 | /** @var \OC\Files\Storage\Storage $storage */ |
| 1136 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); |
|
| 1136 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); |
|
| 1137 | 1137 | if ($run and $storage) { |
| 1138 | 1138 | if (in_array('write', $hooks) || in_array('delete', $hooks)) { |
| 1139 | 1139 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); |
@@ -1172,7 +1172,7 @@ discard block |
||
| 1172 | 1172 | $unlockLater = true; |
| 1173 | 1173 | // make sure our unlocking callback will still be called if connection is aborted |
| 1174 | 1174 | ignore_user_abort(true); |
| 1175 | - $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { |
|
| 1175 | + $result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { |
|
| 1176 | 1176 | if (in_array('write', $hooks)) { |
| 1177 | 1177 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); |
| 1178 | 1178 | } else if (in_array('read', $hooks)) { |
@@ -1233,7 +1233,7 @@ discard block |
||
| 1233 | 1233 | return true; |
| 1234 | 1234 | } |
| 1235 | 1235 | |
| 1236 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); |
|
| 1236 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); |
|
| 1237 | 1237 | } |
| 1238 | 1238 | |
| 1239 | 1239 | /** |
@@ -1252,7 +1252,7 @@ discard block |
||
| 1252 | 1252 | if ($hook != 'read') { |
| 1253 | 1253 | \OC_Hook::emit( |
| 1254 | 1254 | Filesystem::CLASSNAME, |
| 1255 | - $prefix . $hook, |
|
| 1255 | + $prefix.$hook, |
|
| 1256 | 1256 | array( |
| 1257 | 1257 | Filesystem::signal_param_run => &$run, |
| 1258 | 1258 | Filesystem::signal_param_path => $path |
@@ -1261,7 +1261,7 @@ discard block |
||
| 1261 | 1261 | } elseif (!$post) { |
| 1262 | 1262 | \OC_Hook::emit( |
| 1263 | 1263 | Filesystem::CLASSNAME, |
| 1264 | - $prefix . $hook, |
|
| 1264 | + $prefix.$hook, |
|
| 1265 | 1265 | array( |
| 1266 | 1266 | Filesystem::signal_param_path => $path |
| 1267 | 1267 | ) |
@@ -1356,7 +1356,7 @@ discard block |
||
| 1356 | 1356 | return $this->getPartFileInfo($path); |
| 1357 | 1357 | } |
| 1358 | 1358 | $relativePath = $path; |
| 1359 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1359 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1360 | 1360 | |
| 1361 | 1361 | $mount = Filesystem::getMountManager()->find($path); |
| 1362 | 1362 | if (!$mount) { |
@@ -1383,7 +1383,7 @@ discard block |
||
| 1383 | 1383 | //add the sizes of other mount points to the folder |
| 1384 | 1384 | $extOnly = ($includeMountPoints === 'ext'); |
| 1385 | 1385 | $mounts = Filesystem::getMountManager()->findIn($path); |
| 1386 | - $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { |
|
| 1386 | + $info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { |
|
| 1387 | 1387 | $subStorage = $mount->getStorage(); |
| 1388 | 1388 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); |
| 1389 | 1389 | })); |
@@ -1433,12 +1433,12 @@ discard block |
||
| 1433 | 1433 | /** |
| 1434 | 1434 | * @var \OC\Files\FileInfo[] $files |
| 1435 | 1435 | */ |
| 1436 | - $files = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1436 | + $files = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { |
|
| 1437 | 1437 | if ($sharingDisabled) { |
| 1438 | 1438 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; |
| 1439 | 1439 | } |
| 1440 | 1440 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); |
| 1441 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1441 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); |
|
| 1442 | 1442 | }, $contents); |
| 1443 | 1443 | |
| 1444 | 1444 | //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders |
@@ -1462,7 +1462,7 @@ discard block |
||
| 1462 | 1462 | } catch (\Exception $e) { |
| 1463 | 1463 | // sometimes when the storage is not available it can be any exception |
| 1464 | 1464 | \OC::$server->getLogger()->logException($e, [ |
| 1465 | - 'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"', |
|
| 1465 | + 'message' => 'Exception while scanning storage "'.$subStorage->getId().'"', |
|
| 1466 | 1466 | 'level' => \OCP\Util::ERROR, |
| 1467 | 1467 | 'app' => 'lib', |
| 1468 | 1468 | ]); |
@@ -1500,7 +1500,7 @@ discard block |
||
| 1500 | 1500 | break; |
| 1501 | 1501 | } |
| 1502 | 1502 | } |
| 1503 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1503 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ |
|
| 1504 | 1504 | |
| 1505 | 1505 | // if sharing was disabled for the user we remove the share permissions |
| 1506 | 1506 | if (\OCP\Util::isSharingDisabledForUser()) { |
@@ -1508,14 +1508,14 @@ discard block |
||
| 1508 | 1508 | } |
| 1509 | 1509 | |
| 1510 | 1510 | $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); |
| 1511 | - $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1511 | + $files[] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); |
|
| 1512 | 1512 | } |
| 1513 | 1513 | } |
| 1514 | 1514 | } |
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | 1517 | if ($mimetype_filter) { |
| 1518 | - $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { |
|
| 1518 | + $files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { |
|
| 1519 | 1519 | if (strpos($mimetype_filter, '/')) { |
| 1520 | 1520 | return $file->getMimetype() === $mimetype_filter; |
| 1521 | 1521 | } else { |
@@ -1544,7 +1544,7 @@ discard block |
||
| 1544 | 1544 | if ($data instanceof FileInfo) { |
| 1545 | 1545 | $data = $data->getData(); |
| 1546 | 1546 | } |
| 1547 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); |
|
| 1547 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); |
|
| 1548 | 1548 | /** |
| 1549 | 1549 | * @var \OC\Files\Storage\Storage $storage |
| 1550 | 1550 | * @var string $internalPath |
@@ -1571,7 +1571,7 @@ discard block |
||
| 1571 | 1571 | * @return FileInfo[] |
| 1572 | 1572 | */ |
| 1573 | 1573 | public function search($query) { |
| 1574 | - return $this->searchCommon('search', array('%' . $query . '%')); |
|
| 1574 | + return $this->searchCommon('search', array('%'.$query.'%')); |
|
| 1575 | 1575 | } |
| 1576 | 1576 | |
| 1577 | 1577 | /** |
@@ -1622,10 +1622,10 @@ discard block |
||
| 1622 | 1622 | |
| 1623 | 1623 | $results = call_user_func_array(array($cache, $method), $args); |
| 1624 | 1624 | foreach ($results as $result) { |
| 1625 | - if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { |
|
| 1625 | + if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { |
|
| 1626 | 1626 | $internalPath = $result['path']; |
| 1627 | - $path = $mountPoint . $result['path']; |
|
| 1628 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); |
|
| 1627 | + $path = $mountPoint.$result['path']; |
|
| 1628 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); |
|
| 1629 | 1629 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1630 | 1630 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1631 | 1631 | } |
@@ -1643,8 +1643,8 @@ discard block |
||
| 1643 | 1643 | if ($results) { |
| 1644 | 1644 | foreach ($results as $result) { |
| 1645 | 1645 | $internalPath = $result['path']; |
| 1646 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); |
|
| 1647 | - $path = rtrim($mountPoint . $internalPath, '/'); |
|
| 1646 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); |
|
| 1647 | + $path = rtrim($mountPoint.$internalPath, '/'); |
|
| 1648 | 1648 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); |
| 1649 | 1649 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); |
| 1650 | 1650 | } |
@@ -1665,7 +1665,7 @@ discard block |
||
| 1665 | 1665 | public function getOwner($path) { |
| 1666 | 1666 | $info = $this->getFileInfo($path); |
| 1667 | 1667 | if (!$info) { |
| 1668 | - throw new NotFoundException($path . ' not found while trying to get owner'); |
|
| 1668 | + throw new NotFoundException($path.' not found while trying to get owner'); |
|
| 1669 | 1669 | } |
| 1670 | 1670 | return $info->getOwner()->getUID(); |
| 1671 | 1671 | } |
@@ -1699,7 +1699,7 @@ discard block |
||
| 1699 | 1699 | * @return string |
| 1700 | 1700 | */ |
| 1701 | 1701 | public function getPath($id) { |
| 1702 | - $id = (int)$id; |
|
| 1702 | + $id = (int) $id; |
|
| 1703 | 1703 | $manager = Filesystem::getMountManager(); |
| 1704 | 1704 | $mounts = $manager->findIn($this->fakeRoot); |
| 1705 | 1705 | $mounts[] = $manager->find($this->fakeRoot); |
@@ -1714,7 +1714,7 @@ discard block |
||
| 1714 | 1714 | $cache = $mount->getStorage()->getCache(); |
| 1715 | 1715 | $internalPath = $cache->getPathById($id); |
| 1716 | 1716 | if (is_string($internalPath)) { |
| 1717 | - $fullPath = $mount->getMountPoint() . $internalPath; |
|
| 1717 | + $fullPath = $mount->getMountPoint().$internalPath; |
|
| 1718 | 1718 | if (!is_null($path = $this->getRelativePath($fullPath))) { |
| 1719 | 1719 | return $path; |
| 1720 | 1720 | } |
@@ -1757,10 +1757,10 @@ discard block |
||
| 1757 | 1757 | } |
| 1758 | 1758 | |
| 1759 | 1759 | // note: cannot use the view because the target is already locked |
| 1760 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); |
|
| 1760 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); |
|
| 1761 | 1761 | if ($fileId === -1) { |
| 1762 | 1762 | // target might not exist, need to check parent instead |
| 1763 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1763 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); |
|
| 1764 | 1764 | } |
| 1765 | 1765 | |
| 1766 | 1766 | // check if any of the parents were shared by the current owner (include collections) |
@@ -1860,7 +1860,7 @@ discard block |
||
| 1860 | 1860 | $resultPath = ''; |
| 1861 | 1861 | foreach ($parts as $part) { |
| 1862 | 1862 | if ($part) { |
| 1863 | - $resultPath .= '/' . $part; |
|
| 1863 | + $resultPath .= '/'.$part; |
|
| 1864 | 1864 | $result[] = $resultPath; |
| 1865 | 1865 | } |
| 1866 | 1866 | } |
@@ -2123,16 +2123,16 @@ discard block |
||
| 2123 | 2123 | public function getUidAndFilename($filename) { |
| 2124 | 2124 | $info = $this->getFileInfo($filename); |
| 2125 | 2125 | if (!$info instanceof \OCP\Files\FileInfo) { |
| 2126 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); |
|
| 2126 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); |
|
| 2127 | 2127 | } |
| 2128 | 2128 | $uid = $info->getOwner()->getUID(); |
| 2129 | 2129 | if ($uid != \OCP\User::getUser()) { |
| 2130 | 2130 | Filesystem::initMountPoints($uid); |
| 2131 | - $ownerView = new View('/' . $uid . '/files'); |
|
| 2131 | + $ownerView = new View('/'.$uid.'/files'); |
|
| 2132 | 2132 | try { |
| 2133 | 2133 | $filename = $ownerView->getPath($info['fileid']); |
| 2134 | 2134 | } catch (NotFoundException $e) { |
| 2135 | - throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); |
|
| 2135 | + throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); |
|
| 2136 | 2136 | } |
| 2137 | 2137 | } |
| 2138 | 2138 | return [$uid, $filename]; |
@@ -2149,7 +2149,7 @@ discard block |
||
| 2149 | 2149 | $directoryParts = array_filter($directoryParts); |
| 2150 | 2150 | foreach ($directoryParts as $key => $part) { |
| 2151 | 2151 | $currentPathElements = array_slice($directoryParts, 0, $key); |
| 2152 | - $currentPath = '/' . implode('/', $currentPathElements); |
|
| 2152 | + $currentPath = '/'.implode('/', $currentPathElements); |
|
| 2153 | 2153 | if ($this->is_file($currentPath)) { |
| 2154 | 2154 | return false; |
| 2155 | 2155 | } |
@@ -29,56 +29,56 @@ |
||
| 29 | 29 | * @package OC\App\AppStore |
| 30 | 30 | */ |
| 31 | 31 | class VersionParser { |
| 32 | - /** |
|
| 33 | - * @param string $versionString |
|
| 34 | - * @return bool |
|
| 35 | - */ |
|
| 36 | - private function isValidVersionString($versionString) { |
|
| 37 | - return (bool)preg_match('/^[0-9.]+$/', $versionString); |
|
| 38 | - } |
|
| 32 | + /** |
|
| 33 | + * @param string $versionString |
|
| 34 | + * @return bool |
|
| 35 | + */ |
|
| 36 | + private function isValidVersionString($versionString) { |
|
| 37 | + return (bool)preg_match('/^[0-9.]+$/', $versionString); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Returns the version for a version string |
|
| 42 | - * |
|
| 43 | - * @param string $versionSpec |
|
| 44 | - * @return Version |
|
| 45 | - * @throws \Exception If the version cannot be parsed |
|
| 46 | - */ |
|
| 47 | - public function getVersion($versionSpec) { |
|
| 48 | - // * indicates that the version is compatible with all versions |
|
| 49 | - if($versionSpec === '*') { |
|
| 50 | - return new Version('', ''); |
|
| 51 | - } |
|
| 40 | + /** |
|
| 41 | + * Returns the version for a version string |
|
| 42 | + * |
|
| 43 | + * @param string $versionSpec |
|
| 44 | + * @return Version |
|
| 45 | + * @throws \Exception If the version cannot be parsed |
|
| 46 | + */ |
|
| 47 | + public function getVersion($versionSpec) { |
|
| 48 | + // * indicates that the version is compatible with all versions |
|
| 49 | + if($versionSpec === '*') { |
|
| 50 | + return new Version('', ''); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - // Count the amount of =, if it is one then it's either maximum or minimum |
|
| 54 | - // version. If it is two then it is maximum and minimum. |
|
| 55 | - $versionElements = explode(' ', $versionSpec); |
|
| 56 | - $firstVersion = isset($versionElements[0]) ? $versionElements[0] : ''; |
|
| 57 | - $firstVersionNumber = substr($firstVersion, 2); |
|
| 58 | - $secondVersion = isset($versionElements[1]) ? $versionElements[1] : ''; |
|
| 59 | - $secondVersionNumber = substr($secondVersion, 2); |
|
| 53 | + // Count the amount of =, if it is one then it's either maximum or minimum |
|
| 54 | + // version. If it is two then it is maximum and minimum. |
|
| 55 | + $versionElements = explode(' ', $versionSpec); |
|
| 56 | + $firstVersion = isset($versionElements[0]) ? $versionElements[0] : ''; |
|
| 57 | + $firstVersionNumber = substr($firstVersion, 2); |
|
| 58 | + $secondVersion = isset($versionElements[1]) ? $versionElements[1] : ''; |
|
| 59 | + $secondVersionNumber = substr($secondVersion, 2); |
|
| 60 | 60 | |
| 61 | - switch(count($versionElements)) { |
|
| 62 | - case 1: |
|
| 63 | - if(!$this->isValidVersionString($firstVersionNumber)) { |
|
| 64 | - break; |
|
| 65 | - } |
|
| 66 | - if(strpos($firstVersion, '>') === 0) { |
|
| 67 | - return new Version($firstVersionNumber, ''); |
|
| 68 | - } |
|
| 69 | - return new Version('', $firstVersionNumber); |
|
| 70 | - case 2: |
|
| 71 | - if(!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) { |
|
| 72 | - break; |
|
| 73 | - } |
|
| 74 | - return new Version($firstVersionNumber, $secondVersionNumber); |
|
| 75 | - } |
|
| 61 | + switch(count($versionElements)) { |
|
| 62 | + case 1: |
|
| 63 | + if(!$this->isValidVersionString($firstVersionNumber)) { |
|
| 64 | + break; |
|
| 65 | + } |
|
| 66 | + if(strpos($firstVersion, '>') === 0) { |
|
| 67 | + return new Version($firstVersionNumber, ''); |
|
| 68 | + } |
|
| 69 | + return new Version('', $firstVersionNumber); |
|
| 70 | + case 2: |
|
| 71 | + if(!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) { |
|
| 72 | + break; |
|
| 73 | + } |
|
| 74 | + return new Version($firstVersionNumber, $secondVersionNumber); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - throw new \Exception( |
|
| 78 | - sprintf( |
|
| 79 | - 'Version cannot be parsed: %s', |
|
| 80 | - $versionSpec |
|
| 81 | - ) |
|
| 82 | - ); |
|
| 83 | - } |
|
| 77 | + throw new \Exception( |
|
| 78 | + sprintf( |
|
| 79 | + 'Version cannot be parsed: %s', |
|
| 80 | + $versionSpec |
|
| 81 | + ) |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * @return bool |
| 35 | 35 | */ |
| 36 | 36 | private function isValidVersionString($versionString) { |
| 37 | - return (bool)preg_match('/^[0-9.]+$/', $versionString); |
|
| 37 | + return (bool) preg_match('/^[0-9.]+$/', $versionString); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | */ |
| 47 | 47 | public function getVersion($versionSpec) { |
| 48 | 48 | // * indicates that the version is compatible with all versions |
| 49 | - if($versionSpec === '*') { |
|
| 49 | + if ($versionSpec === '*') { |
|
| 50 | 50 | return new Version('', ''); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,17 +58,17 @@ discard block |
||
| 58 | 58 | $secondVersion = isset($versionElements[1]) ? $versionElements[1] : ''; |
| 59 | 59 | $secondVersionNumber = substr($secondVersion, 2); |
| 60 | 60 | |
| 61 | - switch(count($versionElements)) { |
|
| 61 | + switch (count($versionElements)) { |
|
| 62 | 62 | case 1: |
| 63 | - if(!$this->isValidVersionString($firstVersionNumber)) { |
|
| 63 | + if (!$this->isValidVersionString($firstVersionNumber)) { |
|
| 64 | 64 | break; |
| 65 | 65 | } |
| 66 | - if(strpos($firstVersion, '>') === 0) { |
|
| 66 | + if (strpos($firstVersion, '>') === 0) { |
|
| 67 | 67 | return new Version($firstVersionNumber, ''); |
| 68 | 68 | } |
| 69 | 69 | return new Version('', $firstVersionNumber); |
| 70 | 70 | case 2: |
| 71 | - if(!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) { |
|
| 71 | + if (!$this->isValidVersionString($firstVersionNumber) || !$this->isValidVersionString($secondVersionNumber)) { |
|
| 72 | 72 | break; |
| 73 | 73 | } |
| 74 | 74 | return new Version($firstVersionNumber, $secondVersionNumber); |
@@ -35,42 +35,42 @@ |
||
| 35 | 35 | * |
| 36 | 36 | */ |
| 37 | 37 | class OwnCloud extends \OC\Files\Storage\DAV{ |
| 38 | - const OC_URL_SUFFIX = 'remote.php/webdav'; |
|
| 38 | + const OC_URL_SUFFIX = 'remote.php/webdav'; |
|
| 39 | 39 | |
| 40 | - public function __construct($params) { |
|
| 41 | - // extract context path from host if specified |
|
| 42 | - // (owncloud install path on host) |
|
| 43 | - $host = $params['host']; |
|
| 44 | - // strip protocol |
|
| 45 | - if (substr($host, 0, 8) === "https://") { |
|
| 46 | - $host = substr($host, 8); |
|
| 47 | - $params['secure'] = true; |
|
| 48 | - } else if (substr($host, 0, 7) === "http://") { |
|
| 49 | - $host = substr($host, 7); |
|
| 50 | - $params['secure'] = false; |
|
| 51 | - } |
|
| 52 | - $contextPath = ''; |
|
| 53 | - $hostSlashPos = strpos($host, '/'); |
|
| 54 | - if ($hostSlashPos !== false){ |
|
| 55 | - $contextPath = substr($host, $hostSlashPos); |
|
| 56 | - $host = substr($host, 0, $hostSlashPos); |
|
| 57 | - } |
|
| 40 | + public function __construct($params) { |
|
| 41 | + // extract context path from host if specified |
|
| 42 | + // (owncloud install path on host) |
|
| 43 | + $host = $params['host']; |
|
| 44 | + // strip protocol |
|
| 45 | + if (substr($host, 0, 8) === "https://") { |
|
| 46 | + $host = substr($host, 8); |
|
| 47 | + $params['secure'] = true; |
|
| 48 | + } else if (substr($host, 0, 7) === "http://") { |
|
| 49 | + $host = substr($host, 7); |
|
| 50 | + $params['secure'] = false; |
|
| 51 | + } |
|
| 52 | + $contextPath = ''; |
|
| 53 | + $hostSlashPos = strpos($host, '/'); |
|
| 54 | + if ($hostSlashPos !== false){ |
|
| 55 | + $contextPath = substr($host, $hostSlashPos); |
|
| 56 | + $host = substr($host, 0, $hostSlashPos); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - if (substr($contextPath, -1) !== '/'){ |
|
| 60 | - $contextPath .= '/'; |
|
| 61 | - } |
|
| 59 | + if (substr($contextPath, -1) !== '/'){ |
|
| 60 | + $contextPath .= '/'; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - if (isset($params['root'])){ |
|
| 64 | - $root = '/' . ltrim($params['root'], '/'); |
|
| 65 | - } |
|
| 66 | - else{ |
|
| 67 | - $root = '/'; |
|
| 68 | - } |
|
| 63 | + if (isset($params['root'])){ |
|
| 64 | + $root = '/' . ltrim($params['root'], '/'); |
|
| 65 | + } |
|
| 66 | + else{ |
|
| 67 | + $root = '/'; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $params['host'] = $host; |
|
| 71 | - $params['root'] = $contextPath . self::OC_URL_SUFFIX . $root; |
|
| 72 | - $params['authType'] = Client::AUTH_BASIC; |
|
| 70 | + $params['host'] = $host; |
|
| 71 | + $params['root'] = $contextPath . self::OC_URL_SUFFIX . $root; |
|
| 72 | + $params['authType'] = Client::AUTH_BASIC; |
|
| 73 | 73 | |
| 74 | - parent::__construct($params); |
|
| 75 | - } |
|
| 74 | + parent::__construct($params); |
|
| 75 | + } |
|
| 76 | 76 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * http://%host/%context/remote.php/webdav/%root |
| 35 | 35 | * |
| 36 | 36 | */ |
| 37 | -class OwnCloud extends \OC\Files\Storage\DAV{ |
|
| 37 | +class OwnCloud extends \OC\Files\Storage\DAV { |
|
| 38 | 38 | const OC_URL_SUFFIX = 'remote.php/webdav'; |
| 39 | 39 | |
| 40 | 40 | public function __construct($params) { |
@@ -51,24 +51,24 @@ discard block |
||
| 51 | 51 | } |
| 52 | 52 | $contextPath = ''; |
| 53 | 53 | $hostSlashPos = strpos($host, '/'); |
| 54 | - if ($hostSlashPos !== false){ |
|
| 54 | + if ($hostSlashPos !== false) { |
|
| 55 | 55 | $contextPath = substr($host, $hostSlashPos); |
| 56 | 56 | $host = substr($host, 0, $hostSlashPos); |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - if (substr($contextPath, -1) !== '/'){ |
|
| 59 | + if (substr($contextPath, -1) !== '/') { |
|
| 60 | 60 | $contextPath .= '/'; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - if (isset($params['root'])){ |
|
| 64 | - $root = '/' . ltrim($params['root'], '/'); |
|
| 63 | + if (isset($params['root'])) { |
|
| 64 | + $root = '/'.ltrim($params['root'], '/'); |
|
| 65 | 65 | } |
| 66 | - else{ |
|
| 66 | + else { |
|
| 67 | 67 | $root = '/'; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | $params['host'] = $host; |
| 71 | - $params['root'] = $contextPath . self::OC_URL_SUFFIX . $root; |
|
| 71 | + $params['root'] = $contextPath.self::OC_URL_SUFFIX.$root; |
|
| 72 | 72 | $params['authType'] = Client::AUTH_BASIC; |
| 73 | 73 | |
| 74 | 74 | parent::__construct($params); |
@@ -62,8 +62,7 @@ |
||
| 62 | 62 | |
| 63 | 63 | if (isset($params['root'])){ |
| 64 | 64 | $root = '/' . ltrim($params['root'], '/'); |
| 65 | - } |
|
| 66 | - else{ |
|
| 65 | + } else{ |
|
| 67 | 66 | $root = '/'; |
| 68 | 67 | } |
| 69 | 68 | |
@@ -35,350 +35,350 @@ |
||
| 35 | 35 | use Icewind\Streams\CallbackWrapper; |
| 36 | 36 | |
| 37 | 37 | class TAR extends Archive { |
| 38 | - const PLAIN = 0; |
|
| 39 | - const GZIP = 1; |
|
| 40 | - const BZIP = 2; |
|
| 38 | + const PLAIN = 0; |
|
| 39 | + const GZIP = 1; |
|
| 40 | + const BZIP = 2; |
|
| 41 | 41 | |
| 42 | - private $fileList; |
|
| 43 | - private $cachedHeaders; |
|
| 42 | + private $fileList; |
|
| 43 | + private $cachedHeaders; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @var \Archive_Tar tar |
|
| 47 | - */ |
|
| 48 | - private $tar = null; |
|
| 49 | - private $path; |
|
| 45 | + /** |
|
| 46 | + * @var \Archive_Tar tar |
|
| 47 | + */ |
|
| 48 | + private $tar = null; |
|
| 49 | + private $path; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param string $source |
|
| 53 | - */ |
|
| 54 | - public function __construct($source) { |
|
| 55 | - $types = array(null, 'gz', 'bz2'); |
|
| 56 | - $this->path = $source; |
|
| 57 | - $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]); |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * @param string $source |
|
| 53 | + */ |
|
| 54 | + public function __construct($source) { |
|
| 55 | + $types = array(null, 'gz', 'bz2'); |
|
| 56 | + $this->path = $source; |
|
| 57 | + $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * try to detect the type of tar compression |
|
| 62 | - * |
|
| 63 | - * @param string $file |
|
| 64 | - * @return integer |
|
| 65 | - */ |
|
| 66 | - static public function getTarType($file) { |
|
| 67 | - if (strpos($file, '.')) { |
|
| 68 | - $extension = substr($file, strrpos($file, '.')); |
|
| 69 | - switch ($extension) { |
|
| 70 | - case '.gz': |
|
| 71 | - case '.tgz': |
|
| 72 | - return self::GZIP; |
|
| 73 | - case '.bz': |
|
| 74 | - case '.bz2': |
|
| 75 | - return self::BZIP; |
|
| 76 | - case '.tar': |
|
| 77 | - return self::PLAIN; |
|
| 78 | - default: |
|
| 79 | - return self::PLAIN; |
|
| 80 | - } |
|
| 81 | - } else { |
|
| 82 | - return self::PLAIN; |
|
| 83 | - } |
|
| 84 | - } |
|
| 60 | + /** |
|
| 61 | + * try to detect the type of tar compression |
|
| 62 | + * |
|
| 63 | + * @param string $file |
|
| 64 | + * @return integer |
|
| 65 | + */ |
|
| 66 | + static public function getTarType($file) { |
|
| 67 | + if (strpos($file, '.')) { |
|
| 68 | + $extension = substr($file, strrpos($file, '.')); |
|
| 69 | + switch ($extension) { |
|
| 70 | + case '.gz': |
|
| 71 | + case '.tgz': |
|
| 72 | + return self::GZIP; |
|
| 73 | + case '.bz': |
|
| 74 | + case '.bz2': |
|
| 75 | + return self::BZIP; |
|
| 76 | + case '.tar': |
|
| 77 | + return self::PLAIN; |
|
| 78 | + default: |
|
| 79 | + return self::PLAIN; |
|
| 80 | + } |
|
| 81 | + } else { |
|
| 82 | + return self::PLAIN; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * add an empty folder to the archive |
|
| 88 | - * |
|
| 89 | - * @param string $path |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function addFolder($path) { |
|
| 93 | - $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder(); |
|
| 94 | - $path = rtrim($path, '/') . '/'; |
|
| 95 | - if ($this->fileExists($path)) { |
|
| 96 | - return false; |
|
| 97 | - } |
|
| 98 | - $parts = explode('/', $path); |
|
| 99 | - $folder = $tmpBase; |
|
| 100 | - foreach ($parts as $part) { |
|
| 101 | - $folder .= '/' . $part; |
|
| 102 | - if (!is_dir($folder)) { |
|
| 103 | - mkdir($folder); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase); |
|
| 107 | - rmdir($tmpBase . $path); |
|
| 108 | - $this->fileList = false; |
|
| 109 | - $this->cachedHeaders = false; |
|
| 110 | - return $result; |
|
| 111 | - } |
|
| 86 | + /** |
|
| 87 | + * add an empty folder to the archive |
|
| 88 | + * |
|
| 89 | + * @param string $path |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function addFolder($path) { |
|
| 93 | + $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder(); |
|
| 94 | + $path = rtrim($path, '/') . '/'; |
|
| 95 | + if ($this->fileExists($path)) { |
|
| 96 | + return false; |
|
| 97 | + } |
|
| 98 | + $parts = explode('/', $path); |
|
| 99 | + $folder = $tmpBase; |
|
| 100 | + foreach ($parts as $part) { |
|
| 101 | + $folder .= '/' . $part; |
|
| 102 | + if (!is_dir($folder)) { |
|
| 103 | + mkdir($folder); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase); |
|
| 107 | + rmdir($tmpBase . $path); |
|
| 108 | + $this->fileList = false; |
|
| 109 | + $this->cachedHeaders = false; |
|
| 110 | + return $result; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * add a file to the archive |
|
| 115 | - * |
|
| 116 | - * @param string $path |
|
| 117 | - * @param string $source either a local file or string data |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function addFile($path, $source = '') { |
|
| 121 | - if ($this->fileExists($path)) { |
|
| 122 | - $this->remove($path); |
|
| 123 | - } |
|
| 124 | - if ($source and $source[0] == '/' and file_exists($source)) { |
|
| 125 | - $source = file_get_contents($source); |
|
| 126 | - } |
|
| 127 | - $result = $this->tar->addString($path, $source); |
|
| 128 | - $this->fileList = false; |
|
| 129 | - $this->cachedHeaders = false; |
|
| 130 | - return $result; |
|
| 131 | - } |
|
| 113 | + /** |
|
| 114 | + * add a file to the archive |
|
| 115 | + * |
|
| 116 | + * @param string $path |
|
| 117 | + * @param string $source either a local file or string data |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function addFile($path, $source = '') { |
|
| 121 | + if ($this->fileExists($path)) { |
|
| 122 | + $this->remove($path); |
|
| 123 | + } |
|
| 124 | + if ($source and $source[0] == '/' and file_exists($source)) { |
|
| 125 | + $source = file_get_contents($source); |
|
| 126 | + } |
|
| 127 | + $result = $this->tar->addString($path, $source); |
|
| 128 | + $this->fileList = false; |
|
| 129 | + $this->cachedHeaders = false; |
|
| 130 | + return $result; |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * rename a file or folder in the archive |
|
| 135 | - * |
|
| 136 | - * @param string $source |
|
| 137 | - * @param string $dest |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - public function rename($source, $dest) { |
|
| 141 | - //no proper way to delete, rename entire archive, rename file and remake archive |
|
| 142 | - $tmp = \OCP\Files::tmpFolder(); |
|
| 143 | - $this->tar->extract($tmp); |
|
| 144 | - rename($tmp . $source, $tmp . $dest); |
|
| 145 | - $this->tar = null; |
|
| 146 | - unlink($this->path); |
|
| 147 | - $types = array(null, 'gz', 'bz'); |
|
| 148 | - $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]); |
|
| 149 | - $this->tar->createModify(array($tmp), '', $tmp . '/'); |
|
| 150 | - $this->fileList = false; |
|
| 151 | - $this->cachedHeaders = false; |
|
| 152 | - return true; |
|
| 153 | - } |
|
| 133 | + /** |
|
| 134 | + * rename a file or folder in the archive |
|
| 135 | + * |
|
| 136 | + * @param string $source |
|
| 137 | + * @param string $dest |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + public function rename($source, $dest) { |
|
| 141 | + //no proper way to delete, rename entire archive, rename file and remake archive |
|
| 142 | + $tmp = \OCP\Files::tmpFolder(); |
|
| 143 | + $this->tar->extract($tmp); |
|
| 144 | + rename($tmp . $source, $tmp . $dest); |
|
| 145 | + $this->tar = null; |
|
| 146 | + unlink($this->path); |
|
| 147 | + $types = array(null, 'gz', 'bz'); |
|
| 148 | + $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]); |
|
| 149 | + $this->tar->createModify(array($tmp), '', $tmp . '/'); |
|
| 150 | + $this->fileList = false; |
|
| 151 | + $this->cachedHeaders = false; |
|
| 152 | + return true; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * @param string $file |
|
| 157 | - */ |
|
| 158 | - private function getHeader($file) { |
|
| 159 | - if (!$this->cachedHeaders) { |
|
| 160 | - $this->cachedHeaders = $this->tar->listContent(); |
|
| 161 | - } |
|
| 162 | - foreach ($this->cachedHeaders as $header) { |
|
| 163 | - if ($file == $header['filename'] |
|
| 164 | - or $file . '/' == $header['filename'] |
|
| 165 | - or '/' . $file . '/' == $header['filename'] |
|
| 166 | - or '/' . $file == $header['filename'] |
|
| 167 | - ) { |
|
| 168 | - return $header; |
|
| 169 | - } |
|
| 170 | - } |
|
| 171 | - return null; |
|
| 172 | - } |
|
| 155 | + /** |
|
| 156 | + * @param string $file |
|
| 157 | + */ |
|
| 158 | + private function getHeader($file) { |
|
| 159 | + if (!$this->cachedHeaders) { |
|
| 160 | + $this->cachedHeaders = $this->tar->listContent(); |
|
| 161 | + } |
|
| 162 | + foreach ($this->cachedHeaders as $header) { |
|
| 163 | + if ($file == $header['filename'] |
|
| 164 | + or $file . '/' == $header['filename'] |
|
| 165 | + or '/' . $file . '/' == $header['filename'] |
|
| 166 | + or '/' . $file == $header['filename'] |
|
| 167 | + ) { |
|
| 168 | + return $header; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | + return null; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * get the uncompressed size of a file in the archive |
|
| 176 | - * |
|
| 177 | - * @param string $path |
|
| 178 | - * @return int |
|
| 179 | - */ |
|
| 180 | - public function filesize($path) { |
|
| 181 | - $stat = $this->getHeader($path); |
|
| 182 | - return $stat['size']; |
|
| 183 | - } |
|
| 174 | + /** |
|
| 175 | + * get the uncompressed size of a file in the archive |
|
| 176 | + * |
|
| 177 | + * @param string $path |
|
| 178 | + * @return int |
|
| 179 | + */ |
|
| 180 | + public function filesize($path) { |
|
| 181 | + $stat = $this->getHeader($path); |
|
| 182 | + return $stat['size']; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * get the last modified time of a file in the archive |
|
| 187 | - * |
|
| 188 | - * @param string $path |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function mtime($path) { |
|
| 192 | - $stat = $this->getHeader($path); |
|
| 193 | - return $stat['mtime']; |
|
| 194 | - } |
|
| 185 | + /** |
|
| 186 | + * get the last modified time of a file in the archive |
|
| 187 | + * |
|
| 188 | + * @param string $path |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function mtime($path) { |
|
| 192 | + $stat = $this->getHeader($path); |
|
| 193 | + return $stat['mtime']; |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * get the files in a folder |
|
| 198 | - * |
|
| 199 | - * @param string $path |
|
| 200 | - * @return array |
|
| 201 | - */ |
|
| 202 | - public function getFolder($path) { |
|
| 203 | - $files = $this->getFiles(); |
|
| 204 | - $folderContent = array(); |
|
| 205 | - $pathLength = strlen($path); |
|
| 206 | - foreach ($files as $file) { |
|
| 207 | - if ($file[0] == '/') { |
|
| 208 | - $file = substr($file, 1); |
|
| 209 | - } |
|
| 210 | - if (substr($file, 0, $pathLength) == $path and $file != $path) { |
|
| 211 | - $result = substr($file, $pathLength); |
|
| 212 | - if ($pos = strpos($result, '/')) { |
|
| 213 | - $result = substr($result, 0, $pos + 1); |
|
| 214 | - } |
|
| 215 | - if (array_search($result, $folderContent) === false) { |
|
| 216 | - $folderContent[] = $result; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - return $folderContent; |
|
| 221 | - } |
|
| 196 | + /** |
|
| 197 | + * get the files in a folder |
|
| 198 | + * |
|
| 199 | + * @param string $path |
|
| 200 | + * @return array |
|
| 201 | + */ |
|
| 202 | + public function getFolder($path) { |
|
| 203 | + $files = $this->getFiles(); |
|
| 204 | + $folderContent = array(); |
|
| 205 | + $pathLength = strlen($path); |
|
| 206 | + foreach ($files as $file) { |
|
| 207 | + if ($file[0] == '/') { |
|
| 208 | + $file = substr($file, 1); |
|
| 209 | + } |
|
| 210 | + if (substr($file, 0, $pathLength) == $path and $file != $path) { |
|
| 211 | + $result = substr($file, $pathLength); |
|
| 212 | + if ($pos = strpos($result, '/')) { |
|
| 213 | + $result = substr($result, 0, $pos + 1); |
|
| 214 | + } |
|
| 215 | + if (array_search($result, $folderContent) === false) { |
|
| 216 | + $folderContent[] = $result; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + return $folderContent; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * get all files in the archive |
|
| 225 | - * |
|
| 226 | - * @return array |
|
| 227 | - */ |
|
| 228 | - public function getFiles() { |
|
| 229 | - if ($this->fileList) { |
|
| 230 | - return $this->fileList; |
|
| 231 | - } |
|
| 232 | - if (!$this->cachedHeaders) { |
|
| 233 | - $this->cachedHeaders = $this->tar->listContent(); |
|
| 234 | - } |
|
| 235 | - $files = array(); |
|
| 236 | - foreach ($this->cachedHeaders as $header) { |
|
| 237 | - $files[] = $header['filename']; |
|
| 238 | - } |
|
| 239 | - $this->fileList = $files; |
|
| 240 | - return $files; |
|
| 241 | - } |
|
| 223 | + /** |
|
| 224 | + * get all files in the archive |
|
| 225 | + * |
|
| 226 | + * @return array |
|
| 227 | + */ |
|
| 228 | + public function getFiles() { |
|
| 229 | + if ($this->fileList) { |
|
| 230 | + return $this->fileList; |
|
| 231 | + } |
|
| 232 | + if (!$this->cachedHeaders) { |
|
| 233 | + $this->cachedHeaders = $this->tar->listContent(); |
|
| 234 | + } |
|
| 235 | + $files = array(); |
|
| 236 | + foreach ($this->cachedHeaders as $header) { |
|
| 237 | + $files[] = $header['filename']; |
|
| 238 | + } |
|
| 239 | + $this->fileList = $files; |
|
| 240 | + return $files; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * get the content of a file |
|
| 245 | - * |
|
| 246 | - * @param string $path |
|
| 247 | - * @return string |
|
| 248 | - */ |
|
| 249 | - public function getFile($path) { |
|
| 250 | - return $this->tar->extractInString($path); |
|
| 251 | - } |
|
| 243 | + /** |
|
| 244 | + * get the content of a file |
|
| 245 | + * |
|
| 246 | + * @param string $path |
|
| 247 | + * @return string |
|
| 248 | + */ |
|
| 249 | + public function getFile($path) { |
|
| 250 | + return $this->tar->extractInString($path); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * extract a single file from the archive |
|
| 255 | - * |
|
| 256 | - * @param string $path |
|
| 257 | - * @param string $dest |
|
| 258 | - * @return bool |
|
| 259 | - */ |
|
| 260 | - public function extractFile($path, $dest) { |
|
| 261 | - $tmp = \OCP\Files::tmpFolder(); |
|
| 262 | - if (!$this->fileExists($path)) { |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - if ($this->fileExists('/' . $path)) { |
|
| 266 | - $success = $this->tar->extractList(array('/' . $path), $tmp); |
|
| 267 | - } else { |
|
| 268 | - $success = $this->tar->extractList(array($path), $tmp); |
|
| 269 | - } |
|
| 270 | - if ($success) { |
|
| 271 | - rename($tmp . $path, $dest); |
|
| 272 | - } |
|
| 273 | - \OCP\Files::rmdirr($tmp); |
|
| 274 | - return $success; |
|
| 275 | - } |
|
| 253 | + /** |
|
| 254 | + * extract a single file from the archive |
|
| 255 | + * |
|
| 256 | + * @param string $path |
|
| 257 | + * @param string $dest |
|
| 258 | + * @return bool |
|
| 259 | + */ |
|
| 260 | + public function extractFile($path, $dest) { |
|
| 261 | + $tmp = \OCP\Files::tmpFolder(); |
|
| 262 | + if (!$this->fileExists($path)) { |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + if ($this->fileExists('/' . $path)) { |
|
| 266 | + $success = $this->tar->extractList(array('/' . $path), $tmp); |
|
| 267 | + } else { |
|
| 268 | + $success = $this->tar->extractList(array($path), $tmp); |
|
| 269 | + } |
|
| 270 | + if ($success) { |
|
| 271 | + rename($tmp . $path, $dest); |
|
| 272 | + } |
|
| 273 | + \OCP\Files::rmdirr($tmp); |
|
| 274 | + return $success; |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * extract the archive |
|
| 279 | - * |
|
| 280 | - * @param string $dest |
|
| 281 | - * @return bool |
|
| 282 | - */ |
|
| 283 | - public function extract($dest) { |
|
| 284 | - return $this->tar->extract($dest); |
|
| 285 | - } |
|
| 277 | + /** |
|
| 278 | + * extract the archive |
|
| 279 | + * |
|
| 280 | + * @param string $dest |
|
| 281 | + * @return bool |
|
| 282 | + */ |
|
| 283 | + public function extract($dest) { |
|
| 284 | + return $this->tar->extract($dest); |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - /** |
|
| 288 | - * check if a file or folder exists in the archive |
|
| 289 | - * |
|
| 290 | - * @param string $path |
|
| 291 | - * @return bool |
|
| 292 | - */ |
|
| 293 | - public function fileExists($path) { |
|
| 294 | - $files = $this->getFiles(); |
|
| 295 | - if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) { |
|
| 296 | - return true; |
|
| 297 | - } else { |
|
| 298 | - $folderPath = rtrim($path, '/') . '/'; |
|
| 299 | - $pathLength = strlen($folderPath); |
|
| 300 | - foreach ($files as $file) { |
|
| 301 | - if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) { |
|
| 302 | - return true; |
|
| 303 | - } |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - if ($path[0] != '/') { //not all programs agree on the use of a leading / |
|
| 307 | - return $this->fileExists('/' . $path); |
|
| 308 | - } else { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - } |
|
| 287 | + /** |
|
| 288 | + * check if a file or folder exists in the archive |
|
| 289 | + * |
|
| 290 | + * @param string $path |
|
| 291 | + * @return bool |
|
| 292 | + */ |
|
| 293 | + public function fileExists($path) { |
|
| 294 | + $files = $this->getFiles(); |
|
| 295 | + if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) { |
|
| 296 | + return true; |
|
| 297 | + } else { |
|
| 298 | + $folderPath = rtrim($path, '/') . '/'; |
|
| 299 | + $pathLength = strlen($folderPath); |
|
| 300 | + foreach ($files as $file) { |
|
| 301 | + if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) { |
|
| 302 | + return true; |
|
| 303 | + } |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + if ($path[0] != '/') { //not all programs agree on the use of a leading / |
|
| 307 | + return $this->fileExists('/' . $path); |
|
| 308 | + } else { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | 312 | |
| 313 | - /** |
|
| 314 | - * remove a file or folder from the archive |
|
| 315 | - * |
|
| 316 | - * @param string $path |
|
| 317 | - * @return bool |
|
| 318 | - */ |
|
| 319 | - public function remove($path) { |
|
| 320 | - if (!$this->fileExists($path)) { |
|
| 321 | - return false; |
|
| 322 | - } |
|
| 323 | - $this->fileList = false; |
|
| 324 | - $this->cachedHeaders = false; |
|
| 325 | - //no proper way to delete, extract entire archive, delete file and remake archive |
|
| 326 | - $tmp = \OCP\Files::tmpFolder(); |
|
| 327 | - $this->tar->extract($tmp); |
|
| 328 | - \OCP\Files::rmdirr($tmp . $path); |
|
| 329 | - $this->tar = null; |
|
| 330 | - unlink($this->path); |
|
| 331 | - $this->reopen(); |
|
| 332 | - $this->tar->createModify(array($tmp), '', $tmp); |
|
| 333 | - return true; |
|
| 334 | - } |
|
| 313 | + /** |
|
| 314 | + * remove a file or folder from the archive |
|
| 315 | + * |
|
| 316 | + * @param string $path |
|
| 317 | + * @return bool |
|
| 318 | + */ |
|
| 319 | + public function remove($path) { |
|
| 320 | + if (!$this->fileExists($path)) { |
|
| 321 | + return false; |
|
| 322 | + } |
|
| 323 | + $this->fileList = false; |
|
| 324 | + $this->cachedHeaders = false; |
|
| 325 | + //no proper way to delete, extract entire archive, delete file and remake archive |
|
| 326 | + $tmp = \OCP\Files::tmpFolder(); |
|
| 327 | + $this->tar->extract($tmp); |
|
| 328 | + \OCP\Files::rmdirr($tmp . $path); |
|
| 329 | + $this->tar = null; |
|
| 330 | + unlink($this->path); |
|
| 331 | + $this->reopen(); |
|
| 332 | + $this->tar->createModify(array($tmp), '', $tmp); |
|
| 333 | + return true; |
|
| 334 | + } |
|
| 335 | 335 | |
| 336 | - /** |
|
| 337 | - * get a file handler |
|
| 338 | - * |
|
| 339 | - * @param string $path |
|
| 340 | - * @param string $mode |
|
| 341 | - * @return resource |
|
| 342 | - */ |
|
| 343 | - public function getStream($path, $mode) { |
|
| 344 | - if (strrpos($path, '.') !== false) { |
|
| 345 | - $ext = substr($path, strrpos($path, '.')); |
|
| 346 | - } else { |
|
| 347 | - $ext = ''; |
|
| 348 | - } |
|
| 349 | - $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 350 | - if ($this->fileExists($path)) { |
|
| 351 | - $this->extractFile($path, $tmpFile); |
|
| 352 | - } elseif ($mode == 'r' or $mode == 'rb') { |
|
| 353 | - return false; |
|
| 354 | - } |
|
| 355 | - if ($mode == 'r' or $mode == 'rb') { |
|
| 356 | - return fopen($tmpFile, $mode); |
|
| 357 | - } else { |
|
| 358 | - $handle = fopen($tmpFile, $mode); |
|
| 359 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 360 | - $this->writeBack($tmpFile, $path); |
|
| 361 | - }); |
|
| 362 | - } |
|
| 363 | - } |
|
| 336 | + /** |
|
| 337 | + * get a file handler |
|
| 338 | + * |
|
| 339 | + * @param string $path |
|
| 340 | + * @param string $mode |
|
| 341 | + * @return resource |
|
| 342 | + */ |
|
| 343 | + public function getStream($path, $mode) { |
|
| 344 | + if (strrpos($path, '.') !== false) { |
|
| 345 | + $ext = substr($path, strrpos($path, '.')); |
|
| 346 | + } else { |
|
| 347 | + $ext = ''; |
|
| 348 | + } |
|
| 349 | + $tmpFile = \OCP\Files::tmpFile($ext); |
|
| 350 | + if ($this->fileExists($path)) { |
|
| 351 | + $this->extractFile($path, $tmpFile); |
|
| 352 | + } elseif ($mode == 'r' or $mode == 'rb') { |
|
| 353 | + return false; |
|
| 354 | + } |
|
| 355 | + if ($mode == 'r' or $mode == 'rb') { |
|
| 356 | + return fopen($tmpFile, $mode); |
|
| 357 | + } else { |
|
| 358 | + $handle = fopen($tmpFile, $mode); |
|
| 359 | + return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 360 | + $this->writeBack($tmpFile, $path); |
|
| 361 | + }); |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | - /** |
|
| 366 | - * write back temporary files |
|
| 367 | - */ |
|
| 368 | - public function writeBack($tmpFile, $path) { |
|
| 369 | - $this->addFile($path, $tmpFile); |
|
| 370 | - unlink($tmpFile); |
|
| 371 | - } |
|
| 365 | + /** |
|
| 366 | + * write back temporary files |
|
| 367 | + */ |
|
| 368 | + public function writeBack($tmpFile, $path) { |
|
| 369 | + $this->addFile($path, $tmpFile); |
|
| 370 | + unlink($tmpFile); |
|
| 371 | + } |
|
| 372 | 372 | |
| 373 | - /** |
|
| 374 | - * reopen the archive to ensure everything is written |
|
| 375 | - */ |
|
| 376 | - private function reopen() { |
|
| 377 | - if ($this->tar) { |
|
| 378 | - $this->tar->_close(); |
|
| 379 | - $this->tar = null; |
|
| 380 | - } |
|
| 381 | - $types = array(null, 'gz', 'bz'); |
|
| 382 | - $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]); |
|
| 383 | - } |
|
| 373 | + /** |
|
| 374 | + * reopen the archive to ensure everything is written |
|
| 375 | + */ |
|
| 376 | + private function reopen() { |
|
| 377 | + if ($this->tar) { |
|
| 378 | + $this->tar->_close(); |
|
| 379 | + $this->tar = null; |
|
| 380 | + } |
|
| 381 | + $types = array(null, 'gz', 'bz'); |
|
| 382 | + $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]); |
|
| 383 | + } |
|
| 384 | 384 | } |
@@ -91,20 +91,20 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function addFolder($path) { |
| 93 | 93 | $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder(); |
| 94 | - $path = rtrim($path, '/') . '/'; |
|
| 94 | + $path = rtrim($path, '/').'/'; |
|
| 95 | 95 | if ($this->fileExists($path)) { |
| 96 | 96 | return false; |
| 97 | 97 | } |
| 98 | 98 | $parts = explode('/', $path); |
| 99 | 99 | $folder = $tmpBase; |
| 100 | 100 | foreach ($parts as $part) { |
| 101 | - $folder .= '/' . $part; |
|
| 101 | + $folder .= '/'.$part; |
|
| 102 | 102 | if (!is_dir($folder)) { |
| 103 | 103 | mkdir($folder); |
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | - $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase); |
|
| 107 | - rmdir($tmpBase . $path); |
|
| 106 | + $result = $this->tar->addModify(array($tmpBase.$path), '', $tmpBase); |
|
| 107 | + rmdir($tmpBase.$path); |
|
| 108 | 108 | $this->fileList = false; |
| 109 | 109 | $this->cachedHeaders = false; |
| 110 | 110 | return $result; |
@@ -141,12 +141,12 @@ discard block |
||
| 141 | 141 | //no proper way to delete, rename entire archive, rename file and remake archive |
| 142 | 142 | $tmp = \OCP\Files::tmpFolder(); |
| 143 | 143 | $this->tar->extract($tmp); |
| 144 | - rename($tmp . $source, $tmp . $dest); |
|
| 144 | + rename($tmp.$source, $tmp.$dest); |
|
| 145 | 145 | $this->tar = null; |
| 146 | 146 | unlink($this->path); |
| 147 | 147 | $types = array(null, 'gz', 'bz'); |
| 148 | 148 | $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]); |
| 149 | - $this->tar->createModify(array($tmp), '', $tmp . '/'); |
|
| 149 | + $this->tar->createModify(array($tmp), '', $tmp.'/'); |
|
| 150 | 150 | $this->fileList = false; |
| 151 | 151 | $this->cachedHeaders = false; |
| 152 | 152 | return true; |
@@ -161,9 +161,9 @@ discard block |
||
| 161 | 161 | } |
| 162 | 162 | foreach ($this->cachedHeaders as $header) { |
| 163 | 163 | if ($file == $header['filename'] |
| 164 | - or $file . '/' == $header['filename'] |
|
| 165 | - or '/' . $file . '/' == $header['filename'] |
|
| 166 | - or '/' . $file == $header['filename'] |
|
| 164 | + or $file.'/' == $header['filename'] |
|
| 165 | + or '/'.$file.'/' == $header['filename'] |
|
| 166 | + or '/'.$file == $header['filename'] |
|
| 167 | 167 | ) { |
| 168 | 168 | return $header; |
| 169 | 169 | } |
@@ -262,13 +262,13 @@ discard block |
||
| 262 | 262 | if (!$this->fileExists($path)) { |
| 263 | 263 | return false; |
| 264 | 264 | } |
| 265 | - if ($this->fileExists('/' . $path)) { |
|
| 266 | - $success = $this->tar->extractList(array('/' . $path), $tmp); |
|
| 265 | + if ($this->fileExists('/'.$path)) { |
|
| 266 | + $success = $this->tar->extractList(array('/'.$path), $tmp); |
|
| 267 | 267 | } else { |
| 268 | 268 | $success = $this->tar->extractList(array($path), $tmp); |
| 269 | 269 | } |
| 270 | 270 | if ($success) { |
| 271 | - rename($tmp . $path, $dest); |
|
| 271 | + rename($tmp.$path, $dest); |
|
| 272 | 272 | } |
| 273 | 273 | \OCP\Files::rmdirr($tmp); |
| 274 | 274 | return $success; |
@@ -292,10 +292,10 @@ discard block |
||
| 292 | 292 | */ |
| 293 | 293 | public function fileExists($path) { |
| 294 | 294 | $files = $this->getFiles(); |
| 295 | - if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) { |
|
| 295 | + if ((array_search($path, $files) !== false) or (array_search($path.'/', $files) !== false)) { |
|
| 296 | 296 | return true; |
| 297 | 297 | } else { |
| 298 | - $folderPath = rtrim($path, '/') . '/'; |
|
| 298 | + $folderPath = rtrim($path, '/').'/'; |
|
| 299 | 299 | $pathLength = strlen($folderPath); |
| 300 | 300 | foreach ($files as $file) { |
| 301 | 301 | if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) { |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | } |
| 305 | 305 | } |
| 306 | 306 | if ($path[0] != '/') { //not all programs agree on the use of a leading / |
| 307 | - return $this->fileExists('/' . $path); |
|
| 307 | + return $this->fileExists('/'.$path); |
|
| 308 | 308 | } else { |
| 309 | 309 | return false; |
| 310 | 310 | } |
@@ -325,7 +325,7 @@ discard block |
||
| 325 | 325 | //no proper way to delete, extract entire archive, delete file and remake archive |
| 326 | 326 | $tmp = \OCP\Files::tmpFolder(); |
| 327 | 327 | $this->tar->extract($tmp); |
| 328 | - \OCP\Files::rmdirr($tmp . $path); |
|
| 328 | + \OCP\Files::rmdirr($tmp.$path); |
|
| 329 | 329 | $this->tar = null; |
| 330 | 330 | unlink($this->path); |
| 331 | 331 | $this->reopen(); |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | return fopen($tmpFile, $mode); |
| 357 | 357 | } else { |
| 358 | 358 | $handle = fopen($tmpFile, $mode); |
| 359 | - return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) { |
|
| 359 | + return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) { |
|
| 360 | 360 | $this->writeBack($tmpFile, $path); |
| 361 | 361 | }); |
| 362 | 362 | } |