Merge pull request #573 from fabrixxm/catavatar

Cat Avatar Generator: Fix coding style
pull/575/head
Hypolite Petovan 2018-04-06 12:44:09 -04:00 committed by GitHub
commit ee0dcbd9f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 129 additions and 74 deletions

View File

@ -5,6 +5,7 @@
* Version: 1.1 * Version: 1.1
* Author: Fabio <https://kirgroup.com/profile/fabrixxm> * Author: Fabio <https://kirgroup.com/profile/fabrixxm>
*/ */
use Friendica\App;
use Friendica\Core\Addon; use Friendica\Core\Addon;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
@ -18,48 +19,58 @@ define("CATAVATAR_SIZE", 256);
/** /**
* Installs the addon hook * Installs the addon hook
*/ */
function catavatar_install() { function catavatar_install()
{
Addon::registerHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup'); 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', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
Addon::registerHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post'); Addon::registerHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
logger("registered catavatar"); logger('registered catavatar');
} }
/** /**
* Removes the addon hook * Removes the addon hook
*/ */
function catavatar_uninstall() { function catavatar_uninstall()
{
Addon::unregisterHook('avatar_lookup', 'addon/catavatar/catavatar.php', 'catavatar_lookup'); 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', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings');
Addon::unregisterHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post'); Addon::unregisterHook('addon_settings_post', 'addon/catavatar/catavatar.php', 'catavatar_addon_settings_post');
logger("unregistered catavatar"); logger('unregistered catavatar');
} }
/**
function catavatar_addon_settings(&$a, &$s) { * Cat avatar user settings page
if(! local_user()) */
function catavatar_addon_settings(App $a, &$s)
{
if (!local_user()) {
return; return;
}
$t = get_markup_template("settings.tpl", "addon/catavatar/" ); $t = get_markup_template('settings.tpl', 'addon/catavatar/');
$s = replace_macros ($t, [ $s = replace_macros ($t, [
'$postpost' => x($_POST,"catavatar-morecat") || x($_POST,"catavatar-emailcat"), '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
'$uncache' => time(), '$uncache' => time(),
'$uid' => local_user(), '$uid' => local_user(),
'$usecat' => L10n::t('Use Cat as Avatar'), '$usecat' => L10n::t('Use Cat as Avatar'),
'$morecat' => L10n::t('More Random Cat!'), '$morecat' => L10n::t('More Random Cat!'),
'$emailcat' => L10n::t('Reset to email Cat'), '$emailcat' => L10n::t('Reset to email Cat'),
'$seed' => PConfig::get(local_user(), "catavatar", "seed", false), '$seed' => PConfig::get(local_user(), 'catavatar', 'seed', false),
'$header' => L10n::t('Cat Avatar').' '.L10n::t('Settings'), '$header' => L10n::t('Cat Avatar Settings'),
]); ]);
return;
} }
function catavatar_addon_settings_post(&$a, &$s) { /**
if(! local_user()) * Cat avatar user settings POST handle
*/
function catavatar_addon_settings_post(App $a, &$s)
{
if (!local_user()) {
return; return;
}
// delete the current cached cat avatar // delete the current cached cat avatar
$user = dba::selectFirst('user', ['email'], $user = dba::selectFirst('user', ['email'],
[ [
@ -69,44 +80,44 @@ function catavatar_addon_settings_post(&$a, &$s) {
'account_removed' => 0, 'account_removed' => 0,
] ]
); );
$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email'])))); $seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email']))));
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed); $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed);
$imageurl = substr($imageurl,0,35).''; $imageurl = substr($imageurl,0,35).'';
$cachefile = get_cachefile($imageurl); $cachefile = get_cachefile($imageurl);
if ($cachefile != "" && file_exists($cachefile)) { if ($cachefile != "" && file_exists($cachefile)) {
unlink($cachefile); unlink($cachefile);
} }
if (x($_POST,"catavatar-usecat")) { if (!empty($_POST['catavatar-usecat'])) {
$url = $a->get_baseurl()."/catavatar/".local_user(); $url = $a->get_baseurl() . '/catavatar/' . local_user();
// set the catavatar url as avatar url in contact and default profile // set the catavatar url as avatar url in contact and default profile
// and set profile to 0 to current photo // and set profile to 0 to current photo
// I'm not sure it's the correct way to do this... // I'm not sure it's the correct way to do this...
$r = dba::update('contact', $r = dba::update('contact',
['photo'=>$url."/4", 'thumb'=>$url."/5", 'micro'=>$url."/6", 'avatar-date'=>DateTimeFormat::utcNow()], ['photo' => $url . '/4', 'thumb' => $url . '/5', 'micro' => $url . '/6', 'avatar-date' => DateTimeFormat::utcNow()],
['uid'=>local_user(), 'self'=>1] ['uid' => local_user(), 'self' => 1]
); );
if ($r===false) { if ($r===false) {
notice(L10n::t('There was an error, the cat ran away.')); notice(L10n::t('There was an error, the cat ran away.'));
return; return;
} }
$r = dba::update('profile', $r = dba::update('profile',
['photo'=>$url."/4", 'thumb'=>$url."/5"], ['photo' => $url . '/4', 'thumb' => $url . '/5'],
['uid'=>local_user(), 'is-default'=>1] ['uid' => local_user(), 'is-default' => 1]
); );
if ($r===false) { if ($r===false) {
notice(L10n::t('There was an error, the cat ran away.')); notice(L10n::t('There was an error, the cat ran away.'));
return; return;
} }
$r = dba::update('photo', $r = dba::update('photo',
['profile'=>0], ['profile' => 0],
['uid'=>local_user(), 'profile'=>1] ['uid' => local_user(), 'profile' => 1]
); );
if ($r===false) { if ($r === false) {
notice(L10n::t('There was an error, the cat ran away.')); notice(L10n::t('There was an error, the cat ran away.'));
return; return;
} }
@ -115,23 +126,23 @@ function catavatar_addon_settings_post(&$a, &$s) {
// Update global directory in background // Update global directory in background
$url = $a->get_baseurl() . '/profile/' . $a->user['nickname']; $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
if ($url && strlen(Config::get('system','directory'))) { if ($url && strlen(Config::get('system','directory'))) {
Worker::add(PRIORITY_LOW, "Directory", $url); Worker::add(PRIORITY_LOW, 'Directory', $url);
} }
Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user()); Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
info(L10n::t("Meow!")); info(L10n::t('Meow!'));
return; return;
} }
if (x($_POST,"catavatar-morecat")) {
PConfig::set(local_user(), "catavatar", "seed", time()); if (!empty($_POST['catavatar-morecat'])) {
PConfig::set(local_user(), 'catavatar', 'seed', time());
} }
if (x($_POST,"catavatar-emailcat")) { if (!empty($_POST['catavatar-emailcat'])) {
PConfig::delete(local_user(), "catavatar", "seed"); PConfig::delete(local_user(), 'catavatar', 'seed');
} }
} }
@ -142,15 +153,15 @@ function catavatar_addon_settings_post(&$a, &$s) {
* @param $a array * @param $a array
* @param &$b array * @param &$b array
*/ */
function catavatar_lookup($a, &$b) { function catavatar_lookup(App $a, &$b)
$user = dba::selectFirst('user', ['uid'],['email'=>$b['email']]); {
$user = dba::selectFirst('user', ['uid'], ['email' => $b['email']]);
$url = $a->get_baseurl().'/catavatar/'.$user['uid']; $url = $a->get_baseurl() . '/catavatar/' . $user['uid'];
switch($b['size']) { switch($b['size']) {
case 175: $url.="/4"; break; case 175: $url .= "/4"; break;
case 80: $url.="/5"; break; case 80: $url .= "/5"; break;
case 47: $url.="/6"; break; case 47: $url .= "/6"; break;
} }
$b['url'] = $url; $b['url'] = $url;
@ -166,19 +177,20 @@ function catavatar_module(){}
* *
* @throws NotFoundException * @throws NotFoundException
* *
* @TODO: support sizes
*/ */
function catavatar_content($a) { function catavatar_content(App $a)
if ($a->argc < 2 || $a->argc > 3) {
if ($a->argc < 2 || $a->argc > 3) {
throw new NotFoundException(); // this should be catched on index and show default "not found" page. throw new NotFoundException(); // this should be catched on index and show default "not found" page.
}
$uid = intval($a->argv[1]); $uid = intval($a->argv[1]);
$size = 0; $size = 0;
if ($a->argc == 3) { if ($a->argc == 3) {
$size = intval($a->argv[2]); $size = intval($a->argv[2]);
} }
$user = dba::selectFirst('user', ['email'], $user = dba::selectFirst('user', ['email'],
[ [
'uid' => $uid, 'uid' => $uid,
@ -186,49 +198,48 @@ function catavatar_content($a) {
'account_expired' => 0, 'account_expired' => 0,
'account_removed' => 0, 'account_removed' => 0,
] ]
); );
if ($user === False) if ($user === false) {
throw new NotFoundException(); throw new NotFoundException();
}
$seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email'])))); $seed = PConfig::get(local_user(), "catavatar", "seed", md5(trim(strtolower($user['email']))));
//echo "<pre>"; var_dump($hash); killme();
// from cat-avatar-generator.php // from cat-avatar-generator.php
$imageurl = $seed . "-" . $size;
$imageurl = $seed."-".$size; $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl);
$imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl); $imageurl = substr($imageurl,0,35) . '';
$imageurl = substr($imageurl,0,35).'';
$cachefile = get_cachefile($imageurl); $cachefile = get_cachefile($imageurl);
$cachetime = 604800; # 1 week (1 day = 86400) $cachetime = 604800; # 1 week (1 day = 86400)
// Serve from the cache if it is younger than $cachetime // Serve from the cache if it is younger than $cachetime
if ($cachefile != "" && file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { if ($cachefile != "" && file_exists($cachefile) && (time() - $cachetime) < filemtime($cachefile)) {
header('Pragma: public'); header('Pragma: public');
header('Cache-Control: max-age=86400'); header('Cache-Control: max-age=86400');
header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400)); header('Expires: '. gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
header('Content-Type: image/jpg'); header('Content-Type: image/jpg');
readfile($cachefile); readfile($cachefile);
exit; exit();
} }
// ...Or start generation // ...Or start generation
ob_start(); ob_start();
// render the picture: // render the picture:
build_cat($seed, $size); build_cat($seed, $size);
// Save/cache the output to a file // Save/cache the output to a file
if ($cachefile!=""){ if ($cachefile != "") {
$savedfile = fopen($cachefile, 'w+'); # w+ to be at start of the file, write mode, and attempt to create if not existing. $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()); fwrite($savedfile, ob_get_contents());
fclose($savedfile); fclose($savedfile);
chmod($cachefile, 0755); chmod($cachefile, 0755);
} }
ob_end_flush(); ob_end_flush();
killme(); exit();
} }
@ -237,12 +248,12 @@ function catavatar_content($a) {
* ==================== * ====================
* CAT-AVATAR-GENERATOR * CAT-AVATAR-GENERATOR
* ==================== * ====================
* *
* @authors: Andreas Gohr, David Revoy * @authors: Andreas Gohr, David Revoy
* *
* This PHP is licensed under the short and simple permissive: * This PHP is licensed under the short and simple permissive:
* [MIT License](https://en.wikipedia.org/wiki/MIT_License) * [MIT License](https://en.wikipedia.org/wiki/MIT_License)
* *
**/ **/
function build_cat($seed='', $size=0){ function build_cat($seed='', $size=0){
@ -283,7 +294,7 @@ function build_cat($seed='', $size=0){
case 5: $size = 80; break; case 5: $size = 80; break;
case 6: $size = 48; break; case 6: $size = 48; break;
} }
$dest = imagecreatetruecolor($size, $size); $dest = imagecreatetruecolor($size, $size);
imagealphablending($dest, false); imagealphablending($dest, false);
imagesavealpha($dest, true); imagesavealpha($dest, true);
@ -291,9 +302,11 @@ function build_cat($seed='', $size=0){
imagedestroy($cat); imagedestroy($cat);
$cat = $dest; $cat = $dest;
} }
// restore random seed // restore random seed
if($seed) srand(); if ($seed) {
srand();
}
header('Pragma: public'); header('Pragma: public');
header('Cache-Control: max-age=86400'); header('Cache-Control: max-age=86400');

View File

@ -0,0 +1,42 @@
# ADDON catavatar
# Copyright (C)
# This file is distributed under the same license as the Friendica catavatar addon package.
#
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-04-06 18:26+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
#: catavatar.php:57
msgid "Use Cat as Avatar"
msgstr ""
#: catavatar.php:58
msgid "More Random Cat!"
msgstr ""
#: catavatar.php:59
msgid "Reset to email Cat"
msgstr ""
#: catavatar.php:61
msgid "Cat Avatar Settings"
msgstr ""
#: catavatar.php:103 catavatar.php:112 catavatar.php:121
msgid "There was an error, the cat ran away."
msgstr ""
#: catavatar.php:134
msgid "Meow!"
msgstr ""