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) {
10 $stamp1 = microtime(true);
14 $ch = @curl_init($url);
15 if(($redirects > 8) || (! $ch))
18 @curl_setopt($ch, CURLOPT_HEADER, true);
21 // These settings aren't needed. We're following the location already.
22 // @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
23 // @curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
25 if (!is_null($accept_content)){
26 curl_setopt($ch,CURLOPT_HTTPHEADER, array (
27 "Accept: " . $accept_content
31 @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
32 //@curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
33 @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
36 if(intval($timeout)) {
37 @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
40 $curl_time = intval(get_config('system','curl_timeout'));
41 @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
43 // by default we will allow self-signed certs
44 // but you can override this
46 $check_cert = get_config('system','verifyssl');
47 @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
49 $prx = get_config('system','proxy');
51 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
52 @curl_setopt($ch, CURLOPT_PROXY, $prx);
53 $prxusr = @get_config('system','proxyuser');
55 @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
58 @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
62 // don't let curl abort the entire application
63 // if it throws any errors.
68 $curl_info = @curl_getinfo($ch);
69 $http_code = $curl_info['http_code'];
70 // logger('fetch_url:' . $http_code . ' data: ' . $s);
73 // Pull out multiple headers, e.g. proxy and continuation headers
74 // allow for HTTP/2.x without fixing code
76 while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
77 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
79 $base = substr($base,strlen($chunk));
82 if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
83 $new_location_info = @parse_url($curl_info["redirect_url"]);
84 $old_location_info = @parse_url($curl_info["url"]);
86 $newurl = $curl_info["redirect_url"];
88 if (($new_location_info["path"] == "") AND ($new_location_info["host"] != ""))
89 $newurl = $new_location_info["scheme"]."://".$new_location_info["host"].$old_location_info["path"];
92 if (preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches)) {
93 $newurl = trim(array_pop($matches));
95 if(strpos($newurl,'/') === 0)
96 $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
97 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
99 return fetch_url($newurl,$binary,$redirects,$timeout);
103 $a->set_curl_code($http_code);
104 $a->set_curl_content_type($curl_info['content_type']);
106 $body = substr($s,strlen($header));
107 $a->set_curl_headers($header);
110 $a->save_timestamp($stamp1, "network");
115 // post request to $url. $params is an array of post variables.
117 if(! function_exists('post_url')) {
118 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
120 $stamp1 = microtime(true);
123 $ch = curl_init($url);
124 if(($redirects > 8) || (! $ch))
127 curl_setopt($ch, CURLOPT_HEADER, true);
128 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
129 curl_setopt($ch, CURLOPT_POST,1);
130 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
131 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
133 if(intval($timeout)) {
134 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
137 $curl_time = intval(get_config('system','curl_timeout'));
138 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
141 if(defined('LIGHTTPD')) {
142 if(!is_array($headers)) {
143 $headers = array('Expect:');
145 if(!in_array('Expect:', $headers)) {
146 array_push($headers, 'Expect:');
151 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
153 $check_cert = get_config('system','verifyssl');
154 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false));
155 $prx = get_config('system','proxy');
157 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
158 curl_setopt($ch, CURLOPT_PROXY, $prx);
159 $prxusr = get_config('system','proxyuser');
161 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
164 $a->set_curl_code(0);
166 // don't let curl abort the entire application
167 // if it throws any errors.
169 $s = @curl_exec($ch);
172 $curl_info = curl_getinfo($ch);
173 $http_code = $curl_info['http_code'];
177 // Pull out multiple headers, e.g. proxy and continuation headers
178 // allow for HTTP/2.x without fixing code
180 while(preg_match('/^HTTP\/[1-2].+? [1-5][0-9][0-9]/',$base)) {
181 $chunk = substr($base,0,strpos($base,"\r\n\r\n")+4);
183 $base = substr($base,strlen($chunk));
186 if($http_code == 301 || $http_code == 302 || $http_code == 303) {
188 preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
189 $newurl = trim(array_pop($matches));
190 if(strpos($newurl,'/') === 0)
191 $newurl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $newurl;
192 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
194 return fetch_url($newurl,false,$redirects,$timeout);
197 $a->set_curl_code($http_code);
198 $body = substr($s,strlen($header));
200 $a->set_curl_headers($header);
204 $a->save_timestamp($stamp1, "network");
209 // Generic XML return
210 // Outputs a basic dfrn XML status structure to STDOUT, with a <status> variable
211 // of $st and an optional text <message> of $message and terminates the current process.
213 if(! function_exists('xml_status')) {
214 function xml_status($st, $message = '') {
216 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
219 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
221 header( "Content-type: text/xml" );
222 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
223 echo "<result>\r\n\t<status>$st</status>\r\n$xml_message</result>\r\n";
228 if(! function_exists('http_status_exit')) {
229 function http_status_exit($val) {
234 if($val >= 200 && $val < 300)
237 logger('http_status_exit ' . $val);
238 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
244 // convert an XML document to a normalised, case-corrected array
247 if(! function_exists('convert_xml_element_to_array')) {
248 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
250 // If we're getting too deep, bail out
251 if ($recursion_depth > 512) {
255 if (!is_string($xml_element) &&
256 !is_array($xml_element) &&
257 (get_class($xml_element) == 'SimpleXMLElement')) {
258 $xml_element_copy = $xml_element;
259 $xml_element = get_object_vars($xml_element);
262 if (is_array($xml_element)) {
263 $result_array = array();
264 if (count($xml_element) <= 0) {
265 return (trim(strval($xml_element_copy)));
268 foreach($xml_element as $key=>$value) {
271 $result_array[strtolower($key)] =
272 convert_xml_element_to_array($value, $recursion_depth);
275 if ($recursion_depth == 0) {
276 $temp_array = $result_array;
277 $result_array = array(
278 strtolower($xml_element_copy->getName()) => $temp_array,
282 return ($result_array);
285 return (trim(strval($xml_element)));
289 // Given an email style address, perform webfinger lookup and
290 // return the resulting DFRN profile URL, or if no DFRN profile URL
291 // is located, returns an OStatus subscription template (prefixed
292 // with the string 'stat:' to identify it as on OStatus template).
293 // If this isn't an email style address just return $s.
294 // Return an empty string if email-style addresses but webfinger fails,
295 // or if the resultant personal XRD doesn't contain a supported
296 // subscription/friend-request attribute.
298 // amended 7/9/2011 to return an hcard which could save potentially loading
299 // a lengthy content page to scrape dfrn attributes
301 if(! function_exists('webfinger_dfrn')) {
302 function webfinger_dfrn($s,&$hcard) {
303 if(! strstr($s,'@')) {
308 $links = webfinger($s);
309 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
311 foreach($links as $link) {
312 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
313 $profile_link = $link['@attributes']['href'];
314 if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB)
315 $profile_link = 'stat:' . $link['@attributes']['template'];
316 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
317 $hcard = $link['@attributes']['href'];
320 return $profile_link;
323 // Given an email style address, perform webfinger lookup and
324 // return the array of link attributes from the personal XRD file.
325 // On error/failure return an empty array.
328 if(! function_exists('webfinger')) {
329 function webfinger($s, $debug = false) {
332 $host = substr($s,strpos($s,'@') + 1);
335 $tpl = fetch_lrdd_template($host);
336 logger('webfinger: lrdd template: ' . $tpl);
338 $pxrd = str_replace('{uri}', urlencode('acct:' . $s), $tpl);
339 logger('webfinger: pxrd: ' . $pxrd);
340 $links = fetch_xrd_links($pxrd);
341 if(! count($links)) {
342 // try with double slashes
343 $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl);
344 logger('webfinger: pxrd: ' . $pxrd);
345 $links = fetch_xrd_links($pxrd);
353 if(! function_exists('lrdd')) {
354 function lrdd($uri, $debug = false) {
358 // default priority is host priority, host-meta first
362 // All we have is an email address. Resource-priority is irrelevant
363 // because our URI isn't directly resolvable.
365 if(strstr($uri,'@')) {
366 return(webfinger($uri));
369 // get the host meta file
371 $host = @parse_url($uri);
374 $url = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
375 $url .= $host['host'] . '/.well-known/host-meta' ;
380 logger('lrdd: constructed url: ' . $url);
382 $xml = fetch_url($url);
384 $headers = $a->get_curl_headers();
389 logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
391 if(! stristr($xml,'<xrd'))
394 $h = parse_xml_string($xml);
398 $arr = convert_xml_element_to_array($h);
400 if(isset($arr['xrd']['property'])) {
401 $property = $arr['crd']['property'];
402 if(! isset($property[0]))
403 $properties = array($property);
405 $properties = $property;
406 foreach($properties as $prop)
407 if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
408 $priority = 'resource';
411 // save the links in case we need them
415 if(isset($arr['xrd']['link'])) {
416 $link = $arr['xrd']['link'];
417 if(! isset($link[0]))
418 $links = array($link);
423 // do we have a template or href?
426 foreach($links as $link) {
427 if($link['@attributes']['rel'] && attribute_contains($link['@attributes']['rel'],'lrdd')) {
428 if(x($link['@attributes'],'template'))
429 $tpl = $link['@attributes']['template'];
430 elseif(x($link['@attributes'],'href'))
431 $href = $link['@attributes']['href'];
436 if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
439 if($priority === 'host') {
441 $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
445 logger('lrdd: (host priority) pxrd: ' . $pxrd);
446 $links = fetch_xrd_links($pxrd);
450 $lines = explode("\n",$headers);
452 foreach($lines as $line) {
453 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
454 return(fetch_xrd_links($matches[1]));
462 // priority 'resource'
465 $html = fetch_url($uri);
466 $headers = $a->get_curl_headers();
467 logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
469 // don't try and parse raw xml as html
470 if(! strstr($html,'<?xml')) {
471 require_once('library/HTML5/Parser.php');
474 $dom = HTML5_Parser::parse($html);
475 } catch (DOMException $e) {
476 logger('lrdd: parse error: ' . $e);
479 if(isset($dom) && $dom) {
480 $items = $dom->getElementsByTagName('link');
481 foreach($items as $item) {
482 $x = $item->getAttribute('rel');
484 $pagelink = $item->getAttribute('href');
492 return(fetch_xrd_links($pagelink));
494 // next look in HTTP headers
496 $lines = explode("\n",$headers);
498 foreach($lines as $line) {
499 // TODO alter the following regex to support multiple relations (space separated)
500 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
501 $pagelink = $matches[1];
504 // don't try and run feeds through the html5 parser
505 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
507 if(stristr($html,'<rss') || stristr($html,'<feed'))
513 return(fetch_xrd_links($pagelink));
515 // If we haven't found any links, return the host xrd links (which we have already fetched)
526 // Given a host name, locate the LRDD template from that
527 // host. Returns the LRDD template or an empty string on
530 if(! function_exists('fetch_lrdd_template')) {
531 function fetch_lrdd_template($host) {
534 $url1 = 'https://' . $host . '/.well-known/host-meta' ;
535 $url2 = 'http://' . $host . '/.well-known/host-meta' ;
536 $links = fetch_xrd_links($url1);
537 logger('fetch_lrdd_template from: ' . $url1);
538 logger('template (https): ' . print_r($links,true));
539 if(! count($links)) {
540 logger('fetch_lrdd_template from: ' . $url2);
541 $links = fetch_xrd_links($url2);
542 logger('template (http): ' . print_r($links,true));
545 foreach($links as $link)
546 if($link['@attributes']['rel'] && $link['@attributes']['rel'] === 'lrdd')
547 $tpl = $link['@attributes']['template'];
549 if(! strpos($tpl,'{uri}'))
554 // Given a URL, retrieve the page as an XRD document.
555 // Return an array of links.
556 // on error/failure return empty array.
558 if(! function_exists('fetch_xrd_links')) {
559 function fetch_xrd_links($url) {
561 $xrd_timeout = intval(get_config('system','xrd_timeout'));
563 $xml = fetch_url($url,false,$redirects,(($xrd_timeout) ? $xrd_timeout : 20));
565 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
567 if ((! $xml) || (! stristr($xml,'<xrd')))
570 // fix diaspora's bad xml
571 $xml = str_replace(array('href="','"/>'),array('href="','"/>'),$xml);
573 $h = parse_xml_string($xml);
577 $arr = convert_xml_element_to_array($h);
581 if(isset($arr['xrd']['link'])) {
582 $link = $arr['xrd']['link'];
583 if(! isset($link[0]))
584 $links = array($link);
588 if(isset($arr['xrd']['alias'])) {
589 $alias = $arr['xrd']['alias'];
590 if(! isset($alias[0]))
591 $aliases = array($alias);
594 if(is_array($aliases) && count($aliases)) {
595 foreach($aliases as $alias) {
596 $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
601 logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
608 // Take a URL from the wild, prepend http:// if necessary
609 // and check DNS to see if it's real (or check if is a valid IP address)
610 // return true if it's OK, false if something is wrong with it
612 if(! function_exists('validate_url')) {
613 function validate_url(&$url) {
615 // no naked subdomains (allow localhost for tests)
616 if(strpos($url,'.') === false && strpos($url,'/localhost/') === false)
618 if(substr($url,0,4) != 'http')
619 $url = 'http://' . $url;
620 $h = @parse_url($url);
622 if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
628 // checks that email is an actual resolvable internet address
630 if(! function_exists('validate_email')) {
631 function validate_email($addr) {
633 if(get_config('system','disable_email_validation'))
636 if(! strpos($addr,'@'))
638 $h = substr($addr,strpos($addr,'@') + 1);
640 if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
646 // Check $url against our list of allowed sites,
647 // wildcards allowed. If allowed_sites is unset return true;
648 // If url is allowed, return true.
649 // otherwise, return false
651 if(! function_exists('allowed_url')) {
652 function allowed_url($url) {
654 $h = @parse_url($url);
660 $str_allowed = get_config('system','allowed_sites');
666 $host = strtolower($h['host']);
668 // always allow our own site
670 if($host == strtolower($_SERVER['SERVER_NAME']))
673 $fnmatch = function_exists('fnmatch');
674 $allowed = explode(',',$str_allowed);
676 if(count($allowed)) {
677 foreach($allowed as $a) {
678 $pat = strtolower(trim($a));
679 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
688 // check if email address is allowed to register here.
689 // Compare against our list (wildcards allowed).
690 // Returns false if not allowed, true if allowed or if
691 // allowed list is not configured.
693 if(! function_exists('allowed_email')) {
694 function allowed_email($email) {
697 $domain = strtolower(substr($email,strpos($email,'@') + 1));
701 $str_allowed = get_config('system','allowed_email');
707 $fnmatch = function_exists('fnmatch');
708 $allowed = explode(',',$str_allowed);
710 if(count($allowed)) {
711 foreach($allowed as $a) {
712 $pat = strtolower(trim($a));
713 if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
723 if(! function_exists('avatar_img')) {
724 function avatar_img($email) {
728 $avatar['size'] = 175;
729 $avatar['email'] = $email;
731 $avatar['success'] = false;
733 call_hooks('avatar_lookup', $avatar);
735 if(! $avatar['success'])
736 $avatar['url'] = $a->get_baseurl() . '/images/person-175.jpg';
738 logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
739 return $avatar['url'];
743 if(! function_exists('parse_xml_string')) {
744 function parse_xml_string($s,$strict = true) {
746 if(! strstr($s,'<?xml'))
748 $s2 = substr($s,strpos($s,'<?xml'));
752 libxml_use_internal_errors(true);
754 $x = @simplexml_load_string($s2);
756 logger('libxml: parse: error: ' . $s2, LOGGER_DATA);
757 foreach(libxml_get_errors() as $err)
758 logger('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, LOGGER_DATA);
759 libxml_clear_errors();
764 function add_fcontact($arr,$update = false) {
767 $r = q("UPDATE `fcontact` SET
780 WHERE `url` = '%s' AND `network` = '%s' LIMIT 1",
782 dbesc($arr['photo']),
783 dbesc($arr['request']),
786 dbesc($arr['batch']),
787 dbesc($arr['notify']),
789 dbesc($arr['confirm']),
790 dbesc($arr['alias']),
791 dbesc($arr['pubkey']),
792 dbesc(datetime_convert()),
794 dbesc($arr['network'])
798 $r = q("insert into fcontact ( `url`,`name`,`photo`,`request`,`nick`,`addr`,
799 `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated` )
800 values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
803 dbesc($arr['photo']),
804 dbesc($arr['request']),
807 dbesc($arr['batch']),
808 dbesc($arr['notify']),
810 dbesc($arr['confirm']),
811 dbesc($arr['network']),
812 dbesc($arr['alias']),
813 dbesc($arr['pubkey']),
814 dbesc(datetime_convert())
822 function scale_external_images($srctext, $include_link = true, $scale_replace = false) {
826 // Picture addresses can contain special characters
827 $s = htmlspecialchars_decode($srctext);
830 $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
832 require_once('include/Photo.php');
833 foreach($matches as $mtch) {
834 logger('scale_external_image: ' . $mtch[1]);
836 $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
837 if(stristr($mtch[1],$hostname))
840 // $scale_replace, if passed, is an array of two elements. The
841 // first is the name of the full-size image. The second is the
842 // name of a remote, scaled-down version of the full size image.
843 // This allows Friendica to display the smaller remote image if
844 // one exists, while still linking to the full-size image
846 $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
849 $i = @fetch_url($scaled);
853 $cachefile = get_cachefile(hash("md5", $scaled));
854 if ($cachefile != '') {
855 $stamp1 = microtime(true);
856 file_put_contents($cachefile, $i);
857 $a->save_timestamp($stamp1, "file");
860 // guess mimetype from headers or filename
861 $type = guess_image_type($mtch[1],true);
864 $ph = new Photo($i, $type);
865 if($ph->is_valid()) {
866 $orig_width = $ph->getWidth();
867 $orig_height = $ph->getHeight();
869 if($orig_width > 640 || $orig_height > 640) {
871 $ph->scaleImage(640);
872 $new_width = $ph->getWidth();
873 $new_height = $ph->getHeight();
874 logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG);
875 $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $scaled . '[/img]'
876 . "\n" . (($include_link)
877 ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n"
879 logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
886 // replace the special char encoding
887 $s = htmlspecialchars($s,ENT_NOQUOTES,'UTF-8');
892 function fix_contact_ssl_policy(&$contact,$new_policy) {
894 $ssl_changed = false;
895 if((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'],'https:')) {
897 $contact['url'] = str_replace('https:','http:',$contact['url']);
898 $contact['request'] = str_replace('https:','http:',$contact['request']);
899 $contact['notify'] = str_replace('https:','http:',$contact['notify']);
900 $contact['poll'] = str_replace('https:','http:',$contact['poll']);
901 $contact['confirm'] = str_replace('https:','http:',$contact['confirm']);
902 $contact['poco'] = str_replace('https:','http:',$contact['poco']);
905 if((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'],'http:')) {
907 $contact['url'] = str_replace('http:','https:',$contact['url']);
908 $contact['request'] = str_replace('http:','https:',$contact['request']);
909 $contact['notify'] = str_replace('http:','https:',$contact['notify']);
910 $contact['poll'] = str_replace('http:','https:',$contact['poll']);
911 $contact['confirm'] = str_replace('http:','https:',$contact['confirm']);
912 $contact['poco'] = str_replace('http:','https:',$contact['poco']);
916 q("update contact set
923 where id = %d limit 1",
924 dbesc($contact['url']),
925 dbesc($contact['request']),
926 dbesc($contact['notify']),
927 dbesc($contact['poll']),
928 dbesc($contact['confirm']),
929 dbesc($contact['poco']),
930 intval($contact['id'])
938 * xml2array() will convert the given XML text to an array in the XML structure.
939 * Link: http://www.bin-co.com/php/scripts/xml2array/
940 * Portions significantly re-written by mike@macgirvin.com for Friendica (namespaces, lowercase tags, get_attribute default changed, more...)
941 * Arguments : $contents - The XML text
942 * $namespaces - true or false include namespace information in the returned array as array elements.
943 * $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.
944 * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
945 * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
946 * Examples: $array = xml2array(file_get_contents('feed.xml'));
947 * $array = xml2array(file_get_contents('feed.xml', true, 1, 'attribute'));
950 function xml2array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
951 if(!$contents) return array();
953 if(!function_exists('xml_parser_create')) {
954 logger('xml2array: parser function missing');
959 libxml_use_internal_errors(true);
960 libxml_clear_errors();
963 $parser = @xml_parser_create_ns("UTF-8",':');
965 $parser = @xml_parser_create();
968 logger('xml2array: xml_parser_create: no resource');
972 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
973 // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
974 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
975 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
976 @xml_parse_into_struct($parser, trim($contents), $xml_values);
977 @xml_parser_free($parser);
980 logger('xml2array: libxml: parse error: ' . $contents, LOGGER_DATA);
981 foreach(libxml_get_errors() as $err)
982 logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
983 libxml_clear_errors();
988 $xml_array = array();
990 $opened_tags = array();
993 $current = &$xml_array; // Reference
995 // Go through the tags.
996 $repeated_tag_index = array(); // Multiple tags with same name will be turned into an array
997 foreach($xml_values as $data) {
998 unset($attributes,$value); // Remove existing values, or there will be trouble
1000 // This command will extract these variables into the foreach scope
1001 // tag(string), type(string), level(int), attributes(array).
1002 extract($data); // We could use the array by itself, but this cooler.
1005 $attributes_data = array();
1008 if($priority == 'tag') $result = $value;
1009 else $result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
1012 //Set the attributes too.
1013 if(isset($attributes) and $get_attributes) {
1014 foreach($attributes as $attr => $val) {
1015 if($priority == 'tag') $attributes_data[$attr] = $val;
1016 else $result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
1020 // See tag status and do the needed.
1021 if($namespaces && strpos($tag,':')) {
1022 $namespc = substr($tag,0,strrpos($tag,':'));
1023 $tag = strtolower(substr($tag,strlen($namespc)+1));
1024 $result['@namespace'] = $namespc;
1026 $tag = strtolower($tag);
1028 if($type == "open") { // The starting of the tag '<tag>'
1029 $parent[$level-1] = &$current;
1030 if(!is_array($current) or (!in_array($tag, array_keys($current)))) { // Insert New tag
1031 $current[$tag] = $result;
1032 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
1033 $repeated_tag_index[$tag.'_'.$level] = 1;
1035 $current = &$current[$tag];
1037 } else { // There was another element with the same tag name
1039 if(isset($current[$tag][0])) { // If there is a 0th element it is already an array
1040 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1041 $repeated_tag_index[$tag.'_'.$level]++;
1042 } else { // This section will make the value an array if multiple tags with the same name appear together
1043 $current[$tag] = array($current[$tag],$result); // This will combine the existing item and the new item together to make an array
1044 $repeated_tag_index[$tag.'_'.$level] = 2;
1046 if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1047 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1048 unset($current[$tag.'_attr']);
1052 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
1053 $current = &$current[$tag][$last_item_index];
1056 } elseif($type == "complete") { // Tags that ends in 1 line '<tag />'
1057 //See if the key is already taken.
1058 if(!isset($current[$tag])) { //New Key
1059 $current[$tag] = $result;
1060 $repeated_tag_index[$tag.'_'.$level] = 1;
1061 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
1063 } else { // If taken, put all things inside a list(array)
1064 if(isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
1066 // ...push the new element into that array.
1067 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1069 if($priority == 'tag' and $get_attributes and $attributes_data) {
1070 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1072 $repeated_tag_index[$tag.'_'.$level]++;
1074 } else { // If it is not an array...
1075 $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
1076 $repeated_tag_index[$tag.'_'.$level] = 1;
1077 if($priority == 'tag' and $get_attributes) {
1078 if(isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
1080 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1081 unset($current[$tag.'_attr']);
1084 if($attributes_data) {
1085 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1088 $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
1092 } elseif($type == 'close') { // End of tag '</tag>'
1093 $current = &$parent[$level-1];