]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
add connect link to friend suggest
[friendica.git] / mod / follow.php
1 <?php
2
3 require_once('Scrape.php');
4
5 function follow_post(&$a) {
6
7         if(! local_user()) {
8                 notice( t('Permission denied.') . EOL);
9                 goaway($_SESSION['return_url']);
10                 // NOTREACHED
11         }
12
13         $url = $orig_url = notags(trim($_REQUEST['url']));
14
15         // remove ajax junk, e.g. Twitter
16
17         $url = str_replace('/#!/','/',$url);
18
19         if(! allowed_url($url)) {
20                 notice( t('Disallowed profile URL.') . EOL);
21                 goaway($_SESSION['return_url']);
22                 // NOTREACHED
23         }
24
25         $ret = probe_url($url);
26
27         if($ret['network'] === NETWORK_DFRN) {
28                 if(strlen($a->path))
29                         $myaddr = bin2hex($a->get_baseurl() . '/profile/' . $a->user['nickname']);
30                 else
31                         $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
32  
33                 goaway($ret['request'] . "&addr=$myaddr");
34                 
35                 // NOTREACHED
36         }
37         else {
38                 if(get_config('system','dfrn_only')) {
39                         notice( t('This site is not configured to allow communications with other networks.') . EOL);
40                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
41                         goaway($_SESSION['return_url']);
42                 }
43         }
44
45         // do we have enough information?
46         
47         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
48                 notice( t('The profile address specified does not provide adequate information.') . EOL);
49                 if(! x($ret,'poll'))
50                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
51                 if(! x($ret,'name'))
52                         notice( t('An author or name was not found.') . EOL);
53                 if(! x($ret,'url'))
54                         notice( t('No browser URL could be matched to this address.') . EOL);
55                 if(strpos($url,'@') !== false)
56                         notice('Unable to match @-style Identity Address with a known protocol or email contact');
57                 goaway($_SESSION['return_url']);
58         }
59
60         if($ret['network'] === NETWORK_OSTATUS && get_config('system','ostatus_disabled')) {
61                 notice( t('The profile address specified belongs to a network which has been disabled on this site.') . EOL);
62                 $ret['notify'] = '';
63         }
64
65         if(! $ret['notify']) {
66                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
67         }
68
69         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
70         $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
71
72         if($ret['network'] === NETWORK_MAIL) {
73                 $writeable = 1;
74                 
75         }
76         if($ret['network'] === NETWORK_DIASPORA)
77                 $writeable = 1;
78
79         // check if we already have a contact
80         // the poll url is more reliable than the profile url, as we may have
81         // indirect links or webfinger links
82
83         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
84                 intval(local_user()),
85                 dbesc($ret['poll'])
86         );                      
87
88         if(count($r)) {
89                 // update contact
90                 if($r[0]['rel'] == CONTACT_IS_FOLLOWER || ($network === NETWORK_DIASPORA && $r[0]['rel'] == CONTACT_IS_SHARING)) {
91                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
92                                 intval(CONTACT_IS_FRIEND),
93                                 intval($r[0]['id']),
94                                 intval(local_user())
95                         );
96                 }
97         }
98         else {
99
100                 $new_relation = (($ret['network'] === NETWORK_MAIL) ? CONTACT_IS_FRIEND : CONTACT_IS_SHARING);
101                 if($ret['network'] === NETWORK_DIASPORA)
102                         $new_relation = CONTACT_IS_FOLLOWER;
103
104                 // create contact record 
105                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `batch`, `notify`, `poll`, `poco`, `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
106                         `writable`, `hidden`, `blocked`, `readonly`, `pending` )
107                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, 0, 0, 0 ) ",
108                         intval(local_user()),
109                         dbesc(datetime_convert()),
110                         dbesc($ret['url']),
111                         dbesc(normalise_link($ret['url'])),
112                         dbesc($ret['addr']),
113                         dbesc($ret['alias']),
114                         dbesc($ret['batch']),
115                         dbesc($ret['notify']),
116                         dbesc($ret['poll']),
117                         dbesc($ret['poco']),
118                         dbesc($ret['name']),
119                         dbesc($ret['nick']),
120                         dbesc($ret['photo']),
121                         dbesc($ret['network']),
122                         dbesc($ret['pubkey']),
123                         intval($new_relation),
124                         intval($ret['priority']),
125                         intval($writeable),
126                         intval($hidden)
127                 );
128         }
129
130         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
131                 dbesc($ret['url']),
132                 intval(local_user())
133         );
134
135         if(! count($r)) {
136                 notice( t('Unable to retrieve contact information.') . EOL);
137                 goaway($_SESSION['return_url']);
138                 // NOTREACHED
139         }
140
141         $contact = $r[0];
142         $contact_id  = $r[0]['id'];
143
144         require_once("Photo.php");
145
146         $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
147
148         $r = q("UPDATE `contact` SET `photo` = '%s', 
149                         `thumb` = '%s',
150                         `micro` = '%s', 
151                         `name-date` = '%s', 
152                         `uri-date` = '%s', 
153                         `avatar-date` = '%s'
154                         WHERE `id` = %d LIMIT 1
155                 ",
156                         dbesc($photos[0]),
157                         dbesc($photos[1]),
158                         dbesc($photos[2]),
159                         dbesc(datetime_convert()),
160                         dbesc(datetime_convert()),
161                         dbesc(datetime_convert()),
162                         intval($contact_id)
163                 );                      
164
165
166         // pull feed and consume it, which should subscribe to the hub.
167
168         proc_run('php',"include/poller.php","$contact_id");
169
170         // create a follow slap
171
172         $tpl = get_markup_template('follow_slap.tpl');
173         $slap = replace_macros($tpl, array(
174                 '$name' => $a->user['username'],
175                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
176                 '$photo' => $a->contact['photo'],
177                 '$thumb' => $a->contact['thumb'],
178                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
179                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
180                 '$title' => '',
181                 '$type' => 'text',
182                 '$content' => t('following'),
183                 '$nick' => $a->user['nickname'],
184                 '$verb' => ACTIVITY_FOLLOW,
185                 '$ostat_follow' => ''
186         ));
187
188         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
189                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
190                         intval(local_user())
191         );
192
193         if(count($r)) {
194                 if(($contact['network'] == NETWORK_OSTATUS) && (strlen($contact['notify']))) {
195                         require_once('include/salmon.php');
196                         slapper($r[0],$contact['notify'],$slap);
197                 }
198                 if($contact['network'] == NETWORK_DIASPORA) {
199                         require_once('include/diaspora.php');
200                         $ret = diaspora_share($a->user,$contact);
201                         logger('mod_follow: diaspora_share returns: ' . $ret);
202                 }
203         }
204
205         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
206 //      goaway($_SESSION['return_url']);
207         // NOTREACHED
208 }