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