Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cat Avatar Generator Addon #571

Merged
merged 1 commit into from Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions catavatar/README.md
@@ -0,0 +1,13 @@
# Friendica Cat Avatar Generator Addon

Generate a default avatar based on David Revoy's cat-avatar-generator https://framagit.org/Deevad/cat-avatar-generator

## License:

**Artworks:**
PNG and ORA files licensed under: [CC-By 4.0](https://creativecommons.org/licenses/by/4.0/)
attribution: David Revoy with the following exception:
Generated cats used as Avatar (for blog,forum,social-network) don't need direct
attribution and so, can be used as regular avatars without pasting David Revoy's
name all over the place.

Binary file added catavatar/avatars/accessorie_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_14.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_15.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_17.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_18.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_19.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_20.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/accessorie_9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_10.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_11.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_12.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_13.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added catavatar/avatars/body_14.png
Binary file added catavatar/avatars/body_15.png
Binary file added catavatar/avatars/body_2.png
Binary file added catavatar/avatars/body_3.png
Binary file added catavatar/avatars/body_4.png
Binary file added catavatar/avatars/body_5.png
Binary file added catavatar/avatars/body_6.png
Binary file added catavatar/avatars/body_7.png
Binary file added catavatar/avatars/body_8.png
Binary file added catavatar/avatars/body_9.png
Binary file added catavatar/avatars/eyes_1.png
Binary file added catavatar/avatars/eyes_10.png
Binary file added catavatar/avatars/eyes_11.png
Binary file added catavatar/avatars/eyes_12.png
Binary file added catavatar/avatars/eyes_13.png
Binary file added catavatar/avatars/eyes_14.png
Binary file added catavatar/avatars/eyes_15.png
Binary file added catavatar/avatars/eyes_2.png
Binary file added catavatar/avatars/eyes_3.png
Binary file added catavatar/avatars/eyes_4.png
Binary file added catavatar/avatars/eyes_5.png
Binary file added catavatar/avatars/eyes_6.png
Binary file added catavatar/avatars/eyes_7.png
Binary file added catavatar/avatars/eyes_8.png
Binary file added catavatar/avatars/eyes_9.png
Binary file added catavatar/avatars/fur_1.png
Binary file added catavatar/avatars/fur_10.png
Binary file added catavatar/avatars/fur_2.png
Binary file added catavatar/avatars/fur_3.png
Binary file added catavatar/avatars/fur_4.png
Binary file added catavatar/avatars/fur_5.png
Binary file added catavatar/avatars/fur_6.png
Binary file added catavatar/avatars/fur_7.png
Binary file added catavatar/avatars/fur_8.png
Binary file added catavatar/avatars/fur_9.png
Binary file added catavatar/avatars/mouth_1.png
Binary file added catavatar/avatars/mouth_10.png
Binary file added catavatar/avatars/mouth_2.png
Binary file added catavatar/avatars/mouth_3.png
Binary file added catavatar/avatars/mouth_4.png
Binary file added catavatar/avatars/mouth_5.png
Binary file added catavatar/avatars/mouth_6.png
Binary file added catavatar/avatars/mouth_7.png
Binary file added catavatar/avatars/mouth_8.png
Binary file added catavatar/avatars/mouth_9.png
Binary file added catavatar/avatars/zz_1.png
Binary file added catavatar/avatars/zz_2.png
Binary file added catavatar/avatars/zz_bg.png
Binary file added catavatar/cat_src.ora
Binary file not shown.
306 changes: 306 additions & 0 deletions catavatar/catavatar.php
@@ -0,0 +1,306 @@
<?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>
*/
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;

define("CATAVATAR_SIZE", 256);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.


/**
* Installs the addon hook
*/
function catavatar_install() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

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');

logger("registered catavatar");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

}

/**
* Removes the addon hook
*/
function catavatar_uninstall() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

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');

logger("unregistered catavatar");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

}


function catavatar_addon_settings(&$a, &$s) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use (\Friendica\App $a, &$s) parameter list instead.

if(! local_user())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after conditional operators.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add braces to every conditional statement.

return;

$t = get_markup_template("settings.tpl", "addon/catavatar/" );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please remove the extra space before the closing parenthesis.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

