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