4 use Friendica\Core\System;
6 require_once 'include/probe.php';
7 require_once 'include/socgraph.php';
8 require_once 'include/group.php';
9 require_once 'include/salmon.php';
10 require_once 'include/ostatus.php';
11 require_once 'include/Photo.php';
12 require_once 'include/diaspora.php';
14 function update_contact($id) {
16 Warning: Never ever fetch the public key via probe_url and write it into the contacts.
17 This will reliably kill your communication with Friendica contacts.
20 $r = q("SELECT `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `network` FROM `contact` WHERE `id` = %d", intval($id));
24 $ret = probe_url($r[0]["url"]);
26 // If probe_url fails the network code will be different
27 if ($ret["network"] != $r[0]["network"])
32 // make sure to not overwrite existing values with blank entries
33 foreach ($ret AS $key => $val) {
34 if (isset($r[0][$key]) && ($r[0][$key] != "") && ($val == ""))
35 $ret[$key] = $r[0][$key];
37 if (isset($r[0][$key]) && ($ret[$key] != $r[0][$key]))
44 q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s' WHERE `id` = %d",
46 dbesc(normalise_link($ret['url'])),
50 dbesc($ret['notify']),
56 // Update the corresponding gcontact entry
57 poco_last_updated($ret["url"]);
63 // Takes a $uid and a url/handle and adds a new contact
64 // Currently if the contact is DFRN, interactive needs to be true, to redirect to the
67 // Otherwise this can be used to bulk add statusnet contacts, twitter contacts, etc.
69 // $return['success'] boolean true if successful
70 // $return['message'] error text if success is false.
74 function new_contact($uid,$url,$interactive = false) {
76 $result = array('cid' => -1, 'success' => false,'message' => '');
80 // remove ajax junk, e.g. Twitter
82 $url = str_replace('/#!/','/',$url);
84 if (! allowed_url($url)) {
85 $result['message'] = t('Disallowed profile URL.');
89 if (blocked_url($url)) {
90 $result['message'] = t('Blocked domain');
95 $result['message'] = t('Connect URL missing.');
99 $arr = array('url' => $url, 'contact' => array());
101 call_hooks('follow', $arr);
103 if (x($arr['contact'],'name')) {
104 $ret = $arr['contact'];
107 $ret = probe_url($url);
110 if ($ret['network'] === NETWORK_DFRN) {
112 if (strlen($a->path)) {
113 $myaddr = bin2hex(App::get_baseurl() . '/profile/' . $a->user['nickname']);
115 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
118 goaway($ret['request'] . "&addr=$myaddr");
122 } elseif (get_config('system','dfrn_only')) {
123 $result['message'] = t('This site is not configured to allow communications with other networks.') . EOL;
124 $result['message'] != t('No compatible communication protocols or feeds were discovered.') . EOL;
128 // This extra param just confuses things, remove it
129 if ($ret['network'] === NETWORK_DIASPORA) {
130 $ret['url'] = str_replace('?absolute=true','',$ret['url']);
133 // do we have enough information?
135 if (! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
136 $result['message'] .= t('The profile address specified does not provide adequate information.') . EOL;
137 if (! x($ret,'poll')) {
138 $result['message'] .= t('No compatible communication protocols or feeds were discovered.') . EOL;
140 if (! x($ret,'name')) {
141 $result['message'] .= t('An author or name was not found.') . EOL;
143 if (! x($ret,'url')) {
144 $result['message'] .= t('No browser URL could be matched to this address.') . EOL;
146 if (strpos($url,'@') !== false) {
147 $result['message'] .= t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
148 $result['message'] .= t('Use mailto: in front of address to force email check.') . EOL;
153 if ($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
154 $result['message'] .= t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
158 if (! $ret['notify']) {
159 $result['message'] .= t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
162 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
164 $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
166 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
168 if (in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) {
172 // check if we already have a contact
173 // the poll url is more reliable than the profile url, as we may have
174 // indirect links or webfinger links
176 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
179 dbesc(normalise_link($ret['poll'])),
180 dbesc($ret['network'])
183 if (!dbm::is_result($r))
184 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' LIMIT 1",
185 intval($uid), dbesc(normalise_link($url)), dbesc($ret['network'])
188 if (dbm::is_result($r)) {
190 if ($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
191 q("UPDATE `contact` SET `rel` = %d , `subhub` = %d, `readonly` = 0 WHERE `id` = %d AND `uid` = %d",
192 intval(CONTACT_IS_FRIEND),
199 // check service class limits
201 $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `pending` = 0 AND `self` = 0",
204 if (dbm::is_result($r))
205 $total_contacts = $r[0]['total'];
207 if (! service_class_allows($uid,'total_contacts',$total_contacts)) {
208 $result['message'] .= upgrade_message();
212 $r = q("SELECT COUNT(`network`) AS `total` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND `pending` = 0 AND `self` = 0",
216 if (dbm::is_result($r)) {
217 $total_network = $r[0]['total'];
220 if (! service_class_allows($uid,'total_contacts_' . $network,$total_network)) {
221 $result['message'] .= upgrade_message();
225 $new_relation = ((in_array($ret['network'], array(NETWORK_MAIL, NETWORK_DIASPORA))) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
227 // create contact record
228 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `network`, `pubkey`, `rel`, `priority`,
229 `writable`, `hidden`, `blocked`, `readonly`, `pending`, `subhub` )
230 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0, %d ) ",
232 dbesc(datetime_convert()),
234 dbesc(normalise_link($ret['url'])),
236 dbesc($ret['alias']),
237 dbesc($ret['batch']),
238 dbesc($ret['notify']),
243 dbesc($ret['network']),
244 dbesc($ret['pubkey']),
245 intval($new_relation),
246 intval($ret['priority']),
253 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1",
255 dbesc($ret['network']),
259 if (! dbm::is_result($r)) {
260 $result['message'] .= t('Unable to retrieve contact information.') . EOL;
265 $contact_id = $r[0]['id'];
266 $result['cid'] = $contact_id;
268 $def_gid = get_default_group($uid, $contact["network"]);
269 if (intval($def_gid)) {
270 group_add_member($uid, '', $contact_id, $def_gid);
274 update_contact_avatar($ret['photo'],$uid,$contact_id);
276 // pull feed and consume it, which should subscribe to the hub.
278 proc_run(PRIORITY_HIGH, "include/onepoll.php", $contact_id, "force");
280 $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
281 WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
285 if (dbm::is_result($r)) {
286 if (($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
287 // create a follow slap
289 $item['verb'] = ACTIVITY_FOLLOW;
290 $item['follow'] = $contact["url"];
291 $slap = ostatus::salmon($item, $r[0]);
292 slapper($r[0], $contact['notify'], $slap);
295 if ($contact['network'] == NETWORK_DIASPORA) {
296 $ret = Diaspora::send_share($a->user,$contact);
297 logger('share returns: '.$ret);
301 $result['success'] = true;