3 function zot_get($url,$args) {
5 foreach($args as $k => $v) {
8 $argstr .= $k . '=' . $v;
10 $s = fetch_url($url . '?' . $argstr);
19 function zot_post($url,$args) {
20 $s = post_url($url,$args);
30 function zot_prv_encode($s,$prvkey) {
32 $res = openssl_private_encrypt($s,$x,$prvkey);
33 return base64url_encode($y);
35 function zot_pub_encode($s,$pubkey) {
37 $res = openssl_public_encrypt($s,$x,$pubkey);
38 return base64url_encode($x);
41 function zot_prv_decode($s,$prvkey) {
42 $s = base64url_decode($s);
44 openssl_private_decrypt($s,$x,$prvkey);
48 function zot_pub_decode($s,$pubkey) {
49 $s = base64url_decode($s);
51 openssl_public_decrypt($s,$x,$pubkey);
56 function zot_getzid($url,$myaddress,$myprvkey) {
58 $j = zot_get($url,array('sender' => $myaddress));
60 $ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
62 $ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
66 function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
69 $zinit = random_string(32);
71 $j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
74 if(! $a->get_curl_code())
77 logger('zot_post_init: no zinit returned.');
80 if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
81 logger('zot_post_init: incorrect zinit returned.');
86 $s = zot_prv_decode($j->challenge,$myprvkey);
87 $s1 = substr($s,0,strpos($s,'.'));
89 logger("zot_post_init: incorrect zid returned");
92 $ret['result'] = substr($s,strpos($s,'.') + 1);
93 $ret['perms'] = $j->perms;
99 function zot_encrypt_data($data,&$key) {
100 $key = random_string();
101 return aes_encrypt($data,$key);
105 // encrypt the data prior to calling this function so it only need be done once per message
106 // regardless of the number of recipients.
108 function zot_post_data($url,$zid,$myprvkey,$theirpubkey,$encrypted_data,$key, $intro = false) {
109 $i = zot_post_init($url,$zid,$myprvkey,$theirpubkey);
110 if($i === ZCURL_TIMEOUT)
111 return ZCURL_TIMEOUT;
113 if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
115 if((! stristr($i['perms'],'post')) && ($intro === false)) {
116 logger("zot_post_data: no permission to post: url=$url zid=$zid");
121 $p['result'] = zot_pub_encode($i['result'],$theirpubkey);
122 $p['aes_key'] = zot_prv_encode($key,$myprvkey);
123 $p['data'] = $encrypted_data;
124 $s = zot_post($url,$p);
126 if(! $a->get_curl_code())
127 return ZCURL_TIMEOUT;
130 $j = json_decode($s);
136 function zot_deliver($recipients,$myprvkey,$data) {
138 if(is_array($recipients) && count($recipients)) {
141 $encrypted = zot_encrypt_data($data,$key);
144 foreach($recipients as $r) {
145 $result = zot_post_data(
153 if($result === false) {
155 logger('zot_deliver: failed: ' . print_r($r,true));
157 elseif($result === ZCURL_TIMEOUT) {
158 // queue for redelivery
160 elseif($result->error) {
161 // failed at other end
162 logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
164 elseif($result->success) {
165 logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
168 logger('zot_deliver: unknown failure.');
174 function zot_new_contact($user,$cc) {
176 $zid = random_string(32);
177 $zkey = random_string(32);
179 logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));
182 $ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
183 $ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);