4 // curl wrapper. If binary flag is true, return binary
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) {
16 array('timeout'=>$timeout,
17 'accept_content'=>$accept_content,
18 'cookiejar'=>$cookiejar
24 if(!function_exists('z_fetch_url')){
26 * @brief fetches an URL.
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
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
49 function z_fetch_url($url,$binary = false, &$redirects = 0, $opts=array()) {
51 $ret = array('return_code' => 0, 'success' => false, 'header' => "", 'body' => "");
54 $stamp1 = microtime(true);
58 $ch = @curl_init($url);
59 if(($redirects > 8) || (! $ch))
62 @curl_setopt($ch, CURLOPT_HEADER, true);
64 if(x($opts,"cookiejar")) {
65 curl_setopt($ch, CURLOPT_COOKIEJAR, $opts["cookiejar"]);
66 curl_setopt($ch, CURLOPT_COOKIEFILE, $opts["cookiejar"]);
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);
73 if (x($opts,'accept_content')){
74 curl_setopt($ch,CURLOPT_HTTPHEADER, array (
75 "Accept: " . $opts['accept_content']
79 @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
80 @curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
84 if(x($opts,'headers')){
85 @curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['headers']);
87 if(x($opts,'nobody')){
88 @curl_setopt($ch, CURLOPT_NOBODY, $opts['nobody']);
90 if(x($opts,'timeout')){
91 @curl_setopt($ch, CURLOPT_TIMEOUT, $opts['timeout']);
93 $curl_time = intval(get_config('system','curl_timeout'));
94 @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
96 // by default we will allow self-signed certs
97 // but you can override this
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));
103 $prx = get_config('system','proxy');
105 @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
106 @curl_setopt($ch, CURLOPT_PROXY, $prx);
107 $prxusr = @get_config('system','proxyuser');
109 @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
112 @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
114 $a->set_curl_code(0);
116 // don't let curl abort the entire application
117 // if it throws any errors.
119 $s = @curl_exec($ch);
120 if (curl_errno($ch) !== CURLE_OK) {
121 logger('fetch_url error fetching '.$url.': '.curl_error($ch), LOGGER_NORMAL);
125 $curl_info = @curl_getinfo($ch);
127 $http_code = $curl_info['http_code'];
128 logger('fetch_url '.$url.': '.$http_code." ".$s, LOGGER_DATA);
131 // Pull out multiple headers, e.g. proxy and continuation headers
132 // allow for HTTP/2.x without fixing code
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);
137 $base = substr($base,strlen($chunk));
140 $a->set_curl_code($http_code);
141 $a->set_curl_content_type($curl_info['content_type']);
142 $a->set_curl_headers($header);
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"]);
148 $newurl = $curl_info["redirect_url"];
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"];
154 if (preg_match('/(Location:|URI:)(.*?)\n/i', $header, $matches)) {
155 $newurl = trim(array_pop($matches));
157 if(strpos($newurl,'/') === 0)
158 $newurl = $old_location_info["scheme"]."://".$old_location_info["host"].$newurl;
159 if (filter_var($newurl, FILTER_VALIDATE_URL)) {
162 return z_fetch_url($newurl,$binary, $redirects, $opts);
167 $a->set_curl_code($http_code);
168 $a->set_curl_content_type($curl_info['content_type']);
170 $body = substr($s,strlen($header));
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);
184 $ret['body'] = substr($s,strlen($header));
185 $ret['header'] = $header;
186 if(x($opts,'debug')) {
187 $ret['debug'] = $curl_info;
191 $a->save_timestamp($stamp1, "network");
197 // post request to $url. $params is an array of post variables.
199 if(! function_exists('post_url')) {
200 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
201 $stamp1 = microtime(true);
204 $ch = curl_init($url);
205 if(($redirects > 8) || (! $ch))
208 logger("post_url: start ".$url, LOGGER_DATA);
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());
216 if(intval($timeout)) {
217 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
220 $curl_time = intval(get_config('system','curl_timeout'));
221 curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60));
224 if(defined('LIGHTTPD')) {
225 if(!is_array($headers)) {
226 $headers = array('Expect:');
228 if(!in_array('Expect:', $headers)) {
229 array_push($headers, 'Expect:');
234 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
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');
241 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
242 curl_setopt($ch, CURLOPT_PROXY, $prx);
243 $prxusr = get_config('system','proxyuser');
245 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr);
248 $a->set_curl_code(0);
250 // don't let curl abort the entire application
251 // if it throws any errors.
253 $s = @curl_exec($ch);
256 $curl_info = curl_getinfo($ch);
257 $http_code = $curl_info['http_code'];
259 logger("post_url: result ".$http_code." - ".$url, LOGGER_DATA);
263 // Pull out multiple headers, e.g. proxy and continuation headers
264 // allow for HTTP/2.x without fixing code
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);
269 $base = substr($base,strlen($chunk));
272 if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
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)) {
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);
285 $a->set_curl_code($http_code);
286 $body = substr($s,strlen($header));
288 $a->set_curl_headers($header);
292 $a->save_timestamp($stamp1, "network");
294 logger("post_url: end ".$url, LOGGER_DATA);
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.
303 if(! function_exists('xml_status')) {
304 function xml_status($st, $message = '') {
306 $xml_message = ((strlen($message)) ? "\t<message>" . xmlify($message) . "</message>\r\n" : '');
309 logger('xml_status returning non_zero: ' . $st . " message=" . $message);
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";
318 if(! function_exists('http_status_exit')) {
319 function http_status_exit($val, $description = array()) {
323 if (!isset($description["title"]))
324 $description["title"] = $err." ".$val;
326 if($val >= 200 && $val < 300)
329 logger('http_status_exit ' . $val);
330 header($_SERVER["SERVER_PROTOCOL"] . ' ' . $val . ' ' . $err);
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"]));
343 // convert an XML document to a normalised, case-corrected array
346 if(! function_exists('convert_xml_element_to_array')) {
347 function convert_xml_element_to_array($xml_element, &$recursion_depth=0) {
349 // If we're getting too deep, bail out
350 if ($recursion_depth > 512) {
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);
361 if (is_array($xml_element)) {
362 $result_array = array();
363 if (count($xml_element) <= 0) {
364 return (trim(strval($xml_element_copy)));
367 foreach($xml_element as $key=>$value) {
370 $result_array[strtolower($key)] =
371 convert_xml_element_to_array($value, $recursion_depth);
374 if ($recursion_depth == 0) {
375 $temp_array = $result_array;
376 $result_array = array(
377 strtolower($xml_element_copy->getName()) => $temp_array,
381 return ($result_array);
384 return (trim(strval($xml_element)));
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.
397 // amended 7/9/2011 to return an hcard which could save potentially loading
398 // a lengthy content page to scrape dfrn attributes
400 if(! function_exists('webfinger_dfrn')) {
401 function webfinger_dfrn($s,&$hcard) {
402 if(! strstr($s,'@')) {
407 $links = webfinger($s);
408 logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA);
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'];
419 return $profile_link;
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.
427 if(! function_exists('webfinger')) {
428 function webfinger($s, $debug = false) {
431 $host = substr($s,strpos($s,'@') + 1);
434 $tpl = fetch_lrdd_template($host);
435 logger('webfinger: lrdd template: ' . $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);
452 if(! function_exists('lrdd')) {
453 function lrdd($uri, $debug = false) {
457 // default priority is host priority, host-meta first
461 // All we have is an email address. Resource-priority is irrelevant
462 // because our URI isn't directly resolvable.
464 if(strstr($uri,'@')) {
465 return(webfinger($uri));
468 // get the host meta file
470 $host = @parse_url($uri);
473 $url = ((x($host,'scheme')) ? $host['scheme'] : 'http') . '://';
474 $url .= $host['host'] . '/.well-known/host-meta' ;
479 logger('lrdd: constructed url: ' . $url);
481 $xml = fetch_url($url);
483 $headers = $a->get_curl_headers();
488 logger('lrdd: host_meta: ' . $xml, LOGGER_DATA);
490 if(! stristr($xml,'<xrd'))
493 $h = parse_xml_string($xml);
497 $arr = convert_xml_element_to_array($h);
499 if(isset($arr['xrd']['property'])) {
500 $property = $arr['crd']['property'];
501 if(! isset($property[0]))
502 $properties = array($property);
504 $properties = $property;
505 foreach($properties as $prop)
506 if((string) $prop['@attributes'] === 'http://lrdd.net/priority/resource')
507 $priority = 'resource';
510 // save the links in case we need them
514 if(isset($arr['xrd']['link'])) {
515 $link = $arr['xrd']['link'];
516 if(! isset($link[0]))
517 $links = array($link);
522 // do we have a template or href?
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'];
535 if((! isset($tpl)) || (! strpos($tpl,'{uri}')))
538 if($priority === 'host') {
540 $pxrd = str_replace('{uri}', urlencode($uri), $tpl);
544 logger('lrdd: (host priority) pxrd: ' . $pxrd);
545 $links = fetch_xrd_links($pxrd);
549 $lines = explode("\n",$headers);
551 foreach($lines as $line) {
552 if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) {
553 return(fetch_xrd_links($matches[1]));
561 // priority 'resource'
564 $html = fetch_url($uri);
565 $headers = $a->get_curl_headers();
566 logger('lrdd: headers=' . $headers, LOGGER_DEBUG);
568 // don't try and parse raw xml as html
569 if(! strstr($html,'<?xml')) {
570 require_once('library/HTML5/Parser.php');
573 $dom = HTML5_Parser::parse($html);
574 } catch (DOMException $e) {
575 logger('lrdd: parse error: ' . $e);
578 if(isset($dom) && $dom) {
579 $items = $dom->getElementsByTagName('link');
580 foreach($items as $item) {
581 $x = $item->getAttribute('rel');
583 $pagelink = $item->getAttribute('href');
591 return(fetch_xrd_links($pagelink));
593 // next look in HTTP headers
595 $lines = explode("\n",$headers);
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];
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'))))
606 if(stristr($html,'<rss') || stristr($html,'<feed'))
612 return(fetch_xrd_links($pagelink));
614 // If we haven't found any links, return the host xrd links (which we have already fetched)
625 // Given a host name, locate the LRDD template from that
626 // host. Returns the LRDD template or an empty string on
629 if(! function_exists('fetch_lrdd_template')) {
630 function fetch_lrdd_template($host) {
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));
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'];
648 if(! strpos($tpl,'{uri}'))
653 // Given a URL, retrieve the page as an XRD document.
654 // Return an array of links.
655 // on error/failure return empty array.
657 if(! function_exists('fetch_xrd_links')) {
658 function fetch_xrd_links($url) {
660 $xrd_timeout = intval(get_config('system','xrd_timeout'));
662 $xml = fetch_url($url,false,$redirects,(($xrd_timeout) ? $xrd_timeout : 20), "application/xrd+xml");
664 logger('fetch_xrd_links: ' . $xml, LOGGER_DATA);
666 if ((! $xml) || (! stristr($xml,'<xrd')))
669 // fix diaspora's bad xml
670 $xml = str_replace(array('href="','"/>'),array('href="','"/>'),$xml);
672 $h = parse_xml_string($xml);
676 $arr = convert_xml_element_to_array($h);
680 if(isset($arr['xrd']['link'])) {
681 $link = $arr['xrd']['link'];
682 if(! isset($link[0]))
683 $links = array($link);
687 if(isset($arr['xrd']['alias'])) {
688 $alias = $arr['xrd']['alias'];
689 if(! isset($alias[0]))
690 $aliases = array($alias);
693 if(is_array($aliases) && count($aliases)) {
694 foreach($aliases as $alias) {
695 $links[]['@attributes'] = array('rel' => 'alias' , 'href' => $alias);
700 logger('fetch_xrd_links: ' . print_r($links,true), LOGGER_DATA);
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
711 if(! function_exists('validate_url')) {
712 function validate_url(&$url) {
714 if(get_config('system','disable_url_validation'))
716 // no naked subdomains (allow localhost for tests)
717 if(strpos($url,'.') === false && strpos($url,'/localhost/') === false)
719 if(substr($url,0,4) != 'http')
720 $url = 'http://' . $url;
721 $h = @parse_url($url);
723 if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) {
729 // checks that email is an actual resolvable internet address
731 if(! function_exists('validate_email')) {
732 function validate_email($addr) {
734 if(get_config('system','disable_email_validation'))
737 if(! strpos($addr,'@'))
739 $h = substr($addr,strpos($addr,'@') + 1);
741 if(($h) && (dns_get_record($h, DNS_A + DNS_CNAME + DNS_PTR + DNS_MX) || filter_var($h, FILTER_VALIDATE_IP) )) {
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
752 if(! function_exists('allowed_url')) {
753 function allowed_url($url) {
755 $h = @parse_url($url);
761 $str_allowed = get_config('system','allowed_sites');
767 $host = strtolower($h['host']);
769 // always allow our own site
771 if($host == strtolower($_SERVER['SERVER_NAME']))
774 $fnmatch = function_exists('fnmatch');
775 $allowed = explode(',',$str_allowed);
777 if(count($allowed)) {
778 foreach($allowed as $a) {
779 $pat = strtolower(trim($a));
780 if(($fnmatch && fnmatch($pat,$host)) || ($pat == $host)) {
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.
794 if(! function_exists('allowed_email')) {
795 function allowed_email($email) {
798 $domain = strtolower(substr($email,strpos($email,'@') + 1));
802 $str_allowed = get_config('system','allowed_email');
808 $fnmatch = function_exists('fnmatch');
809 $allowed = explode(',',$str_allowed);
811 if(count($allowed)) {
812 foreach($allowed as $a) {
813 $pat = strtolower(trim($a));
814 if(($fnmatch && fnmatch($pat,$domain)) || ($pat == $domain)) {
824 if(! function_exists('avatar_img')) {
825 function avatar_img($email) {
829 $avatar['size'] = 175;
830 $avatar['email'] = $email;
832 $avatar['success'] = false;
834 call_hooks('avatar_lookup', $avatar);
836 if(! $avatar['success'])
837 $avatar['url'] = $a->get_baseurl() . '/images/person-175.jpg';
839 logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG);
840 return $avatar['url'];
844 if(! function_exists('parse_xml_string')) {
845 function parse_xml_string($s,$strict = true) {
847 if(! strstr($s,'<?xml'))
849 $s2 = substr($s,strpos($s,'<?xml'));
853 libxml_use_internal_errors(true);
855 $x = @simplexml_load_string($s2);
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();
865 function scale_external_images($srctext, $include_link = true, $scale_replace = false) {
867 // Suppress "view full size"
868 if (intval(get_config('system','no_view_full_size')))
869 $include_link = false;
873 // Picture addresses can contain special characters
874 $s = htmlspecialchars_decode($srctext);
877 $c = preg_match_all('/\[img.*?\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER);
879 require_once('include/Photo.php');
880 foreach($matches as $mtch) {
881 logger('scale_external_image: ' . $mtch[1]);
883 $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3));
884 if(stristr($mtch[1],$hostname))
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
893 $scaled = str_replace($scale_replace[0], $scale_replace[1], $mtch[1]);
896 $i = @fetch_url($scaled);
900 // guess mimetype from headers or filename
901 $type = guess_image_type($mtch[1],true);
904 $ph = new Photo($i, $type);
905 if($ph->is_valid()) {
906 $orig_width = $ph->getWidth();
907 $orig_height = $ph->getHeight();
909 if($orig_width > 640 || $orig_height > 640) {
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"
919 logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG);
926 // replace the special char encoding
927 $s = htmlspecialchars($s,ENT_NOQUOTES,'UTF-8');
932 function fix_contact_ssl_policy(&$contact,$new_policy) {
934 $ssl_changed = false;
935 if((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'],'https:')) {
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']);
945 if((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'],'http:')) {
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']);
956 q("update contact set
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'])
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'));
990 function xml2array($contents, $namespaces = true, $get_attributes=1, $priority = 'attribute') {
991 if(!$contents) return array();
993 if(!function_exists('xml_parser_create')) {
994 logger('xml2array: parser function missing');
999 libxml_use_internal_errors(true);
1000 libxml_clear_errors();
1003 $parser = @xml_parser_create_ns("UTF-8",':');
1005 $parser = @xml_parser_create();
1008 logger('xml2array: xml_parser_create: no resource');
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);
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();
1028 $xml_array = array();
1030 $opened_tags = array();
1033 $current = &$xml_array; // Reference
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
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.
1045 $attributes_data = array();
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
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'
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;
1066 $tag = strtolower($tag);
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;
1075 $current = &$current[$tag];
1077 } else { // There was another element with the same tag name
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;
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']);
1092 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
1093 $current = &$current[$tag][$last_item_index];
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;
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...
1106 // ...push the new element into that array.
1107 $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
1109 if($priority == 'tag' and $get_attributes and $attributes_data) {
1110 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1112 $repeated_tag_index[$tag.'_'.$level]++;
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
1120 $current[$tag]['0_attr'] = $current[$tag.'_attr'];
1121 unset($current[$tag.'_attr']);
1124 if($attributes_data) {
1125 $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
1128 $repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
1132 } elseif($type == 'close') { // End of tag '</tag>'
1133 $current = &$parent[$level-1];
1140 function original_url($url, $depth=1, $fetchbody = false) {
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);
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",
1156 "woo_campaign", "woo_source", "woo_medium", "woo_content", "woo_term"))) {
1158 $pair = $param."=".urlencode($value);
1159 $url = str_replace($pair, "", $url);
1161 // Second try: if the url isn't encoded completely
1162 $pair = $param."=".str_replace(" ", "+", $value);
1163 $url = str_replace($pair, "", $url);
1165 // Third try: Maybey the url isn't encoded at all
1166 $pair = $param."=".$value;
1167 $url = str_replace($pair, "", $url);
1169 $url = str_replace(array("?&", "&&"), array("?", ""), $url);
1172 if (substr($url, -1, 1) == "?")
1173 $url = substr($url, 0, -1);
1179 $url = trim($url, "'");
1181 $stamp1 = microtime(true);
1183 $siteinfo = array();
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());
1192 $header = curl_exec($ch);
1193 $curl_info = @curl_getinfo($ch);
1194 $http_code = $curl_info['http_code'];
1197 $a->save_timestamp($stamp1, "network");
1199 if ($http_code == 0)
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));
1207 return(original_url($curl_info['location'], ++$depth, $fetchbody));
1210 // Check for redirects in the meta elements of the body if there are no redirects in the header.
1212 return(original_url($url, ++$depth, true));
1214 // if the file is too large then exit
1215 if ($curl_info["download_content_length"] > 1000000)
1218 // if it isn't a HTML file then exit
1219 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
1222 $stamp1 = microtime(true);
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());
1232 $body = curl_exec($ch);
1235 $a->save_timestamp($stamp1, "network");
1237 if (trim($body) == "")
1240 // Check for redirect in meta elements
1241 $doc = new DOMDocument();
1242 @$doc->loadHTML($body);
1244 $xpath = new DomXPath($doc);
1246 $list = $xpath->query("//meta[@content]");
1247 foreach ($list as $node) {
1249 if ($node->attributes->length)
1250 foreach ($node->attributes as $attribute)
1251 $attr[$attribute->name] = $attribute->value;
1253 if (@$attr["http-equiv"] == 'refresh') {
1254 $path = $attr["content"];
1255 $pathinfo = explode(";", $path);
1257 foreach ($pathinfo AS $value)
1258 if (substr(strtolower($value), 0, 4) == "url=")
1259 return(original_url(substr($value, 4), ++$depth));
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');
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()));
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()));
1287 return $slinky->short();
1291 * @brief Encodes content to json
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.
1297 * @param array $x The input content
1299 function json_return_and_die($x) {
1300 header("content-type: application/json");
1301 echo json_encode($x);