2013-07-26 10:20:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2013-07-29 09:34:38 +00:00
|
|
|
/**
|
|
|
|
* This class gets and sets users avatars.
|
2013-08-18 14:41:00 +00:00
|
|
|
* Available backends are local (saved in users root at avatar.[png|jpg]), gravatar TODO and custom backends.
|
2013-07-29 09:34:38 +00:00
|
|
|
* However the get function is easy to extend with further backends.
|
|
|
|
*/
|
|
|
|
|
2013-07-26 10:20:11 +00:00
|
|
|
class OC_Avatar {
|
|
|
|
/**
|
2013-08-08 15:03:19 +00:00
|
|
|
* @brief gets the users avatar
|
|
|
|
* @param $user string username, if not provided, the default avatar will be returned
|
2013-07-26 10:20:11 +00:00
|
|
|
* @param $size integer size in px of the avatar, defaults to 64
|
2013-08-08 15:03:19 +00:00
|
|
|
* @return mixed \OC_Image containing the avatar, a link to the avatar, false if avatars are disabled
|
2013-07-26 10:20:11 +00:00
|
|
|
*/
|
2013-08-08 15:03:19 +00:00
|
|
|
public static function get ($user = false, $size = 64) {
|
|
|
|
$mode = self::getMode();
|
2013-07-26 10:20:11 +00:00
|
|
|
if ($mode === "none") {
|
|
|
|
// avatars are disabled
|
|
|
|
return false;
|
2013-08-08 15:03:19 +00:00
|
|
|
} else {
|
|
|
|
if ($user === false) {
|
|
|
|
return self::getDefaultAvatar($size);
|
|
|
|
} elseif ($mode === "gravatar") {
|
|
|
|
return self::getGravatar($user, $size);
|
|
|
|
} elseif ($mode === "local") {
|
|
|
|
return self::getLocalAvatar($user, $size);
|
|
|
|
} elseif ($mode === "custom") {
|
|
|
|
return self::getCustomAvatar($user, $size);
|
|
|
|
}
|
2013-07-26 10:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-29 09:34:38 +00:00
|
|
|
/**
|
|
|
|
* @brief returns the active avatar mode
|
|
|
|
* @return string active avatar mode
|
|
|
|
*/
|
|
|
|
public static function getMode () {
|
2013-08-08 15:03:19 +00:00
|
|
|
return \OC_Config::getValue("avatar", "local");
|
2013-07-29 09:34:38 +00:00
|
|
|
}
|
2013-07-26 10:20:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief sets the users local avatar
|
|
|
|
* @param $user string user to set the avatar for
|
2013-08-01 15:13:11 +00:00
|
|
|
* @param $data mixed imagedata or path to set a new avatar, or false to delete the current avatar
|
2013-07-30 14:09:54 +00:00
|
|
|
* @throws Exception if the provided file is not a jpg or png image
|
2013-07-29 09:34:38 +00:00
|
|
|
* @throws Exception if the provided image is not valid, or not a square
|
2013-07-26 10:20:11 +00:00
|
|
|
* @return true on success
|
|
|
|
*/
|
2013-08-01 15:13:11 +00:00
|
|
|
public static function setLocalAvatar ($user, $data) {
|
2013-07-29 09:34:38 +00:00
|
|
|
$view = new \OC\Files\View('/'.$user);
|
|
|
|
|
2013-08-01 15:13:11 +00:00
|
|
|
if ($data === false) {
|
2013-07-29 09:34:38 +00:00
|
|
|
$view->unlink('avatar.jpg');
|
|
|
|
$view->unlink('avatar.png');
|
|
|
|
return true;
|
|
|
|
} else {
|
2013-08-01 15:13:11 +00:00
|
|
|
$img = new OC_Image($data);
|
2013-07-30 14:09:54 +00:00
|
|
|
$type = substr($img->mimeType(), -3);
|
|
|
|
if ($type === 'peg') { $type = 'jpg'; }
|
|
|
|
if ($type !== 'jpg' && $type !== 'png') {
|
2013-08-12 12:58:35 +00:00
|
|
|
$l = \OC_L10N::get('lib');
|
|
|
|
throw new \Exception($l->t("Unknown filetype for avatar"));
|
2013-07-30 14:09:54 +00:00
|
|
|
}
|
2013-07-29 09:34:38 +00:00
|
|
|
|
|
|
|
if (!( $img->valid() && ($img->height() === $img->width()) )) {
|
2013-08-12 12:58:35 +00:00
|
|
|
$l = \OC_L10N::get('lib');
|
|
|
|
throw new \Exception($l->t("Invalid image, or the provided image is not square"));
|
2013-07-29 09:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$view->unlink('avatar.jpg');
|
|
|
|
$view->unlink('avatar.png');
|
2013-08-01 15:13:11 +00:00
|
|
|
$view->file_put_contents('avatar.'.$type, $data);
|
2013-07-29 09:34:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get the users gravatar
|
|
|
|
* @param $user string which user to get the gravatar for
|
2013-08-18 14:41:00 +00:00
|
|
|
* @param $size integer size in px of the avatar, defaults to 64
|
2013-08-08 15:03:19 +00:00
|
|
|
* @return string link to the gravatar, or \OC_Image with the default avatar
|
2013-07-29 09:34:38 +00:00
|
|
|
*/
|
|
|
|
public static function getGravatar ($user, $size = 64) {
|
2013-08-08 15:03:19 +00:00
|
|
|
$email = \OC_Preferences::getValue($user, 'settings', 'email');
|
2013-07-29 09:34:38 +00:00
|
|
|
if ($email !== null) {
|
|
|
|
$emailhash = md5(strtolower(trim($email)));
|
2013-08-17 09:01:47 +00:00
|
|
|
$url = "http://secure.gravatar.com/avatar/".$emailhash."?d=404&s=".$size;
|
|
|
|
$headers = get_headers($url, 1);
|
|
|
|
if (strpos($headers[0], "404 Not Found") === false) {
|
|
|
|
return $url;
|
|
|
|
}
|
2013-07-29 09:34:38 +00:00
|
|
|
}
|
2013-08-18 14:41:00 +00:00
|
|
|
return self::getDefaultAvatar($user, $size);
|
2013-07-29 09:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get the local avatar
|
|
|
|
* @param $user string which user to get the avatar for
|
|
|
|
* @param $size integer size in px of the avatar, defaults to 64
|
2013-08-08 15:03:19 +00:00
|
|
|
* @return string \OC_Image containing the avatar
|
2013-07-29 09:34:38 +00:00
|
|
|
*/
|
|
|
|
public static function getLocalAvatar ($user, $size = 64) {
|
|
|
|
$view = new \OC\Files\View('/'.$user);
|
|
|
|
|
|
|
|
if ($view->file_exists('avatar.jpg')) {
|
2013-08-08 15:03:19 +00:00
|
|
|
$ext = 'jpg';
|
2013-07-29 09:34:38 +00:00
|
|
|
} elseif ($view->file_exists('avatar.png')) {
|
2013-08-08 15:03:19 +00:00
|
|
|
$ext = 'png';
|
2013-07-29 09:34:38 +00:00
|
|
|
} else {
|
2013-08-18 14:41:00 +00:00
|
|
|
return self::getDefaultAvatar($user, $size);
|
2013-07-26 10:20:11 +00:00
|
|
|
}
|
2013-07-29 09:34:38 +00:00
|
|
|
|
2013-08-08 15:03:19 +00:00
|
|
|
$avatar = new OC_Image($view->file_get_contents('avatar.'.$ext));
|
2013-07-29 09:34:38 +00:00
|
|
|
$avatar->resize($size);
|
2013-08-08 15:03:19 +00:00
|
|
|
return $avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-17 09:01:47 +00:00
|
|
|
* @todo todo
|
2013-08-08 15:03:19 +00:00
|
|
|
*/
|
|
|
|
public static function getCustomAvatar($user, $size) {
|
|
|
|
// TODO
|
2013-07-26 10:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief gets the default avatar
|
2013-08-18 14:41:00 +00:00
|
|
|
* @brief $user string which user to get the avatar for
|
2013-07-29 09:34:38 +00:00
|
|
|
* @param $size integer size of the avatar in px, defaults to 64
|
2013-08-08 15:03:19 +00:00
|
|
|
* @return \OC_Image containing the default avatar
|
2013-08-17 09:01:47 +00:00
|
|
|
* @todo use custom default images, when they arive
|
2013-07-29 09:34:38 +00:00
|
|
|
*/
|
2013-08-18 14:41:00 +00:00
|
|
|
public static function getDefaultAvatar ($user, $size = 64) {
|
2013-07-29 09:34:38 +00:00
|
|
|
$default = new OC_Image(OC::$SERVERROOT."/core/img/defaultavatar.png");
|
|
|
|
$default->resize($size);
|
2013-08-08 15:03:19 +00:00
|
|
|
return $default;
|
2013-07-26 10:20:11 +00:00
|
|
|
}
|
|
|
|
}
|