2018-04-05 10:56:36 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Name: Cat Avatar Generator
|
|
|
|
* Description: Generate a default avatar based on David Revoy's cat-avatar-generator https://framagit.org/Deevad/cat-avatar-generator
|
|
|
|
* Version: 1.1
|
|
|
|
* Author: Fabio <https://kirgroup.com/profile/fabrixxm>
|
|
|
|
*/
|
2018-07-20 04:18:02 +02:00
|
|
|
|
2018-04-06 18:25:44 +02:00
|
|
|
use Friendica\App;
|
2018-12-26 08:28:16 +01:00
|
|
|
use Friendica\Core\Hook;
|
2018-10-30 00:40:18 +01:00
|
|
|
use Friendica\Core\Logger;
|
2018-10-31 15:55:15 +01:00
|
|
|
use Friendica\Core\Renderer;
|
2018-07-20 04:18:02 +02:00
|
|
|
use Friendica\Core\Worker;
|
2018-07-20 14:20:48 +02:00
|
|
|
use Friendica\Database\DBA;
|
2019-12-16 01:05:14 +01:00
|
|
|
use Friendica\DI;
|
2018-04-13 08:34:16 +02:00
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Model\Photo;
|
2021-06-16 07:26:10 +02:00
|
|
|
use Friendica\Model\Profile;
|
2018-07-20 04:18:02 +02:00
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
define("CATAVATAR_SIZE", 256);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the addon hook
|
|
|
|
*/
|
2018-04-05 22:21:59 +02:00
|
|
|
function catavatar_install()
|
|
|
|
{
|
2021-11-20 10:56:55 +01:00
|
|
|
Hook::register('avatar_lookup', __FILE__, 'catavatar_lookup');
|
|
|
|
Hook::register('addon_settings', __FILE__, 'catavatar_addon_settings');
|
|
|
|
Hook::register('addon_settings_post', __FILE__, 'catavatar_addon_settings_post');
|
2018-04-05 10:56:36 +02:00
|
|
|
|
2021-10-21 08:04:27 +02:00
|
|
|
Logger::notice('registered catavatar');
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 22:21:59 +02:00
|
|
|
/**
|
|
|
|
* Cat avatar user settings page
|
|
|
|
*/
|
2021-11-20 10:56:55 +01:00
|
|
|
function catavatar_addon_settings(App $a, array &$data)
|
2018-04-05 22:21:59 +02:00
|
|
|
{
|
|
|
|
if (!local_user()) {
|
2018-04-05 10:56:36 +02:00
|
|
|
return;
|
2018-04-05 22:21:59 +02:00
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
2021-11-20 10:56:55 +01:00
|
|
|
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
|
|
|
|
$html = Renderer::replaceMacros($t, [
|
|
|
|
'$uncache' => time(),
|
|
|
|
'$uid' => local_user(),
|
2020-12-28 23:26:25 +01:00
|
|
|
'$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the cat.'),
|
2018-04-05 10:56:36 +02:00
|
|
|
]);
|
2021-11-20 10:56:55 +01:00
|
|
|
|
|
|
|
$data = [
|
|
|
|
'addon' => 'catavar',
|
|
|
|
'title' => DI::l10n()->t('Cat Avatar Settings'),
|
|
|
|
'html' => $html,
|
|
|
|
'submit' => [
|
|
|
|
'catavatar-usecat' => DI::l10n()->t('Use Cat as Avatar'),
|
|
|
|
'catavatar-morecat' => DI::l10n()->t('Another random Cat!'),
|
|
|
|
'catavatar-emailcat' => DI::pConfig()->get(local_user(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null,
|
|
|
|
],
|
|
|
|
];
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 22:21:59 +02:00
|
|
|
/**
|
|
|
|
* Cat avatar user settings POST handle
|
|
|
|
*/
|
2018-04-06 18:25:44 +02:00
|
|
|
function catavatar_addon_settings_post(App $a, &$s)
|
2018-04-05 22:21:59 +02:00
|
|
|
{
|
|
|
|
if (!local_user()) {
|
2018-04-05 10:56:36 +02:00
|
|
|
return;
|
2018-04-05 22:21:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:25:44 +02:00
|
|
|
if (!empty($_POST['catavatar-usecat'])) {
|
2019-12-16 01:05:14 +01:00
|
|
|
$url = DI::baseUrl()->get() . '/catavatar/' . local_user() . '?ts=' . time();
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2018-07-20 14:20:48 +02:00
|
|
|
$self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
|
2018-07-21 14:46:13 +02:00
|
|
|
if (!DBA::isResult($self)) {
|
2020-01-18 20:52:33 +01:00
|
|
|
notice(DI::l10n()->t("The cat hadn't found itself."));
|
2018-04-05 10:56:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 08:34:16 +02:00
|
|
|
Photo::importProfilePhoto($url, local_user(), $self['id']);
|
2018-04-05 10:56:36 +02:00
|
|
|
|
2018-04-13 08:34:16 +02:00
|
|
|
$condition = ['uid' => local_user(), 'contact-id' => $self['id']];
|
2018-07-20 14:20:48 +02:00
|
|
|
$photo = DBA::selectFirst('photo', ['resource-id'], $condition);
|
2018-07-21 14:46:13 +02:00
|
|
|
if (!DBA::isResult($photo)) {
|
2020-01-18 20:52:33 +01:00
|
|
|
notice(DI::l10n()->t('There was an error, the cat ran away.'));
|
2018-04-05 10:56:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-20 14:20:48 +02:00
|
|
|
DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
|
2018-04-13 08:34:16 +02:00
|
|
|
|
2020-01-18 20:52:33 +01:00
|
|
|
$fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
|
2018-07-20 14:20:48 +02:00
|
|
|
DBA::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
|
2018-04-13 08:34:16 +02:00
|
|
|
|
|
|
|
Photo::importProfilePhoto($url, local_user(), $self['id']);
|
|
|
|
|
|
|
|
Contact::updateSelfFromUserID(local_user(), true);
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
// Update global directory in background
|
2021-06-16 07:26:10 +02:00
|
|
|
Profile::publishUpdate(local_user());
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2020-01-18 20:52:33 +01:00
|
|
|
info(DI::l10n()->t('Meow!'));
|
2018-04-05 10:56:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:21:59 +02:00
|
|
|
if (!empty($_POST['catavatar-morecat'])) {
|
2020-01-18 16:54:49 +01:00
|
|
|
DI::pConfig()->set(local_user(), 'catavatar', 'seed', time());
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
|
2018-04-05 22:21:59 +02:00
|
|
|
if (!empty($_POST['catavatar-emailcat'])) {
|
2020-01-18 16:56:45 +01:00
|
|
|
DI::pConfig()->delete(local_user(), 'catavatar', 'seed');
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL to the cat avatar
|
|
|
|
*
|
|
|
|
* @param $a array
|
|
|
|
* @param &$b array
|
|
|
|
*/
|
2018-04-06 18:25:44 +02:00
|
|
|
function catavatar_lookup(App $a, &$b)
|
2018-04-05 22:21:59 +02:00
|
|
|
{
|
2018-07-20 14:20:48 +02:00
|
|
|
$user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
|
2020-12-06 17:04:07 +01:00
|
|
|
if (DBA::isResult($user)) {
|
|
|
|
$url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
|
|
|
|
} else {
|
|
|
|
$url = DI::baseUrl()->get() . '/catavatar/' . md5(trim(strtolower($b['email'])));
|
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
switch($b['size']) {
|
2018-10-23 16:38:01 +02:00
|
|
|
case 300: $url .= "/4"; break;
|
2018-04-05 22:21:59 +02:00
|
|
|
case 80: $url .= "/5"; break;
|
2020-12-06 17:04:07 +01:00
|
|
|
case 48: $url .= "/6"; break;
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$b['url'] = $url;
|
|
|
|
$b['success'] = true;
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:52:11 +02:00
|
|
|
function catavatar_module() {}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns image for user id
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*
|
|
|
|
*/
|
2018-04-06 18:25:44 +02:00
|
|
|
function catavatar_content(App $a)
|
2018-04-05 22:21:59 +02:00
|
|
|
{
|
2021-07-25 14:29:43 +02:00
|
|
|
if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
|
2018-04-05 10:56:36 +02:00
|
|
|
throw new NotFoundException(); // this should be catched on index and show default "not found" page.
|
2018-04-05 22:21:59 +02:00
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
2021-07-25 14:29:43 +02:00
|
|
|
if (is_numeric(DI::args()->getArgv()[1])) {
|
|
|
|
$uid = intval(DI::args()->getArgv()[1]);
|
2020-12-06 17:04:07 +01:00
|
|
|
$condition = ['uid' => $uid,
|
|
|
|
'account_expired' => false, 'account_removed' => false];
|
|
|
|
$user = DBA::selectFirst('user', ['email'], $condition);
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2020-12-06 17:04:07 +01:00
|
|
|
if ($user === false) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2020-12-06 17:04:07 +01:00
|
|
|
$seed = DI::pConfig()->get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
|
2021-07-25 14:29:43 +02:00
|
|
|
} elseif (!empty(DI::args()->getArgv()[1])) {
|
|
|
|
$seed = DI::args()->getArgv()[1];
|
2020-12-06 17:04:07 +01:00
|
|
|
} else {
|
2018-04-05 10:56:36 +02:00
|
|
|
throw new NotFoundException();
|
2018-04-05 22:21:59 +02:00
|
|
|
}
|
|
|
|
|
2020-12-06 17:04:07 +01:00
|
|
|
$size = 0;
|
2021-07-25 14:29:43 +02:00
|
|
|
if (DI::args()->getArgc() == 3) {
|
|
|
|
$size = intval(DI::args()->getArgv()[2]);
|
2020-12-06 17:04:07 +01:00
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
// ...Or start generation
|
2018-04-05 22:21:59 +02:00
|
|
|
ob_start();
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
// render the picture:
|
|
|
|
build_cat($seed, $size);
|
|
|
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
2018-04-06 18:25:44 +02:00
|
|
|
exit();
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ====================
|
|
|
|
* CAT-AVATAR-GENERATOR
|
|
|
|
* ====================
|
2018-04-05 22:21:59 +02:00
|
|
|
*
|
2018-04-05 10:56:36 +02:00
|
|
|
* @authors: Andreas Gohr, David Revoy
|
2018-04-05 22:21:59 +02:00
|
|
|
*
|
2018-04-05 10:56:36 +02:00
|
|
|
* This PHP is licensed under the short and simple permissive:
|
|
|
|
* [MIT License](https://en.wikipedia.org/wiki/MIT_License)
|
2018-04-05 22:21:59 +02:00
|
|
|
*
|
2018-04-05 10:56:36 +02:00
|
|
|
**/
|
|
|
|
|
2018-04-13 10:31:16 +02:00
|
|
|
function build_cat($seed = '', $size = 0)
|
|
|
|
{
|
2018-04-05 10:56:36 +02:00
|
|
|
// init random seed
|
2018-04-13 10:31:16 +02:00
|
|
|
if ($seed) {
|
2018-04-13 12:28:25 +02:00
|
|
|
srand(hexdec(substr(md5($seed), 0, 6)));
|
2018-04-13 10:31:16 +02:00
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
// throw the dice for body parts
|
|
|
|
$parts = array(
|
2018-04-13 12:28:25 +02:00
|
|
|
'body' => rand(1, 15),
|
|
|
|
'fur' => rand(1, 10),
|
|
|
|
'eyes' => rand(1, 15),
|
|
|
|
'mouth' => rand(1, 10),
|
|
|
|
'accessorie' => rand(1, 20)
|
2018-04-05 10:56:36 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// create backgound
|
|
|
|
$cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
|
|
|
|
or die("GD image create failed");
|
|
|
|
$white = imagecolorallocate($cat, 255, 255, 255);
|
2018-04-13 12:28:25 +02:00
|
|
|
imagefill($cat, 0, 0, $white);
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
// add parts
|
2018-04-16 08:52:11 +02:00
|
|
|
foreach ($parts as $part => $num) {
|
2018-04-13 12:28:25 +02:00
|
|
|
$file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
$im = @imagecreatefrompng($file);
|
2018-04-13 10:31:16 +02:00
|
|
|
if (!$im) {
|
2018-04-13 12:28:25 +02:00
|
|
|
die('Failed to load ' . $file);
|
2018-04-13 10:31:16 +02:00
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
imageSaveAlpha($im, true);
|
2018-04-13 12:28:25 +02:00
|
|
|
imagecopy($cat, $im, 0, 0, 0, 0, CATAVATAR_SIZE, CATAVATAR_SIZE);
|
2018-04-05 10:56:36 +02:00
|
|
|
imagedestroy($im);
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale image
|
|
|
|
if ($size > 3 && $size < 7) {
|
2018-04-13 10:31:16 +02:00
|
|
|
switch ($size) {
|
|
|
|
case 4:
|
2018-10-23 16:38:01 +02:00
|
|
|
$size = 300;
|
2018-04-13 10:31:16 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
$size = 80;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
$size = 48;
|
|
|
|
break;
|
2018-04-05 10:56:36 +02:00
|
|
|
}
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2018-04-05 10:56:36 +02:00
|
|
|
$dest = imagecreatetruecolor($size, $size);
|
|
|
|
imagealphablending($dest, false);
|
|
|
|
imagesavealpha($dest, true);
|
|
|
|
imagecopyresampled($dest, $cat, 0, 0, 0, 0, $size, $size, CATAVATAR_SIZE, CATAVATAR_SIZE);
|
|
|
|
imagedestroy($cat);
|
|
|
|
$cat = $dest;
|
|
|
|
}
|
2018-04-05 22:21:59 +02:00
|
|
|
|
2018-04-05 10:56:36 +02:00
|
|
|
// restore random seed
|
2018-04-06 18:25:44 +02:00
|
|
|
if ($seed) {
|
|
|
|
srand();
|
|
|
|
}
|
2018-04-05 10:56:36 +02:00
|
|
|
|
|
|
|
header('Pragma: public');
|
|
|
|
header('Cache-Control: max-age=86400');
|
2018-04-13 12:28:25 +02:00
|
|
|
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
|
2018-04-05 10:56:36 +02:00
|
|
|
header('Content-Type: image/jpg');
|
|
|
|
imagejpeg($cat, NULL, 90);
|
|
|
|
imagedestroy($cat);
|
|
|
|
}
|