2 require_once("include/Scrape.php");
4 function update_contact($id) {
6 Warning: Never ever fetch the public key via probe_url and write it into the contacts.
7 This will reliably kill your communication with Friendica contacts.
10 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
14 $ret = probe_url($r[0]["url"]);
16 // If probe_url fails the network code will be different
17 if ($ret["network"] != $r[0]["network"])
22 // make sure to not overwrite existing values with blank entries
23 foreach ($ret AS $key => $val) {
24 if (isset($r[0][$key]) AND ($r[0][$key] != "") AND ($val == ""))
25 $ret[$key] = $r[0][$key];
27 if (isset($r[0][$key]) AND ($ret[$key] != $r[0][$key]))
34 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
36 dbesc(normalise_link($ret['url'])),
40 dbesc($ret['notify']),
50 // Takes a $uid and a url/handle and adds a new contact
51 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
54 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
56 // $return['success'] boolean true if successful
57 // $return['message'] error text if success is false.
61 function new_contact($uid,$url,$interactive = false) {
63 $result = array('cid' => -1, 'success' => false,'message' => '');
67 // remove ajax junk, e.g. Twitter
69 $url = str_replace('/#!/','/',$url);
71 if(! allowed_url($url)) {
72 $result['message'] = t('Disallowed profile URL.');
77 $result['message'] = t('Connect URL missing.');
81 $arr = array('url' => $url, 'contact' => array());
83 call_hooks('follow', $arr);
85 if(x($arr['contact'],'name'))
86 $ret = $arr['contact'];
88 $ret = probe_url($url);
90 if($ret['network'] === NETWORK_DFRN) {
93 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
95 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
97 goaway($ret['request'] . "&addr=$myaddr");
103 if(get_config('system','dfrn_only')) {
104 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
105 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
115 // This extra param just confuses things, remove it
116 if($ret['network'] === NETWORK_DIASPORA)
117 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
120 // do we have enough information?
122 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
123 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
125 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
127 $result['message'] .= t('An author or name was not found.') . EOL;
129 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
130 if(strpos($url,'@') !== false) {
131 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
132 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
137 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
138 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
147 if(! $ret['notify']) {
148 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
151 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
153 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
155 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
157 if(in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA)))
160 // check if we already have a contact
161 // the poll url is more reliable than the profile url, as we may have
162 // indirect links or webfinger links
164 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
167 dbesc(normalise_link($ret['poll'])),
168 dbesc($ret['network'])
172 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
173 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
178 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
179 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
180 intval(CONTACT_IS_FRIEND),
189 // check service class limits
191 $r = q("select count(*) as total from contact where uid = %d and pending = 0 and self = 0",
195 $total_contacts = $r[0]['total'];
197 if(! service_class_allows($uid,'total_contacts',$total_contacts)) {
198 $result['message'] .= upgrade_message();
202 $r = q("select count(network) as total from contact where uid = %d and network = '%s' and pending = 0 and self = 0",
207 $total_network = $r[0]['total'];
209 if(! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
210 $result['message'] .= upgrade_message();
214 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
216 // create contact record
217 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
218 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
219 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
221 dbesc(datetime_convert()),
223 dbesc(normalise_link($ret['url'])),
225 dbesc($ret['alias']),
226 dbesc($ret['batch']),
227 dbesc($ret['notify']),
232 dbesc($ret['network']),
233 dbesc($ret['pubkey']),
234 intval($new_relation),
235 intval($ret['priority']),
242 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
244 dbesc($ret['network']),
249 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
254 $contact_id = $r[0]['id'];
255 $result['cid'] = $contact_id;
257 $g = q("select def_gid from user where uid = %d limit 1",
260 if($g && intval($g[0]['def_gid'])) {
261 require_once('include/group.php');
262 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
265 require_once("include/Photo.php");
267 $photos = import_profile_photo($ret['photo'],$uid,$contact_id);
269 $r = q("UPDATE `contact` SET `photo` = '%s',
279 dbesc(datetime_convert()),
280 dbesc(datetime_convert()),
281 dbesc(datetime_convert()),
286 // pull feed and consume it, which should subscribe to the hub.
288 proc_run('php',"include/onepoll.php","$contact_id", "force");
290 // create a follow slap
292 $tpl = get_markup_template('follow_slap.tpl');
293 $slap = replace_macros($tpl, array(
294 '$name' => $a->user['username'],
295 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
296 '$photo' => $a->contact['photo'],
297 '$thumb' => $a->contact['thumb'],
298 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
299 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . get_guid(32),
302 '$content' => t('following'),
303 '$nick' => $a->user['nickname'],
304 '$verb' => ACTIVITY_FOLLOW,
305 '$ostat_follow' => ''
308 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
309 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
314 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
315 require_once('include/salmon.php');
316 slapper($r[0],$contact['notify'],$slap);
318 if($contact['network'] == NETWORK_DIASPORA) {
319 require_once('include/diaspora.php');
320 $ret = diaspora_share($a->user,$contact);
321 logger('mod_follow: diaspora_share returns: ' . $ret);
325 $result['success'] = true;