]> git.mxchange.org Git - friendica.git/blob - mod/dfrn_poll.php
working on readonly contact attribute
[friendica.git] / mod / dfrn_poll.php
1 <?php
2
3 require_once('include/items.php');
4 require_once('include/auth.php');
5
6
7 function dfrn_poll_init(&$a) {
8
9         if(x($_GET,'dfrn_id'))
10                 $dfrn_id = $a->config['dfrn_poll_dfrn_id'] = $_GET['dfrn_id'];
11         if(x($_GET,'type'))
12                 $type = $a->config['dfrn_poll_type'] = $_GET['type'];
13         if(x($_GET,'last_update'))
14                 $last_update = $a->config['dfrn_poll_last_update'] = $_GET['last_update'];
15
16
17
18         if(! x($dfrn_id))
19                 return;
20
21
22         if(($dfrn_id == '*') && ($a->argc > 1) && (intval($a->argv[1]))) {
23                 $o = get_feed_for($a,'*', $a->argv[1],$last_update);
24                 echo $o;
25                 killme();
26         }
27
28         if((x($type)) && ($type == 'profile')) {
29
30                 $r = q("SELECT `contact`.*, `user`.`nickname` 
31                         FROM `contact` LEFT JOIN `user` ON `contact`.`uid` = `user`.`uid`
32                         WHERE `issued-id` = '%s' LIMIT 1",
33                         dbesc($dfrn_id));
34                 if(count($r)) {
35                         $s = fetch_url($r[0]['poll'] . '?dfrn_id=' . $dfrn_id . '&type=profile-check');
36                         if(strlen($s)) {
37                                 $xml = simplexml_load_string($s);
38                                 if((int) $xml->status == 1) {
39                                         $_SESSION['authenticated'] = 1;
40                                         $_SESSION['visitor_id'] = $r[0]['id'];
41                                         $_SESSION['sysmsg'] .= "Hi {$r[0]['name']}" . EOL;
42                                         // Visitors get 1 day session.
43                                         $session_id = session_id();
44                                         $expire = time() + 86400;
45                                         q("UPDATE `session` SET `expire` = '%s' WHERE `sid` = '%s' LIMIT 1",
46                                                 dbesc($expire),
47                                                 dbesc($session_id)); 
48                                 }
49                         }
50                         $profile = ((strlen($r[0]['nickname'])) ? $r[0]['nickname'] : $r[0]['uid']);
51                         goaway($a->get_baseurl() . "/profile/$profile/visit");
52                 }
53                 goaway($a->get_baseurl());
54         }
55
56         if((x($type)) && ($type == 'profile-check')) {
57
58                 q("DELETE FROM `profile_check` WHERE `expire` < " . intval(time()));
59                 $r = q("SELECT * FROM `profile_check` WHERE `dfrn_id` = '%s' ORDER BY `expire` DESC",
60                         dbesc($dfrn_id));
61                 if(count($r))
62                         xml_status(1);
63                 xml_status(0);
64                 return; // NOTREACHED
65         }
66
67
68 }
69
70
71
72 function dfrn_poll_post(&$a) {
73
74         $dfrn_id = notags(trim($_POST['dfrn_id']));
75         $challenge = notags(trim($_POST['challenge']));
76         $url = $_POST['url'];
77
78         $r = q("SELECT * FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
79                 dbesc($dfrn_id),
80                 dbesc($challenge)
81         );
82         if(! count($r))
83                 killme();
84
85         $type = $r[0]['type'];
86         $last_update = $r[0]['last_update'];
87
88         $r = q("DELETE FROM `challenge` WHERE `dfrn-id` = '%s' AND `challenge` = '%s' LIMIT 1",
89                 dbesc($dfrn_id),
90                 dbesc($challenge)
91         );
92
93
94         $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' LIMIT 1",
95                 dbesc($dfrn_id)
96         );
97         if(! count($r))
98                 killme();
99
100         $owner_uid = $r[0]['uid'];
101         $contact_id = $r[0]['id']; 
102
103
104         if($type == 'reputation' && strlen($url)) {
105                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
106                         dbesc($url),
107                         intval($owner_uid)
108                 );
109                 $reputation = 0;
110                 $text = '';
111
112                 if(count($r)) {
113                         $reputation = $r[0]['rating'];
114                         $text = $r[0]['reason'];
115
116                         if($r[0]['id'] == $contact_id) {        // inquiring about own reputation not allowed
117                                 $reputation = 0;
118                                 $text = '';
119                         }
120                 }
121
122                 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
123                 <reputation>
124                         <url>$url</url>
125                         <rating>$reputation</rating>
126                         <description>$text</description>
127                 </reputation>
128                 ";
129                 killme();
130                 return; // NOTREACHED
131         }
132         else {
133
134                 $o = get_feed_for($a,$dfrn_id, $a->argv[1], $last_update);
135                 echo $o;
136                 killme();
137
138         }
139 }
140
141 function dfrn_poll_content(&$a) {
142
143         if(x($_GET,'dfrn_id'))
144                 $dfrn_id = $a->config['dfrn_poll_dfrn_id'] = $_GET['dfrn_id'];
145         if(x($_GET,'type'))
146                 $type = $a->config['dfrn_poll_type'] = $_GET['type'];
147         if(x($_GET,'last_update'))
148                 $last_update = $a->config['dfrn_poll_last_update'] = $_GET['last_update'];
149
150
151         if($dfrn_id != '*') {
152                 // initial communication from external contact
153                 $hash = random_string();
154
155                 $status = 0;
156
157                 $r = q("DELETE FROM `challenge` WHERE `expire` < " . intval(time()));
158
159                 $r = q("INSERT INTO `challenge` ( `challenge`, `dfrn-id`, `expire` , `type`, `last_update` )
160                         VALUES( '%s', '%s', '%s', '%s', '%s' ) ",
161                         dbesc($hash),
162                         dbesc(notags(trim($_GET['dfrn_id']))),
163                         intval(time() + 60 ),
164                         dbesc($type),
165                         dbesc($last_update)
166                 );
167
168                 $r = q("SELECT * FROM `contact` WHERE `issued-id` = '%s' AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
169                         dbesc($_GET['dfrn_id']));
170                 if((! count($r)) || (! strlen($r[0]['prvkey'])))
171                         $status = 1;
172
173                 $challenge = '';
174
175                 openssl_private_encrypt($hash,$challenge,$r[0]['prvkey']);
176                 $challenge = bin2hex($challenge);
177                 echo '<?xml version="1.0" encoding="UTF-8"?><dfrn_poll><status>' .$status . '</status><dfrn_id>' . $_GET['dfrn_id'] . '</dfrn_id>'
178                         . '<challenge>' . $challenge . '</challenge></dfrn_poll>' . "\r\n" ;
179                 session_write_close();
180                 exit;           
181         }
182
183
184
185
186
187 }
188
189