avatar.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Lukas Reschke <lukas@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC;
  30. use OC\Files\Filesystem;
  31. use OC_Image;
  32. /**
  33. * This class gets and sets users avatars.
  34. */
  35. class Avatar implements \OCP\IAvatar {
  36. /** @var Files\View */
  37. private $view;
  38. /**
  39. * constructor
  40. * @param string $user user to do avatar-management with
  41. * @throws \Exception In case the username is potentially dangerous
  42. */
  43. public function __construct ($user) {
  44. if(!Filesystem::isValidPath($user)) {
  45. throw new \Exception('Username may not contain slashes');
  46. }
  47. $this->view = new \OC\Files\View('/'.$user);
  48. }
  49. /**
  50. * get the users avatar
  51. * @param int $size size in px of the avatar, avatars are square, defaults to 64
  52. * @return boolean|\OCP\IImage containing the avatar or false if there's no image
  53. */
  54. public function get ($size = 64) {
  55. if ($this->view->file_exists('avatar.jpg')) {
  56. $ext = 'jpg';
  57. } elseif ($this->view->file_exists('avatar.png')) {
  58. $ext = 'png';
  59. } else {
  60. return false;
  61. }
  62. $avatar = new OC_Image();
  63. $avatar->loadFromData($this->view->file_get_contents('avatar.'.$ext));
  64. $avatar->resize($size);
  65. return $avatar;
  66. }
  67. /**
  68. * Check if an avatar exists for the user
  69. *
  70. * @return bool
  71. */
  72. public function exists() {
  73. return $this->view->file_exists('avatar.jpg') || $this->view->file_exists('avatar.png');
  74. }
  75. /**
  76. * sets the users avatar
  77. * @param \OCP\IImage|resource|string $data An image object, imagedata or path to set a new avatar
  78. * @throws \Exception if the provided file is not a jpg or png image
  79. * @throws \Exception if the provided image is not valid
  80. * @throws \OC\NotSquareException if the image is not square
  81. * @return void
  82. */
  83. public function set ($data) {
  84. if($data instanceOf \OCP\IImage) {
  85. $img = $data;
  86. $data = $img->data();
  87. } else {
  88. $img = new OC_Image($data);
  89. }
  90. $type = substr($img->mimeType(), -3);
  91. if ($type === 'peg') {
  92. $type = 'jpg';
  93. }
  94. if ($type !== 'jpg' && $type !== 'png') {
  95. $l = \OC::$server->getL10N('lib');
  96. throw new \Exception($l->t("Unknown filetype"));
  97. }
  98. if (!$img->valid()) {
  99. $l = \OC::$server->getL10N('lib');
  100. throw new \Exception($l->t("Invalid image"));
  101. }
  102. if (!($img->height() === $img->width())) {
  103. throw new \OC\NotSquareException();
  104. }
  105. $this->view->unlink('avatar.jpg');
  106. $this->view->unlink('avatar.png');
  107. $this->view->file_put_contents('avatar.'.$type, $data);
  108. }
  109. /**
  110. * remove the users avatar
  111. * @return void
  112. */
  113. public function remove () {
  114. $this->view->unlink('avatar.jpg');
  115. $this->view->unlink('avatar.png');
  116. }
  117. }