4 // curl wrapper. If binary flag is true, return binary
7 if(! function_exists('fetch_url')) {
8 function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null) {
12 $ch = @curl_init($url);
13 if(($redirects > 8) || (! $ch))
16 @curl_setopt($ch, CURLOPT_HEADER, true);
19 // These settings aren't needed. We're following the location already.
20 // @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
21 // @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
23 if (!is_null($accept_content)){
24 curl_setopt($ch,CURLOPT_HTTPHEADER, array (
25 "Accept: " . $accept_content
29 @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
30 //@curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
31 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
34 if(intval($timeout)) {
35 @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
38 $curl_time = intval(get_config('system','curl_timeout'));
39 @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
41 // by default we will allow self-signed certs
42 // but you can override this
44 $check_cert = get_config('system','verifyssl');
45 @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
47 $prx = get_config('system','proxy');
49 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
50 @curl_setopt($ch, CURLOPT_PROXY, $prx);
51 $prxusr = @get_config('system','proxyuser');
53 @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
56 @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
60 // don't let curl abort the entire application
61 // if it throws any errors.
66 $curl_info = @curl_getinfo($ch);
67 $http_code = $curl_info['http_code'];
68 // logger('fetch_url:' . $http_code . ' data: ' . $s);
71 // Pull out multiple headers, e.g. proxy and continuation headers
72 // allow for HTTP/2.x without fixing code
74 while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
75 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
77 $base = substr($base,strlen($chunk));
80 if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
82 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
83 $newurl = trim(array_pop($matches));
84 if(strpos($newurl,'/') === 0)
85 $newurl = $url . $newurl;
86 $url_parsed = @parse_url($newurl);
87 if (isset($url_parsed)) {
89 return fetch_url($newurl,$binary,$redirects,$timeout);
93 $a->set_curl_code($http_code);
95 $body = substr($s,strlen($header));
96 $a->set_curl_headers($header);
101 // post request to $url. $params is an array of post variables.
103 if(! function_exists('post_url')) {
104 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
106 $ch = curl_init($url);
107 if(($redirects > 8) || (! $ch))
110 curl_setopt($ch, CURLOPT_HEADER, true);
111 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
112 curl_setopt($ch, CURLOPT_POST,1);
113 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
114 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
116 if(intval($timeout)) {
117 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
120 $curl_time = intval(get_config('system','curl_timeout'));
121 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
124 if(defined('LIGHTTPD')) {
125 if(!is_array($headers)) {
126 $headers = array('Expect:');
128 if(!in_array('Expect:', $headers)) {
129 array_push($headers, 'Expect:');
134 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
136 $check_cert = get_config('system','verifyssl');
137 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
138 $prx = get_config('system','proxy');
140 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
141 curl_setopt($ch, CURLOPT_PROXY, $prx);
142 $prxusr = get_config('system','proxyuser');
144 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
147 $a->set_curl_code(0);
149 // don't let curl abort the entire application
150 // if it throws any errors.
152 $s = @curl_exec($ch);
155 $curl_info = curl_getinfo($ch);
156 $http_code = $curl_info['http_code'];
160 // Pull out multiple headers, e.g. proxy and continuation headers
161 // allow for HTTP/2.x without fixing code
163 while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
164 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
166 $base = substr($base,strlen($chunk));
169 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
171 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
172 $newurl = trim(array_pop($matches));
173 if(strpos($newurl,'/') === 0)
174 $newurl = $url . $newurl;
175 $url_parsed = @parse_url($newurl);
176 if (isset($url_parsed)) {
178 return fetch_url($newurl,false,$redirects,$timeout);
181 $a->set_curl_code($http_code);
182 $body = substr($s,strlen($header));
184 $a->set_curl_headers($header);
190 // Generic XML return
191 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
192 // of $st and an optional text <message> of $message and terminates the current process.
194 if(! function_exists('xml_status')) {
195 function xml_status($st, $message = '') {
197 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
200 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
202 header( "Content-type: text/xml" );
203 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
204 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
209 if(! function_exists('http_status_exit')) {
210 function http_status_exit($val) {
215 if($val >= 200 && $val < 300)
218 logger('http_status_exit ' . $val);
219 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
225 // convert an XML document to a normalised, case-corrected array
228 if(! function_exists('convert_xml_element_to_array')) {
229 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
231 // If we're getting too deep, bail out
232 if ($recursion_depth > 512) {
236 if (!is_string($xml_element) &&
237 !is_array($xml_element) &&
238 (get_class($xml_element) == 'SimpleXMLElement')) {
239 $xml_element_copy = $xml_element;
240 $xml_element = get_object_vars($xml_element);
243 if (is_array($xml_element)) {
244 $result_array = array();
245 if (count($xml_element) <= 0) {
246 return (trim(strval($xml_element_copy)));
249 foreach($xml_element as $key=>$value) {
252 $result_array[strtolower($key)] =
253 convert_xml_element_to_array($value, $recursion_depth);
256 if ($recursion_depth == 0) {
257 $temp_array = $result_array;
258 $result_array = array(
259 strtolower($xml_element_copy->getName()) => $temp_array,
263 return ($result_array);
266 return (trim(strval($xml_element)));
270 // Given an email style address, perform webfinger lookup and
271 // return the resulting DFRN profile URL, or if no DFRN profile URL
272 // is located, returns an OStatus subscription template (prefixed
273 // with the string 'stat:' to identify it as on OStatus template).
274 // If this isn't an email style address just return $s.
275 // Return an empty string if email-style addresses but webfinger fails,
276 // or if the resultant personal XRD doesn't contain a supported
277 // subscription/friend-request attribute.
279 // amended 7/9/2011 to return an hcard which could save potentially loading
280 // a lengthy content page to scrape dfrn attributes
282 if(! function_exists('webfinger_dfrn')) {
283 function webfinger_dfrn($s,&$hcard) {
284 if(! strstr($s,'@')) {
289 $links = webfinger($s);
290 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
292 foreach($links as $link) {
293 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
294 $profile_link = $link['@attributes']['href'];
295 if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
296 $profile_link = 'stat:' . $link['@attributes']['template'];
297 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
298 $hcard = $link['@attributes']['href'];
301 return $profile_link;
304 // Given an email style address, perform webfinger lookup and
305 // return the array of link attributes from the personal XRD file.
306 // On error/failure return an empty array.
309 if(! function_exists('webfinger')) {
310 function webfinger($s, $debug = false) {
313 $host = substr($s,strpos($s,'@') + 1);
316 $tpl = fetch_lrdd_template($host);
317 logger('webfinger: lrdd template: ' . $tpl);
319 $pxrd = str_replace('{uri}', urlencode('acct:' . $s), $tpl);
320 logger('webfinger: pxrd: ' . $pxrd);
321 $links = fetch_xrd_links($pxrd);
322 if(! count($links)) {
323 // try with double slashes
324 $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl);
325 logger('webfinger: pxrd: ' . $pxrd);
326 $links = fetch_xrd_links($pxrd);
334 if(! function_exists('lrdd')) {
335 function lrdd($uri, $debug = false) {
339 // default priority is host priority, host-meta first
343 // All we have is an email address. Resource-priority is irrelevant
344 // because our URI isn't directly resolvable.
346 if(strstr($uri,'@')) {
347 return(webfinger($uri));
350 // get the host meta file
352 $host = @parse_url($uri);
355 $url = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
356 $url .= $host['host'] . '/.well-known/host-meta' ;
361 logger('lrdd: constructed url: ' . $url);
363 $xml = fetch_url($url);
364 $headers = $a->get_curl_headers();
369 logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
371 if(! stristr($xml,'<xrd'))
374 $h = parse_xml_string($xml);
378 $arr = convert_xml_element_to_array($h);
380 if(isset($arr['xrd']['property'])) {
381 $property = $arr['crd']['property'];
382 if(! isset($property[0]))
383 $properties = array($property);
385 $properties = $property;
386 foreach($properties as $prop)
387 if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
388 $priority = 'resource';
391 // save the links in case we need them
395 if(isset($arr['xrd']['link'])) {
396 $link = $arr['xrd']['link'];
397 if(! isset($link[0]))
398 $links = array($link);
403 // do we have a template or href?
406 foreach($links as $link) {
407 if($link['@attributes']['rel'] && attribute_contains($link['@attributes']['rel'],'lrdd')) {
408 if(x($link['@attributes'],'template'))
409 $tpl = $link['@attributes']['template'];
410 elseif(x($link['@attributes'],'href'))
411 $href = $link['@attributes']['href'];
416 if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
419 if($priority === 'host') {
421 $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
425 logger('lrdd: (host priority) pxrd: ' . $pxrd);
426 $links = fetch_xrd_links($pxrd);
430 $lines = explode("\n",$headers);
432 foreach($lines as $line) {
433 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
434 return(fetch_xrd_links($matches[1]));
442 // priority 'resource'
445 $html = fetch_url($uri);
446 $headers = $a->get_curl_headers();
447 logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
449 // don't try and parse raw xml as html
450 if(! strstr($html,'<?xml')) {
451 require_once('library/HTML5/Parser.php');
454 $dom = HTML5_Parser::parse($html);
455 } catch (DOMException $e) {
456 logger('lrdd: parse error: ' . $e);
459 if(isset($dom) && $dom) {
460 $items = $dom->getElementsByTagName('link');
461 foreach($items as $item) {
462 $x = $item->getAttribute('rel');
464 $pagelink = $item->getAttribute('href');
472 return(fetch_xrd_links($pagelink));
474 // next look in HTTP headers
476 $lines = explode("\n",$headers);
478 foreach($lines as $line) {
479 // TODO alter the following regex to support multiple relations (space separated)
480 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
481 $pagelink = $matches[1];
484 // don't try and run feeds through the html5 parser
485 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
487 if(stristr($html,'<rss') || stristr($html,'<feed'))
493 return(fetch_xrd_links($pagelink));
495 // If we haven't found any links, return the host xrd links (which we have already fetched)
506 // Given a host name, locate the LRDD template from that
507 // host. Returns the LRDD template or an empty string on
510 if(! function_exists('fetch_lrdd_template')) {
511 function fetch_lrdd_template($host) {
514 $url1 = 'https://' . $host . '/.well-known/host-meta' ;
515 $url2 = 'http://' . $host . '/.well-known/host-meta' ;
516 $links = fetch_xrd_links($url1);
517 logger('fetch_lrdd_template from: ' . $url1);
518 logger('template (https): ' . print_r($links,true));
519 if(! count($links)) {
520 logger('fetch_lrdd_template from: ' . $url2);
521 $links = fetch_xrd_links($url2);
522 logger('template (http): ' . print_r($links,true));
525 foreach($links as $link)
526 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
527 $tpl = $link['@attributes']['template'];
529 if(! strpos($tpl,'{uri}'))
534 // Given a URL, retrieve the page as an XRD document.
535 // Return an array of links.
536 // on error/failure return empty array.
538 if(! function_exists('fetch_xrd_links')) {
539 function fetch_xrd_links($url) {
541 $xrd_timeout = intval(get_config('system','xrd_timeout'));
543 $xml = fetch_url($url,false,$redirects,(($xrd_timeout) ? $xrd_timeout : 20));
545 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
547 if ((! $xml) || (! stristr($xml,'<xrd')))
550 // fix diaspora's bad xml
551 $xml = str_replace(array('href="','"/>'),array('href="','"/>'),$xml);
553 $h = parse_xml_string($xml);
557 $arr = convert_xml_element_to_array($h);
561 if(isset($arr['xrd']['link'])) {
562 $link = $arr['xrd']['link'];
563 if(! isset($link[0]))
564 $links = array($link);
568 if(isset($arr['xrd']['alias'])) {
569 $alias = $arr['xrd']['alias'];
570 if(! isset($alias[0]))
571 $aliases = array($alias);
574 if(is_array($aliases) && count($aliases)) {
575 foreach($aliases as $alias) {
576 $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
581 logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
588 // Take a URL from the wild, prepend http:// if necessary
589 // and check DNS to see if it's real (or check if is a valid IP address)
590 // return true if it's OK, false if something is wrong with it
592 if(! function_exists('validate_url')) {
593 function validate_url(&$url) {
595 // no naked subdomains (allow localhost for tests)
596 if(strpos($url,'.') === false && strpos($url,'/localhost/') === false)
598 if(substr($url,0,4) != 'http')
599 $url = 'http://' . $url;
600 $h = @parse_url($url);
602 if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
608 // checks that email is an actual resolvable internet address
610 if(! function_exists('validate_email')) {
611 function validate_email($addr) {
613 if(get_config('system','disable_email_validation'))
616 if(! strpos($addr,'@'))
618 $h = substr($addr,strpos($addr,'@') + 1);
620 if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
626 // Check $url against our list of allowed sites,
627 // wildcards allowed. If allowed_sites is unset return true;
628 // If url is allowed, return true.
629 // otherwise, return false
631 if(! function_exists('allowed_url')) {
632 function allowed_url($url) {
634 $h = @parse_url($url);
640 $str_allowed = get_config('system','allowed_sites');
646 $host = strtolower($h['host']);
648 // always allow our own site
650 if($host == strtolower($_SERVER['SERVER_NAME']))
653 $fnmatch = function_exists('fnmatch');
654 $allowed = explode(',',$str_allowed);
656 if(count($allowed)) {
657 foreach($allowed as $a) {
658 $pat = strtolower(trim($a));
659 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
668 // check if email address is allowed to register here.
669 // Compare against our list (wildcards allowed).
670 // Returns false if not allowed, true if allowed or if
671 // allowed list is not configured.
673 if(! function_exists('allowed_email')) {
674 function allowed_email($email) {
677 $domain = strtolower(substr($email,strpos($email,'@') + 1));
681 $str_allowed = get_config('system','allowed_email');
687 $fnmatch = function_exists('fnmatch');
688 $allowed = explode(',',$str_allowed);
690 if(count($allowed)) {
691 foreach($allowed as $a) {
692 $pat = strtolower(trim($a));
693 if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
703 if(! function_exists('avatar_img')) {
704 function avatar_img($email) {
708 $avatar['size'] = 175;
709 $avatar['email'] = $email;
711 $avatar['success'] = false;
713 call_hooks('avatar_lookup', $avatar);
715 if(! $avatar['success'])
716 $avatar['url'] = $a->get_baseurl() . '/images/person-175.jpg';
718 logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
719 return $avatar['url'];
723 if(! function_exists('parse_xml_string')) {
724 function parse_xml_string($s,$strict = true) {
726 if(! strstr($s,'<?xml'))
728 $s2 = substr($s,strpos($s,'<?xml'));
732 libxml_use_internal_errors(true);
734 $x = @simplexml_load_string($s2);
736 logger('libxml: parse: error: ' . $s2, LOGGER_DATA);
737 foreach(libxml_get_errors() as $err)
738 logger('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, LOGGER_DATA);
739 libxml_clear_errors();
744 function add_fcontact($arr,$update = false) {
747 $r = q("UPDATE `fcontact` SET
760 WHERE `url` = '%s' AND `network` = '%s' LIMIT 1",
762 dbesc($arr['photo']),
763 dbesc($arr['request']),
766 dbesc($arr['batch']),
767 dbesc($arr['notify']),
769 dbesc($arr['confirm']),
770 dbesc($arr['alias']),
771 dbesc($arr['pubkey']),
772 dbesc(datetime_convert()),
774 dbesc($arr['network'])
778 $r = q("insert into fcontact ( `url`,`name`,`photo`,`request`,`nick`,`addr`,
779 `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated` )
780 values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
783 dbesc($arr['photo']),
784 dbesc($arr['request']),
787 dbesc($arr['batch']),
788 dbesc($arr['notify']),
790 dbesc($arr['confirm']),
791 dbesc($arr['network']),
792 dbesc($arr['alias']),
793 dbesc($arr['pubkey']),
794 dbesc(datetime_convert())
802 function scale_external_images($s, $include_link = true, $scale_replace = false) {
806 // Picture addresses can contain special characters
807 $s = htmlspecialchars_decode($s);
810 $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
812 require_once('include/Photo.php');
813 foreach($matches as $mtch) {
814 logger('scale_external_image: ' . $mtch[1]);
816 $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
817 if(stristr($mtch[1],$hostname))
820 // $scale_replace, if passed, is an array of two elements. The
821 // first is the name of the full-size image. The second is the
822 // name of a remote, scaled-down version of the full size image.
823 // This allows Friendica to display the smaller remote image if
824 // one exists, while still linking to the full-size image
826 $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
829 $i = fetch_url($scaled);
831 $cachefile = get_cachefile(hash("md5", $scaled));
832 if ($cachefile != '')
833 file_put_contents($cachefile, $i);
835 // guess mimetype from headers or filename
836 $type = guess_image_type($mtch[1],true);
839 $ph = new Photo($i, $type);
840 if($ph->is_valid()) {
841 $orig_width = $ph->getWidth();
842 $orig_height = $ph->getHeight();
844 if($orig_width > 640 || $orig_height > 640) {
846 $ph->scaleImage(640);
847 $new_width = $ph->getWidth();
848 $new_height = $ph->getHeight();
849 logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
850 $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
851 . "\n" . (($include_link)
852 ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
854 logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
861 // replace the special char encoding
862 $s = htmlspecialchars($s,ENT_NOQUOTES,'UTF-8');
867 function fix_contact_ssl_policy(&$contact,$new_policy) {
869 $ssl_changed = false;
870 if((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'],'https:')) {
872 $contact['url'] = str_replace('https:','http:',$contact['url']);
873 $contact['request'] = str_replace('https:','http:',$contact['request']);
874 $contact['notify'] = str_replace('https:','http:',$contact['notify']);
875 $contact['poll'] = str_replace('https:','http:',$contact['poll']);
876 $contact['confirm'] = str_replace('https:','http:',$contact['confirm']);
877 $contact['poco'] = str_replace('https:','http:',$contact['poco']);
880 if((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'],'http:')) {
882 $contact['url'] = str_replace('http:','https:',$contact['url']);
883 $contact['request'] = str_replace('http:','https:',$contact['request']);
884 $contact['notify'] = str_replace('http:','https:',$contact['notify']);
885 $contact['poll'] = str_replace('http:','https:',$contact['poll']);
886 $contact['confirm'] = str_replace('http:','https:',$contact['confirm']);
887 $contact['poco'] = str_replace('http:','https:',$contact['poco']);
891 q("update contact set
898 where id = %d limit 1",
899 dbesc($contact['url']),
900 dbesc($contact['request']),
901 dbesc($contact['notify']),
902 dbesc($contact['poll']),
903 dbesc($contact['confirm']),
904 dbesc($contact['poco']),
905 intval($contact['id'])
913 * xml2array() will convert the given XML text to an array in the XML structure.
914 * Link: http://www.bin-co.com/php/scripts/xml2array/
915 * Portions significantly re-written by mike@macgirvin.com for Friendica (namespaces, lowercase tags, get_attribute default changed, more...)
916 * Arguments : $contents - The XML text
917 * $namespaces - true or false include namespace information in the returned array as array elements.
918 * $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.
919 * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
920 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
921 * Examples: $array = xml2array(file_get_contents('feed.xml'));
922 * $array = xml2array(file_get_contents('feed.xml', true, 1, 'attribute'));
925 function xml2array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
926 if(!$contents) return array();
928 if(!function_exists('xml_parser_create')) {
929 logger('xml2array: parser function missing');
934 libxml_use_internal_errors(true);
935 libxml_clear_errors();
938 $parser = @xml_parser_create_ns("UTF-8",':');
940 $parser = @xml_parser_create();
943 logger('xml2array: xml_parser_create: no resource');
947 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
948 // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
949 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
950 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
951 @xml_parse_into_struct($parser, trim($contents), $xml_values);
952 @xml_parser_free($parser);
955 logger('xml2array: libxml: parse error: ' . $contents, LOGGER_DATA);
956 foreach(libxml_get_errors() as $err)
957 logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
958 libxml_clear_errors();
963 $xml_array = array();
965 $opened_tags = array();
968 $current = &$xml_array; // Reference
970 // Go through the tags.
971 $repeated_tag_index = array(); // Multiple tags with same name will be turned into an array
972 foreach($xml_values as $data) {
973 unset($attributes,$value); // Remove existing values, or there will be trouble
975 // This command will extract these variables into the foreach scope
976 // tag(string), type(string), level(int), attributes(array).
977 extract($data); // We could use the array by itself, but this cooler.
980 $attributes_data = array();
983 if($priority == 'tag') $result = $value;
984 else $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
987 //Set the attributes too.
988 if(isset($attributes) and $get_attributes) {
989 foreach($attributes as $attr => $val) {
990 if($priority == 'tag') $attributes_data[$attr] = $val;
991 else $result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
995 // See tag status and do the needed.
996 if($namespaces && strpos($tag,':')) {
997 $namespc = substr($tag,0,strrpos($tag,':'));
998 $tag = strtolower(substr($tag,strlen($namespc)+1));
999 $result['@namespace'] = $namespc;
1001 $tag = strtolower($tag);
1003 if($type == "open") { // The starting of the tag '<tag>'
1004 $parent[$level-1] = &$current;
1005 if(!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag
1006 $current[$tag] = $result;
1007 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
1008 $repeated_tag_index[$tag.'_'.$level] = 1;
1010 $current = &$current[$tag];
1012 } else { // There was another element with the same tag name
1014 if(isset($current[$tag][0])) { // If there is a 0th element it is already an array
1015 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1016 $repeated_tag_index[$tag.'_'.$level]++;
1017 } else { // This section will make the value an array if multiple tags with the same name appear together
1018 $current[$tag] = array($current[$tag],$result); // This will combine the existing item and the new item together to make an array
1019 $repeated_tag_index[$tag.'_'.$level] = 2;
1021 if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1022 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1023 unset($current[$tag.'_attr']);
1027 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
1028 $current = &$current[$tag][$last_item_index];
1031 } elseif($type == "complete") { // Tags that ends in 1 line '<tag />'
1032 //See if the key is already taken.
1033 if(!isset($current[$tag])) { //New Key
1034 $current[$tag] = $result;
1035 $repeated_tag_index[$tag.'_'.$level] = 1;
1036 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
1038 } else { // If taken, put all things inside a list(array)
1039 if(isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
1041 // ...push the new element into that array.
1042 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1044 if($priority == 'tag' and $get_attributes and $attributes_data) {
1045 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1047 $repeated_tag_index[$tag.'_'.$level]++;
1049 } else { // If it is not an array...
1050 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
1051 $repeated_tag_index[$tag.'_'.$level] = 1;
1052 if($priority == 'tag' and $get_attributes) {
1053 if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1055 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1056 unset($current[$tag.'_attr']);
1059 if($attributes_data) {
1060 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1063 $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
1067 } elseif($type == 'close') { // End of tag '</tag>'
1068 $current = &$parent[$level-1];