From fac671b14ed06233d37ad38194ebf9a99118644a Mon Sep 17 00:00:00 2001 From: kondou Date: Mon, 29 Jul 2013 11:34:38 +0200 Subject: [PATCH] Modularize get(), async getAvatar, avatars @ usermgmt And other small improvements --- core/ajax/getavatar.php | 14 ++++ core/css/styles.css | 1 + core/routes.php | 3 + lib/avatar.php | 115 ++++++++++++++++++++++++++------ lib/installer.php | 1 + lib/public/avatar.php | 4 ++ lib/templatelayout.php | 2 +- settings/ajax/newavatar.php | 30 +++++++++ settings/js/personal.js | 24 +++++++ settings/routes.php | 2 + settings/templates/admin.php | 3 + settings/templates/personal.php | 7 +- settings/templates/users.php | 6 ++ settings/users.php | 1 + 14 files changed, 188 insertions(+), 25 deletions(-) create mode 100644 core/ajax/getavatar.php create mode 100644 settings/ajax/newavatar.php diff --git a/core/ajax/getavatar.php b/core/ajax/getavatar.php new file mode 100644 index 00000000000..66bab0230a6 --- /dev/null +++ b/core/ajax/getavatar.php @@ -0,0 +1,14 @@ + \OC_Avatar::get($_POST['user'], $_POST['size']))); + } else { + OC_JSON::success(array('data' => \OC_Avatar::get($_POST['user']))); + } +} else { + OC_JSON::error(); +} diff --git a/core/css/styles.css b/core/css/styles.css index 1e7098d16a2..367f3f7ca41 100644 --- a/core/css/styles.css +++ b/core/css/styles.css @@ -592,6 +592,7 @@ label.infield { cursor:text !important; top:1.05em; left:.85em; } .hidden { display:none; } .bold { font-weight:bold; } .center { text-align:center; } +.inlineblock { display: inline-block; } #notification-container { position: fixed; top: 0px; width: 100%; text-align: center; z-index: 101; line-height: 1.2;} #notification, #update-notification { z-index:101; background-color:#fc4; border:0; padding:0 .7em .3em; display:none; position: relative; top:0; -moz-border-radius-bottomleft:1em; -webkit-border-bottom-left-radius:1em; border-bottom-left-radius:1em; -moz-border-radius-bottomright:1em; -webkit-border-bottom-right-radius:1em; border-bottom-right-radius:1em; } diff --git a/core/routes.php b/core/routes.php index dd8222d4378..309ed7484d9 100644 --- a/core/routes.php +++ b/core/routes.php @@ -36,6 +36,9 @@ $this->create('core_ajax_vcategories_favorites', '/core/ajax/vcategories/favorit ->actionInclude('core/ajax/vcategories/favorites.php'); $this->create('core_ajax_vcategories_edit', '/core/ajax/vcategories/edit.php') ->actionInclude('core/ajax/vcategories/edit.php'); +// Avatars +$this->create('core_ajax_getavatar', '/core/ajax/getavatar.php') + ->actionInclude('core/ajax/getavatar.php'); // oC JS config $this->create('js_config', '/core/js/config.js') ->actionInclude('core/js/config.php'); diff --git a/lib/avatar.php b/lib/avatar.php index 2b087c48b65..b232e9be762 100644 --- a/lib/avatar.php +++ b/lib/avatar.php @@ -6,9 +6,15 @@ * See the COPYING-README file. */ +/** + * This class gets and sets users avatars. + * Avalaible backends are local (saved in users root at avatar.[png|jpg]) and gravatar. + * However the get function is easy to extend with further backends. +*/ + class OC_Avatar { /** - * @brief gets the users avatar + * @brief gets a link to the users avatar * @param $user string username * @param $size integer size in px of the avatar, defaults to 64 * @return mixed link to the avatar, false if avatars are disabled @@ -19,41 +25,106 @@ class OC_Avatar { // avatars are disabled return false; } elseif ($mode === "gravatar") { - $email = OC_Preferences::getValue($user, 'settings', 'email'); - if ($email !== null) { - $emailhash = md5(strtolower(trim($email))); - $url = "http://www.gravatar.com/avatar/".$emailhash."?s=".$size; - return $url; - } else { - return \OC_Avatar::getDefaultAvatar($size); - } + return \OC_Avatar::getGravatar($user, $size); } elseif ($mode === "local") { - if (false) { - // - } else { - return \OC_Avatar::getDefaultAvatar($size); - } + return \OC_Avatar::getLocalAvatar($user, $size); } } + /** + * @brief returns the active avatar mode + * @return string active avatar mode + */ + public static function getMode () { + return OC_Config::getValue("avatar", "local"); + } /** * @brief sets the users local avatar * @param $user string user to set the avatar for - * @param $path string path where the avatar is + * @param $img mixed imagedata to set a new avatar, or false to delete the current avatar + * @param $type string fileextension + * @throws Exception if the provided image is not valid, or not a square * @return true on success */ - public static function setLocalAvatar ($user, $path) { - if (OC_Config::getValue("avatar", "local") === "local") { - // + public static function setLocalAvatar ($user, $img, $type) { + $view = new \OC\Files\View('/'.$user); + + if ($img === false) { + $view->unlink('avatar.jpg'); + $view->unlink('avatar.png'); + return true; + } else { + $img = new OC_Image($img); + + if (!( $img->valid() && ($img->height() === $img->width()) )) { + throw new Exception(); + } + + $view->unlink('avatar.jpg'); + $view->unlink('avatar.png'); + $view->file_put_contents('avatar.'.$type, $img); + return true; } } /** - * @brief gets the default avatar - * @return link to the default avatar + * @brief get the users gravatar + * @param $user string which user to get the gravatar for + * @param size integer size in px of the avatar, defaults to 64 + * @return string link to the gravatar, or base64encoded, html-ready image */ - public static function getDefaultAvatar ($size) { - return OC_Helper::imagePath("core", "defaultavatar.png"); + public static function getGravatar ($user, $size = 64) { + $email = OC_Preferences::getValue($user, 'settings', 'email'); + if ($email !== null) { + $emailhash = md5(strtolower(trim($email))); + $url = "http://www.gravatar.com/avatar/".$emailhash."?s=".$size; + return $url; + } else { + return \OC_Avatar::wrapIntoImg(\OC_Avatar::getDefaultAvatar($size), 'png'); + } + } + + /** + * @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 + * @return string base64encoded encoded, html-ready image + */ + public static function getLocalAvatar ($user, $size = 64) { + $view = new \OC\Files\View('/'.$user); + + if ($view->file_exists('avatar.jpg')) { + $type = 'jpg'; + } elseif ($view->file_exists('avatar.png')) { + $type = 'png'; + } else { + return \OC_Avatar::wrapIntoImg(\OC_Avatar::getDefaultAvatar($size), 'png'); + } + + $avatar = new OC_Image($view->file_get_contents('avatar.'.$type)); + $avatar->resize($size); + return \OC_Avatar::wrapIntoImg((string)$avatar, $type); + } + + /** + * @brief gets the default avatar + * @param $size integer size of the avatar in px, defaults to 64 + * @return string base64 encoded default avatar + */ + public static function getDefaultAvatar ($size = 64) { + $default = new OC_Image(OC::$SERVERROOT."/core/img/defaultavatar.png"); + $default->resize($size); + return (string)$default; + } + + /** + * @brief wrap a base64encoded image, so it can be used in html + * @param $img string base64encoded image + * @param $type string imagetype + * @return string wrapped image + */ + public static function wrapIntoImg($img, $type) { + return 'data:image/'.$type.';base64,'.$img; } } diff --git a/lib/installer.php b/lib/installer.php index b9684eaeea0..179b279c5b4 100644 --- a/lib/installer.php +++ b/lib/installer.php @@ -426,6 +426,7 @@ class OC_Installer{ 'OC_API::', 'OC_App::', 'OC_AppConfig::', + 'OC_Avatar::', 'OC_BackgroundJob::', 'OC_Config::', 'OC_DB::', diff --git a/lib/public/avatar.php b/lib/public/avatar.php index 65356b8a710..5d432f07cce 100644 --- a/lib/public/avatar.php +++ b/lib/public/avatar.php @@ -12,4 +12,8 @@ class Avatar { public static function get ($user, $size = 64) { \OC_Avatar::get($user, $size); } + + public static function getMode () { + \OC_Avatar::getMode(); + } } diff --git a/lib/templatelayout.php b/lib/templatelayout.php index 06cbacb692a..f24cd9cfd9e 100644 --- a/lib/templatelayout.php +++ b/lib/templatelayout.php @@ -19,7 +19,7 @@ class OC_TemplateLayout extends OC_Template { } // display avatars if they are enabled - if (OC_Config::getValue('avatar') === 'gravatar' || OC_Config::getValue('avatar') === 'local') { + if (OC_Config::getValue('avatar') === 'gravatar' || OC_Config::getValue('avatar', 'local') === 'local') { $this->assign('avatar', ''); } diff --git a/settings/ajax/newavatar.php b/settings/ajax/newavatar.php new file mode 100644 index 00000000000..b52317c9678 --- /dev/null +++ b/settings/ajax/newavatar.php @@ -0,0 +1,30 @@ +file_get_contents($path); + + $type = substr($path, -3); + if ($type === 'peg') { $type = 'jpg'; } + + if ($type === 'jpg' or $type === 'png') { + \OC_Avatar::setLocalAvatar($user, $img, $type); + OC_JSON::success(); + } else { + OC_JSON::error(); + } + } +} elseif (isset($_POST['image'])) { // upload a new image + \OC_Avatar::setLocalAvatar($user, $_POST['image']); + OC_JSON::success(); +} else { + OC_JSON::error(); +} diff --git a/settings/js/personal.js b/settings/js/personal.js index 8ad26c086b5..fdaca07e98d 100644 --- a/settings/js/personal.js +++ b/settings/js/personal.js @@ -44,6 +44,17 @@ function changeDisplayName(){ } } +function selectAvatar (path) { + $.post(OC.filePath('settings', 'ajax', 'newavatar.php'), {path: path}); + updateAvatar(); +} + +function updateAvatar () { + $.post(OC.filePath('core', 'ajax', 'getavatar.php'), {user: OC.currentUser, size: 128}, function(data){ + $('#avatar img').attr('src', data.data); + }); +} + $(document).ready(function(){ $("#passwordbutton").click( function(){ if ($('#pass1').val() !== '' && $('#pass2').val() !== '') { @@ -128,6 +139,19 @@ $(document).ready(function(){ } }); + $('#uploadavatar').click(function(){ + alert('To be done'); + updateAvatar(); + }); + + $('#selectavatar').click(function(){ + OC.dialogs.filepicker(t('settings', "Select an avatar"), selectAvatar, false, "image"); + }); + + $('#removeavatar').click(function(){ + $.post(OC.filePath('settings', 'ajax', 'newavatar.php'), {path: false}); + updateAvatar(); + }); } ); OC.Encryption = { diff --git a/settings/routes.php b/settings/routes.php index 9a27c3e439b..7d323008419 100644 --- a/settings/routes.php +++ b/settings/routes.php @@ -72,3 +72,5 @@ $this->create('isadmin', '/settings/js/isadmin.js') ->actionInclude('settings/js/isadmin.php'); $this->create('settings_ajax_setavatarmode', '/settings/ajax/setavatarmode.php') ->actionInclude('settings/ajax/setavatarmode.php'); +$this->create('settings_ajax_newavatar', '/settings/ajax/newavatar.php') + ->actionInclude('settings/ajax/newavatar.php'); diff --git a/settings/templates/admin.php b/settings/templates/admin.php index a166aec7775..e5b941f2b2f 100644 --- a/settings/templates/admin.php +++ b/settings/templates/admin.php @@ -128,6 +128,9 @@ if (!$_['internetconnectionworking']) {
t('Use gravatar for avatars')); ?>
t('This sends data to gravatar')); ?> + +
t('Gravatar needs an internet connection!')); ?> + diff --git a/settings/templates/personal.php b/settings/templates/personal.php index 55f626aa574..01415a6f9a1 100644 --- a/settings/templates/personal.php +++ b/settings/templates/personal.php @@ -87,8 +87,11 @@ if($_['passwordChangeSupported']) {
t('Avatar')); ?> -
- +
+ t('Your avatar has to be a square and either a PNG or JPG image')); ?>
+
t('Upload a new avatar')); ?>
+
t('Select a new avatar from your files')); ?>
+
t('Remove my avatar')); ?>
diff --git a/settings/templates/users.php b/settings/templates/users.php index 22450fdf25f..81d9a46d89d 100644 --- a/settings/templates/users.php +++ b/settings/templates/users.php @@ -81,6 +81,9 @@ $_['subadmingroups'] = array_flip($items); + + + @@ -96,6 +99,9 @@ $_['subadmingroups'] = array_flip($items); " data-displayName=""> + + +
t('Avatar')); ?> t('Username'))?> t( 'Display Name' )); ?> t( 'Password' )); ?>
"> $displayName) { $users[] = array( "name" => $uid, + "avatar" => \OC_Avatar::get($uid, 32), "displayName" => $displayName, "groups" => OC_Group::getUserGroups($uid), 'quota' => $quota,