]> git.mxchange.org Git - friendica.git/blob - include/network.php
first checkin - updated contact_edit, more work remains
[friendica.git] / include / network.php
1 <?php
2
3
4 // curl wrapper. If binary flag is true, return binary
5 // results. 
6
7 if(! function_exists('fetch_url')) {
8 function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) {
9
10         $a = get_app();
11
12         $ch = @curl_init($url);
13         if(($redirects > 8) || (! $ch)) 
14                 return false;
15
16         @curl_setopt($ch, CURLOPT_HEADER, true);
17         @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
18         @curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
19
20         if(intval($timeout)) {
21                 @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
22         }
23         else {
24                 $curl_time = intval(get_config('system','curl_timeout'));
25                 @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
26         }
27         // by default we will allow self-signed certs
28         // but you can override this
29
30         $check_cert = get_config('system','verifyssl');
31         @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
32
33         $prx = get_config('system','proxy');
34         if(strlen($prx)) {
35                 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
36                 @curl_setopt($ch, CURLOPT_PROXY, $prx);
37                 $prxusr = @get_config('system','proxyuser');
38                 if(strlen($prxusr))
39                         @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
40         }
41         if($binary)
42                 @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
43
44         $a->set_curl_code(0);
45
46         // don't let curl abort the entire application
47         // if it throws any errors.
48
49         $s = @curl_exec($ch);
50
51         $base = $s;
52         $curl_info = @curl_getinfo($ch);
53         $http_code = $curl_info['http_code'];
54
55         $header = '';
56
57         // Pull out multiple headers, e.g. proxy and continuation headers
58         // allow for HTTP/2.x without fixing code
59
60         while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
61                 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
62                 $header .= $chunk;
63                 $base = substr($base,strlen($chunk));
64         }
65
66         if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
67         $matches = array();
68         preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
69         $url = trim(array_pop($matches));
70         $url_parsed = @parse_url($url);
71         if (isset($url_parsed)) {
72             $redirects++;
73             return fetch_url($url,$binary,$redirects,$timeout);
74         }
75     }
76
77         $a->set_curl_code($http_code);
78
79         $body = substr($s,strlen($header));
80
81         $a->set_curl_headers($header);
82
83         @curl_close($ch);
84         return($body);
85 }}
86
87 // post request to $url. $params is an array of post variables.
88
89 if(! function_exists('post_url')) {
90 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
91         $a = get_app();
92         $ch = curl_init($url);
93         if(($redirects > 8) || (! $ch)) 
94                 return false;
95
96         curl_setopt($ch, CURLOPT_HEADER, true);
97         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
98         curl_setopt($ch, CURLOPT_POST,1);
99         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
100         curl_setopt($ch, CURLOPT_USERAGENT, "Friendika");
101
102         if(intval($timeout)) {
103                 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
104         }
105         else {
106                 $curl_time = intval(get_config('system','curl_timeout'));
107                 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
108         }
109
110         if(defined('LIGHTTPD')) {
111                 if(!is_array($headers)) {
112                         $headers = array('Expect:');
113                 } else {
114                         if(!in_array('Expect:', $headers)) {
115                                 array_push($headers, 'Expect:');
116                         }
117                 }
118         }
119         if($headers)
120                 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
121
122         $check_cert = get_config('system','verifyssl');
123         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
124         $prx = get_config('system','proxy');
125         if(strlen($prx)) {
126                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
127                 curl_setopt($ch, CURLOPT_PROXY, $prx);
128                 $prxusr = get_config('system','proxyuser');
129                 if(strlen($prxusr))
130                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
131         }
132
133         $a->set_curl_code(0);
134
135         // don't let curl abort the entire application
136         // if it throws any errors.
137
138         $s = @curl_exec($ch);
139
140         $base = $s;
141         $curl_info = curl_getinfo($ch);
142         $http_code = $curl_info['http_code'];
143
144         $header = '';
145
146         // Pull out multiple headers, e.g. proxy and continuation headers
147         // allow for HTTP/2.x without fixing code
148
149         while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
150                 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
151                 $header .= $chunk;
152                 $base = substr($base,strlen($chunk));
153         }
154
155         if($http_code == 301 || $http_code == 302 || $http_code == 303) {
156         $matches = array();
157         preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
158         $url = trim(array_pop($matches));
159         $url_parsed = @parse_url($url);
160         if (isset($url_parsed)) {
161             $redirects++;
162             return post_url($url,$params,$headers,$redirects,$timeout);
163         }
164     }
165         $a->set_curl_code($http_code);
166         $body = substr($s,strlen($header));
167
168         $a->set_curl_headers($header);
169
170         curl_close($ch);
171         return($body);
172 }}
173
174 // Generic XML return
175 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable 
176 // of $st and an optional text <message> of $message and terminates the current process. 
177
178 if(! function_exists('xml_status')) {
179 function xml_status($st, $message = '') {
180
181         $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
182
183         if($st)
184                 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
185
186         header( "Content-type: text/xml" );
187         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
188         echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
189         killme();
190 }}
191
192
193 if(! function_exists('http_status_exit')) {
194 function http_status_exit($val) {
195
196         if($val >= 400)
197                 $err = 'Error';
198         if($val >= 200 && $val < 300)
199                 $err = 'OK';
200
201         logger('http_status_exit ' . $val);     
202         header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
203         killme();
204
205 }}
206
207
208 // convert an XML document to a normalised, case-corrected array
209 // used by webfinger
210
211 if(! function_exists('convert_xml_element_to_array')) {
212 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
213
214         // If we're getting too deep, bail out
215         if ($recursion_depth > 512) {
216                 return(null);
217         }
218
219         if (!is_string($xml_element) &&
220         !is_array($xml_element) &&
221         (get_class($xml_element) == 'SimpleXMLElement')) {
222                 $xml_element_copy = $xml_element;
223                 $xml_element = get_object_vars($xml_element);
224         }
225
226         if (is_array($xml_element)) {
227                 $result_array = array();
228                 if (count($xml_element) <= 0) {
229                         return (trim(strval($xml_element_copy)));
230                 }
231
232                 foreach($xml_element as $key=>$value) {
233
234                         $recursion_depth++;
235                         $result_array[strtolower($key)] =
236                 convert_xml_element_to_array($value, $recursion_depth);
237                         $recursion_depth--;
238                 }
239                 if ($recursion_depth == 0) {
240                         $temp_array = $result_array;
241                         $result_array = array(
242                                 strtolower($xml_element_copy->getName()) => $temp_array,
243                         );
244                 }
245
246                 return ($result_array);
247
248         } else {
249                 return (trim(strval($xml_element)));
250         }
251 }}
252
253 // Given an email style address, perform webfinger lookup and 
254 // return the resulting DFRN profile URL, or if no DFRN profile URL
255 // is located, returns an OStatus subscription template (prefixed 
256 // with the string 'stat:' to identify it as on OStatus template).
257 // If this isn't an email style address just return $s.
258 // Return an empty string if email-style addresses but webfinger fails,
259 // or if the resultant personal XRD doesn't contain a supported 
260 // subscription/friend-request attribute.
261
262 // amended 7/9/2011 to return an hcard which could save potentially loading 
263 // a lengthy content page to scrape dfrn attributes
264
265 if(! function_exists('webfinger_dfrn')) {
266 function webfinger_dfrn($s,&$hcard) {
267         if(! strstr($s,'@')) {
268                 return $s;
269         }
270         $profile_link = '';
271
272         $links = webfinger($s);
273         logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
274         if(count($links)) {
275                 foreach($links as $link) {
276                         if($link['@attributes']['rel'] === NAMESPACE_DFRN)
277                                 $profile_link = $link['@attributes']['href'];
278                         if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
279                                 $profile_link = 'stat:' . $link['@attributes']['template'];     
280                         if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
281                                 $hcard = $link['@attributes']['href'];                          
282                 }
283         }
284         return $profile_link;
285 }}
286
287 // Given an email style address, perform webfinger lookup and 
288 // return the array of link attributes from the personal XRD file.
289 // On error/failure return an empty array.
290
291
292 if(! function_exists('webfinger')) {
293 function webfinger($s) {
294         $host = '';
295         if(strstr($s,'@')) {
296                 $host = substr($s,strpos($s,'@') + 1);
297         }
298         if(strlen($host)) {
299                 $tpl = fetch_lrdd_template($host);
300                 logger('webfinger: lrdd template: ' . $tpl);
301                 if(strlen($tpl)) {
302                         $pxrd = str_replace('{uri}', urlencode('acct:' . $s), $tpl);
303                         logger('webfinger: pxrd: ' . $pxrd);
304                         $links = fetch_xrd_links($pxrd);
305                         if(! count($links)) {
306                                 // try with double slashes
307                                 $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl);
308                                 logger('webfinger: pxrd: ' . $pxrd);
309                                 $links = fetch_xrd_links($pxrd);
310                         }
311                         return $links;
312                 }
313         }
314         return array();
315 }}
316
317 if(! function_exists('lrdd')) {
318 function lrdd($uri) {
319
320         $a = get_app();
321
322         // default priority is host priority, host-meta first
323
324         $priority = 'host';
325
326         // All we have is an email address. Resource-priority is irrelevant
327         // because our URI isn't directly resolvable.
328
329         if(strstr($uri,'@')) {  
330                 return(webfinger($uri));
331         }
332
333         // get the host meta file
334
335         $host = @parse_url($uri);
336
337         if($host) {
338                 $url  = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
339                 $url .= $host['host'] . '/.well-known/host-meta' ;
340         }
341         else
342                 return array();
343
344         logger('lrdd: constructed url: ' . $url);
345
346         $xml = fetch_url($url);
347         $headers = $a->get_curl_headers();
348
349         if (! $xml)
350                 return array();
351
352         logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
353
354         $h = parse_xml_string($xml);
355         if(! $h)
356                 return array();
357
358         $arr = convert_xml_element_to_array($h);
359
360         if(isset($arr['xrd']['property'])) {
361                 $property = $arr['crd']['property'];
362                 if(! isset($property[0]))
363                         $properties = array($property);
364                 else
365                         $properties = $property;
366                 foreach($properties as $prop)
367                         if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
368                                 $priority = 'resource';
369         } 
370
371         // save the links in case we need them
372
373         $links = array();
374
375         if(isset($arr['xrd']['link'])) {
376                 $link = $arr['xrd']['link'];
377                 if(! isset($link[0]))
378                         $links = array($link);
379                 else
380                         $links = $link;
381         }
382
383         // do we have a template or href?
384
385         if(count($links)) {
386                 foreach($links as $link) {
387                         if($link['@attributes']['rel'] && attribute_contains($link['@attributes']['rel'],'lrdd')) {
388                                 if(x($link['@attributes'],'template'))
389                                         $tpl = $link['@attributes']['template'];
390                                 elseif(x($link['@attributes'],'href'))
391                                         $href = $link['@attributes']['href'];
392                         }
393                 }               
394         }
395
396         if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
397                 $tpl = '';
398
399         if($priority === 'host') {
400                 if(strlen($tpl)) 
401                         $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
402                 elseif(isset($href))
403                         $pxrd = $href;
404                 if(isset($pxrd)) {
405                         logger('lrdd: (host priority) pxrd: ' . $pxrd);
406                         $links = fetch_xrd_links($pxrd);
407                         return $links;
408                 }
409
410                 $lines = explode("\n",$headers);
411                 if(count($lines)) {
412                         foreach($lines as $line) {                              
413                                 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
414                                         return(fetch_xrd_links($matches[1]));
415                                         break;
416                                 }
417                         }
418                 }
419         }
420
421
422         // priority 'resource'
423
424
425         $html = fetch_url($uri);
426         $headers = $a->get_curl_headers();
427         logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
428
429         // don't try and parse raw xml as html
430         if(! strstr($html,'<?xml')) {
431                 require_once('library/HTML5/Parser.php');
432
433                 try {
434                         $dom = HTML5_Parser::parse($html);
435                 } catch (DOMException $e) {
436                         logger('lrdd: parse error: ' . $e);
437                 }
438
439                 if($dom) {
440                         $items = $dom->getElementsByTagName('link');
441                         foreach($items as $item) {
442                                 $x = $item->getAttribute('rel');
443                                 if($x == "lrdd") {
444                                         $pagelink = $item->getAttribute('href');
445                                         break;
446                                 }
447                         }
448                 }
449         }
450
451         if(isset($pagelink))
452                 return(fetch_xrd_links($pagelink));
453
454         // next look in HTTP headers
455
456         $lines = explode("\n",$headers);
457         if(count($lines)) {
458                 foreach($lines as $line) {                              
459                         // TODO alter the following regex to support multiple relations (space separated)
460                         if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
461                                 $pagelink = $matches[1];
462                                 break;
463                         }
464                         // don't try and run feeds through the html5 parser
465                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
466                                 return array();
467                         if(stristr($html,'<rss') || stristr($html,'<feed'))
468                                 return array();
469                 }
470         }
471
472         if(isset($pagelink))
473                 return(fetch_xrd_links($pagelink));
474
475         // If we haven't found any links, return the host xrd links (which we have already fetched)
476
477         if(isset($links))
478                 return $links;
479
480         return array();
481
482 }}
483
484
485
486 // Given a host name, locate the LRDD template from that
487 // host. Returns the LRDD template or an empty string on
488 // error/failure.
489
490 if(! function_exists('fetch_lrdd_template')) {
491 function fetch_lrdd_template($host) {
492         $tpl = '';
493
494         $url1 = 'https://' . $host . '/.well-known/host-meta' ;
495         $url2 = 'http://' . $host . '/.well-known/host-meta' ;
496         $links = fetch_xrd_links($url1);
497         logger('fetch_lrdd_template from: ' . $url1);
498         logger('template (https): ' . print_r($links,true));
499         if(! count($links)) {
500                 logger('fetch_lrdd_template from: ' . $url2);
501                 $links = fetch_xrd_links($url2);
502                 logger('template (http): ' . print_r($links,true));
503         }
504         if(count($links)) {
505                 foreach($links as $link)
506                         if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
507                                 $tpl = $link['@attributes']['template'];
508         }
509         if(! strpos($tpl,'{uri}'))
510                 $tpl = '';
511         return $tpl;
512 }}
513
514 // Given a URL, retrieve the page as an XRD document.
515 // Return an array of links.
516 // on error/failure return empty array.
517
518 if(! function_exists('fetch_xrd_links')) {
519 function fetch_xrd_links($url) {
520
521         $xrd_timeout = intval(get_config('system','xrd_timeout'));
522         $redirects = 0;
523         $xml = fetch_url($url,false,$redirects,(($xrd_timeout) ? $xrd_timeout : 20));
524
525         logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
526
527         if ((! $xml) || (! stristr($xml,'<xrd')))
528                 return array();
529
530         $h = parse_xml_string($xml);
531         if(! $h)
532                 return array();
533
534         $arr = convert_xml_element_to_array($h);
535
536         $links = array();
537
538         if(isset($arr['xrd']['link'])) {
539                 $link = $arr['xrd']['link'];
540                 if(! isset($link[0]))
541                         $links = array($link);
542                 else
543                         $links = $link;
544         }
545         if(isset($arr['xrd']['alias'])) {
546                 $alias = $arr['xrd']['alias'];
547                 if(! isset($alias[0]))
548                         $aliases = array($alias);
549                 else
550                         $aliases = $alias;
551                 if(is_array($aliases) && count($aliases)) {
552                         foreach($aliases as $alias) {
553                                 $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
554                         }
555                 }
556         }
557
558         logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
559
560         return $links;
561
562 }}
563
564
565 // Take a URL from the wild, prepend http:// if necessary
566 // and check DNS to see if it's real
567 // return true if it's OK, false if something is wrong with it
568
569 if(! function_exists('validate_url')) {
570 function validate_url(&$url) {
571         if(substr($url,0,4) != 'http')
572                 $url = 'http://' . $url;
573         $h = @parse_url($url);
574
575         if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR))) {
576                 return true;
577         }
578         return false;
579 }}
580
581 // checks that email is an actual resolvable internet address
582
583 if(! function_exists('validate_email')) {
584 function validate_email($addr) {
585
586         if(! strpos($addr,'@'))
587                 return false;
588         $h = substr($addr,strpos($addr,'@') + 1);
589
590         if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX))) {
591                 return true;
592         }
593         return false;
594 }}
595
596 // Check $url against our list of allowed sites,
597 // wildcards allowed. If allowed_sites is unset return true;
598 // If url is allowed, return true.
599 // otherwise, return false
600
601 if(! function_exists('allowed_url')) {
602 function allowed_url($url) {
603
604         $h = @parse_url($url);
605
606         if(! $h) {
607                 return false;
608         }
609
610         $str_allowed = get_config('system','allowed_sites');
611         if(! $str_allowed)
612                 return true;
613
614         $found = false;
615
616         $host = strtolower($h['host']);
617
618         // always allow our own site
619
620         if($host == strtolower($_SERVER['SERVER_NAME']))
621                 return true;
622
623         $fnmatch = function_exists('fnmatch');
624         $allowed = explode(',',$str_allowed);
625
626         if(count($allowed)) {
627                 foreach($allowed as $a) {
628                         $pat = strtolower(trim($a));
629                         if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
630                                 $found = true; 
631                                 break;
632                         }
633                 }
634         }
635         return $found;
636 }}
637
638 // check if email address is allowed to register here.
639 // Compare against our list (wildcards allowed).
640 // Returns false if not allowed, true if allowed or if
641 // allowed list is not configured.
642
643 if(! function_exists('allowed_email')) {
644 function allowed_email($email) {
645
646
647         $domain = strtolower(substr($email,strpos($email,'@') + 1));
648         if(! $domain)
649                 return false;
650
651         $str_allowed = get_config('system','allowed_email');
652         if(! $str_allowed)
653                 return true;
654
655         $found = false;
656
657         $fnmatch = function_exists('fnmatch');
658         $allowed = explode(',',$str_allowed);
659
660         if(count($allowed)) {
661                 foreach($allowed as $a) {
662                         $pat = strtolower(trim($a));
663                         if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
664                                 $found = true; 
665                                 break;
666                         }
667                 }
668         }
669         return $found;
670 }}
671
672
673 if(! function_exists('gravatar_img')) {
674 function gravatar_img($email) {
675         $size = 175;
676         $opt = 'identicon';   // psuedo-random geometric pattern if not found
677         $rating = 'pg';
678         $hash = md5(trim(strtolower($email)));
679         
680         $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg' 
681                 . '?s=' . $size . '&d=' . $opt . '&r=' . $rating;
682
683         logger('gravatar: ' . $email . ' ' . $url);
684         return $url;
685 }}
686
687
688 if(! function_exists('parse_xml_string')) {
689 function parse_xml_string($s,$strict = true) {
690         if($strict) {
691                 if(! strstr($s,'<?xml'))
692                         return false;
693                 $s2 = substr($s,strpos($s,'<?xml'));
694         }
695         else
696                 $s2 = $s;
697         libxml_use_internal_errors(true);
698
699         $x = @simplexml_load_string($s2);
700         if(! $x) {
701                 logger('libxml: parse: error: ' . $s2, LOGGER_DATA);
702                 foreach(libxml_get_errors() as $err)
703                         logger('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, LOGGER_DATA);
704                 libxml_clear_errors();
705         }
706         return $x;
707 }}
708
709 function add_fcontact($arr,$update = false) {
710
711         if($update) {
712                 $r = q("UPDATE `fcontact` SET
713                         `name` = '%s',
714                         `photo` = '%s',
715                         `request` = '%s',
716                         `nick` = '%s',
717                         `addr` = '%s',
718                         `batch` = '%s',
719                         `notify` = '%s',
720                         `poll` = '%s',
721                         `confirm` = '%s',
722                         `alias` = '%s',
723                         `pubkey` = '%s',
724                         `updated` = '%s'
725                         WHERE `url` = '%s' AND `network` = '%s' LIMIT 1", 
726                         dbesc($arr['name']),
727                         dbesc($arr['photo']),
728                         dbesc($arr['request']),
729                         dbesc($arr['nick']),
730                         dbesc($arr['addr']),
731                         dbesc($arr['batch']),
732                         dbesc($arr['notify']),
733                         dbesc($arr['poll']),
734                         dbesc($arr['confirm']),
735                         dbesc($arr['alias']),
736                         dbesc($arr['pubkey']),
737                         dbesc(datetime_convert()),
738                         dbesc($arr['url']),
739                         dbesc($arr['network'])
740                 );
741         }
742         else {
743                 $r = q("insert into fcontact ( `url`,`name`,`photo`,`request`,`nick`,`addr`,
744                         `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated` )
745                         values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
746                         dbesc($arr['url']),
747                         dbesc($arr['name']),
748                         dbesc($arr['photo']),
749                         dbesc($arr['request']),
750                         dbesc($arr['nick']),
751                         dbesc($arr['addr']),
752                         dbesc($arr['batch']),
753                         dbesc($arr['notify']),
754                         dbesc($arr['poll']),
755                         dbesc($arr['confirm']),
756                         dbesc($arr['network']),
757                         dbesc($arr['alias']),
758                         dbesc($arr['pubkey']),
759                         dbesc(datetime_convert())
760                 );
761         }
762
763         return $r;
764 }