+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?
+ /// @todo use gcontact and/or the addr field for a lookup
+ 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 ORDER BY `id` LIMIT 2",
+ dbesc(normalise_link($url)),
+ intval($uid));
+
+ if (!$contact)
+ $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
+ 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);
+ } elseif ($uid != 0)
+ return 0;
+
+ 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;
+
+ $url = $data["url"];
+
+ if ($contactid == 0) {
+ q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
+ `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
+ `batch`, `request`, `confirm`, `poco`,
+ `writable`, `blocked`, `readonly`, `pending`)
+ VALUES (%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 ORDER BY `id` LIMIT 2",
+ dbesc(normalise_link($data["url"])),
+ intval($uid));
+ if (!$contact)
+ return 0;
+
+ $contactid = $contact[0]["id"];
+ }
+
+ if ((count($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
+ q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d",
+ dbesc(normalise_link($url)),
+ intval($contactid));
+
+ require_once("Photo.php");
+
+ update_contact_avatar($data["photo"],$uid,$contactid);
+
+ q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
+ `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
+ dbesc($data["addr"]),
+ dbesc($data["alias"]),
+ dbesc($data["name"]),
+ dbesc($data["nick"]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($contactid)
+ );
+
+ return $contactid;
+}
+
+/**
+ * @brief Returns posts from a given gcontact
+ *
+ * @param App $a argv application class
+ * @param int $gcontact_id Global contact
+ *
+ * @return string posts in HTML
+ */
+function posts_from_gcontact($a, $gcontact_id) {
+
+ require_once('include/conversation.php');
+
+ // There are no posts with "uid = 0" with connector networks
+ // This speeds up the query a lot
+ $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
+ if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
+ $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
+ else
+ $sql = "`item`.`uid` = %d";
+
+ if(get_config('system', 'old_pager')) {
+ $r = q("SELECT COUNT(*) AS `total` FROM `item`
+ WHERE `gcontact-id` = %d and $sql",
+ intval($gcontact_id),
+ intval(local_user()));
+
+ $a->set_pager_total($r[0]['total']);
+ }
+
+ $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
+ `author-name` AS `name`, `owner-avatar` AS `photo`,
+ `owner-link` AS `url`, `owner-avatar` AS `thumb`
+ FROM `item` FORCE INDEX (`gcontactid_uid_created`)
+ WHERE `gcontact-id` = %d AND $sql AND
+ NOT `deleted` AND NOT `moderated` AND `visible`
+ ORDER BY `item`.`created` DESC LIMIT %d, %d",
+ intval($gcontact_id),
+ intval(local_user()),
+ intval($a->pager['start']),
+ intval($a->pager['itemspage'])
+ );
+
+ $o = conversation($a,$r,'community',false);
+
+ if(!get_config('system', 'old_pager')) {
+ $o .= alt_pager($a,count($r));
+ } else {
+ $o .= paginate($a);
+ }
+
+ return $o;
+}
+
+/**
+ * @brief Returns posts from a given contact
+ *
+ * @param App $a argv application class
+ * @param int $contact_id contact
+ *
+ * @return string posts in HTML
+ */
+function posts_from_contact($a, $contact_id) {
+
+ require_once('include/conversation.php');
+
+ $r = q("SELECT `url` FROM `contact` WHERE `id` = %d", intval($contact_id));
+ if (!$r)
+ return false;
+
+ $contact = $r[0];
+
+ if(get_config('system', 'old_pager')) {
+ $r = q("SELECT COUNT(*) AS `total` FROM `item`
+ WHERE `item`.`uid` = %d AND `author-link` IN ('%s', '%s')",
+ intval(local_user()),
+ dbesc(str_replace("https://", "http://", $contact["url"])),
+ dbesc(str_replace("http://", "https://", $contact["url"])));
+
+ $a->set_pager_total($r[0]['total']);
+ }
+
+ $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
+ `author-name` AS `name`, `owner-avatar` AS `photo`,
+ `owner-link` AS `url`, `owner-avatar` AS `thumb`
+ FROM `item` FORCE INDEX (`uid_contactid_created`)
+ WHERE `item`.`uid` = %d AND `contact-id` = %d
+ AND `author-link` IN ('%s', '%s')
+ AND NOT `deleted` AND NOT `moderated` AND `visible`
+ ORDER BY `item`.`created` DESC LIMIT %d, %d",
+ intval(local_user()),
+ intval($contact_id),
+ dbesc(str_replace("https://", "http://", $contact["url"])),
+ dbesc(str_replace("http://", "https://", $contact["url"])),
+ intval($a->pager['start']),
+ intval($a->pager['itemspage'])
+ );
+
+ $o .= conversation($a,$r,'community',false);
+
+ if(!get_config('system', 'old_pager'))
+ $o .= alt_pager($a,count($r));
+ else
+ $o .= paginate($a);
+
+ return $o;
+}
+
+/**
+ * @brief Returns a formatted location string from the given profile array
+ *
+ * @param array $profile Profile array (Generated from the "profile" table)
+ *
+ * @return string Location string
+ */
+function formatted_location($profile) {
+ $location = '';
+
+ if($profile['locality'])
+ $location .= $profile['locality'];
+
+ if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
+ if($location)
+ $location .= ', ';
+
+ $location .= $profile['region'];
+ }
+
+ if($profile['country-name']) {
+ if($location)
+ $location .= ', ';
+
+ $location .= $profile['country-name'];
+ }
+
+ return $location;
+}
+?>