2018-04-05 08:56:36 +00: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-04-06 16:25:44 +00:00
|
|
|
use Friendica\App;
|
2018-04-05 08:56:36 +00:00
|
|
|
use Friendica\Core\Addon;
|
|
|
|
use Friendica\Core\Config;
|
|
|
|
use Friendica\Core\L10n;
|
|
|
|
use Friendica\Core\Worker;
|
|
|
|
use Friendica\Core\PConfig;
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
2018-04-13 06:34:16 +00:00
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Model\Photo;
|
|
|
|
use Friendica\Database\DBM;
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
define("CATAVATAR_SIZE", 256);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installs the addon hook
|
|
|
|
*/
|
2018-04-05 20:21:59 +00:00
|
|
|
function catavatar_install()
|
|
|
|
{
|
2018-04-05 08:56:36 +00:00
|
|
|
Addon::registerHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
|
|
|
|
Addon::registerHook('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
|
|
|
|
Addon::registerHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
|
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
logger('registered catavatar');
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the addon hook
|
|
|
|
*/
|
2018-04-05 20:21:59 +00:00
|
|
|
function catavatar_uninstall()
|
|
|
|
{
|
2018-04-05 08:56:36 +00:00
|
|
|
Addon::unregisterHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup');
|
|
|
|
Addon::unregisterHook('addon_settings', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
|
|
|
|
Addon::unregisterHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
|
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
logger('unregistered catavatar');
|
|
|
|
}
|
2018-04-05 08:56:36 +00:00
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
/**
|
|
|
|
* Cat avatar user settings page
|
|
|
|
*/
|
2018-04-06 16:25:44 +00:00
|
|
|
function catavatar_addon_settings(App $a, &$s)
|
2018-04-05 20:21:59 +00:00
|
|
|
{
|
|
|
|
if (!local_user()) {
|
2018-04-05 08:56:36 +00:00
|
|
|
return;
|
2018-04-05 20:21:59 +00:00
|
|
|
}
|
2018-04-05 08:56:36 +00:00
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
$t = get_markup_template('settings.tpl', 'addon/catavatar/');
|
2018-04-09 13:16:52 +00:00
|
|
|
$s .= replace_macros ($t, [
|
2018-04-05 20:21:59 +00:00
|
|
|
'$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
|
2018-04-05 08:56:36 +00:00
|
|
|
'$uncache' => time(),
|
|
|
|
'$uid' => local_user(),
|
|
|
|
'$usecat' => L10n::t('Use Cat as Avatar'),
|
|
|
|
'$morecat' => L10n::t('More Random Cat!'),
|
|
|
|
'$emailcat' => L10n::t('Reset to email Cat'),
|
2018-04-05 20:21:59 +00:00
|
|
|
'$seed' => PConfig::get(local_user(), 'catavatar', 'seed', false),
|
|
|
|
'$header' => L10n::t('Cat Avatar Settings'),
|
2018-04-05 08:56:36 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
/**
|
|
|
|
* Cat avatar user settings POST handle
|
|
|
|
*/
|
2018-04-06 16:25:44 +00:00
|
|
|
function catavatar_addon_settings_post(App $a, &$s)
|
2018-04-05 20:21:59 +00:00
|
|
|
{
|
|
|
|
if (!local_user()) {
|
2018-04-05 08:56:36 +00:00
|
|
|
return;
|
2018-04-05 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
// delete the current cached cat avatar
|
|
|
|
$user = dba::selectFirst('user', ['email'],
|
|
|
|
[
|
|
|
|
'uid' => $uid,
|
|
|
|
'blocked' => 0,
|
|
|
|
'account_expired' => 0,
|
|
|
|
'account_removed' => 0,
|
|
|
|
]
|
|
|
|
);
|
2018-04-05 20:21:59 +00:00
|
|
|
$seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
|
|
|
|
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed);
|
2018-04-05 08:56:36 +00:00
|
|
|
$imageurl = substr($imageurl,0,35).'';
|
|
|
|
$cachefile = get_cachefile($imageurl);
|
|
|
|
if ($cachefile != "" && file_exists($cachefile)) {
|
|
|
|
unlink($cachefile);
|
|
|
|
}
|
2018-04-05 20:21:59 +00:00
|
|
|
|
|
|
|
|
2018-04-06 16:25:44 +00:00
|
|
|
if (!empty($_POST['catavatar-usecat'])) {
|
2018-04-05 20:21:59 +00:00
|
|
|
$url = $a->get_baseurl() . '/catavatar/' . local_user();
|
|
|
|
|
2018-04-13 06:34:16 +00:00
|
|
|
$self = dba::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
|
|
|
|
if (!DBM::is_result($self)) {
|
|
|
|
notice(L10n::t("The cat hadn't found itself."));
|
2018-04-05 08:56:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 06:34:16 +00:00
|
|
|
Photo::importProfilePhoto($url, local_user(), $self['id']);
|
2018-04-05 08:56:36 +00:00
|
|
|
|
2018-04-13 06:34:16 +00:00
|
|
|
$condition = ['uid' => local_user(), 'contact-id' => $self['id']];
|
|
|
|
$photo = dba::selectFirst('photo', ['resource-id'], $condition);
|
|
|
|
if (!DBM::is_result($photo)) {
|
2018-04-05 08:56:36 +00:00
|
|
|
notice(L10n::t('There was an error, the cat ran away.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-13 06:34:16 +00:00
|
|
|
dba::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
|
|
|
|
|
|
|
|
$fields = ['profile' => true, 'album' => L10n::t('Profile Photos'), 'contact-id' => 0];
|
|
|
|
dba::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
|
|
|
|
|
|
|
|
Photo::importProfilePhoto($url, local_user(), $self['id']);
|
|
|
|
|
|
|
|
Contact::updateSelfFromUserID(local_user(), true);
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
// Update global directory in background
|
|
|
|
$url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
|
|
|
|
if ($url && strlen(Config::get('system','directory'))) {
|
2018-04-05 20:21:59 +00:00
|
|
|
Worker::add(PRIORITY_LOW, 'Directory', $url);
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
|
2018-04-05 20:21:59 +00:00
|
|
|
|
|
|
|
info(L10n::t('Meow!'));
|
2018-04-05 08:56:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
|
|
|
|
if (!empty($_POST['catavatar-morecat'])) {
|
|
|
|
PConfig::set(local_user(), 'catavatar', 'seed', time());
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:21:59 +00:00
|
|
|
if (!empty($_POST['catavatar-emailcat'])) {
|
|
|
|
PConfig::delete(local_user(), 'catavatar', 'seed');
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL to the cat avatar
|
|
|
|
*
|
|
|
|
* @param $a array
|
|
|
|
* @param &$b array
|
|
|
|
*/
|
2018-04-06 16:25:44 +00:00
|
|
|
function catavatar_lookup(App $a, &$b)
|
2018-04-05 20:21:59 +00:00
|
|
|
{
|
|
|
|
$user = dba::selectFirst('user', ['uid'], ['email' => $b['email']]);
|
|
|
|
$url = $a->get_baseurl() . '/catavatar/' . $user['uid'];
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
switch($b['size']) {
|
2018-04-05 20:21:59 +00:00
|
|
|
case 175: $url .= "/4"; break;
|
|
|
|
case 80: $url .= "/5"; break;
|
|
|
|
case 47: $url .= "/6"; break;
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$b['url'] = $url;
|
|
|
|
$b['success'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function catavatar_module(){}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns image for user id
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
*
|
|
|
|
*/
|
2018-04-06 16:25:44 +00:00
|
|
|
function catavatar_content(App $a)
|
2018-04-05 20:21:59 +00:00
|
|
|
{
|
|
|
|
if ($a->argc < 2 || $a->argc > 3) {
|
2018-04-05 08:56:36 +00:00
|
|
|
throw new NotFoundException(); // this should be catched on index and show default "not found" page.
|
2018-04-05 20:21:59 +00:00
|
|
|
}
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
$uid = intval($a->argv[1]);
|
2018-04-05 20:21:59 +00:00
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
$size = 0;
|
|
|
|
if ($a->argc == 3) {
|
|
|
|
$size = intval($a->argv[2]);
|
|
|
|
}
|
2018-04-05 20:21:59 +00:00
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
$user = dba::selectFirst('user', ['email'],
|
|
|
|
[
|
|
|
|
'uid' => $uid,
|
|
|
|
'blocked' => 0,
|
|
|
|
'account_expired' => 0,
|
|
|
|
'account_removed' => 0,
|
|
|
|
]
|
2018-04-05 20:21:59 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($user === false) {
|
2018-04-05 08:56:36 +00:00
|
|
|
throw new NotFoundException();
|
2018-04-05 20:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email']))));
|
|
|
|
|
|
|
|
// from cat-avatar-generator.php
|
2018-04-05 20:21:59 +00:00
|
|
|
$imageurl = $seed . "-" . $size;
|
|
|
|
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl);
|
|
|
|
$imageurl = substr($imageurl,0,35) . '';
|
2018-04-05 08:56:36 +00:00
|
|
|
$cachefile = get_cachefile($imageurl);
|
|
|
|
$cachetime = 604800; # 1 week (1 day = 86400)
|
|
|
|
|
|
|
|
// Serve from the cache if it is younger than $cachetime
|
2018-04-05 20:21:59 +00:00
|
|
|
if ($cachefile != "" && file_exists($cachefile) && (time() - $cachetime) < filemtime($cachefile)) {
|
2018-04-05 08:56:36 +00:00
|
|
|
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');
|
|
|
|
readfile($cachefile);
|
2018-04-06 16:25:44 +00:00
|
|
|
exit();
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ...Or start generation
|
2018-04-05 20:21:59 +00:00
|
|
|
ob_start();
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
// render the picture:
|
|
|
|
build_cat($seed, $size);
|
|
|
|
|
|
|
|
// Save/cache the output to a file
|
2018-04-05 20:21:59 +00:00
|
|
|
if ($cachefile != "") {
|
2018-04-05 08:56:36 +00:00
|
|
|
$savedfile = fopen($cachefile, 'w+'); # w+ to be at start of the file, write mode, and attempt to create if not existing.
|
|
|
|
fwrite($savedfile, ob_get_contents());
|
|
|
|
fclose($savedfile);
|
|
|
|
chmod($cachefile, 0755);
|
|
|
|
}
|
2018-04-05 20:21:59 +00:00
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
ob_end_flush();
|
|
|
|
|
2018-04-06 16:25:44 +00:00
|
|
|
exit();
|
2018-04-05 08:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ====================
|
|
|
|
* CAT-AVATAR-GENERATOR
|
|
|
|
* ====================
|
2018-04-05 20:21:59 +00:00
|
|
|
*
|
2018-04-05 08:56:36 +00:00
|
|
|
* @authors: Andreas Gohr, David Revoy
|
2018-04-05 20:21:59 +00:00
|
|
|
*
|
2018-04-05 08:56:36 +00:00
|
|
|
* This PHP is licensed under the short and simple permissive:
|
|
|
|
* [MIT License](https://en.wikipedia.org/wiki/MIT_License)
|
2018-04-05 20:21:59 +00:00
|
|
|
*
|
2018-04-05 08:56:36 +00:00
|
|
|
**/
|
|
|
|
|
|
|
|
function build_cat($seed='', $size=0){
|
|
|
|
|
|
|
|
// init random seed
|
|
|
|
if($seed) srand( hexdec(substr(md5($seed),0,6)) );
|
|
|
|
|
|
|
|
// throw the dice for body parts
|
|
|
|
$parts = array(
|
|
|
|
'body' => rand(1,15),
|
|
|
|
'fur' => rand(1,10),
|
|
|
|
'eyes' => rand(1,15),
|
|
|
|
'mouth' => rand(1,10),
|
|
|
|
'accessorie' => rand(1,20)
|
|
|
|
);
|
|
|
|
|
|
|
|
// create backgound
|
|
|
|
$cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
|
|
|
|
or die("GD image create failed");
|
|
|
|
$white = imagecolorallocate($cat, 255, 255, 255);
|
|
|
|
imagefill($cat,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($cat,$im,0,0,0,0,CATAVATAR_SIZE,CATAVATAR_SIZE);
|
|
|
|
imagedestroy($im);
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale image
|
|
|
|
if ($size > 3 && $size < 7) {
|
|
|
|
switch($size) {
|
|
|
|
case 4: $size = 175; break;
|
|
|
|
case 5: $size = 80; break;
|
|
|
|
case 6: $size = 48; break;
|
|
|
|
}
|
2018-04-05 20:21:59 +00:00
|
|
|
|
2018-04-05 08:56:36 +00: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 20:21:59 +00:00
|
|
|
|
2018-04-05 08:56:36 +00:00
|
|
|
// restore random seed
|
2018-04-06 16:25:44 +00:00
|
|
|
if ($seed) {
|
|
|
|
srand();
|
|
|
|
}
|
2018-04-05 08:56:36 +00:00
|
|
|
|
|
|
|
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($cat, NULL, 90);
|
|
|
|
imagedestroy($cat);
|
|
|
|
}
|
|
|
|
|
|
|
|
|