* Version: 1.1
* Author: Fabio <https://kirgroup.com/profile/fabrixxm>
*/
+
use Friendica\App;
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\L10n;
+use Friendica\Core\Hook;
+use Friendica\Core\Logger;
+use Friendica\Core\Renderer;
+use Friendica\Core\Session;
use Friendica\Core\Worker;
-use Friendica\Core\PConfig;
-use Friendica\Util\DateTimeFormat;
-use Friendica\Network\HTTPException\NotFoundException;
+use Friendica\Database\DBA;
+use Friendica\DI;
use Friendica\Model\Contact;
use Friendica\Model\Photo;
-use Friendica\Database\DBM;
+use Friendica\Model\Profile;
+use Friendica\Network\HTTPException\NotFoundException;
define("CATAVATAR_SIZE", 256);
*/
function catavatar_install()
{
- 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');
-}
-
-/**
- * Removes the addon hook
- */
-function catavatar_uninstall()
-{
- 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');
+ 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');
- logger('unregistered catavatar');
+ Logger::notice('registered catavatar');
}
/**
* Cat avatar user settings page
*/
-function catavatar_addon_settings(App $a, &$s)
+function catavatar_addon_settings(App $a, array &$data)
{
- if (!local_user()) {
+ if (!Session::getLocalUser()) {
return;
}
- $t = get_markup_template('settings.tpl', 'addon/catavatar/');
- $s .= replace_macros ($t, [
- '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']),
- '$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),
- '$header' => L10n::t('Cat Avatar Settings'),
+ $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/catavatar/');
+ $html = Renderer::replaceMacros($t, [
+ '$uncache' => time(),
+ '$uid' => Session::getLocalUser(),
+ '$setrandomize' => DI::l10n()->t('Set default profile avatar or randomize the cat.'),
]);
+
+ $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(Session::getLocalUser(), 'catavatar', 'seed', false) ? DI::l10n()->t('Reset to email Cat') : null,
+ ],
+ ];
}
/**
*/
function catavatar_addon_settings_post(App $a, &$s)
{
- if (!local_user()) {
+ if (!Session::getLocalUser()) {
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']))));
- $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $seed);
- $imageurl = substr($imageurl, 0, 35) . '';
- $cachefile = get_cachefile($imageurl);
- if ($cachefile != "" && file_exists($cachefile)) {
- unlink($cachefile);
- }
-
-
if (!empty($_POST['catavatar-usecat'])) {
- $url = $a->get_baseurl() . '/catavatar/' . local_user() . '?ts=' . time();
+ $url = DI::baseUrl()->get() . '/catavatar/' . Session::getLocalUser() . '?ts=' . time();
- $self = dba::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]);
- if (!DBM::is_result($self)) {
- notice(L10n::t("The cat hadn't found itself."));
+ $self = DBA::selectFirst('contact', ['id'], ['uid' => Session::getLocalUser(), 'self' => true]);
+ if (!DBA::isResult($self)) {
+ DI::sysmsg()->addNotice(DI::l10n()->t("The cat hadn't found itself."));
return;
}
- Photo::importProfilePhoto($url, local_user(), $self['id']);
+ Photo::importProfilePhoto($url, Session::getLocalUser(), $self['id']);
- $condition = ['uid' => local_user(), 'contact-id' => $self['id']];
- $photo = dba::selectFirst('photo', ['resource-id'], $condition);
- if (!DBM::is_result($photo)) {
- notice(L10n::t('There was an error, the cat ran away.'));
+ $condition = ['uid' => Session::getLocalUser(), 'contact-id' => $self['id']];
+ $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
+ if (!DBA::isResult($photo)) {
+ DI::sysmsg()->addNotice(DI::l10n()->t('There was an error, the cat ran away.'));
return;
}
- dba::update('photo', ['profile' => false], ['profile' => true, 'uid' => local_user()]);
+ DBA::update('photo', ['profile' => false], ['profile' => true, 'uid' => Session::getLocalUser()]);
- $fields = ['profile' => true, 'album' => L10n::t('Profile Photos'), 'contact-id' => 0];
- dba::update('photo', $fields, ['uid' => local_user(), 'resource-id' => $photo['resource-id']]);
+ $fields = ['profile' => true, 'album' => DI::l10n()->t('Profile Photos'), 'contact-id' => 0];
+ DBA::update('photo', $fields, ['uid' => Session::getLocalUser(), 'resource-id' => $photo['resource-id']]);
- Photo::importProfilePhoto($url, local_user(), $self['id']);
+ Photo::importProfilePhoto($url, Session::getLocalUser(), $self['id']);
- Contact::updateSelfFromUserID(local_user(), true);
+ Contact::updateSelfFromUserID(Session::getLocalUser(), true);
// Update global directory in background
- $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
- if ($url && strlen(Config::get('system', 'directory'))) {
- Worker::add(PRIORITY_LOW, 'Directory', $url);
- }
+ Profile::publishUpdate(Session::getLocalUser());
- Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
-
- info(L10n::t('Meow!'));
+ DI::sysmsg()->addInfo(DI::l10n()->t('Meow!'));
return;
}
-
-
if (!empty($_POST['catavatar-morecat'])) {
- PConfig::set(local_user(), 'catavatar', 'seed', time());
+ DI::pConfig()->set(Session::getLocalUser(), 'catavatar', 'seed', time());
}
if (!empty($_POST['catavatar-emailcat'])) {
- PConfig::delete(local_user(), 'catavatar', 'seed');
+ DI::pConfig()->delete(Session::getLocalUser(), 'catavatar', 'seed');
}
}
-
/**
* Returns the URL to the cat avatar
*
* @param $a array
* @param &$b array
*/
-function catavatar_lookup(App $a, &$b)
+function catavatar_lookup(App $a, array &$b)
{
- $user = dba::selectFirst('user', ['uid'], ['email' => $b['email']]);
- $url = $a->get_baseurl() . '/catavatar/' . $user['uid'];
+ $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]);
+ if (DBA::isResult($user)) {
+ $url = DI::baseUrl()->get() . '/catavatar/' . $user['uid'];
+ } else {
+ $url = DI::baseUrl()->get() . '/catavatar/' . md5(trim(strtolower($b['email'])));
+ }
switch($b['size']) {
- case 175: $url .= "/4"; break;
+ case 300: $url .= "/4"; break;
case 80: $url .= "/5"; break;
- case 47: $url .= "/6"; break;
+ case 48: $url .= "/6"; break;
}
$b['url'] = $url;
$b['success'] = true;
}
-
-function catavatar_module(){}
-
+/**
+ * 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 catavatar_module() {}
/**
* Returns image for user id
*/
function catavatar_content(App $a)
{
- if ($a->argc < 2 || $a->argc > 3) {
+ if (DI::args()->getArgc() < 2 || DI::args()->getArgc() > 3) {
throw new NotFoundException(); // this should be catched on index and show default "not found" page.
}
- $uid = intval($a->argv[1]);
+ if (is_numeric(DI::args()->getArgv()[1])) {
+ $uid = intval(DI::args()->getArgv()[1]);
+ $condition = ['uid' => $uid,
+ 'account_expired' => false, 'account_removed' => false];
+ $user = DBA::selectFirst('user', ['email'], $condition);
- $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) {
+ throw new NotFoundException();
+ }
- if ($user === false) {
+ $seed = DI::pConfig()->get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
+ } elseif (!empty(DI::args()->getArgv()[1])) {
+ $seed = DI::args()->getArgv()[1];
+ } else {
throw new NotFoundException();
}
- $seed = PConfig::get($uid, "catavatar", "seed", md5(trim(strtolower($user['email']))));
-
- // from cat-avatar-generator.php
- $imageurl = $seed . "-" . $size;
- $imageurl = preg_replace('/[^A-Za-z0-9\._-]/', '', $imageurl);
- $imageurl = substr($imageurl, 0, 35) . '';
- $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)) {
- 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);
- exit();
+ $size = 0;
+ if (DI::args()->getArgc() == 3) {
+ $size = intval(DI::args()->getArgv()[2]);
}
// ...Or start generation
// render the picture:
build_cat($seed, $size);
- // Save/cache the output to a file
- if ($cachefile != "") {
- $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();
exit();
}
-
-
/**
* ====================
* CAT-AVATAR-GENERATOR
imagefill($cat, 0, 0, $white);
// add parts
- foreach ($parts as $part => $num){
+ foreach ($parts as $part => $num) {
$file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png';
$im = @imagecreatefrompng($file);
if ($size > 3 && $size < 7) {
switch ($size) {
case 4:
- $size = 175;
+ $size = 300;
break;
case 5:
$size = 80;