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