$s = replace_macros ($t, [
'$postpost' => x($_POST,"catavatar-morecat") || x($_POST,"catavatar-emailcat"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete: Please use !empty() instead of x().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

'$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'),
'$seed' => PConfig::get(local_user(), "catavatar", "seed", false),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

'$header' => L10n::t('Cat Avatar').' '.L10n::t('Settings'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

]);
return;
}

function catavatar_addon_settings_post(&$a, &$s) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use (\Friendica\App $a, &$s) parameter list instead.

if(! local_user())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add braces to every conditional statement.

return;

// delete the current cached cat avatar
$user = dba::selectFirst('user', ['email'],
[
'uid' => $uid,
'blocked' => 0,
'account_expired' => 0,
'account_removed' => 0,
]
);
$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email']))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed);
$imageurl = substr($imageurl,0,35).'';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

$cachefile = get_cachefile($imageurl);
if ($cachefile != "" && file_exists($cachefile)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

unlink($cachefile);
}


if (x($_POST,"catavatar-usecat")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete: Please use !empty() instead of x().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

$url = $a->get_baseurl()."/catavatar/".local_user();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.


// set the catavatar url as avatar url in contact and default profile
// and set profile to 0 to current photo
// I'm not sure it's the correct way to do this...
$r = dba::update('contact',
['photo'=>$url."/4", 'thumb'=>$url."/5", 'micro'=>$url."/6", 'avatar-date'=>DateTimeFormat::utcNow()],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

['uid'=>local_user(), 'self'=>1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

);
if ($r===false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

notice(L10n::t('There was an error, the cat ran away.'));
return;
}

$r = dba::update('profile',
['photo'=>$url."/4", 'thumb'=>$url."/5"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

['uid'=>local_user(), 'is-default'=>1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

);
if ($r===false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

notice(L10n::t('There was an error, the cat ran away.'));
return;
}

$r = dba::update('photo',
['profile'=>0],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

['uid'=>local_user(), 'profile'=>1]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

);
if ($r===false) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

notice(L10n::t('There was an error, the cat ran away.'));
return;
}


// Update global directory in background
$url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
if ($url && strlen(Config::get('system','directory'))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

Worker::add(PRIORITY_LOW, "Directory", $url);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

}

Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());

info(L10n::t("Meow!"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

return;
}



if (x($_POST,"catavatar-morecat")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete: Please use !empty() instead of x().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

PConfig::set(local_user(), "catavatar", "seed", time());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

}

if (x($_POST,"catavatar-emailcat")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obsolete: Please use !empty() instead of x().

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

PConfig::delete(local_user(), "catavatar", "seed");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

}
}


/**
* Returns the URL to the cat avatar
*
* @param $a array
* @param &$b array
*/
function catavatar_lookup($a, &$b) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use (\Friendica\App $a, &$b) parameter list instead.

$user = dba::selectFirst('user', ['uid'],['email'=>$b['email']]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.


$url = $a->get_baseurl().'/catavatar/'.$user['uid'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.


switch($b['size']) {
case 175: $url.="/4"; break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

case 80: $url.="/5"; break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

case 47: $url.="/6"; break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

}

$b['url'] = $url;
$b['success'] = true;
}


function catavatar_module(){}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space before opening braces.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as is a empty function, what's the standard here?

function catavatar_module()
{}

// or

function catavatar_module()
{
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go with function catavatar_module() {} as it just is a (wrong) way of indicating the addon provides a module.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Ok.
( in a future hopefully we will see something like

class CatAvatar extends Addon implements Module {

:-D )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather see

class CatAvatar extends Addon
{
    public function __construct(\Friendica\App $a)
    {
        $a->addRoute('catavatar', '\Friendica\Addon\Module\CatAvatar');
        ...
    }
    ...
}

and have the module in a separate file in the addon.



/**
* Returns image for user id
*
* @throws NotFoundException
*
* @TODO: support sizes
*/
function catavatar_content($a) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use (\Friendica\App $a) parameter list instead.

if ($a->argc < 2 || $a->argc > 3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add braces to every conditional statement.

throw new NotFoundException(); // this should be catched on index and show default "not found" page.

$uid = intval($a->argv[1]);

$size = 0;
if ($a->argc == 3) {
$size = intval($a->argv[2]);
}

$user = dba::selectFirst('user', ['email'],
[
'uid' => $uid,
'blocked' => 0,
'account_expired' => 0,
'account_removed' => 0,
]
);

if ($user === False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap conditionals with braces.

throw new NotFoundException();

$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email']))));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

//echo "<pre>"; var_dump($hash); killme();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please remove debug code.



// from cat-avatar-generator.php

$imageurl = $seed."-".$size;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl);
$imageurl = substr($imageurl,0,35).'';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

$cachefile = get_cachefile($imageurl);
$cachetime = 604800; # 1 week (1 day = 86400)

// Serve from the cache if it is younger than $cachetime
if ($cachefile != "" && file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

header('Content-Type: image/jpg');
readfile($cachefile);
exit;
}

// ...Or start generation
ob_start();

// render the picture:
build_cat($seed, $size);

// Save/cache the output to a file
if ($cachefile!=""){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space before conditional opening brace.

$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);
}
ob_end_flush();

killme();
}



/**
* ====================
* CAT-AVATAR-GENERATOR
* ====================
*
* @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_cat($seed='', $size=0){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a new line before function opening braces.


// init random seed
if($seed) srand( hexdec(substr(md5($seed),0,6)) );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap conditionals with braces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please remove extra spaces in srand() argument list.


// throw the dice for body parts
$parts = array(
'body' => rand(1,15),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

'fur' => rand(1,10),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

'eyes' => rand(1,15),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

'mouth' => rand(1,10),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

'accessorie' => rand(1,20)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

);

// create backgound
$cat = @imagecreatetruecolor(CATAVATAR_SIZE, CATAVATAR_SIZE)
or die("GD image create failed");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please use single-quotes or double-quotes but not a mix and match.

$white = imagecolorallocate($cat, 255, 255, 255);
imagefill($cat,0,0,$white);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.


// add parts
foreach($parts as $part => $num){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space before opening braces.

$file = dirname(__FILE__).'/avatars/'.$part.'_'.$num.'.png';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.


$im = @imagecreatefrompng($file);
if(!$im) die('Failed to load '.$file);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap conditionals with braces.

imageSaveAlpha($im, true);
imagecopy($cat,$im,0,0,0,0,CATAVATAR_SIZE,CATAVATAR_SIZE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please add a space after commas.

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;
}

$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;
}

// restore random seed
if($seed) srand();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap conditionals with braces.


header('Pragma: public');
header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standards: Please wrap operators with spaces.

header('Content-Type: image/jpg');
imagejpeg($cat, NULL, 90);
imagedestroy($cat);
}


24 changes: 24 additions & 0 deletions catavatar/templates/settings.tpl
@@ -0,0 +1,24 @@
<span id="settings_catavatar_inflated" class="settings-block fakelink"
style="{{if $postpost}}display: none;{{else}}display: block;{{/if}}"
onclick="openClose('settings_catavatar_expanded'); openClose('settings_catavatar_inflated');">
<h3>{{$header}}</h3>
</span>
<div id="settings_catavatar_expanded" class="settings-block"
style="{{if $postpost}}display: block;{{else}}display: none;{{/if}}">
<span class="fakelink" onclick="openClose('settings_catavatar_expanded'); openClose('settings_catavatar_inflated');">
<h3>{{$header}}</h3>
</span>
<img src="{{$basepath}}/catavatar/{{$uid}}?{{$uncache}}">
<p>Set default profile avatar or randomize the cat</p>
<div class="settings-submit-wrapper" >
<input type="submit" name="catavatar-usecat"
class="btn btn-primary settings-submit" value="{{$usecat}}" />

<div class="btn-group" role="group" aria-label="...">
<input type="submit" name="catavatar-morecat"
class="btn btn-default settings-submit" value="{{$morecat}}" />
<input type="submit" name="catavatar-emailcat" {{if !$seed}}disabled{{/if}}
class="btn btn-default settings-submit" value="{{$emailcat}}" />
</div>
</div>
</div>