Example #1
0
function local_delivery($importer, $data)
{
    $a = get_app();
    logger(__FUNCTION__, LOGGER_TRACE);
    if ($importer['readonly']) {
        // We aren't receiving stuff from this person. But we will quietly ignore them
        // rather than a blatant "go away" message.
        logger('local_delivery: ignoring');
        return 0;
        //NOTREACHED
    }
    // Consume notification feed. This may differ from consuming a public feed in several ways
    // - might contain email or friend suggestions
    // - might contain remote followup to our message
    //		- in which case we need to accept it and then notify other conversants
    // - we may need to send various email notifications
    $feed = new SimplePie();
    $feed->set_raw_data($data);
    $feed->enable_order_by_date(false);
    $feed->init();
    if ($feed->error()) {
        logger('local_delivery: Error parsing XML: ' . $feed->error());
    }
    // Check at the feed level for updated contact name and/or photo
    $name_updated = '';
    $new_name = '';
    $photo_timestamp = '';
    $photo_url = '';
    $contact_updated = '';
    $rawtags = $feed->get_feed_tags(NAMESPACE_DFRN, 'owner');
    // Fallback should not be needed here. If it isn't DFRN it won't have DFRN updated tags
    //	if(! $rawtags)
    //		$rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
    if ($rawtags) {
        $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
        if ($elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
            $name_updated = $elems['name'][0]['attribs'][NAMESPACE_DFRN]['updated'];
            $new_name = $elems['name'][0]['data'];
            // Manually checking for changed contact names
            if ($new_name != $importer['name'] and $new_name != "" and $name_updated <= $importer['name-date']) {
                $name_updated = date("c");
                $photo_timestamp = date("c");
            }
        }
        if (x($elems, 'link') && $elems['link'][0]['attribs']['']['rel'] === 'photo' && $elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']) {
            if ($photo_timestamp == "") {
                $photo_timestamp = datetime_convert('UTC', 'UTC', $elems['link'][0]['attribs'][NAMESPACE_DFRN]['updated']);
            }
            $photo_url = $elems['link'][0]['attribs']['']['href'];
        }
    }
    if ($photo_timestamp && strlen($photo_url) && $photo_timestamp > $importer['avatar-date']) {
        $contact_updated = $photo_timestamp;
        logger('local_delivery: Updating photo for ' . $importer['name']);
        require_once "include/Photo.php";
        $photos = import_profile_photo($photo_url, $importer['importer_uid'], $importer['id']);
        q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s'\n\t\t\tWHERE `uid` = %d AND `id` = %d AND NOT `self`", dbesc(datetime_convert()), dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), intval($importer['importer_uid']), intval($importer['id']));
    }
    if ($name_updated && strlen($new_name) && $name_updated > $importer['name-date']) {
        if ($name_updated > $contact_updated) {
            $contact_updated = $name_updated;
        }
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1", intval($importer['importer_uid']), intval($importer['id']));
        $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s' AND NOT `self`", dbesc(notags(trim($new_name))), dbesc(datetime_convert()), intval($importer['importer_uid']), intval($importer['id']), dbesc(notags(trim($new_name))));
        // do our best to update the name on content items
        if (count($r) and notags(trim($new_name)) != $r[0]['name']) {
            q("UPDATE `item` SET `author-name` = '%s' WHERE `author-name` = '%s' AND `author-link` = '%s' AND `uid` = %d AND `author-name` != '%s'", dbesc(notags(trim($new_name))), dbesc($r[0]['name']), dbesc($r[0]['url']), intval($importer['importer_uid']), dbesc(notags(trim($new_name))));
        }
    }
    if ($contact_updated and $new_name and $photo_url) {
        poco_check($importer['url'], $new_name, NETWORK_DFRN, $photo_url, "", "", "", "", "", $contact_updated, 2, $importer['id'], $importer['importer_uid']);
    }
    // Currently unsupported - needs a lot of work
    $reloc = $feed->get_feed_tags(NAMESPACE_DFRN, 'relocate');
    if (isset($reloc[0]['child'][NAMESPACE_DFRN])) {
        $base = $reloc[0]['child'][NAMESPACE_DFRN];
        $newloc = array();
        $newloc['uid'] = $importer['importer_uid'];
        $newloc['cid'] = $importer['id'];
        $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
        $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
        $newloc['thumb'] = notags(unxmlify($base['thumb'][0]['data']));
        $newloc['micro'] = notags(unxmlify($base['micro'][0]['data']));
        $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
        $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
        $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
        $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
        $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
        $newloc['sitepubkey'] = notags(unxmlify($base['sitepubkey'][0]['data']));
        /** relocated user must have original key pair */
        /*$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
        		$newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));*/
        logger("items:relocate contact " . print_r($newloc, true) . print_r($importer, true), LOGGER_DEBUG);
        // update contact
        $r = q("SELECT photo, url FROM contact WHERE id=%d AND uid=%d;", intval($importer['id']), intval($importer['importer_uid']));
        if ($r === false) {
            return 1;
        }
        $old = $r[0];
        $x = q("UPDATE contact SET\n\t\t\t\t\tname = '%s',\n\t\t\t\t\tphoto = '%s',\n\t\t\t\t\tthumb = '%s',\n\t\t\t\t\tmicro = '%s',\n\t\t\t\t\turl = '%s',\n\t\t\t\t\tnurl = '%s',\n\t\t\t\t\trequest = '%s',\n\t\t\t\t\tconfirm = '%s',\n\t\t\t\t\tnotify = '%s',\n\t\t\t\t\tpoll = '%s',\n\t\t\t\t\t`site-pubkey` = '%s'\n\t\t\tWHERE id=%d AND uid=%d;", dbesc($newloc['name']), dbesc($newloc['photo']), dbesc($newloc['thumb']), dbesc($newloc['micro']), dbesc($newloc['url']), dbesc(normalise_link($newloc['url'])), dbesc($newloc['request']), dbesc($newloc['confirm']), dbesc($newloc['notify']), dbesc($newloc['poll']), dbesc($newloc['sitepubkey']), intval($importer['id']), intval($importer['importer_uid']));
        if ($x === false) {
            return 1;
        }
        // update items
        $fields = array('owner-link' => array($old['url'], $newloc['url']), 'author-link' => array($old['url'], $newloc['url']), 'owner-avatar' => array($old['photo'], $newloc['photo']), 'author-avatar' => array($old['photo'], $newloc['photo']));
        foreach ($fields as $n => $f) {
            $x = q("UPDATE `item` SET `%s`='%s' WHERE `%s`='%s' AND uid=%d", $n, dbesc($f[1]), $n, dbesc($f[0]), intval($importer['importer_uid']));
            if ($x === false) {
                return 1;
            }
        }
        // TODO
        // merge with current record, current contents have priority
        // update record, set url-updated
        // update profile photos
        // schedule a scan?
        return 0;
    }
    // handle friend suggestion notification
    $sugg = $feed->get_feed_tags(NAMESPACE_DFRN, 'suggest');
    if (isset($sugg[0]['child'][NAMESPACE_DFRN])) {
        $base = $sugg[0]['child'][NAMESPACE_DFRN];
        $fsugg = array();
        $fsugg['uid'] = $importer['importer_uid'];
        $fsugg['cid'] = $importer['id'];
        $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
        $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
        $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
        $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
        $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
        // Does our member already have a friend matching this description?
        $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1", dbesc($fsugg['name']), dbesc(normalise_link($fsugg['url'])), intval($fsugg['uid']));
        if (count($r)) {
            return 0;
        }
        // Do we already have an fcontact record for this person?
        $fid = 0;
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
            // OK, we do. Do we already have an introduction for this person ?
            $r = q("select id from intro where uid = %d and fid = %d limit 1", intval($fsugg['uid']), intval($fid));
            if (count($r)) {
                return 0;
            }
        }
        if (!$fid) {
            $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ", dbesc($fsugg['name']), dbesc($fsugg['url']), dbesc($fsugg['photo']), dbesc($fsugg['request']));
        }
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
        } else {
            return 0;
        }
        $hash = random_string();
        $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )\n\t\t\tVALUES( %d, %d, %d, '%s', '%s', '%s', %d )", intval($fsugg['uid']), intval($fid), intval($fsugg['cid']), dbesc($fsugg['body']), dbesc($hash), dbesc(datetime_convert()), intval(0));
        notification(array('type' => NOTIFY_SUGGEST, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $fsugg, 'link' => $a->get_baseurl() . '/notifications/intros', 'source_name' => $importer['name'], 'source_link' => $importer['url'], 'source_photo' => $importer['photo'], 'verb' => ACTIVITY_REQ_FRIEND, 'otype' => 'intro'));
        return 0;
    }
    $ismail = false;
    $rawmail = $feed->get_feed_tags(NAMESPACE_DFRN, 'mail');
    if (isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
        logger('local_delivery: private message received');
        $ismail = true;
        $base = $rawmail[0]['child'][NAMESPACE_DFRN];
        $msg = array();
        $msg['uid'] = $importer['importer_uid'];
        $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
        $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
        $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
        $msg['contact-id'] = $importer['id'];
        $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
        $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
        $msg['seen'] = 0;
        $msg['replied'] = 0;
        $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
        $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
        $msg['created'] = datetime_convert(notags(unxmlify('UTC', 'UTC', $base['sentdate'][0]['data'])));
        dbesc_array($msg);
        $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) . "`) VALUES ('" . implode("', '", array_values($msg)) . "')");
        // send notifications.
        require_once 'include/enotify.php';
        $notif_params = array('type' => NOTIFY_MAIL, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $msg, 'source_name' => $msg['from-name'], 'source_link' => $importer['url'], 'source_photo' => $importer['thumb'], 'verb' => ACTIVITY_POST, 'otype' => 'mail');
        notification($notif_params);
        return 0;
        // NOTREACHED
    }
    $community_page = 0;
    $rawtags = $feed->get_feed_tags(NAMESPACE_DFRN, 'community');
    if ($rawtags) {
        $community_page = intval($rawtags[0]['data']);
    }
    if (intval($importer['forum']) != $community_page) {
        q("update contact set forum = %d where id = %d", intval($community_page), intval($importer['id']));
        $importer['forum'] = (string) $community_page;
    }
    logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
    // process any deleted entries
    $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
    if (is_array($del_entries) && count($del_entries)) {
        foreach ($del_entries as $dentry) {
            $deleted = false;
            if (isset($dentry['attribs']['']['ref'])) {
                $uri = $dentry['attribs']['']['ref'];
                $deleted = true;
                if (isset($dentry['attribs']['']['when'])) {
                    $when = $dentry['attribs']['']['when'];
                    $when = datetime_convert('UTC', 'UTC', $when, 'Y-m-d H:i:s');
                } else {
                    $when = datetime_convert('UTC', 'UTC', 'now', 'Y-m-d H:i:s');
                }
            }
            if ($deleted) {
                // check for relayed deletes to our conversation
                $is_reply = false;
                $r = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($uri), intval($importer['importer_uid']));
                if (count($r)) {
                    $parent_uri = $r[0]['parent-uri'];
                    if ($r[0]['id'] != $r[0]['parent']) {
                        $is_reply = true;
                    }
                }
                if ($is_reply) {
                    $community = false;
                    if ($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP) {
                        $sql_extra = '';
                        $community = true;
                        logger('local_delivery: possible community delete');
                    } else {
                        $sql_extra = " and contact.self = 1 and item.wall = 1 ";
                    }
                    // was the top-level post for this reply written by somebody on this site?
                    // Specifically, the recipient?
                    $is_a_remote_delete = false;
                    // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
                    $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,\n\t\t\t\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`\n\t\t\t\t\t\tINNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`\n\t\t\t\t\t\tWHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')\n\t\t\t\t\t\tAND `item`.`uid` = %d\n\t\t\t\t\t\t{$sql_extra}\n\t\t\t\t\t\tLIMIT 1", dbesc($parent_uri), dbesc($parent_uri), dbesc($parent_uri), intval($importer['importer_uid']));
                    if ($r && count($r)) {
                        $is_a_remote_delete = true;
                    }
                    // Does this have the characteristics of a community or private group comment?
                    // If it's a reply to a wall post on a community/prvgroup page it's a
                    // valid community comment. Also forum_mode makes it valid for sure.
                    // If neither, it's not.
                    if ($is_a_remote_delete && $community) {
                        if (!$r[0]['forum_mode'] && !$r[0]['wall']) {
                            $is_a_remote_delete = false;
                            logger('local_delivery: not a community delete');
                        }
                    }
                    if ($is_a_remote_delete) {
                        logger('local_delivery: received remote delete');
                    }
                }
                $r = q("SELECT `item`.*, `contact`.`self` FROM `item` INNER JOIN contact on `item`.`contact-id` = `contact`.`id`\n\t\t\t\t\tWHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1", dbesc($uri), intval($importer['importer_uid']), intval($importer['id']));
                if (count($r)) {
                    $item = $r[0];
                    if ($item['deleted']) {
                        continue;
                    }
                    logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
                    if ($item['object-type'] === ACTIVITY_OBJ_EVENT) {
                        logger("Deleting event " . $item['event-id'], LOGGER_DEBUG);
                        event_delete($item['event-id']);
                    }
                    if ($item['verb'] === ACTIVITY_TAG && $item['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                        $xo = parse_xml_string($item['object'], false);
                        $xt = parse_xml_string($item['target'], false);
                        if ($xt->type === ACTIVITY_OBJ_NOTE) {
                            $i = q("select * from `item` where uri = '%s' and uid = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                            if (count($i)) {
                                // For tags, the owner cannot remove the tag on the author's copy of the post.
                                $owner_remove = $item['contact-id'] == $i[0]['contact-id'] ? true : false;
                                $author_remove = $item['origin'] && $item['self'] ? true : false;
                                $author_copy = $item['origin'] ? true : false;
                                if ($owner_remove && $author_copy) {
                                    continue;
                                }
                                if ($author_remove || $owner_remove) {
                                    $tags = explode(',', $i[0]['tag']);
                                    $newtags = array();
                                    if (count($tags)) {
                                        foreach ($tags as $tag) {
                                            if (trim($tag) !== trim($xo->body)) {
                                                $newtags[] = trim($tag);
                                            }
                                        }
                                    }
                                    q("update item set tag = '%s' where id = %d", dbesc(implode(',', $newtags)), intval($i[0]['id']));
                                    create_tags_from_item($i[0]['id']);
                                }
                            }
                        }
                    }
                    if ($item['uri'] == $item['parent-uri']) {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',\n\t\t\t\t\t\t\t`body` = '', `title` = ''\n\t\t\t\t\t\t\tWHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($when), dbesc(datetime_convert()), dbesc($item['uri']), intval($importer['importer_uid']));
                        create_tags_from_itemuri($item['uri'], $importer['importer_uid']);
                        create_files_from_itemuri($item['uri'], $importer['importer_uid']);
                        update_thread_uri($item['uri'], $importer['importer_uid']);
                    } else {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s',\n\t\t\t\t\t\t\t`body` = '', `title` = ''\n\t\t\t\t\t\t\tWHERE `uri` = '%s' AND `uid` = %d", dbesc($when), dbesc(datetime_convert()), dbesc($uri), intval($importer['importer_uid']));
                        create_tags_from_itemuri($uri, $importer['importer_uid']);
                        create_files_from_itemuri($uri, $importer['importer_uid']);
                        update_thread_uri($uri, $importer['importer_uid']);
                        if ($item['last-child']) {
                            // ensure that last-child is set in case the comment that had it just got wiped.
                            q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", dbesc(datetime_convert()), dbesc($item['parent-uri']), intval($item['uid']));
                            // who is the last child now?
                            $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d\n\t\t\t\t\t\t\t\tORDER BY `created` DESC LIMIT 1", dbesc($item['parent-uri']), intval($importer['importer_uid']));
                            if (count($r)) {
                                q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", intval($r[0]['id']));
                            }
                        }
                        // if this is a relayed delete, propagate it to other recipients
                        if ($is_a_remote_delete) {
                            proc_run('php', "include/notifier.php", "drop", $item['id']);
                        }
                    }
                }
            }
        }
    }
    foreach ($feed->get_items() as $item) {
        $is_reply = false;
        $item_id = $item->get_id();
        $rawthread = $item->get_item_tags(NAMESPACE_THREAD, 'in-reply-to');
        if (isset($rawthread[0]['attribs']['']['ref'])) {
            $is_reply = true;
            $parent_uri = $rawthread[0]['attribs']['']['ref'];
        }
        if ($is_reply) {
            $community = false;
            if ($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP) {
                $sql_extra = '';
                $community = true;
                logger('local_delivery: possible community reply');
            } else {
                $sql_extra = " and contact.self = 1 and item.wall = 1 ";
            }
            // was the top-level post for this reply written by somebody on this site?
            // Specifically, the recipient?
            $is_a_remote_comment = false;
            $top_uri = $parent_uri;
            $r = q("select `item`.`parent-uri` from `item`\n\t\t\t\tWHERE `item`.`uri` = '%s'\n\t\t\t\tLIMIT 1", dbesc($parent_uri));
            if ($r && count($r)) {
                $top_uri = $r[0]['parent-uri'];
                // POSSIBLE CLEANUP --> Why select so many fields when only forum_mode and wall are used?
                $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`,\n\t\t\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item`\n\t\t\t\t\tINNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`\n\t\t\t\t\tWHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' or `item`.`thr-parent` = '%s')\n\t\t\t\t\tAND `item`.`uid` = %d\n\t\t\t\t\t{$sql_extra}\n\t\t\t\t\tLIMIT 1", dbesc($top_uri), dbesc($top_uri), dbesc($top_uri), intval($importer['importer_uid']));
                if ($r && count($r)) {
                    $is_a_remote_comment = true;
                }
            }
            // Does this have the characteristics of a community or private group comment?
            // If it's a reply to a wall post on a community/prvgroup page it's a
            // valid community comment. Also forum_mode makes it valid for sure.
            // If neither, it's not.
            if ($is_a_remote_comment && $community) {
                if (!$r[0]['forum_mode'] && !$r[0]['wall']) {
                    $is_a_remote_comment = false;
                    logger('local_delivery: not a community reply');
                }
            }
            if ($is_a_remote_comment) {
                logger('local_delivery: received remote comment');
                $is_like = false;
                // remote reply to our post. Import and then notify everybody else.
                $datarray = get_atom_elements($feed, $item);
                $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body`  FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
                // Update content if 'updated' changes
                if (count($r)) {
                    $iid = $r[0]['id'];
                    if (edited_timestamp_is_newer($r[0], $datarray)) {
                        // do not accept (ignore) an earlier edit than one we currently have.
                        if (datetime_convert('UTC', 'UTC', $datarray['edited']) < $r[0]['edited']) {
                            continue;
                        }
                        logger('received updated comment', LOGGER_DEBUG);
                        $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                        create_tags_from_itemuri($item_id, $importer['importer_uid']);
                        proc_run('php', "include/notifier.php", "comment-import", $iid);
                    }
                    continue;
                }
                $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1", intval($importer['importer_uid']));
                $datarray['type'] = 'remote-comment';
                $datarray['wall'] = 1;
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['owner-name'] = $own[0]['name'];
                $datarray['owner-link'] = $own[0]['url'];
                $datarray['owner-avatar'] = $own[0]['thumb'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] === ACTIVITY_LIKE || $datarray['verb'] === ACTIVITY_DISLIKE || $datarray['verb'] === ACTIVITY_ATTEND || $datarray['verb'] === ACTIVITY_ATTENDNO || $datarray['verb'] === ACTIVITY_ATTENDMAYBE) {
                    $is_like = true;
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                    $datarray['last-child'] = 0;
                    // only one like or dislike per person
                    // splitted into two queries for performance issues
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`parent-uri` = '%s') and deleted = 0 limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']), dbesc($datarray['parent-uri']));
                    if ($r && count($r)) {
                        continue;
                    }
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s') and deleted = 0 limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']), dbesc($datarray['parent-uri']));
                    if ($r && count($r)) {
                        continue;
                    }
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE && $xt->id) {
                        // fetch the parent item
                        $tagp = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                        if (!count($tagp)) {
                            continue;
                        }
                        // extract tag, if not duplicate, and this user allows tags, add to parent item
                        if ($xo->id && $xo->content) {
                            $newtag = '#[url=' . $xo->id . ']' . $xo->content . '[/url]';
                            if (!stristr($tagp[0]['tag'], $newtag)) {
                                $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1", intval($importer['importer_uid']));
                                if (count($i) && !intval($i[0]['blocktags'])) {
                                    q("UPDATE item SET tag = '%s', `edited` = '%s', `changed` = '%s' WHERE id = %d", dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag), intval($tagp[0]['id']), dbesc(datetime_convert()), dbesc(datetime_convert()));
                                    create_tags_from_item($tagp[0]['id']);
                                }
                            }
                        }
                    }
                }
                $posted_id = item_store($datarray);
                $parent = 0;
                if ($posted_id) {
                    $datarray["id"] = $posted_id;
                    $r = q("SELECT `parent`, `parent-uri` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($posted_id), intval($importer['importer_uid']));
                    if (count($r)) {
                        $parent = $r[0]['parent'];
                        $parent_uri = $r[0]['parent-uri'];
                    }
                    if (!$is_like) {
                        $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($r[0]['parent']));
                        $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($posted_id));
                    }
                    if ($posted_id && $parent) {
                        proc_run('php', "include/notifier.php", "comment-import", "{$posted_id}");
                        if (!$is_like && !$importer['self']) {
                            require_once 'include/enotify.php';
                            notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . urlencode(get_item_guid($posted_id)), 'source_name' => stripslashes($datarray['author-name']), 'source_link' => $datarray['author-link'], 'source_photo' => link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $parent, 'parent_uri' => $parent_uri));
                        }
                    }
                    return 0;
                    // NOTREACHED
                }
            } else {
                // regular comment that is part of this total conversation. Have we seen it? If not, import it.
                $item_id = $item->get_id();
                $datarray = get_atom_elements($feed, $item);
                if ($importer['rel'] == CONTACT_IS_FOLLOWER) {
                    continue;
                }
                $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
                // Update content if 'updated' changes
                if (count($r)) {
                    if (edited_timestamp_is_newer($r[0], $datarray)) {
                        // do not accept (ignore) an earlier edit than one we currently have.
                        if (datetime_convert('UTC', 'UTC', $datarray['edited']) < $r[0]['edited']) {
                            continue;
                        }
                        $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                        create_tags_from_itemuri($item_id, $importer['importer_uid']);
                    }
                    // update last-child if it changes
                    $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                    if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                        $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc(datetime_convert()), dbesc($parent_uri), intval($importer['importer_uid']));
                        $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                    }
                    continue;
                }
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] === ACTIVITY_LIKE || $datarray['verb'] === ACTIVITY_DISLIKE || $datarray['verb'] === ACTIVITY_ATTEND || $datarray['verb'] === ACTIVITY_ATTENDNO || $datarray['verb'] === ACTIVITY_ATTENDMAYBE) {
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                    // only one like or dislike per person
                    // splitted into two queries for performance issues
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']), dbesc($parent_uri));
                    if ($r && count($r)) {
                        continue;
                    }
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']), dbesc($parent_uri));
                    if ($r && count($r)) {
                        continue;
                    }
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE) {
                        $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                        if (!count($r)) {
                            continue;
                        }
                        // extract tag, if not duplicate, add to parent item
                        if ($xo->content) {
                            if (!stristr($r[0]['tag'], trim($xo->content))) {
                                q("UPDATE item SET tag = '%s' WHERE id = %d", dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']' . $xo->content . '[/url]'), intval($r[0]['id']));
                                create_tags_from_item($r[0]['id']);
                            }
                        }
                    }
                }
                $posted_id = item_store($datarray);
                // find out if our user is involved in this conversation and wants to be notified.
                if (!x($datarray['type']) || $datarray['type'] != 'activity') {
                    $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0", dbesc($top_uri), intval($importer['importer_uid']));
                    if (count($myconv)) {
                        $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
                        // first make sure this isn't our own post coming back to us from a wall-to-wall event
                        if (!link_compare($datarray['author-link'], $importer_url)) {
                            foreach ($myconv as $conv) {
                                // now if we find a match, it means we're in this conversation
                                if (!link_compare($conv['author-link'], $importer_url)) {
                                    continue;
                                }
                                require_once 'include/enotify.php';
                                $conv_parent = $conv['parent'];
                                notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . urlencode(get_item_guid($posted_id)), 'source_name' => stripslashes($datarray['author-name']), 'source_link' => $datarray['author-link'], 'source_photo' => link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $conv_parent, 'parent_uri' => $parent_uri));
                                // only send one notification
                                break;
                            }
                        }
                    }
                }
                continue;
            }
        } else {
            // Head post of a conversation. Have we seen it? If not, import it.
            $item_id = $item->get_id();
            $datarray = get_atom_elements($feed, $item);
            if (x($datarray, 'object-type') && $datarray['object-type'] === ACTIVITY_OBJ_EVENT) {
                $ev = bbtoevent($datarray['body']);
                if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
                    $ev['cid'] = $importer['id'];
                    $ev['uid'] = $importer['uid'];
                    $ev['uri'] = $item_id;
                    $ev['edited'] = $datarray['edited'];
                    $ev['private'] = $datarray['private'];
                    $ev['guid'] = $datarray['guid'];
                    $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['uid']));
                    if (count($r)) {
                        $ev['id'] = $r[0]['id'];
                    }
                    $xyz = event_store($ev);
                    continue;
                }
            }
            $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
            // Update content if 'updated' changes
            if (count($r)) {
                if (edited_timestamp_is_newer($r[0], $datarray)) {
                    // do not accept (ignore) an earlier edit than one we currently have.
                    if (datetime_convert('UTC', 'UTC', $datarray['edited']) < $r[0]['edited']) {
                        continue;
                    }
                    $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                    create_tags_from_itemuri($item_id, $importer['importer_uid']);
                    update_thread_uri($item_id, $importer['importer_uid']);
                }
                // update last-child if it changes
                $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                    $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                }
                continue;
            }
            $datarray['parent-uri'] = $item_id;
            $datarray['uid'] = $importer['importer_uid'];
            $datarray['contact-id'] = $importer['id'];
            if (!link_compare($datarray['owner-link'], $importer['url'])) {
                // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
                // but otherwise there's a possible data mixup on the sender's system.
                // the tgroup delivery code called from item_store will correct it if it's a forum,
                // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
                $datarray['owner-name'] = $importer['senderName'];
                $datarray['owner-link'] = $importer['url'];
                $datarray['owner-avatar'] = $importer['thumb'];
            }
            if ($importer['rel'] == CONTACT_IS_FOLLOWER && !tgroup_check($importer['importer_uid'], $datarray)) {
                continue;
            }
            // This is my contact on another system, but it's really me.
            // Turn this into a wall post.
            $notify = item_is_remote_self($importer, $datarray);
            $posted_id = item_store($datarray, false, $notify);
            if (stristr($datarray['verb'], ACTIVITY_POKE)) {
                $verb = urldecode(substr($datarray['verb'], strpos($datarray['verb'], '#') + 1));
                if (!$verb) {
                    continue;
                }
                $xo = parse_xml_string($datarray['object'], false);
                if ($xo->type == ACTIVITY_OBJ_PERSON && $xo->id) {
                    // somebody was poked/prodded. Was it me?
                    $links = parse_xml_string("<links>" . unxmlify($xo->link) . "</links>", false);
                    foreach ($links->link as $l) {
                        $atts = $l->attributes();
                        switch ($atts['rel']) {
                            case "alternate":
                                $Blink = $atts['href'];
                                break;
                            default:
                                break;
                        }
                    }
                    if ($Blink && link_compare($Blink, $a->get_baseurl() . '/profile/' . $importer['nickname'])) {
                        // send a notification
                        require_once 'include/enotify.php';
                        notification(array('type' => NOTIFY_POKE, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . urlencode(get_item_guid($posted_id)), 'source_name' => stripslashes($datarray['author-name']), 'source_link' => $datarray['author-link'], 'source_photo' => link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'], 'verb' => $datarray['verb'], 'otype' => 'person', 'activity' => $verb, 'parent' => $datarray['parent']));
                    }
                }
            }
            continue;
        }
    }
    return 0;
    // NOTREACHED
}
Example #2
0
function zot_unencapsulate($data, $prvkey)
{
    $ret = array();
    $c = array();
    $x = parse_xml_string($data);
    $c = array('key' => $x->key, 'iv' => $x->iv, 'data' => $x->data);
    openssl_private_decrypt(base64url_decode($x->sender), $s, $prvkey);
    $ret['sender'] = $s;
    $ret['data'] = aes_unencapsulate($x, $prvkey);
    return $ret;
}
Example #3
0
function notifications_content(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        return;
    }
    nav_set_selected('notifications');
    $json = $a->argc > 1 && $a->argv[$a->argc - 1] === 'json' ? true : false;
    $o = '';
    $tabs = array(array('label' => t('System'), 'url' => $a->get_baseurl(true) . '/notifications/system', 'sel' => $a->argv[1] == 'system' ? 'active' : ''), array('label' => t('Network'), 'url' => $a->get_baseurl(true) . '/notifications/network', 'sel' => $a->argv[1] == 'network' ? 'active' : ''), array('label' => t('Personal'), 'url' => $a->get_baseurl(true) . '/notifications/personal', 'sel' => $a->argv[1] == 'personal' ? 'active' : ''), array('label' => t('Home'), 'url' => $a->get_baseurl(true) . '/notifications/home', 'sel' => $a->argv[1] == 'home' ? 'active' : ''), array('label' => t('Introductions'), 'url' => $a->get_baseurl(true) . '/notifications/intros', 'sel' => $a->argv[1] == 'intros' ? 'active' : ''));
    $o = "";
    if ($a->argc > 1 && $a->argv[1] == 'intros' || $a->argc == 1) {
        nav_set_selected('introductions');
        if ($a->argc > 2 && $a->argv[2] == 'all') {
            $sql_extra = '';
        } else {
            $sql_extra = " AND `ignore` = 0 ";
        }
        $notif_tpl = get_markup_template('notifications.tpl');
        $notif_content .= '<a href="' . (strlen($sql_extra) ? 'notifications/intros/all' : 'notifications/intros') . '" id="notifications-show-hide-link" >' . (strlen($sql_extra) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
        $r = q("SELECT COUNT(*)\tAS `total` FROM `intro`\n\t\t\tWHERE `intro`.`uid` = %d {$sql_extra} AND `intro`.`blocked` = 0 ", intval($_SESSION['uid']));
        if ($r && count($r)) {
            $a->set_pager_total($r[0]['total']);
            $a->set_pager_itemspage(20);
        }
        $r = q("SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, `fcontact`.`name` AS `fname`,`fcontact`.`url` AS `furl`,`fcontact`.`photo` AS `fphoto`,`fcontact`.`request` AS `frequest`\n\t\t\tFROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d {$sql_extra} AND `intro`.`blocked` = 0 ", intval($_SESSION['uid']));
        if ($r !== false && count($r)) {
            $sugg = get_markup_template('suggestions.tpl');
            $tpl = get_markup_template("intros.tpl");
            foreach ($r as $rr) {
                if ($rr['fid']) {
                    $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . ($a->path ? '/' . $a->path : ''));
                    $notif_content .= replace_macros($sugg, array('$str_notifytype' => t('Notification type: '), '$notify_type' => t('Friend Suggestion'), '$intro_id' => $rr['intro_id'], '$madeby' => sprintf(t('suggested by %s'), $rr['name']), '$contact_id' => $rr['contact-id'], '$photo' => x($rr, 'fphoto') ? $rr['fphoto'] : "images/person-175.jpg", '$fullname' => $rr['fname'], '$url' => zrl($rr['furl']), '$hidden' => array('hidden', t('Hide this contact from others'), $rr['hidden'] == 1, ''), '$activity' => array('activity', t('Post a new friend activity'), intval(get_pconfig(local_user(), 'system', 'post_newfriend')) ? '1' : 0, t('if applicable')), '$knowyou' => $knowyou, '$approve' => t('Approve'), '$note' => $rr['note'], '$request' => $rr['frequest'] . '?addr=' . $return_addr, '$ignore' => t('Ignore'), '$discard' => t('Discard')));
                    continue;
                }
                $friend_selected = $rr['network'] !== NETWORK_OSTATUS ? ' checked="checked" ' : ' disabled ';
                $fan_selected = $rr['network'] === NETWORK_OSTATUS ? ' checked="checked" disabled ' : '';
                $dfrn_tpl = get_markup_template('netfriend.tpl');
                $knowyou = '';
                $dfrn_text = '';
                if ($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
                    if ($rr['network'] === NETWORK_DFRN) {
                        $knowyou = t('Claims to be known to you: ') . ($rr['knowyou'] ? t('yes') : t('no'));
                        $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Fan/Admirer" means that you allow to read but you do not want to read theirs. Approve as: ');
                    } else {
                        $knowyou = '';
                        $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Sharer" means that you allow to read but you do not want to read theirs. Approve as: ');
                    }
                    $dfrn_text = replace_macros($dfrn_tpl, array('$intro_id' => $rr['intro_id'], '$friend_selected' => $friend_selected, '$fan_selected' => $fan_selected, '$approve_as' => $helptext, '$as_friend' => t('Friend'), '$as_fan' => $rr['network'] == NETWORK_DIASPORA ? t('Sharer') : t('Fan/Admirer')));
                }
                $notif_content .= replace_macros($tpl, array('$str_notifytype' => t('Notification type: '), '$notify_type' => $rr['network'] !== NETWORK_OSTATUS ? t('Friend/Connect Request') : t('New Follower'), '$dfrn_text' => $dfrn_text, '$dfrn_id' => $rr['issued-id'], '$uid' => $_SESSION['uid'], '$intro_id' => $rr['intro_id'], '$contact_id' => $rr['contact-id'], '$photo' => x($rr, 'photo') ? $rr['photo'] : "images/person-175.jpg", '$fullname' => $rr['name'], '$hidden' => array('hidden', t('Hide this contact from others'), $rr['hidden'] == 1, ''), '$activity' => array('activity', t('Post a new friend activity'), intval(get_pconfig(local_user(), 'system', 'post_newfriend')) ? '1' : 0, t('if applicable')), '$url' => zrl($rr['url']), '$knowyou' => $knowyou, '$approve' => t('Approve'), '$note' => $rr['note'], '$ignore' => t('Ignore'), '$discard' => t('Discard')));
            }
        } else {
            info(t('No introductions.') . EOL);
        }
        $o .= replace_macros($notif_tpl, array('$notif_header' => t('Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
        $o .= paginate($a);
        return $o;
    } else {
        if ($a->argc > 1 && $a->argv[1] == 'network') {
            $notif_tpl = get_markup_template('notifications.tpl');
            $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC", intval(local_user()));
            $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
            $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
            $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
            $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
            $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
            $notif_content = '';
            if ($r) {
                foreach ($r as $it) {
                    switch ($it['verb']) {
                        case ACTIVITY_LIKE:
                            $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                            break;
                        case ACTIVITY_DISLIKE:
                            $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                            break;
                        case ACTIVITY_FRIEND:
                            $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                            $obj = parse_xml_string($xmlhead . $it['object']);
                            $it['fname'] = $obj->title;
                            $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                            break;
                        default:
                            $item_text = $it['id'] == $it['parent'] ? sprintf(t("%s created a new post"), $it['author-name']) : sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']);
                            $tpl = $it['id'] == $it['parent'] ? $tpl_item_posts : $tpl_item_comments;
                            $notif_content .= replace_macros($tpl, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => $item_text, '$item_when' => relative_date($it['created'])));
                    }
                }
            } else {
                $notif_content = t('No more network notifications.');
            }
            $o .= replace_macros($notif_tpl, array('$notif_header' => t('Network Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
        } else {
            if ($a->argc > 1 && $a->argv[1] == 'system') {
                $notif_tpl = get_markup_template('notifications.tpl');
                $not_tpl = get_markup_template('notify.tpl');
                require_once 'include/bbcode.php';
                $r = q("SELECT * from notify where uid = %d and seen = 0 order by date desc", intval(local_user()));
                if (count($r) > 0) {
                    foreach ($r as $it) {
                        $notif_content .= replace_macros($not_tpl, array('$item_link' => $a->get_baseurl(true) . '/notify/view/' . $it['id'], '$item_image' => $it['photo'], '$item_text' => strip_tags(bbcode($it['msg'])), '$item_when' => relative_date($it['date'])));
                    }
                } else {
                    $notif_content .= t('No more system notifications.');
                }
                $o .= replace_macros($notif_tpl, array('$notif_header' => t('System Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
            } else {
                if ($a->argc > 1 && $a->argv[1] == 'personal') {
                    $notif_tpl = get_markup_template('notifications.tpl');
                    $myurl = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
                    $myurl = substr($myurl, strpos($myurl, '://') + 3);
                    $myurl = str_replace(array('www.', '.'), array('', '\\.'), $myurl);
                    $diasp_url = str_replace('/profile/', '/u/', $myurl);
                    $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ", dbesc($myurl . '$'), dbesc($myurl . '\\]'), dbesc($diasp_url . '\\]'));
                    $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1\n\t\t\t\t{$sql_extra}\n\t\t\t\tAND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC", intval(local_user()));
                    $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
                    $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
                    $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
                    $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
                    $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
                    $notif_content = '';
                    if (count($r) > 0) {
                        foreach ($r as $it) {
                            switch ($it['verb']) {
                                case ACTIVITY_LIKE:
                                    $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                case ACTIVITY_DISLIKE:
                                    $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                case ACTIVITY_FRIEND:
                                    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                                    $obj = parse_xml_string($xmlhead . $it['object']);
                                    $it['fname'] = $obj->title;
                                    $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                default:
                                    $item_text = $it['id'] == $it['parent'] ? sprintf(t("%s created a new post"), $it['author-name']) : sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']);
                                    $tpl = $it['id'] == $it['parent'] ? $tpl_item_posts : $tpl_item_comments;
                                    $notif_content .= replace_macros($tpl, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => $item_text, '$item_when' => relative_date($it['created'])));
                            }
                        }
                    } else {
                        $notif_content = t('No more personal notifications.');
                    }
                    $o .= replace_macros($notif_tpl, array('$notif_header' => t('Personal Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
                } else {
                    if ($a->argc > 1 && $a->argv[1] == 'home') {
                        $notif_tpl = get_markup_template('notifications.tpl');
                        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC", intval(local_user()));
                        $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
                        $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
                        $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
                        $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
                        $notif_content = '';
                        if (count($r) > 0) {
                            foreach ($r as $it) {
                                switch ($it['verb']) {
                                    case ACTIVITY_LIKE:
                                        $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    case ACTIVITY_DISLIKE:
                                        $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    case ACTIVITY_FRIEND:
                                        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                                        $obj = parse_xml_string($xmlhead . $it['object']);
                                        $it['fname'] = $obj->title;
                                        $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    default:
                                        $notif_content .= replace_macros($tpl_item_comments, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                }
                            }
                        } else {
                            $notif_content = t('No more home notifications.');
                        }
                        $o .= replace_macros($notif_tpl, array('$notif_header' => t('Home Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
                    }
                }
            }
        }
    }
    $o .= paginate($a);
    return $o;
}
Example #4
0
function dfrn_poll_content(&$a)
{
    $dfrn_id = x($_GET, 'dfrn_id') ? $_GET['dfrn_id'] : '';
    $type = x($_GET, 'type') ? $_GET['type'] : 'data';
    $last_update = x($_GET, 'last_update') ? $_GET['last_update'] : '';
    $destination_url = x($_GET, 'destination_url') ? $_GET['destination_url'] : '';
    $sec = x($_GET, 'sec') ? $_GET['sec'] : '';
    $dfrn_version = x($_GET, 'dfrn_version') ? (double) $_GET['dfrn_version'] : 2.0;
    $perm = x($_GET, 'perm') ? $_GET['perm'] : 'r';
    $direction = -1;
    if (strpos($dfrn_id, ':') == 1) {
        $direction = intval(substr($dfrn_id, 0, 1));
        $dfrn_id = substr($dfrn_id, 2);
    }
    if ($dfrn_id != '') {
        // initial communication from external contact
        $hash = random_string();
        $status = 0;
        $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
        if ($type !== 'profile') {
            $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )\n\t\t\t\tVALUES( '%s', '%s', '%s', '%s', '%s' ) ", dbesc($hash), dbesc($dfrn_id), intval(time() + 60), dbesc($type), dbesc($last_update));
        }
        $sql_extra = '';
        switch ($direction) {
            case -1:
                if ($type === 'profile') {
                    $sql_extra = sprintf(" AND ( `dfrn-id` = '%s' OR `issued-id` = '%s' ) ", dbesc($dfrn_id), dbesc($dfrn_id));
                } else {
                    $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($dfrn_id));
                }
                $my_id = $dfrn_id;
                break;
            case 0:
                $sql_extra = sprintf(" AND `issued-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
                $my_id = '1:' . $dfrn_id;
                break;
            case 1:
                $sql_extra = sprintf(" AND `dfrn-id` = '%s' AND `duplex` = 1 ", dbesc($dfrn_id));
                $my_id = '0:' . $dfrn_id;
                break;
            default:
                goaway(z_root());
                break;
                // NOTREACHED
        }
        $r = q("SELECT `contact`.*, `user`.`username`, `user`.`nickname` \n\t\t\tFROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`\n\t\t\tWHERE `contact`.`blocked` = 0 AND `contact`.`pending` = 0 \n\t\t\tAND `user`.`nickname` = '%s' {$sql_extra} LIMIT 1", dbesc($a->argv[1]));
        if (count($r)) {
            $challenge = '';
            $encrypted_id = '';
            $id_str = $my_id . '.' . mt_rand(1000, 9999);
            if ($r[0]['duplex'] && strlen($r[0]['pubkey'])) {
                openssl_public_encrypt($hash, $challenge, $r[0]['pubkey']);
                openssl_public_encrypt($id_str, $encrypted_id, $r[0]['pubkey']);
            } else {
                openssl_private_encrypt($hash, $challenge, $r[0]['prvkey']);
                openssl_private_encrypt($id_str, $encrypted_id, $r[0]['prvkey']);
            }
            $challenge = bin2hex($challenge);
            $encrypted_id = bin2hex($encrypted_id);
        } else {
            $status = 1;
            $challenge = '';
            $encrypted_id = '';
        }
        if ($type === 'profile' && strlen($sec)) {
            // URL reply
            if ($dfrn_version < 2.2) {
                $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $encrypted_id . '&type=profile-check' . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&challenge=' . $challenge . '&sec=' . $sec);
            } else {
                $s = post_url($r[0]['poll'], array('dfrn_id' => $encrypted_id, 'type' => 'profile-check', 'dfrn_version' => DFRN_PROTOCOL_VERSION, 'challenge' => $challenge, 'sec' => $sec));
            }
            $profile = $r[0]['nickname'];
            switch ($destination_url) {
                case 'profile':
                    $dest = $a->get_baseurl() . '/profile/' . $profile . '?tab=profile';
                    break;
                case 'photos':
                    $dest = $a->get_baseurl() . '/photos/' . $profile;
                    break;
                case 'status':
                case '':
                    $dest = $a->get_baseurl() . '/profile/' . $profile;
                    break;
                default:
                    $dest = $destination_url;
                    break;
            }
            logger("dfrn_poll: sec profile: " . $s, LOGGER_DATA);
            if (strlen($s) && strstr($s, '<?xml')) {
                $xml = parse_xml_string($s);
                logger('dfrn_poll: profile: parsed xml: ' . print_r($xml, true), LOGGER_DATA);
                logger('dfrn_poll: secure profile: challenge: ' . $xml->challenge . ' expecting ' . $hash);
                logger('dfrn_poll: secure profile: sec: ' . $xml->sec . ' expecting ' . $sec);
                if ((int) $xml->status == 0 && $xml->challenge == $hash && $xml->sec == $sec) {
                    $_SESSION['authenticated'] = 1;
                    $_SESSION['visitor_id'] = $r[0]['id'];
                    $_SESSION['visitor_home'] = $r[0]['url'];
                    $_SESSION['visitor_visiting'] = $r[0]['uid'];
                    info(sprintf(t('%s welcomes %s'), $r[0]['username'], $r[0]['name']) . EOL);
                    // Visitors get 1 day session.
                    $session_id = session_id();
                    $expire = time() + 86400;
                    q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1", dbesc($expire), dbesc($session_id));
                }
                goaway($dest);
            }
            goaway($dest);
            // NOTREACHED
        } else {
            // XML reply
            header("Content-type: text/xml");
            echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<dfrn_poll>' . "\r\n" . "\t" . '<status>' . $status . '</status>' . "\r\n" . "\t" . '<dfrn_version>' . DFRN_PROTOCOL_VERSION . '</dfrn_version>' . "\r\n" . "\t" . '<dfrn_id>' . $encrypted_id . '</dfrn_id>' . "\r\n" . "\t" . '<challenge>' . $challenge . '</challenge>' . "\r\n" . '</dfrn_poll>' . "\r\n";
            killme();
            // NOTREACHED
        }
    }
}
Example #5
0
function diaspora_reshare($importer, $xml, $msg)
{
    logger('diaspora_reshare: init: ' . print_r($xml, true));
    $a = get_app();
    $guid = notags(unxmlify($xml->guid));
    $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
    if ($diaspora_handle != $msg['author']) {
        logger('diaspora_post: Potential forgery. Message handle is not the same as envelope sender.');
        return 202;
    }
    $contact = diaspora_get_contact_by_handle($importer['uid'], $diaspora_handle);
    if (!$contact) {
        return;
    }
    if (!diaspora_post_allow($importer, $contact)) {
        logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml, true));
        return 202;
    }
    $message_id = $diaspora_handle . ':' . $guid;
    $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1", intval($importer['uid']), dbesc($message_id), dbesc($guid));
    if (count($r)) {
        logger('diaspora_reshare: message exists: ' . $guid);
        return;
    }
    $orig_author = notags(unxmlify($xml->root_diaspora_id));
    $orig_guid = notags(unxmlify($xml->root_guid));
    $source_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/p/' . $orig_guid . '.xml';
    $orig_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/posts/' . $orig_guid;
    $x = fetch_url($source_url);
    if (!$x) {
        $x = fetch_url(str_replace('https://', 'http://', $source_url));
    }
    if (!$x) {
        logger('diaspora_reshare: unable to fetch source url ' . $source_url);
        return;
    }
    logger('diaspora_reshare: source: ' . $x);
    $x = str_replace(array('<activity_streams-photo>', '</activity_streams-photo>'), array('<asphoto>', '</asphoto>'), $x);
    $source_xml = parse_xml_string($x, false);
    if (strlen($source_xml->post->asphoto->objectId) && $source_xml->post->asphoto->objectId != 0 && $source_xml->post->asphoto->image_url) {
        $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
        $body = scale_external_images($body, false);
    } elseif ($source_xml->post->asphoto->image_url) {
        $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
        $body = scale_external_images($body);
    } elseif ($source_xml->post->status_message) {
        $body = diaspora2bb($source_xml->post->status_message->raw_message);
        // Checking for embedded pictures
        if ($source_xml->post->status_message->photo->remote_photo_path and $source_xml->post->status_message->photo->remote_photo_name) {
            $remote_photo_path = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_path));
            $remote_photo_name = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_name));
            $body = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n" . $body;
            logger('diaspora_reshare: embedded picture link found: ' . $body, LOGGER_DEBUG);
        }
        $body = scale_external_images($body);
        // Add OEmbed and other information to the body
        $body = add_page_info_to_body($body, false, true);
    } else {
        // Maybe it is a reshare of a photo that will be delivered at a later time (testing)
        logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml, true));
        $body = "";
        //return;
    }
    //if(! $body) {
    //	logger('diaspora_reshare: empty body: source= ' . $x);
    //	return;
    //}
    $person = find_diaspora_person_by_handle($orig_author);
    /*if(is_array($person) && x($person,'name') && x($person,'url'))
    		$details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
    	else
    		$details = $orig_author;
    
    	$prefix = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8') . $details . "\n";*/
    // allocate a guid on our system - we aren't fixing any collisions.
    // we're ignoring them
    $g = q("select * from guid where guid = '%s' limit 1", dbesc($guid));
    if (!count($g)) {
        q("insert into guid ( guid ) values ( '%s' )", dbesc($guid));
    }
    $created = unxmlify($xml->created_at);
    $private = unxmlify($xml->public) == 'false' ? 1 : 0;
    $datarray = array();
    $str_tags = '';
    $tags = get_tags($body);
    if (count($tags)) {
        foreach ($tags as $tag) {
            if (strpos($tag, '#') === 0) {
                if (strpos($tag, '[url=')) {
                    continue;
                }
                // don't link tags that are already embedded in links
                if (preg_match('/\\[(.*?)' . preg_quote($tag, '/') . '(.*?)\\]/', $body)) {
                    continue;
                }
                if (preg_match('/\\[(.*?)\\]\\((.*?)' . preg_quote($tag, '/') . '(.*?)\\)/', $body)) {
                    continue;
                }
                $basetag = str_replace('_', ' ', substr($tag, 1));
                $body = str_replace($tag, '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]', $body);
                if (strlen($str_tags)) {
                    $str_tags .= ',';
                }
                $str_tags .= '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
                continue;
            }
        }
    }
    $plink = 'https://' . substr($diaspora_handle, strpos($diaspora_handle, '@') + 1) . '/posts/' . $guid;
    $datarray['uid'] = $importer['uid'];
    $datarray['contact-id'] = $contact['id'];
    $datarray['wall'] = 0;
    $datarray['network'] = NETWORK_DIASPORA;
    $datarray['guid'] = $guid;
    $datarray['uri'] = $datarray['parent-uri'] = $message_id;
    $datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert('UTC', 'UTC', $created);
    $datarray['private'] = $private;
    $datarray['parent'] = 0;
    $datarray['plink'] = $plink;
    $datarray['owner-name'] = $contact['name'];
    $datarray['owner-link'] = $contact['url'];
    $datarray['owner-avatar'] = x($contact, 'thumb') ? $contact['thumb'] : $contact['photo'];
    if (!intval(get_config('system', 'wall-to-wall_share'))) {
        $prefix = "[share author='" . str_replace(array("'", "[", "]"), array("&#x27;", "&#x5B;", "&#x5D;"), $person['name']) . "' profile='" . $person['url'] . "' avatar='" . (x($person, 'thumb') ? $person['thumb'] : $person['photo']) . "' link='" . str_replace(array("'", "[", "]"), array("&#x27;", "&#x5B;", "&#x5D;"), $orig_url) . "']";
        $datarray['author-name'] = $contact['name'];
        $datarray['author-link'] = $contact['url'];
        $datarray['author-avatar'] = $contact['thumb'];
        $datarray['body'] = $prefix . $body . "[/share]";
    } else {
        // Let reshared messages look like wall-to-wall posts
        $datarray['author-name'] = $person['name'];
        $datarray['author-link'] = $person['url'];
        $datarray['author-avatar'] = x($person, 'thumb') ? $person['thumb'] : $person['photo'];
        $datarray['body'] = $body;
    }
    $datarray['tag'] = $str_tags;
    $datarray['app'] = 'Diaspora';
    // if empty content it might be a photo that hasn't arrived yet. If a photo arrives, we'll make it visible. (testing)
    $datarray['visible'] = strlen($body) ? 1 : 0;
    $message_id = item_store($datarray);
    //if($message_id) {
    //	q("update item set plink = '%s' where id = %d",
    //		dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id),
    //		intval($message_id)
    //	);
    //}
    return;
}
Example #6
0
/**
 * Render actions localized
 */
function localize_item(&$item)
{
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if ($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
        $r = q("SELECT * from `item`,`contact` WHERE \n\t\t\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        switch ($item['verb']) {
            case ACTIVITY_LIKE:
                $bodyverb = t('%1$s likes %2$s\'s %3$s');
                break;
            case ACTIVITY_DISLIKE:
                $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
                break;
        }
        $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
    }
    if ($item['verb'] === ACTIVITY_FRIEND) {
        if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>");
        $Bname = $obj->title;
        $Blink = "";
        $Bphoto = "";
        foreach ($links->link as $l) {
            $atts = $l->attributes();
            switch ($atts['rel']) {
                case "alternate":
                    $Blink = $atts['href'];
                case "photo":
                    $Bphoto = $atts['href'];
            }
        }
        $A = '[url=' . $Alink . ']' . $Aname . '[/url]';
        $B = '[url=' . $Blink . ']' . $Bname . '[/url]';
        if ($Bphoto != "") {
            $Bphoto = '[url=' . $Blink . '][img]' . $Bphoto . '[/img][/url]';
        }
        $item['body'] = sprintf(t('%1$s is now friends with %2$s'), $A, $B) . "\n\n\n" . $Bphoto;
    }
    if ($item['verb'] === ACTIVITY_TAG) {
        $r = q("SELECT * from `item`,`contact` WHERE \n\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        $parsedobj = parse_xml_string($xmlhead . $item['object']);
        $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
        $item['body'] = sprintf(t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag);
    }
}
Example #7
0
/**
 * Render actions localized
 */
function localize_item(&$item)
{
    $Text = $item['body'];
    $saved_image = '';
    $img_start = strpos($Text, '[img]data:');
    $img_end = strpos($Text, '[/img]');
    if ($img_start !== false && $img_end !== false && $img_end > $img_start) {
        $start_fragment = substr($Text, 0, $img_start);
        $img_start += strlen('[img]');
        $saved_image = substr($Text, $img_start, $img_end - $img_start);
        $end_fragment = substr($Text, $img_end + strlen('[/img]'));
        $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
        $search = '/\\[url\\=(.*?)\\]\\[!#saved_image#!\\]\\[\\/url\\]' . '/is';
        $replace = '[url=' . z_path() . '/redir/' . $item['contact-id'] . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]';
        $Text = preg_replace($search, $replace, $Text);
        if (strlen($saved_image)) {
            $item['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]', $Text);
        }
    }
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if ($item['verb'] === ACTIVITY_LIKE || $item['verb'] === ACTIVITY_DISLIKE) {
        $r = q("SELECT * from `item`,`contact` WHERE \n\t\t\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        switch ($item['verb']) {
            case ACTIVITY_LIKE:
                $bodyverb = t('%1$s likes %2$s\'s %3$s');
                break;
            case ACTIVITY_DISLIKE:
                $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
                break;
        }
        $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
    }
    if ($item['verb'] === ACTIVITY_FRIEND) {
        if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>");
        $Bname = $obj->title;
        $Blink = "";
        $Bphoto = "";
        foreach ($links->link as $l) {
            $atts = $l->attributes();
            switch ($atts['rel']) {
                case "alternate":
                    $Blink = $atts['href'];
                case "photo":
                    $Bphoto = $atts['href'];
            }
        }
        $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
        $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
        if ($Bphoto != "") {
            $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
        }
        $item['body'] = sprintf(t('%1$s is now friends with %2$s'), $A, $B) . "\n\n\n" . $Bphoto;
    }
    if ($item['verb'] === ACTIVITY_TAG) {
        $r = q("SELECT * from `item`,`contact` WHERE \n\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        $parsedobj = parse_xml_string($xmlhead . $item['object']);
        $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
        $item['body'] = sprintf(t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag);
    }
    if ($item['verb'] === ACTIVITY_FAVORITE) {
        if ($item['object-type'] == "") {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        if (strlen($obj->id)) {
            $r = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($obj->id), intval($item['uid']));
            if (count($r) && $r[0]['plink']) {
                $target = $r[0];
                $Bname = $target['author-name'];
                $Blink = $target['author-link'];
                $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
                $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
                $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
                $item['body'] = sprintf(t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P) . "\n";
            }
        }
    }
    $matches = null;
    if (preg_match_all('/@\\[url=(.*?)\\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
        foreach ($matches as $mtch) {
            if (!strpos($mtch[1], 'zrl=')) {
                $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
            }
        }
    }
    if (preg_match_all('/\\[url=(.*?)\\/photos\\/(.*?)\\/image\\/(.*?)\\]\\[img(.*?)\\]h(.*?)\\[\\/img\\]\\[\\/url\\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
        logger('matched');
        foreach ($matches as $mtch) {
            $item['body'] = str_replace($mtch[0], '[url=' . zrl($mtch[1] . '/photos/' . $mtch[2] . '/image/' . $mtch[3], true) . '][img' . $mtch[4] . ']h' . $mtch[5] . '[/img][/url]', $item['body']);
        }
    }
}
Example #8
0
function poller_run($argv, $argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    require_once 'library/simplepie/simplepie.inc';
    require_once 'include/items.php';
    require_once 'include/Contact.php';
    require_once 'include/email.php';
    require_once 'include/socgraph.php';
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    logger('poller: start');
    // run queue delivery process in the background
    proc_run('php', "include/queue.php");
    // expire any expired accounts
    q("UPDATE user SET `account_expired` = 1 where `account_expired` = 0 \n\t\tAND `account_expires_on` != '0000-00-00 00:00:00' \n\t\tAND `account_expires_on` < UTC_TIMESTAMP() ");
    $abandon_days = intval(get_config('system', 'account_abandon_days'));
    if ($abandon_days < 1) {
        $abandon_days = 0;
    }
    // once daily run birthday_updates and then expire in background
    $d1 = get_config('system', 'last_expire_day');
    $d2 = intval(datetime_convert('UTC', 'UTC', 'now', 'd'));
    if ($d2 != intval($d1)) {
        update_contact_birthdays();
        update_suggestions();
        set_config('system', 'last_expire_day', $d2);
        proc_run('php', 'include/expire.php');
    }
    // clear old cache
    Cache::clear();
    $manual_id = 0;
    $generation = 0;
    $hub_update = false;
    $force = false;
    $restart = false;
    if ($argc > 1 && $argv[1] == 'force') {
        $force = true;
    }
    if ($argc > 1 && $argv[1] == 'restart') {
        $restart = true;
        $generation = intval($argv[2]);
        if (!$generation) {
            killme();
        }
    }
    if ($argc > 1 && intval($argv[1])) {
        $manual_id = intval($argv[1]);
        $force = true;
    }
    $sql_extra = $manual_id ? " AND `id` = {$manual_id} " : "";
    reload_plugins();
    $d = datetime_convert();
    if (!$restart) {
        proc_run('php', 'include/cronhooks.php');
    }
    // Only poll from those with suitable relationships,
    // and which have a polling address and ignore Diaspora since
    // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
    $abandon_sql = $abandon_days ? sprintf(" AND `user`.`login_date` > UTC_TIMESTAMP() - INTERVAL %d DAY ", intval($abandon_days)) : '';
    $contacts = q("SELECT `contact`.`id` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` \n\t\tWHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''\n\t\tAND NOT `network` IN ( '%s', '%s' )\n\t\t{$sql_extra} \n\t\tAND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0 \n\t\tAND `user`.`account_expired` = 0 {$abandon_sql} ORDER BY RAND()", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_FACEBOOK));
    if (!count($contacts)) {
        return;
    }
    foreach ($contacts as $c) {
        $res = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($c['id']));
        if (!$res || !count($res)) {
            continue;
        }
        foreach ($res as $contact) {
            $xml = false;
            if ($manual_id) {
                $contact['last-update'] = '0000-00-00 00:00:00';
            }
            if ($contact['network'] === NETWORK_DFRN || $contact['network'] === NETWORK_OSTATUS) {
                $contact['priority'] = 2;
            }
            if ($contact['priority'] || $contact['subhub']) {
                $hub_update = true;
                $update = false;
                $t = $contact['last-update'];
                // We should be getting everything via a hub. But just to be sure, let's check once a day.
                // (You can make this more or less frequent if desired by setting 'pushpoll_frequency' appropriately)
                // This also lets us update our subscription to the hub, and add or replace hubs in case it
                // changed. We will only update hubs once a day, regardless of 'pushpoll_frequency'.
                if ($contact['subhub']) {
                    $interval = get_config('system', 'pushpoll_frequency');
                    $contact['priority'] = $interval !== false ? intval($interval) : 3;
                    $hub_update = false;
                    if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day") || $force) {
                        $hub_update = true;
                    }
                } else {
                    $hub_update = false;
                }
                /**
                 * Based on $contact['priority'], should we poll this site now? Or later?
                 */
                switch ($contact['priority']) {
                    case 5:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 month")) {
                            $update = true;
                        }
                        break;
                    case 4:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 week")) {
                            $update = true;
                        }
                        break;
                    case 3:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
                            $update = true;
                        }
                        break;
                    case 2:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 12 hour")) {
                            $update = true;
                        }
                        break;
                    case 1:
                    default:
                        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 hour")) {
                            $update = true;
                        }
                        break;
                }
                if (!$update && !$force) {
                    continue;
                }
            }
            // Check to see if we are running out of memory - if so spawn a new process and kill this one
            $avail_memory = return_bytes(ini_get('memory_limit'));
            $memused = memory_get_peak_usage(true);
            if (intval($avail_memory)) {
                if ($memused / $avail_memory > 0.95) {
                    if ($generation + 1 > 10) {
                        logger('poller: maximum number of spawns exceeded. Terminating.');
                        killme();
                    }
                    logger('poller: memory exceeded. ' . $memused . ' bytes used. Spawning new poll.');
                    proc_run('php', 'include/poller.php', 'restart', (string) $generation + 1);
                    killme();
                }
            }
            $importer_uid = $contact['uid'];
            $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($importer_uid));
            if (!count($r)) {
                continue;
            }
            $importer = $r[0];
            logger("poller: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
            $last_update = $contact['last-update'] === '0000-00-00 00:00:00' ? datetime_convert('UTC', 'UTC', 'now - 30 days', ATOM_TIME) : datetime_convert('UTC', 'UTC', $contact['last-update'], ATOM_TIME);
            if ($contact['network'] === NETWORK_DFRN) {
                $idtosend = $orig_id = $contact['dfrn-id'] ? $contact['dfrn-id'] : $contact['issued-id'];
                if (intval($contact['duplex']) && $contact['dfrn-id']) {
                    $idtosend = '0:' . $orig_id;
                }
                if (intval($contact['duplex']) && $contact['issued-id']) {
                    $idtosend = '1:' . $orig_id;
                }
                // they have permission to write to us. We already filtered this in the contact query.
                $perm = 'rw';
                $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=data&last_update=' . $last_update . '&perm=' . $perm;
                $handshake_xml = fetch_url($url);
                logger('poller: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
                if (!$handshake_xml) {
                    logger("poller: {$url} appears to be dead - marking for death ");
                    // dead connection - might be a transient event, or this might
                    // mean the software was uninstalled or the domain expired.
                    // Will keep trying for one month.
                    mark_for_death($contact);
                    // set the last-update so we don't keep polling
                    $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($contact['id']));
                    continue;
                }
                if (!strstr($handshake_xml, '<?xml')) {
                    logger('poller: response from ' . $url . ' did not contain XML.');
                    $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($contact['id']));
                    continue;
                }
                $res = parse_xml_string($handshake_xml);
                if (intval($res->status) == 1) {
                    logger("poller: {$url} replied status 1 - marking for death ");
                    // we may not be friends anymore. Will keep trying for one month.
                    // set the last-update so we don't keep polling
                    $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($contact['id']));
                    mark_for_death($contact);
                } else {
                    if ($contact['term-date'] != '0000-00-00 00:00:00') {
                        logger("poller: {$url} back from the dead - removing mark for death");
                        unmark_for_death($contact);
                    }
                }
                if (intval($res->status) != 0 || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
                    continue;
                }
                if ((double) $res->dfrn_version > 2.21 && $contact['poco'] == '') {
                    q("update contact set poco = '%s' where id = %d limit 1", dbesc(str_replace('/profile/', '/poco/', $contact['url'])), intval($contact['id']));
                }
                $postvars = array();
                $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
                $challenge = hex2bin((string) $res->challenge);
                $final_dfrn_id = '';
                if ($contact['duplex'] && strlen($contact['prvkey'])) {
                    openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
                    openssl_private_decrypt($challenge, $postvars['challenge'], $contact['prvkey']);
                } else {
                    openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
                    openssl_public_decrypt($challenge, $postvars['challenge'], $contact['pubkey']);
                }
                $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
                if (strpos($final_dfrn_id, ':') == 1) {
                    $final_dfrn_id = substr($final_dfrn_id, 2);
                }
                if ($final_dfrn_id != $orig_id) {
                    logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
                    // did not decode properly - cannot trust this site
                    continue;
                }
                $postvars['dfrn_id'] = $idtosend;
                $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
                $postvars['perm'] = 'rw';
                $xml = post_url($contact['poll'], $postvars);
            } elseif ($contact['network'] === NETWORK_OSTATUS || $contact['network'] === NETWORK_DIASPORA || $contact['network'] === NETWORK_FEED) {
                // Upgrading DB fields from an older Friendika version
                // Will only do this once per notify-enabled OStatus contact
                // or if relationship changes
                $stat_writeable = $contact['notify'] && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND) ? 1 : 0;
                if ($stat_writeable != $contact['writable']) {
                    q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d LIMIT 1", intval($stat_writeable), intval($contact['id']));
                }
                // Are we allowed to import from this person?
                if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
                    continue;
                }
                $xml = fetch_url($contact['poll']);
            } elseif ($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) {
                $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
                if ($mail_disabled) {
                    continue;
                }
                $mbox = null;
                $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer_uid));
                $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1", intval($importer_uid));
                if (count($x) && count($mailconf)) {
                    $mailbox = construct_mailbox_name($mailconf[0]);
                    $password = '';
                    openssl_private_decrypt(hex2bin($mailconf[0]['pass']), $password, $x[0]['prvkey']);
                    $mbox = email_connect($mailbox, $mailconf[0]['user'], $password);
                    unset($password);
                    if ($mbox) {
                        q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1", dbesc(datetime_convert()), intval($mailconf[0]['id']), intval($importer_uid));
                    }
                }
                if ($mbox) {
                    $msgs = email_poll($mbox, $contact['addr']);
                    if (count($msgs)) {
                        foreach ($msgs as $msg_uid) {
                            $datarray = array();
                            $meta = email_msg_meta($mbox, $msg_uid);
                            $headers = email_msg_headers($mbox, $msg_uid);
                            // look for a 'references' header and try and match with a parent item we have locally.
                            $raw_refs = x($headers, 'references') ? str_replace("\t", '', $headers['references']) : '';
                            $datarray['uri'] = trim($meta->message_id, '<>');
                            if ($raw_refs) {
                                $refs_arr = explode(' ', $raw_refs);
                                if (count($refs_arr)) {
                                    for ($x = 0; $x < count($refs_arr); $x++) {
                                        $refs_arr[$x] = "'" . str_replace(array('<', '>', ' '), array('', '', ''), dbesc($refs_arr[$x])) . "'";
                                    }
                                }
                                $qstr = implode(',', $refs_arr);
                                $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( {$qstr} ) AND `uid` = %d LIMIT 1", intval($importer_uid));
                                if (count($r)) {
                                    $datarray['parent-uri'] = $r[0]['uri'];
                                }
                            }
                            if (!x($datarray, 'parent-uri')) {
                                $datarray['parent-uri'] = $datarray['uri'];
                            }
                            // Have we seen it before?
                            $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($importer_uid), dbesc($datarray['uri']));
                            if (count($r)) {
                                if ($meta->deleted && !$r[0]['deleted']) {
                                    q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($r[0]['id']));
                                }
                                continue;
                            }
                            $datarray['title'] = notags(trim($meta->subject));
                            $datarray['created'] = datetime_convert('UTC', 'UTC', $meta->date);
                            $r = email_get_msg($mbox, $msg_uid);
                            if (!$r) {
                                continue;
                            }
                            $datarray['body'] = escape_tags($r['body']);
                            // some mailing lists have the original author as 'from' - add this sender info to msg body.
                            // todo: adding a gravatar for the original author would be cool
                            if (!stristr($meta->from, $contact['addr'])) {
                                $datarray['body'] = t('From: ') . escape_tags($meta->from) . "\n\n" . $datarray['body'];
                            }
                            $datarray['uid'] = $importer_uid;
                            $datarray['contact-id'] = $contact['id'];
                            if ($datarray['parent-uri'] === $datarray['uri']) {
                                $datarray['private'] = 1;
                            }
                            if ($contact['network'] === NETWORK_MAIL && !get_pconfig($importer_uid, 'system', 'allow_public_email_replies')) {
                                $datarray['private'] = 1;
                                $datarray['allow_cid'] = '<' . $contact['id'] . '>';
                            }
                            $datarray['author-name'] = $contact['name'];
                            $datarray['author-link'] = 'mailbox';
                            $datarray['author-avatar'] = $contact['photo'];
                            $stored_item = item_store($datarray);
                            q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($datarray['parent-uri']), intval($importer_uid));
                            q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1", intval($stored_item));
                        }
                    }
                    imap_close($mbox);
                }
            } elseif ($contact['network'] === NETWORK_FACEBOOK) {
                // This is picked up by the Facebook plugin on a cron hook.
                // Ignored here.
            }
            if ($xml) {
                logger('poller: received xml : ' . $xml, LOGGER_DATA);
                if (!strstr($xml, '<?xml')) {
                    logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
                    $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($contact['id']));
                    continue;
                }
                consume_feed($xml, $importer, $contact, $hub, 1, 1);
                // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
                consume_feed($xml, $importer, $contact, $hub, 1, 2);
                $hubmode = 'subscribe';
                if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) {
                    $hubmode = 'unsubscribe';
                }
                if (strlen($hub) && $hub_update && $contact['rel'] != CONTACT_IS_FOLLOWER) {
                    logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : '******'name']);
                    $hubs = explode(',', $hub);
                    if (count($hubs)) {
                        foreach ($hubs as $h) {
                            $h = trim($h);
                            if (!strlen($h)) {
                                continue;
                            }
                            subscribe_to_hub($h, $importer, $contact, $hubmode);
                        }
                    }
                }
            }
            $updated = datetime_convert();
            $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d LIMIT 1", dbesc($updated), dbesc($updated), intval($contact['id']));
            // load current friends if possible.
            if ($contact['poco']) {
                $r = q("SELECT count(*) as total from glink \n\t\t\t\t\twhere `cid` = %d and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY", intval($contact['id']));
            }
            if (count($r)) {
                if (!$r[0]['total']) {
                    poco_load($contact['id'], $importer_uid, $contact['poco']);
                }
            }
            // loop - next contact
        }
    }
    return;
}
Example #9
0
/**
 *
 * diaspora_decode($importer,$xml,$format)
 *   array $importer -> from user table
 *   string $xml -> urldecoded Diaspora salmon
 *   string $format 'legacy', 'salmon', or 'json' 
 *
 * Returns array
 * 'message' -> decoded Diaspora XML message
 * 'author' -> author diaspora handle
 * 'key' -> author public key (converted to pkcs#8)
 *
 * Author and key are used elsewhere to save a lookup for verifying replies and likes
 */
