Modularize get(), async getAvatar, avatars @ usermgmt

And other small improvements
This commit is contained in:
kondou 2013-07-29 11:34:38 +02:00
parent 4a08f7d710
commit fac671b14e
14 changed files with 188 additions and 25 deletions

14
core/ajax/getavatar.php Normal file
View File

@ -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();
}

View File

@ -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; }

View File

@ -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');

View File

@ -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;
}
}

View File

@ -426,6 +426,7 @@ class OC_Installer{
'OC_API::',
'OC_App::',
'OC_AppConfig::',
'OC_Avatar::',
'OC_BackgroundJob::',
'OC_Config::',
'OC_DB::',

View File

@ -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();
}
}

View File

@ -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).'">');
}

View File

@ -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();
}

View File

@ -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 = {

View File

@ -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');

View File

@ -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>

View File

@ -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; ?>

View File

@ -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'))?>"

View File

@ -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,