4 require_once('include/salmon.php');
6 function zot_get($url,$args) {
8 foreach($args as $k => $v) {
11 $argstr .= $k . '=' . $v;
13 $s = fetch_url($url . '?' . $argstr);
22 function zot_post($url,$args) {
23 $s = post_url($url,$args);
33 function zot_prv_encode($s,$prvkey) {
35 $res = openssl_private_encrypt($s,$x,$prvkey);
36 return base64url_encode($y);
38 function zot_pub_encode($s,$pubkey) {
40 $res = openssl_public_encrypt($s,$x,$pubkey);
41 return base64url_encode($x);
44 function zot_prv_decode($s,$prvkey) {
45 $s = base64url_decode($s);
47 openssl_private_decrypt($s,$x,$prvkey);
51 function zot_pub_decode($s,$pubkey) {
52 $s = base64url_decode($s);
54 openssl_public_decrypt($s,$x,$pubkey);
59 function zot_getzid($url,$myaddress,$myprvkey) {
61 $j = zot_get($url,array('sender' => $myaddress));
63 $ret['zid'] = zot_prv_decode($j->zid_encoded,$myprvkey);
65 $ret['zkey'] = zot_prv_decode($j->zkey_encoded,$myprvkey);
69 function zot_post_init($url,$zid,$myprvkey,$theirpubkey) {
72 $zinit = random_string(32);
74 $j = zot_get($url,array('zid' => $zid,'zinit' => $zinit));
77 if(! $a->get_curl_code())
80 logger('zot_post_init: no zinit returned.');
83 if(zot_pub_decode($j->zinit,$thierpubkey) !== $zinit) {
84 logger('zot_post_init: incorrect zinit returned.');
89 $s = zot_prv_decode($j->challenge,$myprvkey);
90 $s1 = substr($s,0,strpos($s,'.'));
92 logger("zot_post_init: incorrect zid returned");
95 $ret['result'] = substr($s,strpos($s,'.') + 1);
96 $ret['perms'] = $j->perms;
102 function zot_encrypt_data($data,&$key) {
103 $key = random_string();
104 return aes_encrypt($data,$key);
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.
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;
116 if((! $i) || (! array_key_exists('perms',$i)) || (! array_key_exists('result',$i)))
118 if((! stristr($i['perms'],'post')) && ($intro === false)) {
119 logger("zot_post_data: no permission to post: url=$url 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);
129 if(! $a->get_curl_code())
130 return ZCURL_TIMEOUT;
133 $j = json_decode($s);
139 function zot_deliver($recipients,$myprvkey,$data) {
141 if(is_array($recipients) && count($recipients)) {
144 $encrypted = zot_encrypt_data($data,$key);
147 foreach($recipients as $r) {
148 $result = zot_post_data(
156 if($result === false) {
158 logger('zot_deliver: failed: ' . print_r($r,true));
160 elseif($result === ZCURL_TIMEOUT) {
161 // queue for redelivery
163 elseif($result->error) {
164 // failed at other end
165 logger('zot_deliver: remote failure: ' . $result->error . ' ' . print_r($r,true));
167 elseif($result->success) {
168 logger('zot_deliver: success ' . print_r($r,true, LOGGER_DEBUG));
171 logger('zot_deliver: unknown failure.');
177 function zot_new_contact($user,$cc) {
179 $zid = random_string(32);
180 $zkey = random_string(32);
182 logger("zot_new_contact: zid=$zid zkey=$zkey uid={$user['uid']} " . print_r($cc,true));
185 $ret['zid_encoded'] = zot_pub_encode($zid,$cc['pubkey']);
186 $ret['zkey_encoded'] = zot_pub_encode($zkey,$cc['pubkey']);