示例#1
0
function event_addtocal($item_id, $uid)
{
    $c = q("select * from channel where channel_id = %d limit 1", intval($uid));
    if (!$c) {
        return false;
    }
    $channel = $c[0];
    $r = q("select * from item where id = %d and uid = %d limit 1", intval($item_id), intval($channel['channel_id']));
    if (!$r || $r[0]['obj_type'] !== ACTIVITY_OBJ_EVENT) {
        return false;
    }
    $item = $r[0];
    $ev = bbtoevent($r[0]['body']);
    if (x($ev, 'summary') && x($ev, 'start')) {
        $ev['event_xchan'] = $item['author_xchan'];
        $ev['uid'] = $channel['channel_id'];
        $ev['account'] = $channel['channel_account_id'];
        $ev['edited'] = $item['edited'];
        $ev['mid'] = $item['mid'];
        $ev['private'] = $item['item_private'];
        // is this an edit?
        if ($item['resource_type'] === 'event') {
            $ev['event_hash'] = $item['resource_id'];
        }
        $event = event_store_event($ev);
        if ($event) {
            $r = q("update item set resource_id = '%s', resource_type = 'event' where id = %d and uid = %d", dbesc($event['event_hash']), intval($item['id']), intval($channel['channel_id']));
            $item['resource_id'] = $event['event_hash'];
            $item['resource_type'] = 'event';
            $i = array($item);
            xchan_query($i);
            $sync_item = fetch_post_tags($i);
            $z = q("select * from event where event_hash = '%s' and uid = %d limit 1", dbesc($event['event_hash']), intval($channel['channel_id']));
            if ($z) {
                build_sync_packet($channel['channel_id'], array('event_item' => array(encode_item($sync_item[0], true)), 'event' => $z));
            }
            return true;
        }
    }
    return false;
}
示例#2
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
}
示例#3
0
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $simplehtml = false, $forplaintext = false)
{
    $a = get_app();
    // Hide all [noparse] contained bbtags by spacefying them
    // POSSIBLE BUG --> Will the 'preg' functions crash if there's an embedded image?
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    // Move all spaces out of the tags
    $Text = preg_replace("/\\[(\\w*)\\](\\s*)/ism", '$2[$1]', $Text);
    $Text = preg_replace("/(\\s*)\\[\\/(\\w*)\\]/ism", '[/$2]$1', $Text);
    // Extract the private images which use data urls since preg has issues with
    // large data sizes. Stash them away while we do bbcode conversion, and then put them back
    // in after we've done all the regex matching. We cannot use any preg functions to do this.
    $extracted = bb_extract_images($Text);
    $Text = $extracted['body'];
    $saved_image = $extracted['images'];
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // remove some newlines before the general conversion
    $Text = preg_replace("/\\s?\\[share(.*?)\\]\\s?(.*?)\\s?\\[\\/share\\]\\s?/ism", "[share\$1]\$2[/share]", $Text);
    $Text = preg_replace("/\\s?\\[quote(.*?)\\]\\s?(.*?)\\s?\\[\\/quote\\]\\s?/ism", "[quote\$1]\$2[/quote]", $Text);
    $Text = preg_replace("/\n\\[code\\]/ism", "[code]", $Text);
    $Text = preg_replace("/\\[\\/code\\]\n/ism", "[/code]", $Text);
    // Rearrange shares to attachments
    $Text = preg_replace_callback("((.*?)\\[class=(.*?)\\](.*?)\\[\\/class\\])ism", "bb_rearrange_share", $Text);
    // when the content is meant exporting to other systems then remove the avatar picture since this doesn't really look good on these systems
    if (!$tryoembed) {
        $Text = preg_replace("/\\[share(.*?)avatar\\s?=\\s?'.*?'\\s?(.*?)\\]\\s?(.*?)\\s?\\[\\/share\\]\\s?/ism", "\n[share\$1\$2]\$3[/share]", $Text);
    }
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = trim($Text);
    $Text = str_replace("\r\n", "\n", $Text);
    // removing multiplicated newlines
    if (get_config("system", "remove_multiplicated_lines")) {
        $search = array("\n\n\n", "\n ", " \n", "[/quote]\n\n", "\n[/quote]", "[/li]\n", "\n[li]", "\n[ul]", "[/ul]\n", "\n\n[share ", "[/attachment]\n", "\n[h1]", "[/h1]\n", "\n[h2]", "[/h2]\n", "\n[h3]", "[/h3]\n", "\n[h4]", "[/h4]\n", "\n[h5]", "[/h5]\n", "\n[h6]", "[/h6]\n");
        $replace = array("\n\n", "\n", "\n", "[/quote]\n", "[/quote]", "[/li]", "[li]", "[ul]", "[/ul]", "\n[share ", "[/attachment]", "[h1]", "[/h1]", "[h2]", "[/h2]", "[h3]", "[/h3]", "[h4]", "[/h4]", "[h5]", "[/h5]", "[h6]", "[/h6]");
        do {
            $oldtext = $Text;
            $Text = str_replace($search, $replace, $Text);
        } while ($oldtext != $Text);
    }
    // Handle attached links or videos
    $Text = bb_attachment($Text, $simplehtml, $tryoembed);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Remove all hashtag addresses
    if ((!$tryoembed or $simplehtml) and !in_array($simplehtml, array(3, 7))) {
        $Text = preg_replace("/([#@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1$3', $Text);
    } elseif ($simplehtml == 3) {
        $Text = preg_replace("/([@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1<a href="$2">$3</a>', $Text);
    } elseif ($simplehtml == 7) {
        $Text = preg_replace("/([@])\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '$1<span class="vcard"><a href="$2" class="url" title="$3"><span class="fn nickname mention">$3</span></a></span>', $Text);
    }
    // Bookmarks in red - will be converted to bookmarks in friendica
    $Text = preg_replace("/#\\^\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '[bookmark=$1]$1[/bookmark]', $Text);
    $Text = preg_replace("/#\\^\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '[bookmark=$1]$2[/bookmark]', $Text);
    $Text = preg_replace("/#\\[url\\=[{$URLSearchString}]*\\]\\^\\[\\/url\\]\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/i", "[bookmark=\$1]\$2[/bookmark]", $Text);
    if (in_array($simplehtml, array(2, 6, 7, 8, 9))) {
        $Text = preg_replace_callback("/([^#@])\\[url\\=([^\\]]*)\\](.*?)\\[\\/url\\]/ism", "bb_expand_links", $Text);
        //$Text = preg_replace("/[^#@]\[url\=([^\]]*)\](.*?)\[\/url\]/ism",' $2 [url]$1[/url]',$Text);
        $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", ' $2 [url]$1[/url]', $Text);
    }
    if ($simplehtml == 5) {
        $Text = preg_replace("/[^#@]\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '[url]$1[/url]', $Text);
    }
    // Perform URL Search
    if ($tryoembed) {
        $Text = preg_replace_callback("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", 'tryoembed', $Text);
    }
    if ($simplehtml == 5) {
        $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url]$1[/url]', $Text);
    } else {
        $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url=$1]$2[/url]', $Text);
    }
    // Handle Diaspora posts
    $Text = preg_replace_callback("&\\[url=/posts/([^\\[\\]]*)\\](.*)\\[\\/url\\]&Usi", 'bb_DiasporaLinks', $Text);
    // if the HTML is used to generate plain text, then don't do this search, but replace all URL of that kind to text
    //	if ($simplehtml != 7) {
    if (!$forplaintext) {
        $Text = preg_replace("/([^\\]\\='" . '"' . "]|^)(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1<a href="$2" target="_blank">$2</a>', $Text);
    } else {
        $Text = preg_replace("(\\[url\\]([{$URLSearchString}]*)\\[\\/url\\])ism", " \$1 ", $Text);
        $Text = preg_replace_callback("&\\[url=([^\\[\\]]*)\\]\\[img\\](.*)\\[\\/img\\]\\[\\/url\\]&Usi", 'bb_RemovePictureLinks', $Text);
    }
    //	}
    if ($tryoembed) {
        $Text = preg_replace_callback("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
    }
    $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" target="_blank">$1</a>', $Text);
    $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // Red compatibility, though the link can't be authenticated on Friendica
    $Text = preg_replace("/\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // we may need to restrict this further if it picks up too many strays
    // link acct:user@host to a webfinger profile redirector
    $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "\$1@\$2" . '" target="extlink" >acct:' . "\$1@\$2\$3" . '</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // leave open the posibility of [map=something]
    // this is replaced in prepare_body() which has knowledge of the item location
    if (strpos($Text, '[/map]') !== false) {
        $Text = preg_replace_callback("/\\[map\\](.*?)\\[\\/map\\]/ism", 'bb_map_location', $Text);
    }
    if (strpos($Text, '[map=') !== false) {
        $Text = preg_replace_callback("/\\[map=(.*?)\\]/ism", 'bb_map_coords', $Text);
    }
    if (strpos($Text, '[map]') !== false) {
        $Text = preg_replace("/\\[map\\]/", '<div class="map"></div>', $Text);
    }
    // Check for headers
    $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px; line-height: initial;\">\$2</span>", $Text);
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1; line-height: initial;\">\$2</span>", $Text);
    // Check for centered text
    $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // Check for style sheet commands
    $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_cleanstyle", $Text);
    // Check for CSS classes
    $Text = preg_replace_callback("(\\[class=(.*?)\\](.*?)\\[\\/class\\])ism", "bb_cleanclass", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    }
    $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    //	$Text = preg_replace_callback("/\[code\](.*?)\[\/code\]/ism",'stripcode_br_cb',$Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // Check for [spoiler] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler\\](.*?)\\[\\/spoiler\\]/ism", "{$SpoilerLayout}", $Text);
    }
    // Check for [spoiler=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/spoiler\\]/ism", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    }
    // Check for [quote=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/quote\\]/ism", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // [img=widthxheight]image source[/img]
    $Text = preg_replace_callback("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", 'bb_PictureCacheExt', $Text);
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
    $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace_callback("/\\[img\\](.*?)\\[\\/img\\]/ism", 'bb_PictureCache', $Text);
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    // Shared content
    $Text = preg_replace_callback("/(.*?)\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", function ($match) use($simplehtml) {
        return bb_ShareAttributes($match, $simplehtml);
    }, $Text);
    $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br/><img src="' . $a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br />', $Text);
    $Text = preg_replace("/\\[crypt(.*?)\\](.*?)\\[\\/crypt\\]/ism", '<br/><img src="' . $a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
    //$Text = preg_replace("/\[crypt=(.*?)\](.*?)\[\/crypt\]/ism",'<br/><img src="' .$a->get_baseurl() . '/images/lock_icon.gif" alt="' . t('Encrypted content') . '" title="' . '$1' . ' ' . t('Encrypted content') . '" /><br />', $Text);
    // Try to Oembed
    if ($tryoembed) {
        $Text = preg_replace("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4))\\[\\/video\\]/ism", '<video src="$1" controls="controls" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></video>', $Text);
        $Text = preg_replace("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
        $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    } else {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1" target="_blank">$1</a>', $Text);
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1" target="_blank">$1</a>', $Text);
    }
    // html5 video and audio
    if ($tryoembed) {
        $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="' . $a->videowidth . '" height="' . $a->videoheight . '"><a href="$1">$1</a></iframe>', $Text);
    } else {
        $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1">$1</a>', $Text);
    }
    // Youtube extensions
    if ($tryoembed) {
        $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    }
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    if ($tryoembed) {
        $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
    } else {
        $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<a href="https://www.youtube.com/watch?v=$1" target="_blank">https://www.youtube.com/watch?v=$1</a>', $Text);
    }
    if ($tryoembed) {
        $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
        $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    }
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    if ($tryoembed) {
        $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="https://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
    } else {
        $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<a href="https://vimeo.com/$1" target="_blank">https://vimeo.com/$1</a>', $Text);
    }
    //	$Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // Summary (e.g. title) is required, earlier revisions only required description (in addition to
    // start which is always required). Allow desc with a missing summary for compatibility.
    if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
        $sub = format_event_html($ev, $simplehtml);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    //replace oneliner <code> with <key>
    $Text = preg_replace_callback("|(?!<br[^>]*>)<code>([^<]*)</code>(?!<br[^>]*>)|ism", 'bb_onelinecode_cb', $Text);
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    $Text = preg_replace('/\\&\\#039\\;/', '\'', $Text);
    $Text = preg_replace('/\\&quot\\;/', '"', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<([^>]*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    $Text = preg_replace("/\\<([^>]*?)(src|href)=\"(?!http|ftp|mailto|gopher|cid)(.*?)\\>/ism", '<$1$2="">', $Text);
    if ($saved_image) {
        $Text = bb_replace_images($Text, $saved_image);
    }
    // Clean up the HTML by loading and saving the HTML with the DOM.
    // Bad structured html can break a whole page.
    // For performance reasons do it only with ativated item cache or at export.
    if (!$tryoembed or get_itemcachepath() != "") {
        $doc = new DOMDocument();
        $doc->preserveWhiteSpace = false;
        $Text = mb_convert_encoding($Text, 'HTML-ENTITIES', "UTF-8");
        $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">';
        $encoding = '<?xml encoding="UTF-8">';
        @$doc->loadHTML($encoding . $doctype . "<html><body>" . $Text . "</body></html>");
        $doc->encoding = 'UTF-8';
        $Text = $doc->saveHTML();
        $Text = str_replace(array("<html><body>", "</body></html>", $doctype, $encoding), array("", "", "", ""), $Text);
        $Text = str_replace('<br></li>', '</li>', $Text);
        //$Text = mb_convert_encoding($Text, "UTF-8", 'HTML-ENTITIES');
    }
    // Clean up some useless linebreaks in lists
    //$Text = str_replace('<br /><ul','<ul ', $Text);
    //$Text = str_replace('</ul><br />','</ul>', $Text);
    //$Text = str_replace('</li><br />','</li>', $Text);
    //$Text = str_replace('<br /><li>','<li>', $Text);
    //	$Text = str_replace('<br /><ul','<ul ', $Text);
    call_hooks('bbcode', $Text);
    return trim($Text);
}
示例#4
0
function bbtovcal($s)
{
    $o = '';
    $ev = bbtoevent($s);
    if ($ev['desc']) {
        $o = format_event_html($ev);
    }
    return $o;
}
示例#5
0
function bbcode($Text, $preserve_nl = false, $tryoembed = true, $cache = false)
{
    // Hide all [noparse] contained bbtags by spacefying them
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // process [observer] tags before we do anything else because we might
    // be stripping away stuff that then doesn't need to be worked on anymore
    if ($cache) {
        $observer = false;
    } else {
        $observer = App::get_observer();
    }
    if (strpos($Text, '[/observer]') !== false || strpos($Text, '[/rpost]') !== false) {
        if ($observer) {
            $Text = preg_replace("/\\[observer\\=1\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[observer\\=0\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace_callback("/\\[rpost(=(.*?))?\\](.*?)\\[\\/rpost\\]/ism", 'rpost_callback', $Text);
        } else {
            $Text = preg_replace("/\\[observer\\=1\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace("/\\[observer\\=0\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[rpost(=.*?)?\\](.*?)\\[\\/rpost\\]/ism", '', $Text);
        }
    }
    if ($cache) {
        $channel = false;
    } else {
        $channel = App::get_channel();
    }
    if (strpos($Text, '[/channel]') !== false) {
        if ($channel) {
            $Text = preg_replace("/\\[channel\\=1\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[channel\\=0\\].*?\\[\\/channel\\]/ism", '', $Text);
        } else {
            $Text = preg_replace("/\\[channel\\=1\\].*?\\[\\/channel\\]/ism", '', $Text);
            $Text = preg_replace("/\\[channel\\=0\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
        }
    }
    $x = bb_extract_images($Text);
    $Text = $x['body'];
    $saved_images = $x['images'];
    $Text = str_replace(array('[baseurl]', '[sitename]'), array(z_root(), get_config('system', 'sitename')), $Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = str_replace("\r\n", "\n", $Text);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    $Text = str_replace(array("\t", "  "), array("&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;"), $Text);
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // replace [observer.baseurl]
    if ($observer) {
        $s1 = '<span class="bb_observer" title="' . t('Different viewers will see this text differently') . '">';
        $s2 = '</span>';
        $obsBaseURL = $observer['xchan_connurl'];
        $obsBaseURL = preg_replace("/\\/poco\\/.*\$/", '', $obsBaseURL);
        $Text = str_replace('[observer.baseurl]', $obsBaseURL, $Text);
        $Text = str_replace('[observer.url]', $observer['xchan_url'], $Text);
        $Text = str_replace('[observer.name]', $s1 . $observer['xchan_name'] . $s2, $Text);
        $Text = str_replace('[observer.address]', $s1 . $observer['xchan_addr'] . $s2, $Text);
        $Text = str_replace('[observer.webname]', substr($observer['xchan_addr'], 0, strpos($observer['xchan_addr'], '@')), $Text);
        $Text = str_replace('[observer.photo]', $s1 . '[zmg]' . $observer['xchan_photo_l'] . '[/zmg]' . $s2, $Text);
    } else {
        $Text = str_replace('[observer.baseurl]', '', $Text);
        $Text = str_replace('[observer.url]', '', $Text);
        $Text = str_replace('[observer.name]', '', $Text);
        $Text = str_replace('[observer.address]', '', $Text);
        $Text = str_replace('[observer.webname]', '', $Text);
        $Text = str_replace('[observer.photo]', '', $Text);
    }
    // Perform URL Search
    $urlchars = '[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\@\\_\\~\\#\\%\\$\\!\\+\\,\\@]';
    if (strpos($Text, 'http') !== false) {
        $Text = preg_replace("/([^\\]\\='" . '"' . "\\/]|^|\\#\\^)(https?\\:\\/\\/{$urlchars}+)/ism", '$1<a href="$2" target="_blank" >$2</a>', $Text);
    }
    if (strpos($Text, '[/share]') !== false) {
        $Text = preg_replace_callback("/\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", 'bb_ShareAttributes', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/url]') !== false) {
            $Text = preg_replace_callback("/[^\\^]\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
        }
    }
    if (strpos($Text, '[/url]') !== false) {
        $Text = preg_replace("/\\#\\^\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" target="_blank" >$2</a>', $Text);
        $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" target="_blank" >$2</a>', $Text);
    }
    if (strpos($Text, '[/zrl]') !== false) {
        $Text = preg_replace("/\\#\\^\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" target="_blank" >$2</a>', $Text);
        $Text = preg_replace("/\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" target="_blank" >$2</a>', $Text);
    }
    // Remove bookmarks from UNO
    if (UNO) {
        $Text = str_replace('<span class="bookmark-identifier">#^</span>', '', $Text);
    }
    // Perform MAIL Search
    if (strpos($Text, '[/mail]') !== false) {
        $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1" target="_blank" >$1</a>', $Text);
        $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1" target="_blank" >$2</a>', $Text);
    }
    // leave open the posibility of [map=something]
    // this is replaced in prepare_body() which has knowledge of the item location
    if (strpos($Text, '[/map]') !== false) {
        $Text = preg_replace_callback("/\\[map\\](.*?)\\[\\/map\\]/ism", 'bb_map_location', $Text);
    }
    if (strpos($Text, '[map=') !== false) {
        $Text = preg_replace_callback("/\\[map=(.*?)\\]/ism", 'bb_map_coords', $Text);
    }
    if (strpos($Text, '[map]') !== false) {
        $Text = preg_replace("/\\[map\\]/", '<div class="map"></div>', $Text);
    }
    // Check for bold text
    if (strpos($Text, '[b]') !== false) {
        $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    }
    // Check for Italics text
    if (strpos($Text, '[i]') !== false) {
        $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    }
    // Check for Underline text
    if (strpos($Text, '[u]') !== false) {
        $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    }
    // Check for strike-through text
    if (strpos($Text, '[s]') !== false) {
        $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    }
    // Check for over-line text
    if (strpos($Text, '[o]') !== false) {
        $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    }
    if (strpos($Text, '[sup]') !== false) {
        $Text = preg_replace("(\\[sup\\](.*?)\\[\\/sup\\])ism", '<sup>$1</sup>', $Text);
    }
    if (strpos($Text, '[sub]') !== false) {
        $Text = preg_replace("(\\[sub\\](.*?)\\[\\/sub\\])ism", '<sub>$1</sub>', $Text);
    }
    // Check for colored text
    if (strpos($Text, '[/color]') !== false) {
        $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    }
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    if (strpos($Text, '[/size]') !== false) {
        $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
        $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    }
    // Check for h1
    if (strpos($Text, '[h1]') !== false) {
        $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    }
    // Check for h2
    if (strpos($Text, '[h2]') !== false) {
        $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    }
    // Check for h3
    if (strpos($Text, '[h3]') !== false) {
        $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    }
    // Check for h4
    if (strpos($Text, '[h4]') !== false) {
        $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    }
    // Check for h5
    if (strpos($Text, '[h5]') !== false) {
        $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    }
    // Check for h6
    if (strpos($Text, '[h6]') !== false) {
        $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    }
    // Check for table of content without params
    if (strpos($Text, '[toc]') !== false) {
        $Text = preg_replace("/\\[toc\\]/ism", '<ul id="toc"></ul>', $Text);
    }
    // Check for table of content with params
    if (strpos($Text, '[toc') !== false) {
        $Text = preg_replace("/\\[toc([^\\]]+?)\\]/ism", '<ul$1></ul>', $Text);
    }
    // Check for centered text
    if (strpos($Text, '[/center]') !== false) {
        $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    }
    // Check for footer
    if (strpos($Text, '[/footer]') !== false) {
        $Text = preg_replace("(\\[footer\\](.*?)\\[\\/footer\\])ism", "<div class=\"wall-item-footer\">\$1</div>", $Text);
    }
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/dl]") !== false && strpos($Text, "[dl") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
        // [dl] tags have an optional [dl terms="bi"] form where bold/italic/underline/mono/large
        // etc. style may be specified for the "terms" in the definition list. The quotation marks
        // are also optional. The regex looks intimidating, but breaks down as:
        //   "[dl" <optional-whitespace> <optional-termStyles> "]" <matchGroup2> "[/dl]"
        // where optional-termStyles are: "terms=" <optional-quote> <matchGroup1> <optional-quote>
        $Text = preg_replace_callback('/\\[dl[[:space:]]*(?:terms=(?:&quot;|")?([a-zA-Z]+)(?:&quot;|")?)?\\](.*?)\\[\\/dl\\]/ism', 'bb_definitionList', $Text);
    }
    if (strpos($Text, '[th]') !== false) {
        $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    }
    if (strpos($Text, '[td]') !== false) {
        $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    }
    if (strpos($Text, '[tr]') !== false) {
        $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    }
    if (strpos($Text, '[/table]') !== false) {
        $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
        $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
        $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    }
    $Text = str_replace('</tr><br /><tr>', "</tr>\n<tr>", $Text);
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    if (strpos($Text, '[/font]') !== false) {
        $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    }
    // Check for [code] text
    if (strpos($Text, '[code]') !== false) {
        $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'bb_code', $Text);
    }
    // Check for [spoiler] text
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[spoiler\\](.*?)\\[\\/spoiler\\]/ism", 'bb_spoilertag', $Text);
    }
    // Check for [spoiler=Author] text
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[spoiler=(.*?)\\](.*?)\\[\\/spoiler\\]/ism", 'bb_spoilertag', $Text);
    }
    // Check for [open] text
    $endlessloop = 0;
    while (strpos($Text, "[/open]") !== false and strpos($Text, "[open]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[open\\](.*?)\\[\\/open\\]/ism", 'bb_opentag', $Text);
    }
    // Check for [open=Title] text
    $endlessloop = 0;
    while (strpos($Text, "[/open]") !== false and strpos($Text, "[open=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace_callback("/\\[open=(.*?)\\](.*?)\\[\\/open\\]/ism", 'bb_opentag', $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    }
    // Check for [quote=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/quote\\]/ism", "<span class=" . '"bb-quote"' . ">" . $t_wrote . "</span><blockquote>\$2</blockquote>", $Text);
    }
    // Images
    // [img]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=left\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=right\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=left\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=right\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // style (sanitized)
    if (strpos($Text, '[/style]') !== false) {
        $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_sanitize_style", $Text);
    }
    // crypt
    if (strpos($Text, '[/crypt]') !== false) {
        $x = random_string();
        $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br /><div id="' . $x . '"><img src="' . z_root() . '/images/lock_icon.gif" onclick="red_decrypt(\'rot13\',\'\',\'$1\',\'#' . $x . '\');" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br /></div>', $Text);
        $Text = preg_replace_callback("/\\[crypt (.*?)\\](.*?)\\[\\/crypt\\]/ism", 'bb_parse_crypt', $Text);
    }
    if (strpos($Text, '[/app]') !== false) {
        $Text = preg_replace_callback("/\\[app\\](.*?)\\[\\/app\\]/ism", 'bb_parse_app', $Text);
    }
    if (strpos($Text, '[/element]') !== false) {
        $Text = preg_replace_callback("/\\[element\\](.*?)\\[\\/element\\]/ism", 'bb_parse_element', $Text);
    }
    // html5 video and audio
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace_callback("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mpeg|mpg))\\[\\/video\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace_callback("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3|opus))\\[\\/audio\\]/ism", 'tryzrlaudio', $Text);
    }
    if (strpos($Text, '[/zvideo]') !== false) {
        $Text = preg_replace_callback("/\\[zvideo\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mpeg|mpg))\\[\\/zvideo\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/zaudio]') !== false) {
        $Text = preg_replace_callback("/\\[zaudio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3|opus))\\[\\/zaudio\\]/ism", 'tryzrlaudio', $Text);
    }
    // Try to Oembed
    if ($tryoembed) {
        if (strpos($Text, '[/video]') !== false) {
            $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/audio]') !== false) {
            $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/zvideo]') !== false) {
            $Text = preg_replace_callback("/\\[zvideo\\](.*?)\\[\\/zvideo\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/zaudio]') !== false) {
            $Text = preg_replace_callback("/\\[zaudio\\](.*?)\\[\\/zaudio\\]/ism", 'tryoembed', $Text);
        }
    }
    // if video couldn't be embedded, link to it instead.
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/zvideo]') !== false) {
        $Text = preg_replace("/\\[zvideo\\](.*?)\\[\\/zvideo\\]/", '<a class="zid" href="$1" target="_blank" >$1</a>', $Text);
    }
    if (strpos($Text, '[/zaudio]') !== false) {
        $Text = preg_replace("/\\[zaudio\\](.*?)\\[\\/zaudio\\]/", '<a class="zid" href="$1" target="_blank" >$1</a>', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace_callback("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", 'bb_iframe', $Text);
        }
    } else {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1" target="_blank" >$1</a>', $Text);
        }
    }
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // Summary (e.g. title) is required, earlier revisions only required description (in addition to
    // start which is always required). Allow desc with a missing summary for compatibility.
    if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $sub = str_replace('$', "", $sub);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-id\\](.*?)\\[\\/event\\-id\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
        $Text = str_replace("", '$', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    if (strpos($Text, '&amp;') !== false) {
        $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    }
    // This is subtle - it's an XSS filter. It only accepts links with a protocol scheme and where
    // the scheme begins with z (zhttp), h (http(s)), f (ftp), m (mailto), and named anchors.
    $Text = preg_replace("/\\<(.*?)(src|href)=\"[^zhfm#](.*?)\\>/ism", '<$1$2="">', $Text);
    $Text = bb_replace_images($Text, $saved_images);
    call_hooks('bbcode', $Text);
    return $Text;
}
示例#6
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
}
示例#7
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
}
示例#8
0
function bbcode($Text, $preserve_nl = false)
{
    // Extract a single private image which uses data url's since preg has issues with
    // large data sizes. Stash it away while we do bbcode conversion, and then put it back
    // in after we've done all the regex matching. We cannot use any preg functions to do this.
    $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]'));
        //		logger('saved_image: ' . $saved_image,LOGGER_DEBUG);
        $Text = $start_fragment . '[$#saved_image#$]' . $end_fragment;
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    $Text = nl2br($Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Perform URL Search
    $Text = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
    $Text = preg_replace_callback("/\\[bookmark\\=([^\\]]*)\\].*?\\[\\/bookmark\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url=$1]$2[/url]', $Text);
    $Text = preg_replace_callback("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
    $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
    //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    // Check for list text
    if (stristr($Text, '[/list]')) {
        $Text = str_replace("[*]", "<li>", $Text);
    }
    if (stristr($Text, '[/list]')) {
        $Text = str_replace("[*]", "<li>", $Text);
    }
    $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=i\\](.*?)\\[\\/list\\]/sm", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=I\\](.*?)\\[\\/list\\]/sm", '<ul class="listupperroman" style="list-style-type: upper-roman;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=a\\](.*?)\\[\\/list\\]/sm", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$1</ul>', $Text);
    $Text = preg_replace("/\\[list=A\\](.*?)\\[\\/list\\]/sm", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$1</ul>', $Text);
    $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/sm", '<li>$1</li>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    // Check for font change text
    $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'stripcode_br_cb', $Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    // [img=widthxheight]image source[/img]
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    // Try to Oembed
    $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    // html5 video and audio
    $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/ism", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
    $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
    $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
    /*if (get_pconfig(local_user(), 'oembed', 'use_for_youtube' )==1){
    		// use oembed for youtube links
    		$Text = preg_replace("/\[youtube\]/",'[embed]',$Text); 
    		$Text = preg_replace("/\[\/youtube\]/",'[/embed]',$Text); 
    	} else {*/
    // Youtube extensions
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="425" height="350" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
    //}
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="425" height="350" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
    //	$Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    if (x($ev, 'desc') && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    if (strlen($saved_image)) {
        $Text = str_replace('[$#saved_image#$]', '<img src="' . $saved_image . '" alt="' . t('Image/photo') . '" />', $Text);
    }
    call_hooks('bbcode', $Text);
    return $Text;
}
示例#9
0
function bb2diaspora($Text, $preserve_nl = false)
{
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Perform URL Search
    // [img]pathtoimage[/img]
    $Text = preg_replace("/\\[bookmark\\]([{$URLSearchString}]*)\\[\\/bookmark\\]/ism", '[$1]($1)', $Text);
    $Text = preg_replace("/\\[bookmark\\=([{$URLSearchString}]*)\\](.*?)\\[\\/bookmark\\]/ism", '[$2]($1)', $Text);
    $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '[$1]($1)', $Text);
    $Text = preg_replace("/\\#\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '[#$2]($1)', $Text);
    $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '[$2]($1)', $Text);
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/", '![' . t('image/photo') . '](' . '$1' . ')', $Text);
    // Perform MAIL Search
    $Text = preg_replace("(\\[mail\\]([{$MAILSearchString}]*)\\[/mail\\])", '[$1](mailto:$1)', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '[$2](mailto:$1)', $Text);
    $Text = str_replace('*', '\\*', $Text);
    $Text = str_replace('_', '\\_', $Text);
    $Text = str_replace('`', '\\`', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])is", '**$1**', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])is", '_$1_', $Text);
    // Check for Underline text
    //	$Text = preg_replace("(\[u\](.*?)\[\/u\])is",'<u>$1</u>',$Text);
    // Check for strike-through text
    //	$Text = preg_replace("(\[s\](.*?)\[\/s\])is",'<strike>$1</strike>',$Text);
    // Check for over-line text
    //	$Text = preg_replace("(\[o\](.*?)\[\/o\])is",'<span class="overline">$1</span>',$Text);
    // Check for colored text
    //	$Text = preg_replace("(\[color=(.*?)\](.*?)\[\/color\])is","<span style=\"color: $1;\">$2</span>",$Text);
    // Check for sized text
    //	$Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","<span style=\"font-size: $1;\">$2</span>",$Text);
    // Check for list text
    //	$Text = preg_replace("/\[list\](.*?)\[\/list\]/is", '<ul class="listbullet">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[list=1\](.*?)\[\/list\]/is", '<ul class="listdecimal">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[list=i\](.*?)\[\/list\]/s",'<ul class="listlowerroman">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[list=I\](.*?)\[\/list\]/s", '<ul class="listupperroman">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[list=a\](.*?)\[\/list\]/s", '<ul class="listloweralpha">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[list=A\](.*?)\[\/list\]/s", '<ul class="listupperalpha">$1</ul>' ,$Text);
    //	$Text = preg_replace("/\[li\](.*?)\[\/li\]/s", '<li>$1</li>' ,$Text);
    //	$Text = preg_replace("/\[td\](.*?)\[\/td\]/s", '<td>$1</td>' ,$Text);
    //	$Text = preg_replace("/\[tr\](.*?)\[\/tr\]/s", '<tr>$1</tr>' ,$Text);
    //	$Text = preg_replace("/\[table\](.*?)\[\/table\]/s", '<table>$1</table>' ,$Text);
    //	$Text = preg_replace("/\[table border=1\](.*?)\[\/table\]/s", '<table border="1" >$1</table>' ,$Text);
    //	$Text = preg_replace("/\[table border=0\](.*?)\[\/table\]/s", '<table border="0" >$1</table>' ,$Text);
    //	$Text = str_replace("[*]", "<li>", $Text);
    // Check for font change text
    //	$Text = preg_replace("(\[font=(.*?)\](.*?)\[\/font\])","<span style=\"font-family: $1;\">$2</span>",$Text);
    $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/is", 'stripdcode_br_cb', $Text);
    // Check for [code] text
    $Text = preg_replace("/(\\[code\\])+(.*?)(\\[\\/code\\])+/is", "\t\$2\n", $Text);
    // Declare the format for [quote] layout
    //	$QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/is", ">\$1\n\n", $Text);
    // Images
    // html5 video and audio
    //	$Text = preg_replace("/\[video\](.*?)\[\/video\]/", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
    //	$Text = preg_replace("/\[audio\](.*?)\[\/audio\]/", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
    //	$Text = preg_replace("/\[iframe\](.*?)\[\/iframe\]/", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
    // [img=widthxheight]image source[/img]
    //	$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/", '<img src="$3" style="height:{$2}px; width:{$1}px;" >', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", 'http://www.youtube.com/watch?v=$1', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", 'http://www.youtube.com/watch?v=$1', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", 'http://www.youtube.com/watch?v=$1', $Text);
    $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", 'http://www.youtube.com/watch?v=$1', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", 'http://vimeo.com/$1', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", 'http://vimeo.com/$1', $Text);
    $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", 'http://vimeo.com/$1', $Text);
    // oembed tag
    //	$Text = oembed_bbcode2html($Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    if (x($ev, 'desc') && x($ev, 'start')) {
        $sub = format_event_diaspora($ev);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/is", $sub, $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/is", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/is", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/is", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/is", '', $Text);
    }
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    $Text = preg_replace('/\\[(.*?)\\]\\((.*?)\\\\_(.*?)\\)/ism', '[$1]($2_$3)', $Text);
    call_hooks('bb2diaspora', $Text);
    return $Text;
}
示例#10
0
文件: bbcode.php 项目: Mauru/red
function bbcode($Text, $preserve_nl = false, $tryoembed = true)
{
    $a = get_app();
    // Move all spaces out of the tags
    // ....Uhm why?
    // This is basically doing a trim() on the stuff in between tags, but it messes up
    // carefully crafted bbcode and especially other pre-formatted code.
    // Commenting out until we come up with a use case where it's needed. Then let's try and
    // special case rather than a heavy-handed approach like this.
    //	$Text = preg_replace("/\[(\w*)\](\s*)/ism", '$2[$1]', $Text);
    //	$Text = preg_replace("/(\s*)\[\/(\w*)\]/ism", '[/$2]$1', $Text);
    // Hide all [noparse] contained bbtags by spacefying them
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    }
    //  Not yet implemented - thinking this should display a map or perhaps be a map directive
    //	if (strpos($Text,'[location]') !== false) {
    //		$Text = preg_replace_callback("/\[location\](.*?)\[\/location\]/ism", 'bb_location',$Text);
    //	}
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // process [observer] tags before we do anything else because we might
    // be stripping away stuff that then doesn't need to be worked on anymore
    $observer = $a->get_observer();
    if (strpos($Text, '[/observer]') !== false || strpos($Text, '[/rpost]') !== false) {
        if ($observer) {
            $Text = preg_replace("/\\[observer\\=1\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[observer\\=0\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace_callback("/\\[rpost(=(.*?))?\\](.*?)\\[\\/rpost\\]/ism", 'rpost_callback', $Text);
        } else {
            $Text = preg_replace("/\\[observer\\=1\\].*?\\[\\/observer\\]/ism", '', $Text);
            $Text = preg_replace("/\\[observer\\=0\\](.*?)\\[\\/observer\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[rpost(=.*?)?\\](.*?)\\[\\/rpost\\]/ism", '', $Text);
        }
    }
    $channel = $a->get_channel();
    if (strpos($Text, '[/channel]') !== false) {
        if ($channel) {
            $Text = preg_replace("/\\[channel\\=1\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
            $Text = preg_replace("/\\[channel\\=0\\].*?\\[\\/channel\\]/ism", '', $Text);
        } else {
            $Text = preg_replace("/\\[channel\\=1\\].*?\\[\\/channel\\]/ism", '', $Text);
            $Text = preg_replace("/\\[channel\\=0\\](.*?)\\[\\/channel\\]/ism", '$1', $Text);
        }
    }
    $Text = str_replace(array('[baseurl]', '[sitename]'), array(z_root(), get_config('system', 'sitename')), $Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    // nlbr seems to be hopelessly messed up
    //	$Text = nl2br($Text);
    // We'll emulate it.
    $Text = str_replace("\r\n", "\n", $Text);
    $Text = str_replace(array("\r", "\n"), array('<br />', '<br />'), $Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    $Text = str_replace(array("\t", "  "), array("&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;&nbsp;"), $Text);
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // replace [observer.baseurl]
    if ($observer) {
        $obsBaseURL = $observer['xchan_connurl'];
        $obsBaseURL = preg_replace("/\\/poco\\/.*\$/", '', $obsBaseURL);
        $Text = str_replace('[observer.baseurl]', $obsBaseURL, $Text);
        $Text = str_replace('[observer.url]', $observer['xchan_url'], $Text);
        $Text = str_replace('[observer.name]', $observer['xchan_name'], $Text);
        $Text = str_replace('[observer.address]', $observer['xchan_addr'], $Text);
        $Text = str_replace('[observer.webname]', substr($observer['xchan_addr'], 0, strpos($observer['xchan_addr'], '@')), $Text);
        $Text = str_replace('[observer.photo]', '[zmg]' . $observer['xchan_photo_l'] . '[/zmg]', $Text);
    } else {
        $Text = str_replace('[observer.baseurl]', '', $Text);
        $Text = str_replace('[observer.url]', '', $Text);
        $Text = str_replace('[observer.name]', '', $Text);
        $Text = str_replace('[observer.address]', '', $Text);
        $Text = str_replace('[observer.webname]', '', $Text);
        $Text = str_replace('[observer.photo]', '', $Text);
    }
    // Perform URL Search
    $urlchars = '[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\@\\_\\~\\#\\%\\$\\!\\+\\,]';
    if (strpos($Text, 'http') !== false) {
        $Text = preg_replace("/([^\\]\\='" . '"' . "]|^)(https?\\:\\/\\/{$urlchars}+)/ism", '$1<a href="$2" >$2</a>', $Text);
    }
    if (strpos($Text, '[/qr]') !== false) {
        $Text = preg_replace_callback("/\\[qr\\](.*?)\\[\\/qr\\]/ism", "bb_qr", $Text);
    }
    if (strpos($Text, '[/share]') !== false) {
        $Text = preg_replace_callback("/\\[share(.*?)\\](.*?)\\[\\/share\\]/ism", "bb_ShareAttributes", $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/url]') !== false) {
            $Text = preg_replace_callback("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
        }
    }
    if (strpos($Text, '[/url]') !== false) {
        $Text = preg_replace("/\\#\\^\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<span class="bookmark-identifier">#^</span><a class="bookmark" href="$1" >$2</a>', $Text);
        $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" >$2</a>', $Text);
    }
    if (strpos($Text, '[/zrl]') !== false) {
        $Text = preg_replace("/\\#\\^\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\#\\^\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<span class="bookmark-identifier">#^</span><a class="zrl bookmark" href="$1" >$2</a>', $Text);
        $Text = preg_replace("/\\[zrl\\]([{$URLSearchString}]*)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" >$1</a>', $Text);
        $Text = preg_replace("/\\[zrl\\=([{$URLSearchString}]*)\\](.*?)\\[\\/zrl\\]/ism", '<a class="zrl" href="$1" >$2</a>', $Text);
    }
    // Perform MAIL Search
    if (strpos($Text, '[/mail]') !== false) {
        $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
        $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    }
    // Check for bold text
    if (strpos($Text, '[b]') !== false) {
        $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    }
    // Check for Italics text
    if (strpos($Text, '[i]') !== false) {
        $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    }
    // Check for Underline text
    if (strpos($Text, '[u]') !== false) {
        $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    }
    // Check for strike-through text
    if (strpos($Text, '[s]') !== false) {
        $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    }
    // Check for over-line text
    if (strpos($Text, '[o]') !== false) {
        $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    }
    if (strpos($Text, '[sup]') !== false) {
        $Text = preg_replace("(\\[sup\\](.*?)\\[\\/sup\\])ism", '<sup>$1</sup>', $Text);
    }
    if (strpos($Text, '[sub]') !== false) {
        $Text = preg_replace("(\\[sub\\](.*?)\\[\\/sub\\])ism", '<sub>$1</sub>', $Text);
    }
    // Check for colored text
    if (strpos($Text, '[/color]') !== false) {
        $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    }
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    if (strpos($Text, '[/size]') !== false) {
        $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
        $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    }
    // Check for h1
    if (strpos($Text, '[h1]') !== false) {
        $Text = preg_replace("(\\[h1\\](.*?)\\[\\/h1\\])ism", '<h1>$1</h1>', $Text);
    }
    // Check for h2
    if (strpos($Text, '[h2]') !== false) {
        $Text = preg_replace("(\\[h2\\](.*?)\\[\\/h2\\])ism", '<h2>$1</h2>', $Text);
    }
    // Check for h3
    if (strpos($Text, '[h3]') !== false) {
        $Text = preg_replace("(\\[h3\\](.*?)\\[\\/h3\\])ism", '<h3>$1</h3>', $Text);
    }
    // Check for h4
    if (strpos($Text, '[h4]') !== false) {
        $Text = preg_replace("(\\[h4\\](.*?)\\[\\/h4\\])ism", '<h4>$1</h4>', $Text);
    }
    // Check for h5
    if (strpos($Text, '[h5]') !== false) {
        $Text = preg_replace("(\\[h5\\](.*?)\\[\\/h5\\])ism", '<h5>$1</h5>', $Text);
    }
    // Check for h6
    if (strpos($Text, '[h6]') !== false) {
        $Text = preg_replace("(\\[h6\\](.*?)\\[\\/h6\\])ism", '<h6>$1</h6>', $Text);
    }
    // Check for centered text
    if (strpos($Text, '[/center]') !== false) {
        $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    }
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    // handle nested lists
    $endlessloop = 0;
    while ((strpos($Text, "[/list]") !== false && strpos($Text, "[list") !== false || strpos($Text, "[/ol]") !== false && strpos($Text, "[ol]") !== false || strpos($Text, "[/ul]") !== false && strpos($Text, "[ul]") !== false || strpos($Text, "[/li]") !== false && strpos($Text, "[li]") !== false) && ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    }
    if (strpos($Text, '[th]') !== false) {
        $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    }
    if (strpos($Text, '[td]') !== false) {
        $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    }
    if (strpos($Text, '[tr]') !== false) {
        $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    }
    if (strpos($Text, '[/table]') !== false) {
        $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
        $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
        $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    }
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    if (strpos($Text, '[/font]') !== false) {
        $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    }
    // Declare the format for [code] layout
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    if (strpos($Text, '[code]') !== false) {
        $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    }
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // Check for [spoiler] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler\\](.*?)\\[\\/spoiler\\]/ism", "{$SpoilerLayout}", $Text);
    }
    // Check for [spoiler=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/spoiler\\]/ism", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    }
    // Check for [quote=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/quote\\]/ism", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // Images
    // [img]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" style="max-width=100%;" src="$1" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=left\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img float=right\\](.*?)\\[\\/img\\]/ism", '<img style="max-width=100%;" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=left\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg float=right\\](.*?)\\[\\/zmg\\]/ism", '<img style="max-width=100%;" class="zrl" src="$1" style="float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // [img=widthxheight float={left, right}]pathtoimage[/img]
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/img]') !== false) {
        $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=left\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: left;" alt="' . t('Image/photo') . '" />', $Text);
    }
    if (strpos($Text, '[/zmg]') !== false) {
        $Text = preg_replace("/\\[zmg\\=([0-9]*)x([0-9]*) float=right\\](.*?)\\[\\/zmg\\]/ism", '<img class="zrl" src="$3" style="width: 100%; max-width: $1px; float: right;" alt="' . t('Image/photo') . '" />', $Text);
    }
    // style (sanitized)
    if (strpos($Text, '[/style]') !== false) {
        $Text = preg_replace_callback("(\\[style=(.*?)\\](.*?)\\[\\/style\\])ism", "bb_sanitize_style", $Text);
    }
    // crypt
    if (strpos($Text, '[/crypt]') !== false) {
        $x = random_string();
        $Text = preg_replace("/\\[crypt\\](.*?)\\[\\/crypt\\]/ism", '<br/><div id="' . $x . '"><img src="' . $a->get_baseurl() . '/images/lock_icon.gif" onclick="red_decrypt(\'rot13\',\'\',\'$1\',\'#' . $x . '\');" alt="' . t('Encrypted content') . '" title="' . t('Encrypted content') . '" /><br /></div>', $Text);
        $Text = preg_replace_callback("/\\[crypt (.*?)\\](.*?)\\[\\/crypt\\]/ism", 'bb_parse_crypt', $Text);
    }
    if (strpos($Text, '[/app]') !== false) {
        $Text = preg_replace_callback("/\\[app\\](.*?)\\[\\/app\\]/ism", 'bb_parse_app', $Text);
    }
    // html5 video and audio
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace_callback("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4))\\[\\/video\\]/ism", 'tryzrlvideo', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace_callback("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", 'tryzrlaudio', $Text);
    }
    // Try to Oembed
    if ($tryoembed) {
        if (strpos($Text, '[/video]') !== false) {
            $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
        }
        if (strpos($Text, '[/audio]') !== false) {
            $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
        }
    }
    // if video couldn't be embedded, link to it instead.
    if (strpos($Text, '[/video]') !== false) {
        $Text = preg_replace("/\\[video\\](.*?)\\[\\/video\\]/", '<a href="$1">$1</a>', $Text);
    }
    if (strpos($Text, '[/audio]') !== false) {
        $Text = preg_replace("/\\[audio\\](.*?)\\[\\/audio\\]/", '<a href="$1">$1</a>', $Text);
    }
    if ($tryoembed) {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace_callback("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", 'bbiframe', $Text);
        }
    } else {
        if (strpos($Text, '[/iframe]') !== false) {
            $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<a href="$1">$1</a>', $Text);
        }
    }
    // Youtube extensions
    if (strpos($Text, '[youtube]') !== false) {
        if ($tryoembed) {
            $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
        }
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
        if ($tryoembed) {
            $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
        } else {
            $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", "http://www.youtube.com/watch?v=\$1", $Text);
        }
    }
    if (strpos($Text, '[vimeo]') !== false) {
        if ($tryoembed) {
            $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
            $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
        }
        $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
        $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
        if ($tryoembed) {
            $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="' . $a->videowidth . '" height="' . $a->videoheight . '" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
        } else {
            $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", "http://vimeo.com/\$1", $Text);
        }
    }
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // Avoid triple linefeeds through oembed
    $Text = str_replace("<br style='clear:left'></span><br /><br />", "<br style='clear:left'></span><br />", $Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    // Replace the event-start section with the entire formatted event. The other bbcode is stripped.
    // Summary (e.g. title) is required, earlier revisions only required description (in addition to
    // start which is always required). Allow desc with a missing summary for compatibility.
    if ((x($ev, 'desc') || x($ev, 'summary')) && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-summary\\](.*?)\\[\\/event\\-summary\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    if (strpos($Text, '[noparse]') !== false) {
        $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[nobb]') !== false) {
        $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    if (strpos($Text, '[pre]') !== false) {
        $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    }
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    $Text = preg_replace("/\\<(.*?)(src|href)=\"[^hfm](.*?)\\>/ism", '<$1$2="">', $Text);
    call_hooks('bbcode', $Text);
    return $Text;
}
示例#11
0
function bbcode($Text, $preserve_nl = false)
{
    $a = get_app();
    // Hide all [noparse] contained bbtags spacefying them
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_spacefy', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_spacefy', $Text);
    // Extract a single private image which uses data url's since preg has issues with
    // large data sizes. Stash it away while we do bbcode conversion, and then put it back
    // in after we've done all the regex matching. We cannot use any preg functions to do this.
    $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]'));
        //		logger('saved_image: ' . $saved_image,LOGGER_DEBUG);
        $Text = $start_fragment . '[$#saved_image#$]' . $end_fragment;
    }
    // If we find any event code, turn it into an event.
    // After we're finished processing the bbcode we'll
    // replace all of the event code with a reformatted version.
    $ev = bbtoevent($Text);
    // Replace any html brackets with HTML Entities to prevent executing HTML or script
    // Don't use strip_tags here because it breaks [url] search by replacing & with amp
    $Text = str_replace("<", "&lt;", $Text);
    $Text = str_replace(">", "&gt;", $Text);
    // Convert new line chars to html <br /> tags
    $Text = nl2br($Text);
    if ($preserve_nl) {
        $Text = str_replace(array("\n", "\r"), array('', ''), $Text);
    }
    // Set up the parameters for a URL search string
    $URLSearchString = "^\\[\\]";
    // Set up the parameters for a MAIL search string
    $MAILSearchString = $URLSearchString;
    // Perform URL Search
    $Text = preg_replace("/([^\\]\\=]|^)(https?\\:\\/\\/[a-zA-Z0-9\\:\\/\\-\\?\\&\\;\\.\\=\\_\\~\\#\\%\$\\!\\+\\,]+)/ism", '$1<a href="$2" target="external-link">$2</a>', $Text);
    $Text = preg_replace_callback("/\\[bookmark\\=([^\\]]*)\\].*?\\[\\/bookmark\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", '[url=$1]$2[/url]', $Text);
    $Text = preg_replace_callback("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[url\\]([{$URLSearchString}]*)\\[\\/url\\]/ism", '<a href="$1" target="external-link">$1</a>', $Text);
    $Text = preg_replace("/\\[url\\=([{$URLSearchString}]*)\\](.*?)\\[\\/url\\]/ism", '<a href="$1" target="external-link">$2</a>', $Text);
    //$Text = preg_replace("/\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[\/url\]/ism", '<a href="$1" target="_blank">$2</a>', $Text);
    // we may need to restrict this further if it picks up too many strays
    // link acct:user@host to a webfinger profile redirector
    $Text = preg_replace('/acct:(.*?)@(.*?)([ ,])/', '<a href="' . $a->get_baseurl() . '/acctlink?addr=' . "\$1@\$2" . '" target="extlink" >acct:' . "\$1@\$2\$3" . '</a>', $Text);
    // Perform MAIL Search
    $Text = preg_replace("/\\[mail\\]([{$MAILSearchString}]*)\\[\\/mail\\]/", '<a href="mailto:$1">$1</a>', $Text);
    $Text = preg_replace("/\\[mail\\=([{$MAILSearchString}]*)\\](.*?)\\[\\/mail\\]/", '<a href="mailto:$1">$2</a>', $Text);
    // Check for bold text
    $Text = preg_replace("(\\[b\\](.*?)\\[\\/b\\])ism", '<strong>$1</strong>', $Text);
    // Check for Italics text
    $Text = preg_replace("(\\[i\\](.*?)\\[\\/i\\])ism", '<em>$1</em>', $Text);
    // Check for Underline text
    $Text = preg_replace("(\\[u\\](.*?)\\[\\/u\\])ism", '<u>$1</u>', $Text);
    // Check for strike-through text
    $Text = preg_replace("(\\[s\\](.*?)\\[\\/s\\])ism", '<strike>$1</strike>', $Text);
    // Check for over-line text
    $Text = preg_replace("(\\[o\\](.*?)\\[\\/o\\])ism", '<span class="overline">$1</span>', $Text);
    // Check for colored text
    $Text = preg_replace("(\\[color=(.*?)\\](.*?)\\[\\/color\\])ism", "<span style=\"color: \$1;\">\$2</span>", $Text);
    // Check for sized text
    // [size=50] --> font-size: 50px (with the unit).
    $Text = preg_replace("(\\[size=(\\d*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1px;\">\$2</span>", $Text);
    $Text = preg_replace("(\\[size=(.*?)\\](.*?)\\[\\/size\\])ism", "<span style=\"font-size: \$1;\">\$2</span>", $Text);
    // Check for centered text
    $Text = preg_replace("(\\[center\\](.*?)\\[\\/center\\])ism", "<div style=\"text-align:center;\">\$1</div>", $Text);
    // Check for list text
    $Text = str_replace("[*]", "<li>", $Text);
    $Text = preg_replace("/\\[li\\](.*?)\\[\\/li\\]/ism", '<li>$1</li>', $Text);
    // handle nested lists
    $endlessloop = 0;
    while (strpos($Text, "[/list]") !== false and strpos($Text, "[list") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[list\\](.*?)\\[\\/list\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=\\](.*?)\\[\\/list\\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=1\\](.*?)\\[\\/list\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)i)\\](.*?)\\[\\/list\\]/ism", '<ul class="listlowerroman" style="list-style-type: lower-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)I)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperroman" style="list-style-type: upper-roman;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)a)\\](.*?)\\[\\/list\\]/ism", '<ul class="listloweralpha" style="list-style-type: lower-alpha;">$2</ul>', $Text);
        $Text = preg_replace("/\\[list=((?-i)A)\\](.*?)\\[\\/list\\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>', $Text);
    }
    $Text = preg_replace("/\\[ul\\](.*?)\\[\\/ul\\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>', $Text);
    $Text = preg_replace("/\\[ol\\](.*?)\\[\\/ol\\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>', $Text);
    $Text = preg_replace("/\\[th\\](.*?)\\[\\/th\\]/sm", '<th>$1</th>', $Text);
    $Text = preg_replace("/\\[td\\](.*?)\\[\\/td\\]/sm", '<td>$1</td>', $Text);
    $Text = preg_replace("/\\[tr\\](.*?)\\[\\/tr\\]/sm", '<tr>$1</tr>', $Text);
    $Text = preg_replace("/\\[table\\](.*?)\\[\\/table\\]/sm", '<table>$1</table>', $Text);
    $Text = preg_replace("/\\[table border=1\\](.*?)\\[\\/table\\]/sm", '<table border="1" >$1</table>', $Text);
    $Text = preg_replace("/\\[table border=0\\](.*?)\\[\\/table\\]/sm", '<table border="0" >$1</table>', $Text);
    $Text = str_replace('[hr]', '<hr />', $Text);
    // This is actually executed in prepare_body()
    $Text = str_replace('[nosmile]', '', $Text);
    // Check for font change text
    $Text = preg_replace("/\\[font=(.*?)\\](.*?)\\[\\/font\\]/sm", "<span style=\"font-family: \$1;\">\$2</span>", $Text);
    // Declare the format for [code] layout
    $Text = preg_replace_callback("/\\[code\\](.*?)\\[\\/code\\]/ism", 'stripcode_br_cb', $Text);
    $CodeLayout = '<code>$1</code>';
    // Check for [code] text
    $Text = preg_replace("/\\[code\\](.*?)\\[\\/code\\]/ism", "{$CodeLayout}", $Text);
    // Declare the format for [spoiler] layout
    $SpoilerLayout = '<blockquote class="spoiler">$1</blockquote>';
    // Check for [spoiler] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler\\](.*?)\\[\\/spoiler\\]/ism", "{$SpoilerLayout}", $Text);
    }
    // Check for [spoiler=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/spoiler]") !== false and strpos($Text, "[spoiler=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[spoiler=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/spoiler\\]/ism", "<br /><strong class=" . '"spoiler"' . ">" . $t_wrote . "</strong><blockquote class=" . '"spoiler"' . ">\$2</blockquote>", $Text);
    }
    // Declare the format for [quote] layout
    $QuoteLayout = '<blockquote>$1</blockquote>';
    // Check for [quote] text
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote]") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote\\](.*?)\\[\\/quote\\]/ism", "{$QuoteLayout}", $Text);
    }
    // Check for [quote=Author] text
    $t_wrote = t('$1 wrote:');
    // handle nested quotes
    $endlessloop = 0;
    while (strpos($Text, "[/quote]") !== false and strpos($Text, "[quote=") !== false and ++$endlessloop < 20) {
        $Text = preg_replace("/\\[quote=[\"\\']*(.*?)[\"\\']*\\](.*?)\\[\\/quote\\]/ism", "<br /><strong class=" . '"author"' . ">" . $t_wrote . "</strong><blockquote>\$2</blockquote>", $Text);
    }
    // [img=widthxheight]image source[/img]
    //$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '<img src="$3" style="height: $2px; width: $1px;" >', $Text);
    $Text = preg_replace("/\\[img\\=([0-9]*)x([0-9]*)\\](.*?)\\[\\/img\\]/ism", '<img src="$3" style="width: $1px;" >', $Text);
    // Images
    // [img]pathtoimage[/img]
    $Text = preg_replace("/\\[img\\](.*?)\\[\\/img\\]/ism", '<img src="$1" alt="' . t('Image/photo') . '" />', $Text);
    $Text = preg_replace("/\\[video\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4))\\[\\/video\\]/ism", '<video src="$1" controls="controls" width="425" height="350"><a href="$1">$1</a></video>', $Text);
    $Text = preg_replace("/\\[audio\\](.*?\\.(ogg|ogv|oga|ogm|webm|mp4|mp3))\\[\\/audio\\]/ism", '<audio src="$1" controls="controls"><a href="$1">$1</a></audio>', $Text);
    // Try to Oembed
    $Text = preg_replace_callback("/\\[video\\](.*?)\\[\\/video\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[audio\\](.*?)\\[\\/audio\\]/ism", 'tryoembed', $Text);
    // html5 video and audio
    $Text = preg_replace("/\\[iframe\\](.*?)\\[\\/iframe\\]/ism", '<iframe src="$1" width="425" height="350"><a href="$1">$1</a></iframe>', $Text);
    // Youtube extensions
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](www.youtube.com\\/watch\\?v\\=.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[youtube\\](https?:\\/\\/youtu.be\\/.*?)\\[\\/youtube\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/watch\\?v\\=(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/www.youtube.com\\/embed\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]https?:\\/\\/youtu.be\\/(.*?)\\[\\/youtube\\]/ism", '[youtube]$1[/youtube]', $Text);
    $Text = preg_replace("/\\[youtube\\]([A-Za-z0-9\\-_=]+)(.*?)\\[\\/youtube\\]/ism", '<iframe width="425" height="350" src="http://www.youtube.com/embed/$1" frameborder="0" ></iframe>', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/player.vimeo.com\\/video\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace_callback("/\\[vimeo\\](https?:\\/\\/vimeo.com\\/[0-9]+).*?\\[\\/vimeo\\]/ism", 'tryoembed', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/player.vimeo.com\\/video\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]https?:\\/\\/vimeo.com\\/([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '[vimeo]$1[/vimeo]', $Text);
    $Text = preg_replace("/\\[vimeo\\]([0-9]+)(.*?)\\[\\/vimeo\\]/ism", '<iframe width="425" height="350" src="http://player.vimeo.com/video/$1" frameborder="0" ></iframe>', $Text);
    //	$Text = preg_replace("/\[youtube\](.*?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
    // oembed tag
    $Text = oembed_bbcode2html($Text);
    // If we found an event earlier, strip out all the event code and replace with a reformatted version.
    if (x($ev, 'desc') && x($ev, 'start')) {
        $sub = format_event_html($ev);
        $Text = preg_replace("/\\[event\\-description\\](.*?)\\[\\/event\\-description\\]/ism", $sub, $Text);
        $Text = preg_replace("/\\[event\\-start\\](.*?)\\[\\/event\\-start\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-finish\\](.*?)\\[\\/event\\-finish\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-location\\](.*?)\\[\\/event\\-location\\]/ism", '', $Text);
        $Text = preg_replace("/\\[event\\-adjust\\](.*?)\\[\\/event\\-adjust\\]/ism", '', $Text);
    }
    // Unhide all [noparse] contained bbtags unspacefying them
    // and triming the [noparse] tag.
    $Text = preg_replace_callback("/\\[noparse\\](.*?)\\[\\/noparse\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[nobb\\](.*?)\\[\\/nobb\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace_callback("/\\[pre\\](.*?)\\[\\/pre\\]/ism", 'bb_unspacefy_and_trim', $Text);
    $Text = preg_replace('/\\[\\&amp\\;([#a-z0-9]+)\\;\\]/', '&$1;', $Text);
    // fix any escaped ampersands that may have been converted into links
    $Text = preg_replace("/\\<(.*?)(src|href)=(.*?)\\&amp\\;(.*?)\\>/ism", '<$1$2=$3&$4>', $Text);
    if (strlen($saved_image)) {
        $Text = str_replace('[$#saved_image#$]', '<img src="' . $saved_image . '" alt="' . t('Image/photo') . '" />', $Text);
    }
    call_hooks('bbcode', $Text);
    return $Text;
}
示例#12
0
文件: event.php 项目: Mauru/red
function event_addtocal($item_id, $uid)
{
    $c = q("select * from channel where channel_id = %d limit 1", intval($uid));
    if (!$c) {
        return false;
    }
    $channel = $c[0];
    $r = q("select * from item where id = %d and uid = %d limit 1", intval($item_id), intval($channel['channel_id']));
    if (!$r || $r[0]['obj_type'] !== ACTIVITY_OBJ_EVENT) {
        return false;
    }
    $item = $r[0];
    $ev = bbtoevent($r[0]['body']);
    if (x($ev, 'summary') && x($ev, 'start')) {
        $ev['event_xchan'] = $item['author_xchan'];
        $ev['uid'] = $channel['channel_id'];
        $ev['account'] = $channel['channel_account_id'];
        $ev['edited'] = $item['edited'];
        $ev['mid'] = $item['mid'];
        $ev['private'] = $item['item_private'];
        // is this an edit?
        if ($item['resource_type'] === 'event') {
            $ev['event_hash'] = $item['resource_id'];
        }
        $event = event_store_event($ev);
        if ($event) {
            $r = q("update item set resource_id = '%s', resource_type = 'event' where id = %d and uid = %d limit 1", dbesc($event['event_hash']), intval($item['id']), intval($channel['channel_id']));
            return true;
        }
    }
    return false;
}