Example #1
1
function gprobe_run($argv, $argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    if ($argc != 2) {
        return;
    }
    $url = hex2bin($argv[1]);
    $r = q("select * from gcontact where nurl = '%s' limit 1", dbesc(normalise_link($url)));
    if (!count($r)) {
        $arr = probe_url($url);
        if (count($arr) && x($arr, 'network') && $arr['network'] === NETWORK_DFRN) {
            q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)\n\t\t\t\tvalues ( '%s', '%s', '%s', '%s') ", dbesc($arr['name']), dbesc($arr['url']), dbesc(normalise_link($arr['url'])), dbesc($arr['photo']));
        }
        $r = q("select * from gcontact where nurl = '%s' limit 1", dbesc(normalise_link($url)));
    }
    if (count($r)) {
        poco_load(0, 0, $r[0]['id'], str_replace('/profile/', '/poco/', $r[0]['url']));
    }
    return;
}
Example #2
0
function ostatus_follow_friends($uid, $url)
{
    $contact = probe_url($url);
    if (!$contact) {
        return;
    }
    $api = $contact["baseurl"] . "/api/";
    // Fetching friends
    $data = z_fetch_url($api . "statuses/friends.json?screen_name=" . $contact["nick"]);
    if (!$data["success"]) {
        return;
    }
    $friends = json_decode($data["body"]);
    foreach ($friends as $friend) {
        $url = $friend->statusnet_profile_url;
        $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND\n\t\t\t(`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND\n\t\t\t`network` != '%s' LIMIT 1", intval($uid), dbesc(normalise_link($url)), dbesc(normalise_link($url)), dbesc($url), dbesc(NETWORK_STATUSNET));
        if (!$r) {
            $data = probe_url($friend->statusnet_profile_url);
            if ($data["network"] == NETWORK_OSTATUS) {
                $result = new_contact($uid, $friend->statusnet_profile_url);
                if ($result["success"]) {
                    logger($friend->name . " " . $url . " - success", LOGGER_DEBUG);
                } else {
                    logger($friend->name . " " . $url . " - failed", LOGGER_DEBUG);
                }
            } else {
                logger($friend->name . " " . $url . " - not OStatus", LOGGER_DEBUG);
            }
        }
    }
}
Example #3
0
function follow_content(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $uid = local_user();
    $url = notags(trim($_REQUEST['url']));
    $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND\n\t\t(`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND\n\t\t`network` != '%s' LIMIT 1", intval(local_user()), dbesc(normalise_link($url)), dbesc(normalise_link($url)), dbesc($url), dbesc(NETWORK_STATUSNET));
    if ($r) {
        notice(t('You already added this contact.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $ret = probe_url($url);
    if ($ret['network'] === NETWORK_DFRN) {
        $request = $ret["request"];
        $tpl = get_markup_template('dfrn_request.tpl');
    } else {
        $request = $a->get_baseurl() . "/follow";
        $tpl = get_markup_template('auto_request.tpl');
    }
    $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
    if (!$r) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $myaddr = $r[0]["url"];
    // Makes the connection request for friendica contacts easier
    $_SESSION["fastlane"] = $ret["url"];
    $o = replace_macros($tpl, array('$header' => $ret["name"] . " (" . $ret["addr"] . ")", '$photo' => $ret["photo"], '$desc' => "", '$pls_answer' => t('Please answer the following:'), '$does_know_you' => array('knowyou', sprintf(t('Does %s know you?'), $ret["name"]), false, '', array(t('No'), t('Yes'))), '$add_note' => t('Add a personal note:'), '$page_desc' => "", '$friendica' => "", '$statusnet' => "", '$diaspora' => "", '$diasnote' => "", '$your_address' => t('Your Identity Address:'), '$invite_desc' => "", '$emailnet' => "", '$submit' => t('Submit Request'), '$cancel' => t('Cancel'), '$nickname' => "", '$name' => $ret["name"], '$url' => $ret["url"], '$myaddr' => $myaddr, '$request' => $request));
    return $o;
}
function ostatus_subscribe_content(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $o = "<h2>" . t("Subsribing to OStatus contacts") . "</h2>";
    $uid = local_user();
    $a = get_app();
    $counter = intval($_REQUEST['counter']);
    if (get_pconfig($uid, "ostatus", "legacy_friends") == "") {
        if ($_REQUEST["url"] == "") {
            return $o . t("No contact provided.");
        }
        $contact = probe_url($_REQUEST["url"]);
        if (!$contact) {
            return $o . t("Couldn't fetch information for contact.");
        }
        $api = $contact["baseurl"] . "/api/";
        // Fetching friends
        $data = z_fetch_url($api . "statuses/friends.json?screen_name=" . $contact["nick"]);
        if (!$data["success"]) {
            return $o . t("Couldn't fetch friends for contact.");
        }
        set_pconfig($uid, "ostatus", "legacy_friends", $data["body"]);
    }
    $friends = json_decode(get_pconfig($uid, "ostatus", "legacy_friends"));
    $total = sizeof($friends);
    if ($counter >= $total) {
        $a->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . $a->get_baseurl() . '/settings/connectors">';
        del_pconfig($uid, "ostatus", "legacy_friends");
        del_pconfig($uid, "ostatus", "legacy_contact");
        $o .= t("Done");
        return $o;
    }
    $friend = $friends[$counter++];
    $url = $friend->statusnet_profile_url;
    $o .= "<p>" . $counter . "/" . $total . ": " . $url;
    $data = probe_url($url);
    if ($data["network"] == NETWORK_OSTATUS) {
        $result = new_contact($uid, $url, true);
        if ($result["success"]) {
            $o .= " - " . t("success");
        } else {
            $o .= " - " . t("failed");
        }
    } else {
        $o .= " - " . t("ignored");
    }
    $o .= "</p>";
    $o .= "<p>" . t("Keep this window open until done.") . "</p>";
    $a->page['htmlhead'] = '<meta http-equiv="refresh" content="0; URL=' . $a->get_baseurl() . '/ostatus_subscribe?counter=' . $counter . '">';
    return $o;
}
Example #5
0
function acctlink_init(&$a)
{
    if (x($_GET, 'addr')) {
        $addr = trim($_GET['addr']);
        $res = probe_url($addr);
        //logger('acctlink: ' . print_r($res,true));
        if ($res['url']) {
            goaway($res['url']);
            killme();
        }
    }
}
Example #6
0
function gprobe_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/session.php';
    require_once 'include/datetime.php';
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    if ($argc != 2) {
        return;
    }
    $url = hex2bin($argv[1]);
    $r = q("select * from gcontact where nurl = '%s' limit 1", dbesc(normalise_link($url)));
    logger("gprobe start for " . normalise_link($url), LOGGER_DEBUG);
    if (!count($r)) {
        // Is it a DDoS attempt?
        $urlparts = parse_url($url);
        $result = Cache::get("gprobe:" . $urlparts["host"]);
        if (!is_null($result)) {
            $result = unserialize($result);
            if ($result["network"] == NETWORK_FEED) {
                logger("DDoS attempt detected for " . $urlparts["host"] . " by " . $_SERVER["REMOTE_ADDR"] . ". server data: " . print_r($_SERVER, true), LOGGER_DEBUG);
                return;
            }
        }
        $arr = probe_url($url);
        if (is_null($result)) {
            Cache::set("gprobe:" . $urlparts["host"], serialize($arr));
        }
        if (count($arr) && x($arr, 'network') && $arr['network'] === NETWORK_DFRN) {
            q("insert into `gcontact` (`name`,`url`,`nurl`,`photo`)\n\t\t\t\tvalues ( '%s', '%s', '%s', '%s') ", dbesc($arr['name']), dbesc($arr['url']), dbesc(normalise_link($arr['url'])), dbesc($arr['photo']));
        }
        $r = q("select * from gcontact where nurl = '%s' limit 1", dbesc(normalise_link($url)));
    }
    if (count($r)) {
        poco_load(0, 0, $r[0]['id'], str_replace('/profile/', '/poco/', $r[0]['url']));
    }
    logger("gprobe end for " . normalise_link($url), LOGGER_DEBUG);
    return;
}
Example #7
0
function probe_content(&$a)
{
    $o .= '<h3>Probe Diagnostic</h3>';
    $o .= '<form action="probe" method="get">';
    $o .= 'Lookup address: <input type="text" style="width: 250px;" name="addr" value="' . $_GET['addr'] . '" />';
    $o .= '<input type="submit" name="submit" value="Submit" /></form>';
    $o .= '<br /><br />';
    if (x($_GET, 'addr')) {
        $addr = trim($_GET['addr']);
        $res = probe_url($addr);
        $o .= '<pre>';
        $o .= str_replace("\n", '<br />', print_r($res, true));
        $o .= '</pre>';
    }
    return $o;
}
Example #8
0
function find_diaspora_person_by_handle($handle)
{
    $update = false;
    $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1", dbesc(NETWORK_DIASPORA), dbesc($handle));
    if (count($r)) {
        logger('find_diaspora_person_by handle: in cache ' . print_r($r, true), LOGGER_DEBUG);
        // update record occasionally so it doesn't get stale
        $d = strtotime($r[0]['updated'] . ' +00:00');
        if ($d > strtotime('now - 14 days')) {
            return $r[0];
        }
        $update = true;
    }
    logger('find_diaspora_person_by_handle: refresh', LOGGER_DEBUG);
    require_once 'include/Scrape.php';
    $r = probe_url($handle, PROBE_DIASPORA);
    if (count($r) && $r['network'] === NETWORK_DIASPORA) {
        add_fcontact($r, $update);
        return $r;
    }
    return false;
}
Example #9
0
function _contact_update_profile($contact_id)
{
    $r = q("SELECT `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
    if (!$r) {
        return;
    }
    $data = probe_url($r[0]["url"]);
    // "Feed" is mostly a sign of communication problems
    if ($data["network"] == NETWORK_FEED and $data["network"] != $r[0]["network"]) {
        return;
    }
    $updatefields = array("name", "nick", "url", "addr", "batch", "notify", "poll", "request", "confirm", "poco", "network", "alias", "pubkey");
    $update = array();
    foreach ($updatefields as $field) {
        if (isset($data[$field]) and $data[$field] != "") {
            $update[$field] = $data[$field];
        }
    }
    $update["nurl"] = normalise_link($data["url"]);
    $query = "";
    if (isset($data["priority"]) and $data["priority"] != 0) {
        $query = "`priority` = " . intval($data["priority"]);
    }
    foreach ($update as $key => $value) {
        if ($query != "") {
            $query .= ", ";
        }
        $query .= "`" . $key . "` = '" . dbesc($value) . "'";
    }
    if ($query == "") {
        return;
    }
    $r = q("UPDATE `contact` SET {$query} WHERE `id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
    $photos = import_profile_photo($data['photo'], local_user(), $contact_id);
    $r = q("UPDATE `contact` SET `photo` = '%s',\n\t\t\t`thumb` = '%s',\n\t\t\t`micro` = '%s',\n\t\t\t`name-date` = '%s',\n\t\t\t`uri-date` = '%s',\n\t\t\t`avatar-date` = '%s'\n\t\t\tWHERE `id` = %d", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($contact_id));
}
Example #10
0
/**
 * This function removes the tag $tag from the text $body and replaces it with
 * the appropiate link.
 *
 * @param unknown_type $body the text to replace the tag in
 * @param unknown_type $inform a comma-seperated string containing everybody to inform
 * @param unknown_type $str_tags string to add the tag to
 * @param unknown_type $profile_uid
 * @param unknown_type $tag the tag to replace
 *
 * @return boolean true if replaced, false if not replaced
 */
function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $network = "")
{
    $replaced = false;
    $r = null;
    //is it a person tag?
    if (strpos($tag, '@') === 0) {
        //is it already replaced?
        if (strpos($tag, '[url=')) {
            //append tag to str_tags
            if (!stristr($str_tags, $tag)) {
                if (strlen($str_tags)) {
                    $str_tags .= ',';
                }
                $str_tags .= $tag;
            }
            // Checking for the alias that is used for OStatus
            $pattern = "/@\\[url\\=(.*?)\\](.*?)\\[\\/url\\]/ism";
            if (preg_match($pattern, $tag, $matches)) {
                $data = probe_url($matches[1]);
                if ($data["alias"] != "") {
                    $newtag = '@[url=' . $data["alias"] . ']' . $data["name"] . '[/url]';
                    if (!stristr($str_tags, $newtag)) {
                        if (strlen($str_tags)) {
                            $str_tags .= ',';
                        }
                        $str_tags .= $newtag;
                    }
                }
            }
            return $replaced;
        }
        $stat = false;
        //get the person's name
        $name = substr($tag, 1);
        //is it a link or a full dfrn address?
        if (strpos($name, '@') || strpos($name, 'http://')) {
            $newname = $name;
            //get the profile links
            $links = @lrdd($name);
            if (count($links)) {
                //for all links, collect how is to inform and how's profile is to link
                foreach ($links as $link) {
                    if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
                        $profile = $link['@attributes']['href'];
                    }
                    if ($link['@attributes']['rel'] === 'salmon') {
                        if (strlen($inform)) {
                            $inform .= ',';
                        }
                        $inform .= 'url:' . str_replace(',', '%2c', $link['@attributes']['href']);
                    }
                }
            }
        } elseif ($network != NETWORK_OSTATUS and $network != NETWORK_TWITTER and $network != NETWORK_STATUSNET and $network != NETWORK_APPNET) {
            //if it is a name rather than an address
            $newname = $name;
            $alias = '';
            $tagcid = 0;
            //is it some generated name?
            if (strrpos($newname, '+')) {
                //get the id
                $tagcid = intval(substr($newname, strrpos($newname, '+') + 1));
                //remove the next word from tag's name
                if (strpos($name, ' ')) {
                    $name = substr($name, 0, strpos($name, ' '));
                }
            }
            if ($tagcid) {
                //if there was an id
                //select contact with that id from the logged in user's contact list
                $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($tagcid), intval($profile_uid));
            } else {
                $newname = str_replace('_', ' ', $name);
                // At first try to fetch a contact according to the given network
                if ($network != "") {
                    //select someone from this user's contacts by name
                    $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1", dbesc($newname), dbesc($network), intval($profile_uid));
                    if (!$r) {
                        //select someone by attag or nick and the name passed in
                        $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `network` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1", dbesc($name), dbesc($name), dbesc($network), intval($profile_uid));
                    }
                } else {
                    $r = false;
                }
                if (!$r) {
                    //select someone from this user's contacts by name
                    $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1", dbesc($newname), intval($profile_uid));
                }
                if (!$r) {
                    //select someone by attag or nick and the name passed in
                    $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1", dbesc($name), dbesc($name), intval($profile_uid));
                }
            }
            /*			} elseif(strstr($name,'_') || strstr($name,' ')) { //no id
            				//get the real name
            				$newname = str_replace('_',' ',$name);
            				//select someone from this user's contacts by name
            				$r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
            						dbesc($newname),
            						intval($profile_uid)
            				);
            			} else {
            				//select someone by attag or nick and the name passed in
            				$r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
            						dbesc($name),
            						dbesc($name),
            						intval($profile_uid)
            				);
            			}*/
            //$r is set, if someone could be selected
            if (count($r)) {
                $profile = $r[0]['url'];
                //set newname to nick, find alias
                if ($r[0]['network'] === NETWORK_OSTATUS or $r[0]['network'] === NETWORK_TWITTER or $r[0]['network'] === NETWORK_STATUSNET or $r[0]['network'] === NETWORK_APPNET) {
                    $newname = $r[0]['nick'];
                    $stat = true;
                    if ($r[0]['alias']) {
                        $alias = $r[0]['alias'];
                    }
                } else {
                    $newname = $r[0]['name'];
                }
                //add person's id to $inform
                if (strlen($inform)) {
                    $inform .= ',';
                }
                $inform .= 'cid:' . $r[0]['id'];
            }
        }
        //if there is an url for this persons profile
        if (isset($profile)) {
            $replaced = true;
            //create profile link
            $profile = str_replace(',', '%2c', $profile);
            $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
            $body = str_replace('@' . $name, $newtag, $body);
            //append tag to str_tags
            if (!stristr($str_tags, $newtag)) {
                if (strlen($str_tags)) {
                    $str_tags .= ',';
                }
                $str_tags .= $newtag;
            }
            // Status.Net seems to require the numeric ID URL in a mention if the person isn't
            // subscribed to you. But the nickname URL is OK if they are. Grrr. We'll tag both.
            if (strlen($alias)) {
                $newtag = '@[url=' . $alias . ']' . $newname . '[/url]';
                if (!stristr($str_tags, $newtag)) {
                    if (strlen($str_tags)) {
                        $str_tags .= ',';
                    }
                    $str_tags .= $newtag;
                }
            }
        }
    }
    return array('replaced' => $replaced, 'contact' => $r[0]);
}
Example #11
0
function poco_last_updated($profile, $force = false)
{
    $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
    if ($gcontacts[0]["created"] == "0000-00-00 00:00:00") {
        q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
    }
    if ($gcontacts[0]["server_url"] != "") {
        $server_url = $gcontacts[0]["server_url"];
    } else {
        $server_url = poco_detect_server($profile);
    }
    if ($server_url != "") {
        if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
            if ($force) {
                q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
            }
            return false;
        }
        q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'", dbesc($server_url), dbesc(normalise_link($profile)));
    }
    if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
        $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''", dbesc(normalise_link($server_url)));
        if ($server) {
            q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
        } else {
            return;
        }
    }
    // noscrape is really fast so we don't cache the call.
    if ($gcontacts[0]["server_url"] != "" and $gcontacts[0]["nick"] != "") {
        //  Use noscrape if possible
        $server = q("SELECT `noscrape` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
        if ($server) {
            $noscraperet = z_fetch_url($server[0]["noscrape"] . "/" . $gcontacts[0]["nick"]);
            if ($noscraperet["success"] and $noscraperet["body"] != "") {
                $noscrape = json_decode($noscraperet["body"], true);
                if ($noscrape["fn"] != "" and $noscrape["fn"] != $gcontacts[0]["name"]) {
                    q("UPDATE `gcontact` SET `name` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["fn"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["photo"] != "" and $noscrape["photo"] != $gcontacts[0]["photo"]) {
                    q("UPDATE `gcontact` SET `photo` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["photo"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["updated"] != "" and $noscrape["updated"] != $gcontacts[0]["updated"]) {
                    q("UPDATE `gcontact` SET `updated` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["updated"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["gender"] != "" and $noscrape["gender"] != $gcontacts[0]["gender"]) {
                    q("UPDATE `gcontact` SET `gender` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["gender"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["pdesc"] != "" and $noscrape["pdesc"] != $gcontacts[0]["about"]) {
                    q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["pdesc"]), dbesc(normalise_link($profile)));
                }
                if ($noscrape["about"] != "" and $noscrape["about"] != $gcontacts[0]["about"]) {
                    q("UPDATE `gcontact` SET `about` = '%s' WHERE `nurl` = '%s'", dbesc($noscrape["about"]), dbesc(normalise_link($profile)));
                }
                if (isset($noscrape["comm"]) and $noscrape["comm"] != $gcontacts[0]["community"]) {
                    q("UPDATE `gcontact` SET `community` = %d WHERE `nurl` = '%s'", intval($noscrape["comm"]), dbesc(normalise_link($profile)));
                }
                if (isset($noscrape["tags"])) {
                    $keywords = implode(" ", $noscrape["tags"]);
                } else {
                    $keywords = "";
                }
                if ($keywords != "" and $keywords != $gcontacts[0]["keywords"]) {
                    q("UPDATE `gcontact` SET `keywords` = '%s' WHERE `nurl` = '%s'", dbesc($keywords), dbesc(normalise_link($profile)));
                }
                $location = $noscrape["locality"];
                if ($noscrape["region"] != "") {
                    if ($location != "") {
                        $location .= ", ";
                    }
                    $location .= $noscrape["region"];
                }
                if ($noscrape["country-name"] != "") {
                    if ($location != "") {
                        $location .= ", ";
                    }
                    $location .= $noscrape["country-name"];
                }
                if ($location != "" and $location != $gcontacts[0]["location"]) {
                    q("UPDATE `gcontact` SET `location` = '%s' WHERE `nurl` = '%s'", dbesc($location), dbesc(normalise_link($profile)));
                }
                // If we got data from noscrape then mark the contact as reachable
                if (is_array($noscrape) and count($noscrape)) {
                    q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
                }
                return $noscrape["updated"];
            }
        }
    }
    // If we only can poll the feed, then we only do this once a while
    if (!$force and !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
        return $gcontacts[0]["updated"];
    }
    $data = probe_url($profile);
    // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
    // Then check the other link and delete this one
    if ($data["network"] == NETWORK_OSTATUS and poco_alternate_ostatus_url($profile) and normalise_link($profile) == normalise_link($data["alias"]) and normalise_link($profile) != normalise_link($data["url"])) {
        // Delete the old entry
        q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
        q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
        poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"], $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]);
        poco_last_updated($data["url"], $force);
        return false;
    }
    if ($data["poll"] == "" or $data["network"] == NETWORK_FEED) {
        q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
        return false;
    }
    if ($data["name"] != "" and $data["name"] != $gcontacts[0]["name"]) {
        q("UPDATE `gcontact` SET `name` = '%s' WHERE `nurl` = '%s'", dbesc($data["name"]), dbesc(normalise_link($profile)));
    }
    if ($data["nick"] != "" and $data["nick"] != $gcontacts[0]["nick"]) {
        q("UPDATE `gcontact` SET `nick` = '%s' WHERE `nurl` = '%s'", dbesc($data["nick"]), dbesc(normalise_link($profile)));
    }
    if ($data["addr"] != "" and $data["addr"] != $gcontacts[0]["connect"]) {
        q("UPDATE `gcontact` SET `connect` = '%s' WHERE `nurl` = '%s'", dbesc($data["addr"]), dbesc(normalise_link($profile)));
    }
    if ($data["photo"] != "" and $data["photo"] != $gcontacts[0]["photo"]) {
        q("UPDATE `gcontact` SET `photo` = '%s' WHERE `nurl` = '%s'", dbesc($data["photo"]), dbesc(normalise_link($profile)));
    }
    if ($data["baseurl"] != "" and $data["baseurl"] != $gcontacts[0]["server_url"]) {
        q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'", dbesc($data["baseurl"]), dbesc(normalise_link($profile)));
    }
    $feedret = z_fetch_url($data["poll"]);
    if (!$feedret["success"]) {
        q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc(normalise_link($profile)));
        return false;
    }
    $doc = new DOMDocument();
    @$doc->loadXML($feedret["body"]);
    $xpath = new DomXPath($doc);
    $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
    $entries = $xpath->query('/atom:feed/atom:entry');
    $last_updated = "";
    foreach ($entries as $entry) {
        $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
        $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
        if ($last_updated < $published) {
            $last_updated = $published;
        }
        if ($last_updated < $updated) {
            $last_updated = $updated;
        }
    }
    // Maybe there aren't any entries. Then check if it is a valid feed
    if ($last_updated == "") {
        if ($xpath->query('/atom:feed')->length > 0) {
            $last_updated = "0000-00-00 00:00:00";
        }
    }
    q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'", dbesc($last_updated), dbesc(datetime_convert()), dbesc(normalise_link($profile)));
    if ($gcontacts[0]["generation"] == 0) {
        q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
    }
    return $last_updated;
}
Example #12
0
function dirfind_content(&$a, $prefix = "")
{
    $community = false;
    $discover_user = false;
    $local = get_config('system', 'poco_local_search');
    $search = $prefix . notags(trim($_REQUEST['search']));
    if (strpos($search, '@') === 0) {
        $search = substr($search, 1);
        if (valid_email($search) and validate_email($search) or substr(normalise_link($search), 0, 7) == "http://") {
            $user_data = probe_url($search);
            $discover_user = in_array($user_data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA));
        }
    }
    if (strpos($search, '!') === 0) {
        $search = substr($search, 1);
        $community = true;
    }
    $o = '';
    if ($search) {
        if ($discover_user) {
            $j = new stdClass();
            $j->total = 1;
            $j->items_page = 1;
            $j->page = $a->pager['page'];
            $objresult = new stdClass();
            $objresult->cid = 0;
            $objresult->name = $user_data["name"];
            $objresult->addr = $user_data["addr"];
            $objresult->url = $user_data["url"];
            $objresult->photo = $user_data["photo"];
            $objresult->tags = "";
            $objresult->network = $user_data["network"];
            $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1", dbesc(normalise_link($user_data["url"])), intval(local_user()));
            if ($contact) {
                $objresult->cid = $contact[0]["id"];
            }
            $j->results[] = $objresult;
            poco_check($user_data["url"], $user_data["name"], $user_data["network"], $user_data["photo"], "", "", "", "", "", datetime_convert(), 0);
        } elseif ($local) {
            if ($community) {
                $extra_sql = " AND `community`";
            } else {
                $extra_sql = "";
            }
            $perpage = 80;
            $startrec = $a->pager['page'] * $perpage - $perpage;
            if (get_config('system', 'diaspora_enabled')) {
                $diaspora = NETWORK_DIASPORA;
            } else {
                $diaspora = NETWORK_DFRN;
            }
            if (!get_config('system', 'ostatus_disabled')) {
                $ostatus = NETWORK_OSTATUS;
            } else {
                $ostatus = NETWORK_DFRN;
            }
            $count = q("SELECT count(*) AS `total` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND\n\t\t\t\t\t(`url` REGEXP '%s' OR `name` REGEXP '%s' OR `location` REGEXP '%s' OR\n\t\t\t\t\t\t`about` REGEXP '%s' OR `keywords` REGEXP '%s')" . $extra_sql, dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)));
            $results = q("SELECT `contact`.`id` AS `cid`, `gcontact`.`url`, `gcontact`.`name`, `gcontact`.`photo`, `gcontact`.`network`, `gcontact`.`keywords`, `gcontact`.`addr`\n\t\t\t\t\tFROM `gcontact`\n\t\t\t\t\tLEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl`\n\t\t\t\t\t\tAND `contact`.`uid` = %d AND NOT `contact`.`blocked`\n\t\t\t\t\t\tAND NOT `contact`.`pending` AND `contact`.`rel` IN ('%s', '%s')\n\t\t\t\t\tWHERE `gcontact`.`network` IN ('%s', '%s', '%s') AND\n\t\t\t\t\t((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`)) AND\n\t\t\t\t\t(`gcontact`.`url` REGEXP '%s' OR `gcontact`.`name` REGEXP '%s' OR `gcontact`.`location` REGEXP '%s' OR\n\t\t\t\t\t\t`gcontact`.`about` REGEXP '%s' OR `gcontact`.`keywords` REGEXP '%s') {$extra_sql}\n\t\t\t\t\t\tGROUP BY `gcontact`.`nurl`\n\t\t\t\t\t\tORDER BY `gcontact`.`updated` DESC LIMIT %d, %d", intval(local_user()), dbesc(CONTACT_IS_SHARING), dbesc(CONTACT_IS_FRIEND), dbesc(NETWORK_DFRN), dbesc($ostatus), dbesc($diaspora), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), dbesc(escape_tags($search)), intval($startrec), intval($perpage));
            $j = new stdClass();
            $j->total = $count[0]["total"];
            $j->items_page = $perpage;
            $j->page = $a->pager['page'];
            foreach ($results as $result) {
                if (poco_alternate_ostatus_url($result["url"])) {
                    continue;
                }
                if ($result["name"] == "") {
                    $urlparts = parse_url($result["url"]);
                    $result["name"] = end(explode("/", $urlparts["path"]));
                }
                $objresult = new stdClass();
                $objresult->cid = $result["cid"];
                $objresult->name = $result["name"];
                $objresult->addr = $result["addr"];
                $objresult->url = $result["url"];
                $objresult->photo = $result["photo"];
                $objresult->tags = $result["keywords"];
                $objresult->network = $result["network"];
                $j->results[] = $objresult;
            }
            // Add found profiles from the global directory to the local directory
            proc_run('php', 'include/discover_poco.php', "dirsearch", urlencode($search));
        } else {
            $p = $a->pager['page'] != 1 ? '&p=' . $a->pager['page'] : '';
            if (strlen(get_config('system', 'directory'))) {
                $x = fetch_url(get_server() . '/lsearch?f=' . $p . '&search=' . urlencode($search));
            }
            $j = json_decode($x);
        }
        if ($j->total) {
            $a->set_pager_total($j->total);
            $a->set_pager_itemspage($j->items_page);
        }
        if (count($j->results)) {
            $id = 0;
            foreach ($j->results as $jj) {
                $alt_text = "";
                $contact_details = get_contact_details_by_url($jj->url, local_user());
                $itemurl = $contact_details["addr"] != "" ? $contact_details["addr"] : $jj->url;
                // If We already know this contact then don't show the "connect" button
                if ($jj->cid > 0) {
                    $connlnk = "";
                    $conntxt = "";
                    $contact = q("SELECT * FROM `contact` WHERE `id` = %d", intval($jj->cid));
                    if ($contact) {
                        $photo_menu = contact_photo_menu($contact[0]);
                        $details = _contact_detail_for_template($contact[0]);
                        $alt_text = $details['alt_text'];
                    } else {
                        $photo_menu = array();
                    }
                } else {
                    $connlnk = $a->get_baseurl() . '/follow/?url=' . ($jj->connect ? $jj->connect : $jj->url);
                    $conntxt = t('Connect');
                    $photo_menu = array(array(t("View Profile"), zrl($jj->url)));
                    $photo_menu[] = array(t("Connect/Follow"), $connlnk);
                }
                $jj->photo = str_replace("http:///photo/", get_server() . "/photo/", $jj->photo);
                $entry = array('alt_text' => $alt_text, 'url' => zrl($jj->url), 'itemurl' => $itemurl, 'name' => htmlentities($jj->name), 'thumb' => proxy_url($jj->photo, false, PROXY_SIZE_THUMB), 'img_hover' => $jj->tags, 'conntxt' => $conntxt, 'connlnk' => $connlnk, 'photo_menu' => $photo_menu, 'details' => $contact_details['location'], 'tags' => $contact_details['keywords'], 'about' => $contact_details['about'], 'account_type' => $contact_details['community'] ? t('Forum') : '', 'network' => network_to_name($jj->network, $jj->url), 'id' => ++$id);
                $entries[] = $entry;
            }
            $tpl = get_markup_template('viewcontact_template.tpl');
            $o .= replace_macros($tpl, array('title' => sprintf(t('People Search - %s'), $search), '$contacts' => $entries, '$paginate' => paginate($a)));
        } else {
            info(t('No matches') . EOL);
        }
    }
    return $o;
}
Example #13
0
function probe_url($url, $mode = PROBE_NORMAL, $level = 1)
{
    require_once 'include/email.php';
    $result = array();
    if (!$url) {
        return $result;
    }
    $result = Cache::get("probe_url:" . $mode . ":" . $url);
    if (!is_null($result)) {
        $result = unserialize($result);
        return $result;
    }
    $network = null;
    $diaspora = false;
    $diaspora_base = '';
    $diaspora_guid = '';
    $diaspora_key = '';
    $has_lrdd = false;
    $email_conversant = false;
    $connectornetworks = false;
    $appnet = false;
    if (strpos($url, 'twitter.com')) {
        $connectornetworks = true;
        $network = NETWORK_TWITTER;
    }
    // Twitter is deactivated since twitter closed its old API
    //$twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
    $lastfm = strpos($url, 'last.fm/user') !== false ? true : false;
    $at_addr = strpos($url, '@') !== false ? true : false;
    if (!$appnet && !$lastfm && !$connectornetworks) {
        if (strpos($url, 'mailto:') !== false && $at_addr) {
            $url = str_replace('mailto:', '', $url);
            $links = array();
        } else {
            $links = lrdd($url);
        }
        if (count($links)) {
            $has_lrdd = true;
            logger('probe_url: found lrdd links: ' . print_r($links, true), LOGGER_DATA);
            foreach ($links as $link) {
                if ($link['@attributes']['rel'] === NAMESPACE_ZOT) {
                    $zot = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === NAMESPACE_DFRN) {
                    $dfrn = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === 'salmon') {
                    $notify = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === NAMESPACE_FEED) {
                    $poll = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') {
                    $hcard = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
                    $profile = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0') {
                    $poco = unamp($link['@attributes']['href']);
                }
                if ($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
                    $diaspora_base = unamp($link['@attributes']['href']);
                    $diaspora = true;
                }
                if ($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
                    $diaspora_guid = unamp($link['@attributes']['href']);
                    $diaspora = true;
                }
                if ($link['@attributes']['rel'] === 'diaspora-public-key') {
                    $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
                    if (strstr($diaspora_key, 'RSA ')) {
                        $pubkey = rsatopem($diaspora_key);
                    } else {
                        $pubkey = $diaspora_key;
                    }
                    $diaspora = true;
                }
                if ($link['@attributes']['rel'] === 'http://ostatus.org/schema/1.0/subscribe' and $mode == PROBE_NORMAL) {
                    $diaspora = false;
                }
            }
            // Status.Net can have more than one profile URL. We need to match the profile URL
            // to a contact on incoming messages to prevent spam, and we won't know which one
            // to match. So in case of two, one of them is stored as an alias. Only store URL's
            // and not webfinger user@host aliases. If they've got more than two non-email style
            // aliases, let's hope we're lucky and get one that matches the feed author-uri because
            // otherwise we're screwed.
            foreach ($links as $link) {
                if ($link['@attributes']['rel'] === 'alias') {
                    if (strpos($link['@attributes']['href'], '@') === false) {
                        if (isset($profile)) {
                            if ($link['@attributes']['href'] !== $profile) {
                                $alias = unamp($link['@attributes']['href']);
                            }
                        } else {
                            $profile = unamp($link['@attributes']['href']);
                        }
                    }
                }
            }
            // If the profile is different from the url then the url is abviously an alias
            if ($alias == "" and $profile != "" and !$at_addr and normalise_link($profile) != normalise_link($url)) {
                $alias = $url;
            }
        } elseif ($mode == PROBE_NORMAL) {
            // Check email
            $orig_url = $url;
            if (strpos($orig_url, '@') && validate_email($orig_url)) {
                $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval(local_user()));
                $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval(local_user()));
                if (count($x) && count($r)) {
                    $mailbox = construct_mailbox_name($r[0]);
                    $password = '';
                    openssl_private_decrypt(hex2bin($r[0]['pass']), $password, $x[0]['prvkey']);
                    $mbox = email_connect($mailbox, $r[0]['user'], $password);
                    if (!$mbox) {
                        logger('probe_url: email_connect failed.');
                    }
                    unset($password);
                }
                if ($mbox) {
                    $msgs = email_poll($mbox, $orig_url);
                    logger('probe_url: searching ' . $orig_url . ', ' . count($msgs) . ' messages found.', LOGGER_DEBUG);
                    if (count($msgs)) {
                        $addr = $orig_url;
                        $network = NETWORK_MAIL;
                        $name = substr($url, 0, strpos($url, '@'));
                        $phost = substr($url, strpos($url, '@') + 1);
                        $profile = 'http://' . $phost;
                        // fix nick character range
                        $vcard = array('fn' => $name, 'nick' => $name, 'photo' => avatar_img($url));
                        $notify = 'smtp ' . random_string();
                        $poll = 'email ' . random_string();
                        $priority = 0;
                        $x = email_msg_meta($mbox, $msgs[0]);
                        if (stristr($x[0]->from, $orig_url)) {
                            $adr = imap_rfc822_parse_adrlist($x[0]->from, '');
                        } elseif (stristr($x[0]->to, $orig_url)) {
                            $adr = imap_rfc822_parse_adrlist($x[0]->to, '');
                        }
                        if (isset($adr)) {
                            foreach ($adr as $feadr) {
                                if (strcasecmp($feadr->mailbox, $name) == 0 && strcasecmp($feadr->host, $phost) == 0 && strlen($feadr->personal)) {
                                    $personal = imap_mime_header_decode($feadr->personal);
                                    $vcard['fn'] = "";
                                    foreach ($personal as $perspart) {
                                        if ($perspart->charset != "default") {
                                            $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
                                        } else {
                                            $vcard['fn'] .= $perspart->text;
                                        }
                                    }
                                    $vcard['fn'] = notags($vcard['fn']);
                                }
                            }
                        }
                    }
                    imap_close($mbox);
                }
            }
        }
    }
    if ($mode == PROBE_NORMAL) {
        if (strlen($zot)) {
            $s = fetch_url($zot);
            if ($s) {
                $j = json_decode($s);
                if ($j) {
                    $network = NETWORK_ZOT;
                    $vcard = array('fn' => $j->fullname, 'nick' => $j->nickname, 'photo' => $j->photo);
                    $profile = $j->url;
                    $notify = $j->post;
                    $pubkey = $j->pubkey;
                    $poll = 'N/A';
                }
            }
        }
        if (strlen($dfrn)) {
            $ret = scrape_dfrn($hcard ? $hcard : $dfrn, true);
            if (is_array($ret) && x($ret, 'dfrn-request')) {
                $network = NETWORK_DFRN;
                $request = $ret['dfrn-request'];
                $confirm = $ret['dfrn-confirm'];
                $notify = $ret['dfrn-notify'];
                $poll = $ret['dfrn-poll'];
                $vcard = array();
                $vcard['fn'] = $ret['fn'];
                $vcard['nick'] = $ret['nick'];
                $vcard['photo'] = $ret['photo'];
            }
        }
    }
    if ($diaspora && $diaspora_base && $diaspora_guid) {
        if ($mode == PROBE_DIASPORA || !$notify) {
            $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
            $batch = $diaspora_base . 'receive/public';
        }
        if (strpos($url, '@')) {
            $addr = str_replace('acct:', '', $url);
        }
    }
    if ($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
        if ($diaspora) {
            $network = NETWORK_DIASPORA;
        } elseif ($has_lrdd and $notify) {
            $network = NETWORK_OSTATUS;
        }
        if (strpos($url, '@')) {
            $addr = str_replace('acct:', '', $url);
        }
        $priority = 0;
        if ($hcard && !$vcard) {
            $vcard = scrape_vcard($hcard);
            // Google doesn't use absolute url in profile photos
            if (x($vcard, 'photo') && substr($vcard['photo'], 0, 1) == '/') {
                $h = @parse_url($hcard);
                if ($h) {
                    $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
                }
            }
            logger('probe_url: scrape_vcard: ' . print_r($vcard, true), LOGGER_DATA);
        }
        if ($diaspora && $addr) {
            // Diaspora returns the name as the nick. As the nick will never be updated,
            // let's use the Diaspora nickname (the first part of the handle) as the nick instead
            $addr_parts = explode('@', $addr);
            $vcard['nick'] = $addr_parts[0];
        }
        /* if($twitter) {
        			logger('twitter: setup');
        			$tid = basename($url);
        			$tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
        			if(intval($tid))
        				$poll = $tapi . '?user_id=' . $tid;
        			else
        				$poll = $tapi . '?screen_name=' . $tid;
        			$profile = 'http://twitter.com/#!/' . $tid;
        			//$vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
        			$vcard['photo'] = 'https://api.twitter.com/1/users/profile_image?screen_name=' . $tid . '&size=bigger';
        			$vcard['nick'] = $tid;
        			$vcard['fn'] = $tid;
        		} */
        if ($lastfm) {
            $profile = $url;
            $poll = str_replace(array('www.', 'last.fm/'), array('', 'ws.audioscrobbler.com/1.0/'), $url) . '/recenttracks.rss';
            $vcard['nick'] = basename($url);
            $vcard['fn'] = $vcard['nick'] . t(' on Last.fm');
            $network = NETWORK_FEED;
        }
        if (!x($vcard, 'fn')) {
            if (x($vcard, 'nick')) {
                $vcard['fn'] = $vcard['nick'];
            }
        }
        $check_feed = false;
        if (stristr($url, 'tumblr.com') && !stristr($url, '/rss')) {
            $poll = $url . '/rss';
            $check_feed = true;
            // Will leave it to others to figure out how to grab the avatar, which is on the $url page in the open graph meta links
        }
        if ($appnet || !$poll) {
            $check_feed = true;
        }
        if (!isset($vcard) || !x($vcard, 'fn') || !$profile) {
            $check_feed = true;
        }
        if ($at_addr && !count($links)) {
            $check_feed = false;
        }
        if ($connectornetworks) {
            $check_feed = false;
        }
        if ($check_feed) {
            $feedret = scrape_feed($poll ? $poll : $url);
            logger('probe_url: scrape_feed ' . ($poll ? $poll : $url) . ' returns: ' . print_r($feedret, true), LOGGER_DATA);
            if (count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
                $poll = x($feedret, 'feed_atom') ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']);
                if (!x($vcard)) {
                    $vcard = array();
                }
            }
            if (x($feedret, 'photo') && !x($vcard, 'photo')) {
                $vcard['photo'] = $feedret['photo'];
            }
            require_once 'library/simplepie/simplepie.inc';
            $feed = new SimplePie();
            $xml = fetch_url($poll);
            logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
            $a = get_app();
            logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
            // Don't try and parse an empty string
            $feed->set_raw_data($xml ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
            $feed->init();
            if ($feed->error()) {
                logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
                $network = NETWORK_PHANTOM;
            }
            if (!x($vcard, 'photo')) {
                $vcard['photo'] = $feed->get_image_url();
            }
            $author = $feed->get_author();
            if ($author) {
                $vcard['fn'] = unxmlify(trim($author->get_name()));
                if (!$vcard['fn']) {
                    $vcard['fn'] = trim(unxmlify($author->get_email()));
                }
                if (strpos($vcard['fn'], '@') !== false) {
                    $vcard['fn'] = substr($vcard['fn'], 0, strpos($vcard['fn'], '@'));
                }
                $email = unxmlify($author->get_email());
                if (!$profile && $author->get_link()) {
                    $profile = trim(unxmlify($author->get_link()));
                }
                if (!$vcard['photo']) {
                    $rawtags = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
                    if ($rawtags) {
                        $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
                        if (x($elems, 'link') && $elems['link'][0]['attribs']['']['rel'] === 'photo') {
                            $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
                        }
                    }
                }
                // Fetch fullname via poco:displayName
                $pocotags = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
                if ($pocotags) {
                    $elems = $pocotags[0]['child']['http://portablecontacts.net/spec/1.0'];
                    if (isset($elems["displayName"])) {
                        $vcard['fn'] = $elems["displayName"][0]["data"];
                    }
                    if (isset($elems["preferredUsername"])) {
                        $vcard['nick'] = $elems["preferredUsername"][0]["data"];
                    }
                }
            } else {
                $item = $feed->get_item(0);
                if ($item) {
                    $author = $item->get_author();
                    if ($author) {
                        $vcard['fn'] = trim(unxmlify($author->get_name()));
                        if (!$vcard['fn']) {
                            $vcard['fn'] = trim(unxmlify($author->get_email()));
                        }
                        if (strpos($vcard['fn'], '@') !== false) {
                            $vcard['fn'] = substr($vcard['fn'], 0, strpos($vcard['fn'], '@'));
                        }
                        $email = unxmlify($author->get_email());
                        if (!$profile && $author->get_link()) {
                            $profile = trim(unxmlify($author->get_link()));
                        }
                    }
                    if (!$vcard['photo']) {
                        $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/', 'thumbnail');
                        if ($rawmedia && $rawmedia[0]['attribs']['']['url']) {
                            $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
                        }
                    }
                    if (!$vcard['photo']) {
                        $rawtags = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
                        if ($rawtags) {
                            $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
                            if (x($elems, 'link') && $elems['link'][0]['attribs']['']['rel'] === 'photo') {
                                $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
                            }
                        }
                    }
                }
            }
            // Workaround for misconfigured Friendica servers
            if ($network == "" and strstr($url, "/profile/")) {
                $noscrape = str_replace("/profile/", "/noscrape/", $url);
                $noscrapejson = fetch_url($noscrape);
                if ($noscrapejson) {
                    $network = NETWORK_DFRN;
                    $poco = str_replace("/profile/", "/poco/", $url);
                    $noscrapedata = json_decode($noscrapejson, true);
                    if (isset($noscrapedata["addr"])) {
                        $addr = $noscrapedata["addr"];
                    }
                    if (isset($noscrapedata["fn"])) {
                        $vcard["fn"] = $noscrapedata["fn"];
                    }
                    if (isset($noscrapedata["key"])) {
                        $pubkey = $noscrapedata["key"];
                    }
                    if (isset($noscrapedata["photo"])) {
                        $vcard["photo"] = $noscrapedata["photo"];
                    }
                    if (isset($noscrapedata["dfrn-request"])) {
                        $request = $noscrapedata["dfrn-request"];
                    }
                    if (isset($noscrapedata["dfrn-confirm"])) {
                        $confirm = $noscrapedata["dfrn-confirm"];
                    }
                    if (isset($noscrapedata["dfrn-notify"])) {
                        $notify = $noscrapedata["dfrn-notify"];
                    }
                    if (isset($noscrapedata["dfrn-poll"])) {
                        $poll = $noscrapedata["dfrn-poll"];
                    }
                }
            }
            if (!$vcard['photo'] && strlen($email)) {
                $vcard['photo'] = avatar_img($email);
            }
            if ($poll === $profile) {
                $lnk = $feed->get_permalink();
            }
            if (isset($lnk) && strlen($lnk)) {
                $profile = $lnk;
            }
            if (!$network) {
                $network = NETWORK_FEED;
                // If it is a feed, don't take the author name as feed name
                unset($vcard['fn']);
            }
            if (!x($vcard, 'fn')) {
                $vcard['fn'] = notags($feed->get_title());
            }
            if (!x($vcard, 'fn')) {
                $vcard['fn'] = notags($feed->get_description());
            }
            if (strpos($vcard['fn'], 'Twitter / ') !== false) {
                $vcard['fn'] = substr($vcard['fn'], strpos($vcard['fn'], '/') + 1);
                $vcard['fn'] = trim($vcard['fn']);
            }
            if (!x($vcard, 'nick')) {
                $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
                if (strpos($vcard['nick'], ' ')) {
                    $vcard['nick'] = trim(substr($vcard['nick'], 0, strpos($vcard['nick'], ' ')));
                }
            }
            if (!$priority) {
                $priority = 2;
            }
        }
    }
    if (!x($vcard, 'photo')) {
        $a = get_app();
        $vcard['photo'] = $a->get_baseurl() . '/images/person-175.jpg';
    }
    if (!$profile) {
        $profile = $url;
    }
    // No human could be associated with this link, use the URL as the contact name
    if ($network === NETWORK_FEED && $poll && !x($vcard, 'fn')) {
        $vcard['fn'] = $url;
    }
    if ($notify != "" and $poll != "") {
        $baseurl = matching(normalise_link($notify), normalise_link($poll));
        $baseurl2 = matching($baseurl, normalise_link($profile));
        if ($baseurl2 != "") {
            $baseurl = $baseurl2;
        }
    }
    if ($baseurl == "" and $notify != "") {
        $baseurl = matching(normalise_link($profile), normalise_link($notify));
    }
    if ($baseurl == "" and $poll != "") {
        $baseurl = matching(normalise_link($profile), normalise_link($poll));
    }
    $baseurl = rtrim($baseurl, "/");
    if (strpos($url, '@') and $addr == "" and $network == NETWORK_DFRN) {
        $addr = str_replace('acct:', '', $url);
    }
    $vcard['fn'] = notags($vcard['fn']);
    $vcard['nick'] = str_replace(' ', '', notags($vcard['nick']));
    $result['name'] = $vcard['fn'];
    $result['nick'] = $vcard['nick'];
    $result['url'] = $profile;
    $result['addr'] = $addr;
    $result['batch'] = $batch;
    $result['notify'] = $notify;
    $result['poll'] = $poll;
    $result['request'] = $request;
    $result['confirm'] = $confirm;
    $result['poco'] = $poco;
    $result['photo'] = $vcard['photo'];
    $result['priority'] = $priority;
    $result['network'] = $network;
    $result['alias'] = $alias;
    $result['pubkey'] = $pubkey;
    $result['baseurl'] = $baseurl;
    logger('probe_url: ' . print_r($result, true), LOGGER_DEBUG);
    if ($level == 1) {
        // Trying if it maybe a diaspora account
        if ($result['network'] == NETWORK_FEED or $result['addr'] == "") {
            require_once 'include/bbcode.php';
            $address = GetProfileUsername($url, "", true);
            $result2 = probe_url($address, $mode, ++$level);
            if ($result2['network'] != "") {
                $result = $result2;
            }
        }
        // Maybe it's some non standard GNU Social installation (Single user, subfolder or no uri rewrite)
        if ($result['network'] == NETWORK_FEED and $result['baseurl'] != "" and $result['nick'] != "") {
            $addr = $result['nick'] . '@' . str_replace("http://", "", $result['baseurl']);
            $result2 = probe_url($addr, $mode, ++$level);
            if ($result2['network'] != "" and $result2['network'] != NETWORK_FEED) {
                $result = $result2;
            }
        }
    }
    // Only store into the cache if the value seems to be valid
    if ($result['network'] != NETWORK_PHANTOM) {
        Cache::set("probe_url:" . $mode . ":" . $url, serialize($result), CACHE_DAY);
    }
    return $result;
}
Example #14
0
function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0)
{
    $a = get_app();
    // Generation:
    //  0: No definition
    //  1: Profiles on this server
    //  2: Contacts of profiles on this server
    //  3: Contacts of contacts of profiles on this server
    //  4: ...
    $gcid = "";
    if ($profile_url == "") {
        return $gcid;
    }
    // Don't store the statusnet connector as network
    // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
    if ($network == NETWORK_STATUSNET) {
        $network = "";
    }
    // The global contacts should contain the original picture, not the cached one
    if ($generation != 1 and stristr(normalise_link($profile_photo), normalise_link($a->get_baseurl() . "/photo/"))) {
        $profile_photo = "";
    }
    $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1", dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET));
    if (count($r)) {
        $network = $r[0]["network"];
    }
    if ($network == "" or $network == NETWORK_OSTATUS) {
        $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1", dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET));
        if (count($r)) {
            $network = $r[0]["network"];
            $profile_url = $r[0]["url"];
        }
    }
    $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", dbesc(normalise_link($profile_url)));
    if (count($x) and $network == "" and $x[0]["network"] != NETWORK_STATUSNET) {
        $network = $x[0]["network"];
    }
    if ($network == "" or $name == "" or $profile_photo == "") {
        require_once "include/Scrape.php";
        $data = probe_url($profile_url);
        $network = $data["network"];
        $name = $data["name"];
        $profile_url = $data["url"];
        $profile_photo = $data["photo"];
    }
    if (count($x) and $x[0]["network"] == "" and $network != "") {
        q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'", dbesc($network), dbesc(normalise_link($profile_url)));
    }
    if ($name == "" or $profile_photo == "") {
        return $gcid;
    }
    if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
        return $gcid;
    }
    logger("profile-check generation: " . $generation . " Network: " . $network . " URL: " . $profile_url . " name: " . $name . " avatar: " . $profile_photo, LOGGER_DEBUG);
    if (count($x)) {
        $gcid = $x[0]['id'];
        if ($location == "" and $x[0]['location'] != "") {
            $location = $x[0]['location'];
        }
        if ($about == "" and $x[0]['about'] != "") {
            $about = $x[0]['about'];
        }
        if ($gender == "" and $x[0]['gender'] != "") {
            $gender = $x[0]['gender'];
        }
        if ($keywords == "" and $x[0]['keywords'] != "") {
            $keywords = $x[0]['keywords'];
        }
        if ($generation == 0 and $x[0]['generation'] > 0) {
            $generation = $x[0]['generation'];
        }
        if ($x[0]['name'] != $name || $x[0]['photo'] != $profile_photo || $x[0]['updated'] < $updated) {
            q("UPDATE `gcontact` SET `name` = '%s', `network` = '%s', `photo` = '%s', `connect` = '%s', `url` = '%s',\n\t\t\t\t`updated` = '%s', `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s', `generation` = %d\n\t\t\t\tWHERE (`generation` >= %d OR `generation` = 0) AND `nurl` = '%s'", dbesc($name), dbesc($network), dbesc($profile_photo), dbesc($connect_url), dbesc($profile_url), dbesc($updated), dbesc($location), dbesc($about), dbesc($keywords), dbesc($gender), intval($generation), intval($generation), dbesc(normalise_link($profile_url)));
        }
    } else {
        q("INSERT INTO `gcontact` (`name`,`network`, `url`,`nurl`,`photo`,`connect`, `updated`, `location`, `about`, `keywords`, `gender`, `generation`)\n\t\t\tVALUES ('%s', '%s', '%s', '%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', %d)", dbesc($name), dbesc($network), dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc($profile_photo), dbesc($connect_url), dbesc($updated), dbesc($location), dbesc($about), dbesc($keywords), dbesc($gender), intval($generation));
        $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", dbesc(normalise_link($profile_url)));
        if (count($x)) {
            $gcid = $x[0]['id'];
        }
    }
    if (!$gcid) {
        return $gcid;
    }
    $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1", intval($cid), intval($uid), intval($gcid), intval($zcid));
    if (!count($r)) {
        q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ", intval($cid), intval($uid), intval($gcid), intval($zcid), dbesc(datetime_convert()));
    } else {
        q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d", dbesc(datetime_convert()), intval($cid), intval($uid), intval($gcid), intval($zcid));
    }
    // For unknown reasons there are sometimes duplicates
    q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d AND\n\t\tNOT EXISTS (SELECT `gcid` FROM `glink` WHERE `gcid` = `gcontact`.`id`)", dbesc(normalise_link($profile_url)), intval($gcid));
    return $gcid;
}
Example #15
0
function follow_init(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $url = $orig_url = notags(trim($_REQUEST['url']));
    // remove ajax junk, e.g. Twitter
    $url = str_replace('/#!/', '/', $url);
    if (!allowed_url($url)) {
        notice(t('Disallowed profile URL.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    if (!$url) {
        notice(t('Connect URL missing.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $ret = probe_url($url);
    if ($ret['network'] === NETWORK_DFRN) {
        if (strlen($a->path)) {
            $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
        } else {
            $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
        }
        goaway($ret['request'] . "&addr={$myaddr}");
        // NOTREACHED
    } else {
        if (get_config('system', 'dfrn_only')) {
            notice(t('This site is not configured to allow communications with other networks.') . EOL);
            notice(t('No compatible communication protocols or feeds were discovered.') . EOL);
            goaway($_SESSION['return_url']);
        }
    }
    // do we have enough information?
    if (!(x($ret, 'name') && x($ret, 'poll') && (x($ret, 'url') || x($ret, 'addr')))) {
        notice(t('The profile address specified does not provide adequate information.') . EOL);
        if (!x($ret, 'poll')) {
            notice(t('No compatible communication protocols or feeds were discovered.') . EOL);
        }
        if (!x($ret, 'name')) {
            notice(t('An author or name was not found.') . EOL);
        }
        if (!x($ret, 'url')) {
            notice(t('No browser URL could be matched to this address.') . EOL);
        }
        if (strpos($url, '@') !== false) {
            notice('Unable to match @-style Identity Address with a known protocol or email contact');
        }
        goaway($_SESSION['return_url']);
    }
    if ($ret['network'] === NETWORK_OSTATUS && get_config('system', 'ostatus_disabled')) {
        notice(t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
        $ret['notify'] = '';
    }
    if (!$ret['notify']) {
        notice(t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
    }
    $writeable = $ret['network'] === NETWORK_OSTATUS && $ret['notify'] ? 1 : 0;
    $hidden = $ret['network'] === NETWORK_MAIL ? 1 : 0;
    if ($ret['network'] === NETWORK_MAIL) {
        $writeable = 1;
    }
    if ($ret['network'] === NETWORK_DIASPORA) {
        $writeable = 1;
    }
    // check if we already have a contact
    // the poll url is more reliable than the profile url, as we may have
    // indirect links or webfinger links
    $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1", intval(local_user()), dbesc($ret['poll']));
    if (count($r)) {
        // update contact
        if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || $network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING) {
            q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1", intval(CONTACT_IS_FRIEND), intval($r[0]['id']), intval(local_user()));
        }
    } else {
        $new_relation = $ret['network'] === NETWORK_MAIL ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING;
        if ($ret['network'] === NETWORK_DIASPORA) {
            $new_relation = CONTACT_IS_FOLLOWER;
        }
        // create contact record
        $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,\n\t\t\t`writable`, `hidden`, `blocked`, `readonly`, `pending` )\n\t\t\tVALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ", intval(local_user()), dbesc(datetime_convert()), dbesc($ret['url']), dbesc(normalise_link($ret['url'])), dbesc($ret['addr']), dbesc($ret['alias']), dbesc($ret['batch']), dbesc($ret['notify']), dbesc($ret['poll']), dbesc($ret['poco']), dbesc($ret['name']), dbesc($ret['nick']), dbesc($ret['photo']), dbesc($ret['network']), dbesc($ret['pubkey']), intval($new_relation), intval($ret['priority']), intval($writeable), intval($hidden));
    }
    $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1", dbesc($ret['url']), intval(local_user()));
    if (!count($r)) {
        notice(t('Unable to retrieve contact information.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $contact = $r[0];
    $contact_id = $r[0]['id'];
    require_once "Photo.php";
    $photos = import_profile_photo($ret['photo'], local_user(), $contact_id);
    $r = q("UPDATE `contact` SET `photo` = '%s', \n\t\t\t`thumb` = '%s',\n\t\t\t`micro` = '%s', \n\t\t\t`name-date` = '%s', \n\t\t\t`uri-date` = '%s', \n\t\t\t`avatar-date` = '%s'\n\t\t\tWHERE `id` = %d LIMIT 1\n\t\t", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($contact_id));
    // pull feed and consume it, which should subscribe to the hub.
    proc_run('php', "include/poller.php", "{$contact_id}");
    // create a follow slap
    $tpl = get_markup_template('follow_slap.tpl');
    $slap = replace_macros($tpl, array('$name' => $a->user['username'], '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'], '$photo' => $a->contact['photo'], '$thumb' => $a->contact['thumb'], '$published' => datetime_convert('UTC', 'UTC', 'now', ATOM_TIME), '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(), '$title' => '', '$type' => 'text', '$content' => t('following'), '$nick' => $a->user['nickname'], '$verb' => ACTIVITY_FOLLOW, '$ostat_follow' => ''));
    $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` \n\t\t\tWHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval(local_user()));
    if (count($r)) {
        if ($contact['network'] == NETWORK_OSTATUS && strlen($contact['notify'])) {
            require_once 'include/salmon.php';
            slapper($r[0], $contact['notify'], $slap);
        }
        if ($contact['network'] == NETWORK_DIASPORA) {
            require_once 'include/diaspora.php';
            $ret = diaspora_share($a->user, $contact);
            logger('mod_follow: diaspora_share returns: ' . $ret);
        }
    }
    if (strstr($_SESSION['return_url'], 'contacts')) {
        goaway($a->get_baseurl() . '/contacts/' . $contact_id);
    }
    goaway($_SESSION['return_url']);
    // NOTREACHED
}
Example #16
0
function get_contact($url, $uid = 0)
{
    require_once "include/Scrape.php";
    $data = array();
    $contactid = 0;
    // is it an address in the format user@server.tld?
    if (!strstr($url, "http") or strstr($url, "@")) {
        $data = probe_url($url);
        $url = $data["url"];
        if ($url == "") {
            return 0;
        }
    }
    $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d", dbesc(normalise_link($url)), intval($uid));
    if (!$contact) {
        $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d", dbesc($url), dbesc(normalise_link($url)), intval($uid));
    }
    if ($contact) {
        $contactid = $contact[0]["id"];
        // Update the contact every 7 days
        $update_photo = $contact[0]['avatar-date'] < datetime_convert('', '', 'now -7 days');
        //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
        if (!$update_photo) {
            return $contactid;
        }
    }
    if (!count($data)) {
        $data = probe_url($url);
    }
    // Does this address belongs to a valid network?
    if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
        return 0;
    }
    // tempory programming. Can be deleted after 2015-02-07
    if ($data["alias"] == "" and normalise_link($data["url"]) != normalise_link($url)) {
        $data["alias"] = normalise_link($url);
    }
    if ($contactid == 0) {
        q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,\n\t\t\t\t\t`name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,\n\t\t\t\t\t`batch`, `request`, `confirm`, `poco`,\n\t\t\t\t\t`writable`, `blocked`, `readonly`, `pending`)\n\t\t\t\t\tVALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', 1, 0, 0, 0)", intval($uid), dbesc(datetime_convert()), dbesc($data["url"]), dbesc(normalise_link($data["url"])), dbesc($data["addr"]), dbesc($data["alias"]), dbesc($data["notify"]), dbesc($data["poll"]), dbesc($data["name"]), dbesc($data["nick"]), dbesc($data["photo"]), dbesc($data["network"]), dbesc($data["pubkey"]), intval(CONTACT_IS_SHARING), intval($data["priority"]), dbesc($data["batch"]), dbesc($data["request"]), dbesc($data["confirm"]), dbesc($data["poco"]));
        $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d", dbesc(normalise_link($data["url"])), intval($uid));
        if (!$contact) {
            return 0;
        }
        $contactid = $contact[0]["id"];
    }
    require_once "Photo.php";
    $photos = import_profile_photo($data["photo"], $uid, $contactid);
    q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s',\n\t\t`addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',\n\t\t`name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s' WHERE `id` = %d", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc($data["addr"]), dbesc($data["alias"]), dbesc($data["name"]), dbesc($data["nick"]), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($contactid));
    return $contactid;
}
Example #17
0
function update_gcontact_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once 'include/pidfile.php';
    require_once 'include/Scrape.php';
    require_once "include/socgraph.php";
    load_config('config');
    load_config('system');
    $a->set_baseurl(get_config('system', 'url'));
    load_hooks();
    logger('update_gcontact: start');
    if ($argc > 1 && intval($argv[1])) {
        $contact_id = intval($argv[1]);
    }
    if (!$contact_id) {
        logger('update_gcontact: no contact');
        return;
    }
    $lockpath = get_lockpath();
    if ($lockpath != '') {
        $pidfile = new pidfile($lockpath, 'update_gcontact' . $contact_id);
        if ($pidfile->is_already_running()) {
            logger("update_gcontact: Already running for contact " . $contact_id);
            if ($pidfile->running_time() > 9 * 60) {
                $pidfile->kill();
                logger("killed stale process");
            }
            exit;
        }
    }
    $r = q("SELECT * FROM `gcontact` WHERE `id` = %d", intval($contact_id));
    if (!$r) {
        return;
    }
    if (!in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
        return;
    }
    $data = probe_url($r[0]["url"]);
    if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
        if ($r[0]["server_url"] != "") {
            poco_check_server($r[0]["server_url"], $r[0]["network"]);
        }
        q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), intval($contact_id));
        return;
    }
    if ($data["name"] == "" and $r[0]['name'] != "") {
        $data["name"] = $r[0]['name'];
    }
    if ($data["nick"] == "" and $r[0]['nick'] != "") {
        $data["nick"] = $r[0]['nick'];
    }
    if ($data["addr"] == "" and $r[0]['addr'] != "") {
        $data["addr"] = $r[0]['addr'];
    }
    if ($data["photo"] == "" and $r[0]['photo'] != "") {
        $data["photo"] = $r[0]['photo'];
    }
    q("UPDATE `gcontact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'\n\t\t\t\tWHERE `id` = %d", dbesc($data["name"]), dbesc($data["nick"]), dbesc($data["addr"]), dbesc($data["photo"]), intval($contact_id));
    q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s'\n\t\t\t\tWHERE `uid` = 0 AND `addr` = '' AND `nurl` = '%s'", dbesc($data["name"]), dbesc($data["nick"]), dbesc($data["addr"]), dbesc($data["photo"]), dbesc(normalise_link($data["url"])));
    q("UPDATE `contact` SET `addr` = '%s'\n\t\t\t\tWHERE `uid` != 0 AND `addr` = '' AND `nurl` = '%s'", dbesc($data["addr"]), dbesc(normalise_link($data["url"])));
}
Example #18
0
function follow_content(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $uid = local_user();
    $url = notags(trim($_REQUEST['url']));
    $submit = t('Submit Request');
    // There is a current issue. It seems as if you can't start following a Friendica that is following you
    // With Diaspora this works - but Friendica is special, it seems ...
    $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND ((`rel` != %d) OR (`network` = '%s')) AND\n\t\t(`nurl` = '%s' OR `alias` = '%s' OR `alias` = '%s') AND\n\t\t`network` != '%s' LIMIT 1", intval(local_user()), dbesc(CONTACT_IS_FOLLOWER), dbesc(NETWORK_DFRN), dbesc(normalise_link($url)), dbesc(normalise_link($url)), dbesc($url), dbesc(NETWORK_STATUSNET));
    if ($r) {
        notice(t('You already added this contact.') . EOL);
        $submit = "";
        //goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $ret = probe_url($url);
    if ($ret["network"] == NETWORK_DIASPORA and !get_config('system', 'diaspora_enabled')) {
        notice(t("Diaspora support isn't enabled. Contact can't be added.") . EOL);
        $submit = "";
        //goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    if ($ret["network"] == NETWORK_OSTATUS and get_config('system', 'ostatus_disabled')) {
        notice(t("OStatus support is disabled. Contact can't be added.") . EOL);
        $submit = "";
        //goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    if ($ret["network"] == NETWORK_PHANTOM) {
        notice(t("The network type couldn't be detected. Contact can't be added.") . EOL);
        $submit = "";
        //goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    if ($ret["network"] == NETWORK_MAIL) {
        $ret["url"] = $ret["addr"];
    }
    if ($ret['network'] === NETWORK_DFRN) {
        $request = $ret["request"];
        $tpl = get_markup_template('dfrn_request.tpl');
    } else {
        $request = $a->get_baseurl() . "/follow";
        $tpl = get_markup_template('auto_request.tpl');
    }
    $r = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($uid));
    if (!$r) {
        notice(t('Permission denied.') . EOL);
        goaway($_SESSION['return_url']);
        // NOTREACHED
    }
    $myaddr = $r[0]["url"];
    // Makes the connection request for friendica contacts easier
    $_SESSION["fastlane"] = $ret["url"];
    $r = q("SELECT `location`, `about`, `keywords` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($ret["url"]));
    if (!$r) {
        $r = array(array("location" => "", "about" => "", "keywords" => ""));
    }
    if ($ret['network'] === NETWORK_DIASPORA) {
        $r[0]["location"] = "";
        $r[0]["about"] = "";
    }
    $header = $ret["name"];
    if ($ret["addr"] != "") {
        $header .= " <" . $ret["addr"] . ">";
    }
    $header .= " (" . network_to_name($ret['network'], $ret['url']) . ")";
    $o = replace_macros($tpl, array('$header' => htmlentities($header), '$photo' => proxy_url($ret["photo"], false, PROXY_SIZE_SMALL), '$desc' => "", '$pls_answer' => t('Please answer the following:'), '$does_know_you' => array('knowyou', sprintf(t('Does %s know you?'), $ret["name"]), false, '', array(t('No'), t('Yes'))), '$add_note' => t('Add a personal note:'), '$page_desc' => "", '$friendica' => "", '$statusnet' => "", '$diaspora' => "", '$diasnote' => "", '$your_address' => t('Your Identity Address:'), '$invite_desc' => "", '$emailnet' => "", '$submit' => $submit, '$cancel' => t('Cancel'), '$nickname' => "", '$name' => $ret["name"], '$url' => $ret["url"], '$zrl' => zrl($ret["url"]), '$url_label' => t("Profile URL"), '$myaddr' => $myaddr, '$request' => $request, '$location' => bbcode($r[0]["location"]), '$location_label' => t("Location:"), '$about' => bbcode($r[0]["about"], false, false), '$about_label' => t("About:"), '$keywords' => $r[0]["keywords"], '$keywords_label' => t("Tags:")));
    return $o;
}
Example #19
0
function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto = '')
{
    $a = get_app();
    if (!$recipient) {
        return -1;
    }
    if (!strlen($subject)) {
        $subject = t('[no subject]');
    }
    $hash = random_string();
    $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash;
    $convid = 0;
    $reply = false;
    require_once 'include/Scrape.php';
    $me = probe_url($replyto);
    if (!$me['name']) {
        return -2;
    }
    $conv_guid = get_guid();
    $recip_handle = $recipient['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(), '://') + 3);
    $sender_nick = basename($replyto);
    $sender_host = substr($replyto, strpos($replyto, '://') + 3);
    $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
    $sender_handle = $sender_nick . '@' . $sender_host;
    $handles = $recip_handle . ';' . $sender_handle;
    $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ", intval($recipient['uid']), dbesc($conv_guid), dbesc($sender_handle), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc($subject), dbesc($handles));
    $r = q("select * from conv where guid = '%s' and uid = %d limit 1", dbesc($conv_guid), intval($recipient['uid']));
    if (count($r)) {
        $convid = $r[0]['id'];
    }
    if (!$convid) {
        logger('send message: conversation not found.');
        return -4;
    }
    $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, \n\t\t`contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)\n\t\tVALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )", intval($recipient['uid']), dbesc(get_guid()), intval($convid), dbesc($me['name']), dbesc($me['photo']), dbesc($me['url']), 0, dbesc($subject), dbesc($body), 0, 0, 0, dbesc($uri), dbesc($replyto), datetime_convert(), 1);
    return 0;
}
Example #20
0
function item_post(&$a)
{
    if (!local_user() && !remote_user() && !x($_REQUEST, 'commenter')) {
        return;
    }
    require_once 'include/security.php';
    $uid = local_user();
    if (x($_REQUEST, 'dropitems')) {
        $arr_drop = explode(',', $_REQUEST['dropitems']);
        drop_items($arr_drop);
        $json = array('success' => 1);
        echo json_encode($json);
        killme();
    }
    call_hooks('post_local_start', $_REQUEST);
    //	logger('postinput ' . file_get_contents('php://input'));
    logger('postvars ' . print_r($_REQUEST, true), LOGGER_DATA);
    $api_source = x($_REQUEST, 'api_source') && $_REQUEST['api_source'] ? true : false;
    $message_id = x($_REQUEST, 'message_id') && $api_source ? strip_tags($_REQUEST['message_id']) : '';
    $return_path = x($_REQUEST, 'return') ? $_REQUEST['return'] : '';
    $preview = x($_REQUEST, 'preview') ? intval($_REQUEST['preview']) : 0;
    // Check for doubly-submitted posts, and reject duplicates
    // Note that we have to ignore previews, otherwise nothing will post
    // after it's been previewed
    if (!$preview && x($_REQUEST['post_id_random'])) {
        if (x($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
            logger("item post: duplicate post", LOGGER_DEBUG);
            item_post_return($a->get_baseurl(), $api_source, $return_path);
        } else {
            $_SESSION['post-random'] = $_REQUEST['post_id_random'];
        }
    }
    /**
     * Is this a reply to something?
     */
    $parent = x($_REQUEST, 'parent') ? intval($_REQUEST['parent']) : 0;
    $parent_uri = x($_REQUEST, 'parent_uri') ? trim($_REQUEST['parent_uri']) : '';
    $parent_item = null;
    $parent_contact = null;
    $thr_parent = '';
    $parid = 0;
    $r = false;
    $objecttype = null;
    if ($parent || $parent_uri) {
        $objecttype = ACTIVITY_OBJ_COMMENT;
        if (!x($_REQUEST, 'type')) {
            $_REQUEST['type'] = 'net-comment';
        }
        if ($parent) {
            $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($parent));
        } elseif ($parent_uri && local_user()) {
            // This is coming from an API source, and we are logged in
            $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($parent_uri), intval(local_user()));
        }
        // if this isn't the real parent of the conversation, find it
        if ($r !== false && count($r)) {
            $parid = $r[0]['parent'];
            $parent_uri = $r[0]['uri'];
            if ($r[0]['id'] != $r[0]['parent']) {
                $r = q("SELECT * FROM `item` WHERE `id` = `parent` AND `parent` = %d LIMIT 1", intval($parid));
            }
        }
        if ($r === false || !count($r)) {
            notice(t('Unable to locate original post.') . EOL);
            if (x($_REQUEST, 'return')) {
                goaway($a->get_baseurl() . "/" . $return_path);
            }
            killme();
        }
        $parent_item = $r[0];
        $parent = $r[0]['id'];
        // multi-level threading - preserve the info but re-parent to our single level threading
        //if(($parid) && ($parid != $parent))
        $thr_parent = $parent_uri;
        if ($parent_item['contact-id'] && $uid) {
            $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($parent_item['contact-id']), intval($uid));
            if (count($r)) {
                $parent_contact = $r[0];
                // If the contact id doesn't fit with the contact, then set the contact to null
                $thrparent = q("SELECT `author-link`, `network` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($thr_parent));
                if (count($thrparent) and $thrparent[0]["network"] === NETWORK_OSTATUS and normalise_link($parent_contact["url"]) != normalise_link($thrparent[0]["author-link"])) {
                    $parent_contact = null;
                    require_once "include/Scrape.php";
                    $probed_contact = probe_url($thrparent[0]["author-link"]);
                    if ($probed_contact["network"] != NETWORK_FEED) {
                        $parent_contact = $probed_contact;
                        $parent_contact["nurl"] = normalise_link($probed_contact["url"]);
                        $parent_contact["thumb"] = $probed_contact["photo"];
                        $parent_contact["micro"] = $probed_contact["photo"];
                    }
                    logger('parent contact: ' . print_r($parent_contact, true), LOGGER_DEBUG);
                } else {
                    logger('no contact found: ' . print_r($thrparent, true), LOGGER_DEBUG);
                }
            }
        }
    }
    if ($parent) {
        logger('mod_item: item_post parent=' . $parent);
    }
    $profile_uid = x($_REQUEST, 'profile_uid') ? intval($_REQUEST['profile_uid']) : 0;
    $post_id = x($_REQUEST, 'post_id') ? intval($_REQUEST['post_id']) : 0;
    $app = x($_REQUEST, 'source') ? strip_tags($_REQUEST['source']) : '';
    $extid = x($_REQUEST, 'extid') ? strip_tags($_REQUEST['extid']) : '';
    $allow_moderated = false;
    // here is where we are going to check for permission to post a moderated comment.
    // First check that the parent exists and it is a wall item.
    if (x($_REQUEST, 'commenter') && (!$parent || !$parent_item['wall'])) {
        notice(t('Permission denied.') . EOL);
        if (x($_REQUEST, 'return')) {
            goaway($a->get_baseurl() . "/" . $return_path);
        }
        killme();
    }
    // Now check that it is a page_type of PAGE_BLOG, and that valid personal details
    // have been provided, and run any anti-spam plugins
    // TODO
    if (!can_write_wall($a, $profile_uid) && !$allow_moderated) {
        notice(t('Permission denied.') . EOL);
        if (x($_REQUEST, 'return')) {
            goaway($a->get_baseurl() . "/" . $return_path);
        }
        killme();
    }
    // is this an edited post?
    $orig_post = null;
    if ($post_id) {
        $i = q("SELECT * FROM `item` WHERE `uid` = %d AND `id` = %d LIMIT 1", intval($profile_uid), intval($post_id));
        if (!count($i)) {
            killme();
        }
        $orig_post = $i[0];
    }
    $user = null;
    $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($profile_uid));
    if (count($r)) {
        $user = $r[0];
    }
    if ($orig_post) {
        $str_group_allow = $orig_post['allow_gid'];
        $str_contact_allow = $orig_post['allow_cid'];
        $str_group_deny = $orig_post['deny_gid'];
        $str_contact_deny = $orig_post['deny_cid'];
        $location = $orig_post['location'];
        $coord = $orig_post['coord'];
        $verb = $orig_post['verb'];
        $objecttype = $orig_post['object-type'];
        $emailcc = $orig_post['emailcc'];
        $app = $orig_post['app'];
        $categories = $orig_post['file'];
        $title = notags(trim($_REQUEST['title']));
        $body = escape_tags(trim($_REQUEST['body']));
        $private = $orig_post['private'];
        $pubmail_enable = $orig_post['pubmail'];
        $network = $orig_post['network'];
        $guid = $orig_post['guid'];
        $extid = $orig_post['extid'];
    } else {
        // if coming from the API and no privacy settings are set,
        // use the user default permissions - as they won't have
        // been supplied via a form.
        if ($api_source && !array_key_exists('contact_allow', $_REQUEST) && !array_key_exists('group_allow', $_REQUEST) && !array_key_exists('contact_deny', $_REQUEST) && !array_key_exists('group_deny', $_REQUEST)) {
            $str_group_allow = $user['allow_gid'];
            $str_contact_allow = $user['allow_cid'];
            $str_group_deny = $user['deny_gid'];
            $str_contact_deny = $user['deny_cid'];
        } else {
            // use the posted permissions
            $str_group_allow = perms2str($_REQUEST['group_allow']);
            $str_contact_allow = perms2str($_REQUEST['contact_allow']);
            $str_group_deny = perms2str($_REQUEST['group_deny']);
            $str_contact_deny = perms2str($_REQUEST['contact_deny']);
        }
        $title = notags(trim($_REQUEST['title']));
        $location = notags(trim($_REQUEST['location']));
        $coord = notags(trim($_REQUEST['coord']));
        $verb = notags(trim($_REQUEST['verb']));
        $emailcc = notags(trim($_REQUEST['emailcc']));
        $body = escape_tags(trim($_REQUEST['body']));
        $network = notags(trim($_REQUEST['network']));
        $guid = get_guid(32);
        $naked_body = preg_replace('/\\[(.+?)\\]/', '', $body);
        if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
            $l = new Text_LanguageDetect();
            //$lng = $l->detectConfidence($naked_body);
            //$postopts = (($lng['language']) ? 'lang=' . $lng['language'] . ';' . $lng['confidence'] : '');
            $lng = $l->detect($naked_body, 3);
            if (sizeof($lng) > 0) {
                $postopts = "";
                foreach ($lng as $language => $score) {
                    if ($postopts == "") {
                        $postopts = "lang=";
                    } else {
                        $postopts .= ":";
                    }
                    $postopts .= $language . ";" . $score;
                }
            }
            logger('mod_item: detect language' . print_r($lng, true) . $naked_body, LOGGER_DATA);
        } else {
            $postopts = '';
        }
        $private = strlen($str_group_allow) || strlen($str_contact_allow) || strlen($str_group_deny) || strlen($str_contact_deny) ? 1 : 0;
        if ($user['hidewall']) {
            $private = 2;
        }
        // If this is a comment, set the permissions from the parent.
        if ($parent_item) {
            $private = 0;
            // for non native networks use the network of the original post as network of the item
            if ($parent_item['network'] != NETWORK_DIASPORA and $parent_item['network'] != NETWORK_OSTATUS and $network == "") {
                $network = $parent_item['network'];
            }
            if ($parent_item['private'] || strlen($parent_item['allow_cid']) || strlen($parent_item['allow_gid']) || strlen($parent_item['deny_cid']) || strlen($parent_item['deny_gid'])) {
                $private = $parent_item['private'] ? $parent_item['private'] : 1;
            }
            $str_contact_allow = $parent_item['allow_cid'];
            $str_group_allow = $parent_item['allow_gid'];
            $str_contact_deny = $parent_item['deny_cid'];
            $str_group_deny = $parent_item['deny_gid'];
        }
        $pubmail_enable = x($_REQUEST, 'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && !$private ? 1 : 0;
        // if using the API, we won't see pubmail_enable - figure out if it should be set
        if ($api_source && $profile_uid && $profile_uid == local_user() && !$private) {
            $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
            if (!$mail_disabled) {
                $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1", intval(local_user()));
                if (count($r) && intval($r[0]['pubmail'])) {
                    $pubmail_enabled = true;
                }
            }
        }
        if (!strlen($body)) {
            if ($preview) {
                killme();
            }
            info(t('Empty post discarded.') . EOL);
            if (x($_REQUEST, 'return')) {
                goaway($a->get_baseurl() . "/" . $return_path);
            }
            killme();
        }
    }
    if (strlen($categories)) {
        // get the "fileas" tags for this post
        $filedas = file_tag_file_to_list($categories, 'file');
    }
    // save old and new categories, so we can determine what needs to be deleted from pconfig
    $categories_old = $categories;
    $categories = file_tag_list_to_file(trim($_REQUEST['category']), 'category');
    $categories_new = $categories;
    if (strlen($filedas)) {
        // append the fileas stuff to the new categories list
        $categories .= file_tag_list_to_file($filedas, 'file');
    }
    // Work around doubled linefeeds in Tinymce 3.5b2
    // First figure out if it's a status post that would've been
    // created using tinymce. Otherwise leave it alone.
    /*	$plaintext = (local_user() ? intval(get_pconfig(local_user(),'system','plaintext')) || !feature_enabled($profile_uid,'richtext') : 0);
    	if((! $parent) && (! $api_source) && (! $plaintext)) {
    		$body = fix_mce_lf($body);
    	}*/
    $plaintext = local_user() ? !feature_enabled($profile_uid, 'richtext') : 0;
    if (!$parent && !$api_source && !$plaintext) {
        $body = fix_mce_lf($body);
    }
    // get contact info for poster
    $author = null;
    $self = false;
    $contact_id = 0;
    if (local_user() && local_user() == $profile_uid) {
        $self = true;
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($_SESSION['uid']));
    } elseif (remote_user()) {
        if (is_array($_SESSION['remote'])) {
            foreach ($_SESSION['remote'] as $v) {
                if ($v['uid'] == $profile_uid) {
                    $contact_id = $v['cid'];
                    break;
                }
            }
        }
        if ($contact_id) {
            $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($contact_id));
        }
    }
    if (count($r)) {
        $author = $r[0];
        $contact_id = $author['id'];
    }
    // get contact info for owner
    if ($profile_uid == local_user()) {
        $contact_record = $author;
    } else {
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($profile_uid));
        if (count($r)) {
            $contact_record = $r[0];
        }
    }
    $post_type = notags(trim($_REQUEST['type']));
    if ($post_type === 'net-comment') {
        if ($parent_item !== null) {
            if ($parent_item['wall'] == 1) {
                $post_type = 'wall-comment';
            } else {
                $post_type = 'remote-comment';
            }
        }
    }
    /**
     *
     * When a photo was uploaded into the message using the (profile wall) ajax
     * uploader, The permissions are initially set to disallow anybody but the
     * owner from seeing it. This is because the permissions may not yet have been
     * set for the post. If it's private, the photo permissions should be set
     * appropriately. But we didn't know the final permissions on the post until
     * now. So now we'll look for links of uploaded messages that are in the
     * post and set them to the same permissions as the post itself.
     *
     */
    $match = null;
    if (!$preview && preg_match_all("/\\[img([\\=0-9x]*?)\\](.*?)\\[\\/img\\]/", $body, $match)) {
        $images = $match[2];
        if (count($images)) {
            $objecttype = ACTIVITY_OBJ_IMAGE;
            foreach ($images as $image) {
                if (!stristr($image, $a->get_baseurl() . '/photo/')) {
                    continue;
                }
                $image_uri = substr($image, strrpos($image, '/') + 1);
                $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
                if (!strlen($image_uri)) {
                    continue;
                }
                $srch = '<' . intval($contact_id) . '>';
                $r = q("SELECT `id` FROM `photo` WHERE `allow_cid` = '%s' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = ''\n\t\t\t\t\tAND `resource-id` = '%s' AND `uid` = %d LIMIT 1", dbesc($srch), dbesc($image_uri), intval($profile_uid));
                if (!count($r)) {
                    continue;
                }
                $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'\n\t\t\t\t\tWHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ", dbesc($str_contact_allow), dbesc($str_group_allow), dbesc($str_contact_deny), dbesc($str_group_deny), dbesc($image_uri), intval($profile_uid), dbesc(t('Wall Photos')));
            }
        }
    }
    /**
     * Next link in any attachment references we find in the post.
     */
    $match = false;
    if (!$preview && preg_match_all("/\\[attachment\\](.*?)\\[\\/attachment\\]/", $body, $match)) {
        $attaches = $match[1];
        if (count($attaches)) {
            foreach ($attaches as $attach) {
                $r = q("SELECT * FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1", intval($profile_uid), intval($attach));
                if (count($r)) {
                    $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s'\n\t\t\t\t\t\tWHERE `uid` = %d AND `id` = %d", dbesc($str_contact_allow), dbesc($str_group_allow), dbesc($str_contact_deny), dbesc($str_group_deny), intval($profile_uid), intval($attach));
                }
            }
        }
    }
    // embedded bookmark in post? set bookmark flag
    $bookmark = 0;
    if (preg_match_all("/\\[bookmark\\=([^\\]]*)\\](.*?)\\[\\/bookmark\\]/ism", $body, $match, PREG_SET_ORDER)) {
        $objecttype = ACTIVITY_OBJ_BOOKMARK;
        $bookmark = 1;
    }
    $body = bb_translate_video($body);
    /**
     * Fold multi-line [code] sequences
     */
    $body = preg_replace('/\\[\\/code\\]\\s*\\[code\\]/ism', "\n", $body);
    $body = scale_external_images($body, false);
    // Setting the object type if not defined before
    if (!$objecttype) {
        $objecttype = ACTIVITY_OBJ_NOTE;
        // Default value
        require_once "include/plaintext.php";
        $objectdata = get_attached_data($body);
        if ($post["type"] == "link") {
            $objecttype = ACTIVITY_OBJ_BOOKMARK;
        } elseif ($post["type"] == "video") {
            $objecttype = ACTIVITY_OBJ_VIDEO;
        } elseif ($post["type"] == "photo") {
            $objecttype = ACTIVITY_OBJ_IMAGE;
        }
    }
    /**
     * Look for any tags and linkify them
     */
    $str_tags = '';
    $inform = '';
    $tags = get_tags($body);
    /**
     * add a statusnet style reply tag if the original post was from there
     * and we are replying, and there isn't one already
     */
    if ($parent_contact && $parent_contact['network'] === NETWORK_OSTATUS && $parent_contact['nick'] && !in_array('@' . $parent_contact['nick'], $tags)) {
        $body = '@' . $parent_contact['nick'] . ' ' . $body;
        $tags[] = '@' . $parent_contact['nick'];
    }
    $tagged = array();
    $private_forum = false;
    if (count($tags)) {
        foreach ($tags as $tag) {
            if (strpos($tag, '#') === 0) {
                continue;
            }
            // If we already tagged 'Robert Johnson', don't try and tag 'Robert'.
            // Robert Johnson should be first in the $tags array
            $fullnametagged = false;
            for ($x = 0; $x < count($tagged); $x++) {
                if (stristr($tagged[$x], $tag . ' ')) {
                    $fullnametagged = true;
                    break;
                }
            }
            if ($fullnametagged) {
                continue;
            }
            $success = handle_tag($a, $body, $inform, $str_tags, local_user() ? local_user() : $profile_uid, $tag, $network);
            if ($success['replaced']) {
                $tagged[] = $tag;
            }
            if (is_array($success['contact']) && intval($success['contact']['prv'])) {
                $private_forum = true;
                $private_id = $success['contact']['id'];
            }
        }
    }
    if ($private_forum && !$parent && !$private) {
        // we tagged a private forum in a top level post and the message was public.
        // Restrict it.
        $private = 1;
        $str_contact_allow = '<' . $private_id . '>';
    }
    $attachments = '';
    $match = false;
    if (preg_match_all('/(\\[attachment\\]([0-9]+)\\[\\/attachment\\])/', $body, $match)) {
        foreach ($match[2] as $mtch) {
            $r = q("SELECT `id`,`filename`,`filesize`,`filetype` FROM `attach` WHERE `uid` = %d AND `id` = %d LIMIT 1", intval($profile_uid), intval($mtch));
            if (count($r)) {
                if (strlen($attachments)) {
                    $attachments .= ',';
                }
                $attachments .= '[attach]href="' . $a->get_baseurl() . '/attach/' . $r[0]['id'] . '" length="' . $r[0]['filesize'] . '" type="' . $r[0]['filetype'] . '" title="' . ($r[0]['filename'] ? $r[0]['filename'] : '') . '"[/attach]';
            }
            $body = str_replace($match[1], '', $body);
        }
    }
    $wall = 0;
    if ($post_type === 'wall' || $post_type === 'wall-comment') {
        $wall = 1;
    }
    if (!strlen($verb)) {
        $verb = ACTIVITY_POST;
    }
    if ($network == "") {
        $network = NETWORK_DFRN;
    }
    $gravity = $parent ? 6 : 0;
    // even if the post arrived via API we are considering that it
    // originated on this site by default for determining relayability.
    $origin = x($_REQUEST, 'origin') ? intval($_REQUEST['origin']) : 1;
    $notify_type = $parent ? 'comment-new' : 'wall-new';
    $uri = $message_id ? $message_id : item_new_uri($a->get_hostname(), $profile_uid);
    // Fallback so that we alway have a thr-parent
    if (!$thr_parent) {
        $thr_parent = $uri;
    }
    $datarray = array();
    $datarray['uid'] = $profile_uid;
    $datarray['type'] = $post_type;
    $datarray['wall'] = $wall;
    $datarray['gravity'] = $gravity;
    $datarray['network'] = $network;
    $datarray['contact-id'] = $contact_id;
    $datarray['owner-name'] = $contact_record['name'];
    $datarray['owner-link'] = $contact_record['url'];
    $datarray['owner-avatar'] = $contact_record['thumb'];
    $datarray['author-name'] = $author['name'];
    $datarray['author-link'] = $author['url'];
    $datarray['author-avatar'] = $author['thumb'];
    $datarray['created'] = datetime_convert();
    $datarray['edited'] = datetime_convert();
    $datarray['commented'] = datetime_convert();
    $datarray['received'] = datetime_convert();
    $datarray['changed'] = datetime_convert();
    $datarray['extid'] = $extid;
    $datarray['guid'] = $guid;
    $datarray['uri'] = $uri;
    $datarray['title'] = $title;
    $datarray['body'] = $body;
    $datarray['app'] = $app;
    $datarray['location'] = $location;
    $datarray['coord'] = $coord;
    $datarray['tag'] = $str_tags;
    $datarray['file'] = $categories;
    $datarray['inform'] = $inform;
    $datarray['verb'] = $verb;
    $datarray['object-type'] = $objecttype;
    $datarray['allow_cid'] = $str_contact_allow;
    $datarray['allow_gid'] = $str_group_allow;
    $datarray['deny_cid'] = $str_contact_deny;
    $datarray['deny_gid'] = $str_group_deny;
    $datarray['private'] = $private;
    $datarray['pubmail'] = $pubmail_enable;
    $datarray['attach'] = $attachments;
    $datarray['bookmark'] = intval($bookmark);
    $datarray['thr-parent'] = $thr_parent;
    $datarray['postopts'] = $postopts;
    $datarray['origin'] = $origin;
    $datarray['moderated'] = $allow_moderated;
    /**
     * These fields are for the convenience of plugins...
     * 'self' if true indicates the owner is posting on their own wall
     * If parent is 0 it is a top-level post.
     */
    $datarray['parent'] = $parent;
    $datarray['self'] = $self;
    //	$datarray['prvnets']       = $user['prvnets'];
    if ($orig_post) {
        $datarray['edit'] = true;
    }
    // Search for hashtags
    item_body_set_hashtags($datarray);
    // preview mode - prepare the body for display and send it via json
    if ($preview) {
        require_once 'include/conversation.php';
        $o = conversation($a, array(array_merge($contact_record, $datarray)), 'search', false, true);
        logger('preview: ' . $o);
        echo json_encode(array('preview' => $o));
        killme();
    }
    call_hooks('post_local', $datarray);
    if (x($datarray, 'cancel')) {
        logger('mod_item: post cancelled by plugin.');
        if ($return_path) {
            goaway($a->get_baseurl() . "/" . $return_path);
        }
        $json = array('cancel' => 1);
        if (x($_REQUEST, 'jsreload') && strlen($_REQUEST['jsreload'])) {
            $json['reload'] = $a->get_baseurl() . '/' . $_REQUEST['jsreload'];
        }
        echo json_encode($json);
        killme();
    }
    // Fill the cache field
    put_item_in_cache($datarray);
    if ($orig_post) {
        $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `attach` = '%s', `file` = '%s', `rendered-html` = '%s', `rendered-hash` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d", dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['tag']), dbesc($datarray['attach']), dbesc($datarray['file']), dbesc($datarray['rendered-html']), dbesc($datarray['rendered-hash']), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($post_id), intval($profile_uid));
        create_tags_from_item($post_id);
        create_files_from_item($post_id);
        update_thread($post_id);
        // update filetags in pconfig
        file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category');
        proc_run('php', "include/notifier.php", 'edit_post', "{$post_id}");
        if (x($_REQUEST, 'return') && strlen($return_path)) {
            logger('return: ' . $return_path);
            goaway($a->get_baseurl() . "/" . $return_path);
        }
        killme();
    } else {
        $post_id = 0;
    }
    $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `author-name`, `author-link`, `author-avatar`,\n\t\t`created`, `edited`, `commented`, `received`, `changed`, `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`, `tag`, `inform`, `verb`, `object-type`, `postopts`,\n\t\t`allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`, `rendered-html`, `rendered-hash`)\n\t\tVALUES( '%s', '%s', %d, '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, '%s', '%s', '%s')", dbesc($datarray['guid']), dbesc($datarray['extid']), intval($datarray['uid']), dbesc($datarray['type']), intval($datarray['wall']), intval($datarray['gravity']), dbesc($datarray['network']), intval($datarray['contact-id']), dbesc($datarray['owner-name']), dbesc($datarray['owner-link']), dbesc($datarray['owner-avatar']), dbesc($datarray['author-name']), dbesc($datarray['author-link']), dbesc($datarray['author-avatar']), dbesc($datarray['created']), dbesc($datarray['edited']), dbesc($datarray['commented']), dbesc($datarray['received']), dbesc($datarray['changed']), dbesc($datarray['uri']), dbesc($datarray['thr-parent']), dbesc($datarray['title']), dbesc($datarray['body']), dbesc($datarray['app']), dbesc($datarray['location']), dbesc($datarray['coord']), dbesc($datarray['tag']), dbesc($datarray['inform']), dbesc($datarray['verb']), dbesc($datarray['object-type']), dbesc($datarray['postopts']), dbesc($datarray['allow_cid']), dbesc($datarray['allow_gid']), dbesc($datarray['deny_cid']), dbesc($datarray['deny_gid']), intval($datarray['private']), intval($datarray['pubmail']), dbesc($datarray['attach']), intval($datarray['bookmark']), intval($datarray['origin']), intval($datarray['moderated']), dbesc($datarray['file']), dbesc($datarray['rendered-html']), dbesc($datarray['rendered-hash']));
    $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($datarray['uri']));
    if (!count($r)) {
        logger('mod_item: unable to retrieve post that was just stored.');
        notice(t('System error. Post not saved.') . EOL);
        goaway($a->get_baseurl() . "/" . $return_path);
        // NOTREACHED
    }
    $post_id = $r[0]['id'];
    logger('mod_item: saved item ' . $post_id);
    $datarray["id"] = $post_id;
    $datarray["plink"] = $a->get_baseurl() . '/display/' . urlencode($datarray["guid"]);
    // update filetags in pconfig
    file_tag_update_pconfig($uid, $categories_old, $categories_new, 'category');
    if ($parent) {
        // This item is the last leaf and gets the comment box, clear any ancestors
        $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ", dbesc(datetime_convert()), intval($parent));
        update_thread($parent, true);
        // Inherit ACLs from the parent item.
        $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d\n\t\t\tWHERE `id` = %d", dbesc($parent_item['allow_cid']), dbesc($parent_item['allow_gid']), dbesc($parent_item['deny_cid']), dbesc($parent_item['deny_gid']), intval($parent_item['private']), intval($post_id));
        if ($contact_record != $author) {
            notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $user['notify-flags'], 'language' => $user['language'], 'to_name' => $user['username'], 'to_email' => $user['email'], 'uid' => $user['uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . urlencode($datarray['guid']), 'source_name' => $datarray['author-name'], 'source_link' => $datarray['author-link'], 'source_photo' => $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $parent, 'parent_uri' => $parent_item['uri']));
        }
        // Store the comment signature information in case we need to relay to Diaspora
        store_diaspora_comment_sig($datarray, $author, $self ? $a->user['prvkey'] : false, $parent_item, $post_id);
    } else {
        $parent = $post_id;
        if ($contact_record != $author) {
            notification(array('type' => NOTIFY_WALL, 'notify_flags' => $user['notify-flags'], 'language' => $user['language'], 'to_name' => $user['username'], 'to_email' => $user['email'], 'uid' => $user['uid'], 'item' => $datarray, 'link' => $a->get_baseurl() . '/display/' . urlencode($datarray['guid']), 'source_name' => $datarray['author-name'], 'source_link' => $datarray['author-link'], 'source_photo' => $datarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item'));
        }
    }
    // fallback so that parent always gets set to non-zero.
    if (!$parent) {
        $parent = $post_id;
    }
    $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1\n\t\tWHERE `id` = %d", intval($parent), dbesc($parent == $post_id ? $uri : $parent_item['uri']), dbesc($a->get_baseurl() . '/display/' . urlencode($datarray['guid'])), dbesc(datetime_convert()), intval($post_id));
    // photo comments turn the corresponding item visible to the profile wall
    // This way we don't see every picture in your new photo album posted to your wall at once.
    // They will show up as people comment on them.
    if (!$parent_item['visible']) {
        $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d", intval($parent_item['id']));
        update_thread($parent_item['id']);
    }
    // update the commented timestamp on the parent
    q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d", dbesc(datetime_convert()), dbesc(datetime_convert()), intval($parent));
    if ($post_id != $parent) {
        update_thread($parent);
    }
    call_hooks('post_local_end', $datarray);
    if (strlen($emailcc) && $profile_uid == local_user()) {
        $erecips = explode(',', $emailcc);
        if (count($erecips)) {
            foreach ($erecips as $recip) {
                $addr = trim($recip);
                if (!strlen($addr)) {
                    continue;
                }
                $disclaimer = '<hr />' . sprintf(t('This message was sent to you by %s, a member of the Friendica social network.'), $a->user['username']) . '<br />';
                $disclaimer .= sprintf(t('You may visit them online at %s'), $a->get_baseurl() . '/profile/' . $a->user['nickname']) . EOL;
                $disclaimer .= t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
                if (!$datarray['title'] == '') {
                    $subject = email_header_encode($datarray['title'], 'UTF-8');
                } else {
                    $subject = email_header_encode('[Friendica]' . ' ' . sprintf(t('%s posted an update.'), $a->user['username']), 'UTF-8');
                }
                $link = '<a href="' . $a->get_baseurl() . '/profile/' . $a->user['nickname'] . '"><img src="' . $author['thumb'] . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
                $html = prepare_body($datarray);
                $message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';
                include_once 'include/html2plain.php';
                $params = array('fromName' => $a->user['username'], 'fromEmail' => $a->user['email'], 'toEmail' => $addr, 'replyTo' => $a->user['email'], 'messageSubject' => $subject, 'htmlVersion' => $message, 'textVersion' => html2plain($html . $disclaimer));
                Emailer::send($params);
            }
        }
    }
    create_tags_from_item($post_id);
    create_files_from_item($post_id);
    if ($post_id == $parent) {
        add_thread($post_id);
    }
    // This is a real juggling act on shared hosting services which kill your processes
    // e.g. dreamhost. We used to start delivery to our native delivery agents in the background
    // and then run our plugin delivery from the foreground. We're now doing plugin delivery first,
    // because as soon as you start loading up a bunch of remote delivey processes, *this* page is
    // likely to get killed off. If you end up looking at an /item URL and a blank page,
    // it's very likely the delivery got killed before all your friends could be notified.
    // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting,
    // or cut back on plugins which do remote deliveries.
    proc_run('php', "include/notifier.php", $notify_type, "{$post_id}");
    logger('post_complete');
    item_post_return($a->get_baseurl(), $api_source, $return_path);
    // NOTREACHED
}
Example #21
0
function notifier_run(&$argv, &$argc)
{
    global $a, $db;
    if (is_null($a)) {
        $a = new App();
    }
    if (is_null($db)) {
        @(include ".htconfig.php");
        require_once "include/dba.php";
        $db = new dba($db_host, $db_user, $db_pass, $db_data);
        unset($db_host, $db_user, $db_pass, $db_data);
    }
    require_once "include/session.php";
    require_once "include/datetime.php";
    require_once 'include/items.php';
    require_once 'include/bbcode.php';
    require_once 'include/email.php';
    load_config('config');
    load_config('system');
    load_hooks();
    if ($argc < 3) {
        return;
    }
    $a->set_baseurl(get_config('system', 'url'));
    logger('notifier: invoked: ' . print_r($argv, true), LOGGER_DEBUG);
    $cmd = $argv[1];
    switch ($cmd) {
        case 'mail':
        default:
            $item_id = intval($argv[2]);
            if (!$item_id) {
                return;
            }
            break;
    }
    $expire = false;
    $mail = false;
    $fsuggest = false;
    $relocate = false;
    $top_level = false;
    $recipients = array();
    $url_recipients = array();
    $normal_mode = true;
    if ($cmd === 'mail') {
        $normal_mode = false;
        $mail = true;
        $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1", intval($item_id));
        if (!count($message)) {
            return;
        }
        $uid = $message[0]['uid'];
        $recipients[] = $message[0]['contact-id'];
        $item = $message[0];
    } elseif ($cmd === 'expire') {
        $normal_mode = false;
        $expire = true;
        $items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1\n\t\t\tAND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE", intval($item_id));
        $uid = $item_id;
        $item_id = 0;
        if (!count($items)) {
            return;
        }
    } elseif ($cmd === 'suggest') {
        $normal_mode = false;
        $fsuggest = true;
        $suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item_id));
        if (!count($suggest)) {
            return;
        }
        $uid = $suggest[0]['uid'];
        $recipients[] = $suggest[0]['cid'];
        $item = $suggest[0];
    } elseif ($cmd === 'removeme') {
        $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($item_id));
        if (!$r) {
            return;
        }
        $user = $r[0];
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($item_id));
        if (!$r) {
            return;
        }
        $self = $r[0];
        $r = q("SELECT * FROM `contact` WHERE `self` = 0 AND `uid` = %d", intval($item_id));
        if (!$r) {
            return;
        }
        require_once 'include/Contact.php';
        foreach ($r as $contact) {
            terminate_friendship($user, $self, $contact);
        }
        return;
    } elseif ($cmd === 'relocate') {
        $normal_mode = false;
        $relocate = true;
        $uid = $item_id;
    } else {
        // find ancestors
        $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1", intval($item_id));
        if (!count($r) || !intval($r[0]['parent'])) {
            return;
        }
        $target_item = $r[0];
        $parent_id = intval($r[0]['parent']);
        $uid = $r[0]['uid'];
        $updated = $r[0]['edited'];
        // POSSIBLE CLEANUP --> The following seems superfluous. We've already checked for "if (! intval($r[0]['parent']))" a few lines up
        if (!$parent_id) {
            return;
        }
        $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`\n\t\t\tFROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d and visible = 1 and moderated = 0 ORDER BY `id` ASC", intval($parent_id));
        if (!count($items)) {
            return;
        }
        // avoid race condition with deleting entries
        if ($items[0]['deleted']) {
            foreach ($items as $item) {
                $item['deleted'] = 1;
            }
        }
        if (count($items) == 1 && $items[0]['id'] === $target_item['id'] && $items[0]['uri'] === $items[0]['parent-uri']) {
            logger('notifier: top level post');
            $top_level = true;
        }
    }
    $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,\n\t\t`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,\n\t\t`user`.`page-flags`, `user`.`prvnets`\n\t\tFROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`\n\t\tWHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval($uid));
    if (!count($r)) {
        return;
    }
    $owner = $r[0];
    $walltowall = $top_level && $owner['id'] != $items[0]['contact-id'] ? true : false;
    $hub = get_config('system', 'huburl');
    // If this is a public conversation, notify the feed hub
    $public_message = true;
    // Do a PuSH
    $push_notify = false;
    // fill this in with a single salmon slap if applicable
    $slap = '';
    if (!($mail || $fsuggest || $relocate)) {
        require_once 'include/group.php';
        $parent = $items[0];
        $thr_parent = q("SELECT `network` FROM `item` WHERE `uri` = '%s' AND `uid` = %d", dbesc($target_item["thr-parent"]), intval($target_item["uid"]));
        logger('Parent is ' . $parent['network'] . '. Thread parent is ' . $thr_parent[0]['network'], LOGGER_DEBUG);
        // This is IMPORTANT!!!!
        // We will only send a "notify owner to relay" or followup message if the referenced post
        // originated on our system by virtue of having our hostname somewhere
        // in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
        // if $parent['wall'] == 1 we will already have the parent message in our array
        // and we will relay the whole lot.
        // expire sends an entire group of expire messages and cannot be forwarded.
        // However the conversation owner will be a part of the conversation and will
        // be notified during this run.
        // Other DFRN conversation members will be alerted during polled updates.
        // Diaspora members currently are not notified of expirations, and other networks have
        // either limited or no ability to process deletions. We should at least fix Diaspora
        // by stringing togther an array of retractions and sending them onward.
        $localhost = str_replace('www.', '', $a->get_hostname());
        if (strpos($localhost, ':')) {
            $localhost = substr($localhost, 0, strpos($localhost, ':'));
        }
        /**
         *
         * Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
         * have been known to cause runaway conditions which affected several servers, along with
         * permissions issues.
         *
         */
        $relay_to_owner = false;
        if (!$top_level && $parent['wall'] == 0 && !$expire && stristr($target_item['uri'], $localhost)) {
            $relay_to_owner = true;
        }
        if ($cmd === 'uplink' && intval($parent['forum_mode']) == 1 && !$top_level) {
            $relay_to_owner = true;
        }
        // until the 'origin' flag has been in use for several months
        // we will just use it as a fallback test
        // later we will be able to use it as the primary test of whether or not to relay.
        if (!$target_item['origin']) {
            $relay_to_owner = false;
        }
        if ($parent['origin']) {
            $relay_to_owner = false;
        }
        if ($relay_to_owner) {
            logger('notifier: followup ' . $target_item["guid"], LOGGER_DEBUG);
            // local followup to remote post
            $followup = true;
            $public_message = false;
            // not public
            $conversant_str = dbesc($parent['contact-id']);
            $recipients = array($parent['contact-id']);
            if (!$target_item['private'] and $target_item['wall'] and strlen($target_item['allow_cid'] . $target_item['allow_gid'] . $target_item['deny_cid'] . $target_item['deny_gid']) == 0) {
                $push_notify = true;
            }
            // We notify Friendica users in the thread when it is an OStatus thread.
            // Hopefully this transfers the messages to the other Friendica servers. (Untested)
            if ($thr_parent and $thr_parent[0]['network'] == NETWORK_OSTATUS or $parent['network'] == NETWORK_OSTATUS) {
                $push_notify = true;
                if ($parent["network"] == NETWORK_OSTATUS) {
                    $r = q("SELECT `author-link` FROM `item` WHERE `parent` = %d AND `author-link` != '%s'", intval($target_item["parent"]), dbesc($owner['url']));
                    foreach ($r as $parent_item) {
                        $probed_contact = probe_url($parent_item["author-link"]);
                        if ($probed_contact["notify"] != "" and $probed_contact["network"] == NETWORK_DFRN) {
                            logger('Notify Friendica user ' . $probed_contact["url"] . ': ' . $probed_contact["notify"]);
                            $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
                        }
                    }
                }
                if (count($url_recipients)) {
                    logger("url_recipients " . print_r($url_recipients, true));
                }
            }
        } else {
            $followup = false;
            logger('Distributing directly ' . $target_item["guid"], LOGGER_DEBUG);
            // don't send deletions onward for other people's stuff
            if ($target_item['deleted'] && !intval($target_item['wall'])) {
                logger('notifier: ignoring delete notification for non-wall item');
                return;
            }
            if (strlen($parent['allow_cid']) || strlen($parent['allow_gid']) || strlen($parent['deny_cid']) || strlen($parent['deny_gid'])) {
                $public_message = false;
                // private recipients, not public
            }
            $allow_people = expand_acl($parent['allow_cid']);
            $allow_groups = expand_groups(expand_acl($parent['allow_gid']), true);
            $deny_people = expand_acl($parent['deny_cid']);
            $deny_groups = expand_groups(expand_acl($parent['deny_gid']));
            // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
            // a delivery fork. private groups (forum_mode == 2) do not uplink
            if (intval($parent['forum_mode']) == 1 && !$top_level && $cmd !== 'uplink') {
                proc_run('php', 'include/notifier.php', 'uplink', $item_id);
            }
            $conversants = array();
            foreach ($items as $item) {
                $recipients[] = $item['contact-id'];
                $conversants[] = $item['contact-id'];
                // pull out additional tagged people to notify (if public message)
                if ($public_message && strlen($item['inform'])) {
                    $people = explode(',', $item['inform']);
                    foreach ($people as $person) {
                        if (substr($person, 0, 4) === 'cid:') {
                            $recipients[] = intval(substr($person, 4));
                            $conversants[] = intval(substr($person, 4));
                        } else {
                            $url_recipients[] = substr($person, 4);
                        }
                    }
                }
            }
            if (count($url_recipients)) {
                logger('notifier: ' . $target_item["guid"] . ' url_recipients ' . print_r($url_recipients, true));
            }
            $conversants = array_unique($conversants);
            $recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups));
            $deny = array_unique(array_merge($deny_people, $deny_groups));
            $recipients = array_diff($recipients, $deny);
            $conversant_str = dbesc(implode(', ', $conversants));
        }
        // If the thread parent is OStatus then do some magic to distribute the messages.
        // We have not only to look at the parent, since it could be a Friendica thread.
        if ($thr_parent and $thr_parent[0]['network'] == NETWORK_OSTATUS or $parent['network'] == NETWORK_OSTATUS) {
            logger('Some parent is OStatus for ' . $target_item["guid"], LOGGER_DEBUG);
            // Send a salmon notification to every person we mentioned in the post
            $arr = explode(',', $target_item['tag']);
            foreach ($arr as $x) {
                //logger('Checking tag '.$x, LOGGER_DEBUG);
                $matches = null;
                if (preg_match('/@\\[url=([^\\]]*)\\]/', $x, $matches)) {
                    $probed_contact = probe_url($matches[1]);
                    if ($probed_contact["notify"] != "") {
                        logger('Notify mentioned user ' . $probed_contact["url"] . ': ' . $probed_contact["notify"]);
                        $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
                    }
                }
            }
            // It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
            $sql_extra = " AND `network` IN ('" . NETWORK_OSTATUS . "', '" . NETWORK_DFRN . "')";
        } else {
            $sql_extra = "";
        }
        $r = q("SELECT * FROM `contact` WHERE `id` IN ({$conversant_str}) AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0" . $sql_extra);
        if (count($r)) {
            $contacts = $r;
        }
    }
    $feed_template = get_markup_template('atom_feed.tpl');
    $mail_template = get_markup_template('atom_mail.tpl');
    $atom = '';
    $slaps = array();
    $hubxml = feed_hublinks();
    $birthday = feed_birthday($owner['uid'], $owner['timezone']);
    if (strlen($birthday)) {
        $birthday = '<dfrn:birthday>' . xmlify($birthday) . '</dfrn:birthday>';
    }
    $atom .= replace_macros($feed_template, array('$version' => xmlify(FRIENDICA_VERSION), '$feed_id' => xmlify($a->get_baseurl() . '/profile/' . $owner['nickname']), '$feed_title' => xmlify($owner['name']), '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00', ATOM_TIME)), '$hub' => $hubxml, '$salmon' => '', '$name' => xmlify($owner['name']), '$profile_page' => xmlify($owner['url']), '$photo' => xmlify($owner['photo']), '$thumb' => xmlify($owner['thumb']), '$picdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['avatar-date'] . '+00:00', ATOM_TIME)), '$uridate' => xmlify(datetime_convert('UTC', 'UTC', $owner['uri-date'] . '+00:00', ATOM_TIME)), '$namdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['name-date'] . '+00:00', ATOM_TIME)), '$birthday' => $birthday, '$community' => $owner['page-flags'] == PAGE_COMMUNITY ? '<dfrn:community>1</dfrn:community>' : ''));
    if ($mail) {
        $public_message = false;
        // mail is  not public
        $body = fix_private_photos($item['body'], $owner['uid'], null, $message[0]['contact-id']);
        $atom .= replace_macros($mail_template, array('$name' => xmlify($owner['name']), '$profile_page' => xmlify($owner['url']), '$thumb' => xmlify($owner['thumb']), '$item_id' => xmlify($item['uri']), '$subject' => xmlify($item['title']), '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', ATOM_TIME)), '$content' => xmlify($body), '$parent_id' => xmlify($item['parent-uri'])));
    } elseif ($fsuggest) {
        $public_message = false;
        // suggestions are not public
        $sugg_template = get_markup_template('atom_suggest.tpl');
        $atom .= replace_macros($sugg_template, array('$name' => xmlify($item['name']), '$url' => xmlify($item['url']), '$photo' => xmlify($item['photo']), '$request' => xmlify($item['request']), '$note' => xmlify($item['note'])));
        // We don't need this any more
        q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
    } elseif ($relocate) {
        $public_message = false;
        // suggestions are not public
        $sugg_template = get_markup_template('atom_relocate.tpl');
        /* get site pubkey. this could be a new installation with no site keys*/
        $pubkey = get_config('system', 'site_pubkey');
        if (!$pubkey) {
            $res = new_keypair(1024);
            set_config('system', 'site_prvkey', $res['prvkey']);
            set_config('system', 'site_pubkey', $res['pubkey']);
        }
        $rp = q("SELECT `resource-id` , `scale`, type FROM `photo` \n\t\t\t\t\t\tWHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
        $photos = array();
        $ext = Photo::supportedTypes();
        foreach ($rp as $p) {
            $photos[$p['scale']] = $a->get_baseurl() . '/photo/' . $p['resource-id'] . '-' . $p['scale'] . '.' . $ext[$p['type']];
        }
        unset($rp, $ext);
        $atom .= replace_macros($sugg_template, array('$name' => xmlify($owner['name']), '$photo' => xmlify($photos[4]), '$thumb' => xmlify($photos[5]), '$micro' => xmlify($photos[6]), '$url' => xmlify($owner['url']), '$request' => xmlify($owner['request']), '$confirm' => xmlify($owner['confirm']), '$notify' => xmlify($owner['notify']), '$poll' => xmlify($owner['poll']), '$sitepubkey' => xmlify(get_config('system', 'site_pubkey'))));
        $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d  AND self = 0 AND network = '%s'", intval($uid), NETWORK_DFRN);
        unset($photos);
    } else {
        $slap = ostatus_salmon($target_item, $owner);
        //$slap = atom_entry($target_item,'html',null,$owner,false);
        if ($followup) {
            foreach ($items as $item) {
                // there is only one item
                if (!$item['parent']) {
                    continue;
                }
                if ($item['id'] == $item_id) {
                    logger('notifier: followup: item: ' . print_r($item, true), LOGGER_DATA);
                    //$slap  = atom_entry($item,'html',null,$owner,false);
                    $atom .= atom_entry($item, 'text', null, $owner, false);
                }
            }
        } else {
            foreach ($items as $item) {
                if (!$item['parent']) {
                    continue;
                }
                // private emails may be in included in public conversations. Filter them.
                if ($public_message && $item['private'] == 1) {
                    continue;
                }
                $contact = get_item_contact($item, $contacts);
                if (!$contact) {
                    continue;
                }
                if ($normal_mode) {
                    // we only need the current item, but include the parent because without it
                    // older sites without a corresponding dfrn_notify change may do the wrong thing.
                    if ($item_id == $item['id'] || $item['id'] == $item['parent']) {
                        $atom .= atom_entry($item, 'text', null, $owner, true);
                    }
                } else {
                    $atom .= atom_entry($item, 'text', null, $owner, true);
                }
                if ($top_level && $public_message && $item['author-link'] === $item['owner-link'] && !$expire) {
                    $slaps[] = ostatus_salmon($item, $owner);
                }
                //$slaps[] = atom_entry($item,'html',null,$owner,true);
            }
        }
    }
    $atom .= '</feed>' . "\r\n";
    logger('notifier: ' . $atom, LOGGER_DATA);
    logger('notifier: slaps: ' . print_r($slaps, true), LOGGER_DATA);
    // If this is a public message and pubmail is set on the parent, include all your email contacts
    $mail_disabled = function_exists('imap_open') && !get_config('system', 'imap_disabled') ? 0 : 1;
    if (!$mail_disabled) {
        if (!strlen($target_item['allow_cid']) && !strlen($target_item['allow_gid']) && !strlen($target_item['deny_cid']) && !strlen($target_item['deny_gid']) && intval($target_item['pubmail'])) {
            $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `network` = '%s'", intval($uid), dbesc(NETWORK_MAIL));
            if (count($r)) {
                foreach ($r as $rr) {
                    $recipients[] = $rr['id'];
                }
            }
        }
    }
    if ($followup) {
        $recip_str = $parent['contact-id'];
    } else {
        $recip_str = implode(', ', $recipients);
    }
    if ($relocate) {
        $r = $recipients_relocate;
    } else {
        $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) AND `blocked` = 0 AND `pending` = 0 ", dbesc($recip_str));
    }
    require_once 'include/salmon.php';
    $interval = get_config('system', 'delivery_interval') === false ? 2 : intval(get_config('system', 'delivery_interval'));
    // If we are using the worker we don't need a delivery interval
    if (get_config("system", "worker")) {
        $interval = false;
    }
    // delivery loop
    if (count($r)) {
        foreach ($r as $contact) {
            if (!$mail && !$fsuggest && !$followup && !$relocate && !$contact['self']) {
                if ($contact['network'] === NETWORK_DIASPORA && $public_message) {
                    continue;
                }
                q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )", dbesc($cmd), intval($item_id), intval($contact['id']));
            }
        }
        // This controls the number of deliveries to execute with each separate delivery process.
        // By default we'll perform one delivery per process. Assuming a hostile shared hosting
        // provider, this provides the greatest chance of deliveries if processes start getting
        // killed. We can also space them out with the delivery_interval to also help avoid them
        // getting whacked.
        // If $deliveries_per_process > 1, we will chain this number of multiple deliveries
        // together into a single process. This will reduce the overall number of processes
        // spawned for each delivery, but they will run longer.
        // When using the workerqueue, we don't need this functionality.
        $deliveries_per_process = intval(get_config('system', 'delivery_batch_count'));
        if ($deliveries_per_process <= 0 or get_config("system", "worker")) {
            $deliveries_per_process = 1;
        }
        $this_batch = array();
        for ($x = 0; $x < count($r); $x++) {
            $contact = $r[$x];
            if ($contact['self']) {
                continue;
            }
            logger("Deliver " . $target_item["guid"] . " to " . $contact['url'], LOGGER_DEBUG);
            // potentially more than one recipient. Start a new process and space them out a bit.
            // we will deliver single recipient types of message and email recipients here.
            if (!$mail && !$fsuggest && !$relocate && !$followup) {
                $this_batch[] = $contact['id'];
                if (count($this_batch) == $deliveries_per_process) {
                    proc_run('php', 'include/delivery.php', $cmd, $item_id, $this_batch);
                    $this_batch = array();
                    if ($interval) {
                        @time_sleep_until(microtime(true) + (double) $interval);
                    }
                }
                continue;
            }
            // be sure to pick up any stragglers
            if (count($this_batch)) {
                proc_run('php', 'include/delivery.php', $cmd, $item_id, $this_batch);
            }
            $deliver_status = 0;
            logger("main delivery by notifier: followup={$followup} mail={$mail} fsuggest={$fsuggest} relocate={$relocate}");
            switch ($contact['network']) {
                case NETWORK_DFRN:
                    // perform local delivery if we are on the same site
                    $basepath = implode('/', array_slice(explode('/', $contact['url']), 0, 3));
                    if (link_compare($basepath, $a->get_baseurl())) {
                        $nickname = basename($contact['url']);
                        if ($contact['issued-id']) {
                            $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
                        } else {
                            $sql_extra = sprintf(" AND `issued-id` = '%s' ", dbesc($contact['dfrn-id']));
                        }
                        $x = q("SELECT\t`contact`.*, `contact`.`uid` AS `importer_uid`,\n\t\t\t\t\t\t\t`contact`.`pubkey` AS `cpubkey`,\n\t\t\t\t\t\t\t`contact`.`prvkey` AS `cprvkey`,\n\t\t\t\t\t\t\t`contact`.`thumb` AS `thumb`,\n\t\t\t\t\t\t\t`contact`.`url` as `url`,\n\t\t\t\t\t\t\t`contact`.`name` as `senderName`,\n\t\t\t\t\t\t\t`user`.*\n\t\t\t\t\t\t\tFROM `contact`\n\t\t\t\t\t\t\tINNER JOIN `user` ON `contact`.`uid` = `user`.`uid`\n\t\t\t\t\t\t\tWHERE `contact`.`blocked` = 0 AND `contact`.`archive` = 0\n\t\t\t\t\t\t\tAND `contact`.`pending` = 0\n\t\t\t\t\t\t\tAND `contact`.`network` = '%s' AND `user`.`nickname` = '%s'\n\t\t\t\t\t\t\t{$sql_extra}\n\t\t\t\t\t\t\tAND `user`.`account_expired` = 0 AND `user`.`account_removed` = 0 LIMIT 1", dbesc(NETWORK_DFRN), dbesc($nickname));
                        if ($x && count($x)) {
                            $write_flag = $x[0]['rel'] && $x[0]['rel'] != CONTACT_IS_SHARING ? true : false;
                            if (($owner['page-flags'] == PAGE_COMMUNITY || $write_flag) && !$x[0]['writable']) {
                                q("update contact set writable = 1 where id = %d", intval($x[0]['id']));
                                $x[0]['writable'] = 1;
                            }
                            // if contact's ssl policy changed, which we just determined
                            // is on our own server, update our contact links
                            $ssl_policy = get_config('system', 'ssl_policy');
                            fix_contact_ssl_policy($x[0], $ssl_policy);
                            // If we are setup as a soapbox we aren't accepting top level posts from this person
                            if ($x[0]['page-flags'] == PAGE_SOAPBOX and $top_level) {
                                break;
                            }
                            require_once 'library/simplepie/simplepie.inc';
                            logger('mod-delivery: local delivery');
                            local_delivery($x[0], $atom);
                            break;
                        }
                    }
                    logger('notifier: dfrndelivery: ' . $contact['name']);
                    $deliver_status = dfrn_deliver($owner, $contact, $atom);
                    logger('notifier: dfrn_delivery returns ' . $deliver_status);
                    if ($deliver_status == -1) {
                        logger('notifier: delivery failed: queuing message');
                        // queue message for redelivery
                        add_to_queue($contact['id'], NETWORK_DFRN, $atom);
                    }
                    break;
                case NETWORK_OSTATUS:
                    // Do not send to ostatus if we are not configured to send to public networks
                    if ($owner['prvnets']) {
                        break;
                    }
                    if (get_config('system', 'ostatus_disabled') || get_config('system', 'dfrn_only')) {
                        break;
                    }
                    if ($followup && $contact['notify']) {
                        logger('slapdelivery followup item ' . $item_id . ' to ' . $contact['name']);
                        $deliver_status = slapper($owner, $contact['notify'], $slap);
                        if ($deliver_status == -1) {
                            // queue message for redelivery
                            add_to_queue($contact['id'], NETWORK_OSTATUS, $slap);
                        }
                    } else {
                        // only send salmon if public - e.g. if it's ok to notify
                        // a public hub, it's ok to send a salmon
                        if (count($slaps) && $public_message && !$expire) {
                            logger('slapdelivery item ' . $item_id . ' to ' . $contact['name']);
                            foreach ($slaps as $slappy) {
                                if ($contact['notify']) {
                                    $deliver_status = slapper($owner, $contact['notify'], $slappy);
                                    if ($deliver_status == -1) {
                                        // queue message for redelivery
                                        add_to_queue($contact['id'], NETWORK_OSTATUS, $slappy);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case NETWORK_MAIL:
                case NETWORK_MAIL2:
                    if (get_config('system', 'dfrn_only')) {
                        break;
                    }
                    // WARNING: does not currently convert to RFC2047 header encodings, etc.
                    $addr = $contact['addr'];
                    if (!strlen($addr)) {
                        break;
                    }
                    if ($cmd === 'wall-new' || $cmd === 'comment-new') {
                        $it = null;
                        if ($cmd === 'wall-new') {
                            $it = $items[0];
                        } else {
                            $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($argv[2]), intval($uid));
                            if (count($r)) {
                                $it = $r[0];
                            }
                        }
                        if (!$it) {
                            break;
                        }
                        $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
                        if (!count($local_user)) {
                            break;
                        }
                        $reply_to = '';
                        $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1", intval($uid));
                        if ($r1 && $r1[0]['reply_to']) {
                            $reply_to = $r1[0]['reply_to'];
                        }
                        $subject = $it['title'] ? email_header_encode($it['title'], 'UTF-8') : t("(no subject)");
                        // only expose our real email address to true friends
                        if ($contact['rel'] == CONTACT_IS_FRIEND && !$contact['blocked']) {
                            if ($reply_to) {
                                $headers = 'From: ' . email_header_encode($local_user[0]['username'], 'UTF-8') . ' <' . $reply_to . '>' . "\n";
                                $headers .= 'Sender: ' . $local_user[0]['email'] . "\n";
                            } else {
                                $headers = 'From: ' . email_header_encode($local_user[0]['username'], 'UTF-8') . ' <' . $local_user[0]['email'] . '>' . "\n";
                            }
                        } else {
                            $headers = 'From: ' . email_header_encode($local_user[0]['username'], 'UTF-8') . ' <' . t('noreply') . '@' . $a->get_hostname() . '>' . "\n";
                        }
                        //if($reply_to)
                        //	$headers .= 'Reply-to: ' . $reply_to . "\n";
                        $headers .= 'Message-Id: <' . iri2msgid($it['uri']) . '>' . "\n";
                        if ($it['uri'] !== $it['parent-uri']) {
                            $headers .= "References: <" . iri2msgid($it["parent-uri"]) . ">";
                            // If Threading is enabled, write down the correct parent
                            if ($it["thr-parent"] != "" and $it["thr-parent"] != $it["parent-uri"]) {
                                $headers .= " <" . iri2msgid($it["thr-parent"]) . ">";
                            }
                            $headers .= "\n";
                            if (!$it['title']) {
                                $r = q("SELECT `title` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($it['parent-uri']), intval($uid));
                                if (count($r) and $r[0]['title'] != '') {
                                    $subject = $r[0]['title'];
                                } else {
                                    $r = q("SELECT `title` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($it['parent-uri']), intval($uid));
                                    if (count($r) and $r[0]['title'] != '') {
                                        $subject = $r[0]['title'];
                                    }
                                }
                            }
                            if (strncasecmp($subject, 'RE:', 3)) {
                                $subject = 'Re: ' . $subject;
                            }
                        }
                        email_send($addr, $subject, $headers, $it);
                    }
                    break;
                case NETWORK_DIASPORA:
                    if (get_config('system', 'dfrn_only') || !get_config('system', 'diaspora_enabled')) {
                        break;
                    }
                    if ($mail) {
                        diaspora_send_mail($item, $owner, $contact);
                        break;
                    }
                    if (!$normal_mode) {
                        break;
                    }
                    // special handling for followup to public post
                    // all other public posts processed as public batches further below
                    if ($public_message) {
                        if ($followup) {
                            diaspora_send_followup($target_item, $owner, $contact, true);
                        }
                        break;
                    }
                    if (!$contact['pubkey']) {
                        break;
                    }
                    $unsupported_activities = array(ACTIVITY_DISLIKE, ACTIVITY_ATTEND, ACTIVITY_ATTENDNO, ACTIVITY_ATTENDMAYBE);
                    //don't transmit activities which are not supported by diaspora
                    foreach ($unsupported_activities as $act) {
                        if (activity_match($target_item['verb'], $act)) {
                            break 2;
                        }
                    }
                    if ($target_item['deleted'] && ($target_item['uri'] === $target_item['parent-uri'] || $followup)) {
                        // send both top-level retractions and relayable retractions for owner to relay
                        diaspora_send_retraction($target_item, $owner, $contact);
                        break;
                    } elseif ($followup) {
                        // send comments and likes to owner to relay
                        diaspora_send_followup($target_item, $owner, $contact);
                        break;
                    } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
                        // we are the relay - send comments, likes and relayable_retractions
                        // (of comments and likes) to our conversants
                        diaspora_send_relay($target_item, $owner, $contact);
                        break;
                    } elseif ($top_level && !$walltowall) {
                        // currently no workable solution for sending walltowall
                        diaspora_send_status($target_item, $owner, $contact);
                        break;
                    }
                    break;
                case NETWORK_FEED:
                case NETWORK_FACEBOOK:
                    if (get_config('system', 'dfrn_only')) {
                        break;
                    }
                case NETWORK_PUMPIO:
                    if (get_config('system', 'dfrn_only')) {
                        break;
                    }
                default:
                    break;
            }
        }
    }
    // send additional slaps to mentioned remote tags (@foo@example.com)
    //if($slap && count($url_recipients) && ($followup || $top_level) && ($public_message || $push_notify) && (! $expire)) {
    if ($slap && count($url_recipients) && ($public_message || $push_notify) && !$expire) {
        if (!get_config('system', 'dfrn_only')) {
            foreach ($url_recipients as $url) {
                if ($url) {
                    logger('notifier: urldelivery: ' . $url);
                    $deliver_status = slapper($owner, $url, $slap);
                    // TODO: redeliver/queue these items on failure, though there is no contact record
                }
            }
        }
    }
    if ($public_message) {
        if (!$followup) {
            $r0 = diaspora_fetch_relay();
        } else {
            $r0 = array();
        }
        $r1 = q("SELECT DISTINCT(`batch`), `id`, `name`,`network` FROM `contact` WHERE `network` = '%s'\n\t\t\tAND `uid` = %d AND `rel` != %d group by `batch` ORDER BY rand() ", dbesc(NETWORK_DIASPORA), intval($owner['uid']), intval(CONTACT_IS_SHARING));
        $r2 = q("SELECT `id`, `name`,`network` FROM `contact`\n\t\t\tWHERE `network` in ( '%s', '%s')  AND `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0\n\t\t\tAND `rel` != %d order by rand() ", dbesc(NETWORK_DFRN), dbesc(NETWORK_MAIL2), intval($owner['uid']), intval(CONTACT_IS_SHARING));
        $r = array_merge($r2, $r1, $r0);
        if (count($r)) {
            logger('pubdeliver: ' . print_r($r, true), LOGGER_DEBUG);
            // throw everything into the queue in case we get killed
            foreach ($r as $rr) {
                if (!$mail && !$fsuggest && !$followup) {
                    q("insert into deliverq ( `cmd`,`item`,`contact` ) values ('%s', %d, %d )", dbesc($cmd), intval($item_id), intval($rr['id']));
                }
            }
            foreach ($r as $rr) {
                // except for Diaspora batch jobs
                // Don't deliver to folks who have already been delivered to
                if ($rr['network'] !== NETWORK_DIASPORA && in_array($rr['id'], $conversants)) {
                    logger('notifier: already delivered id=' . $rr['id']);
                    continue;
                }
                if (!$mail && !$fsuggest && !$followup) {
                    logger('notifier: delivery agent: ' . $rr['name'] . ' ' . $rr['id']);
                    proc_run('php', 'include/delivery.php', $cmd, $item_id, $rr['id']);
                    if ($interval) {
                        @time_sleep_until(microtime(true) + (double) $interval);
                    }
                }
            }
        }
        $push_notify = true;
    }
    if ($push_notify and strlen($hub)) {
        $hubs = explode(',', $hub);
        if (count($hubs)) {
            foreach ($hubs as $h) {
                $h = trim($h);
                if (!strlen($h)) {
                    continue;
                }
                if ($h === '[internal]') {
                    // Set push flag for PuSH subscribers to this topic,
                    // they will be notified in queue.php
                    q("UPDATE `push_subscriber` SET `push` = 1 " . "WHERE `nickname` = '%s'", dbesc($owner['nickname']));
                    logger('Activating internal PuSH for item ' . $item_id, LOGGER_DEBUG);
                } else {
                    $params = 'hub.mode=publish&hub.url=' . urlencode($a->get_baseurl() . '/dfrn_poll/' . $owner['nickname']);
                    post_url($h, $params);
                    logger('publish for item ' . $item_id . ' ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
                }
                if (count($hubs) > 1) {
                    sleep(7);
                }
                // try and avoid multiple hubs responding at precisely the same time
            }
        }
        // Handling the pubsubhubbub requests
        proc_run('php', 'include/pubsubpublish.php');
    }
    // If the item was deleted, clean up the `sign` table
    if ($target_item['deleted']) {
        $r = q("DELETE FROM sign where `retract_iid` = %d", intval($target_item['id']));
    }
    logger('notifier: calling hooks', LOGGER_DEBUG);
    if ($normal_mode) {
        call_hooks('notifier_normal', $target_item);
    }
    call_hooks('notifier_end', $target_item);
    return;
}
Example #22
0
function new_contact($uid, $url, $interactive = false)
{
    $result = array('cid' => -1, 'success' => false, 'message' => '');
    $a = get_app();
    // remove ajax junk, e.g. Twitter
    $url = str_replace('/#!/', '/', $url);
    if (!allowed_url($url)) {
        $result['message'] = t('Disallowed profile URL.');
        return $result;
    }
    if (!$url) {
        $result['message'] = t('Connect URL missing.');
        return $result;
    }
    $arr = array('url' => $url, 'contact' => array());
    call_hooks('follow', $arr);
    if (x($arr['contact'], 'name')) {
        $ret = $arr['contact'];
    } else {
        $ret = probe_url($url);
    }
    if ($ret['network'] === NETWORK_DFRN) {
        if ($interactive) {
            if (strlen($a->path)) {
                $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
            } else {
                $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
            }
            goaway($ret['request'] . "&addr={$myaddr}");
            // NOTREACHED
        }
    } else {
        if (get_config('system', 'dfrn_only')) {
            $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
            $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
            return $result;
        }
    }
    // This extra param just confuses things, remove it
    if ($ret['network'] === NETWORK_DIASPORA) {
        $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
    }
    // do we have enough information?
    if (!(x($ret, 'name') && x($ret, 'poll') && (x($ret, 'url') || x($ret, 'addr')))) {
        $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
        if (!x($ret, 'poll')) {
            $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
        }
        if (!x($ret, 'name')) {
            $result['message'] .= t('An author or name was not found.') . EOL;
        }
        if (!x($ret, 'url')) {
            $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
        }
        if (strpos($url, '@') !== false) {
            $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
            $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
        }
        return $result;
    }
    if ($ret['network'] === NETWORK_OSTATUS && get_config('system', 'ostatus_disabled')) {
        $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
        $ret['notify'] = '';
    }
    if (!$ret['notify']) {
        $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
    }
    $writeable = $ret['network'] === NETWORK_OSTATUS && $ret['notify'] ? 1 : 0;
    $subhub = $ret['network'] === NETWORK_OSTATUS ? true : false;
    $hidden = $ret['network'] === NETWORK_MAIL ? 1 : 0;
    if ($ret['network'] === NETWORK_MAIL) {
        $writeable = 1;
    }
    if ($ret['network'] === NETWORK_DIASPORA) {
        $writeable = 1;
    }
    // check if we already have a contact
    // the poll url is more reliable than the profile url, as we may have
    // indirect links or webfinger links
    $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1", intval($uid), dbesc($ret['poll']), dbesc(normalise_link($ret['poll'])), dbesc($ret['network']));
    if (!count($r)) {
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1", intval($uid), dbesc(normalise_link($url)), dbesc($ret['network']));
    }
    if (count($r)) {
        // update contact
        if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || $network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING) {
            q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d", intval(CONTACT_IS_FRIEND), intval($subhub), intval($r[0]['id']), intval($uid));
        }
    } else {
        // check service class limits
        $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0", intval($uid));
        if (count($r)) {
            $total_contacts = $r[0]['total'];
        }
        if (!service_class_allows($uid, 'total_contacts', $total_contacts)) {
            $result['message'] .= upgrade_message();
            return $result;
        }
        $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0", intval($uid), dbesc($network));
        if (count($r)) {
            $total_network = $r[0]['total'];
        }
        if (!service_class_allows($uid, 'total_contacts_' . $network, $total_network)) {
            $result['message'] .= upgrade_message();
            return $result;
        }
        $new_relation = $ret['network'] === NETWORK_MAIL ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING;
        if ($ret['network'] === NETWORK_DIASPORA) {
            $new_relation = CONTACT_IS_FOLLOWER;
        }
        // create contact record
        $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,\n\t\t\t`writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )\n\t\t\tVALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ", intval($uid), dbesc(datetime_convert()), dbesc($ret['url']), dbesc(normalise_link($ret['url'])), dbesc($ret['addr']), dbesc($ret['alias']), dbesc($ret['batch']), dbesc($ret['notify']), dbesc($ret['poll']), dbesc($ret['poco']), dbesc($ret['name']), dbesc($ret['nick']), dbesc($ret['network']), dbesc($ret['pubkey']), intval($new_relation), intval($ret['priority']), intval($writeable), intval($hidden), intval($subhub));
    }
    $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1", dbesc($ret['url']), dbesc($ret['network']), intval($uid));
    if (!count($r)) {
        $result['message'] .= t('Unable to retrieve contact information.') . EOL;
        return $result;
    }
    $contact = $r[0];
    $contact_id = $r[0]['id'];
    $result['cid'] = $contact_id;
    $g = q("select def_gid from user where uid = %d limit 1", intval($uid));
    if ($g && intval($g[0]['def_gid'])) {
        require_once 'include/group.php';
        group_add_member($uid, '', $contact_id, $g[0]['def_gid']);
    }
    require_once "include/Photo.php";
    $photos = import_profile_photo($ret['photo'], $uid, $contact_id);
    $r = q("UPDATE `contact` SET `photo` = '%s',\n\t\t\t`thumb` = '%s',\n\t\t\t`micro` = '%s',\n\t\t\t`name-date` = '%s',\n\t\t\t`uri-date` = '%s',\n\t\t\t`avatar-date` = '%s'\n\t\t\tWHERE `id` = %d", dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), dbesc(datetime_convert()), dbesc(datetime_convert()), dbesc(datetime_convert()), intval($contact_id));
    // pull feed and consume it, which should subscribe to the hub.
    proc_run('php', "include/onepoll.php", "{$contact_id}", "force");
    // create a follow slap
    $tpl = get_markup_template('follow_slap.tpl');
    $slap = replace_macros($tpl, array('$name' => $a->user['username'], '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'], '$photo' => $a->contact['photo'], '$thumb' => $a->contact['thumb'], '$published' => datetime_convert('UTC', 'UTC', 'now', ATOM_TIME), '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . get_guid(32), '$title' => '', '$type' => 'text', '$content' => t('following'), '$nick' => $a->user['nickname'], '$verb' => ACTIVITY_FOLLOW, '$ostat_follow' => ''));
    $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`\n\t\t\tWHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1", intval($uid));
    if (count($r)) {
        if ($contact['network'] == NETWORK_OSTATUS && strlen($contact['notify'])) {
            require_once 'include/salmon.php';
            slapper($r[0], $contact['notify'], $slap);
        }
        if ($contact['network'] == NETWORK_DIASPORA) {
            require_once 'include/diaspora.php';
            $ret = diaspora_share($a->user, $contact);
            logger('mod_follow: diaspora_share returns: ' . $ret);
        }
    }
    $result['success'] = true;
    return $result;
}
Example #23
0
function discover_directory($search)
{
    $data = Cache::get("dirsearch:" . $search);
    if (!is_null($data)) {
        // Only search for the same item every 24 hours
        if (time() < $data + 60 * 60 * 24) {
            logger("Already searched for " . $search . " in the last 24 hours", LOGGER_DEBUG);
            return;
        }
    }
    $x = fetch_url(get_server() . "/lsearch?p=1&n=500&search=" . urlencode($search));
    $j = json_decode($x);
    if (count($j->results)) {
        foreach ($j->results as $jj) {
            // Check if the contact already exists
            $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
            if ($exists) {
                logger("Profile " . $jj->url . " already exists (" . $search . ")", LOGGER_DEBUG);
                if ($exists[0]["last_contact"] < $exists[0]["last_failure"] and $exists[0]["updated"] < $exists[0]["last_failure"]) {
                    continue;
                }
                // Update the contact
                poco_last_updated($jj->url);
                continue;
            }
            // Harcoded paths aren't so good. But in this case it is okay.
            // First: We only will get Friendica contacts (which always are using this url schema)
            // Second: There will be no further problems if we are doing a mistake
            $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "\$1\$2", $jj->url);
            if ($server_url != $jj->url) {
                if (!poco_check_server($server_url)) {
                    logger("Friendica server " . $server_url . " doesn't answer.", LOGGER_DEBUG);
                    continue;
                }
            }
            logger("Friendica server " . $server_url . " seems to be okay.", LOGGER_DEBUG);
            logger("Check if profile " . $jj->url . " is reachable (" . $search . ")", LOGGER_DEBUG);
            $data = probe_url($jj->url);
            if ($data["network"] == NETWORK_DFRN) {
                logger("Add profile " . $jj->url . " to local directory (" . $search . ")", LOGGER_DEBUG);
                poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
            }
        }
    }
    Cache::set("dirsearch:" . $search, time(), CACHE_DAY);
}
Example #24
0
function find_diaspora_person_by_handle($handle)
{
    $person = false;
    $update = false;
    $got_lock = false;
    $endlessloop = 0;
    $maxloops = 10;
    do {
        $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1", dbesc(NETWORK_DIASPORA), dbesc($handle));
        if (count($r)) {
            $person = $r[0];
            logger('find_diaspora_person_by handle: in cache ' . print_r($r, true), LOGGER_DEBUG);
            // update record occasionally so it doesn't get stale
            $d = strtotime($person['updated'] . ' +00:00');
            if ($d < strtotime('now - 14 days')) {
                $update = true;
            }
        }
        // FETCHING PERSON INFORMATION FROM REMOTE SERVER
        //
        // If the person isn't in our 'fcontact' table, or if he/she is but
        // his/her information hasn't been updated for more than 14 days, then
        // we want to fetch the person's information from the remote server.
        //
        // Note that $person isn't changed by this block of code unless the
        // person's information has been successfully fetched from the remote
        // server. So if $person was 'false' to begin with (because he/she wasn't
        // in the local cache), it'll stay false, and if $person held the local
        // cache information to begin with, it'll keep that information. That way
        // if there's a problem with the remote fetch, we can at least use our
        // cached information--it's better than nothing.
        if (!$person || $update) {
            // Lock the function to prevent race conditions if multiple items
            // come in at the same time from a person who doesn't exist in
            // fcontact
            //
            // Don't loop forever. On the last loop, try to create the contact
            // whether the function is locked or not. Maybe the locking thread
            // has died or something. At any rate, a duplicate in 'fcontact'
            // is a much smaller problem than a deadlocked thread
            $got_lock = lock_function('find_diaspora_person_by_handle', false);
            if ($endlessloop + 1 >= $maxloops) {
                $got_lock = true;
            }
            if ($got_lock) {
                logger('find_diaspora_person_by_handle: create or refresh', LOGGER_DEBUG);
                require_once 'include/Scrape.php';
                $r = probe_url($handle, PROBE_DIASPORA);
                // Note that Friendica contacts can return a "Diaspora person"
                // if Diaspora connectivity is enabled on their server
                if (count($r) && $r['network'] === NETWORK_DIASPORA) {
                    add_fcontact($r, $update);
                    $person = $r;
                }
                unlock_function('find_diaspora_person_by_handle');
            } else {
                logger('find_diaspora_person_by_handle: couldn\'t lock function', LOGGER_DEBUG);
                if (!$person) {
                    block_on_function_lock('find_diaspora_person_by_handle');
                }
            }
        }
    } while (!$person && !$got_lock && ++$endlessloop < $maxloops);
    // We need to try again if the person wasn't in 'fcontact' but the function was locked.
    // The fact that the function was locked may mean that another process was creating the
    // person's record. It could also mean another process was creating or updating an unrelated
    // person.
    //
    // At any rate, we need to keep trying until we've either got the person or had a chance to
    // try to fetch his/her remote information. But we don't want to block on locking the
    // function, because if the other process is creating the record, then when we acquire the lock
    // we'll dive right into creating another, duplicate record. We DO want to at least wait
    // until the lock is released, so we don't flood the database with requests.
    //
    // If the person was in the 'fcontact' table, don't try again. It's not worth the time, since
    // we do have some information for the person
    return $person;
}
Example #25
0
function notifications_content(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        return;
    }
    nav_set_selected('notifications');
    $json = $a->argc > 1 && $a->argv[$a->argc - 1] === 'json' ? true : false;
    $o = '';
    $tabs = array(array('label' => t('System'), 'url' => $a->get_baseurl(true) . '/notifications/system', 'sel' => $a->argv[1] == 'system' ? 'active' : '', 'accesskey' => 'y'), array('label' => t('Network'), 'url' => $a->get_baseurl(true) . '/notifications/network', 'sel' => $a->argv[1] == 'network' ? 'active' : '', 'accesskey' => 'w'), array('label' => t('Personal'), 'url' => $a->get_baseurl(true) . '/notifications/personal', 'sel' => $a->argv[1] == 'personal' ? 'active' : '', 'accesskey' => 'r'), array('label' => t('Home'), 'url' => $a->get_baseurl(true) . '/notifications/home', 'sel' => $a->argv[1] == 'home' ? 'active' : '', 'accesskey' => 'h'), array('label' => t('Introductions'), 'url' => $a->get_baseurl(true) . '/notifications/intros', 'sel' => $a->argv[1] == 'intros' ? 'active' : '', 'accesskey' => 'i'));
    $o = "";
    if ($a->argc > 1 && $a->argv[1] == 'intros' || $a->argc == 1) {
        nav_set_selected('introductions');
        if ($a->argc > 2 && $a->argv[2] == 'all') {
            $sql_extra = '';
        } else {
            $sql_extra = " AND `ignore` = 0 ";
        }
        $notif_tpl = get_markup_template('notifications.tpl');
        $notif_content .= '<a href="' . (strlen($sql_extra) ? 'notifications/intros/all' : 'notifications/intros') . '" id="notifications-show-hide-link" >' . (strlen($sql_extra) ? t('Show Ignored Requests') : t('Hide Ignored Requests')) . '</a></div>' . "\r\n";
        $r = q("SELECT COUNT(*)\tAS `total` FROM `intro`\n\t\t\tWHERE `intro`.`uid` = %d {$sql_extra} AND `intro`.`blocked` = 0 ", intval($_SESSION['uid']));
        if ($r && count($r)) {
            $a->set_pager_total($r[0]['total']);
            $a->set_pager_itemspage(20);
        }
        $r = q("SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, `fcontact`.`name` AS `fname`,`fcontact`.`url` AS `furl`,`fcontact`.`photo` AS `fphoto`,`fcontact`.`request` AS `frequest`,\n\t\t\t\t`gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,\n\t\t\t\t`gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,\n\t\t\t\t`gcontact`.`network` AS `gnetwork`\n\t\t\tFROM `intro`\n\t\t\t\tLEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`\n\t\t\t\tLEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`\n\t\t\t\tLEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`\n\t\t\tWHERE `intro`.`uid` = %d {$sql_extra} AND `intro`.`blocked` = 0 ", intval($_SESSION['uid']));
        if ($r !== false && count($r)) {
            $sugg = get_markup_template('suggestions.tpl');
            $tpl = get_markup_template("intros.tpl");
            foreach ($r as $rr) {
                if ($rr['fid']) {
                    $return_addr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname() . ($a->path ? '/' . $a->path : ''));
                    $notif_content .= replace_macros($sugg, array('$str_notifytype' => t('Notification type: '), '$notify_type' => t('Friend Suggestion'), '$intro_id' => $rr['intro_id'], '$madeby' => sprintf(t('suggested by %s'), $rr['name']), '$contact_id' => $rr['contact-id'], '$photo' => x($rr, 'fphoto') ? proxy_url($rr['fphoto'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg", '$fullname' => $rr['fname'], '$url' => zrl($rr['furl']), '$hidden' => array('hidden', t('Hide this contact from others'), $rr['hidden'] == 1, ''), '$activity' => array('activity', t('Post a new friend activity'), intval(get_pconfig(local_user(), 'system', 'post_newfriend')) ? '1' : 0, t('if applicable')), '$knowyou' => $knowyou, '$approve' => t('Approve'), '$note' => $rr['note'], '$request' => $rr['frequest'] . '?addr=' . $return_addr, '$ignore' => t('Ignore'), '$discard' => t('Discard')));
                    continue;
                }
                $friend_selected = $rr['network'] !== NETWORK_OSTATUS ? ' checked="checked" ' : ' disabled ';
                $fan_selected = $rr['network'] === NETWORK_OSTATUS ? ' checked="checked" disabled ' : '';
                $dfrn_tpl = get_markup_template('netfriend.tpl');
                $knowyou = '';
                $dfrn_text = '';
                if ($rr['network'] === NETWORK_DFRN || $rr['network'] === NETWORK_DIASPORA) {
                    if ($rr['network'] === NETWORK_DFRN) {
                        $knowyou = t('Claims to be known to you: ') . ($rr['knowyou'] ? t('yes') : t('no'));
                        $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Fan/Admirer" means that you allow to read but you do not want to read theirs. Approve as: ');
                    } else {
                        $knowyou = '';
                        $helptext = t('Shall your connection be bidirectional or not? "Friend" implies that you allow to read and you subscribe to their posts. "Sharer" means that you allow to read but you do not want to read theirs. Approve as: ');
                    }
                    $dfrn_text = replace_macros($dfrn_tpl, array('$intro_id' => $rr['intro_id'], '$friend_selected' => $friend_selected, '$fan_selected' => $fan_selected, '$approve_as' => $helptext, '$as_friend' => t('Friend'), '$as_fan' => $rr['network'] == NETWORK_DIASPORA ? t('Sharer') : t('Fan/Admirer')));
                }
                $header = $rr["name"];
                $ret = probe_url($rr["url"]);
                if ($rr['gnetwork'] == "") {
                    $rr['gnetwork'] = $ret["network"];
                }
                if ($ret["addr"] != "") {
                    $header .= " <" . $ret["addr"] . ">";
                }
                $header .= " (" . network_to_name($rr['gnetwork'], $rr['url']) . ")";
                // Don't show these data until you are connected. Diaspora is doing the same.
                if ($rr['gnetwork'] === NETWORK_DIASPORA) {
                    $rr['glocation'] = "";
                    $rr['gabout'] = "";
                    $rr['ggender'] = "";
                }
                $notif_content .= replace_macros($tpl, array('$header' => htmlentities($header), '$str_notifytype' => t('Notification type: '), '$notify_type' => $rr['network'] !== NETWORK_OSTATUS ? t('Friend/Connect Request') : t('New Follower'), '$dfrn_text' => $dfrn_text, '$dfrn_id' => $rr['issued-id'], '$uid' => $_SESSION['uid'], '$intro_id' => $rr['intro_id'], '$contact_id' => $rr['contact-id'], '$photo' => x($rr, 'photo') ? proxy_url($rr['photo'], false, PROXY_SIZE_SMALL) : "images/person-175.jpg", '$fullname' => $rr['name'], '$location' => bbcode($rr['glocation'], false, false), '$location_label' => t('Location:'), '$about' => bbcode($rr['gabout'], false, false), '$about_label' => t('About:'), '$keywords' => $rr['gkeywords'], '$keywords_label' => t('Tags:'), '$gender' => $rr['ggender'], '$gender_label' => t('Gender:'), '$hidden' => array('hidden', t('Hide this contact from others'), $rr['hidden'] == 1, ''), '$activity' => array('activity', t('Post a new friend activity'), intval(get_pconfig(local_user(), 'system', 'post_newfriend')) ? '1' : 0, t('if applicable')), '$url' => $rr['url'], '$zrl' => zrl($rr['url']), '$url_label' => t('Profile URL'), '$knowyou' => $knowyou, '$approve' => t('Approve'), '$note' => $rr['note'], '$ignore' => t('Ignore'), '$discard' => t('Discard')));
            }
        } else {
            info(t('No introductions.') . EOL);
        }
        $o .= replace_macros($notif_tpl, array('$notif_header' => t('Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
        $o .= paginate($a);
        return $o;
    } else {
        if ($a->argc > 1 && $a->argv[1] == 'network') {
            $notif_tpl = get_markup_template('notifications.tpl');
            $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND `pitem`.`parent` != 0 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC", intval(local_user()));
            $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
            $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
            $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
            $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
            $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
            $notif_content = '';
            if ($r) {
                foreach ($r as $it) {
                    switch ($it['verb']) {
                        case ACTIVITY_LIKE:
                            $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                            break;
                        case ACTIVITY_DISLIKE:
                            $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                            break;
                        case ACTIVITY_FRIEND:
                            $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                            $obj = parse_xml_string($xmlhead . $it['object']);
                            $it['fname'] = $obj->title;
                            $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                            break;
                        default:
                            $item_text = $it['id'] == $it['parent'] ? sprintf(t("%s created a new post"), $it['author-name']) : sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']);
                            $tpl = $it['id'] == $it['parent'] ? $tpl_item_posts : $tpl_item_comments;
                            $notif_content .= replace_macros($tpl, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => proxy_url($it['author-avatar'], false, PROXY_SIZE_MICRO), '$item_text' => $item_text, '$item_when' => relative_date($it['created'])));
                    }
                }
            } else {
                $notif_content = t('No more network notifications.');
            }
            $o .= replace_macros($notif_tpl, array('$notif_header' => t('Network Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
        } else {
            if ($a->argc > 1 && $a->argv[1] == 'system') {
                $notif_tpl = get_markup_template('notifications.tpl');
                $not_tpl = get_markup_template('notify.tpl');
                require_once 'include/bbcode.php';
                $r = q("SELECT * from notify where uid = %d and seen = 0 order by date desc", intval(local_user()));
                if (count($r) > 0) {
                    foreach ($r as $it) {
                        $notif_content .= replace_macros($not_tpl, array('$item_link' => $a->get_baseurl(true) . '/notify/view/' . $it['id'], '$item_image' => proxy_url($it['photo'], false, PROXY_SIZE_MICRO), '$item_text' => strip_tags(bbcode($it['msg'])), '$item_when' => relative_date($it['date'])));
                    }
                } else {
                    $notif_content .= t('No more system notifications.');
                }
                $o .= replace_macros($notif_tpl, array('$notif_header' => t('System Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
            } else {
                if ($a->argc > 1 && $a->argv[1] == 'personal') {
                    $notif_tpl = get_markup_template('notifications.tpl');
                    $myurl = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
                    $myurl = substr($myurl, strpos($myurl, '://') + 3);
                    $myurl = str_replace(array('www.', '.'), array('', '\\.'), $myurl);
                    $diasp_url = str_replace('/profile/', '/u/', $myurl);
                    $sql_extra .= sprintf(" AND ( `item`.`author-link` regexp '%s' or `item`.`tag` regexp '%s' or `item`.`tag` regexp '%s' ) ", dbesc($myurl . '$'), dbesc($myurl . '\\]'), dbesc($diasp_url . '\\]'));
                    $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1\n\t\t\t\t{$sql_extra}\n\t\t\t\tAND `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 0 ORDER BY `item`.`created` DESC", intval(local_user()));
                    $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
                    $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
                    $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
                    $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
                    $tpl_item_posts = get_markup_template('notifications_posts_item.tpl');
                    $notif_content = '';
                    if (count($r) > 0) {
                        foreach ($r as $it) {
                            switch ($it['verb']) {
                                case ACTIVITY_LIKE:
                                    $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                case ACTIVITY_DISLIKE:
                                    $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                case ACTIVITY_FRIEND:
                                    $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                                    $obj = parse_xml_string($xmlhead . $it['object']);
                                    $it['fname'] = $obj->title;
                                    $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                                    break;
                                default:
                                    $item_text = $it['id'] == $it['parent'] ? sprintf(t("%s created a new post"), $it['author-name']) : sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']);
                                    $tpl = $it['id'] == $it['parent'] ? $tpl_item_posts : $tpl_item_comments;
                                    $notif_content .= replace_macros($tpl, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => $item_text, '$item_when' => relative_date($it['created'])));
                            }
                        }
                    } else {
                        $notif_content = t('No more personal notifications.');
                    }
                    $o .= replace_macros($notif_tpl, array('$notif_header' => t('Personal Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
                } else {
                    if ($a->argc > 1 && $a->argv[1] == 'home') {
                        $notif_tpl = get_markup_template('notifications.tpl');
                        $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`author-name`,\n\t\t\t\t`item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object` as `object`,\n\t\t\t\t`pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`, `pitem`.`guid` as `pguid`\n\t\t\t\tFROM `item` INNER JOIN `item` as `pitem` ON  `pitem`.`id`=`item`.`parent`\n\t\t\t\tWHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND\n\t\t\t\t `item`.`deleted` = 0 AND `item`.`uid` = %d AND `item`.`wall` = 1 ORDER BY `item`.`created` DESC", intval(local_user()));
                        $tpl_item_likes = get_markup_template('notifications_likes_item.tpl');
                        $tpl_item_dislikes = get_markup_template('notifications_dislikes_item.tpl');
                        $tpl_item_friends = get_markup_template('notifications_friends_item.tpl');
                        $tpl_item_comments = get_markup_template('notifications_comments_item.tpl');
                        $notif_content = '';
                        if (count($r) > 0) {
                            foreach ($r as $it) {
                                switch ($it['verb']) {
                                    case ACTIVITY_LIKE:
                                        $notif_content .= replace_macros($tpl_item_likes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s liked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    case ACTIVITY_DISLIKE:
                                        $notif_content .= replace_macros($tpl_item_dislikes, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s disliked %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    case ACTIVITY_FRIEND:
                                        $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
                                        $obj = parse_xml_string($xmlhead . $it['object']);
                                        $it['fname'] = $obj->title;
                                        $notif_content .= replace_macros($tpl_item_friends, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s is now friends with %s"), $it['author-name'], $it['fname']), '$item_when' => relative_date($it['created'])));
                                        break;
                                    default:
                                        $notif_content .= replace_macros($tpl_item_comments, array('$item_link' => $a->get_baseurl(true) . '/display/' . $it['pguid'], '$item_image' => $it['author-avatar'], '$item_text' => sprintf(t("%s commented on %s's post"), $it['author-name'], $it['pname']), '$item_when' => relative_date($it['created'])));
                                }
                            }
                        } else {
                            $notif_content = t('No more home notifications.');
                        }
                        $o .= replace_macros($notif_tpl, array('$notif_header' => t('Home Notifications'), '$tabs' => $tabs, '$notif_content' => $notif_content));
                    }
                }
            }
        }
    }
    $o .= paginate($a);
    return $o;
}