]> git.mxchange.org Git - friendica.git/blob - mod/follow.php
add micro profile photo
[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 = notags(trim($_POST['url']));
14
15         if($url) {
16                 $links = lrdd($url);
17                 if(count($links)) {
18                         foreach($links as $link) {
19                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
20                                         $dfrn = $link['@attributes']['href'];
21                                 if($link['@attributes']['rel'] === 'salmon')
22                                         $notify = $link['@attributes']['href'];
23                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
24                                         $poll = $link['@attributes']['href'];
25                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
26                                         $hcard = $link['@attributes']['href'];
27                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
28                                         $profile = $link['@attributes']['href'];
29
30                         }
31                 }
32         }       
33
34         // If we find a DFRN site, send our subscriber to the other person's
35         // dfrn_request page and all the other details will get sorted.
36
37         if(strlen($dfrn)) {
38                 $ret = scrape_dfrn($dfrn);
39                 if(is_array($ret) && x($ret,'dfrn-request')) {
40                         if(strlen($a->path))
41                                 $myaddr = urlencode($a->get_baseurl() . '/profile/' . $a->user['nickname']);
42                         else
43                                 $myaddr = urlencode($a->user['nickname'] . '@' . $a->get_hostname());
44  
45                         goaway($ret['dfrn-request'] . "&address=$myaddr");
46                 
47                         // NOTREACHED
48                 }
49         }
50
51         if($hcard) {
52                 $vcard = scrape_vcard($hcard);
53         }
54
55         if(! $profile)
56                 $profile = $url;
57
58         // do we have enough information?
59
60         if(! x($vcard,'fn'))
61                 if(x($vcard,'nick'))
62                         $vcard['fn'] = $vcard['nick'];
63
64         if(! ((x($vcard['fn'])) && ($poll) && ($notify) && ($profile))) {
65                 notice( t('The profile address specified does not provide adequate information.') . EOL);
66                 goaway($_SESSION['return_url']);
67         } 
68
69         if(! x($vcard,'photo'))
70                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
71
72         // check if we already have a contact
73         // the poll url is more reliable than the profile url, as we may have
74         // indirect links or webfinger links
75
76         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' LIMIT 1",
77                 intval(local_user()),
78                 dbesc($poll)
79         );                      
80         if(count($r)) {
81                 // update contact
82                 if($r[0]['rel'] == REL_VIP) {
83                         q("UPDATE `contact` SET `rel` = %d , `readonly` = 0 WHERE `id` = %d AND `uid` = %d LIMIT 1",
84                                 intval(REL_BUD),
85                                 intval($r[0]['id']),
86                                 intval(local_user())
87                         );
88                 }
89         }
90         else {
91                 // create contact record 
92                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `notify`, `poll`, `name`, `nick`, `photo`, `network`, `rel`, 
93                         `blocked`, `readonly`, `pending` )
94                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 0 ) ",
95                         intval(local_user()),
96                         dbesc(datetime_convert()),
97                         dbesc($profile),
98                         dbesc($notify),
99                         dbesc($poll),
100                         dbesc($vcard['fn']),
101                         dbesc($vcard['nick']),
102                         dbesc($vcard['photo']),
103                         dbesc('stat'),
104                         intval(REL_FAN)
105                 );
106         }
107         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
108                 dbesc($profile),
109                 intval(local_user())
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($vcard['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
145         // create a follow slap
146
147         $tpl = load_view_file('view/follow_slap.tpl');
148         $slap = replace_macros($tpl, array(
149                 '$name' => $a->user['username'],
150                 '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
151                 '$photo' => $a->contact['photo'],
152                 '$thumb' => $a->contact['thumb'],
153                 '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
154                 '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':follow:' . random_string(),
155                 '$title' => '',
156                 '$type' => 'text',
157                 '$content' => t('following'),
158                 '$nick' => $a->user['nickname'],
159                 '$verb' => ACTIVITY_FOLLOW
160         ));
161
162         $r = q("SELECT `contact`.*, `user`.* FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid` 
163                         WHERE `user`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
164                         intval(local_user())
165         );
166
167         require_once('include/salmon.php');
168         slapper($r[0],$contact['notify'],$slap);
169
170         goaway($_SESSION['return_url']);
171         // NOTREACHED
172 }