friendica-addons/birdavatar/birdavatar.php

272 lines
7.2 KiB
PHP
Raw Permalink Normal View History

2021-11-24 11:05:12 +00:00
<?php
/**
* Name: Bird Avatar Generator
* Description: Generate a default avatar based on David Revoy's bird-avatar-generator https://www.peppercarrot.com/extras/html/2019_bird-generator/index.php
* Version: 1.0
* Author: Fabio <https://kirgroup.com/profile/fabrixxm>
*/
use Friendica\Core\Hook;
use Friendica\Core\Logger;
use Friendica\Core\Renderer;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Photo;
use Friendica\Model\Profile;
use Friendica\Network\HTTPException\NotFoundException;
define("BIRDAVATAR_SIZE", 256);
/**
* Installs the addon hook
*/
function birdavatar_install()
{
Hook::register('avatar_lookup', __FILE__, 'birdavatar_lookup');
Hook::register('addon_settings', __FILE__, 'birdavatar_addon_settings');
Hook::register('addon_settings_post', __FILE__, 'birdavatar_addon_settings_post');
2021-11-24 11:05:12 +00:00
Logger::info('registered birdavatar');
2021-11-24 11:05:12 +00:00
}
/**
* Bird avatar user settings page
*/
function birdavatar_addon_settings(array &$data)
2021-11-24 11:05:12 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2021-11-24 11:05:12 +00:00
return;
}
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/birdavatar/');
$html = Renderer::replaceMacros($t, [
2021-11-24 11:05:12 +00:00
'$uncache' => time(),
2022-10-20 21:51:49 +00:00
'$uid' => DI::userSession()->getLocalUserId(),
2021-11-24 11:05:12 +00:00
'$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the bird.'),
]);
$data = [
'addon' => 'birdavar',
'title' => DI::l10n()->t('Bird Avatar Settings'),
'html' => $html,
'submit' => [
'birdavatar-usebird' => DI::l10n()->t('Use Bird as Avatar'),
'birdavatar-morebird' => DI::l10n()->t('More Random Bird!'),
2022-10-20 21:51:49 +00:00
'birdavatar-emailbird' => DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed', false) ? DI::l10n()->t('Reset to email Bird') : null,
],
];
2021-11-24 11:05:12 +00:00
}
/**
* Bird avatar user settings POST handle
*/
function birdavatar_addon_settings_post(&$s)
2021-11-24 11:05:12 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2021-11-24 11:05:12 +00:00
return;
}
if (!empty($_POST['birdavatar-usebird'])) {
$url = DI::baseUrl() . '/birdavatar/' . DI::userSession()->getLocalUserId() . '?ts=' . time();
2021-11-24 11:05:12 +00:00
2022-10-20 21:51:49 +00:00
$self = DBA::selectFirst('contact', ['id'], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
2021-11-24 11:05:12 +00:00
if (!DBA::isResult($self)) {
2022-10-17 20:17:25 +00:00
DI::sysmsg()->addNotice(DI::l10n()->t("The bird has not found itself."));
2021-11-24 11:05:12 +00:00
return;
}
2022-10-20 21:51:49 +00:00
Photo::importProfilePhoto($url, DI::userSession()->getLocalUserId(), $self['id']);
2021-11-24 11:05:12 +00:00
2022-10-20 21:51:49 +00:00
$condition = ['uid' => DI::userSession()->getLocalUserId(), 'contact-id' => $self['id']];
2021-11-24 11:05:12 +00:00
$photo = DBA::selectFirst('photo', ['resource-id'], $condition);
if (!DBA::isResult($photo)) {
2022-10-17 20:17:25 +00:00
DI::sysmsg()->addNotice(DI::l10n()->t('There was an error, the bird flew away.'));
2021-11-24 11:05:12 +00:00
return;
}
2022-10-20 21:51:49 +00:00
DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => DI::userSession()->getLocalUserId()]);
2021-11-24 11:05:12 +00:00
$fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
2022-10-20 21:51:49 +00:00
DBA::update('photo', $fields, ['uid' => DI::userSession()->getLocalUserId(), 'resource-id' => $photo['resource-id']]);
2021-11-24 11:05:12 +00:00
2022-10-20 21:51:49 +00:00
Photo::importProfilePhoto($url, DI::userSession()->getLocalUserId(), $self['id']);
2021-11-24 11:05:12 +00:00
2022-10-20 21:51:49 +00:00
Contact::updateSelfFromUserID(DI::userSession()->getLocalUserId(), true);
2021-11-24 11:05:12 +00:00
// Update global directory in background
2022-10-20 21:51:49 +00:00
Profile::publishUpdate(DI::userSession()->getLocalUserId());
2021-11-24 11:05:12 +00:00
2022-10-17 20:17:25 +00:00
DI::sysmsg()->addInfo(DI::l10n()->t('Meow!'));
2021-11-24 11:05:12 +00:00
return;
}
if (!empty($_POST['birdavatar-morebird'])) {
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed', time());
2021-11-24 11:05:12 +00:00
}
if (!empty($_POST['birdavatar-emailbird'])) {
2022-10-20 21:51:49 +00:00
DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'birdavatar', 'seed');
2021-11-24 11:05:12 +00:00
}
}
/**
* Returns the URL to the bird avatar
*
* @param &$b array
*/
function birdavatar_lookup(array &$b)
2021-11-24 11:05:12 +00:00
{
$user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
if (DBA::isResult($user)) {
$url = DI::baseUrl() . '/birdavatar/' . $user['uid'];
2021-11-24 11:05:12 +00:00
} else {
$url = DI::baseUrl() . '/birdavatar/' . md5(trim(strtolower($b['email'])));
2021-11-24 11:05:12 +00:00
}
switch ($b['size']) {
case 300: $url .= "/4"; break;
case 80: $url .= "/5"; break;
case 48: $url .= "/6"; break;
}
$b['url'] = $url;
$b['success'] = true;
}
/**
* This is a statement rather than an actual function definition. The simple
* existence of this method is checked to figure out if the addon offers a
* module.
*/
function birdavatar_module() {}
2021-11-24 11:05:12 +00:00
/**
* Returns image for user id
*
* @throws NotFoundException
*
*/
function birdavatar_content()
2021-11-24 11:05:12 +00:00
{
if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
throw new NotFoundException(); // this should be catched on index and show default "not found" page.
}
if (is_numeric(DI::args()->getArgv()[1])) {
2021-11-24 13:30:30 +00:00
$uid = intval(DI::args()->getArgv()[1]);
2021-11-24 11:05:12 +00:00
$condition = ['uid' => $uid,
2021-11-24 13:30:30 +00:00
'account_expired' => false, 'account_removed' => false];
2021-11-24 11:05:12 +00:00
$user = DBA::selectFirst('user', ['email'], $condition);
if ($user === false) {
throw new NotFoundException();
}
$seed = DI::pConfig()->get($uid, "birdavatar", "seed", md5(trim(strtolower($user['email']))));
} elseif (!empty(DI::args()->getArgv()[1])) {
$seed = DI::args()->getArgv()[1];
} else {
throw new NotFoundException();
}
$size = 0;
if (DI::args()->getArgc() == 3) {
$size = intval(DI::args()->getArgv()[2]);
}
// start generation
ob_start();
// render the picture:
build_bird($seed, $size);
ob_end_flush();
exit();
}
/**
* ====================
2021-11-24 11:12:43 +00:00
* BIRD-AVATAR-GENERATOR
2021-11-24 11:05:12 +00:00
* ====================
*
* @authors: Andreas Gohr, David Revoy
*
* This PHP is licensed under the short and simple permissive:
* [MIT License](https://en.wikipedia.org/wiki/MIT_License)
*
**/
function build_bird($seed = '', $size = 0)
{
// init random seed
if ($seed) {
srand(hexdec(substr(md5($seed), 0, 6)));
}
// throw the dice for body parts
$parts = [
'tail' => rand(1,9),
'hoop' => rand(1,10),
'body' => rand(1,9),
'wing' => rand(1,9),
'eyes' => rand(1,9),
'bec' => rand(1,9),
'accessorie' => rand(1,20)
];
// create backgound
$bird = @imagecreatetruecolor(BIRDAVATAR_SIZE, BIRDAVATAR_SIZE)
or die("GD image create failed");
$white = imagecolorallocate($bird, 255, 255, 255);
imagefill($bird, 0, 0, $white);
// add parts
foreach ($parts as $part => $num) {
$file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
$im = @imagecreatefrompng($file);
if (!$im) {
die('Failed to load ' . $file);
}
imageSaveAlpha($im, true);
imagecopy($bird, $im, 0, 0, 0, 0, BIRDAVATAR_SIZE, BIRDAVATAR_SIZE);
imagedestroy($im);
}
// scale image
if ($size > 3 && $size < 7) {
switch ($size) {
case 4:
$size = 300;
break;
case 5:
$size = 80;
break;
case 6:
$size = 48;
break;
}
$dest = imagecreatetruecolor($size, $size) or die("GD image create failed");
imagealphablending($dest, false);
imagesavealpha($dest, true);
imagecopyresampled($dest, $bird, 0, 0, 0, 0, $size, $size, BIRDAVATAR_SIZE, BIRDAVATAR_SIZE);
imagedestroy($bird);
$bird = $dest;
}
// restore random seed
if ($seed) {
srand();
}
header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
header('Content-Type: image/jpg');
imagejpeg($bird, null, 90);
imagedestroy($bird);
}