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