X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fnetwork.php;h=4bec4a172778db2c1c452b937787fe25a9f7aaac;hb=912e008ded3d439f7779310eb422e4b871a9b63d;hp=691a8c9f9ed405d5bbc64ffd222d3b21041aba7f;hpb=5ec4a4e6deb5d28cbfab54093f8b4180912738d4;p=friendica.git diff --git a/include/network.php b/include/network.php index 691a8c9f9e..4bec4a1727 100644 --- a/include/network.php +++ b/include/network.php @@ -5,41 +5,49 @@ // results. if(! function_exists('fetch_url')) { -function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) { +function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null) { $a = get_app(); - $ch = curl_init($url); + $ch = @curl_init($url); if(($redirects > 8) || (! $ch)) return false; - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); - curl_setopt($ch, CURLOPT_USERAGENT, "Friendika"); + @curl_setopt($ch, CURLOPT_HEADER, true); + + if (!is_null($accept_content)){ + curl_setopt($ch,CURLOPT_HTTPHEADER, array ( + "Accept: " . $accept_content + )); + } + + @curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); + @curl_setopt($ch, CURLOPT_USERAGENT, "Friendica"); + if(intval($timeout)) { - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); } else { $curl_time = intval(get_config('system','curl_timeout')); - curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60)); + @curl_setopt($ch, CURLOPT_TIMEOUT, (($curl_time !== false) ? $curl_time : 60)); } // by default we will allow self-signed certs // but you can override this $check_cert = get_config('system','verifyssl'); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false)); + @curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, (($check_cert) ? true : false)); $prx = get_config('system','proxy'); if(strlen($prx)) { - curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); - curl_setopt($ch, CURLOPT_PROXY, $prx); - $prxusr = get_config('system','proxyuser'); + @curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); + @curl_setopt($ch, CURLOPT_PROXY, $prx); + $prxusr = @get_config('system','proxyuser'); if(strlen($prxusr)) - curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr); + @curl_setopt($ch, CURLOPT_PROXYUSERPWD, $prxusr); } if($binary) - curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); + @curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $a->set_curl_code(0); @@ -49,9 +57,10 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) { $s = @curl_exec($ch); $base = $s; - $curl_info = curl_getinfo($ch); + $curl_info = @curl_getinfo($ch); $http_code = $curl_info['http_code']; +// logger('fetch_url:' . $http_code . ' data: ' . $s); $header = ''; // Pull out multiple headers, e.g. proxy and continuation headers @@ -66,11 +75,13 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) { if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); - $url = trim(array_pop($matches)); - $url_parsed = @parse_url($url); + $newurl = trim(array_pop($matches)); + if(strpos($newurl,'/') === 0) + $newurl = $url . $newurl; + $url_parsed = @parse_url($newurl); if (isset($url_parsed)) { $redirects++; - return fetch_url($url,$binary,$redirects,$timeout); + return fetch_url($newurl,$binary,$redirects,$timeout); } } @@ -80,7 +91,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0) { $a->set_curl_headers($header); - curl_close($ch); + @curl_close($ch); return($body); }} @@ -97,7 +108,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$params); - curl_setopt($ch, CURLOPT_USERAGENT, "Friendika"); + curl_setopt($ch, CURLOPT_USERAGENT, "Friendica"); if(intval($timeout)) { curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); @@ -155,11 +166,13 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) if($http_code == 301 || $http_code == 302 || $http_code == 303) { $matches = array(); preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches); - $url = trim(array_pop($matches)); - $url_parsed = @parse_url($url); + $newurl = trim(array_pop($matches)); + if(strpos($newurl,'/') === 0) + $newurl = $url . $newurl; + $url_parsed = @parse_url($newurl); if (isset($url_parsed)) { $redirects++; - return post_url($url,$params,$headers,$redirects,$timeout); + return fetch_url($newurl,false,$redirects,$timeout); } } $a->set_curl_code($http_code); @@ -193,6 +206,7 @@ function xml_status($st, $message = '') { if(! function_exists('http_status_exit')) { function http_status_exit($val) { + $err = ''; if($val >= 400) $err = 'Error'; if($val >= 200 && $val < 300) @@ -259,22 +273,29 @@ function convert_xml_element_to_array($xml_element, &$recursion_depth=0) { // or if the resultant personal XRD doesn't contain a supported // subscription/friend-request attribute. +// amended 7/9/2011 to return an hcard which could save potentially loading +// a lengthy content page to scrape dfrn attributes + if(! function_exists('webfinger_dfrn')) { -function webfinger_dfrn($s) { +function webfinger_dfrn($s,&$hcard) { if(! strstr($s,'@')) { return $s; } + $profile_link = ''; + $links = webfinger($s); logger('webfinger_dfrn: ' . $s . ':' . print_r($links,true), LOGGER_DATA); if(count($links)) { - foreach($links as $link) + foreach($links as $link) { if($link['@attributes']['rel'] === NAMESPACE_DFRN) - return $link['@attributes']['href']; - foreach($links as $link) + $profile_link = $link['@attributes']['href']; if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) - return 'stat:' . $link['@attributes']['template']; + $profile_link = 'stat:' . $link['@attributes']['template']; + if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') + $hcard = $link['@attributes']['href']; + } } - return ''; + return $profile_link; }} // Given an email style address, perform webfinger lookup and @@ -283,7 +304,7 @@ function webfinger_dfrn($s) { if(! function_exists('webfinger')) { -function webfinger($s) { +function webfinger($s, $debug = false) { $host = ''; if(strstr($s,'@')) { $host = substr($s,strpos($s,'@') + 1); @@ -308,7 +329,7 @@ function webfinger($s) { }} if(! function_exists('lrdd')) { -function lrdd($uri) { +function lrdd($uri, $debug = false) { $a = get_app(); @@ -344,6 +365,9 @@ function lrdd($uri) { logger('lrdd: host_meta: ' . $xml, LOGGER_DATA); + if(! stristr($xml,'getElementsByTagName('link'); foreach($items as $item) { $x = $item->getAttribute('rel'); @@ -515,6 +544,9 @@ function fetch_xrd_links($url) { if ((! $xml) || (! stristr($xml,''),array('href="','"/>'),$xml); + $h = parse_xml_string($xml); if(! $h) return array(); @@ -556,10 +588,14 @@ function fetch_xrd_links($url) { if(! function_exists('validate_url')) { function validate_url(&$url) { + + // no naked subdomains (allow localhost for tests) + if(strpos($url,'.') === false && strpos($url,'/localhost/') === false) + return false; if(substr($url,0,4) != 'http') $url = 'http://' . $url; $h = @parse_url($url); - + if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR))) { return true; } @@ -658,18 +694,23 @@ function allowed_email($email) { }} -if(! function_exists('gravatar_img')) { -function gravatar_img($email) { - $size = 175; - $opt = 'identicon'; // psuedo-random geometric pattern if not found - $rating = 'pg'; - $hash = md5(trim(strtolower($email))); - - $url = 'http://www.gravatar.com/avatar/' . $hash . '.jpg' - . '?s=' . $size . '&d=' . $opt . '&r=' . $rating; +if(! function_exists('avatar_img')) { +function avatar_img($email) { + + $a = get_app(); + + $avatar['size'] = 175; + $avatar['email'] = $email; + $avatar['url'] = ''; + $avatar['success'] = false; - logger('gravatar: ' . $email . ' ' . $url); - return $url; + call_hooks('avatar_lookup', $avatar); + + if(! $avatar['success']) + $avatar['url'] = $a->get_baseurl() . '/images/person-175.jpg'; + + logger('Avatar: ' . $avatar['email'] . ' ' . $avatar['url'], LOGGER_DEBUG); + return $avatar['url']; }} @@ -694,24 +735,144 @@ function parse_xml_string($s,$strict = true) { return $x; }} -function add_fcontact($arr) { - - $r = q("insert into fcontact ( `url`,`name`,`photo`,`request`,`nick`,`addr`, - `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated` ) - values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", - dbesc($arr['url']), - dbesc($arr['name']), - dbesc($arr['photo']), - dbesc($arr['request']), - dbesc($arr['nick']), - dbesc($arr['addr']), - dbesc($arr['notify']), - dbesc($arr['poll']), - dbesc($arr['confirm']), - dbesc($arr['network']), - dbesc($arr['alias']), - dbesc($arr['pubkey']), - dbesc(datetime_convert()) - ); +function add_fcontact($arr,$update = false) { + + if($update) { + $r = q("UPDATE `fcontact` SET + `name` = '%s', + `photo` = '%s', + `request` = '%s', + `nick` = '%s', + `addr` = '%s', + `batch` = '%s', + `notify` = '%s', + `poll` = '%s', + `confirm` = '%s', + `alias` = '%s', + `pubkey` = '%s', + `updated` = '%s' + WHERE `url` = '%s' AND `network` = '%s' LIMIT 1", + dbesc($arr['name']), + dbesc($arr['photo']), + dbesc($arr['request']), + dbesc($arr['nick']), + dbesc($arr['addr']), + dbesc($arr['batch']), + dbesc($arr['notify']), + dbesc($arr['poll']), + dbesc($arr['confirm']), + dbesc($arr['alias']), + dbesc($arr['pubkey']), + dbesc(datetime_convert()), + dbesc($arr['url']), + dbesc($arr['network']) + ); + } + else { + $r = q("insert into fcontact ( `url`,`name`,`photo`,`request`,`nick`,`addr`, + `batch`, `notify`,`poll`,`confirm`,`network`,`alias`,`pubkey`,`updated` ) + values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", + dbesc($arr['url']), + dbesc($arr['name']), + dbesc($arr['photo']), + dbesc($arr['request']), + dbesc($arr['nick']), + dbesc($arr['addr']), + dbesc($arr['batch']), + dbesc($arr['notify']), + dbesc($arr['poll']), + dbesc($arr['confirm']), + dbesc($arr['network']), + dbesc($arr['alias']), + dbesc($arr['pubkey']), + dbesc(datetime_convert()) + ); + } + return $r; } + + +function scale_external_images($s,$include_link = true) { + + $a = get_app(); + + $matches = null; + $c = preg_match_all('/\[img\](.*?)\[\/img\]/ism',$s,$matches,PREG_SET_ORDER); + if($c) { + require_once('include/Photo.php'); + foreach($matches as $mtch) { + logger('scale_external_image: ' . $mtch[1]); + $hostname = str_replace('www.','',substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3)); + if(stristr($mtch[1],$hostname)) + continue; + $i = fetch_url($mtch[1]); + if($i) { + $ph = new Photo($i); + if($ph->is_valid()) { + $orig_width = $ph->getWidth(); + $orig_height = $ph->getHeight(); + + if($orig_width > 640 || $orig_height > 640) { + + $ph->scaleImage(640); + $new_width = $ph->getWidth(); + $new_height = $ph->getHeight(); + logger('scale_external_images: ' . $orig_width . '->' . $new_width . 'w ' . $orig_height . '->' . $new_height . 'h' . ' match: ' . $mtch[0], LOGGER_DEBUG); + $s = str_replace($mtch[0],'[img=' . $new_width . 'x' . $new_height. ']' . $mtch[1] . '[/img]' + . "\n" . (($include_link) + ? '[url=' . $mtch[1] . ']' . t('view full size') . '[/url]' . "\n" + : ''),$s); + logger('scale_external_images: new string: ' . $s, LOGGER_DEBUG); + } + } + } + } + } + return $s; +} + + +function fix_contact_ssl_policy(&$contact,$new_policy) { + + $ssl_changed = false; + if((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'],'https:')) { + $ssl_changed = true; + $contact['url'] = str_replace('https:','http:',$contact['url']); + $contact['request'] = str_replace('https:','http:',$contact['request']); + $contact['notify'] = str_replace('https:','http:',$contact['notify']); + $contact['poll'] = str_replace('https:','http:',$contact['poll']); + $contact['confirm'] = str_replace('https:','http:',$contact['confirm']); + $contact['poco'] = str_replace('https:','http:',$contact['poco']); + } + + if((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'],'http:')) { + $ssl_changed = true; + $contact['url'] = str_replace('http:','https:',$contact['url']); + $contact['request'] = str_replace('http:','https:',$contact['request']); + $contact['notify'] = str_replace('http:','https:',$contact['notify']); + $contact['poll'] = str_replace('http:','https:',$contact['poll']); + $contact['confirm'] = str_replace('http:','https:',$contact['confirm']); + $contact['poco'] = str_replace('http:','https:',$contact['poco']); + } + + if($ssl_changed) { + q("update contact set + url = '%s', + request = '%s', + notify = '%s', + poll = '%s', + confirm = '%s', + poco = '%s' + where id = %d limit 1", + dbesc($contact['url']), + dbesc($contact['request']), + dbesc($contact['notify']), + dbesc($contact['poll']), + dbesc($contact['confirm']), + dbesc($contact['poco']), + intval($contact['id']) + ); + } +} +