function diaspora_decode($importer, $xml, $format)
{
    $public = false;
    if ($format === 'json') {
        $json = json_decode($xml, true);
        if ($json['aes_key']) {
            $key_bundle = '';
            $result = openssl_private_decrypt(base64_decode($json['aes_key']), $key_bundle, $importer['channel_prvkey']);
            if (!$result) {
                logger('decrypting key_bundle for ' . $importer['channel_address'] . ' failed: ' . $json['aes_key'], LOGGER_NORMAL, LOG_ERR);
                http_status_exit(400);
            }
            $jkey = json_decode($key_bundle, true);
            $xml = AES256CBC_decrypt(base64_decode($json['encrypted_magic_envelope']), base64_decode($jkey['key']), base64_decode($jkey['iv']));
            if (!$xml) {
                logger('decrypting magic_envelope for ' . $importer['channel_address'] . ' failed: ' . $json['aes_key'], LOGGER_NORMAL, LOG_ERR);
                http_status_exit(400);
            }
        }
    }
    $basedom = parse_xml_string($xml);
    if ($format !== 'legacy') {
        $children = $basedom->children('http://salmon-protocol.org/ns/magic-env');
        $public = true;
        $author_link = str_replace('acct:', '', base64url_decode($children->key_id));
        /**
        			SimpleXMLElement Object
        			(
        			    [encoding] => base64url
        			    [alg] => RSA-SHA256
        			    [data] => ((base64url-encoded payload message))
        			    [sig] => ((the RSA-SHA256 signature of the above data))
        			    [key_id] => ((base64url-encoded Alice's diaspora ID))
        			)
        		**/
    } else {
        $children = $basedom->children('https://joindiaspora.com/protocol');
        if ($children->header) {
            $public = true;
            $author_link = str_replace('acct:', '', $children->header->author_id);
        } else {
            $encrypted_header = json_decode(base64_decode($children->encrypted_header));
            $encrypted_aes_key_bundle = base64_decode($encrypted_header->aes_key);
            $ciphertext = base64_decode($encrypted_header->ciphertext);
            $outer_key_bundle = '';
            openssl_private_decrypt($encrypted_aes_key_bundle, $outer_key_bundle, $importer['channel_prvkey']);
            $j_outer_key_bundle = json_decode($outer_key_bundle);
            $outer_iv = base64_decode($j_outer_key_bundle->iv);
            $outer_key = base64_decode($j_outer_key_bundle->key);
            $decrypted = AES256CBC_decrypt($ciphertext, $outer_key, $outer_iv);
            /**
             * $decrypted now contains something like
             *
             *  <decrypted_header>
             *	 <iv>8e+G2+ET8l5BPuW0sVTnQw==</iv>
             *	 <aes_key>UvSMb4puPeB14STkcDWq+4QE302Edu15oaprAQSkLKU=</aes_key>
             ***** OBSOLETE
             *	 <author>
             *	   <name>Ryan Hughes</name>
             *	   <uri>acct:galaxor@diaspora.pirateship.org</uri>
             *	 </author>
             ***** CURRENT/LEGACY
             *	 <author_id>galaxor@diaspora.pirateship.org</author_id>
             ***** END DIFFS
             *  </decrypted_header>
             */
            logger('decrypted: ' . $decrypted, LOGGER_DATA);
            $idom = parse_xml_string($decrypted, false);
            $inner_iv = base64_decode($idom->iv);
            $inner_aes_key = base64_decode($idom->aes_key);
            $author_link = str_replace('acct:', '', $idom->author_id);
        }
    }
    $dom = $basedom->children(NAMESPACE_SALMON_ME);
    // figure out where in the DOM tree our data is hiding
    if ($dom->provenance->data) {
        $base = $dom->provenance;
    } elseif ($dom->env->data) {
        $base = $dom->env;
    } elseif ($dom->data) {
        $base = $dom;
    }
    if (!$base) {
        logger('mod-diaspora: unable to locate salmon data in xml ', LOGGER_NORMAL, LOG_ERR);
        http_status_exit(400);
    }
    // Stash the signature away for now. We have to find their key or it won't be good for anything.
    $signature = base64url_decode($base->sig);
    // unpack the  data
    // strip whitespace so our data element will return to one big base64 blob
    $data = str_replace(array(" ", "\t", "\r", "\n"), array("", "", "", ""), $base->data);
    // stash away some other stuff for later
    $type = $base->data[0]->attributes()->type[0];
    $keyhash = $base->sig[0]->attributes()->keyhash[0];
    $encoding = $base->encoding;
    $alg = $base->alg;
    $signed_data = $data . '.' . base64url_encode($type, false) . '.' . base64url_encode($encoding, false) . '.' . base64url_encode($alg, false);
    // decode the data
    $data = base64url_decode($data);
    if ($format === 'legacy' && !$public) {
        // Decode the encrypted blob
        $final_msg = AES256CBC_decrypt(base64_decode($data), $inner_aes_key, $inner_iv);
    } else {
        $final_msg = $data;
    }
    if (!$author_link) {
        logger('mod-diaspora: Could not retrieve author URI.');
        http_status_exit(400);
    }
    // Once we have the author URI, go to the web and try to find their public key
    // (first this will look it up locally if it is in the fcontact cache)
    // This will also convert diaspora public key from pkcs#1 to pkcs#8
    logger('mod-diaspora: Fetching key for ' . $author_link);
    $key = get_diaspora_key($author_link);
    if (!$key) {
        logger('mod-diaspora: Could not retrieve author key.', LOGGER_NORMAL, LOG_WARNING);
        http_status_exit(400);
    }
    $verify = rsa_verify($signed_data, $signature, $key);
    if (!$verify) {
        logger('mod-diaspora: Message did not verify. Discarding.', LOGGER_NORMAL, LOG_ERR);
        http_status_exit(400);
    }
    logger('mod-diaspora: Message verified.');
    return array('message' => $final_msg, 'author' => $author_link, 'key' => $key);
}
Example #10
0
function ping_init(&$a)
{
    header("Content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\t\t<result>";
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if (local_user()) {
        // Different login session than the page that is calling us.
        if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
            echo '<invalid>1</invalid></result>';
            killme();
        }
        $notifs = ping_get_notifications(local_user());
        $sysnotify = 0;
        // we will update this in a moment
        $tags = array();
        $comments = array();
        $likes = array();
        $dislikes = array();
        $friends = array();
        $posts = array();
        $home = 0;
        $network = 0;
        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,\n\t\t\t\t`item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0\n\t\t\t\tORDER BY `item`.`created` DESC", intval(local_user()));
        if (count($r)) {
            $arr = array('items' => $r);
            call_hooks('network_ping', $arr);
            foreach ($r as $it) {
                if ($it['wall']) {
                    $home++;
                } else {
                    $network++;
                }
                switch ($it['verb']) {
                    case ACTIVITY_TAG:
                        $obj = parse_xml_string($xmlhead . $it['object']);
                        $it['tname'] = $obj->content;
                        $tags[] = $it;
                        break;
                    case ACTIVITY_LIKE:
                        $likes[] = $it;
                        break;
                    case ACTIVITY_DISLIKE:
                        $dislikes[] = $it;
                        break;
                    case ACTIVITY_FRIEND:
                        $obj = parse_xml_string($xmlhead . $it['object']);
                        $it['fname'] = $obj->title;
                        $friends[] = $it;
                        break;
                    default:
                        if ($it['parent'] != $it['id']) {
                            $comments[] = $it;
                        } else {
                            if (!$it['wall']) {
                                $posts[] = $it;
                            }
                        }
                }
            }
        }
        $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,\n\t\t\t`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`\n\t\t\tFROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0", intval(local_user()));
        $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,\n\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`photo`\n\t\t\tFROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0", intval(local_user()));
        $intro = count($intros1) + count($intros2);
        $intros = $intros1 + $intros2;
        $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
        $mails = q("SELECT * FROM `mail`\n\t\t\tWHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", intval(local_user()), dbesc($myurl));
        $mail = count($mails);
        if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
            $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
            if ($regs) {
                $register = $regs[0]['total'];
            }
        } else {
            $register = "0";
        }
        $all_events = 0;
        $all_events_today = 0;
        $events = 0;
        $events_today = 0;
        $birthdays = 0;
        $birthdays_today = 0;
        $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`\n\t\t\tWHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0\n\t\t\tORDER BY `start` ASC ", intval(local_user()), dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')), dbesc(datetime_convert('UTC', 'UTC', 'now')));
        if ($ev && count($ev)) {
            $all_events = intval($ev[0]['total']);
            if ($all_events) {
                $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
                foreach ($ev as $x) {
                    $bd = false;
                    if ($x['type'] === 'birthday') {
                        $birthdays++;
                        $bd = true;
                    } else {
                        $events++;
                    }
                    if (datetime_convert('UTC', intval($x['adjust']) ? $a->timezone : 'UTC', $x['start'], 'Y-m-d') === $str_now) {
                        $all_events_today++;
                        if ($bd) {
                            $birthdays_today++;
                        } else {
                            $events_today++;
                        }
                    }
                }
            }
        }
        /**
         * return xml from notification array
         *
         * @param array $n Notification array:
         *		'href' => notification link
         *		'name' => subject name
         *		'url' => subject url
         *		'photo' => subject photo
         *		'date' => notification date
         *		'seen' => bool true/false
         *		'message' => notification message. "{0}" will be replaced by subject name
         **/
        function xmlize($n)
        {
            $n['photo'] = proxy_url($n['photo']);
            $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8");
            $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8");
            // Are the nofications calles from the regular process or via the friendica app?
            $regularnotifications = (intval($_GET['uid']) and intval($_GET['_']));
            $a = get_app();
            if ($a->is_friendica_app() or !$regularnotifications) {
                $n['message'] = str_replace("{0}", $n['name'], $n['message']);
            }
            $local_time = datetime_convert('UTC', date_default_timezone_get(), $n['date']);
            call_hooks('ping_xmlize', $n);
            $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" timestamp="%s" >%s</note>' . "\n";
            return sprintf($notsxml, xmlify($n['href']), xmlify($n['name']), xmlify($n['url']), xmlify($n['photo']), xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)), xmlify($n['message']));
        }
        echo "<intro>{$intro}</intro>\n\t\t\t\t<mail>{$mail}</mail>\n\t\t\t\t<net>{$network}</net>\n\t\t\t\t<home>{$home}</home>\r\n";
        if ($register != 0) {
            echo "<register>{$register}</register>";
        }
        echo "<all-events>{$all_events}</all-events>\n\t\t\t<all-events-today>{$all_events_today}</all-events-today>\n\t\t\t<events>{$events}</events>\n\t\t\t<events-today>{$events_today}</events-today>\n\t\t\t<birthdays>{$birthdays}</birthdays>\n\t\t\t<birthdays-today>{$birthdays_today}</birthdays-today>\r\n";
        if (count($notifs) && !$sysnotify) {
            foreach ($notifs as $zz) {
                if ($zz['seen'] == 0) {
                    $sysnotify++;
                }
            }
        }
        echo '	<notif count="' . ($sysnotify + $intro + $mail + $register) . '">';
        // merge all notification types in one array
        if ($intro > 0) {
            foreach ($intros as $i) {
                $n = array('href' => $a->get_baseurl() . '/notifications/intros/' . $i['id'], 'name' => $i['name'], 'url' => $i['url'], 'photo' => $i['photo'], 'date' => $i['datetime'], 'seen' => false, 'message' => t("{0} wants to be your friend"));
                $notifs[] = $n;
            }
        }
        if ($mail > 0) {
            foreach ($mails as $i) {
                $n = array('href' => $a->get_baseurl() . '/message/' . $i['id'], 'name' => $i['from-name'], 'url' => $i['from-url'], 'photo' => $i['from-photo'], 'date' => $i['created'], 'seen' => false, 'message' => t("{0} sent you a message"));
                $notifs[] = $n;
            }
        }
        if ($register > 0) {
            foreach ($regs as $i) {
                $n = array('href' => $a->get_baseurl() . '/admin/users/', 'name' => $i['name'], 'url' => $i['url'], 'photo' => $i['micro'], 'date' => $i['created'], 'seen' => false, 'message' => t("{0} requested registration"));
                $notifs[] = $n;
            }
        }
        // sort notifications by $[]['date']
        $sort_function = function ($a, $b) {
            $adate = date($a['date']);
            $bdate = date($b['date']);
            if ($adate == $bdate) {
                return 0;
            }
            return $adate < $bdate ? 1 : -1;
        };
        usort($notifs, $sort_function);
        if (count($notifs)) {
            foreach ($notifs as $n) {
                echo xmlize($n);
            }
        }
        echo "  </notif>";
    }
    echo " <sysmsgs>";
    if (x($_SESSION, 'sysmsg')) {
        foreach ($_SESSION['sysmsg'] as $m) {
            echo "<notice>" . xmlify($m) . "</notice>";
        }
        unset($_SESSION['sysmsg']);
    }
    if (x($_SESSION, 'sysmsg_info')) {
        foreach ($_SESSION['sysmsg_info'] as $m) {
            echo "<info>" . xmlify($m) . "</info>";
        }
        unset($_SESSION['sysmsg_info']);
    }
    echo " </sysmsgs>";
    echo "</result>\n\t";
    killme();
}
Example #11
0
function local_delivery($importer, $data)
{
    $a = get_app();
    if ($importer['readonly']) {
        // We aren't receiving stuff from this person. But we will quietly ignore them
        // rather than a blatant "go away" message.
        logger('local_delivery: ignoring');
        return 0;
        //NOTREACHED
    }
    // Consume notification feed. This may differ from consuming a public feed in several ways
    // - might contain email or friend suggestions
    // - might contain remote followup to our message
    //		- in which case we need to accept it and then notify other conversants
    // - we may need to send various email notifications
    $feed = new SimplePie();
    $feed->set_raw_data($data);
    $feed->enable_order_by_date(false);
    $feed->init();
    /*
    	// Currently unsupported - needs a lot of work
    	$reloc = $feed->get_feed_tags( NAMESPACE_DFRN, 'relocate' );
    	if(isset($reloc[0]['child'][NAMESPACE_DFRN])) {
    		$base = $reloc[0]['child'][NAMESPACE_DFRN];
    		$newloc = array();
    		$newloc['uid'] = $importer['importer_uid'];
    		$newloc['cid'] = $importer['id'];
    		$newloc['name'] = notags(unxmlify($base['name'][0]['data']));
    		$newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
    		$newloc['url'] = notags(unxmlify($base['url'][0]['data']));
    		$newloc['request'] = notags(unxmlify($base['request'][0]['data']));
    		$newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
    		$newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
    		$newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
    		$newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data']));
    		$newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
    		$newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));
    		
    		// TODO
    		// merge with current record, current contents have priority
    		// update record, set url-updated
    		// update profile photos
    		// schedule a scan?
    
    	}
    */
    // handle friend suggestion notification
    $sugg = $feed->get_feed_tags(NAMESPACE_DFRN, 'suggest');
    if (isset($sugg[0]['child'][NAMESPACE_DFRN])) {
        $base = $sugg[0]['child'][NAMESPACE_DFRN];
        $fsugg = array();
        $fsugg['uid'] = $importer['importer_uid'];
        $fsugg['cid'] = $importer['id'];
        $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
        $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
        $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
        $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
        $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
        // Does our member already have a friend matching this description?
        $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1", dbesc($fsugg['name']), dbesc(normalise_link($fsugg['url'])), intval($fsugg['uid']));
        if (count($r)) {
            return 0;
        }
        // Do we already have an fcontact record for this person?
        $fid = 0;
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
            // OK, we do. Do we already have an introduction for this person ?
            $r = q("select id from intro where uid = %d and fid = %d limit 1", intval($fsugg['uid']), intval($fid));
            if (count($r)) {
                return 0;
            }
        }
        if (!$fid) {
            $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ", dbesc($fsugg['name']), dbesc($fsugg['url']), dbesc($fsugg['photo']), dbesc($fsugg['request']));
        }
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
        } else {
            return 0;
        }
        $hash = random_string();
        $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )\n\t\t\tVALUES( %d, %d, %d, '%s', '%s', '%s', %d )", intval($fsugg['uid']), intval($fid), intval($fsugg['cid']), dbesc($fsugg['body']), dbesc($hash), dbesc(datetime_convert()), intval(0));
        notification(array('type' => NOTIFY_SUGGEST, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $fsugg, 'link' => $a->get_baseurl() . '/notifications/intros', 'source_name' => $importer['name'], 'source_link' => $importer['url'], 'source_photo' => $importer['photo'], 'verb' => ACTIVITY_REQ_FRIEND, 'otype' => 'intro'));
        return 0;
    }
    $ismail = false;
    $rawmail = $feed->get_feed_tags(NAMESPACE_DFRN, 'mail');
    if (isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
        logger('local_delivery: private message received');
        $ismail = true;
        $base = $rawmail[0]['child'][NAMESPACE_DFRN];
        $msg = array();
        $msg['uid'] = $importer['importer_uid'];
        $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
        $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
        $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
        $msg['contact-id'] = $importer['id'];
        $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
        $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
        $msg['seen'] = 0;
        $msg['replied'] = 0;
        $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
        $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
        $msg['created'] = datetime_convert(notags(unxmlify('UTC', 'UTC', $base['sentdate'][0]['data'])));
        dbesc_array($msg);
        $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) . "`) VALUES ('" . implode("', '", array_values($msg)) . "')");
        // send notifications.
        require_once 'include/enotify.php';
        $notif_params = array('type' => NOTIFY_MAIL, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $msg, 'source_name' => $msg['from-name'], 'source_link' => $importer['url'], 'source_photo' => $importer['thumb'], 'verb' => ACTIVITY_POST, 'otype' => 'mail');
        notification($notif_params);
        return 0;
        // NOTREACHED
    }
    $community_page = 0;
    $rawtags = $feed->get_feed_tags(NAMESPACE_DFRN, 'community');
    if ($rawtags) {
        $community_page = intval($rawtags[0]['data']);
    }
    if (intval($importer['forum']) != $community_page) {
        q("update contact set forum = %d where id = %d limit 1", intval($community_page), intval($importer['id']));
        $importer['forum'] = (string) $community_page;
    }
    logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
    // process any deleted entries
    $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
    if (is_array($del_entries) && count($del_entries)) {
        foreach ($del_entries as $dentry) {
            $deleted = false;
            if (isset($dentry['attribs']['']['ref'])) {
                $uri = $dentry['attribs']['']['ref'];
                $deleted = true;
                if (isset($dentry['attribs']['']['when'])) {
                    $when = $dentry['attribs']['']['when'];
                    $when = datetime_convert('UTC', 'UTC', $when, 'Y-m-d H:i:s');
                } else {
                    $when = datetime_convert('UTC', 'UTC', 'now', 'Y-m-d H:i:s');
                }
            }
            if ($deleted) {
                $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`\n\t\t\t\t\tWHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d AND NOT `item`.`file` LIKE '%%[%%' LIMIT 1", dbesc($uri), intval($importer['importer_uid']), intval($importer['id']));
                if (count($r)) {
                    $item = $r[0];
                    if ($item['deleted']) {
                        continue;
                    }
                    logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
                    if ($item['verb'] === ACTIVITY_TAG && $item['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                        $xo = parse_xml_string($item['object'], false);
                        $xt = parse_xml_string($item['target'], false);
                        if ($xt->type === ACTIVITY_OBJ_NOTE) {
                            $i = q("select * from `item` where uri = '%s' and uid = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                            if (count($i)) {
                                // For tags, the owner cannot remove the tag on the author's copy of the post.
                                $owner_remove = $item['contact-id'] == $i[0]['contact-id'] ? true : false;
                                $author_remove = $item['origin'] && $item['self'] ? true : false;
                                $author_copy = $item['origin'] ? true : false;
                                if ($owner_remove && $author_copy) {
                                    continue;
                                }
                                if ($author_remove || $owner_remove) {
                                    $tags = explode(',', $i[0]['tag']);
                                    $newtags = array();
                                    if (count($tags)) {
                                        foreach ($tags as $tag) {
                                            if (trim($tag) !== trim($xo->body)) {
                                                $newtags[] = trim($tag);
                                            }
                                        }
                                    }
                                    q("update item set tag = '%s' where id = %d limit 1", dbesc(implode(',', $newtags)), intval($i[0]['id']));
                                }
                            }
                        }
                    }
                    if ($item['uri'] == $item['parent-uri']) {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'\n\t\t\t\t\t\t\tWHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($when), dbesc(datetime_convert()), dbesc($item['uri']), intval($importer['importer_uid']));
                    } else {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' \n\t\t\t\t\t\t\tWHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($when), dbesc(datetime_convert()), dbesc($uri), intval($importer['importer_uid']));
                        if ($item['last-child']) {
                            // ensure that last-child is set in case the comment that had it just got wiped.
                            q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", dbesc(datetime_convert()), dbesc($item['parent-uri']), intval($item['uid']));
                            // who is the last child now?
                            $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d\n\t\t\t\t\t\t\t\tORDER BY `created` DESC LIMIT 1", dbesc($item['parent-uri']), intval($importer['importer_uid']));
                            if (count($r)) {
                                q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1", intval($r[0]['id']));
                            }
                        }
                    }
                }
            }
        }
    }
    foreach ($feed->get_items() as $item) {
        $is_reply = false;
        $item_id = $item->get_id();
        $rawthread = $item->get_item_tags(NAMESPACE_THREAD, 'in-reply-to');
        if (isset($rawthread[0]['attribs']['']['ref'])) {
            $is_reply = true;
            $parent_uri = $rawthread[0]['attribs']['']['ref'];
        }
        if ($is_reply) {
            $community = false;
            if ($importer['page-flags'] == PAGE_COMMUNITY || $importer['page-flags'] == PAGE_PRVGROUP) {
                $sql_extra = '';
                $community = true;
                logger('local_delivery: possible community reply');
            } else {
                $sql_extra = " and contact.self = 1 and item.wall = 1 ";
            }
            // was the top-level post for this reply written by somebody on this site?
            // Specifically, the recipient?
            $is_a_remote_comment = false;
            $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, `item`.`forum_mode`,`item`.`origin`,`item`.`wall`, \n\t\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` \n\t\t\t\tLEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` \n\t\t\t\tWHERE `item`.`uri` = '%s' AND `item`.`parent-uri` = '%s'\n\t\t\t\tAND `item`.`uid` = %d \n\t\t\t\t{$sql_extra}\n\t\t\t\tLIMIT 1", dbesc($parent_uri), dbesc($parent_uri), intval($importer['importer_uid']));
            if ($r && count($r)) {
                $is_a_remote_comment = true;
            }
            // Does this have the characteristics of a community or private group comment?
            // If it's a reply to a wall post on a community/prvgroup page it's a
            // valid community comment. Also forum_mode makes it valid for sure.
            // If neither, it's not.
            if ($is_a_remote_comment && $community) {
                if (!$r[0]['forum_mode'] && !$r[0]['wall']) {
                    $is_a_remote_comment = false;
                    logger('local_delivery: not a community reply');
                }
            }
            if ($is_a_remote_comment) {
                logger('local_delivery: received remote comment');
                $is_like = false;
                // remote reply to our post. Import and then notify everybody else.
                $datarray = get_atom_elements($feed, $item);
                $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body`  FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
                // Update content if 'updated' changes
                if (count($r)) {
                    $iid = $r[0]['id'];
                    if (x($datarray, 'edited') !== false && datetime_convert('UTC', 'UTC', $datarray['edited']) !== $r[0]['edited']) {
                        logger('received updated comment', LOGGER_DEBUG);
                        $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc($item_id), intval($importer['importer_uid']));
                        proc_run('php', "include/notifier.php", "comment-import", $iid);
                    }
                    continue;
                }
                // TODO: make this next part work against both delivery threads of a community post
                //				if((! link_compare($datarray['author-link'],$importer['url'])) && (! $community)) {
                //					logger('local_delivery: received relay claiming to be from ' . $importer['url'] . ' however comment author url is ' . $datarray['author-link'] );
                // they won't know what to do so don't report an error. Just quietly die.
                //					return 0;
                //				}
                // our user with $importer['importer_uid'] is the owner
                $own = q("select name,url,thumb from contact where uid = %d and self = 1 limit 1", intval($importer['importer_uid']));
                $datarray['type'] = 'remote-comment';
                $datarray['wall'] = 1;
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['owner-name'] = $own[0]['name'];
                $datarray['owner-link'] = $own[0]['url'];
                $datarray['owner-avatar'] = $own[0]['thumb'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] === ACTIVITY_LIKE || $datarray['verb'] === ACTIVITY_DISLIKE) {
                    $is_like = true;
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                    $datarray['last-child'] = 0;
                    // only one like or dislike per person
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']));
                    if ($r && count($r)) {
                        continue;
                    }
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE && $xt->id) {
                        // fetch the parent item
                        $tagp = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                        if (!count($tagp)) {
                            continue;
                        }
                        // extract tag, if not duplicate, and this user allows tags, add to parent item
                        if ($xo->id && $xo->content) {
                            $newtag = '#[url=' . $xo->id . ']' . $xo->content . '[/url]';
                            if (!stristr($tagp[0]['tag'], $newtag)) {
                                $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1", intval($importer['importer_uid']));
                                if (count($i) && !intval($i[0]['blocktags'])) {
                                    q("UPDATE item SET tag = '%s', `edited` = '%s' WHERE id = %d LIMIT 1", dbesc($tagp[0]['tag'] . (strlen($tagp[0]['tag']) ? ',' : '') . $newtag), intval($tagp[0]['id']), dbesc(datetime_convert()));
                                }
                            }
                        }
                    }
                }
                // 				if($community) {
                //					$newtag = '@[url=' . $a->get_baseurl() . '/profile/' . $importer['nickname'] . ']' . $importer['username'] . '[/url]';
                //					if(! stristr($datarray['tag'],$newtag)) {
                //						if(strlen($datarray['tag']))
                //							$datarray['tag'] .= ',';
                //						$datarray['tag'] .= $newtag;
                //					}
                //				}
                $posted_id = item_store($datarray);
                $parent = 0;
                if ($posted_id) {
                    $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($posted_id), intval($importer['importer_uid']));
                    if (count($r)) {
                        $parent = $r[0]['parent'];
                    }
                    if (!$is_like) {
                        $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($r[0]['parent']));
                        $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($posted_id));
                    }
                    if ($posted_id && $parent) {
                        proc_run('php', "include/notifier.php", "comment-import", "{$posted_id}");
                        if (!$is_like && !$importer['self']) {
                            require_once 'include/enotify.php';
                            notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id, 'source_name' => stripslashes($datarray['author-name']), 'source_link' => $datarray['author-link'], 'source_photo' => link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $parent));
                        }
                    }
                    return 0;
                    // NOTREACHED
                }
            } else {
                // regular comment that is part of this total conversation. Have we seen it? If not, import it.
                $item_id = $item->get_id();
                $datarray = get_atom_elements($feed, $item);
                $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
                // Update content if 'updated' changes
                if (count($r)) {
                    if (x($datarray, 'edited') !== false && datetime_convert('UTC', 'UTC', $datarray['edited']) !== $r[0]['edited']) {
                        $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc($item_id), intval($importer['importer_uid']));
                    }
                    // update last-child if it changes
                    $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                    if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                        $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc(datetime_convert()), dbesc($parent_uri), intval($importer['importer_uid']));
                        $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                    }
                    continue;
                }
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] == ACTIVITY_LIKE || $datarray['verb'] == ACTIVITY_DISLIKE) {
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                    // only one like or dislike per person
                    $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 limit 1", intval($datarray['uid']), intval($datarray['contact-id']), dbesc($datarray['verb']));
                    if ($r && count($r)) {
                        continue;
                    }
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE) {
                        $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                        if (!count($r)) {
                            continue;
                        }
                        // extract tag, if not duplicate, add to parent item
                        if ($xo->content) {
                            if (!stristr($r[0]['tag'], trim($xo->content))) {
                                q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1", dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']' . $xo->content . '[/url]'), intval($r[0]['id']));
                            }
                        }
                    }
                }
                $posted_id = item_store($datarray);
                // find out if our user is involved in this conversation and wants to be notified.
                if (!x($datarray['type']) || $datarray['type'] != 'activity') {
                    $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0", dbesc($parent_uri), intval($importer['importer_uid']));
                    if (count($myconv)) {
                        $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
                        // first make sure this isn't our own post coming back to us from a wall-to-wall event
                        if (!link_compare($datarray['author-link'], $importer_url)) {
                            foreach ($myconv as $conv) {
                                // now if we find a match, it means we're in this conversation
                                if (!link_compare($conv['author-link'], $importer_url)) {
                                    continue;
                                }
                                require_once 'include/enotify.php';
                                $conv_parent = $conv['parent'];
                                notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $importer['notify-flags'], 'language' => $importer['language'], 'to_name' => $importer['username'], 'to_email' => $importer['email'], 'uid' => $importer['importer_uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id, 'source_name' => stripslashes($datarray['author-name']), 'source_link' => $datarray['author-link'], 'source_photo' => link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $conv_parent));
                                // only send one notification
                                break;
                            }
                        }
                    }
                }
                continue;
            }
        } else {
            // Head post of a conversation. Have we seen it? If not, import it.
            $item_id = $item->get_id();
            $datarray = get_atom_elements($feed, $item);
            if (x($datarray, 'object-type') && $datarray['object-type'] === ACTIVITY_OBJ_EVENT) {
                $ev = bbtoevent($datarray['body']);
                if (x($ev, 'desc') && x($ev, 'start')) {
                    $ev['cid'] = $importer['id'];
                    $ev['uid'] = $importer['uid'];
                    $ev['uri'] = $item_id;
                    $ev['edited'] = $datarray['edited'];
                    $ev['private'] = $datarray['private'];
                    $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['uid']));
                    if (count($r)) {
                        $ev['id'] = $r[0]['id'];
                    }
                    $xyz = event_store($ev);
                    continue;
                }
            }
            $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
            // Update content if 'updated' changes
            if (count($r)) {
                if (x($datarray, 'edited') !== false && datetime_convert('UTC', 'UTC', $datarray['edited']) !== $r[0]['edited']) {
                    $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc($item_id), intval($importer['importer_uid']));
                }
                // update last-child if it changes
                $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                    $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                }
                continue;
            }
            // This is my contact on another system, but it's really me.
            // Turn this into a wall post.
            if ($importer['remote_self']) {
                $datarray['wall'] = 1;
            }
            $datarray['parent-uri'] = $item_id;
            $datarray['uid'] = $importer['importer_uid'];
            $datarray['contact-id'] = $importer['id'];
            if (!link_compare($datarray['owner-link'], $contact['url'])) {
                // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery,
                // but otherwise there's a possible data mixup on the sender's system.
                // the tgroup delivery code called from item_store will correct it if it's a forum,
                // but we're going to unconditionally correct it here so that the post will always be owned by our contact.
                logger('local_delivery: Correcting item owner.', LOGGER_DEBUG);
                $datarray['owner-name'] = $importer['senderName'];
                $datarray['owner-link'] = $importer['url'];
                $datarray['owner-avatar'] = $importer['thumb'];
            }
            $r = item_store($datarray);
            continue;
        }
    }
    return 0;
    // NOTREACHED
}
Example #12
0
function onepoll_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    require_once 'library/simplepie/simplepie.inc';
    require_once 'include/items.php';
    require_once 'include/Contact.php';
    require_once 'include/email.php';
    require_once 'include/socgraph.php';
    require_once 'include/pidfile.php';
    require_once 'include/queue_fn.php';
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    logger('onepoll: start');
    $manual_id = 0;
    $generation = 0;
    $hub_update = false;
    $force = false;
    $restart = false;
    if ($argc > 1 && intval($argv[1])) {
        $contact_id = intval($argv[1]);
    }
    if (!$contact_id) {
        logger('onepoll: no contact');
        return;
    }
    $lockpath = get_lockpath();
    if ($lockpath != '') {
        $pidfile = new pidfile($lockpath, 'onepoll' . $contact_id);
        if ($pidfile->is_already_running()) {
            logger("onepoll: Already running for contact " . $contact_id);
            if ($pidfile->running_time() > 9 * 60) {
                $pidfile->kill();
                logger("killed stale process");
            }
            exit;
        }
    }
    $d = datetime_convert();
    // Only poll from those with suitable relationships,
    // and which have a polling address and ignore Diaspora since
    // we are unable to match those posts with a Diaspora GUID and prevent duplicates.
    $contacts = q("SELECT `contact`.* FROM `contact`\n\t\tWHERE ( `rel` = %d OR `rel` = %d ) AND `poll` != ''\n\t\tAND NOT `network` IN ( '%s', '%s', '%s' )\n\t\tAND `contact`.`id` = %d\n\t\tAND `self` = 0 AND `contact`.`blocked` = 0 AND `contact`.`readonly` = 0\n\t\tAND `contact`.`archive` = 0 LIMIT 1", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_FACEBOOK), dbesc(NETWORK_PUMPIO), intval($contact_id));
    if (!count($contacts)) {
        return;
    }
    $contact = $contacts[0];
    $xml = false;
    $t = $contact['last-update'];
    if ($contact['subhub']) {
        $poll_interval = get_config('system', 'pushpoll_frequency');
        $contact['priority'] = $poll_interval !== false ? intval($poll_interval) : 3;
        $hub_update = false;
        if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', $t . " + 1 day")) {
            $hub_update = true;
        }
    } else {
        $hub_update = false;
    }
    $importer_uid = $contact['uid'];
    $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `contact`.`uid` = `user`.`uid` WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval($importer_uid));
    if (!count($r)) {
        return;
    }
    $importer = $r[0];
    logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['name']}, CONTACT: {$contact['name']}");
    $last_update = $contact['last-update'] === '0000-00-00 00:00:00' ? datetime_convert('UTC', 'UTC', 'now - 7 days', ATOM_TIME) : datetime_convert('UTC', 'UTC', $contact['last-update'], ATOM_TIME);
    if ($contact['network'] === NETWORK_DFRN) {
        $idtosend = $orig_id = $contact['dfrn-id'] ? $contact['dfrn-id'] : $contact['issued-id'];
        if (intval($contact['duplex']) && $contact['dfrn-id']) {
            $idtosend = '0:' . $orig_id;
        }
        if (intval($contact['duplex']) && $contact['issued-id']) {
            $idtosend = '1:' . $orig_id;
        }
        // they have permission to write to us. We already filtered this in the contact query.
        $perm = 'rw';
        // But this may be our first communication, so set the writable flag if it isn't set already.
        if (!intval($contact['writable'])) {
            q("update contact set writable = 1 where id = %d", intval($contact['id']));
        }
        $url = $contact['poll'] . '?dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=data&last_update=' . $last_update . '&perm=' . $perm;
        $handshake_xml = fetch_url($url);
        $html_code = $a->get_curl_code();
        logger('onepoll: handshake with url ' . $url . ' returns xml: ' . $handshake_xml, LOGGER_DATA);
        if (!strlen($handshake_xml) || $html_code >= 400 || !$html_code) {
            logger("poller: {$url} appears to be dead - marking for death ");
            // dead connection - might be a transient event, or this might
            // mean the software was uninstalled or the domain expired.
            // Will keep trying for one month.
            mark_for_death($contact);
            // set the last-update so we don't keep polling
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        if (!strstr($handshake_xml, '<')) {
            logger('poller: response from ' . $url . ' did not contain XML.');
            mark_for_death($contact);
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        $res = parse_xml_string($handshake_xml);
        if (intval($res->status) == 1) {
            logger("poller: {$url} replied status 1 - marking for death ");
            // we may not be friends anymore. Will keep trying for one month.
            // set the last-update so we don't keep polling
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            mark_for_death($contact);
        } else {
            if ($contact['term-date'] != '0000-00-00 00:00:00') {
                logger("poller: {$url} back from the dead - removing mark for death");
                unmark_for_death($contact);
            }
        }
        if (intval($res->status) != 0 || !strlen($res->challenge) || !strlen($res->dfrn_id)) {
            return;
        }
        if ((double) $res->dfrn_version > 2.21 && $contact['poco'] == '') {
            q("update contact set poco = '%s' where id = %d", dbesc(str_replace('/profile/', '/poco/', $contact['url'])), intval($contact['id']));
        }
        $postvars = array();
        $sent_dfrn_id = hex2bin((string) $res->dfrn_id);
        $challenge = hex2bin((string) $res->challenge);
        $final_dfrn_id = '';
        if ($contact['duplex'] && strlen($contact['prvkey'])) {
            openssl_private_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['prvkey']);
            openssl_private_decrypt($challenge, $postvars['challenge'], $contact['prvkey']);
        } else {
            openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']);
            openssl_public_decrypt($challenge, $postvars['challenge'], $contact['pubkey']);
        }
        $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
        if (strpos($final_dfrn_id, ':') == 1) {
            $final_dfrn_id = substr($final_dfrn_id, 2);
        }
        if ($final_dfrn_id != $orig_id) {
            logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
            // did not decode properly - cannot trust this site
            return;
        }
        $postvars['dfrn_id'] = $idtosend;
        $postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
        $postvars['perm'] = 'rw';
        $xml = post_url($contact['poll'], $postvars);
    } elseif ($contact['network'] === NETWORK_OSTATUS || $contact['network'] === NETWORK_DIASPORA || $contact['network'] === NETWORK_FEED) {
        // Upgrading DB fields from an older Friendica version
        // Will only do this once per notify-enabled OStatus contact
        // or if relationship changes
        $stat_writeable = $contact['notify'] && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND) ? 1 : 0;
        if ($contact['network'] === NETWORK_OSTATUS && get_pconfig($importer_uid, 'system', 'ostatus_autofriend')) {
            $stat_writeable = 1;
        }
        if ($stat_writeable != $contact['writable']) {
            q("UPDATE `contact` SET `writable` = %d WHERE `id` = %d", intval($stat_writeable), intval($contact['id']));
        }
        // Are we allowed to import from this person?
        if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
            return;
        }
        $xml = fetch_url($contact['poll']);
    } elseif ($contact['network'] === NETWORK_MAIL || $contact['network'] === NETWORK_MAIL2) {
        logger("Mail: Fetching", LOGGER_DEBUG);
        $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
        if ($mail_disabled) {
            return;
        }
        logger("Mail: Enabled", LOGGER_DEBUG);
        $mbox = null;
        $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer_uid));
        $mailconf = q("SELECT * FROM `mailacct` WHERE `server` != '' AND `uid` = %d LIMIT 1", intval($importer_uid));
        if (count($x) && count($mailconf)) {
            $mailbox = construct_mailbox_name($mailconf[0]);
            $password = '';
            openssl_private_decrypt(hex2bin($mailconf[0]['pass']), $password, $x[0]['prvkey']);
            $mbox = email_connect($mailbox, $mailconf[0]['user'], $password);
            unset($password);
            logger("Mail: Connect to " . $mailconf[0]['user']);
            if ($mbox) {
                q("UPDATE `mailacct` SET `last_check` = '%s' WHERE `id` = %d AND `uid` = %d", dbesc(datetime_convert()), intval($mailconf[0]['id']), intval($importer_uid));
                logger("Mail: Connected to " . $mailconf[0]['user']);
            } else {
                logger("Mail: Connection error " . $mailconf[0]['user'] . " " . print_r(imap_errors()));
            }
        }
        if ($mbox) {
            $msgs = email_poll($mbox, $contact['addr']);
            if (count($msgs)) {
                logger("Mail: Parsing " . count($msgs) . " mails for " . $mailconf[0]['user'], LOGGER_DEBUG);
                $metas = email_msg_meta($mbox, implode(',', $msgs));
                if (count($metas) != count($msgs)) {
                    logger("onepoll: for " . $mailconf[0]['user'] . " there are " . count($msgs) . " messages but received " . count($metas) . " metas", LOGGER_DEBUG);
                } else {
                    $msgs = array_combine($msgs, $metas);
                    foreach ($msgs as $msg_uid => $meta) {
                        logger("Mail: Parsing mail " . $msg_uid, LOGGER_DATA);
                        $datarray = array();
                        $datarray['verb'] = ACTIVITY_POST;
                        $datarray['object-type'] = ACTIVITY_OBJ_NOTE;
                        //					$meta = email_msg_meta($mbox,$msg_uid);
                        //					$headers = email_msg_headers($mbox,$msg_uid);
                        $datarray['uri'] = msgid2iri(trim($meta->message_id, '<>'));
                        // Have we seen it before?
                        $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($importer_uid), dbesc($datarray['uri']));
                        if (count($r)) {
                            logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . " UID: " . $importer_uid . " URI: " . $datarray['uri'], LOGGER_DEBUG);
                            // Only delete when mails aren't automatically moved or deleted
                            if ($mailconf[0]['action'] != 1 and $mailconf[0]['action'] != 3) {
                                if ($meta->deleted && !$r[0]['deleted']) {
                                    q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($r[0]['id']));
                                }
                            }
                            switch ($mailconf[0]['action']) {
                                case 0:
                                    logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . ". Doing nothing.", LOGGER_DEBUG);
                                    break;
                                case 1:
                                    logger("Mail: Deleting " . $msg_uid . " for " . $mailconf[0]['user']);
                                    imap_delete($mbox, $msg_uid, FT_UID);
                                    break;
                                case 2:
                                    logger("Mail: Mark as seen " . $msg_uid . " for " . $mailconf[0]['user']);
                                    imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                    break;
                                case 3:
                                    logger("Mail: Moving " . $msg_uid . " to " . $mailconf[0]['movetofolder'] . " for " . $mailconf[0]['user']);
                                    imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                    if ($mailconf[0]['movetofolder'] != "") {
                                        imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
                                    }
                                    break;
                            }
                            continue;
                        }
                        // look for a 'references' or an 'in-reply-to' header and try to match with a parent item we have locally.
                        //					$raw_refs = ((x($headers,'references')) ? str_replace("\t",'',$headers['references']) : '');
                        $raw_refs = property_exists($meta, 'references') ? str_replace("\t", '', $meta->references) : '';
                        if (!trim($raw_refs)) {
                            $raw_refs = property_exists($meta, 'in_reply_to') ? str_replace("\t", '', $meta->in_reply_to) : '';
                        }
                        $raw_refs = trim($raw_refs);
                        // Don't allow a blank reference in $refs_arr
                        if ($raw_refs) {
                            $refs_arr = explode(' ', $raw_refs);
                            if (count($refs_arr)) {
                                for ($x = 0; $x < count($refs_arr); $x++) {
                                    $refs_arr[$x] = "'" . msgid2iri(str_replace(array('<', '>', ' '), array('', '', ''), dbesc($refs_arr[$x]))) . "'";
                                }
                            }
                            $qstr = implode(',', $refs_arr);
                            $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `uri` IN ( {$qstr} ) AND `uid` = %d LIMIT 1", intval($importer_uid));
                            if (count($r)) {
                                $datarray['parent-uri'] = $r[0]['parent-uri'];
                            }
                            // Set the parent as the top-level item
                            //							$datarray['parent-uri'] = $r[0]['uri'];
                        }
                        // Decoding the header
                        $subject = imap_mime_header_decode($meta->subject);
                        $datarray['title'] = "";
                        foreach ($subject as $subpart) {
                            if ($subpart->charset != "default") {
                                $datarray['title'] .= iconv($subpart->charset, 'UTF-8//IGNORE', $subpart->text);
                            } else {
                                $datarray['title'] .= $subpart->text;
                            }
                        }
                        $datarray['title'] = notags(trim($datarray['title']));
                        //$datarray['title'] = notags(trim($meta->subject));
                        $datarray['created'] = datetime_convert('UTC', 'UTC', $meta->date);
                        // Is it a reply?
                        $reply = (substr(strtolower($datarray['title']), 0, 3) == "re:" or substr(strtolower($datarray['title']), 0, 3) == "re-" or $raw_refs != "");
                        // Remove Reply-signs in the subject
                        $datarray['title'] = RemoveReply($datarray['title']);
                        // If it seems to be a reply but a header couldn't be found take the last message with matching subject
                        if (!x($datarray, 'parent-uri') and $reply) {
                            $r = q("SELECT `uri` , `parent-uri` FROM `item` WHERE `title` = \"%s\" AND `uid` = %d ORDER BY `created` DESC LIMIT 1", dbesc(protect_sprintf($datarray['title'])), intval($importer_uid));
                            if (count($r)) {
                                $datarray['parent-uri'] = $r[0]['parent-uri'];
                            }
                        }
                        if (!x($datarray, 'parent-uri')) {
                            $datarray['parent-uri'] = $datarray['uri'];
                        }
                        $r = email_get_msg($mbox, $msg_uid, $reply);
                        if (!$r) {
                            logger("Mail: can't fetch msg " . $msg_uid . " for " . $mailconf[0]['user']);
                            continue;
                        }
                        $datarray['body'] = escape_tags($r['body']);
                        $datarray['body'] = limit_body_size($datarray['body']);
                        logger("Mail: Importing " . $msg_uid . " for " . $mailconf[0]['user']);
                        // some mailing lists have the original author as 'from' - add this sender info to msg body.
                        // todo: adding a gravatar for the original author would be cool
                        if (!stristr($meta->from, $contact['addr'])) {
                            $from = imap_mime_header_decode($meta->from);
                            $fromdecoded = "";
                            foreach ($from as $frompart) {
                                if ($frompart->charset != "default") {
                                    $fromdecoded .= iconv($frompart->charset, 'UTF-8//IGNORE', $frompart->text);
                                } else {
                                    $fromdecoded .= $frompart->text;
                                }
                            }
                            $fromarr = imap_rfc822_parse_adrlist($fromdecoded, $a->get_hostname());
                            $frommail = $fromarr[0]->mailbox . "@" . $fromarr[0]->host;
                            if (isset($fromarr[0]->personal)) {
                                $fromname = $fromarr[0]->personal;
                            } else {
                                $fromname = $frommail;
                            }
                            //$datarray['body'] = "[b]".t('From: ') . escape_tags($fromdecoded) . "[/b]\n\n" . $datarray['body'];
                            $datarray['author-name'] = $fromname;
                            $datarray['author-link'] = "mailto:" . $frommail;
                            $datarray['author-avatar'] = $contact['photo'];
                            $datarray['owner-name'] = $contact['name'];
                            $datarray['owner-link'] = "mailto:" . $contact['addr'];
                            $datarray['owner-avatar'] = $contact['photo'];
                        } else {
                            $datarray['author-name'] = $contact['name'];
                            $datarray['author-link'] = 'mailbox';
                            $datarray['author-avatar'] = $contact['photo'];
                        }
                        $datarray['uid'] = $importer_uid;
                        $datarray['contact-id'] = $contact['id'];
                        if ($datarray['parent-uri'] === $datarray['uri']) {
                            $datarray['private'] = 1;
                        }
                        if ($contact['network'] === NETWORK_MAIL && !get_pconfig($importer_uid, 'system', 'allow_public_email_replies')) {
                            $datarray['private'] = 1;
                            $datarray['allow_cid'] = '<' . $contact['id'] . '>';
                        }
                        $stored_item = item_store($datarray);
                        q("UPDATE `item` SET `last-child` = 0 WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($datarray['parent-uri']), intval($importer_uid));
                        q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", intval($stored_item));
                        switch ($mailconf[0]['action']) {
                            case 0:
                                logger("Mail: Seen before " . $msg_uid . " for " . $mailconf[0]['user'] . ". Doing nothing.", LOGGER_DEBUG);
                                break;
                            case 1:
                                logger("Mail: Deleting " . $msg_uid . " for " . $mailconf[0]['user']);
                                imap_delete($mbox, $msg_uid, FT_UID);
                                break;
                            case 2:
                                logger("Mail: Mark as seen " . $msg_uid . " for " . $mailconf[0]['user']);
                                imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                break;
                            case 3:
                                logger("Mail: Moving " . $msg_uid . " to " . $mailconf[0]['movetofolder'] . " for " . $mailconf[0]['user']);
                                imap_setflag_full($mbox, $msg_uid, "\\Seen", ST_UID);
                                if ($mailconf[0]['movetofolder'] != "") {
                                    imap_mail_move($mbox, $msg_uid, $mailconf[0]['movetofolder'], FT_UID);
                                }
                                break;
                        }
                    }
                }
            } else {
                logger("Mail: no mails for " . $mailconf[0]['user']);
            }
            logger("Mail: closing connection for " . $mailconf[0]['user']);
            imap_close($mbox);
        }
    } elseif ($contact['network'] === NETWORK_FACEBOOK) {
        // This is picked up by the Facebook plugin on a cron hook.
        // Ignored here.
    } elseif ($contact['network'] === NETWORK_PUMPIO) {
        // This is picked up by the pump.io plugin on a cron hook.
        // Ignored here.
    }
    if ($xml) {
        logger('poller: received xml : ' . $xml, LOGGER_DATA);
        if (!strstr($xml, '<')) {
            logger('poller: post_handshake: response from ' . $url . ' did not contain XML.');
            $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact['id']));
            return;
        }
        consume_feed($xml, $importer, $contact, $hub, 1, 1);
        // do it twice. Ensures that children of parents which may be later in the stream aren't tossed
        consume_feed($xml, $importer, $contact, $hub, 1, 2);
        $hubmode = 'subscribe';
        if ($contact['network'] === NETWORK_DFRN || $contact['blocked'] || $contact['readonly']) {
            $hubmode = 'unsubscribe';
        }
        if (($contact['network'] === NETWORK_OSTATUS || $contact['network'] == NETWORK_FEED) && !$contact['hub-verify']) {
            $hub_update = true;
        }
        if (strlen($hub) && $hub_update && ($contact['rel'] != CONTACT_IS_FOLLOWER || $contact['network'] == NETWORK_FEED)) {
            logger('poller: hub ' . $hubmode . ' : ' . $hub . ' contact name : ' . $contact['name'] . ' local user : '******'name']);
            $hubs = explode(',', $hub);
            if (count($hubs)) {
                foreach ($hubs as $h) {
                    $h = trim($h);
                    if (!strlen($h)) {
                        continue;
                    }
                    subscribe_to_hub($h, $importer, $contact, $hubmode);
                }
            }
        }
    }
    $updated = datetime_convert();
    $r = q("UPDATE `contact` SET `last-update` = '%s', `success_update` = '%s' WHERE `id` = %d", dbesc($updated), dbesc($updated), intval($contact['id']));
    // load current friends if possible.
    if ($contact['poco']) {
        $r = q("SELECT count(*) as total from glink\n\t\t\twhere `cid` = %d and updated > UTC_TIMESTAMP() - INTERVAL 1 DAY", intval($contact['id']));
    }
    if (count($r)) {
        if (!$r[0]['total']) {
            poco_load($contact['id'], $importer_uid, 0, $contact['poco']);
        }
    }
    return;
}
Example #13
0
function dfrn_confirm_post(&$a, $handsfree = null)
{
    if (is_array($handsfree)) {
        /**
         * We were called directly from dfrn_request due to automatic friend acceptance.
         * Any $_POST parameters we may require are supplied in the $handsfree array.
         *
         */
        $node = $handsfree['node'];
        $a->interactive = false;
        // notice() becomes a no-op since nobody is there to see it
    } else {
        if ($a->argc > 1) {
            $node = $a->argv[1];
        }
    }
    /**
     *
     * Main entry point. Scenario 1. Our user received a friend request notification (perhaps
     * from another site) and clicked 'Approve'.
     * $POST['source_url'] is not set. If it is, it indicates Scenario 2.
     *
     * We may also have been called directly from dfrn_request ($handsfree != null) due to
     * this being a page type which supports automatic friend acceptance. That is also Scenario 1
     * since we are operating on behalf of our registered user to approve a friendship.
     *
     */
    if (!x($_POST, 'source_url')) {
        $uid = is_array($handsfree) ? $handsfree['uid'] : local_user();
        if (!$uid) {
            notice(t('Permission denied.') . EOL);
            return;
        }
        $user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
        if (!$user) {
            notice(t('Profile not found.') . EOL);
            return;
        }
        // These data elements may come from either the friend request notification form or $handsfree array.
        if (is_array($handsfree)) {
            logger('Confirm in handsfree mode');
            $dfrn_id = $handsfree['dfrn_id'];
            $intro_id = $handsfree['intro_id'];
            $duplex = $handsfree['duplex'];
            $hidden = array_key_exists('hidden', $handsfree) ? intval($handsfree['hidden']) : 0;
            $activity = array_key_exists('activity', $handsfree) ? intval($handsfree['activity']) : 0;
        } else {
            $dfrn_id = x($_POST, 'dfrn_id') ? notags(trim($_POST['dfrn_id'])) : "";
            $intro_id = x($_POST, 'intro_id') ? intval($_POST['intro_id']) : 0;
            $duplex = x($_POST, 'duplex') ? intval($_POST['duplex']) : 0;
            $cid = x($_POST, 'contact_id') ? intval($_POST['contact_id']) : 0;
            $hidden = x($_POST, 'hidden') ? intval($_POST['hidden']) : 0;
            $activity = x($_POST, 'activity') ? intval($_POST['activity']) : 0;
        }
        /**
         *
         * Ensure that dfrn_id has precedence when we go to find the contact record.
         * We only want to search based on contact id if there is no dfrn_id,
         * e.g. for OStatus network followers.
         *
         */
        if (strlen($dfrn_id)) {
            $cid = 0;
        }
        logger('Confirming request for dfrn_id (issued) ' . $dfrn_id);
        if ($cid) {
            logger('Confirming follower with contact_id: ' . $cid);
        }
        /**
         *
         * The other person will have been issued an ID when they first requested friendship.
         * Locate their record. At this time, their record will have both pending and blocked set to 1.
         * There won't be any dfrn_id if this is a network follower, so use the contact_id instead.
         *
         */
        $r = q("SELECT * FROM `contact` WHERE ( ( `issued-id` != '' AND `issued-id` = '%s' ) OR ( `id` = %d AND `id` != 0 ) ) AND `uid` = %d AND `duplex` = 0 LIMIT 1", dbesc($dfrn_id), intval($cid), intval($uid));
        if (!count($r)) {
            logger('Contact not found in DB.');
            notice(t('Contact not found.') . EOL);
            notice(t('This may occasionally happen if contact was requested by both persons and it has already been approved.') . EOL);
            return;
        }
        $contact = $r[0];
        $contact_id = $contact['id'];
        $relation = $contact['rel'];
        $site_pubkey = $contact['site-pubkey'];
        $dfrn_confirm = $contact['confirm'];
        $aes_allow = $contact['aes_allow'];
        $network = strlen($contact['issued-id']) ? NETWORK_DFRN : NETWORK_OSTATUS;
        if ($contact['network']) {
            $network = $contact['network'];
        }
        if ($network === NETWORK_DFRN) {
            /**
             *
             * Generate a key pair for all further communications with this person.
             * We have a keypair for every contact, and a site key for unknown people.
             * This provides a means to carry on relationships with other people if
             * any single key is compromised. It is a robust key. We're much more
             * worried about key leakage than anybody cracking it.
             *
             */
            require_once 'include/crypto.php';
            $res = new_keypair(4096);
            $private_key = $res['prvkey'];
            $public_key = $res['pubkey'];
            // Save the private key. Send them the public key.
            $r = q("UPDATE `contact` SET `prvkey` = '%s' WHERE `id` = %d AND `uid` = %d", dbesc($private_key), intval($contact_id), intval($uid));
            $params = array();
            /**
             *
             * Per the DFRN protocol, we will verify both ends by encrypting the dfrn_id with our
             * site private key (person on the other end can decrypt it with our site public key).
             * Then encrypt our profile URL with the other person's site public key. They can decrypt
             * it with their site private key. If the decryption on the other end fails for either
             * item, it indicates tampering or key failure on at least one site and we will not be
             * able to provide a secure communication pathway.
             *
             * If other site is willing to accept full encryption, (aes_allow is 1 AND we have php5.3
             * or later) then we encrypt the personal public key we send them using AES-256-CBC and a
             * random key which is encrypted with their site public key.
             *
             */
            $src_aes_key = random_string();
            $result = '';
            openssl_private_encrypt($dfrn_id, $result, $user[0]['prvkey']);
            $params['dfrn_id'] = bin2hex($result);
            $params['public_key'] = $public_key;
            $my_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
            openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
            $params['source_url'] = bin2hex($params['source_url']);
            if ($aes_allow && function_exists('openssl_encrypt')) {
                openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey);
                $params['aes_key'] = bin2hex($params['aes_key']);
                $params['public_key'] = bin2hex(openssl_encrypt($public_key, 'AES-256-CBC', $src_aes_key));
            }
            $params['dfrn_version'] = DFRN_PROTOCOL_VERSION;
            if ($duplex == 1) {
                $params['duplex'] = 1;
            }
            if ($user[0]['page-flags'] == PAGE_COMMUNITY) {
                $params['page'] = 1;
            }
            if ($user[0]['page-flags'] == PAGE_PRVGROUP) {
                $params['page'] = 2;
            }
            logger('Confirm: posting data to ' . $dfrn_confirm . ': ' . print_r($params, true), LOGGER_DATA);
            /**
             *
             * POST all this stuff to the other site.
             * Temporarily raise the network timeout to 120 seconds because the default 60
             * doesn't always give the other side quite enough time to decrypt everything.
             *
             */
            $a->config['system']['curl_timeout'] = 120;
            $res = post_url($dfrn_confirm, $params);
            logger(' Confirm: received data: ' . $res, LOGGER_DATA);
            // Now figure out what they responded. Try to be robust if the remote site is
            // having difficulty and throwing up errors of some kind.
            $leading_junk = substr($res, 0, strpos($res, '<?xml'));
            $res = substr($res, strpos($res, '<?xml'));
            if (!strlen($res)) {
                // No XML at all, this exchange is messed up really bad.
                // We shouldn't proceed, because the xml parser might choke,
                // and $status is going to be zero, which indicates success.
                // We can hardly call this a success.
                notice(t('Response from remote site was not understood.') . EOL);
                return;
            }
            if (strlen($leading_junk) && get_config('system', 'debugging')) {
                // This might be more common. Mixed error text and some XML.
                // If we're configured for debugging, show the text. Proceed in either case.
                notice(t('Unexpected response from remote site: ') . EOL . $leading_junk . EOL);
            }
            if (stristr($res, "<status") === false) {
                // wrong xml! stop here!
                notice(t('Unexpected response from remote site: ') . EOL . htmlspecialchars($res) . EOL);
                return;
            }
            $xml = parse_xml_string($res);
            $status = (int) $xml->status;
            $message = unxmlify($xml->message);
            // human readable text of what may have gone wrong.
            switch ($status) {
                case 0:
                    info(t("Confirmation completed successfully.") . EOL);
                    if (strlen($message)) {
                        notice(t('Remote site reported: ') . $message . EOL);
                    }
                    break;
                case 1:
                    // birthday paradox - generate new dfrn-id and fall through.
                    $new_dfrn_id = random_string();
                    $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d AND `uid` = %d", dbesc($new_dfrn_id), intval($contact_id), intval($uid));
                case 2:
                    notice(t("Temporary failure. Please wait and try again.") . EOL);
                    if (strlen($message)) {
                        notice(t('Remote site reported: ') . $message . EOL);
                    }
                    break;
                case 3:
                    notice(t("Introduction failed or was revoked.") . EOL);
                    if (strlen($message)) {
                        notice(t('Remote site reported: ') . $message . EOL);
                    }
                    break;
            }
            if ($status == 0 && $intro_id) {
                // Success. Delete the notification.
                $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d", intval($intro_id), intval($uid));
            }
            if ($status != 0) {
                return;
            }
        }
        /*
         *
         * We have now established a relationship with the other site.
         * Let's make our own personal copy of their profile photo so we don't have
         * to always load it from their site.
         *
         * We will also update the contact record with the nature and scope of the relationship.
         *
         */
        require_once 'include/Photo.php';
        $photos = import_profile_photo($contact['photo'], $uid, $contact_id);
        logger('dfrn_confirm: confirm - imported photos');
        if ($network === NETWORK_DFRN) {
            $new_relation = CONTACT_IS_FOLLOWER;
            if ($relation == CONTACT_IS_SHARING || $duplex) {
                $new_relation = CONTACT_IS_FRIEND;
            }
            if ($relation == CONTACT_IS_SHARING && $duplex) {
                $duplex = 0;
            }
            $r = q("UPDATE `contact` SET\n\t\t\t\t`photo` = '%s',\n\t\t\t\t`thumb` = '%s',\n\t\t\t\t`micro` = '%s',\n\t\t\t\t`rel` = %d,\n\t\t\t\t`name-date` = '%s',\n\t\t\t\t`uri-date` = '%s',\n\t\t\t\t`avatar-date` = '%s',\n\t\t\t\t`blocked` = 0,\n\t\t\t\t`pending` = 0,\n\t\t\t\t`duplex` = %d,\n\t\t\t\t`hidden` = %d,\n\t\t\t\t`network` = '%s' WHERE `id` = %d\n\t\t\t", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), intval($new_relation), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($duplex), intval($hidden), dbesc(NETWORK_DFRN), intval($contact_id));
        } else {
            // $network !== NETWORK_DFRN
            $network = $contact['network'] ? $contact['network'] : NETWORK_OSTATUS;
            $notify = $contact['notify'] ? $contact['notify'] : '';
            $poll = $contact['poll'] ? $contact['poll'] : '';
            if (!$contact['notify'] || !$contact['poll']) {
                $arr = lrdd($contact['url']);
                if (count($arr)) {
                    foreach ($arr as $link) {
                        if ($link['@attributes']['rel'] === 'salmon') {
                            $notify = $link['@attributes']['href'];
                        }
                        if ($link['@attributes']['rel'] === NAMESPACE_FEED) {
                            $poll = $link['@attributes']['href'];
                        }
                    }
                }
            }
            $new_relation = $contact['rel'];
            $writable = $contact['writable'];
            if ($network === NETWORK_DIASPORA) {
                if ($duplex) {
                    $new_relation = CONTACT_IS_FRIEND;
                } else {
                    $new_relation = CONTACT_IS_FOLLOWER;
                }
                if ($new_relation != CONTACT_IS_FOLLOWER) {
                    $writable = 1;
                }
            }
            $r = q("DELETE FROM `intro` WHERE `id` = %d AND `uid` = %d", intval($intro_id), intval($uid));
            $r = q("UPDATE `contact` SET `photo` = '%s',\n\t\t\t\t`thumb` = '%s',\n\t\t\t\t`micro` = '%s',\n\t\t\t\t`name-date` = '%s',\n\t\t\t\t`uri-date` = '%s',\n\t\t\t\t`avatar-date` = '%s',\n\t\t\t\t`notify` = '%s',\n\t\t\t\t`poll` = '%s',\n\t\t\t\t`blocked` = 0,\n\t\t\t\t`pending` = 0,\n\t\t\t\t`network` = '%s',\n\t\t\t\t`writable` = %d,\n\t\t\t\t`hidden` = %d,\n\t\t\t\t`rel` = %d\n\t\t\t\tWHERE `id` = %d\n\t\t\t", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc($notify), dbesc($poll), dbesc($network), intval($writable), intval($hidden), intval($new_relation), intval($contact_id));
        }
        if ($r === false) {
            notice(t('Unable to set contact photo.') . EOL);
        }
        // reload contact info
        $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($contact_id));
        if (count($r)) {
            $contact = $r[0];
        } else {
            $contact = null;
        }
        if (isset($new_relation) && $new_relation == CONTACT_IS_FRIEND) {
            if ($contact && $contact['network'] === NETWORK_DIASPORA) {
                require_once 'include/diaspora.php';
                $ret = diaspora_share($user[0], $r[0]);
                logger('mod_follow: diaspora_share returns: ' . $ret);
            }
            // Send a new friend post if we are allowed to...
            $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1", intval($uid));
            if (count($r) && $r[0]['hide-friends'] == 0 && $activity && !$hidden) {
                require_once 'include/items.php';
                $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", intval($uid));
                if (count($self)) {
                    $arr = array();
                    $arr['uri'] = $arr['parent-uri'] = item_new_uri($a->get_hostname(), $uid);
                    $arr['uid'] = $uid;
                    $arr['contact-id'] = $self[0]['id'];
                    $arr['wall'] = 1;
                    $arr['type'] = 'wall';
                    $arr['gravity'] = 0;
                    $arr['origin'] = 1;
                    $arr['author-name'] = $arr['owner-name'] = $self[0]['name'];
                    $arr['author-link'] = $arr['owner-link'] = $self[0]['url'];
                    $arr['author-avatar'] = $arr['owner-avatar'] = $self[0]['thumb'];
                    $A = '[url=' . $self[0]['url'] . ']' . $self[0]['name'] . '[/url]';
                    $APhoto = '[url=' . $self[0]['url'] . ']' . '[img]' . $self[0]['thumb'] . '[/img][/url]';
                    $B = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
                    $BPhoto = '[url=' . $contact['url'] . ']' . '[img]' . $contact['thumb'] . '[/img][/url]';
                    $arr['verb'] = ACTIVITY_FRIEND;
                    $arr['object-type'] = ACTIVITY_OBJ_PERSON;
                    $arr['body'] = sprintf(t('%1$s is now friends with %2$s'), $A, $B) . "\n\n\n" . $BPhoto;
                    $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $contact['name'] . '</title>' . '<id>' . $contact['url'] . '/' . $contact['name'] . '</id>';
                    $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $contact['url'] . '" />' . "\n");
                    $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $contact['thumb'] . '" />' . "\n");
                    $arr['object'] .= '</link></object>' . "\n";
                    $arr['last-child'] = 1;
                    $arr['allow_cid'] = $user[0]['allow_cid'];
                    $arr['allow_gid'] = $user[0]['allow_gid'];
                    $arr['deny_cid'] = $user[0]['deny_cid'];
                    $arr['deny_gid'] = $user[0]['deny_gid'];
                    $i = item_store($arr);
                    if ($i) {
                        proc_run('php', "include/notifier.php", "activity", "{$i}");
                    }
                }
            }
        }
        $g = q("select def_gid from user where uid = %d limit 1", intval($uid));
        if ($contact && $g && intval($g[0]['def_gid'])) {
            require_once 'include/group.php';
            group_add_member($uid, '', $contact['id'], $g[0]['def_gid']);
        }
        // Let's send our user to the contact editor in case they want to
        // do anything special with this new friend.
        if ($handsfree === null) {
            goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));
        } else {
            return;
        }
        //NOTREACHED
    }
    /**
     *
     *
     * End of Scenario 1. [Local confirmation of remote friend request].
     *
     * Begin Scenario 2. This is the remote response to the above scenario.
     * This will take place on the site that originally initiated the friend request.
     * In the section above where the confirming party makes a POST and
     * retrieves xml status information, they are communicating with the following code.
     *
     */
    if (x($_POST, 'source_url')) {
        // We are processing an external confirmation to an introduction created by our user.
        $public_key = x($_POST, 'public_key') ? $_POST['public_key'] : '';
        $dfrn_id = x($_POST, 'dfrn_id') ? hex2bin($_POST['dfrn_id']) : '';
        $source_url = x($_POST, 'source_url') ? hex2bin($_POST['source_url']) : '';
        $aes_key = x($_POST, 'aes_key') ? $_POST['aes_key'] : '';
        $duplex = x($_POST, 'duplex') ? intval($_POST['duplex']) : 0;
        $page = x($_POST, 'page') ? intval($_POST['page']) : 0;
        $version_id = x($_POST, 'dfrn_version') ? (double) $_POST['dfrn_version'] : 2.0;
        $forum = $page == 1 ? 1 : 0;
        $prv = $page == 2 ? 1 : 0;
        logger('dfrn_confirm: requestee contacted: ' . $node);
        logger('dfrn_confirm: request: POST=' . print_r($_POST, true), LOGGER_DATA);
        // If $aes_key is set, both of these items require unpacking from the hex transport encoding.
        if (x($aes_key)) {
            $aes_key = hex2bin($aes_key);
            $public_key = hex2bin($public_key);
        }
        // Find our user's account
        $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($node));
        if (!count($r)) {
            $message = sprintf(t('No user record found for \'%s\' '), $node);
            xml_status(3, $message);
            // failure
            // NOTREACHED
        }
        $my_prvkey = $r[0]['prvkey'];
        $local_uid = $r[0]['uid'];
        if (!strstr($my_prvkey, 'PRIVATE KEY')) {
            $message = t('Our site encryption key is apparently messed up.');
            xml_status(3, $message);
        }
        // verify everything
        $decrypted_source_url = "";
        openssl_private_decrypt($source_url, $decrypted_source_url, $my_prvkey);
        if (!strlen($decrypted_source_url)) {
            $message = t('Empty site URL was provided or URL could not be decrypted by us.');
            xml_status(3, $message);
            // NOTREACHED
        }
        $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1", dbesc($decrypted_source_url), intval($local_uid));
        if (!count($ret)) {
            if (strstr($decrypted_source_url, 'http:')) {
                $newurl = str_replace('http:', 'https:', $decrypted_source_url);
            } else {
                $newurl = str_replace('https:', 'http:', $decrypted_source_url);
            }
            $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1", dbesc($newurl), intval($local_uid));
            if (!count($ret)) {
                // this is either a bogus confirmation (?) or we deleted the original introduction.
                $message = t('Contact record was not found for you on our site.');
                xml_status(3, $message);
                return;
                // NOTREACHED
            }
        }
        $relation = $ret[0]['rel'];
        // Decrypt all this stuff we just received
        $foreign_pubkey = $ret[0]['site-pubkey'];
        $dfrn_record = $ret[0]['id'];
        if (!$foreign_pubkey) {
            $message = sprintf(t('Site public key not available in contact record for URL %s.'), $newurl);
            xml_status(3, $message);
        }
        $decrypted_dfrn_id = "";
        openssl_public_decrypt($dfrn_id, $decrypted_dfrn_id, $foreign_pubkey);
        if (strlen($aes_key)) {
            $decrypted_aes_key = "";
            openssl_private_decrypt($aes_key, $decrypted_aes_key, $my_prvkey);
            $dfrn_pubkey = openssl_decrypt($public_key, 'AES-256-CBC', $decrypted_aes_key);
        } else {
            $dfrn_pubkey = $public_key;
        }
        $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1", dbesc($decrypted_dfrn_id));
        if (count($r)) {
            $message = t('The ID provided by your system is a duplicate on our system. It should work if you try again.');
            xml_status(1, $message);
            // Birthday paradox - duplicate dfrn-id
            // NOTREACHED
        }
        $r = q("UPDATE `contact` SET `dfrn-id` = '%s', `pubkey` = '%s' WHERE `id` = %d", dbesc($decrypted_dfrn_id), dbesc($dfrn_pubkey), intval($dfrn_record));
        if (!count($r)) {
            $message = t('Unable to set your contact credentials on our system.');
            xml_status(3, $message);
        }
        // It's possible that the other person also requested friendship.
        // If it is a duplex relationship, ditch the issued-id if one exists.
        if ($duplex) {
            $r = q("UPDATE `contact` SET `issued-id` = '' WHERE `id` = %d", intval($dfrn_record));
        }
        // We're good but now we have to scrape the profile photo and send notifications.
        $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1", intval($dfrn_record));
        if (count($r)) {
            $photo = $r[0]['photo'];
        } else {
            $photo = $a->get_baseurl() . '/images/person-175.jpg';
        }
        require_once "include/Photo.php";
        $photos = import_profile_photo($photo, $local_uid, $dfrn_record);
        logger('dfrn_confirm: request - photos imported');
        $new_relation = CONTACT_IS_SHARING;
        if ($relation == CONTACT_IS_FOLLOWER || $duplex) {
            $new_relation = CONTACT_IS_FRIEND;
        }
        if ($relation == CONTACT_IS_FOLLOWER && $duplex) {
            $duplex = 0;
        }
        $r = q("UPDATE `contact` SET\n\t\t\t`photo` = '%s',\n\t\t\t`thumb` = '%s',\n\t\t\t`micro` = '%s',\n\t\t\t`rel` = %d,\n\t\t\t`name-date` = '%s',\n\t\t\t`uri-date` = '%s',\n\t\t\t`avatar-date` = '%s',\n\t\t\t`blocked` = 0,\n\t\t\t`pending` = 0,\n\t\t\t`duplex` = %d,\n\t\t\t`forum` = %d,\n\t\t\t`prv` = %d,\n\t\t\t`network` = '%s' WHERE `id` = %d\n\t\t", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), intval($new_relation), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($duplex), intval($forum), intval($prv), dbesc(NETWORK_DFRN), intval($dfrn_record));
        if ($r === false) {
            // indicates schema is messed up or total db failure
            $message = t('Unable to update your contact profile details on our system');
            xml_status(3, $message);
        }
        // Otherwise everything seems to have worked and we are almost done. Yay!
        // Send an email notification
        logger('dfrn_confirm: request: info updated');
        $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`\n\t\t\tWHERE `contact`.`id` = %d LIMIT 1", intval($dfrn_record));
        if (count($r)) {
            $combined = $r[0];
        }
        if (count($r) && $r[0]['notify-flags'] & NOTIFY_CONFIRM) {
            $mutual = $new_relation == CONTACT_IS_FRIEND;
            notification(array('type' => NOTIFY_CONFIRM, 'notify_flags' => $r[0]['notify-flags'], 'language' => $r[0]['language'], 'to_name' => $r[0]['username'], 'to_email' => $r[0]['email'], 'uid' => $r[0]['uid'], 'link' => $a->get_baseurl() . '/contacts/' . $dfrn_record, 'source_name' => strlen(stripslashes($r[0]['name'])) ? stripslashes($r[0]['name']) : t('[Name Withheld]'), 'source_link' => $r[0]['url'], 'source_photo' => $r[0]['photo'], 'verb' => $mutual ? ACTIVITY_FRIEND : ACTIVITY_FOLLOW, 'otype' => 'intro'));
        }
        // Send a new friend post if we are allowed to...
        if ($page && intval(get_pconfig($local_uid, 'system', 'post_joingroup'))) {
            $r = q("SELECT `hide-friends` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1", intval($local_uid));
            if (count($r) && $r[0]['hide-friends'] == 0) {
                require_once 'include/items.php';
                $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", intval($local_uid));
                if (count($self)) {
                    $arr = array();
                    $arr['uri'] = $arr['parent-uri'] = item_new_uri($a->get_hostname(), $local_uid);
                    $arr['uid'] = $local_uid;
                    $arr['contact-id'] = $self[0]['id'];
                    $arr['wall'] = 1;
                    $arr['type'] = 'wall';
                    $arr['gravity'] = 0;
                    $arr['origin'] = 1;
                    $arr['author-name'] = $arr['owner-name'] = $self[0]['name'];
                    $arr['author-link'] = $arr['owner-link'] = $self[0]['url'];
                    $arr['author-avatar'] = $arr['owner-avatar'] = $self[0]['thumb'];
                    $A = '[url=' . $self[0]['url'] . ']' . $self[0]['name'] . '[/url]';
                    $APhoto = '[url=' . $self[0]['url'] . ']' . '[img]' . $self[0]['thumb'] . '[/img][/url]';
                    $B = '[url=' . $combined['url'] . ']' . $combined['name'] . '[/url]';
                    $BPhoto = '[url=' . $combined['url'] . ']' . '[img]' . $combined['thumb'] . '[/img][/url]';
                    $arr['verb'] = ACTIVITY_JOIN;
                    $arr['object-type'] = ACTIVITY_OBJ_GROUP;
                    $arr['body'] = sprintf(t('%1$s has joined %2$s'), $A, $B) . "\n\n\n" . $BPhoto;
                    $arr['object'] = '<object><type>' . ACTIVITY_OBJ_GROUP . '</type><title>' . $combined['name'] . '</title>' . '<id>' . $combined['url'] . '/' . $combined['name'] . '</id>';
                    $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $combined['url'] . '" />' . "\n");
                    $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $combined['thumb'] . '" />' . "\n");
                    $arr['object'] .= '</link></object>' . "\n";
                    $arr['last-child'] = 1;
                    $arr['allow_cid'] = $user[0]['allow_cid'];
                    $arr['allow_gid'] = $user[0]['allow_gid'];
                    $arr['deny_cid'] = $user[0]['deny_cid'];
                    $arr['deny_gid'] = $user[0]['deny_gid'];
                    $i = item_store($arr);
                    if ($i) {
                        proc_run('php', "include/notifier.php", "activity", "{$i}");
                    }
                }
            }
        }
        xml_status(0);
        // Success
        return;
        // NOTREACHED
        ////////////////////// End of this scenario ///////////////////////////////////////////////
    }
    // somebody arrived here by mistake or they are fishing. Send them to the homepage.
    goaway(z_root());
    // NOTREACHED
}
Example #14
0
function local_delivery($importer, $data)
{
    $a = get_app();
    if ($importer['readonly']) {
        // We aren't receiving stuff from this person. But we will quietly ignore them
        // rather than a blatant "go away" message.
        logger('local_delivery: ignoring');
        return 0;
        //NOTREACHED
    }
    // Consume notification feed. This may differ from consuming a public feed in several ways
    // - might contain email or friend suggestions
    // - might contain remote followup to our message
    //		- in which case we need to accept it and then notify other conversants
    // - we may need to send various email notifications
    $feed = new SimplePie();
    $feed->set_raw_data($data);
    $feed->enable_order_by_date(false);
    $feed->init();
    $reloc = $feed->get_feed_tags(NAMESPACE_DFRN, 'relocate');
    if (isset($reloc[0]['child'][NAMESPACE_DFRN])) {
        $base = $reloc[0]['child'][NAMESPACE_DFRN];
        $newloc = array();
        $newloc['uid'] = $importer['importer_uid'];
        $newloc['cid'] = $importer['id'];
        $newloc['name'] = notags(unxmlify($base['name'][0]['data']));
        $newloc['photo'] = notags(unxmlify($base['photo'][0]['data']));
        $newloc['url'] = notags(unxmlify($base['url'][0]['data']));
        $newloc['request'] = notags(unxmlify($base['request'][0]['data']));
        $newloc['confirm'] = notags(unxmlify($base['confirm'][0]['data']));
        $newloc['notify'] = notags(unxmlify($base['notify'][0]['data']));
        $newloc['poll'] = notags(unxmlify($base['poll'][0]['data']));
        $newloc['site-pubkey'] = notags(unxmlify($base['site-pubkey'][0]['data']));
        $newloc['pubkey'] = notags(unxmlify($base['pubkey'][0]['data']));
        $newloc['prvkey'] = notags(unxmlify($base['prvkey'][0]['data']));
        // TODO
        // merge with current record, current contents have priority
        // update record, set url-updated
        // update profile photos
        // schedule a scan?
    }
    // handle friend suggestion notification
    $sugg = $feed->get_feed_tags(NAMESPACE_DFRN, 'suggest');
    if (isset($sugg[0]['child'][NAMESPACE_DFRN])) {
        $base = $sugg[0]['child'][NAMESPACE_DFRN];
        $fsugg = array();
        $fsugg['uid'] = $importer['importer_uid'];
        $fsugg['cid'] = $importer['id'];
        $fsugg['name'] = notags(unxmlify($base['name'][0]['data']));
        $fsugg['photo'] = notags(unxmlify($base['photo'][0]['data']));
        $fsugg['url'] = notags(unxmlify($base['url'][0]['data']));
        $fsugg['request'] = notags(unxmlify($base['request'][0]['data']));
        $fsugg['body'] = escape_tags(unxmlify($base['note'][0]['data']));
        // Does our member already have a friend matching this description?
        $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1", dbesc($fsugg['name']), dbesc(normalise_link($fsugg['url'])), intval($fsugg['uid']));
        if (count($r)) {
            return 0;
        }
        // Do we already have an fcontact record for this person?
        $fid = 0;
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
        }
        if (!$fid) {
            $r = q("INSERT INTO `fcontact` ( `name`,`url`,`photo`,`request` ) VALUES ( '%s', '%s', '%s', '%s' ) ", dbesc($fsugg['name']), dbesc($fsugg['url']), dbesc($fsugg['photo']), dbesc($fsugg['request']));
        }
        $r = q("SELECT * FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1", dbesc($fsugg['url']), dbesc($fsugg['name']), dbesc($fsugg['request']));
        if (count($r)) {
            $fid = $r[0]['id'];
        } else {
            return 0;
        }
        $hash = random_string();
        $r = q("INSERT INTO `intro` ( `uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked` )\n\t\t\tVALUES( %d, %d, %d, '%s', '%s', '%s', %d )", intval($fsugg['uid']), intval($fid), intval($fsugg['cid']), dbesc($fsugg['body']), dbesc($hash), dbesc(datetime_convert()), intval(0));
        // TODO - send email notify (which may require a new notification preference)
        return 0;
    }
    $ismail = false;
    $rawmail = $feed->get_feed_tags(NAMESPACE_DFRN, 'mail');
    if (isset($rawmail[0]['child'][NAMESPACE_DFRN])) {
        logger('local_delivery: private message received');
        $ismail = true;
        $base = $rawmail[0]['child'][NAMESPACE_DFRN];
        $msg = array();
        $msg['uid'] = $importer['importer_uid'];
        $msg['from-name'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['name'][0]['data']));
        $msg['from-photo'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['avatar'][0]['data']));
        $msg['from-url'] = notags(unxmlify($base['sender'][0]['child'][NAMESPACE_DFRN]['uri'][0]['data']));
        $msg['contact-id'] = $importer['id'];
        $msg['title'] = notags(unxmlify($base['subject'][0]['data']));
        $msg['body'] = escape_tags(unxmlify($base['content'][0]['data']));
        $msg['seen'] = 0;
        $msg['replied'] = 0;
        $msg['uri'] = notags(unxmlify($base['id'][0]['data']));
        $msg['parent-uri'] = notags(unxmlify($base['in-reply-to'][0]['data']));
        $msg['created'] = datetime_convert(notags(unxmlify('UTC', 'UTC', $base['sentdate'][0]['data'])));
        dbesc_array($msg);
        $r = dbq("INSERT INTO `mail` (`" . implode("`, `", array_keys($msg)) . "`) VALUES ('" . implode("', '", array_values($msg)) . "')");
        // send email notification if requested.
        require_once 'bbcode.php';
        if ($importer['notify-flags'] & NOTIFY_MAIL) {
            push_lang($importer['language']);
            // name of the automated email sender
            $msg['notificationfromname'] = t('Administrator');
            // noreply address to send from
            $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
            // text version
            // process the message body to display properly in text mode
            // 		1) substitute a \n character for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
            //		2) remove escape slashes
            //		3) decode any bbcode from the message editor
            //		4) decode any encoded html tags
            //		5) remove html tags
            $msg['textversion'] = strip_tags(html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n"), "\n", $msg['body']))), ENT_QUOTES, 'UTF-8'));
            // html version
            // process the message body to display properly in text mode
            // 		1) substitute a <br /> tag for the "\" then "n", so it behaves properly (it doesn't come in as a \n character)
            //		2) remove escape slashes
            //		3) decode any bbcode from the message editor
            //		4) decode any encoded html tags
            $msg['htmlversion'] = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n\\n", "\\n"), "<br />\n", $msg['body']))));
            // load the template for private message notifications
            $tpl = get_intltext_template('mail_received_html_body_eml.tpl');
            $email_html_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$siteName' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $importer['thumb'], '$email' => $importer['email'], '$url' => $importer['url'], '$from' => $msg['from-name'], '$title' => stripslashes($msg['title']), '$htmlversion' => $msg['htmlversion'], '$mimeboundary' => $msg['mimeboundary'], '$hostname' => $a->get_hostname()));
            // load the template for private message notifications
            $tpl = get_intltext_template('mail_received_text_body_eml.tpl');
            $email_text_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$siteName' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $importer['thumb'], '$email' => $importer['email'], '$url' => $importer['url'], '$from' => $msg['from-name'], '$title' => stripslashes($msg['title']), '$textversion' => $msg['textversion'], '$mimeboundary' => $msg['mimeboundary'], '$hostname' => $a->get_hostname()));
            // use the EmailNotification library to send the message
            require_once "include/EmailNotification.php";
            EmailNotification::sendTextHtmlEmail($msg['notificationfromname'], $msg['notificationfromemail'], $msg['notificationfromemail'], $importer['email'], t('New mail received at ') . $a->config['sitename'], $email_html_body_tpl, $email_text_body_tpl);
            pop_lang();
        }
        return 0;
        // NOTREACHED
    }
    logger('local_delivery: feed item count = ' . $feed->get_item_quantity());
    // process any deleted entries
    $del_entries = $feed->get_feed_tags(NAMESPACE_TOMB, 'deleted-entry');
    if (is_array($del_entries) && count($del_entries)) {
        foreach ($del_entries as $dentry) {
            $deleted = false;
            if (isset($dentry['attribs']['']['ref'])) {
                $uri = $dentry['attribs']['']['ref'];
                $deleted = true;
                if (isset($dentry['attribs']['']['when'])) {
                    $when = $dentry['attribs']['']['when'];
                    $when = datetime_convert('UTC', 'UTC', $when, 'Y-m-d H:i:s');
                } else {
                    $when = datetime_convert('UTC', 'UTC', 'now', 'Y-m-d H:i:s');
                }
            }
            if ($deleted) {
                $r = q("SELECT `item`.*, `contact`.`self` FROM `item` left join contact on `item`.`contact-id` = `contact`.`id`\n\t\t\t\t\tWHERE `uri` = '%s' AND `item`.`uid` = %d AND `contact-id` = %d LIMIT 1", dbesc($uri), intval($importer['importer_uid']), intval($importer['id']));
                if (count($r)) {
                    $item = $r[0];
                    if ($item['deleted']) {
                        continue;
                    }
                    logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
                    if ($item['verb'] === ACTIVITY_TAG && $item['object-type'] === ACTVITY_OBJ_TAGTERM) {
                        $xo = parse_xml_string($item['object'], false);
                        $xt = parse_xml_string($item['target'], false);
                        if ($xt->type === ACTIVITY_OBJ_NOTE) {
                            $i = q("select * from `item` where uri = '%s' and uid = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                            if (count($i)) {
                                // For tags, the owner cannot remove the tag on the author's copy of the post.
                                $owner_remove = $item['contact-id'] == $i[0]['contact-id'] ? true : false;
                                $author_remove = $item['origin'] && $item['self'] ? true : false;
                                $author_copy = $item['origin'] ? true : false;
                                if ($owner_remove && $author_copy) {
                                    continue;
                                }
                                if ($author_remove || $owner_remove) {
                                    $tags = explode(',', $i[0]['tag']);
                                    $newtags = array();
                                    if (count($tags)) {
                                        foreach ($tags as $tag) {
                                            if (trim($tag) !== trim($xo->body)) {
                                                $newtags[] = trim($tag);
                                            }
                                        }
                                    }
                                    q("update item set tag = '%s' where id = %d limit 1", dbesc(implode(',', $newtags)), intval($i[0]['id']));
                                }
                            }
                        }
                    }
                    if ($item['uri'] == $item['parent-uri']) {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s'\n\t\t\t\t\t\t\tWHERE `parent-uri` = '%s' AND `uid` = %d", dbesc($when), dbesc(datetime_convert()), dbesc($item['uri']), intval($importer['importer_uid']));
                    } else {
                        $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' \n\t\t\t\t\t\t\tWHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($when), dbesc(datetime_convert()), dbesc($uri), intval($importer['importer_uid']));
                        if ($item['last-child']) {
                            // ensure that last-child is set in case the comment that had it just got wiped.
                            q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d ", dbesc(datetime_convert()), dbesc($item['parent-uri']), intval($item['uid']));
                            // who is the last child now?
                            $r = q("SELECT `id` FROM `item` WHERE `parent-uri` = '%s' AND `type` != 'activity' AND `deleted` = 0 AND `uid` = %d\n\t\t\t\t\t\t\t\tORDER BY `created` DESC LIMIT 1", dbesc($item['parent-uri']), intval($importer['importer_uid']));
                            if (count($r)) {
                                q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d LIMIT 1", intval($r[0]['id']));
                            }
                        }
                    }
                }
            }
        }
    }
    foreach ($feed->get_items() as $item) {
        $is_reply = false;
        $item_id = $item->get_id();
        $rawthread = $item->get_item_tags(NAMESPACE_THREAD, 'in-reply-to');
        if (isset($rawthread[0]['attribs']['']['ref'])) {
            $is_reply = true;
            $parent_uri = $rawthread[0]['attribs']['']['ref'];
        }
        if ($is_reply) {
            $community = false;
            if ($importer['page-flags'] == PAGE_COMMUNITY) {
                $sql_extra = '';
                $community = true;
                logger('local_delivery: community reply');
            } else {
                $sql_extra = " and contact.self = 1 and item.wall = 1 ";
            }
            // was the top-level post for this reply written by somebody on this site?
            // Specifically, the recipient?
            $r = q("select `item`.`id`, `item`.`uri`, `item`.`tag`, \n\t\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`thumb` from `item` \n\t\t\t\tLEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id` \n\t\t\t\tWHERE `item`.`uri` = '%s' AND `item`.`parent-uri` = '%s'\n\t\t\t\tAND `item`.`uid` = %d \n\t\t\t\t{$sql_extra}\n\t\t\t\tLIMIT 1", dbesc($parent_uri), dbesc($parent_uri), intval($importer['importer_uid']));
            if ($r && count($r)) {
                logger('local_delivery: received remote comment');
                $is_like = false;
                // remote reply to our post. Import and then notify everybody else.
                $datarray = get_atom_elements($feed, $item);
                // TODO: make this next part work against both delivery threads of a community post
                //				if((! link_compare($datarray['author-link'],$importer['url'])) && (! $community)) {
                //					logger('local_delivery: received relay claiming to be from ' . $importer['url'] . ' however comment author url is ' . $datarray['author-link'] );
                // they won't know what to do so don't report an error. Just quietly die.
                //					return 0;
                //				}
                $datarray['type'] = 'remote-comment';
                $datarray['wall'] = 1;
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['owner-name'] = $r[0]['name'];
                $datarray['owner-link'] = $r[0]['url'];
                $datarray['owner-avatar'] = $r[0]['thumb'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] === ACTIVITY_LIKE || $datarray['verb'] === ACTIVITY_DISLIKE) {
                    $is_like = true;
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                    $datarray['last-child'] = 0;
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE && $xt->id == $r[0]['uri']) {
                        // extract tag, if not duplicate, and this user allows tags, add to parent item
                        if ($xo->id && $xo->content) {
                            $newtag = '#[url=' . $xo->id . ']' . $xo->content . '[/url]';
                            if (!stristr($r[0]['tag'], $newtag)) {
                                $i = q("SELECT `blocktags` FROM `user` where `uid` = %d LIMIT 1", intval($importer['importer_uid']));
                                if (count($i) && !$i[0]['blocktags']) {
                                    q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1", dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . $newtag), intval($r[0]['id']));
                                }
                            }
                        }
                    }
                }
                // 				if($community) {
                //					$newtag = '@[url=' . $a->get_baseurl() . '/profile/' . $importer['nickname'] . ']' . $importer['username'] . '[/url]';
                //					if(! stristr($datarray['tag'],$newtag)) {
                //						if(strlen($datarray['tag']))
                //							$datarray['tag'] .= ',';
                //						$datarray['tag'] .= $newtag;
                //					}
                //				}
                $posted_id = item_store($datarray);
                $parent = 0;
                if ($posted_id) {
                    $r = q("SELECT `parent` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($posted_id), intval($importer['importer_uid']));
                    if (count($r)) {
                        $parent = $r[0]['parent'];
                    }
                    if (!$is_like) {
                        $r1 = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `uid` = %d AND `parent` = %d", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($r[0]['parent']));
                        $r2 = q("UPDATE `item` SET `last-child` = 1, `changed` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($importer['importer_uid']), intval($posted_id));
                    }
                    if ($posted_id && $parent) {
                        proc_run('php', "include/notifier.php", "comment-import", "{$posted_id}");
                        if (!$is_like && $importer['notify-flags'] & NOTIFY_COMMENT && !$importer['self']) {
                            push_lang($importer['language']);
                            require_once 'bbcode.php';
                            $from = stripslashes($datarray['author-name']);
                            // name of the automated email sender
                            $msg['notificationfromname'] = stripslashes($datarray['author-name']);
                            // noreply address to send from
                            $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
                            // text version
                            // process the message body to display properly in text mode
                            $msg['textversion'] = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
                            // html version
                            // process the message body to display properly in text mode
                            $msg['htmlversion'] = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n\\n", "\\n"), "<br />\n", $datarray['body']))));
                            $imgtouse = link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'];
                            // load the template for private message notifications
                            $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
                            $email_html_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$sitename' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $imgtouse, '$email' => $importer['email'], '$url' => $datarray['author-link'], '$from' => $from, '$body' => $msg['htmlversion'], '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id));
                            // load the template for private message notifications
                            $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
                            $email_text_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$sitename' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $imgtouse, '$email' => $importer['email'], '$url' => $datarray['author-link'], '$from' => $from, '$body' => $msg['textversion'], '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id));
                            // use the EmailNotification library to send the message
                            require_once "include/EmailNotification.php";
                            EmailNotification::sendTextHtmlEmail($msg['notificationfromname'], t("Administrator") . '@' . $a->get_hostname(), t("noreply") . '@' . $a->get_hostname(), $importer['email'], sprintf(t('%s commented on an item at %s'), $from, $a->config['sitename']), $email_html_body_tpl, $email_text_body_tpl);
                            pop_lang();
                        }
                    }
                    return 0;
                    // NOTREACHED
                }
            } else {
                // regular comment that is part of this total conversation. Have we seen it? If not, import it.
                $item_id = $item->get_id();
                $datarray = get_atom_elements($feed, $item);
                $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
                // Update content if 'updated' changes
                if (count($r)) {
                    if (x($datarray, 'edited') !== false && datetime_convert('UTC', 'UTC', $datarray['edited']) !== $r[0]['edited']) {
                        $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($datarray['body']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc($item_id), intval($importer['importer_uid']));
                    }
                    // update last-child if it changes
                    $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                    if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                        $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d", dbesc(datetime_convert()), dbesc($parent_uri), intval($importer['importer_uid']));
                        $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s'  WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                    }
                    continue;
                }
                $datarray['parent-uri'] = $parent_uri;
                $datarray['uid'] = $importer['importer_uid'];
                $datarray['contact-id'] = $importer['id'];
                if ($datarray['verb'] == ACTIVITY_LIKE || $datarray['verb'] == ACTIVITY_DISLIKE) {
                    $datarray['type'] = 'activity';
                    $datarray['gravity'] = GRAVITY_LIKE;
                }
                if ($datarray['verb'] === ACTIVITY_TAG && $datarray['object-type'] === ACTIVITY_OBJ_TAGTERM) {
                    $xo = parse_xml_string($datarray['object'], false);
                    $xt = parse_xml_string($datarray['target'], false);
                    if ($xt->type == ACTIVITY_OBJ_NOTE) {
                        $r = q("select * from item where `uri` = '%s' AND `uid` = %d limit 1", dbesc($xt->id), intval($importer['importer_uid']));
                        if (!count($r)) {
                            continue;
                        }
                        // extract tag, if not duplicate, add to parent item
                        if ($xo->content) {
                            if (!stristr($r[0]['tag'], trim($xo->content))) {
                                q("UPDATE item SET tag = '%s' WHERE id = %d LIMIT 1", dbesc($r[0]['tag'] . (strlen($r[0]['tag']) ? ',' : '') . '#[url=' . $xo->id . ']' . $xo->content . '[/url]'), intval($r[0]['id']));
                            }
                        }
                    }
                }
                $posted_id = item_store($datarray);
                // find out if our user is involved in this conversation and wants to be notified.
                if ($datarray['type'] != 'activity' && $importer['notify-flags'] & NOTIFY_COMMENT) {
                    $myconv = q("SELECT `author-link`, `author-avatar` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 ", dbesc($parent_uri), intval($importer['importer_uid']));
                    if (count($myconv)) {
                        $importer_url = $a->get_baseurl() . '/profile/' . $importer['nickname'];
                        foreach ($myconv as $conv) {
                            if (!link_compare($conv['author-link'], $importer_url)) {
                                continue;
                            }
                            push_lang($importer['language']);
                            require_once 'bbcode.php';
                            $from = stripslashes($datarray['author-name']);
                            // name of the automated email sender
                            $msg['notificationfromname'] = stripslashes($datarray['author-name']);
                            // noreply address to send from
                            $msg['notificationfromemail'] = t('noreply') . '@' . $a->get_hostname();
                            // text version
                            // process the message body to display properly in text mode
                            $msg['textversion'] = html_entity_decode(strip_tags(bbcode(stripslashes($datarray['body']))), ENT_QUOTES, 'UTF-8');
                            // html version
                            // process the message body to display properly in text mode
                            $msg['htmlversion'] = html_entity_decode(bbcode(stripslashes(str_replace(array("\\r\\n", "\\r", "\\n\\n", "\\n"), "<br />\n", $datarray['body']))));
                            $imgtouse = link_compare($datarray['author-link'], $importer['url']) ? $importer['thumb'] : $datarray['author-avatar'];
                            // load the template for private message notifications
                            $tpl = get_intltext_template('cmnt_received_html_body_eml.tpl');
                            $email_html_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$sitename' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $imgtouse, '$url' => $datarray['author-link'], '$from' => $from, '$body' => $msg['htmlversion'], '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id));
                            // load the template for private message notifications
                            $tpl = get_intltext_template('cmnt_received_text_body_eml.tpl');
                            $email_text_body_tpl = replace_macros($tpl, array('$username' => $importer['username'], '$sitename' => $a->config['sitename'], '$siteurl' => $a->get_baseurl(), '$thumb' => $imgtouse, '$url' => $datarray['author-link'], '$from' => $from, '$body' => $msg['textversion'], '$display' => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $posted_id));
                            // use the EmailNotification library to send the message
                            require_once "include/EmailNotification.php";
                            EmailNotification::sendTextHtmlEmail($msg['notificationfromname'], t("Administrator@") . $a->get_hostname(), t("noreply") . '@' . $a->get_hostname(), $importer['email'], sprintf(t('%s commented on an item at %s'), $from, $a->config['sitename']), $email_html_body_tpl, $email_text_body_tpl);
                            pop_lang();
                            break;
                        }
                    }
                }
                continue;
            }
        } else {
            // Head post of a conversation. Have we seen it? If not, import it.
            $item_id = $item->get_id();
            $datarray = get_atom_elements($feed, $item);
            if (x($datarray, 'object-type') && $datarray['object-type'] === ACTIVITY_OBJ_EVENT) {
                $ev = bbtoevent($datarray['body']);
                if (x($ev, 'desc') && x($ev, 'start')) {
                    $ev['cid'] = $importer['id'];
                    $ev['uid'] = $importer['uid'];
                    $ev['uri'] = $item_id;
                    $ev['edited'] = $datarray['edited'];
                    $ev['private'] = $datarray['private'];
                    $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['uid']));
                    if (count($r)) {
                        $ev['id'] = $r[0]['id'];
                    }
                    $xyz = event_store($ev);
                    continue;
                }
            }
            $r = q("SELECT `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($item_id), intval($importer['importer_uid']));
            // Update content if 'updated' changes
            if (count($r)) {
                if (x($datarray, 'edited') !== false && datetime_convert('UTC', 'UTC', $datarray['edited']) !== $r[0]['edited']) {
                    $r = q("UPDATE `item` SET `body` = '%s', `edited` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($datarray['body']), dbesc(datetime_convert('UTC', 'UTC', $datarray['edited'])), dbesc($item_id), intval($importer['importer_uid']));
                }
                // update last-child if it changes
                $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow');
                if ($allow && $allow[0]['data'] != $r[0]['last-child']) {
                    $r = q("UPDATE `item` SET `last-child` = %d , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", intval($allow[0]['data']), dbesc(datetime_convert()), dbesc($item_id), intval($importer['importer_uid']));
                }
                continue;
            }
            // This is my contact on another system, but it's really me.
            // Turn this into a wall post.
            if ($contact['remote_self']) {
                $datarray['wall'] = 1;
            }
            $datarray['parent-uri'] = $item_id;
            $datarray['uid'] = $importer['importer_uid'];
            $datarray['contact-id'] = $importer['id'];
            $r = item_store($datarray);
            continue;
        }
    }
    return 0;
    // NOTREACHED
}
Example #15
0
function fetch_xrd_links($url)
{
    logger('fetch_xrd_links: ' . $url);
    $redirects = 0;
    $x = z_fetch_url($url, false, $redirects, array('timeout' => 20));
    if (!$x['success']) {
        return array();
    }
    $xml = $x['body'];
    logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
    if (!$xml || !stristr($xml, '<xrd')) {
        return array();
    }
    // fix diaspora's bad xml
    $xml = str_replace(array('href=&quot;', '&quot;/>'), array('href="', '"/>'), $xml);
    $h = parse_xml_string($xml);
    if (!$h) {
        return array();
    }
    $arr = convert_xml_element_to_array($h);
    $links = array();
    if (isset($arr['xrd']['link'])) {
        $link = $arr['xrd']['link'];
        if (!isset($link[0])) {
            $links = array($link);
        } else {
            $links = $link;
        }
    }
    if (isset($arr['xrd']['alias'])) {
        $alias = $arr['xrd']['alias'];
        if (!isset($alias[0])) {
            $aliases = array($alias);
        } else {
            $aliases = $alias;
        }
        if (is_array($aliases) && count($aliases)) {
            foreach ($aliases as $alias) {
                $links[]['@attributes'] = array('rel' => 'alias', 'href' => $alias);
            }
        }
    }
    logger('fetch_xrd_links: ' . print_r($links, true), LOGGER_DATA);
    return $links;
}
Example #16
0
function get_diaspora_reshare_xml($url, $recurse = 0)
{
    $x = z_fetch_url($url);
    if (!$x['success']) {
        $x = z_fetch_url(str_replace('https://', 'http://', $url));
    }
    if (!$x['success']) {
        logger('get_diaspora_reshare_xml: unable to fetch source url ' . $url);
        return;
    }
    logger('get_diaspora_reshare_xml: source: ' . $x['body'], LOGGER_DEBUG);
    $source_xml = parse_xml_string($x['body'], false);
    if (!$source_xml) {
        logger('get_diaspora_reshare_xml: unparseable result from ' . $url);
        return '';
    }
    if ($source_xml->post->status_message) {
        return $source_xml;
    }
    // see if it's a reshare of a reshare
    if ($source_xml->post->reshare) {
        $xml = $source_xml->post->reshare;
    } else {
        return false;
    }
    if ($xml->root_diaspora_id && $xml->root_guid && $recurse < 15) {
        $orig_author = notags(unxmlify($xml->root_diaspora_id));
        $orig_guid = notags(unxmlify($xml->root_guid));
        $source_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/p/' . $orig_guid . '.xml';
        $y = get_diaspora_reshare_xml($source_url, $recurse + 1);
        if ($y) {
            return $y;
        }
    }
    return false;
}
Example #17
0
function diaspora_reshare($importer, $xml, $msg)
{
    logger('diaspora_reshare: init: ' . print_r($xml, true));
    $a = get_app();
    $guid = notags(unxmlify($xml->guid));
    $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
    if ($diaspora_handle != $msg['author']) {
        logger('diaspora_post: Potential forgery. Message handle is not the same as envelope sender.');
        return 202;
    }
    $contact = diaspora_get_contact_by_handle($importer['channel_id'], $diaspora_handle);
    if (!$contact) {
        return;
    }
    if (!perm_is_allowed($importer['channel_id'], $contact['xchan_hash'], 'send_stream')) {
        logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml, true));
        return 202;
    }
    $search_guid = strlen($guid) == 64 ? $guid . '%' : $guid;
    $r = q("SELECT id FROM item WHERE uid = %d AND mid like '%s' LIMIT 1", intval($importer['channel_id']), dbesc($search_guid));
    if ($r) {
        logger('diaspora_reshare: message exists: ' . $guid);
        return;
    }
    $orig_author = notags(unxmlify($xml->root_diaspora_id));
    $orig_guid = notags(unxmlify($xml->root_guid));
    $source_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/p/' . $orig_guid . '.xml';
    $orig_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/posts/' . $orig_guid;
    $x = z_fetch_url($source_url);
    if (!$x['success']) {
        $x = z_fetch_url(str_replace('https://', 'http://', $source_url));
    }
    if (!$x['success']) {
        logger('diaspora_reshare: unable to fetch source url ' . $source_url);
        return;
    }
    logger('diaspora_reshare: source: ' . $x['body']);
    $source_xml = parse_xml_string($x['body'], false);
    if ($source_xml->post->status_message) {
        $body = diaspora2bb($source_xml->post->status_message->raw_message);
        // Checking for embedded pictures
        if ($source_xml->post->status_message->photo->remote_photo_path && $source_xml->post->status_message->photo->remote_photo_name) {
            $remote_photo_path = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_path));
            $remote_photo_name = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_name));
            $body = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n" . $body;
            logger('diaspora_reshare: embedded picture link found: ' . $body, LOGGER_DEBUG);
        }
        $body = scale_external_images($body);
        // Add OEmbed and other information to the body
        //		$body = add_page_info_to_body($body, false, true);
    } else {
        // Maybe it is a reshare of a photo that will be delivered at a later time (testing)
        logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml, true));
        $body = "";
        //return;
    }
    //if(! $body) {
    //	logger('diaspora_reshare: empty body: source= ' . $x);
    //	return;
    //}
    $person = find_diaspora_person_by_handle($orig_author);
    if ($person) {
        $orig_author_name = $person['xchan_name'];
        $orig_author_link = $person['xchan_url'];
        $orig_author_photo = $person['xchan_photo_m'];
    }
    $newbody = "[share author='" . urlencode($orig_author_name) . "' profile='" . $orig_author_link . "' avatar='" . $orig_author_photo . "' link='" . $orig_url . "' posted='" . datetime_convert('UTC', 'UTC', unxmlify($sourcexml->post->status_message->created_at)) . "' message_id='" . unxmlify($source_xml->post->status_message->guid) . "]" . $body . "[/share]";
    $created = unxmlify($xml->created_at);
    $private = unxmlify($xml->public) == 'false' ? 1 : 0;
    $datarray = array();
    $str_tags = '';
    $tags = get_tags($newbody);
    if (count($tags)) {
        $datarray['term'] = array();
        foreach ($tags as $tag) {
            if (strpos($tag, '#') === 0) {
                if (strpos($tag, '[url=')) {
                    continue;
                }
                // don't link tags that are already embedded in links
                if (preg_match('/\\[(.*?)' . preg_quote($tag, '/') . '(.*?)\\]/', $newbody)) {
                    continue;
                }
                if (preg_match('/\\[(.*?)\\]\\((.*?)' . preg_quote($tag, '/') . '(.*?)\\)/', $newbody)) {
                    continue;
                }
                $basetag = str_replace('_', ' ', substr($tag, 1));
                $newbody = str_replace($tag, '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]', $newbody);
                $datarray['term'][] = array('uid' => $importer['channel_id'], 'type' => TERM_HASHTAG, 'otype' => TERM_OBJ_POST, 'term' => $basetag, 'url' => z_root() . '/search?tag=' . rawurlencode($basetag));
            }
        }
    }
    $cnt = preg_match_all('/@\\[url=(.*?)\\](.*?)\\[\\/url\\]/ism', $newbody, $matches, PREG_SET_ORDER);
    if ($cnt) {
        foreach ($matches as $mtch) {
            $datarray['term'][] = array('uid' => $importer['channel_id'], 'type' => TERM_MENTION, 'otype' => TERM_OBJ_POST, 'term' => $mtch[2], 'url' => $mtch[1]);
        }
    }
    // This won't work
    $plink = 'https://' . substr($diaspora_handle, strpos($diaspora_handle, '@') + 1) . '/posts/' . $guid;
    $datarray['uid'] = $importer['channel_id'];
    $datarray['mid'] = $datarray['parent_mid'] = $guid;
    $datarray['changed'] = $datarray['created'] = $datarray['edited'] = datetime_convert('UTC', 'UTC', $created);
    $datarray['item_private'] = $private;
    $datarray['plink'] = $plink;
    $datarray['owner_xchan'] = $contact['xchan_hash'];
    $datarray['author_xchan'] = $contact['xchan_hash'];
    $datarray['body'] = $newbody;
    $datarray['app'] = 'Diaspora';
    $result = item_store($datarray);
    return;
}
Example #18
0
function ping_init(&$a)
{
    header("Content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\t\t<result>";
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if (local_user()) {
        // Different login session than the page that is calling us.
        if (intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
            echo '<invalid>1</invalid></result>';
            killme();
        }
        $firehose = intval(get_pconfig(local_user(), 'system', 'notify_full'));
        $t = q("select count(*) as total from notify where uid = %d and seen = 0", intval(local_user()));
        if ($t && intval($t[0]['total']) > 49) {
            $z = q("select * from notify where uid = %d\n\t\t\t\tand seen = 0 order by date desc limit 0, 50", intval(local_user()));
            $sysnotify = $t[0]['total'];
        } else {
            $z1 = q("select * from notify where uid = %d\n\t\t\t\tand seen = 0 order by date desc limit 0, 50", intval(local_user()));
            $z2 = q("select * from notify where uid = %d\n\t\t\t\tand seen = 1 order by date desc limit 0, %d", intval(local_user()), intval(50 - intval($t[0]['total'])));
            $z = array_merge($z1, $z2);
            $sysnotify = 0;
            // we will update this in a moment
        }
        $tags = array();
        $comments = array();
        $likes = array();
        $dislikes = array();
        $friends = array();
        $posts = array();
        $home = 0;
        $network = 0;
        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,\n\t\t\t\t`item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0\n\t\t\t\tORDER BY `item`.`created` DESC", intval(local_user()));
        if (count($r)) {
            $arr = array('items' => $r);
            call_hooks('network_ping', $arr);
            foreach ($r as $it) {
                if ($it['wall']) {
                    $home++;
                } else {
                    $network++;
                }
                switch ($it['verb']) {
                    case ACTIVITY_TAG:
                        $obj = parse_xml_string($xmlhead . $it['object']);
                        $it['tname'] = $obj->content;
                        $tags[] = $it;
                        break;
                    case ACTIVITY_LIKE:
                        $likes[] = $it;
                        break;
                    case ACTIVITY_DISLIKE:
                        $dislikes[] = $it;
                        break;
                    case ACTIVITY_FRIEND:
                        $obj = parse_xml_string($xmlhead . $it['object']);
                        $it['fname'] = $obj->title;
                        $friends[] = $it;
                        break;
                    default:
                        if ($it['parent'] != $it['id']) {
                            $comments[] = $it;
                        } else {
                            if (!$it['wall']) {
                                $posts[] = $it;
                            }
                        }
                }
            }
        }
        $intros1 = q("SELECT  `intro`.`id`, `intro`.`datetime`,\n\t\t\t`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`\n\t\t\tFROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0", intval(local_user()));
        $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,\n\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`photo`\n\t\t\tFROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0", intval(local_user()));
        $intro = count($intros1) + count($intros2);
        $intros = $intros1 + $intros2;
        $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
        $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`\n\t\t\tWHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", intval(local_user()), dbesc($myurl));
        if ($mails) {
            $mail = $mails[0]['total'];
        }
        if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
            $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
            if ($regs) {
                $register = $regs[0]['total'];
            }
        } else {
            $register = "0";
        }
        $all_events = 0;
        $all_events_today = 0;
        $events = 0;
        $events_today = 0;
        $birthdays = 0;
        $birthdays_today = 0;
        $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`\n\t\t\tWHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0\n\t\t\tORDER BY `start` ASC ", intval(local_user()), dbesc(datetime_convert('UTC', 'UTC', 'now + 7 days')), dbesc(datetime_convert('UTC', 'UTC', 'now')));
        if ($ev && count($ev)) {
            $all_events = intval($ev[0]['total']);
            if ($all_events) {
                $str_now = datetime_convert('UTC', $a->timezone, 'now', 'Y-m-d');
                foreach ($ev as $x) {
                    $bd = false;
                    if ($x['type'] === 'birthday') {
                        $birthdays++;
                        $bd = true;
                    } else {
                        $events++;
                    }
                    if (datetime_convert('UTC', intval($x['adjust']) ? $a->timezone : 'UTC', $x['start'], 'Y-m-d') === $str_now) {
                        $all_events_today++;
                        if ($bd) {
                            $birthdays_today++;
                        } else {
                            $events_today++;
                        }
                    }
                }
            }
        }
        function xmlize($href, $name, $url, $photo, $date, $seen, $message)
        {
            require_once "mod/proxy.php";
            $photo = proxy_url($photo);
            $data = array('href' => &$href, 'name' => &$name, 'url' => &$url, 'photo' => &$photo, 'date' => &$date, 'seen' => &$seen, 'messsage' => &$message);
            call_hooks('ping_xmlize', $data);
            $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" >%s</note>';
            return sprintf($notsxml, xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($seen), xmlify($message));
        }
        echo "<intro>{$intro}</intro>\n\t\t\t\t<mail>{$mail}</mail>\n\t\t\t\t<net>{$network}</net>\n\t\t\t\t<home>{$home}</home>\r\n";
        if ($register != 0) {
            echo "<register>{$register}</register>";
        }
        echo "<all-events>{$all_events}</all-events>\n\t\t\t<all-events-today>{$all_events_today}</all-events-today>\n\t\t\t<events>{$events}</events>\n\t\t\t<events-today>{$events_today}</events-today>\n\t\t\t<birthdays>{$birthdays}</birthdays>\n\t\t\t<birthdays-today>{$birthdays_today}</birthdays-today>\r\n";
        $tot = $mail + $intro + $register + count($comments) + count($likes) + count($dislikes) + count($friends) + count($posts) + count($tags);
        require_once 'include/bbcode.php';
        if ($firehose) {
            echo '	<notif count="' . $tot . '">';
        } else {
            if (count($z) && !$sysnotify) {
                foreach ($z as $zz) {
                    if ($zz['seen'] == 0) {
                        $sysnotify++;
                    }
                }
            }
            echo '	<notif count="' . $sysnotify . '">';
            if (count($z)) {
                foreach ($z as $zz) {
                    echo xmlize($a->get_baseurl() . '/notify/view/' . $zz['id'], $zz['name'], $zz['url'], $zz['photo'], relative_date($zz['date']), $zz['seen'] ? 'notify-seen' : 'notify-unseen', ($zz['seen'] ? '' : '&rarr; ') . strip_tags(bbcode($zz['msg'])));
                }
            }
        }
        if ($firehose) {
            if ($intro > 0) {
                foreach ($intros as $i) {
                    echo xmlize($a->get_baseurl() . '/notifications/intros/' . $i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), 'notify-unseen', t("{0} wants to be your friend"));
                }
            }
            if ($mail > 0) {
                foreach ($mails as $i) {
                    echo xmlize($a->get_baseurl() . '/message/' . $i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), 'notify-unseen', t("{0} sent you a message"));
                }
            }
            if ($register > 0) {
                foreach ($regs as $i) {
                    echo xmlize($a->get_baseurl() . '/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), 'notify-unseen', t("{0} requested registration"));
                }
            }
            if (count($comments)) {
                foreach ($comments as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} commented %s's post"), $i['pname']));
                }
            }
            if (count($likes)) {
                foreach ($likes as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} liked %s's post"), $i['pname']));
                }
            }
            if (count($dislikes)) {
                foreach ($dislikes as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} disliked %s's post"), $i['pname']));
                }
            }
            if (count($friends)) {
                foreach ($friends as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} is now friends with %s"), $i['fname']));
                }
            }
            if (count($posts)) {
                foreach ($posts as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} posted")));
                }
            }
            if (count($tags)) {
                foreach ($tags as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', sprintf(t("{0} tagged %s's post with #%s"), $i['pname'], $i['tname']));
                }
            }
            if (count($cit)) {
                foreach ($cit as $i) {
                    echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), 'notify-unseen', t("{0} mentioned you in a post"));
                }
            }
        }
        echo "  </notif>";
    }
    echo " <sysmsgs>";
    if (x($_SESSION, 'sysmsg')) {
        foreach ($_SESSION['sysmsg'] as $m) {
            echo "<notice>" . xmlify($m) . "</notice>";
        }
        unset($_SESSION['sysmsg']);
    }
    if (x($_SESSION, 'sysmsg_info')) {
        foreach ($_SESSION['sysmsg_info'] as $m) {
            echo "<info>" . xmlify($m) . "</info>";
        }
        unset($_SESSION['sysmsg_info']);
    }
    echo " </sysmsgs>";
    echo "</result>\n\t";
    killme();
}
Example #19
0
function ping_init(&$a)
{
    header("Content-type: text/xml");
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\t\t<result>";
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if (local_user()) {
        $tags = array();
        $comments = array();
        $likes = array();
        $dislikes = array();
        $friends = array();
        $posts = array();
        $cit = array();
        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, \n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, `item`.`body`, \n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` \n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 \n\t\t\t\tORDER BY `item`.`created` DESC", intval(local_user()));
        $network = count($r);
        foreach ($r as $it) {
            switch ($it['verb']) {
                case ACTIVITY_TAG:
                    $obj = parse_xml_string($xmlhead . $it['object']);
                    $it['tname'] = $obj->content;
                    $tags[] = $it;
                    break;
                case ACTIVITY_LIKE:
                    $likes[] = $it;
                    break;
                case ACTIVITY_DISLIKE:
                    $dislikes[] = $it;
                    break;
                case ACTIVITY_FRIEND:
                    $obj = parse_xml_string($xmlhead . $it['object']);
                    $it['fname'] = $obj->title;
                    $friends[] = $it;
                    break;
                default:
                    $reg = "|@\\[url=" . $a->get_baseurl() . "/profile/" . $a->user['nickname'] . "|";
                    if ($it['parent'] != $it['id']) {
                        $comments[] = $it;
                    } else {
                        if (preg_match($reg, $it['body'])) {
                            $cit[] = $it;
                        } else {
                            $posts[] = $it;
                        }
                    }
            }
        }
        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`, \n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`, \n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink` \n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1", intval(local_user()));
        $home = count($r);
        foreach ($r as $it) {
            switch ($it['verb']) {
                case ACTIVITY_TAG:
                    $obj = parse_xml_string($xmlhead . $it['object']);
                    $it['tname'] = $obj->content;
                    $tags[] = $it;
                    break;
                case ACTIVITY_LIKE:
                    $likes[] = $it;
                    break;
                case ACTIVITY_DISLIKE:
                    $dislikes[] = $it;
                    break;
                case ACTIVITY_FRIEND:
                    $obj = parse_xml_string($xmlhead . $it['object']);
                    $it['fname'] = $obj->title;
                    $friends[] = $it;
                    break;
                default:
                    if ($it['parent'] != $it['id']) {
                        $comments[] = $it;
                    }
                    if (preg_match("/@\\[[^]]*\\]" . $a->user['username'] . "/", $it['body'])) {
                        $cit[] = $it;
                    }
            }
        }
        $intros1 = q("SELECT COUNT(`intro`.`id`) AS `total`, `intro`.`id`, `intro`.`datetime`, \n\t\t\t`fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` \n\t\t\tFROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0", intval(local_user()));
        $intros2 = q("SELECT COUNT(`intro`.`id`) AS `total`, `intro`.`id`, `intro`.`datetime`, \n\t\t\t`contact`.`name`, `contact`.`url`, `contact`.`photo` \n\t\t\tFROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d  AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0", intval(local_user()));
        $intro = $intros1[0]['total'] + $intros2[0]['total'];
        if ($intros1[0]['total'] == 0) {
            $intros1 = array();
        }
        if ($intros2[0]['total'] == 0) {
            $intros2 = array();
        }
        $intros = $intros1 + $intros2;
        $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
        $mails = q("SELECT *,  COUNT(*) AS `total` FROM `mail`\n\t\t\tWHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ", intval(local_user()), dbesc($myurl));
        $mail = $mails[0]['total'];
        if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()) {
            $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
            $register = $regs[0]['total'];
        } else {
            $register = "0";
        }
        function xmlize($href, $name, $url, $photo, $date, $message)
        {
            $notsxml = '<note href="%s" name="%s" url="%s" photo="%s" date="%s">%s</note>';
            return sprintf($notsxml, xmlify($href), xmlify($name), xmlify($url), xmlify($photo), xmlify($date), xmlify($message));
        }
        echo "<intro>{$intro}</intro>\n\t\t\t\t<mail>{$mail}</mail>\n\t\t\t\t<net>{$network}</net>\n\t\t\t\t<home>{$home}</home>";
        if ($register != 0) {
            echo "<register>{$register}</register>";
        }
        $tot = $mail + $intro + $register + count($comments) + count($likes) + count($dislikes) + count($friends) + count($posts) + count($tags) + count($cit);
        echo '	<notif count="' . $tot . '">';
        if ($intro > 0) {
            foreach ($intros as $i) {
                echo xmlize($a->get_baseurl() . '/notifications/intros/' . $i['id'], $i['name'], $i['url'], $i['photo'], relative_date($i['datetime']), t("{0} wants to be your friend"));
            }
        }
        if ($mail > 0) {
            foreach ($mails as $i) {
                echo xmlize($a->get_baseurl() . '/message/' . $i['id'], $i['from-name'], $i['from-url'], $i['from-photo'], relative_date($i['created']), t("{0} sent you a message"));
            }
        }
        if ($register > 0) {
            foreach ($regs as $i) {
                echo xmlize($a->get_baseurl() . '/admin/users/', $i['name'], $i['url'], $i['micro'], relative_date($i['created']), t("{0} requested registration"));
            }
        }
        if (count($comments)) {
            foreach ($comments as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf(t("{0} commented %s's post"), $i['pname']));
            }
        }
        if (count($likes)) {
            foreach ($likes as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf(t("{0} liked %s's post"), $i['pname']));
            }
        }
        if (count($dislikes)) {
            foreach ($dislikes as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf(t("{0} disliked %s's post"), $i['pname']));
            }
        }
        if (count($friends)) {
            foreach ($friends as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf(t("{0} is now friends with %s"), $i['fname']));
            }
        }
        if (count($posts)) {
            foreach ($posts as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), t("{0} posted"));
            }
        }
        if (count($tags)) {
            foreach ($tags as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), sprintf(t("{0} tagged %s's post with #%s"), $i['pname'], $i['tname']));
            }
        }
        if (count($cit)) {
            foreach ($cit as $i) {
                echo xmlize($a->get_baseurl() . '/display/' . $a->user['nickname'] . "/" . $i['parent'], $i['author-name'], $i['author-link'], $i['author-avatar'], relative_date($i['created']), t("{0} mentioned you in a post"));
            }
        }
        echo "  </notif>";
    }
    echo " <sysmsgs>";
    if (x($_SESSION, 'sysmsg')) {
        foreach ($_SESSION['sysmsg'] as $m) {
            echo "<notice>" . xmlify($m) . "</notice>";
        }
        unset($_SESSION['sysmsg']);
    }
    if (x($_SESSION, 'sysmsg_info')) {
        foreach ($_SESSION['sysmsg_info'] as $m) {
            echo "<info>" . xmlify($m) . "</info>";
        }
        unset($_SESSION['sysmsg_info']);
    }
    echo " </sysmsgs>";
    echo "</result>\n\t";
    killme();
}
Example #20
0
function diaspora_reshare($importer, $xml)
{
    logger('diaspora_reshare: init: ' . print_r($xml, true));
    $a = get_app();
    $guid = notags(unxmlify($xml->guid));
    $diaspora_handle = notags(unxmlify($xml->diaspora_handle));
    $contact = diaspora_get_contact_by_handle($importer['uid'], $diaspora_handle);
    if (!$contact) {
        return;
    }
    if ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['blocked'] || $contact['readonly']) {
        logger('diaspora_reshare: Ignoring this author: ' . $diaspora_handle . ' ' . print_r($xml, true));
        return 202;
    }
    $message_id = $diaspora_handle . ':' . $guid;
    $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `guid` = '%s' LIMIT 1", intval($importer['uid']), dbesc($message_id), dbesc($guid));
    if (count($r)) {
        logger('diaspora_reshare: message exists: ' . $guid);
        return;
    }
    $orig_author = notags(unxmlify($xml->root_diaspora_id));
    $orig_guid = notags(unxmlify($xml->root_guid));
    $source_url = 'https://' . substr($orig_author, strpos($orig_author, '@') + 1) . '/p/' . $orig_guid . '.xml';
    $x = fetch_url($source_url);
    if (!$x) {
        $x = fetch_url(str_replace('https://', 'http://', $source_url));
    }
    if (!$x) {
        logger('diaspora_reshare: unable to fetch source url ' . $source_url);
        return;
    }
    logger('diaspora_reshare: source: ' . $x);
    $x = str_replace(array('<activity_streams-photo>', '</activity_streams-photo>'), array('<asphoto>', '</asphoto>'), $x);
    $source_xml = parse_xml_string($x, false);
    if (strlen($source_xml->post->asphoto->objectId) && $source_xml->post->asphoto->objectId != 0 && $source_xml->post->asphoto->image_url) {
        $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
        $body = scale_diaspora_images($body, false);
    } elseif ($source_xml->post->asphoto->image_url) {
        $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
        $body = scale_diaspora_images($body);
    } elseif ($source_xml->post->status_message) {
        $body = diaspora2bb($source_xml->post->status_message->raw_message);
        $body = scale_diaspora_images($body);
    } else {
        logger('diaspora_reshare: no reshare content found: ' . print_r($source_xml, true));
        return;
    }
    if (!$body) {
        logger('diaspora_reshare: empty body: source= ' . $x);
        return;
    }
    $person = find_diaspora_person_by_handle($orig_author);
    if (is_array($person) && x($person, 'name') && x($person, 'url')) {
        $details = '[url=' . $person['url'] . ']' . $person['name'] . '[/url]';
    } else {
        $details = $orig_author;
    }
    $prefix = '&#x2672; ' . $details . "\n";
    // allocate a guid on our system - we aren't fixing any collisions.
    // we're ignoring them
    $g = q("select * from guid where guid = '%s' limit 1", dbesc($guid));
    if (!count($g)) {
        q("insert into guid ( guid ) values ( '%s' )", dbesc($guid));
    }
    $created = unxmlify($xml->created_at);
    $private = unxmlify($xml->public) == 'false' ? 1 : 0;
    $datarray = array();
    $str_tags = '';
    $tags = get_tags($body);
    if (count($tags)) {
        foreach ($tags as $tag) {
            if (strpos($tag, '#') === 0) {
                if (strpos($tag, '[url=')) {
                    continue;
                }
                $basetag = str_replace('_', ' ', substr($tag, 1));
                $body = str_replace($tag, '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]', $body);
                if (strlen($str_tags)) {
                    $str_tags .= ',';
                }
                $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
                continue;
            }
        }
    }
    $datarray['uid'] = $importer['uid'];
    $datarray['contact-id'] = $contact['id'];
    $datarray['wall'] = 0;
    $datarray['guid'] = $guid;
    $datarray['uri'] = $datarray['parent-uri'] = $message_id;
    $datarray['created'] = $datarray['edited'] = datetime_convert('UTC', 'UTC', $created);
    $datarray['private'] = $private;
    $datarray['parent'] = 0;
    $datarray['owner-name'] = $contact['name'];
    $datarray['owner-link'] = $contact['url'];
    $datarray['owner-avatar'] = $contact['thumb'];
    $datarray['author-name'] = $contact['name'];
    $datarray['author-link'] = $contact['url'];
    $datarray['author-avatar'] = $contact['thumb'];
    $datarray['body'] = $prefix . $body;
    $datarray['tag'] = $str_tags;
    $datarray['app'] = 'Diaspora';
    $message_id = item_store($datarray);
    if ($message_id) {
        q("update item set plink = '%s' where id = %d limit 1", dbesc($a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $message_id), intval($message_id));
    }
    return;
}
Example #21
0
/**
 * Render actions localized
 */
function localize_item(&$item)
{
    $extracted = item_extract_images($item['body']);
    if ($extracted['images']) {
        $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
    }
    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
    if (activity_match($item['verb'], ACTIVITY_LIKE) || activity_match($item['verb'], ACTIVITY_DISLIKE)) {
        $r = q("SELECT * from `item`,`contact` WHERE\n\t\t\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . $obj['author-link'] . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        if (activity_match($item['verb'], ACTIVITY_LIKE)) {
            $bodyverb = t('%1$s likes %2$s\'s %3$s');
        } elseif (activity_match($item['verb'], ACTIVITY_DISLIKE)) {
            $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
        }
        $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
    }
    if (activity_match($item['verb'], ACTIVITY_FRIEND)) {
        if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>");
        $Bname = $obj->title;
        $Blink = "";
        $Bphoto = "";
        foreach ($links->link as $l) {
            $atts = $l->attributes();
            switch ($atts['rel']) {
                case "alternate":
                    $Blink = $atts['href'];
                case "photo":
                    $Bphoto = $atts['href'];
            }
        }
        $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
        $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
        if ($Bphoto != "") {
            $Bphoto = '[url=' . zrl($Blink) . '][img]' . $Bphoto . '[/img][/url]';
        }
        $item['body'] = sprintf(t('%1$s is now friends with %2$s'), $A, $B) . "\n\n\n" . $Bphoto;
    }
    if (stristr($item['verb'], ACTIVITY_POKE)) {
        $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1));
        if (!$verb) {
            return;
        }
        if ($item['object-type'] == "" || $item['object-type'] !== ACTIVITY_OBJ_PERSON) {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        $links = parse_xml_string($xmlhead . "<links>" . unxmlify($obj->link) . "</links>");
        $Bname = $obj->title;
        $Blink = "";
        $Bphoto = "";
        foreach ($links->link as $l) {
            $atts = $l->attributes();
            switch ($atts['rel']) {
                case "alternate":
                    $Blink = $atts['href'];
                case "photo":
                    $Bphoto = $atts['href'];
            }
        }
        $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
        $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
        if ($Bphoto != "") {
            $Bphoto = '[url=' . zrl($Blink) . '][img=80x80]' . $Bphoto . '[/img][/url]';
        }
        // we can't have a translation string with three positions but no distinguishable text
        // So here is the translate string.
        $txt = t('%1$s poked %2$s');
        // now translate the verb
        $txt = str_replace(t('poked'), t($verb), $txt);
        // then do the sprintf on the translation string
        $item['body'] = sprintf($txt, $A, $B) . "\n\n\n" . $Bphoto;
    }
    if (stristr($item['verb'], ACTIVITY_MOOD)) {
        $verb = urldecode(substr($item['verb'], strpos($item['verb'], '#') + 1));
        if (!$verb) {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
        $txt = t('%1$s is currently %2$s');
        $item['body'] = sprintf($txt, $A, t($verb));
    }
    if (activity_match($item['verb'], ACTIVITY_TAG)) {
        $r = q("SELECT * from `item`,`contact` WHERE\n\t\t`item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';", dbesc($item['parent-uri']));
        if (count($r) == 0) {
            return;
        }
        $obj = $r[0];
        $author = '[url=' . zrl($item['author-link']) . ']' . $item['author-name'] . '[/url]';
        $objauthor = '[url=' . zrl($obj['author-link']) . ']' . $obj['author-name'] . '[/url]';
        switch ($obj['verb']) {
            case ACTIVITY_POST:
                switch ($obj['object-type']) {
                    case ACTIVITY_OBJ_EVENT:
                        $post_type = t('event');
                        break;
                    default:
                        $post_type = t('status');
                }
                break;
            default:
                if ($obj['resource-id']) {
                    $post_type = t('photo');
                    $m = array();
                    preg_match("/\\[url=([^]]*)\\]/", $obj['body'], $m);
                    $rr['plink'] = $m[1];
                } else {
                    $post_type = t('status');
                }
        }
        $plink = '[url=' . $obj['plink'] . ']' . $post_type . '[/url]';
        $parsedobj = parse_xml_string($xmlhead . $item['object']);
        $tag = sprintf('#[url=%s]%s[/url]', $parsedobj->id, $parsedobj->content);
        $item['body'] = sprintf(t('%1$s tagged %2$s\'s %3$s with %4$s'), $author, $objauthor, $plink, $tag);
    }
    if (activity_match($item['verb'], ACTIVITY_FAVORITE)) {
        if ($item['object-type'] == "") {
            return;
        }
        $Aname = $item['author-name'];
        $Alink = $item['author-link'];
        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
        $obj = parse_xml_string($xmlhead . $item['object']);
        if (strlen($obj->id)) {
            $r = q("select * from item where uri = '%s' and uid = %d limit 1", dbesc($obj->id), intval($item['uid']));
            if (count($r) && $r[0]['plink']) {
                $target = $r[0];
                $Bname = $target['author-name'];
                $Blink = $target['author-link'];
                $A = '[url=' . zrl($Alink) . ']' . $Aname . '[/url]';
                $B = '[url=' . zrl($Blink) . ']' . $Bname . '[/url]';
                $P = '[url=' . $target['plink'] . ']' . t('post/item') . '[/url]';
                $item['body'] = sprintf(t('%1$s marked %2$s\'s %3$s as favorite'), $A, $B, $P) . "\n";
            }
        }
    }
    $matches = null;
    if (preg_match_all('/@\\[url=(.*?)\\]/is', $item['body'], $matches, PREG_SET_ORDER)) {
        foreach ($matches as $mtch) {
            if (!strpos($mtch[1], 'zrl=')) {
                $item['body'] = str_replace($mtch[0], '@[url=' . zrl($mtch[1]) . ']', $item['body']);
            }
        }
    }
    // add zrl's to public images
    $photo_pattern = "/\\[url=(.*?)\\/photos\\/(.*?)\\/image\\/(.*?)\\]\\[img(.*?)\\]h(.*?)\\[\\/img\\]\\[\\/url\\]/is";
    if (preg_match($photo_pattern, $item['body'])) {
        $photo_replace = '[url=' . zrl('$1' . '/photos/' . '$2' . '/image/' . '$3', true) . '][img' . '$4' . ']h' . '$5' . '[/img][/url]';
        $item['body'] = bb_tag_preg_replace($photo_pattern, $photo_replace, 'url', $item['body']);
    }
    // add sparkle links to appropriate permalinks
    $x = stristr($item['plink'], '/display/');
    if ($x) {
        $sparkle = false;
        $y = best_link_url($item, $sparkle, true);
        if (strstr($y, '/redir/')) {
            $item['plink'] = $y . '?f=&url=' . $item['plink'];
        }
    }
}
Example #22
0
function geonames_post_hook($a, &$item)
{
    /**
     *
     * An item was posted on the local system.
     * We are going to look for specific items:
     *      - A status post by a profile owner
     *      - The profile owner must have allowed our plugin
     *
     */
    logger('geonames invoked');
    if (!local_user()) {
        /* non-zero if this is a logged in user of this system */
        return;
    }
    if (local_user() != $item['uid']) {
        /* Does this person own the post? */
        return;
    }
    if ($item['parent']) {
        /* If the item has a parent, this is a comment or something else, not a status post. */
        return;
    }
    /* Retrieve our personal config setting */
    $geo_account = get_config('geonames', 'username');
    $active = get_pconfig(local_user(), 'geonames', 'enable');
    if (!$geo_account || !$active) {
        return;
    }
    if (!$item['coord'] || $item['location']) {
        return;
    }
    $coords = explode(' ', $item['coord']);
    /**
     *
     * OK, we're allowed to do our stuff.
     *
     */
    $s = fetch_url('http://api.geonames.org/findNearbyPlaceName?lat=' . $coords[0] . '&lng=' . $coords[1] . '&username='******'location'] = $xml->geoname->name . ', ' . $xml->geoname->countryName;
    }
    //	logger('geonames : ' . print_r($xml,true), LOGGER_DATA);
    return;
}
Example #23
0
function diaspora_fetch_message($guid, $server, $level = 0)
{
    if ($level > 5) {
        return false;
    }
    $a = get_app();
    // This will not work if the server is not a Diaspora server
    $source_url = $server . '/p/' . $guid . '.xml';
    $x = fetch_url($source_url);
    if (!$x) {
        return false;
    }
    $x = str_replace(array('<activity_streams-photo>', '</activity_streams-photo>'), array('<asphoto>', '</asphoto>'), $x);
    $source_xml = parse_xml_string($x, false);
    $item = array();
    $item["app"] = 'Diaspora';
    $item["guid"] = $guid;
    $body = "";
    if ($source_xml->post->status_message->created_at) {
        $item["created"] = unxmlify($source_xml->post->status_message->created_at);
    }
    if ($source_xml->post->status_message->provider_display_name) {
        $item["app"] = unxmlify($source_xml->post->status_message->provider_display_name);
    }
    if ($source_xml->post->status_message->diaspora_handle) {
        $item["author"] = unxmlify($source_xml->post->status_message->diaspora_handle);
    }
    if ($source_xml->post->status_message->guid) {
        $item["guid"] = unxmlify($source_xml->post->status_message->guid);
    }
    $item["private"] = unxmlify($source_xml->post->status_message->public) == 'false';
    $item["object"] = json_encode($source_xml->post);
    if (strlen($source_xml->post->asphoto->objectId) && $source_xml->post->asphoto->objectId != 0 && $source_xml->post->asphoto->image_url) {
        $item["object-type"] = ACTIVITY_OBJ_PHOTO;
        $body = '[url=' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '][img]' . notags(unxmlify($source_xml->post->asphoto->objectId)) . '[/img][/url]' . "\n";
        $body = scale_external_images($body, false);
    } elseif ($source_xml->post->asphoto->image_url) {
        $item["object-type"] = ACTIVITY_OBJ_PHOTO;
        $body = '[img]' . notags(unxmlify($source_xml->post->asphoto->image_url)) . '[/img]' . "\n";
        $body = scale_external_images($body);
    } elseif ($source_xml->post->status_message) {
        $body = diaspora2bb($source_xml->post->status_message->raw_message);
        // Checking for embedded pictures
        if ($source_xml->post->status_message->photo->remote_photo_path and $source_xml->post->status_message->photo->remote_photo_name) {
            $item["object-type"] = ACTIVITY_OBJ_PHOTO;
            $remote_photo_path = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_path));
            $remote_photo_name = notags(unxmlify($source_xml->post->status_message->photo->remote_photo_name));
            $body = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n" . $body;
            logger('embedded picture link found: ' . $body, LOGGER_DEBUG);
        } else {
            $item["object-type"] = ACTIVITY_OBJ_NOTE;
        }
        $body = scale_external_images($body);
        // Add OEmbed and other information to the body
        // To-Do: It could be a repeated redmatrix item
        // Then we shouldn't add further data to it
        if ($item["object-type"] == ACTIVITY_OBJ_NOTE) {
            $body = add_page_info_to_body($body, false, true);
        }
    } elseif ($source_xml->post->reshare) {
        // Reshare of a reshare
        return diaspora_fetch_message($source_xml->post->reshare->root_guid, $server, ++$level);
    } else {
        // Maybe it is a reshare of a photo that will be delivered at a later time (testing)
        logger('no content found: ' . print_r($source_xml, true));
        return false;
    }
    if (trim($body) == "") {
        return false;
    }
    $item["tag"] = '';
    $item["body"] = $body;
    return $item;
}
Example #24
0
 function fetch_xrd_links($url)
 {
     $xrd_timeout = intval(get_config('system', 'xrd_timeout'));
     $redirects = 0;
     $xml = fetch_url($url, false, $redirects, $xrd_timeout ? $xrd_timeout : 20);
     logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
     if (!$xml || !stristr($xml, '<xrd')) {
         return array();
     }
     $h = parse_xml_string($xml);
     if (!$h) {
         return array();
     }
     $arr = convert_xml_element_to_array($h);
     $links = array();
     if (isset($arr['xrd']['link'])) {
         $link = $arr['xrd']['link'];
         if (!isset($link[0])) {
             $links = array($link);
         } else {
             $links = $link;
         }
     }
     if (isset($arr['xrd']['alias'])) {
         $alias = $arr['xrd']['alias'];
         if (!isset($alias[0])) {
             $aliases = array($alias);
         } else {
             $aliases = $alias;
         }
         if (is_array($aliases) && count($aliases)) {
             foreach ($aliases as $alias) {
                 $links[]['@attributes'] = array('rel' => 'alias', 'href' => $alias);
             }
         }
     }
     logger('fetch_xrd_links: ' . print_r($links, true), LOGGER_DATA);
     return $links;
 }