summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkondou <kondou@ts.unde.re>2013-07-29 11:34:38 +0200
committerkondou <kondou@ts.unde.re>2013-08-25 21:04:04 +0200
commitfac671b14ed06233d37ad38194ebf9a99118644a (patch)
treedb99cca8e2ee41b79b444b44f8a429bf8fe32b8a
parent4a08f7d710ced1c564e05471e1f873ecfb9ca161 (diff)
downloadnextcloud-server-fac671b14ed06233d37ad38194ebf9a99118644a.tar.gz
nextcloud-server-fac671b14ed06233d37ad38194ebf9a99118644a.zip
Modularize get(), async getAvatar, avatars @ usermgmt
And other small improvements
-rw-r--r--core/ajax/getavatar.php14
-rw-r--r--core/css/styles.css1
-rw-r--r--core/routes.php3
-rw-r--r--lib/avatar.php113
-rw-r--r--lib/installer.php1
-rw-r--r--lib/public/avatar.php4
-rw-r--r--lib/templatelayout.php2
-rw-r--r--settings/ajax/newavatar.php30
-rw-r--r--settings/js/personal.js24
-rw-r--r--settings/routes.php2
-rw-r--r--settings/templates/admin.php3
-rw-r--r--settings/templates/personal.php7
-rw-r--r--settings/templates/users.php6
-rw-r--r--settings/users.php1
14 files changed, 187 insertions, 24 deletions
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 @@
+<?php
+
+OC_JSON::checkLoggedIn();
+OC_JSON::callCheck();
+
+if(isset($_POST['user'])) {
+ if(isset($_POST['size'])) {
+ OC_JSON::success(array('data' => \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 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 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
- * @return link to 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 getDefaultAvatar ($size) {
- return OC_Helper::imagePath("core", "defaultavatar.png");
+ 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', '<img src="'.OC_Avatar::get(OC_User::getUser(), 32).'">');
}
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 @@
+<?php
+
+OC_JSON::checkLoggedIn();
+OC_JSON::callCheck();
+$user = OC_User::getUser();
+
+if(isset($_POST['path'])) {
+ $path = $_POST['path'];
+ if ($path === "false") { // delete avatar
+ \OC_Avatar::setLocalAvatar($user, false, false);
+ } else { // select an image from own files
+ $view = new \OC\Files\View('/'.$user.'/files');
+ $img = $view->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']) {
<label for="avatar_gravatar">Gravatar</label><br>
<em><?php print_unescaped($l->t('Use <a href="http://gravatar.com/">gravatar</a> for avatars')); ?></em><br>
<em><?php p($l->t('This sends data to gravatar')); ?></em>
+ <?php if (!$_['internetconnectionworking']): ?>
+ <br><em><?php p($l->t('Gravatar needs an internet connection!')); ?></em>
+ <?php endif; ?>
</td>
</tr>
<tr>
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']) {
<form id="avatar">
<fieldset class="personalblock">
<legend><strong><?php p($l->t('Avatar')); ?></strong></legend>
- <img src="<?php print_unescaped(\OC_Avatar::get(\OC_User::getUser())); ?>"><br>
- <button><?php p($l->t('Upload a new avatar')); ?></button>
+ <img src="<?php print_unescaped(\OC_Avatar::get(\OC_User::getUser(), 128)); ?>"><br>
+ <em><?php p($l->t('Your avatar has to be a square and either a PNG or JPG image')); ?></em><br>
+ <div class="inlineblock button" id="uploadavatar"><?php p($l->t('Upload a new avatar')); ?></div>
+ <div class="inlineblock button" id="selectavatar"><?php p($l->t('Select a new avatar from your files')); ?></div>
+ <div class="inlineblock button" id="removeavatar"><?php p($l->t('Remove my avatar')); ?></div>
</fieldset>
</form>
<?php endif; ?>
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);
<table class="hascontrols" data-groups="<?php p(json_encode($allGroups));?>">
<thead>
<tr>
+ <?php if(\OC_Avatar::getMode() !== "none"): ?>
+ <th id='headerAvatar'><?php p($l->t('Avatar')); ?></th>
+ <?php endif; ?>
<th id='headerName'><?php p($l->t('Username'))?></th>
<th id="headerDisplayName"><?php p($l->t( 'Display Name' )); ?></th>
<th id="headerPassword"><?php p($l->t( 'Password' )); ?></th>
@@ -96,6 +99,9 @@ $_['subadmingroups'] = array_flip($items);
<?php foreach($_["users"] as $user): ?>
<tr data-uid="<?php p($user["name"]) ?>"
data-displayName="<?php p($user["displayName"]) ?>">
+ <?php if(\OC_Avatar::getMode() !== "none"): ?>
+ <td class="avatar"><img src="<?php p($user["avatar"]); ?>"></td>
+ <?php endif; ?>
<td class="name"><?php p($user["name"]); ?></td>
<td class="displayName"><span><?php p($user["displayName"]); ?></span> <img class="svg action"
src="<?php p(image_path('core', 'actions/rename.svg'))?>"
diff --git a/settings/users.php b/settings/users.php
index 213d1eecfda..7dba45e1284 100644
--- a/settings/users.php
+++ b/settings/users.php
@@ -58,6 +58,7 @@ foreach($accessibleusers as $uid => $displayName) {
$users[] = array(
"name" => $uid,
+ "avatar" => \OC_Avatar::get($uid, 32),
"displayName" => $displayName,
"groups" => OC_Group::getUserGroups($uid),
'quota' => $quota,