Skip to content

Commit

Permalink
Add Gravatar / Libravatar plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Afterster committed Jan 28, 2014
1 parent 4253d85 commit 470dcea
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 5 deletions.
5 changes: 5 additions & 0 deletions image.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
$size['height'] = 200;
$size['width'] = 200; // 200px width, set via CSS
break;
case '5':
/* Web Player size */
$size['height'] = 32;
$size['width'] = 32;
break;
default:
$size['height'] = '275';
$size['width'] = '275';
Expand Down
16 changes: 16 additions & 0 deletions lib/class/update.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ public static function populate_version()
$update_string = '- Add website field on users.<br />';
$version[] = array('version' => '360039','description' => $update_string);

$update_string = '- Add avatar field on users.<br />';
$version[] = array('version' => '360040','description' => $update_string);

return $version;
}

Expand Down Expand Up @@ -2148,4 +2151,17 @@ public static function update_360039()

return true;
}

/**
* update_360040
*
* Add avatar field on users
*/
public static function update_360040()
{
$sql = "ALTER TABLE `user` ADD `avatar` BLOB NULL AFTER `website`";
Dba::write($sql);

return true;
}
}
61 changes: 57 additions & 4 deletions lib/class/user.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class User extends database_object
public $create_date;
public $validation;
public $website;
public $avatar;

// Constructed variables
public $prefs = array();
Expand Down Expand Up @@ -479,6 +480,7 @@ public function update($data)
case 'username':
case 'fullname':
case 'website':
case 'avatar':
if ($this->$name != $value) {
$function = 'update_' . $name;
$this->$function($value);
Expand Down Expand Up @@ -555,6 +557,17 @@ public function update_website($new_website)

} // update_website

/**
* update_avatar
* updates their avatar
*/
public function update_avatar($new_avatar)
{
$sql = "UPDATE `user` SET `avatar` = ? WHERE `id` = ?";
$db_results = Dba::write($sql, array($new_avatar, $this->id));

} // update_avatar

/**
* disable
* This disables the current user
Expand Down Expand Up @@ -710,7 +723,7 @@ public function insert_ip_history()
* create
* inserts a new user into ampache
*/
public static function create($username, $fullname, $email, $website, $password, $access, $disabled = false)
public static function create($username, $fullname, $email, $website, $password, $access, $avatar = null, $disabled = false)
{
$website = rtrim($website, "/");
$password = hash('sha256', $password);
Expand All @@ -719,9 +732,9 @@ public static function create($username, $fullname, $email, $website, $password,
/* Now Insert this new user */
$sql = "INSERT INTO `user` (`username`, `disabled`, " .
"`fullname`, `email`, `website`, `password`, `access`, " .
"`create_date`)" .
"VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
$db_results = Dba::write($sql, array($username, $disabled, $fullname, $email, $website, $password, $access, time()));
"`avatar`, `create_date`)" .
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)";
$db_results = Dba::write($sql, array($username, $disabled, $fullname, $email, $website, $password, $access, $avatar, time()));

if (!$db_results) { return false; }

Expand Down Expand Up @@ -787,6 +800,14 @@ public function format()
$this->ip_history = T_('Not Enough Data');
}

$avatar = $this->get_avatar();
if (!empty($avatar['url'])) {
$this->f_avatar = '<img src="' . $avatar['url'] . '" title="' . $avatar['title'] . '" />';
}
if (!empty($avatar['url_mini'])) {
$this->f_avatar_mini = '<img src="' . $avatar['url_mini'] . '" title="' . $avatar['title'] . '" />';
}

} // format_user

/**
Expand Down Expand Up @@ -1096,6 +1117,38 @@ public function get_ip_history($count='',$distinct='')

} // get_ip_history

/**
* get_avatar
* Get the user avatar
*/
public function get_avatar()
{
$avatar = array();

$avatar['title'] = T_('User avatar');
if ($this->avatar) {
$avatar['url'] = AmpConfig::get('web_path') . '/image.php?object_type=user&id=' . $this->id;
$avatar['url_mini'] = $avatar['url'];
$avatar['url'] .= '&thumb=3';
$avatar['url_mini'] .= '&thumb=5';
$avatar['data'] = $this->avatar;
} else {
foreach (Plugin::get_plugins('get_avatar_url') as $plugin_name) {
$plugin = new Plugin($plugin_name);
if ($plugin->load($GLOBALS['user'])) {
$avatar['url'] = $plugin->_plugin->get_avatar_url($this);
if (!empty($avatar['url'])) {
$avatar['url_mini'] = $plugin->_plugin->get_avatar_url($this, 32);
$avatar['title'] .= ' (' . $plugin->_plugin->name . ')';
break;
}
}
}
}

return $avatar;
} // get_avatar

