Ejemplo n.º 1
0
function thing_init(&$a)
{
    if (!local_channel()) {
        return;
    }
    $channel = App::get_channel();
    $term_hash = $_REQUEST['term_hash'] ? $_REQUEST['term_hash'] : '';
    $name = escape_tags($_REQUEST['term']);
    $verb = escape_tags($_REQUEST['verb']);
    $activity = intval($_REQUEST['activity']);
    $profile_guid = escape_tags($_REQUEST['profile_assign']);
    $url = $_REQUEST['link'];
    $photo = $_REQUEST['img'];
    $hash = random_string();
    $verbs = obj_verbs();
    /**
     * verbs: [0] = first person singular, e.g. "I want", [1] = 3rd person singular, e.g. "Bill wants" 
     * We use the first person form when creating an activity, but the third person for use in activities
     * @FIXME There is no accounting for verb gender for languages where this is significant. We may eventually
     * require obj_verbs() to provide full conjugations and specify which form to use in the $_REQUEST params to this module.
     */
    $translated_verb = $verbs[$verb][1];
    /*
     * The site administrator can do things that normals cannot.
     * This is restricted because it will likely cause
     * an activitystreams protocol violation and the activity might
     * choke in some other network and result in unnecessary 
     * support requests. It isn't because we're trying to be heavy-handed
     * about what you can and can't do. 
     */
    if (!$translated_verb) {
        if (is_site_admin()) {
            $translated_verb = $verb;
        }
    }
    /*
     * Things, objects: We do not provide definite (a, an) or indefinite (the) articles or singular/plural designators
     * That needs to be specified in your thing. e.g. Mike has "a carrot", Greg wants "balls", Bob likes "the Boston Red Sox".  
     */
    /*
     * Future work on this module might produce more complex activities with targets, e.g. Phillip likes Karen's moustache
     * and to describe other non-thing objects like channels, such as Karl wants Susan - where Susan represents a channel profile.
     */
    if (!$name || !$translated_verb) {
        return;
    }
    $acl = new Zotlabs\Access\AccessList($channel);
    if (array_key_exists('contact_allow', $_REQUEST) || array_key_exists('group_allow', $_REQUEST) || array_key_exists('contact_deny', $_REQUEST) || array_key_exists('group_deny', $_REQUEST)) {
        $acl->set_from_array($_REQUEST);
    }
    $x = $acl->get();
    if ($term_hash) {
        $t = q("select * from obj where obj_obj = '%s' and obj_channel = %d limit 1", dbesc($term_hash), intval(local_channel()));
        if (!$t) {
            notice(t('Item not found.') . EOL);
            return;
        }
        $orig_record = $t[0];
        if ($photo != $orig_record['obj_imgurl']) {
            $arr = import_xchan_photo($photo, get_observer_hash(), true);
            $local_photo = $arr[0];
            $local_photo_type = $arr[3];
        } else {
            $local_photo = $orig_record['obj_imgurl'];
        }
        $r = q("update obj set obj_term = '%s', obj_url = '%s', obj_imgurl = '%s', obj_edited = '%s', allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where obj_obj = '%s' and obj_channel = %d ", dbesc($name), dbesc($url ? $url : z_root() . '/thing/' . $term_hash), dbesc($local_photo), dbesc(datetime_convert()), dbesc($x['allow_cid']), dbesc($x['allow_gid']), dbesc($x['deny_cid']), dbesc($x['deny_gid']), dbesc($term_hash), intval(local_channel()));
        info(t('Thing updated') . EOL);
        $r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1", intval(local_channel()), dbesc($term_hash));
        if ($r) {
            build_sync_packet(0, array('obj' => $r));
        }
        return;
    }
    $sql = $profile_guid ? " and profile_guid = '" . dbesc($profile_guid) . "' " : " and is_default = 1 ";
    $p = q("select profile_guid, is_default from profile where uid = %d {$sql} limit 1", intval(local_channel()));
    if ($p) {
        $profile = $p[0];
    } else {
        return;
    }
    $local_photo = null;
    if ($photo) {
        $arr = import_xchan_photo($photo, get_observer_hash(), true);
        $local_photo = $arr[0];
        $local_photo_type = $arr[3];
    }
    $created = datetime_convert();
    $url = $url ? $url : z_root() . '/thing/' . $hash;
    $r = q("insert into obj ( obj_page, obj_verb, obj_type, obj_channel, obj_obj, obj_term, obj_url, obj_imgurl, obj_created, obj_edited, allow_cid, allow_gid, deny_cid, deny_gid ) values ('%s','%s', %d, %d, '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s') ", dbesc($profile['profile_guid']), dbesc($verb), intval(TERM_OBJ_THING), intval(local_channel()), dbesc($hash), dbesc($name), dbesc($url), dbesc($photo ? $local_photo : ''), dbesc($created), dbesc($created), dbesc($x['allow_cid']), dbesc($x['allow_gid']), dbesc($x['deny_cid']), dbesc($x['deny_gid']));
    if (!$r) {
        notice(t('Object store: failed'));
        return;
    }
    info(t('Thing added'));
    $r = q("select * from obj where obj_channel = %d and obj_obj = '%s' limit 1", intval(local_channel()), dbesc($hash));
    if ($r) {
        build_sync_packet(0, array('obj' => $r));
    }
    if ($activity) {
        $arr = array();
        $links = array(array('rel' => 'alternate', 'type' => 'text/html', 'href' => $url));
        if ($local_photo) {
            $links[] = array('rel' => 'photo', 'type' => $local_photo_type, 'href' => $local_photo);
        }
        $objtype = ACTIVITY_OBJ_THING;
        $obj = json_encode(array('type' => $objtype, 'id' => $url, 'link' => $links, 'title' => $name, 'content' => $name));
        $bodyverb = str_replace('OBJ: ', '', t('OBJ: %1$s %2$s %3$s'));
        $arr['owner_xchan'] = $channel['channel_hash'];
        $arr['author_xchan'] = $channel['channel_hash'];
        $arr['item_origin'] = 1;
        $arr['item_wall'] = 1;
        $arr['item_thread_top'] = 1;
        $ulink = '[zrl=' . $channel['xchan_url'] . ']' . $channel['channel_name'] . '[/zrl]';
        $plink = '[zrl=' . $url . ']' . $name . '[/zrl]';
        $arr['body'] = sprintf($bodyverb, $ulink, $translated_verb, $plink);
        if ($local_photo) {
            $arr['body'] .= "\n\n[zmg]" . $local_photo . "[/zmg]";
        }
        $arr['verb'] = $verb;
        $arr['obj_type'] = $objtype;
        $arr['object'] = $obj;
        if (!$profile['is_default']) {
            $arr['item_private'] = true;
            $str = '';
            $r = q("select abook_xchan from abook where abook_channel = %d and abook_profile = '%s'", intval(local_channel()), dbesc($profile_guid));
            if ($r) {
                $arr['allow_cid'] = '';
                foreach ($r as $rr) {
                    $arr['allow_cid'] .= '<' . $rr['abook_xchan'] . '>';
                }
            } else {
                $arr['allow_cid'] = '<' . get_observer_hash() . '>';
            }
        }
        $ret = post_activity_item($arr);
    }
}
Ejemplo n.º 2
0
function get_things($profile_hash, $uid)
{
    $sql_extra = $profile_hash ? " and obj_page = '" . $profile_hash . "' " : '';
    $r = q("select * from obj where obj_channel = %d and obj_type = %d {$sql_extra} order by obj_verb, obj_term", intval($uid), intval(TERM_OBJ_THING));
    $things = $sorted_things = null;
    $profile_hashes = array();
    if ($r) {
        // if no profile_hash was specified (display on profile page mode), match each of the things to a profile name
        // (list all my things mode). This is harder than it sounds.
        foreach ($r as $rr) {
            $rr['profile_name'] = '';
            if (!in_array($rr['obj_obj'], $profile_hashes)) {
                $profile_hashes[] = $rr['obj_obj'];
            }
        }
        stringify_array_elms($profile_hashes);
        if (!$profile_hash) {
            $exp = explode(',', $profile_hashes);
            $p = q("select profile_guid as hash, profile_name as name from profile where profile_guid in ( {$exp} ) ");
            if ($p) {
                foreach ($r as $rr) {
                    foreach ($p as $pp) {
                        if ($rr['obj_page'] == $pp['hash']) {
                            $rr['profile_name'] == $pp['name'];
                        }
                    }
                }
            }
        }
        $things = array();
        // Use the system obj_verbs array as a sort key, since we don't really
        // want an alphabetic sort. To change the order, use a plugin to
        // alter the obj_verbs() array or alter it in code. Unknown verbs come
        // after the known ones - in no particular order.
        $v = obj_verbs();
        foreach ($v as $k => $foo) {
            $things[$k] = null;
        }
        foreach ($r as $rr) {
            $l = q("select xchan_name, xchan_url from likes left join xchan on likee = xchan_hash where\n\t\t\t\ttarget_type = '%s' and target_id = '%s' and channel_id = %d", dbesc(ACTIVITY_OBJ_THING), dbesc($rr['obj_obj']), intval($uid));
            for ($x = 0; $x < count($l); $x++) {
                $l[$x]['xchan_url'] = zid($l[$x]['xchan_url']);
            }
            if (!$things[$rr['obj_verb']]) {
                $things[$rr['obj_verb']] = array();
            }
            $things[$rr['obj_verb']][] = array('term' => $rr['obj_term'], 'url' => $rr['obj_url'], 'img' => $rr['obj_imgurl'], 'profile' => $rr['profile_name'], 'term_hash' => $rr['obj_obj'], 'likes' => $l, 'like_count' => count($l), 'like_label' => tt('Like', 'Likes', count($l), 'noun'));
        }
        $sorted_things = array();
        if ($things) {
            foreach ($things as $k => $v) {
                if (is_array($things[$k])) {
                    $sorted_things[$k] = $v;
                }
            }
        }
    }
    //logger('things: ' . print_r($sorted_things,true));
    return $sorted_things;
}