]> git.mxchange.org Git - friendica.git/blob - include/network.php
Merge develop into 1404_reworked_autocomplete
[friendica.git] / include / network.php
1 <?php
2
3
4 // curl wrapper. If binary flag is true, return binary
5 // results.
6
7 // Set the cookiejar argument to a string (e.g. "/tmp/friendica-cookies.txt")
8 // to preserve cookies from one request to the next.
9 if(! function_exists('fetch_url')) {
10 function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null, $cookiejar = 0) {
11
12         $ret = z_fetch_url(
13                 $url,
14                 $binary,
15                 $redirects,
16                 array('timeout'=>$timeout,
17                 'accept_content'=>$accept_content,
18                 'cookiejar'=>$cookiejar
19                 ));
20
21         return($ret['body']);
22 }}
23
24 if(!function_exists('z_fetch_url')){
25 /**
26  * @brief fetches an URL.
27  *
28  * @param string $url
29  *    URL to fetch
30  * @param boolean $binary default false
31  *    TRUE if asked to return binary results (file download)
32  * @param int $redirects default 0
33  *    internal use, recursion counter
34  * @param array $opts (optional parameters) assoziative array with:
35  *  * \b accept_content => supply Accept: header with 'accept_content' as the value
36  *  * \b timeout => int seconds, default system config value or 60 seconds
37  *  * \b http_auth => username:password
38  *  * \b novalidate => do not validate SSL certs, default is to validate using our CA list
39  *  * \b nobody => only return the header
40  *      * \b cookiejar => path to cookie jar file
41  *
42  * @return array an assoziative array with:
43  *  * \e int \b return_code => HTTP return code or 0 if timeout or failure
44  *  * \e boolean \b success => boolean true (if HTTP 2xx result) or false
45  *  * \e string \b redirect_url => in case of redirect, content was finally retrieved from this URL
46  *  * \e string \b header => HTTP headers
47  *  * \e string \b body => fetched content
48  */
49 function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) {
50
51         $ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
52
53
54         $stamp1 = microtime(true);
55
56         $a = get_app();
57
58         $ch = @curl_init($url);
59         if(($redirects > 8) || (! $ch))
60                 return false;
61
62         @curl_setopt($ch, CURLOPT_HEADER, true);
63
64         if(x($opts,"cookiejar")) {
65                 curl_setopt($ch, CURLOPT_COOKIEJAR, $opts["cookiejar"]);
66                 curl_setopt($ch, CURLOPT_COOKIEFILE, $opts["cookiejar"]);
67         }
68
69 //  These settings aren't needed. We're following the location already.
70 //      @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
71 //      @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
72
73         if (x($opts,'accept_content')){
74                 curl_setopt($ch,CURLOPT_HTTPHEADER, array (
75                         "Accept: " . $opts['accept_content']
76                 ));
77         }
78
79         @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
80         @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
81
82
83
84         if(x($opts,'headers')){
85                 @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
86         }
87         if(x($opts,'nobody')){
88                 @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
89         }
90         if(x($opts,'timeout')){
91                 @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
92         } else {
93                 $curl_time = intval(get_config('system','curl_timeout'));
94                 @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
95         }
96         // by default we will allow self-signed certs
97         // but you can override this
98
99         $check_cert = get_config('system','verifyssl');
100         @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
101         @curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, (($check_cert) ? 2 : false));
102
103         $prx = get_config('system','proxy');
104         if(strlen($prx)) {
105                 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
106                 @curl_setopt($ch, CURLOPT_PROXY, $prx);
107                 $prxusr = @get_config('system','proxyuser');
108                 if(strlen($prxusr))
109                         @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
110         }
111         if($binary)
112                 @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
113
114         $a->set_curl_code(0);
115
116         // don't let curl abort the entire application
117         // if it throws any errors.
118
119         $s = @curl_exec($ch);
120         if (curl_errno($ch) !== CURLE_OK) {
121                 logger('fetch_url error fetching '.$url.': '.curl_error($ch), LOGGER_NORMAL);
122         }
123
124         $base = $s;
125         $curl_info = @curl_getinfo($ch);
126
127         $http_code = $curl_info['http_code'];
128         logger('fetch_url '.$url.': '.$http_code." ".$s, LOGGER_DATA);
129         $header = '';
130
131         // Pull out multiple headers, e.g. proxy and continuation headers
132         // allow for HTTP/2.x without fixing code
133
134         while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
135                 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
136                 $header .= $chunk;
137                 $base = substr($base,strlen($chunk));
138         }
139
140         $a->set_curl_code($http_code);
141         $a->set_curl_content_type($curl_info['content_type']);
142         $a->set_curl_headers($header);
143
144         if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
145                 $new_location_info = @parse_url($curl_info["redirect_url"]);
146                 $old_location_info = @parse_url($curl_info["url"]);
147
148                 $newurl = $curl_info["redirect_url"];
149
150                 if (($new_location_info["path"] == "") AND ($new_location_info["host"] != ""))
151                         $newurl = $new_location_info["scheme"]."://".$new_location_info["host"].$old_location_info["path"];
152
153                 $matches = array();
154                 if (preg_match('/(Location:|URI:)(.*?)\n/i', $header, $matches)) {
155                         $newurl = trim(array_pop($matches));
156                 }
157                 if(strpos($newurl,'/') === 0)
158                         $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
159                 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
160                         $redirects++;
161                         @curl_close($ch);
162                         return z_fetch_url($newurl,$binary, $redirects, $opts);
163                 }
164         }
165
166
167         $a->set_curl_code($http_code);
168         $a->set_curl_content_type($curl_info['content_type']);
169
170         $body = substr($s,strlen($header));
171
172
173
174         $rc = intval($http_code);
175         $ret['return_code'] = $rc;
176         $ret['success'] = (($rc >= 200 && $rc <= 299) ? true : false);
177         $ret['redirect_url'] = $url;
178         if(! $ret['success']) {
179                 $ret['error'] = curl_error($ch);
180                 $ret['debug'] = $curl_info;
181                 logger('z_fetch_url: error: ' . $url . ': ' . $ret['error'], LOGGER_DEBUG);
182                 logger('z_fetch_url: debug: ' . print_r($curl_info,true), LOGGER_DATA);
183         }
184         $ret['body'] = substr($s,strlen($header));
185         $ret['header'] = $header;
186         if(x($opts,'debug')) {
187                 $ret['debug'] = $curl_info;
188         }
189         @curl_close($ch);
190
191         $a->save_timestamp($stamp1, "network");
192
193         return($ret);
194
195 }}
196
197 // post request to $url. $params is an array of post variables.
198
199 if(! function_exists('post_url')) {
200 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
201         $stamp1 = microtime(true);
202
203         $a = get_app();
204         $ch = curl_init($url);
205         if(($redirects > 8) || (! $ch))
206                 return false;
207
208         logger("post_url: start ".$url, LOGGER_DATA);
209
210         curl_setopt($ch, CURLOPT_HEADER, true);
211         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
212         curl_setopt($ch, CURLOPT_POST,1);
213         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
214         curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
215
216         if(intval($timeout)) {
217                 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
218         }
219         else {
220                 $curl_time = intval(get_config('system','curl_timeout'));
221                 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
222         }
223
224         if(defined('LIGHTTPD')) {
225                 if(!is_array($headers)) {
226                         $headers = array('Expect:');
227                 } else {
228                         if(!in_array('Expect:', $headers)) {
229                                 array_push($headers, 'Expect:');
230                         }
231                 }
232         }
233         if($headers)
234                 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
235
236         $check_cert = get_config('system','verifyssl');
237         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
238         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, (($check_cert) ? 2 : false));
239         $prx = get_config('system','proxy');
240         if(strlen($prx)) {
241                 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
242                 curl_setopt($ch, CURLOPT_PROXY, $prx);
243                 $prxusr = get_config('system','proxyuser');
244                 if(strlen($prxusr))
245                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
246         }
247
248         $a->set_curl_code(0);
249
250         // don't let curl abort the entire application
251         // if it throws any errors.
252
253         $s = @curl_exec($ch);
254
255         $base = $s;
256         $curl_info = curl_getinfo($ch);
257         $http_code = $curl_info['http_code'];
258
259         logger("post_url: result ".$http_code." - ".$url, LOGGER_DATA);
260
261         $header = '';
262
263         // Pull out multiple headers, e.g. proxy and continuation headers
264         // allow for HTTP/2.x without fixing code
265
266         while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
267                 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
268                 $header .= $chunk;
269                 $base = substr($base,strlen($chunk));
270         }
271
272         if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
273                 $matches = array();
274                 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
275                 $newurl = trim(array_pop($matches));
276                 if(strpos($newurl,'/') === 0)
277                         $newurl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $newurl;
278                 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
279                         $redirects++;
280                         logger("post_url: redirect ".$url." to ".$newurl);
281                         return post_url($newurl,$params, $headers, $redirects, $timeout);
282                         //return fetch_url($newurl,false,$redirects,$timeout);
283                 }
284         }
285         $a->set_curl_code($http_code);
286         $body = substr($s,strlen($header));
287
288         $a->set_curl_headers($header);
289
290         curl_close($ch);
291
292         $a->save_timestamp($stamp1, "network");
293
294         logger("post_url: end ".$url, LOGGER_DATA);
295
296         return($body);
297 }}
298
299 // Generic XML return
300 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
301 // of $st and an optional text <message> of $message and terminates the current process.
302
303 if(! function_exists('xml_status')) {
304 function xml_status($st, $message = '') {
305
306         $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
307
308         if($st)
309                 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
310
311         header( "Content-type: text/xml" );
312         echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
313         echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
314         killme();
315 }}
316
317
318 if(! function_exists('http_status_exit')) {
319 function http_status_exit($val, $description = array()) {
320     $err = '';
321         if($val >= 400) {
322                 $err = 'Error';
323                 if (!isset($description["title"]))
324                         $description["title"] = $err." ".$val;
325         }
326         if($val >= 200 && $val < 300)
327                 $err = 'OK';
328
329         logger('http_status_exit ' . $val);
330         header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
331
332         if (isset($description["title"])) {
333                 $tpl = get_markup_template('http_status.tpl');
334                 echo replace_macros($tpl, array('$title' => $description["title"],
335                                                 '$description' => $description["description"]));
336         }
337
338         killme();
339
340 }}
341
342
343 // convert an XML document to a normalised, case-corrected array
344 // used by webfinger
345
346 if(! function_exists('convert_xml_element_to_array')) {
347 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
348
349         // If we're getting too deep, bail out
350         if ($recursion_depth > 512) {
351                 return(null);
352         }
353
354         if (!is_string($xml_element) &&
355         !is_array($xml_element) &&
356         (get_class($xml_element) == 'SimpleXMLElement')) {
357                 $xml_element_copy = $xml_element;
358                 $xml_element = get_object_vars($xml_element);
359         }
360
361         if (is_array($xml_element)) {
362                 $result_array = array();
363                 if (count($xml_element) <= 0) {
364                         return (trim(strval($xml_element_copy)));
365                 }
366
367                 foreach($xml_element as $key=>$value) {
368
369                         $recursion_depth++;
370                         $result_array[strtolower($key)] =
371                 convert_xml_element_to_array($value, $recursion_depth);
372                         $recursion_depth--;
373                 }
374                 if ($recursion_depth == 0) {
375                         $temp_array = $result_array;
376                         $result_array = array(
377                                 strtolower($xml_element_copy->getName()) => $temp_array,
378                         );
379                 }
380
381                 return ($result_array);
382
383         } else {
384                 return (trim(strval($xml_element)));
385         }
386 }}
387
388 // Given an email style address, perform webfinger lookup and
389 // return the resulting DFRN profile URL, or if no DFRN profile URL
390 // is located, returns an OStatus subscription template (prefixed
391 // with the string 'stat:' to identify it as on OStatus template).
392 // If this isn't an email style address just return $s.
393 // Return an empty string if email-style addresses but webfinger fails,
394 // or if the resultant personal XRD doesn't contain a supported
395 // subscription/friend-request attribute.
396
397 // amended 7/9/2011 to return an hcard which could save potentially loading
398 // a lengthy content page to scrape dfrn attributes
399
400 if(! function_exists('webfinger_dfrn')) {
401 function webfinger_dfrn($s,&$hcard) {
402         if(! strstr($s,'@')) {
403                 return $s;
404         }
405         $profile_link = '';
406
407         $links = webfinger($s);
408         logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
409         if(count($links)) {
410                 foreach($links as $link) {
411                         if($link['@attributes']['rel'] === NAMESPACE_DFRN)
412                                 $profile_link = $link['@attributes']['href'];
413                         if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
414                                 $profile_link = 'stat:' . $link['@attributes']['template'];
415                         if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
416                                 $hcard = $link['@attributes']['href'];
417                 }
418         }
419         return $profile_link;
420 }}
421
422 // Given an email style address, perform webfinger lookup and
423 // return the array of link attributes from the personal XRD file.
424 // On error/failure return an empty array.
425
426
427 if(! function_exists('webfinger')) {
428 function webfinger($s, $debug = false) {
429         $host = '';
430         if(strstr($s,'@')) {
431                 $host = substr($s,strpos($s,'@') + 1);
432         }
433         if(strlen($host)) {
434                 $tpl = fetch_lrdd_template($host);
435                 logger('webfinger: lrdd template: ' . $tpl);
436                 if(strlen($tpl)) {
437                         $pxrd = str_replace('{uri}', urlencode('acct:' . $s), $tpl);
438                         logger('webfinger: pxrd: ' . $pxrd);
439                         $links = fetch_xrd_links($pxrd);
440                         if(! count($links)) {
441                                 // try with double slashes
442                                 $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl);
443                                 logger('webfinger: pxrd: ' . $pxrd);
444                                 $links = fetch_xrd_links($pxrd);
445                         }
446                         return $links;
447                 }
448         }
449         return array();
450 }}
451
452 if(! function_exists('lrdd')) {
453 function lrdd($uri, $debug = false) {
454
455         $a = get_app();
456
457         // default priority is host priority, host-meta first
458
459         $priority = 'host';
460
461         // All we have is an email address. Resource-priority is irrelevant
462         // because our URI isn't directly resolvable.
463
464         if(strstr($uri,'@')) {
465                 return(webfinger($uri));
466         }
467
468         // get the host meta file
469
470         $host = @parse_url($uri);
471
472         if($host) {
473                 $url  = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
474                 $url .= $host['host'] . '/.well-known/host-meta' ;
475         }
476         else
477                 return array();
478
479         logger('lrdd: constructed url: ' . $url);
480
481         $xml = fetch_url($url);
482
483         $headers = $a->get_curl_headers();
484
485         if (! $xml)
486                 return array();
487
488         logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
489
490         if(! stristr($xml,'<xrd'))
491                 return array();
492
493         $h = parse_xml_string($xml);
494         if(! $h)
495                 return array();
496
497         $arr = convert_xml_element_to_array($h);
498
499         if(isset($arr['xrd']['property'])) {
500                 $property = $arr['crd']['property'];
501                 if(! isset($property[0]))
502                         $properties = array($property);
503                 else
504                         $properties = $property;
505                 foreach($properties as $prop)
506                         if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
507                                 $priority = 'resource';
508         }
509
510         // save the links in case we need them
511
512         $links = array();
513
514         if(isset($arr['xrd']['link'])) {
515                 $link = $arr['xrd']['link'];
516                 if(! isset($link[0]))
517                         $links = array($link);
518                 else
519                         $links = $link;
520         }
521
522         // do we have a template or href?
523
524         if(count($links)) {
525                 foreach($links as $link) {
526                         if($link['@attributes']['rel'] && attribute_contains($link['@attributes']['rel'],'lrdd')) {
527                                 if(x($link['@attributes'],'template'))
528                                         $tpl = $link['@attributes']['template'];
529                                 elseif(x($link['@attributes'],'href'))
530                                         $href = $link['@attributes']['href'];
531                         }
532                 }
533         }
534
535         if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
536                 $tpl = '';
537
538         if($priority === 'host') {
539                 if(strlen($tpl))
540                         $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
541                 elseif(isset($href))
542                         $pxrd = $href;
543                 if(isset($pxrd)) {
544                         logger('lrdd: (host priority) pxrd: ' . $pxrd);
545                         $links = fetch_xrd_links($pxrd);
546                         return $links;
547                 }
548
549                 $lines = explode("\n",$headers);
550                 if(count($lines)) {
551                         foreach($lines as $line) {
552                                 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
553                                         return(fetch_xrd_links($matches[1]));
554                                         break;
555                                 }
556                         }
557                 }
558         }
559
560
561         // priority 'resource'
562
563
564         $html = fetch_url($uri);
565         $headers = $a->get_curl_headers();
566         logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
567
568         // don't try and parse raw xml as html
569         if(! strstr($html,'<?xml')) {
570                 require_once('library/HTML5/Parser.php');
571
572                 try {
573                         $dom = HTML5_Parser::parse($html);
574                 } catch (DOMException $e) {
575                         logger('lrdd: parse error: ' . $e);
576                 }
577
578                 if(isset($dom) && $dom) {
579                         $items = $dom->getElementsByTagName('link');
580                         foreach($items as $item) {
581                                 $x = $item->getAttribute('rel');
582                                 if($x == "lrdd") {
583                                         $pagelink = $item->getAttribute('href');
584                                         break;
585                                 }
586                         }
587                 }
588         }
589
590         if(isset($pagelink))
591                 return(fetch_xrd_links($pagelink));
592
593         // next look in HTTP headers
594
595         $lines = explode("\n",$headers);
596         if(count($lines)) {
597                 foreach($lines as $line) {
598                         /// @TODO Alter the following regex to support multiple relations (space separated)
599                         if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
600                                 $pagelink = $matches[1];
601                                 break;
602                         }
603                         // don't try and run feeds through the html5 parser
604                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
605                                 return array();
606                         if(stristr($html,'<rss') || stristr($html,'<feed'))
607                                 return array();
608                 }
609         }
610
611         if(isset($pagelink))
612                 return(fetch_xrd_links($pagelink));
613
614         // If we haven't found any links, return the host xrd links (which we have already fetched)
615
616         if(isset($links))
617                 return $links;
618
619         return array();
620
621 }}
622
623
624
625 // Given a host name, locate the LRDD template from that
626 // host. Returns the LRDD template or an empty string on
627 // error/failure.
628
629 if(! function_exists('fetch_lrdd_template')) {
630 function fetch_lrdd_template($host) {
631         $tpl = '';
632
633         $url1 = 'https://' . $host . '/.well-known/host-meta' ;
634         $url2 = 'http://' . $host . '/.well-known/host-meta' ;
635         $links = fetch_xrd_links($url1);
636         logger('fetch_lrdd_template from: ' . $url1);
637         logger('template (https): ' . print_r($links,true));
638         if(! count($links)) {
639                 logger('fetch_lrdd_template from: ' . $url2);
640                 $links = fetch_xrd_links($url2);
641                 logger('template (http): ' . print_r($links,true));
642         }
643         if(count($links)) {
644                 foreach($links as $link)
645                         if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd' && (!$link['@attributes']['type'] || $link['@attributes']['type'] === 'application/xrd+xml'))
646                                 $tpl = $link['@attributes']['template'];
647         }
648         if(! strpos($tpl,'{uri}'))
649                 $tpl = '';
650         return $tpl;
651 }}
652
653 // Given a URL, retrieve the page as an XRD document.
654 // Return an array of links.
655 // on error/failure return empty array.
656
657 if(! function_exists('fetch_xrd_links')) {
658 function fetch_xrd_links($url) {
659
660         $xrd_timeout = intval(get_config('system','xrd_timeout'));
661         $redirects = 0;
662         $xml = fetch_url($url,false,$redirects,(($xrd_timeout) ? $xrd_timeout : 20), "application/xrd+xml");
663
664         logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
665
666         if ((! $xml) || (! stristr($xml,'<xrd')))
667                 return array();
668
669         // fix diaspora's bad xml
670         $xml = str_replace(array('href=&quot;','&quot;/>'),array('href="','"/>'),$xml);
671
672         $h = parse_xml_string($xml);
673         if(! $h)
674                 return array();
675
676         $arr = convert_xml_element_to_array($h);
677
678         $links = array();
679
680         if(isset($arr['xrd']['link'])) {
681                 $link = $arr['xrd']['link'];
682                 if(! isset($link[0]))
683                         $links = array($link);
684                 else
685                         $links = $link;
686         }
687         if(isset($arr['xrd']['alias'])) {
688                 $alias = $arr['xrd']['alias'];
689                 if(! isset($alias[0]))
690                         $aliases = array($alias);
691                 else
692                         $aliases = $alias;
693                 if(is_array($aliases) && count($aliases)) {
694                         foreach($aliases as $alias) {
695                                 $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
696                         }
697                 }
698         }
699
700         logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
701
702         return $links;
703
704 }}
705
706
707 // Take a URL from the wild, prepend http:// if necessary
708 // and check DNS to see if it's real (or check if is a valid IP address)
709 // return true if it's OK, false if something is wrong with it
710
711 if(! function_exists('validate_url')) {
712 function validate_url(&$url) {
713
714         if(get_config('system','disable_url_validation'))
715                 return true;
716         // no naked subdomains (allow localhost for tests)
717         if(strpos($url,'.') === false && strpos($url,'/localhost/') === false)
718                 return false;
719         if(substr($url,0,4) != 'http')
720                 $url = 'http://' . $url;
721         $h = @parse_url($url);
722
723         if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
724                 return true;
725         }
726         return false;
727 }}
728
729 // checks that email is an actual resolvable internet address
730
731 if(! function_exists('validate_email')) {
732 function validate_email($addr) {
733
734         if(get_config('system','disable_email_validation'))
735                 return true;
736
737         if(! strpos($addr,'@'))
738                 return false;
739         $h = substr($addr,strpos($addr,'@') + 1);
740
741         if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP) )) {
742                 return true;
743         }
744         return false;
745 }}
746
747 // Check $url against our list of allowed sites,
748 // wildcards allowed. If allowed_sites is unset return true;
749 // If url is allowed, return true.
750 // otherwise, return false
751
752 if(! function_exists('allowed_url')) {
753 function allowed_url($url) {
754
755         $h = @parse_url($url);
756
757         if(! $h) {
758                 return false;
759         }
760
761         $str_allowed = get_config('system','allowed_sites');
762         if(! $str_allowed)
763                 return true;
764
765         $found = false;
766
767         $host = strtolower($h['host']);
768
769         // always allow our own site
770
771         if($host == strtolower($_SERVER['SERVER_NAME']))
772                 return true;
773
774         $fnmatch = function_exists('fnmatch');
775         $allowed = explode(',',$str_allowed);
776
777         if(count($allowed)) {
778                 foreach($allowed as $a) {
779                         $pat = strtolower(trim($a));
780                         if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
781                                 $found = true;
782                                 break;
783                         }
784                 }
785         }
786         return $found;
787 }}
788
789 // check if email address is allowed to register here.
790 // Compare against our list (wildcards allowed).
791 // Returns false if not allowed, true if allowed or if
792 // allowed list is not configured.
793
794 if(! function_exists('allowed_email')) {
795 function allowed_email($email) {
796
797
798         $domain = strtolower(substr($email,strpos($email,'@') + 1));
799         if(! $domain)
800                 return false;
801
802         $str_allowed = get_config('system','allowed_email');
803         if(! $str_allowed)
804                 return true;
805
806         $found = false;
807
808         $fnmatch = function_exists('fnmatch');
809         $allowed = explode(',',$str_allowed);
810
811         if(count($allowed)) {
812                 foreach($allowed as $a) {
813                         $pat = strtolower(trim($a));
814                         if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
815                                 $found = true;
816                                 break;
817                         }
818                 }
819         }
820         return $found;
821 }}
822
823
824 if(! function_exists('avatar_img')) {
825 function avatar_img($email) {
826
827         $a = get_app();
828
829         $avatar['size'] = 175;
830         $avatar['email'] = $email;
831         $avatar['url'] = '';
832         $avatar['success'] = false;
833
834         call_hooks('avatar_lookup', $avatar);
835
836         if(! $avatar['success'])
837                 $avatar['url'] = $a->get_baseurl() . '/images/person-175.jpg';
838
839         logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
840         return $avatar['url'];
841 }}
842
843
844 if(! function_exists('parse_xml_string')) {
845 function parse_xml_string($s,$strict = true) {
846         if($strict) {
847                 if(! strstr($s,'<?xml'))
848                         return false;
849                 $s2 = substr($s,strpos($s,'<?xml'));
850         }
851         else
852                 $s2 = $s;
853         libxml_use_internal_errors(true);
854
855         $x = @simplexml_load_string($s2);
856         if(! $x) {
857                 logger('libxml: parse: error: ' . $s2, LOGGER_DATA);
858                 foreach(libxml_get_errors() as $err)
859                         logger('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, LOGGER_DATA);
860                 libxml_clear_errors();
861         }
862         return $x;
863 }}
864
865 function scale_external_images($srctext, $include_link = true, $scale_replace = false) {
866
867         // Suppress "view full size"
868         if (intval(get_config('system','no_view_full_size')))
869                 $include_link = false;
870
871         $a = get_app();
872
873         // Picture addresses can contain special characters
874         $s = htmlspecialchars_decode($srctext);
875
876         $matches = null;
877         $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
878         if($c) {
879                 require_once('include/Photo.php');
880                 foreach($matches as $mtch) {
881                         logger('scale_external_image: ' . $mtch[1]);
882
883                         $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
884                         if(stristr($mtch[1],$hostname))
885                                 continue;
886
887                         // $scale_replace, if passed, is an array of two elements. The
888                         // first is the name of the full-size image. The second is the
889                         // name of a remote, scaled-down version of the full size image.
890                         // This allows Friendica to display the smaller remote image if
891                         // one exists, while still linking to the full-size image
892                         if($scale_replace)
893                                 $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
894                         else
895                                 $scaled = $mtch[1];
896                         $i = @fetch_url($scaled);
897                         if(! $i)
898                                 return $srctext;
899
900                         // guess mimetype from headers or filename
901                         $type = guess_image_type($mtch[1],true);
902
903                         if($i) {
904                                 $ph = new Photo($i, $type);
905                                 if($ph->is_valid()) {
906                                         $orig_width = $ph->getWidth();
907                                         $orig_height = $ph->getHeight();
908
909                                         if($orig_width > 640 || $orig_height > 640) {
910
911                                                 $ph->scaleImage(640);
912                                                 $new_width = $ph->getWidth();
913                                                 $new_height = $ph->getHeight();
914                                                 logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
915                                                 $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
916                                                         . "\n" . (($include_link)
917                                                                 ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
918                                                                 : ''),$s);
919                                                 logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
920                                         }
921                                 }
922                         }
923                 }
924         }
925
926         // replace the special char encoding
927         $s = htmlspecialchars($s,ENT_NOQUOTES,'UTF-8');
928         return $s;
929 }
930
931
932 function fix_contact_ssl_policy(&$contact,$new_policy) {
933
934         $ssl_changed = false;
935         if((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'],'https:')) {
936                 $ssl_changed = true;
937                 $contact['url']     =   str_replace('https:','http:',$contact['url']);
938                 $contact['request'] =   str_replace('https:','http:',$contact['request']);
939                 $contact['notify']  =   str_replace('https:','http:',$contact['notify']);
940                 $contact['poll']    =   str_replace('https:','http:',$contact['poll']);
941                 $contact['confirm'] =   str_replace('https:','http:',$contact['confirm']);
942                 $contact['poco']    =   str_replace('https:','http:',$contact['poco']);
943         }
944
945         if((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'],'http:')) {
946                 $ssl_changed = true;
947                 $contact['url']     =   str_replace('http:','https:',$contact['url']);
948                 $contact['request'] =   str_replace('http:','https:',$contact['request']);
949                 $contact['notify']  =   str_replace('http:','https:',$contact['notify']);
950                 $contact['poll']    =   str_replace('http:','https:',$contact['poll']);
951                 $contact['confirm'] =   str_replace('http:','https:',$contact['confirm']);
952                 $contact['poco']    =   str_replace('http:','https:',$contact['poco']);
953         }
954
955         if($ssl_changed) {
956                 q("update contact set
957                         url = '%s',
958                         request = '%s',
959                         notify = '%s',
960                         poll = '%s',
961                         confirm = '%s',
962                         poco = '%s'
963                         where id = %d limit 1",
964                         dbesc($contact['url']),
965                         dbesc($contact['request']),
966                         dbesc($contact['notify']),
967                         dbesc($contact['poll']),
968                         dbesc($contact['confirm']),
969                         dbesc($contact['poco']),
970                         intval($contact['id'])
971                 );
972         }
973 }
974
975
976
977 /**
978  * xml2array() will convert the given XML text to an array in the XML structure.
979  * Link: http://www.bin-co.com/php/scripts/xml2array/
980  * Portions significantly re-written by mike@macgirvin.com for Friendica (namespaces, lowercase tags, get_attribute default changed, more...)
981  * Arguments : $contents - The XML text
982  *                $namespaces - true or false include namespace information in the returned array as array elements.
983  *                $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
984  *                $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
985  * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
986  * Examples: $array =  xml2array(file_get_contents('feed.xml'));
987  *              $array =  xml2array(file_get_contents('feed.xml', true, 1, 'attribute'));
988  */
989
990 function xml2array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
991     if(!$contents) return array();
992
993     if(!function_exists('xml_parser_create')) {
994         logger('xml2array: parser function missing');
995         return array();
996     }
997
998
999         libxml_use_internal_errors(true);
1000         libxml_clear_errors();
1001
1002         if($namespaces)
1003             $parser = @xml_parser_create_ns("UTF-8",':');
1004         else
1005             $parser = @xml_parser_create();
1006
1007         if(! $parser) {
1008                 logger('xml2array: xml_parser_create: no resource');
1009                 return array();
1010         }
1011
1012     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
1013         // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
1014     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1015     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
1016     @xml_parse_into_struct($parser, trim($contents), $xml_values);
1017     @xml_parser_free($parser);
1018
1019     if(! $xml_values) {
1020                 logger('xml2array: libxml: parse error: ' . $contents, LOGGER_DATA);
1021                 foreach(libxml_get_errors() as $err)
1022                         logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
1023                 libxml_clear_errors();
1024                 return;
1025         }
1026
1027     //Initializations
1028     $xml_array = array();
1029     $parents = array();
1030     $opened_tags = array();
1031     $arr = array();
1032
1033     $current = &$xml_array; // Reference
1034
1035     // Go through the tags.
1036     $repeated_tag_index = array(); // Multiple tags with same name will be turned into an array
1037     foreach($xml_values as $data) {
1038         unset($attributes,$value); // Remove existing values, or there will be trouble
1039
1040         // This command will extract these variables into the foreach scope
1041         // tag(string), type(string), level(int), attributes(array).
1042         extract($data); // We could use the array by itself, but this cooler.
1043
1044         $result = array();
1045         $attributes_data = array();
1046
1047         if(isset($value)) {
1048             if($priority == 'tag') $result = $value;
1049             else $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
1050         }
1051
1052         //Set the attributes too.
1053         if(isset($attributes) and $get_attributes) {
1054             foreach($attributes as $attr => $val) {
1055                 if($priority == 'tag') $attributes_data[$attr] = $val;
1056                 else $result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
1057             }
1058         }
1059
1060         // See tag status and do the needed.
1061                 if($namespaces && strpos($tag,':')) {
1062                         $namespc = substr($tag,0,strrpos($tag,':'));
1063                         $tag = strtolower(substr($tag,strlen($namespc)+1));
1064                         $result['@namespace'] = $namespc;
1065                 }
1066                 $tag = strtolower($tag);
1067
1068                 if($type == "open") {   // The starting of the tag '<tag>'
1069             $parent[$level-1] = &$current;
1070             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag
1071                 $current[$tag] = $result;
1072                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
1073                 $repeated_tag_index[$tag.'_'.$level] = 1;
1074
1075                 $current = &$current[$tag];
1076
1077             } else { // There was another element with the same tag name
1078
1079                 if(isset($current[$tag][0])) { // If there is a 0th element it is already an array
1080                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1081                     $repeated_tag_index[$tag.'_'.$level]++;
1082                 } else { // This section will make the value an array if multiple tags with the same name appear together
1083                     $current[$tag] = array($current[$tag],$result); // This will combine the existing item and the new item together to make an array
1084                     $repeated_tag_index[$tag.'_'.$level] = 2;
1085
1086                     if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1087                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1088                         unset($current[$tag.'_attr']);
1089                     }
1090
1091                 }
1092                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
1093                 $current = &$current[$tag][$last_item_index];
1094             }
1095
1096         } elseif($type == "complete") { // Tags that ends in 1 line '<tag />'
1097             //See if the key is already taken.
1098             if(!isset($current[$tag])) { //New Key
1099                 $current[$tag] = $result;
1100                 $repeated_tag_index[$tag.'_'.$level] = 1;
1101                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
1102
1103             } else { // If taken, put all things inside a list(array)
1104                 if(isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
1105
1106                     // ...push the new element into that array.
1107                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1108
1109                     if($priority == 'tag' and $get_attributes and $attributes_data) {
1110                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1111                     }
1112                     $repeated_tag_index[$tag.'_'.$level]++;
1113
1114                 } else { // If it is not an array...
1115                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
1116                     $repeated_tag_index[$tag.'_'.$level] = 1;
1117                     if($priority == 'tag' and $get_attributes) {
1118                         if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1119
1120                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1121                             unset($current[$tag.'_attr']);
1122                         }
1123
1124                         if($attributes_data) {
1125                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1126                         }
1127                     }
1128                     $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
1129                 }
1130             }
1131
1132         } elseif($type == 'close') { // End of tag '</tag>'
1133             $current = &$parent[$level-1];
1134         }
1135     }
1136
1137     return($xml_array);
1138 }
1139
1140 function original_url($url, $depth=1, $fetchbody = false) {
1141
1142         $a = get_app();
1143
1144         // Remove Analytics Data from Google and other tracking platforms
1145         $urldata = parse_url($url);
1146         if (is_string($urldata["query"])) {
1147                 $query = $urldata["query"];
1148                 parse_str($query, $querydata);
1149
1150                 if (is_array($querydata))
1151                         foreach ($querydata AS $param=>$value)
1152                                 if (in_array($param, array("utm_source", "utm_medium", "utm_term", "utm_content", "utm_campaign",
1153                                                         "wt_mc", "pk_campaign", "pk_kwd", "mc_cid", "mc_eid",
1154                                                         "fb_action_ids", "fb_action_types", "fb_ref",
1155                                                         "awesm", "wtrid",
1156                                                         "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"))) {
1157
1158                                         $pair = $param."=".urlencode($value);
1159                                         $url = str_replace($pair, "", $url);
1160
1161                                         // Second try: if the url isn't encoded completely
1162                                         $pair = $param."=".str_replace(" ", "+", $value);
1163                                         $url = str_replace($pair, "", $url);
1164
1165                                         // Third try: Maybey the url isn't encoded at all
1166                                         $pair = $param."=".$value;
1167                                         $url = str_replace($pair, "", $url);
1168
1169                                         $url = str_replace(array("?&", "&&"), array("?", ""), $url);
1170                                 }
1171
1172                 if (substr($url, -1, 1) == "?")
1173                         $url = substr($url, 0, -1);
1174         }
1175
1176         if ($depth > 10)
1177                 return($url);
1178
1179         $url = trim($url, "'");
1180
1181         $stamp1 = microtime(true);
1182
1183         $siteinfo = array();
1184         $ch = curl_init();
1185         curl_setopt($ch, CURLOPT_URL, $url);
1186         curl_setopt($ch, CURLOPT_HEADER, 1);
1187         curl_setopt($ch, CURLOPT_NOBODY, 1);
1188         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
1189         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1190         curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
1191
1192         $header = curl_exec($ch);
1193         $curl_info = @curl_getinfo($ch);
1194         $http_code = $curl_info['http_code'];
1195         curl_close($ch);
1196
1197         $a->save_timestamp($stamp1, "network");
1198
1199         if ($http_code == 0)
1200                 return($url);
1201
1202         if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
1203                 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
1204                 if ($curl_info['redirect_url'] != "")
1205                         return(original_url($curl_info['redirect_url'], ++$depth, $fetchbody));
1206                 else
1207                         return(original_url($curl_info['location'], ++$depth, $fetchbody));
1208         }
1209
1210         // Check for redirects in the meta elements of the body if there are no redirects in the header.
1211         if (!$fetchbody)
1212                 return(original_url($url, ++$depth, true));
1213
1214         // if the file is too large then exit
1215         if ($curl_info["download_content_length"] > 1000000)
1216                 return($url);
1217
1218         // if it isn't a HTML file then exit
1219         if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
1220                 return($url);
1221
1222         $stamp1 = microtime(true);
1223
1224         $ch = curl_init();
1225         curl_setopt($ch, CURLOPT_URL, $url);
1226         curl_setopt($ch, CURLOPT_HEADER, 0);
1227         curl_setopt($ch, CURLOPT_NOBODY, 0);
1228         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
1229         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1230         curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
1231
1232         $body = curl_exec($ch);
1233         curl_close($ch);
1234
1235         $a->save_timestamp($stamp1, "network");
1236
1237         if (trim($body) == "")
1238                 return($url);
1239
1240         // Check for redirect in meta elements
1241         $doc = new DOMDocument();
1242         @$doc->loadHTML($body);
1243
1244         $xpath = new DomXPath($doc);
1245
1246         $list = $xpath->query("//meta[@content]");
1247         foreach ($list as $node) {
1248                 $attr = array();
1249                 if ($node->attributes->length)
1250                         foreach ($node->attributes as $attribute)
1251                                 $attr[$attribute->name] = $attribute->value;
1252
1253                 if (@$attr["http-equiv"] == 'refresh') {
1254                         $path = $attr["content"];
1255                         $pathinfo = explode(";", $path);
1256                         $content = "";
1257                         foreach ($pathinfo AS $value)
1258                                 if (substr(strtolower($value), 0, 4) == "url=")
1259                                         return(original_url(substr($value, 4), ++$depth));
1260                 }
1261         }
1262
1263         return($url);
1264 }
1265
1266 if (!function_exists('short_link')) {
1267 function short_link($url) {
1268         require_once('library/slinky.php');
1269         $slinky = new Slinky($url);
1270         $yourls_url = get_config('yourls','url1');
1271         if ($yourls_url) {
1272                 $yourls_username = get_config('yourls','username1');
1273                 $yourls_password = get_config('yourls', 'password1');
1274                 $yourls_ssl = get_config('yourls', 'ssl1');
1275                 $yourls = new Slinky_YourLS();
1276                 $yourls->set('username', $yourls_username);
1277                 $yourls->set('password', $yourls_password);
1278                 $yourls->set('ssl', $yourls_ssl);
1279                 $yourls->set('yourls-url', $yourls_url);
1280                 $slinky->set_cascade( array($yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
1281         } else {
1282                 // setup a cascade of shortening services
1283                 // try to get a short link from these services
1284                 // in the order ur1.ca, trim, id.gd, tinyurl
1285                 $slinky->set_cascade(array(new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL()));
1286         }
1287         return $slinky->short();
1288 }};
1289
1290 /**
1291  * @brief Encodes content to json
1292  * 
1293  * This function encodes an array to json format
1294  * and adds an application/json HTTP header to the output.
1295  * After finishing the process is getting killed.
1296  * 
1297  * @param array $x
1298  */
1299 function json_return_and_die($x) {
1300         header("content-type: application/json");
1301         echo json_encode($x);
1302         killme();
1303 }