/**
* activate_user
* the user from public_registration
Expand Down
96 changes: 96 additions & 0 deletions modules/plugins/Gravatar.plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright 2001 - 2013 Ampache.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License v2
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

class AmpacheGravatar {

public $name = 'Gravatar';
public $description = 'Users avatars with Gravatar';
public $url = 'http://gravatar.com';
public $version = '000001';
public $min_ampache = '360040';
public $max_ampache = '999999';

/**
* Constructor
* This function does nothing...
*/
public function __construct() {

return true;

} // constructor

/**
* install
* This is a required plugin function. It inserts our preferences
* into Ampache
*/
public function install() {
return true;
} // install

/**
* uninstall
* This is a required plugin function. It removes our preferences from
* the database returning it to its original form
*/
public function uninstall() {
return true;
} // uninstall

/**
* upgrade
* This is a recommended plugin function
*/
public function upgrade() {
return true;
} // upgrade

public function get_avatar_url($user, $size = 80) {

$url = "";
if (!empty($user->email)) {
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
$url = "https://secure.gravatar.com";
} else {
$url = "http://www.gravatar.com";
}
$url .= "/avatar/";
$url .= md5(strtolower(trim($user->email)));
$url .= "?s=" . $size . "&r=g";
$url .= "&d=identicon";
}

return $url;
}

/**
* load
* This loads up the data we need into this object, this stuff comes
* from the preferences.
*/
public function load($user) {
return true;
} // load

} // end AmpacheGravatar
?>
97 changes: 97 additions & 0 deletions modules/plugins/Libravatar.plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU General Public License, version 2 (GPLv2)
* Copyright 2001 - 2013 Ampache.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License v2
* as published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

class AmpacheLibravatar {

public $name = 'Libravatar';
public $description = 'Users avatars with Libravatar';
public $url = 'https://www.libravatar.org';
public $version = '000001';
public $min_ampache = '360040';
public $max_ampache = '999999';

/**
* Constructor
* This function does nothing...
*/
public function __construct() {

return true;

} // constructor

/**
* install
* This is a required plugin function. It inserts our preferences
* into Ampache
*/
public function install() {
return true;
} // install

/**
* uninstall
* This is a required plugin function. It removes our preferences from
* the database returning it to its original form
*/
public function uninstall() {
return true;
} // uninstall

/**
* upgrade
* This is a recommended plugin function
*/
public function upgrade() {
return true;
} // upgrade

public function get_avatar_url($user, $size = 80) {

$url = "";
if (!empty($user->email)) {
// Federated Servers are not supported here without libravatar.org. Should query DNS server first.
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') {
$url = "https://seccdn.libravatar.org";
} else {
$url = "http://cdn.libravatar.org";
}
$url .= "/avatar/";
$url .= md5(strtolower(trim($user->email)));
$url .= "?s=" . $size . "&r=g";
$url .= "&d=identicon";
}

return $url;
}

/**
* load
* This loads up the data we need into this object, this stuff comes
* from the preferences.
*/
public function load($user) {
return true;
} // load

} // end AmpacheLibravatar
?>
2 changes: 1 addition & 1 deletion register.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@


$new_user = User::create($username, $fullname, $email, $website, $pass1,
$access, AmpConfig::get('admin_enable_required'));
$access, null, AmpConfig::get('admin_enable_required'));

if (!$new_user) {
Error::add('duplicate_user', T_("Error: Insert Failed"));
Expand Down
1 change: 1 addition & 0 deletions templates/show_now_playing.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
foreach ($results as $item) {
$media = $item['media'];
$np_user = $item['client'];
$np_user->format();
$agent = $item['agent'];

/* If we've gotten a non-song object just skip this row */
Expand Down
5 changes: 5 additions & 0 deletions templates/show_now_playing_row.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<label><?php echo T_('Username'); ?></label>
<a title="<?php echo scrub_out($agent); ?>" href="<?php echo $web_path; ?>/stats.php?action=show_user&amp;user_id=<?php echo $np_user->id; ?>">
<?php echo scrub_out($np_user->fullname); ?>
<?php
if ($np_user->f_avatar) {
echo '<div>' . $np_user->f_avatar . '</div>';
}
?>
</a>
</div>
<br />
Expand Down
5 changes: 5 additions & 0 deletions templates/show_user.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
$client->format();
?>
<?php UI::show_box_top($client->fullname); ?>
<?php
if ($client->f_avatar) {
echo '<div class="user_avatar">' . $client->f_avatar . '</div>';
}
?>
<dl class="song_details">
<?php $rowparity = UI::flip_class(); ?>
<dt class="<?php echo $rowparity; ?>"><?php echo T_('Full Name'); ?></dt>
Expand Down
5 changes: 5 additions & 0 deletions templates/show_user_row.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
?>
<td class="cel_username">
<a href="<?php echo $web_path; ?>/stats.php?action=show_user&amp;user_id=<?php echo $client->id; ?>">
<?php
if ($client->f_avatar_mini) {
echo $client->f_avatar_mini;
}
?>
<?php echo $client->fullname; ?> (<?php echo $client->username; ?>)
</a>
</td>
Expand Down
Loading

0 comments on commit 470dcea

Please sign in to comment.