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