X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=catavatar%2Fcatavatar.php;h=27e48b7af3c2fdb18b7a61af4d6a0cd79b55de80;hb=6bc8103ca2b341b4a7d15996a64502ac00d5c626;hp=3a29cc69c3786a5b65c735f750806229fec521fe;hpb=ee0dcbd9f603c3278dc83cb1b64b3f13073f4690;p=friendica-addons.git diff --git a/catavatar/catavatar.php b/catavatar/catavatar.php index 3a29cc69..27e48b7a 100644 --- a/catavatar/catavatar.php +++ b/catavatar/catavatar.php @@ -5,13 +5,16 @@ * Version: 1.1 * Author: Fabio */ + use Friendica\App; 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\Core\Worker; +use Friendica\Database\DBA; +use Friendica\Model\Contact; +use Friendica\Model\Photo; use Friendica\Network\HTTPException\NotFoundException; define("CATAVATAR_SIZE", 256); @@ -50,7 +53,7 @@ function catavatar_addon_settings(App $a, &$s) } $t = get_markup_template('settings.tpl', 'addon/catavatar/'); - $s = replace_macros ($t, [ + $s .= replace_macros ($t, [ '$postpost' => !empty($_POST['catavatar-morecat']) || !empty($_POST['catavatar-emailcat']), '$uncache' => time(), '$uid' => local_user(), @@ -72,60 +75,42 @@ function catavatar_addon_settings_post(App $a, &$s) } // 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); - } + $condition = ['uid' => local_user(), 'blocked' => false, + 'account_expired' => false, 'account_removed' => false]; + $user = DBA::selectFirst('user', ['email'], $condition); + $seed = PConfig::get(local_user(), 'catavatar', 'seed', md5(trim(strtolower($user['email'])))); if (!empty($_POST['catavatar-usecat'])) { - $url = $a->get_baseurl() . '/catavatar/' . local_user(); - - // 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()], - ['uid' => local_user(), 'self' => 1] - ); - if ($r===false) { - notice(L10n::t('There was an error, the cat ran away.')); - return; - } + $url = $a->getBaseURL() . '/catavatar/' . local_user() . '?ts=' . time(); - $r = dba::update('profile', - ['photo' => $url . '/4', 'thumb' => $url . '/5'], - ['uid' => local_user(), 'is-default' => 1] - ); - if ($r===false) { - notice(L10n::t('There was an error, the cat ran away.')); + $self = DBA::selectFirst('contact', ['id'], ['uid' => local_user(), 'self' => true]); + if (!DBA::isResult($self)) { + notice(L10n::t("The cat hadn't found itself.")); return; } - $r = dba::update('photo', - ['profile' => 0], - ['uid' => local_user(), 'profile' => 1] - ); - if ($r === false) { + Photo::importProfilePhoto($url, local_user(), $self['id']); + + $condition = ['uid' => local_user(), 'contact-id' => $self['id']]; + $photo = DBA::selectFirst('photo', ['resource-id'], $condition); + if (!DBA::isResult($photo)) { notice(L10n::t('There was an error, the cat ran away.')); return; } + 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); // Update global directory in background - $url = $a->get_baseurl() . '/profile/' . $a->user['nickname']; - if ($url && strlen(Config::get('system','directory'))) { + $url = $a->getBaseURL() . '/profile/' . $a->user['nickname']; + if ($url && strlen(Config::get('system', 'directory'))) { Worker::add(PRIORITY_LOW, 'Directory', $url); } @@ -135,8 +120,6 @@ function catavatar_addon_settings_post(App $a, &$s) return; } - - if (!empty($_POST['catavatar-morecat'])) { PConfig::set(local_user(), 'catavatar', 'seed', time()); } @@ -146,7 +129,6 @@ function catavatar_addon_settings_post(App $a, &$s) } } - /** * Returns the URL to the cat avatar * @@ -155,8 +137,8 @@ function catavatar_addon_settings_post(App $a, &$s) */ function catavatar_lookup(App $a, &$b) { - $user = dba::selectFirst('user', ['uid'], ['email' => $b['email']]); - $url = $a->get_baseurl() . '/catavatar/' . $user['uid']; + $user = DBA::selectFirst('user', ['uid'], ['email' => $b['email']]); + $url = $a->getBaseURL() . '/catavatar/' . $user['uid']; switch($b['size']) { case 175: $url .= "/4"; break; @@ -168,9 +150,7 @@ function catavatar_lookup(App $a, &$b) $b['success'] = true; } - -function catavatar_module(){} - +function catavatar_module() {} /** * Returns image for user id @@ -191,37 +171,15 @@ function catavatar_content(App $a) $size = intval($a->argv[2]); } - $user = dba::selectFirst('user', ['email'], - [ - 'uid' => $uid, - 'blocked' => 0, - 'account_expired' => 0, - 'account_removed' => 0, - ] - ); + $condition = ['uid' => $uid, + 'account_expired' => false, 'account_removed' => false]; + $user = DBA::selectFirst('user', ['email'], $condition); if ($user === false) { throw new NotFoundException(); } - $seed = PConfig::get(local_user(), "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(); - } + $seed = PConfig::get($uid, "catavatar", "seed", md5(trim(strtolower($user['email'])))); // ...Or start generation ob_start(); @@ -229,14 +187,6 @@ function catavatar_content(App $a) // 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(); @@ -256,43 +206,53 @@ function catavatar_content(App $a) * **/ -function build_cat($seed='', $size=0){ - +function build_cat($seed = '', $size = 0) +{ // init random seed - if($seed) srand( hexdec(substr(md5($seed),0,6)) ); + 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) + '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); + imagefill($cat, 0, 0, $white); // add parts - foreach($parts as $part => $num){ - $file = dirname(__FILE__).'/avatars/'.$part.'_'.$num.'.png'; + foreach ($parts as $part => $num) { + $file = dirname(__FILE__) . '/avatars/' . $part . '_' . $num . '.png'; $im = @imagecreatefrompng($file); - if(!$im) die('Failed to load '.$file); + if (!$im) { + die('Failed to load ' . $file); + } imageSaveAlpha($im, true); - imagecopy($cat,$im,0,0,0,0,CATAVATAR_SIZE,CATAVATAR_SIZE); + 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; + switch ($size) { + case 4: + $size = 175; + break; + case 5: + $size = 80; + break; + case 6: + $size = 48; + break; } $dest = imagecreatetruecolor($size, $size); @@ -310,10 +270,8 @@ function build_cat($seed='', $size=0){ 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('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); } - -