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