]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
event listings
[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($_POST['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         elseif(get_config('system','strict_privacy')) {
38                 unset($ret['notify']);
39         }
40
41         // do we have enough information?
42         
43         if(! ((x($ret,'name')) && (x($ret,'poll')) && ((x($ret,'url')) || (x($ret,'addr'))))) {
44                 notice( t('The profile address specified does not provide adequate information.') . EOL);
45                 if(! x($ret,'poll'))
46                         notice( t('No compatible communication protocols or feeds were discovered.') . EOL);
47                 if(! x($ret,'name'))
48                         notice( t('An author or name was not found.') . EOL);
49                 if(! x($ret,'url'))
50                         notice( t('No browser URL could be matched to this address.') . EOL);
51                 if(strpos($url,'@') !== false)
52                         notice('Unable to match @-style Identity Address with a known protocol or email contact');
53                 goaway($_SESSION['return_url']);
54         }
55
56
57         if(! $ret['notify']) {
58                 notice( t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL);
59         }
60
61         $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
62         if($ret['network'] === NETWORK_MAIL) {
63                 $writeable = 1;
64                 
65         }
66         // check if we already have a contact
67         // the poll url is more reliable than the profile url, as we may have
68         // indirect links or webfinger links
69
70         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
71                 intval(local_user()),
72                 dbesc($ret['poll'])
73         );                      
74
75         if(count($r)) {
76                 // update contact
77                 if($r[0]['rel'] == REL_VIP) {
78                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
79                                 intval(REL_BUD),
80                                 intval($r[0]['id']),
81                                 intval(local_user())
82                         );
83                 }
84         }
85         else {
86                 // create contact record 
87                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `addr`, `alias`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
88                         `writable`, `blocked`, `readonly`, `pending` )
89                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
90                         intval(local_user()),
91                         dbesc(datetime_convert()),
92                         dbesc($ret['url']),
93                         dbesc($ret['addr']),
94                         dbesc($ret['alias']),
95                         dbesc($ret['notify']),
96                         dbesc($ret['poll']),
97                         dbesc($ret['name']),
98                         dbesc($ret['nick']),
99                         dbesc($ret['photo']),
100                         dbesc($ret['network']),
101                         intval(($ret['network'] === NETWORK_MAIL) ? REL_BUD : REL_FAN),
102                         intval($ret['priority']),
103                         intval($writeable)
104                 );
105         }
106
107         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
108                 dbesc($ret['url']),
109                 intval(local_user())
110         );
111
112         if(! count($r)) {
113                 notice( t('Unable to retrieve contact information.') . EOL);
114                 goaway($_SESSION['return_url']);
115                 // NOTREACHED
116         }
117
118         $contact = $r[0];
119         $contact_id  = $r[0]['id'];
120
121         require_once("Photo.php");
122
123         $photos = import_profile_photo($ret['photo'],local_user(),$contact_id);
124
125         $r = q("UPDATE `contact` SET `photo` = '%s', 
126                         `thumb` = '%s',
127                         `micro` = '%s', 
128                         `name-date` = '%s', 
129                         `uri-date` = '%s', 
130                         `avatar-date` = '%s'
131                         WHERE `id` = %d LIMIT 1
132                 ",
133                         dbesc($photos[0]),
134                         dbesc($photos[1]),
135                         dbesc($photos[2]),
136                         dbesc(datetime_convert()),
137                         dbesc(datetime_convert()),
138                         dbesc(datetime_convert()),
139                         intval($contact_id)
140                 );                      
141
142
143         // pull feed and consume it, which should subscribe to the hub.
144
145         proc_run('php',"include/poller.php","$contact_id");
146
147         // create a follow slap
148
149         $tpl = get_markup_template('follow_slap.tpl');
150         $slap = replace_macros($tpl, array(
151                 '$name' => $a->user['username'],
152                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
153                 '$photo' => $a->contact['photo'],
154                 '$thumb' => $a->contact['thumb'],
155                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
156                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
157                 '$title' => '',
158                 '$type' => 'text',
159                 '$content' => t('following'),
160                 '$nick' => $a->user['nickname'],
161                 '$verb' => ACTIVITY_FOLLOW,
162                 '$ostat_follow' => ''
163         ));
164
165         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
166                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
167                         intval(local_user())
168         );
169
170
171         if((count($r)) && (x($contact,'notify')) && (strlen($contact['notify']))) {
172                 require_once('include/salmon.php');
173                 slapper($r[0],$contact['notify'],$slap);
174         }
175
176         goaway($_SESSION['return_url']);
177         // NOTREACHED
178 }