2 require_once("include/Scrape.php");
3 require_once("include/socgraph.php");
4 require_once('include/group.php');
5 require_once('include/salmon.php');
6 require_once('include/ostatus.php');
7 require_once("include/Photo.php");
8 require_once('include/diaspora.php');
10 function update_contact($id) {
12 Warning: Never ever fetch the public key via probe_url and write it into the contacts.
13 This will reliably kill your communication with Friendica contacts.
16 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
20 $ret = probe_url($r[0]["url"]);
22 // If probe_url fails the network code will be different
23 if ($ret["network"] != $r[0]["network"])
28 // make sure to not overwrite existing values with blank entries
29 foreach ($ret AS $key => $val) {
30 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
31 $ret[$key] = $r[0][$key];
33 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
40 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
42 dbesc(normalise_link($ret['url'])),
46 dbesc($ret['notify']),
52 // Update the corresponding gcontact entry
53 poco_last_updated($ret["url"]);
59 // Takes a $uid and a url/handle and adds a new contact
60 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
63 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
65 // $return['success'] boolean true if successful
66 // $return['message'] error text if success is false.
70 function new_contact($uid,$url,$interactive = false) {
72 $result = array('cid' => -1, 'success' => false,'message' => '');
76 // remove ajax junk, e.g. Twitter
78 $url = str_replace('/#!/','/',$url);
80 if(! allowed_url($url)) {
81 $result['message'] = t('Disallowed profile URL.');
86 $result['message'] = t('Connect URL missing.');
90 $arr = array('url' => $url, 'contact' => array());
92 call_hooks('follow', $arr);
94 if(x($arr['contact'],'name'))
95 $ret = $arr['contact'];
97 $ret = probe_url($url);
99 if($ret['network'] === NETWORK_DFRN) {
102 $myaddr = bin2hex(App::get_baseurl() . '/profile/' . $a->user['nickname']);
104 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
106 goaway($ret['request'] . "&addr=$myaddr");
112 if(get_config('system','dfrn_only')) {
113 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
114 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
124 // This extra param just confuses things, remove it
125 if($ret['network'] === NETWORK_DIASPORA)
126 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
129 // do we have enough information?
131 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
132 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
134 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
136 $result['message'] .= t('An author or name was not found.') . EOL;
138 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
139 if(strpos($url,'@') !== false) {
140 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
141 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
146 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
147 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
156 if(! $ret['notify']) {
157 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
160 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
162 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
164 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
166 if(in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA)))
169 // check if we already have a contact
170 // the poll url is more reliable than the profile url, as we may have
171 // indirect links or webfinger links
173 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
176 dbesc(normalise_link($ret['poll'])),
177 dbesc($ret['network'])
180 if (!dbm::is_result($r))
181 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
182 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
185 if (dbm::is_result($r)) {
187 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
188 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
189 intval(CONTACT_IS_FRIEND),
198 // check service class limits
200 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
203 if (dbm::is_result($r))
204 $total_contacts = $r[0]['total'];
206 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
207 $result['message'] .= upgrade_message();
211 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
215 if (dbm::is_result($r))
216 $total_network = $r[0]['total'];
218 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
219 $result['message'] .= upgrade_message();
223 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
225 // create contact record
226 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
227 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
228 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
230 dbesc(datetime_convert()),
232 dbesc(normalise_link($ret['url'])),
234 dbesc($ret['alias']),
235 dbesc($ret['batch']),
236 dbesc($ret['notify']),
241 dbesc($ret['network']),
242 dbesc($ret['pubkey']),
243 intval($new_relation),
244 intval($ret['priority']),
251 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
253 dbesc($ret['network']),
257 if (! dbm::is_result($r)) {
258 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
263 $contact_id = $r[0]['id'];
264 $result['cid'] = $contact_id;
266 $def_gid = get_default_group($uid, $contact["network"]);
267 if (intval($def_gid))
268 group_add_member($uid, '', $contact_id, $def_gid);
271 update_contact_avatar($ret['photo'],$uid,$contact_id);
273 // pull feed and consume it, which should subscribe to the hub.
275 proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
277 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
278 WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
282 if (dbm::is_result($r)) {
283 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
285 // create a follow slap
287 $item['verb'] = ACTIVITY_FOLLOW;
288 $item['follow'] = $contact["url"];
289 $slap = ostatus::salmon($item, $r[0]);
290 slapper($r[0], $contact['notify'], $slap);
292 if ($contact['network'] == NETWORK_DIASPORA) {
293 $ret = diaspora::send_share($a->user,$contact);
294 logger('share returns: '.$ret);
298 $result['success'] = true;