gnu-social/plugins/Gravatar/GravatarPlugin.php

233 lines
9.3 KiB
PHP
Raw Normal View History

2009-12-10 03:32:57 +00:00
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @package GravatarPlugin
* @maintainer Eric Helgeson <erichelgeson@gmail.com>
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
class GravatarPlugin extends Plugin
{
function onInitializePlugin()
{
2009-12-10 03:32:57 +00:00
return true;
}
2010-09-18 16:14:00 +01:00
function onStartAvatarFormData($action)
{
2009-12-10 03:32:57 +00:00
$user = common_current_user();
$hasGravatar = $this->hasGravatar($user->id);
2010-09-18 16:14:00 +01:00
2009-12-10 03:32:57 +00:00
if($hasGravatar) {
return false;
}
}
2010-09-18 16:14:00 +01:00
function onEndAvatarFormData($action)
{
2009-12-10 03:32:57 +00:00
$user = common_current_user();
$hasGravatar = $this->hasGravatar($user->id);
if(!empty($user->email) && !$hasGravatar) { //and not gravatar already set
$action->elementStart('form', array('method' => 'post',
'id' => 'form_settings_gravatar_add',
'class' => 'form_settings',
'action' =>
common_local_url('avatarsettings')));
$action->elementStart('fieldset', array('id' => 'settings_gravatar_add'));
// TRANS: Fieldset legend. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
$action->element('legend', null, _m('Set Gravatar'));
$action->hidden('token', common_session_token());
$action->element('p', 'form_guide',
// TRANS: Form guide. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
_m('If you want to use your Gravatar image, click "Add".'));
$action->element('input', array('type' => 'submit',
'id' => 'settings_gravatar_add_action-submit',
'name' => 'add',
'class' => 'submit',
// TRANS: Button text to add a Gravatar. Gravatar is an avatar service.
'value' => _m('BUTTON','Add')));
2009-12-10 03:32:57 +00:00
$action->elementEnd('fieldset');
$action->elementEnd('form');
} elseif($hasGravatar) {
$action->elementStart('form', array('method' => 'post',
'id' => 'form_settings_gravatar_remove',
'class' => 'form_settings',
'action' =>
common_local_url('avatarsettings')));
$action->elementStart('fieldset', array('id' => 'settings_gravatar_remove'));
// TRANS: Fieldset legend. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
$action->element('legend', null, _m('Remove Gravatar'));
$action->hidden('token', common_session_token());
$action->element('p', 'form_guide',
// TRANS: Form guide. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
_m('If you want to remove your Gravatar image, click "Remove".'));
$action->element('input', array('type' => 'submit',
'id' => 'settings_gravatar_remove_action-submit',
'name' => 'remove',
'class' => 'submit',
// TRANS: Button text to remove a Gravatar. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
'value' => _m('Remove')));
$action->elementEnd('fieldset');
$action->elementEnd('form');
} else {
$action->element('p', 'form_guide',
// TRANS: Form guide. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
_m('To use a Gravatar first enter in an email address.'));
}
}
2010-09-18 16:14:00 +01:00
function onStartAvatarSaveForm($action)
{
2009-12-10 03:32:57 +00:00
if ($action->arg('add')) {
$result = $this->gravatar_save();
if($result['success']===true) {
common_broadcast_profile(common_current_user()->getProfile());
}
2009-12-10 03:32:57 +00:00
$action->showForm($result['message'], $result['success']);
2009-12-10 03:32:57 +00:00
return false;
} else if ($action->arg('remove')) {
$result = $this->gravatar_remove();
if($result['success']===true) {
common_broadcast_profile(common_current_user()->getProfile());
}
2009-12-10 03:32:57 +00:00
$action->showForm($result['message'], $result['success']);
2009-12-10 03:32:57 +00:00
return false;
} else {
return true;
}
}
function hasGravatar($id) {
$avatar = new Avatar();
$avatar->profile_id = $id;
if ($avatar->find()) {
while ($avatar->fetch()) {
if($avatar->filename == null) {
return true;
}
}
}
return false;
}
2010-09-18 16:14:00 +01:00
2009-12-10 03:32:57 +00:00
function gravatar_save()
{
$cur = common_current_user();
2010-09-18 16:14:00 +01:00
2009-12-10 03:32:57 +00:00
if(empty($cur->email)) {
// TRANS: Message displayed when no e-mail address was set when saving Gravatar setting. Gravatar is an avatar service.
2010-09-18 23:13:06 +01:00
return array('message' => _m('You do not have an email address set in your profile.'),
2009-12-10 03:32:57 +00:00
'success' => false);
}
//Get rid of previous Avatar
$this->gravatar_remove();
2010-09-18 16:14:00 +01:00
2009-12-10 03:32:57 +00:00
foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
$gravatar = new Avatar();
$gravatar->profile_id = $cur->id;
$gravatar->width = $size;
$gravatar->height = $size;
$gravatar->original = false; //No file, so no original
$gravatar->mediatype = 'img';//XXX: Unsure what to put here
//$gravatar->filename = null;//No filename. Remote
$gravatar->url = $this->gravatar_url($cur->email, $size);
$gravatar->created = DB_DataObject_Cast::dateTime(); # current time
if (!$gravatar->insert()) {
// TRANS: Message displayed when saving Gravatar setting fails. Gravatar is an avatar service.
2010-09-18 20:27:29 +01:00
return array('message' => _m('Failed to save Gravatar to the database.'),
2009-12-10 03:32:57 +00:00
'success' => false);
}
}
// TRANS: Message displayed when Gravatar was added. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
return array('message' => _m('Gravatar added.'),
'success' => true);
}
function gravatar_remove()
{
$user = common_current_user();
$profile = $user->getProfile();
$avatar = $profile->getOriginalAvatar();
if($avatar) $avatar->delete();
$avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
if($avatar) $avatar->delete();
$avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
if($avatar) $avatar->delete();
$avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
if($avatar) $avatar->delete();
// TRANS: Message displayed when Gravatar was removed. Gravatar is an avatar service.
2009-12-10 03:32:57 +00:00
return array('message' => _m('Gravatar removed.'),
'success' => true);
}
2010-09-18 16:14:00 +01:00
2011-04-14 16:38:41 +01:00
function onEndProfileGetAvatar($profile, $size, &$avatar)
{
if (empty($avatar)) {
$user = $profile->getUser();
if (!empty($user) && !empty($user->email)) {
// Fake one!
$avatar = new Avatar();
$avatar->width = $avatar->height = $size;
$avatar->url = $this->gravatar_url($user->email, $size);
return false;
}
}
return true;
}
function gravatar_url($email, $size)
{
$url = "https://secure.gravatar.com/avatar.php?gravatar_id=".
2009-12-10 03:32:57 +00:00
md5(strtolower($email)).
"&default=".urlencode(Avatar::defaultImage($size)).
"&size=".$size;
return $url;
}
function onPluginVersion(&$versions)
{
$versions[] = array('name' => 'Gravatar',
'version' => STATUSNET_VERSION,
2011-04-14 16:38:41 +01:00
'author' => 'Eric Helgeson, Evan Prodromou',
'homepage' => 'http://status.net/wiki/Plugin:Gravatar',
'rawdescription' =>
// TRANS: Plugin decsription.
_m('The Gravatar plugin allows users to use their <a href="http://www.gravatar.com/">Gravatar</a> with StatusNet.'));
return true;
}
2010-09-18 16:14:00 +01:00
}