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