3 require_once('include/text.php');
5 function zot_get($url,$args) {
7 foreach($args as $k => $v) {
10 $argstr .= $k . '=' . $v;
12 $s = fetch_url($url . '?' . $argstr);
21 function zot_post($url,$args) {
22 $s = post_url($url,$args);
32 function zot_prv_encode($s,$prvkey) {
34 $res = openssl_private_encrypt($s,$x,$prvkey);
35 return base64url_encode($y);
37 function zot_pub_encode($s,$pubkey) {
39 $res = openssl_public_encrypt($s,$x,$pubkey);
40 return base64url_encode($x);
43 function zot_prv_decode($s,$prvkey) {
44 $s = base64url_decode($s);
46 openssl_private_decrypt($s,$x,$prvkey);
50 function zot_pub_decode($s,$pubkey) {
51 $s = base64url_decode($s);
53 openssl_public_decrypt($s,$x,$pubkey);
58 function zot_getzid($url,$myaddress,$myprvkey) {
60 $j = zot_get($url,array('sender' => $myaddress));
62 $ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
64 $ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
68 function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
71 $zinit = random_string(32);
73 $j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
76 if(! $a->get_curl_code())
79 logger('zot_post_init: no zinit returned.');
82 if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
83 logger('zot_post_init: incorrect zinit returned.');
88 $s = zot_prv_decode($j->challenge,$myprvkey);
89 $s1 = substr($s,0,strpos($s,'.'));
91 logger("zot_post_init: incorrect zid returned");
94 $ret['result'] = substr($s,strpos($s,'.') + 1);
95 $ret['perms'] = $j->perms;
101 function zot_encrypt_data($data,&$key) {
102 $key = random_string();
103 return aes_encrypt($data,$key);
107 // encrypt the data prior to calling this function so it only need be done once per message
108 // regardless of the number of recipients.
110 function zot_post_data($url,$zid,$myprvkey,$theirpubkey,$encrypted_data,$key, $intro = false) {
111 $i = zot_post_init($url,$zid,$myprvkey,$theirpubkey);
112 if($i === ZCURL_TIMEOUT)
113 return ZCURL_TIMEOUT;
115 if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
117 if((! stristr($i['perms'],'post')) && ($intro === false)) {
118 logger("zot_post_data: no permission to post: url=$url zid=$zid");
123 $p['result'] = zot_pub_encode($i['result'],$theirpubkey);
124 $p['aes_key'] = zot_prv_encode($key,$myprvkey);
125 $p['data'] = $encrypted_data;
126 $s = zot_post($url,$p);
128 if(! $a->get_curl_code())
129 return ZCURL_TIMEOUT;
132 $j = json_decode($s);
138 function zot_deliver($recipients,$myprvkey,$data) {
140 if(is_array($recipients) && count($recipients)) {
143 $encrypted = zot_encrypt_data($data,$key);
146 foreach($recipients as $r) {
147 $result = zot_post_data(
155 if($result === false) {
157 logger('zot_deliver: failed: ' . print_r($r,true));
159 elseif($result === ZCURL_TIMEOUT) {
160 // queue for redelivery
162 elseif($result->error) {
163 // failed at other end
164 logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
166 elseif($result->success) {
167 logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
170 logger('zot_deliver: unknown failure.');
176 function zot_new_contact($user,$cc) {
178 $zid = random_string(32);
179 $zkey = random_string(32);
181 logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));
184 $ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
185 $ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);