]> git.mxchange.org Git - friendica.git/blob - include/zotfns.php
fix several probe related issues
[friendica.git] / include / zotfns.php
1 <?php
2
3
4 require_once('include/salmon.php');
5
6 function zot_get($url,$args) {
7         $argstr = '';
8         foreach($args as $k => $v) {
9                 if($argstr)
10                         $argstr .= '&';
11                 $argstr .= $k . '=' . $v;
12         }       
13         $s = fetch_url($url . '?' . $argstr);
14         if($s) {
15                 $j = json_decode($s);
16                 if($j)
17                         return($j);
18         }
19         return false;
20 }
21
22 function zot_post($url,$args) {
23         $s = post_url($url,$args);
24         if($s) {
25                 $j = json_decode($s);
26                 if($j)
27                         return($j);
28         }
29         return false;
30 }
31
32
33 function zot_prv_encode($s,$prvkey) {
34         $x = '';
35         $res = openssl_private_encrypt($s,$x,$prvkey);
36         return base64url_encode($y);
37 }
38 function zot_pub_encode($s,$pubkey) {
39         $x = '';
40         $res = openssl_public_encrypt($s,$x,$pubkey);
41         return base64url_encode($x);
42 }
43
44 function zot_prv_decode($s,$prvkey) {
45         $s = base64url_decode($s);
46         $x = '';
47         openssl_private_decrypt($s,$x,$prvkey);
48         return $x;
49 }
50
51 function zot_pub_decode($s,$pubkey) {
52         $s = base64url_decode($s);
53         $x = '';
54         openssl_public_decrypt($s,$x,$pubkey);
55         return $x;
56 }
57
58
59 function zot_getzid($url,$myaddress,$myprvkey) {
60         $ret = array();
61         $j = zot_get($url,array('sender' => $myaddress));
62         if($j->zid_encoded)
63                 $ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
64         if($j->zkey_encoded)
65                 $ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
66         return $ret;
67 }
68
69 function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
70         $ret = array();
71
72         $zinit = random_string(32);
73
74         $j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
75         
76         $a = get_app();
77         if(! $a->get_curl_code())
78                 return ZCURL_TIMEOUT;
79         if(! $j->zinit) {
80                 logger('zot_post_init: no zinit returned.');
81                 return false;
82         }
83         if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
84                 logger('zot_post_init: incorrect zinit returned.');
85                 return false;
86         }
87
88         if($j->challenge) {
89                 $s = zot_prv_decode($j->challenge,$myprvkey);
90                 $s1 = substr($s,0,strpos($s,'.'));
91                 if($s1 != $zid) {
92                         logger("zot_post_init: incorrect zid returned");
93                         return false;
94                 }
95                 $ret['result'] = substr($s,strpos($s,'.') + 1);
96                 $ret['perms'] = $j->perms;
97         }
98         return $ret;
99 }
100
101
102 function zot_encrypt_data($data,&$key) {
103         $key = random_string();
104         return aes_encrypt($data,$key);
105 }
106
107
108 // encrypt the data prior to calling this function so it only need be done once per message
109 // regardless of the number of recipients.
110
111 function zot_post_data($url,$zid,$myprvkey,$theirpubkey,$encrypted_data,$key, $intro = false) {
112         $i = zot_post_init($url,$zid,$myprvkey,$theirpubkey);
113         if($i === ZCURL_TIMEOUT)
114                 return ZCURL_TIMEOUT;
115
116         if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
117                 return false;
118         if((! stristr($i['perms'],'post')) && ($intro === false)) {
119                 logger("zot_post_data: no permission to post: url=$url zid=$zid");
120                 return false;
121         } 
122         $p = array();
123         $p['zid'] = $zid;
124         $p['result'] = zot_pub_encode($i['result'],$theirpubkey);
125         $p['aes_key'] = zot_prv_encode($key,$myprvkey);
126         $p['data'] = $encrypted_data;
127         $s = zot_post($url,$p);
128         $a = get_app();
129         if(! $a->get_curl_code())
130                 return ZCURL_TIMEOUT;
131
132         if($s) {
133                 $j = json_decode($s); 
134                 return $j;
135         }
136         return false;
137 }
138         
139 function zot_deliver($recipients,$myprvkey,$data) {
140
141         if(is_array($recipients) && count($recipients)) {
142
143                 $key = '';
144                 $encrypted = zot_encrypt_data($data,$key);
145
146
147                 foreach($recipients as $r) {
148                         $result = zot_post_data(
149                                 $r['post'],
150                                 $r['zid'],
151                                 $myprvkey,
152                                 $r['pubkey'],
153                                 $encrypted,
154                                 $key
155                         );
156                         if($result === false) {
157                                 // post failed
158                                 logger('zot_deliver: failed: ' . print_r($r,true));
159                         }
160                         elseif($result === ZCURL_TIMEOUT) {
161                                 // queue for redelivery
162                         }
163                         elseif($result->error) {
164                                 // failed at other end
165                                 logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
166                         }
167                         elseif($result->success) {
168                                 logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
169                         }
170                         else
171                                 logger('zot_deliver: unknown failure.');
172                 }
173         }
174 }
175
176
177 function zot_new_contact($user,$cc) {
178
179         $zid = random_string(32);
180         $zkey = random_string(32);
181
182         logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));
183
184         $ret = array();
185         $ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
186         $ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);
187         return $ret;
188
189
190         
191
192
193 }