3 require_once('Scrape.php');
5 function follow_init(&$a) {
8 notice( t('Permission denied.') . EOL);
9 goaway($_SESSION['return_url']);
13 $url = $orig_url = notags(trim($_REQUEST['url']));
15 // remove ajax junk, e.g. Twitter
17 $url = str_replace('/#!/','/',$url);
19 if(! allowed_url($url)) {
20 notice( t('Disallowed profile URL.') . EOL);
21 goaway($_SESSION['return_url']);
27 notice( t('Connect URL missing.') . EOL);
28 goaway($_SESSION['return_url']);
33 $ret = probe_url($url);
35 if($ret['network'] === NETWORK_DFRN) {
37 $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
39 $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
41 goaway($ret['request'] . "&addr=$myaddr");
46 if(get_config('system','dfrn_only')) {
47 notice( t('This site is not configured to allow communications with other networks.') . EOL);
48 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
49 goaway($_SESSION['return_url']);
53 // do we have enough information?
55 if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
56 notice( t('The profile address specified does not provide adequate information.') . EOL);
58 notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
60 notice( t('An author or name was not found.') . EOL);
62 notice( t('No browser URL could be matched to this address.') . EOL);
63 if(strpos($url,'@') !== false)
64 notice('Unable to match @-style Identity Address with a known protocol or email contact');
65 goaway($_SESSION['return_url']);
68 if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
69 notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
73 if(! $ret['notify']) {
74 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
77 $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
78 $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
80 if($ret['network'] === NETWORK_MAIL) {
84 if($ret['network'] === NETWORK_DIASPORA)
87 // check if we already have a contact
88 // the poll url is more reliable than the profile url, as we may have
89 // indirect links or webfinger links
91 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
98 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
99 q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
100 intval(CONTACT_IS_FRIEND),
108 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
109 if($ret['network'] === NETWORK_DIASPORA)
110 $new_relation = CONTACT_IS_FOLLOWER;
112 // create contact record
113 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
114 `writable`, `hidden`, `blocked`, `readonly`, `pending` )
115 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
116 intval(local_user()),
117 dbesc(datetime_convert()),
119 dbesc(normalise_link($ret['url'])),
121 dbesc($ret['alias']),
122 dbesc($ret['batch']),
123 dbesc($ret['notify']),
128 dbesc($ret['photo']),
129 dbesc($ret['network']),
130 dbesc($ret['pubkey']),
131 intval($new_relation),
132 intval($ret['priority']),
138 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
144 notice( t('Unable to retrieve contact information.') . EOL);
145 goaway($_SESSION['return_url']);
150 $contact_id = $r[0]['id'];
152 require_once("Photo.php");
154 $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
156 $r = q("UPDATE `contact` SET `photo` = '%s',
162 WHERE `id` = %d LIMIT 1
167 dbesc(datetime_convert()),
168 dbesc(datetime_convert()),
169 dbesc(datetime_convert()),
174 // pull feed and consume it, which should subscribe to the hub.
176 proc_run('php',"include/poller.php","$contact_id");
178 // create a follow slap
180 $tpl = get_markup_template('follow_slap.tpl');
181 $slap = replace_macros($tpl, array(
182 '$name' => $a->user['username'],
183 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
184 '$photo' => $a->contact['photo'],
185 '$thumb' => $a->contact['thumb'],
186 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
187 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
190 '$content' => t('following'),
191 '$nick' => $a->user['nickname'],
192 '$verb' => ACTIVITY_FOLLOW,
193 '$ostat_follow' => ''
196 $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
197 WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
202 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
203 require_once('include/salmon.php');
204 slapper($r[0],$contact['notify'],$slap);
206 if($contact['network'] == NETWORK_DIASPORA) {
207 require_once('include/diaspora.php');
208 $ret = diaspora_share($a->user,$contact);
209 logger('mod_follow: diaspora_share returns: ' . $ret);
213 if(strstr($_SESSION['return_url'],'contacts'))
214 goaway($a->get_baseurl() . '/contacts/' . $contact_id);
216 goaway($_SESSION['return_url']);