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