function openclipatar_content(&$a)
{
    if (!local_channel()) {
        return;
    }
    $o = '';
    if (argc() == 3 && argv(1) == 'use') {
        $id = argv(2);
        $chan = $a->get_channel();
        $x = z_fetch_url('https://openclipart.org/image/250px/svg_to_png/' . $id . '/' . $id . '.png', true);
        if ($x['success']) {
            $imagedata = $x['body'];
        }
        $ph = photo_factory($imagedata, 'image/png');
        if (!$ph->is_valid()) {
            return t('Unknown error. Please try again later.');
        }
        // create a unique resource_id
        $hash = photo_new_resource();
        // save an original or "scale 0" image
        $p = array('aid' => get_account_id(), 'uid' => local_channel(), 'resource_id' => $hash, 'filename' => $id . '.png', 'album' => t('Profile Photos'), 'scale' => 0);
        $r = $ph->save($p);
        if ($r) {
            // scale 0 success, continue 4, 5, 6
            // we'll skip scales 1,2 (640, 320 rectangular formats as these images are all less than this)
            // ensure squareness at first, subsequent scales keep ratio
            $ph->scaleImageSquare(175);
            $p['scale'] = 4;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
            $ph->scaleImage(80);
            $p['scale'] = 5;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
            $ph->scaleImage(48);
            $p['scale'] = 6;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
        }
        $is_default_profile = 1;
        if ($_REQUEST['profile']) {
            $r = q("select id, is_default from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']), intval(local_channel()));
            if ($r && !intval($r[0]['is_default'])) {
                $is_default_profile = 0;
            }
        }
        if ($is_default_profile) {
            // unset any existing profile photos
            $r = q("UPDATE photo SET profile = 0 WHERE profile = 1 AND uid = %d", intval(local_channel()));
            $r = q("UPDATE photo SET photo_flags = (photo_flags & ~%d ) WHERE (photo_flags & %d )>0 AND uid = %d", intval(PHOTO_PROFILE), intval(PHOTO_PROFILE), intval(local_channel()));
            // set all sizes of this one as profile photos
            $r = q("UPDATE photo SET profile = 1 WHERE uid = %d AND resource_id = '%s'", intval(local_channel()), dbesc($hash));
            $r = q("UPDATE photo SET photo_flags = ( photo_flags | %d ) WHERE uid = %d AND resource_id = '%s'", intval(PHOTO_PROFILE), intval(local_channel()), dbesc($hash));
            require_once 'mod/profile_photo.php';
            profile_photo_set_profile_perms();
            //Reset default profile photo permissions to public
            // only the default needs reload since it uses canonical url -- despite the slightly ambiguous message, left it so as to re-use translations
            info(t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
        } else {
            // not the default profile, set the path in the correct entry in the profile DB
            $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d", dbesc(get_app()->get_baseurl() . '/photo/' . $hash . '-4'), dbesc(get_app()->get_baseurl() . '/photo/' . $hash . '-5'), intval($_REQUEST['profile']), intval(local_channel()));
            info(t('Profile photo updated successfully.') . EOL);
        }
        // set a new photo_date on our xchan so that we can tell everybody to update their cached copy
        $r = q("UPDATE xchan set xchan_photo_date = '%s' where xchan_hash = '%s'", dbesc(datetime_convert()), dbesc($chan['xchan_hash']));
        // tell everybody
        proc_run('php', 'include/directory.php', local_channel());
        $returnafter = get_config('openclipatar', 'returnafter');
        $returnafter_urls = array(0 => $a->get_baseurl() . '/profile/' . ($_REQUEST['profile'] ? $_REQUEST['profile'] . '/view' : $chan['channel_address']), 1 => $a->get_baseurl() . '/profiles/' . ($_REQUEST['profile'] ? $_REQUEST['profile'] : $a->profile_uid), 2 => $a->get_baseurl() . '/profiles');
        goaway($returnafter_urls[$returnafter]);
    } else {
        //invoked as module, we place in content pane the same as we would for the end of the profile photo page. Also handles json for endless scroll for either invokation.
        openclipatar_profile_photo_content_end($a, $o);
    }
    return $o;
}
예제 #2
0
function openclipatar_content(&$a)
{
    if (!local_channel()) {
        return;
    }
    $o = '';
    if (argc() == 3 && argv(1) == 'use') {
        $id = argv(2);
        $chan = App::get_channel();
        $x = z_fetch_url('https://openclipart.org/image/250px/svg_to_png/' . $id . '/' . $id . '.png', true);
        if ($x['success']) {
            $imagedata = $x['body'];
        }
        $ph = photo_factory($imagedata, 'image/png');
        if (!$ph->is_valid()) {
            return t('Unknown error. Please try again later.');
        }
        // create a unique resource_id
        $hash = photo_new_resource();
        $width = $ph->getWidth();
        $height = $ph->getHeight();
        // save an original or "scale 0" image
        $p = array('aid' => get_account_id(), 'uid' => local_channel(), 'resource_id' => $hash, 'filename' => $id . '.png', 'album' => t('Profile Photos'), 'imgscale' => 0);
        $r = $ph->save($p);
        if ($r) {
            if (($width > 1024 || $height > 1024) && !$errors) {
                $ph->scaleImage(1024);
            }
            $p['imgscale'] = 1;
            $r1 = $ph->save($p);
            if (($width > 640 || $height > 640) && !$errors) {
                $ph->scaleImage(640);
            }
            $p['imgscale'] = 2;
            $r2 = $ph->save($p);
            if (($width > 320 || $height > 320) && !$errors) {
                $ph->scaleImage(320);
            }
            $p['imgscale'] = 3;
            $r3 = $ph->save($p);
            // ensure squareness at first, subsequent scales keep ratio
            $ph->scaleImageSquare(175);
            $p['imgscale'] = 4;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
            $ph->scaleImage(80);
            $p['imgscale'] = 5;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
            $ph->scaleImage(48);
            $p['imgscale'] = 6;
            $r = $ph->save($p);
            if ($r === false) {
                $photo_failure = true;
            }
        }
        $is_default_profile = 1;
        if ($_REQUEST['profile']) {
            $r = q("select id, is_default from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']), intval(local_channel()));
            if ($r && !intval($r[0]['is_default'])) {
                $is_default_profile = 0;
            }
        }
        if ($is_default_profile) {
            // unset any existing profile photos
            $r = q("UPDATE photo SET photo_usage = %d WHERE photo_usage = %d AND uid = %d", intval(PHOTO_NORMAL), intval(PHOTO_PROFILE), intval(local_channel()));
            // set all sizes of this one as profile photos
            $r = q("UPDATE photo SET photo_usage = %d WHERE uid = %d AND resource_id = '%s'", intval(PHOTO_PROFILE), intval(local_channel()), dbesc($hash));
            require_once 'include/photos.php';
            profile_photo_set_profile_perms(local_channel());
            //Reset default profile photo permissions to public
            // only the default needs reload since it uses canonical url -- despite the slightly ambiguous message, left it so as to re-use translations
            info(t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
        } else {
            // not the default profile, set the path in the correct entry in the profile DB
            $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d", dbesc(z_root() . '/photo/' . $hash . '-4'), dbesc(z_root() . '/photo/' . $hash . '-5'), intval($_REQUEST['profile']), intval(local_channel()));
            info(t('Profile photo updated successfully.') . EOL);
        }
        // set a new photo_date on our xchan so that we can tell everybody to update their cached copy
        $r = q("UPDATE xchan set xchan_photo_date = '%s' where xchan_hash = '%s'", dbesc(datetime_convert()), dbesc($chan['xchan_hash']));
        // Similarly, tell the nav bar to bypass the cache and update the avater image.
        $_SESSION['reload_avatar'] = true;
        // tell everybody
        Zotlabs\Daemon\Master::Summon(array('Directory', local_channel()));
        $returnafter = get_config('openclipatar', 'returnafter');
        $returnafter_urls = array(0 => z_root() . '/profile/' . ($_REQUEST['profile'] ? $_REQUEST['profile'] . '/view' : $chan['channel_address']), 1 => z_root() . '/profiles/' . ($_REQUEST['profile'] ? $_REQUEST['profile'] : App::$profile_uid), 2 => z_root() . '/profiles');
        goaway($returnafter_urls[$returnafter]);
    } else {
        //invoked as module, we place in content pane the same as we would for the end of the profile photo page. Also handles json for endless scroll for either invokation.
        openclipatar_profile_photo_content_end($a, $o);
    }
    return $o;
}