1<?php 2/** 3 * Circles - Bring cloud-users closer together. 4 * 5 * This file is licensed under the Affero General Public License version 3 or 6 * later. See the COPYING file. 7 * 8 * @author Maxence Lange <maxence@artificial-owl.com> 9 * @copyright 2017 10 * @license GNU AGPL version 3 or any later version 11 * 12 * This program is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU Affero General Public License as 14 * published by the Free Software Foundation, either version 3 of the 15 * License, or (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU Affero General Public License for more details. 21 * 22 * You should have received a copy of the GNU Affero General Public License 23 * along with this program. If not, see <http://www.gnu.org/licenses/>. 24 * 25 */ 26 27namespace OCA\Circles\Model; 28 29use OC; 30use OCA\Circles\AppInfo\Application; 31use OCP\IL10N; 32 33class BaseCircle { 34 public const CIRCLES_SETTINGS_DEFAULT = [ 35 'password_enforcement' => 'false', 36 'password_single' => '', 37 'allow_links' => 'false', 38 'allow_links_auto' => 'false', 39 'allow_links_files' => 'false' 40 ]; 41 42 public const CIRCLES_PERSONAL = 1; 43 public const CIRCLES_SECRET = 2; 44 public const CIRCLES_CLOSED = 4; 45 public const CIRCLES_PUBLIC = 8; 46 47 public const CIRCLES_ALL = 15; 48 49 public const SHORT_UNIQUE_ID_LENGTH = 14; 50 51 /** @var int */ 52 private $id; 53 54 /** @var IL10N */ 55 protected $l10n; 56 57 /** @var string */ 58 private $uniqueId = ''; 59 60 /** @var string */ 61 private $name; 62 63 /** @var string */ 64 private $altName = ''; 65 66 /** @var DeprecatedMember */ 67 private $owner; 68 69 /** @var DeprecatedMember */ 70 private $viewer = null; 71 72 /** @var DeprecatedMember */ 73 private $viewerGroup; 74 75 /** @var string */ 76 private $description = ''; 77 78 /** @var array */ 79 private $settings = []; 80 81 /** @var string */ 82 private $passwordSingle = ''; 83 84 /** @var int */ 85 private $type; 86 87 /** @var string */ 88 private $contactGroupName = ''; 89 90 /** @var int */ 91 private $contactAddressBook = 0; 92 93 94 /** @var string */ 95 private $creation; 96 97 /** @var DeprecatedMember[] */ 98 private $members; 99 100 /** @var DeprecatedMember[] */ 101 private $groups; 102 103 /** @var FederatedLink[] */ 104 private $links; 105 106 public function __construct($type = -1, $name = '') { 107 $this->l10n = OC::$server->getL10N(Application::APP_ID); 108 109 if ($type > -1) { 110 $this->type = $type; 111 } 112 if ($name !== '') { 113 $this->name = $name; 114 } 115 } 116 117 /** 118 * @param integer $id 119 * 120 * @return BaseCircle 121 */ 122 public function setId($id) { 123 $this->id = $id; 124 125 return $this; 126 } 127 128 /** 129 * @return integer 130 */ 131 public function getId() { 132 return $this->id; 133 } 134 135 136 /** 137 * @param string $uniqueId 138 * 139 * @return $this 140 */ 141 public function setUniqueId($uniqueId) { 142 $this->uniqueId = (string)$uniqueId; 143 144 return $this; 145 } 146 147 /** 148 * @param bool $full 149 * 150 * @return string 151 */ 152 public function getUniqueId($full = false) { 153 if ($full) { 154 return $this->uniqueId; 155 } 156 157 return substr($this->uniqueId, 0, self::SHORT_UNIQUE_ID_LENGTH); 158 } 159 160 public function generateUniqueId() { 161 $uniqueId = bin2hex(openssl_random_pseudo_bytes(24)); 162 $this->setUniqueId($uniqueId); 163 $this->setId($this->getUniqueId()); 164 } 165 166 /** 167 * @param string $name 168 * 169 * @return BaseCircle 170 */ 171 public function setName($name) { 172 $this->name = $name; 173 174 return $this; 175 } 176 177 /** 178 * @param bool $real 179 * 180 * @return string 181 */ 182 public function getName(bool $real = false) { 183 if (!$real && $this->altName !== '') { 184 return $this->altName; 185 } 186 187 return $this->name; 188 } 189 190 /** 191 * @param string $name 192 * 193 * @return BaseCircle 194 */ 195 public function setAltName($name) { 196 $this->altName = $name; 197 198 return $this; 199 } 200 201 /** 202 * @return string 203 */ 204 public function getAltName() { 205 return $this->altName; 206 } 207 208 /** 209 * @return DeprecatedMember 210 */ 211 public function getOwner() { 212 return $this->owner; 213 } 214 215 /** 216 * @param DeprecatedMember $owner 217 * 218 * @return BaseCircle 219 */ 220 public function setOwner($owner) { 221 $this->owner = $owner; 222 223 return $this; 224 } 225 226 227 /** 228 * @return DeprecatedMember 229 */ 230 public function getViewer() { 231 return $this->viewer; 232 } 233 234 /** 235 * @param DeprecatedMember $user 236 * 237 * @return BaseCircle 238 */ 239 public function setViewer($user) { 240 $this->viewer = $user; 241 242 return $this; 243 } 244 245 246 public function hasViewer(): bool { 247 return ($this->viewer !== null); 248 } 249 250 251 /** 252 * @return DeprecatedMember 253 */ 254 public function getGroupViewer() { 255 return $this->viewerGroup; 256 } 257 258 /** 259 * @param DeprecatedMember $group 260 * 261 * @return BaseCircle 262 */ 263 public function setGroupViewer($group) { 264 $this->viewerGroup = $group; 265 266 return $this; 267 } 268 269 /** 270 * @return DeprecatedMember 271 */ 272 public function getHigherViewer() { 273 if ($this->getGroupViewer() === null) { 274 return $this->getViewer(); 275 } 276 277 if ($this->getViewer() === null) { 278 return $this->getGroupViewer(); 279 } 280 281 if ($this->getGroupViewer() 282 ->getLevel() > $this->getViewer() 283 ->getLevel() 284 ) { 285 return $this->getGroupViewer(); 286 } 287 288 return $this->getViewer(); 289 } 290 291 292 /** 293 * @param string $description 294 * 295 * @return BaseCircle 296 */ 297 public function setDescription($description) { 298 $this->description = $description; 299 300 return $this; 301 } 302 303 /** 304 * @return string 305 */ 306 public function getDescription() { 307 return $this->description; 308 } 309 310 311 /** 312 * @param int $contactAddressBook 313 * 314 * @return BaseCircle 315 */ 316 public function setContactAddressBook(int $contactAddressBook) { 317 $this->contactAddressBook = $contactAddressBook; 318 319 return $this; 320 } 321 322 /** 323 * @return int 324 */ 325 public function getContactAddressBook() { 326 return $this->contactAddressBook; 327 } 328 329 330 /** 331 * @param string $contactGroupName 332 * 333 * @return BaseCircle 334 */ 335 public function setContactGroupName($contactGroupName) { 336 $this->contactGroupName = $contactGroupName; 337 338 return $this; 339 } 340 341 /** 342 * @return string 343 */ 344 public function getContactGroupName() { 345 return $this->contactGroupName; 346 } 347 348 349 /** 350 * @param string|array $settings 351 * @param bool $all 352 * 353 * @return $this 354 */ 355 public function setSettings($settings, bool $all = false) { 356 if (is_array($settings)) { 357 $this->settings = $settings; 358 } elseif (is_string($settings)) { 359 $this->settings = (array)json_decode($settings, true); 360 } 361 362 if (array_key_exists('password_single', $this->settings)) { 363 $this->setPasswordSingle($this->settings['password_single']); 364 if (!$all) { 365 $this->settings['password_single'] = ''; 366 } 367 } 368 369 return $this; 370 } 371 372 /** 373 * @param bool $json 374 * 375 * @return array|string 376 */ 377 public function getSettings($json = false) { 378 if ($json) { 379 return json_encode($this->settings); 380 } 381 382 $settings = $this->settings; 383 if ($settings === null) { 384 $settings = []; 385 } 386 387 $ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT); 388 foreach ($ak as $k) { 389 if (!key_exists($k, $settings)) { 390 $settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k]; 391 } 392 } 393 394 return $settings; 395 } 396 397 398 /** 399 * @param string $k 400 * @param mixed $v 401 */ 402 public function setSetting($k, $v) { 403 switch ($k) { 404 case 'circle_name': 405 $this->setName($v); 406 break; 407 408 case 'circle_alt_name': 409 $this->setAltName($v); 410 break; 411 412 case 'circle_desc': 413 $this->setDescription($v); 414 break; 415 416 default: 417 $this->settings[$k] = $v; 418 break; 419 } 420 } 421 422 423 /** 424 * @param string $k 425 * 426 * @return string|null 427 */ 428 public function getSetting($k) { 429 if (key_exists($k, $this->settings)) { 430 return $this->settings[$k]; 431 } 432 if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) { 433 return self::CIRCLES_SETTINGS_DEFAULT[$k]; 434 } 435 436 return null; 437 } 438 439 440 /** 441 * @return string 442 */ 443 public function getPasswordSingle(): string { 444 return $this->passwordSingle; 445 } 446 447 /** 448 * @param string $passwordSingle 449 */ 450 public function setPasswordSingle(string $passwordSingle): void { 451 $this->passwordSingle = $passwordSingle; 452 } 453 454 455 /** 456 * 457 * @param string $type 458 * 459 * @return \OCA\Circles\Model\BaseCircle 460 */ 461 public function setType($type) { 462 $this->type = self::typeInt($type); 463 464 return $this; 465 } 466 467 /** 468 * @return string 469 */ 470 public function getType() { 471 return $this->type; 472 } 473 474 /** 475 * @param string $creation 476 * 477 * @return \OCA\Circles\Model\BaseCircle 478 */ 479 public function setCreation($creation) { 480 $this->creation = $creation; 481 482 return $this; 483 } 484 485 /** 486 * @return string 487 */ 488 public function getCreation() { 489 return $this->creation; 490 } 491 492 /** 493 * @param array $members 494 * 495 * @return BaseCircle 496 */ 497 public function setMembers($members) { 498 $this->members = $members; 499 500 return $this; 501 } 502 503 /** 504 * @return DeprecatedMember[] 505 */ 506 public function getMembers() { 507 return $this->members; 508 } 509 510 /** 511 * @param array $groups 512 * 513 * @return BaseCircle 514 */ 515 public function setGroups($groups) { 516 $this->groups = $groups; 517 518 return $this; 519 } 520 521 /** 522 * @return array 523 */ 524 public function getGroups() { 525 return $this->groups; 526 } 527 528 /** 529 * @param array $links 530 * 531 * @return BaseCircle 532 */ 533 public function setLinks($links) { 534 $this->links = $links; 535 536 return $this; 537 } 538 539 /** 540 * @return array 541 */ 542 public function getLinks() { 543 return $this->links; 544 } 545 546 547// public function getRemote() { 548// return $this->remote; 549// } 550// 551// public function addRemote($link) { 552// array_push($this->remote, $link); 553// } 554// 555// public function getRemoteFromToken($token) { 556// foreach ($this->links AS $link) { 557// if ($link->getToken() === $token) { 558// return $link; 559// } 560// } 561// 562// return null; 563// } 564// 565// public function getRemoteFromAddressAndId($address, $id) { 566// foreach ($this->links AS $link) { 567// if ($link->getAddress() === $address && $link->getUniqueId() === $id) { 568// return $link; 569// } 570// } 571// 572// return null; 573// } 574 575 /** 576 * @param integer|string $type 577 * 578 * @return integer 579 */ 580 public static function typeInt($type) { 581 if (is_numeric($type)) { 582 return (int)$type; 583 } 584 585 switch ($type) { 586 case 'Personal': 587 return self::CIRCLES_PERSONAL; 588 case 'Closed': 589 return self::CIRCLES_CLOSED; 590 case 'Secret': 591 return self::CIRCLES_SECRET; 592 case 'Public': 593 return self::CIRCLES_PUBLIC; 594 } 595 596 return 0; 597 } 598} 599