/** * Convert XML Element To Array * * This is a utility function that takes a simpleXMLElement object and returns the contents * as a nested associative PHP array, to make it easier to work with. It's called recursively * to deal with an entire document. Adapted from * http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/ * * @param string $xml_element A single element, possibly containing children * @param int $recursion_depth The depth of the call stack * @return array All the tags in the XML element converted into an associative array */ function convert_xml_element_to_array($xml_element, &$recursion_depth = 0) { // If we're getting too deep, bail out if ($recursion_depth > 512) { return null; } if (!is_string($xml_element) && !is_array($xml_element) && get_class($xml_element) == 'SimpleXMLElement') { $xml_element_copy = $xml_element; $xml_element = get_object_vars($xml_element); } if (is_array($xml_element)) { $result_array = array(); if (count($xml_element) <= 0) { return trim(strval($xml_element_copy)); } foreach ($xml_element as $key => $value) { $recursion_depth++; $result_array[strtolower($key)] = convert_xml_element_to_array($value, $recursion_depth); $recursion_depth--; } if ($recursion_depth == 0) { $temp_array = $result_array; $result_array = array(strtolower($xml_element_copy->getName()) => $temp_array); } return $result_array; } else { return trim(strval($xml_element)); } }
function fetch_xrd_links($url) { logger('fetch_xrd_links: ' . $url); $redirects = 0; $x = z_fetch_url($url, false, $redirects, array('timeout' => 20)); if (!$x['success']) { return array(); } $xml = $x['body']; logger('fetch_xrd_links: ' . $xml, LOGGER_DATA); if (!$xml || !stristr($xml, '<xrd')) { return array(); } // fix diaspora's bad xml $xml = str_replace(array('href="', '"/>'), array('href="', '"/>'), $xml); $h = parse_xml_string($xml); if (!$h) { return array(); } $arr = convert_xml_element_to_array($h); $links = array(); if (isset($arr['xrd']['link'])) { $link = $arr['xrd']['link']; if (!isset($link[0])) { $links = array($link); } else { $links = $link; } } if (isset($arr['xrd']['alias'])) { $alias = $arr['xrd']['alias']; if (!isset($alias[0])) { $aliases = array($alias); } else { $aliases = $alias; } if (is_array($aliases) && count($aliases)) { foreach ($aliases as $alias) { $links[]['@attributes'] = array('rel' => 'alias', 'href' => $alias); } } } logger('fetch_xrd_links: ' . print_r($links, true), LOGGER_DATA); return $links; }
function webfinger($s) { if (!strstr($s, '@')) { return $s; } $host = substr($s, strpos($s, '@') + 1); $url = 'http://' . $host . '/.well-known/host-meta'; $xml = fetch_url($url); if (!$xml) { return ''; } $h = simplexml_load_string($xml); $arr = convert_xml_element_to_array($h); if (!isset($arr['xrd']['link'])) { return ''; } $link = $arr['xrd']['link']; if (!isset($link[0])) { $links = array($link); } else { $links = $link; } foreach ($links as $link) { if ($link['@attributes']['rel'] && $link['@attributes']['rel'] == 'lrdd') { $tpl = $link['@attributes']['template']; } } if (empty($tpl) || !strpos($tpl, '{uri}')) { return ''; } $pxrd = str_replace('{uri}', urlencode('acct://' . $s), $tpl); $xml = fetch_url($pxrd); if (!$xml) { return ''; } $h = simplexml_load_string($xml); $arr = convert_xml_element_to_array($h); if (!isset($arr['xrd']['link'])) { return ''; } $link = $arr['xrd']['link']; if (!isset($link[0])) { $links = array($link); } else { $links = $link; } foreach ($links as $link) { if ($link['@attributes']['rel'] == NAMESPACE_DFRN) { return $link['@attributes']['href']; } } return ''; }
function fetch_xrd_links($url) { $xrd_timeout = intval(get_config('system', 'xrd_timeout')); $redirects = 0; $xml = fetch_url($url, false, $redirects, $xrd_timeout ? $xrd_timeout : 20); logger('fetch_xrd_links: ' . $xml, LOGGER_DATA); if (!$xml || !stristr($xml, '<xrd')) { return array(); } $h = parse_xml_string($xml); if (!$h) { return array(); } $arr = convert_xml_element_to_array($h); $links = array(); if (isset($arr['xrd']['link'])) { $link = $arr['xrd']['link']; if (!isset($link[0])) { $links = array($link); } else { $links = $link; } } if (isset($arr['xrd']['alias'])) { $alias = $arr['xrd']['alias']; if (!isset($alias[0])) { $aliases = array($alias); } else { $aliases = $alias; } if (is_array($aliases) && count($aliases)) { foreach ($aliases as $alias) { $links[]['@attributes'] = array('rel' => 'alias', 'href' => $alias); } } } logger('fetch_xrd_links: ' . print_r($links, true), LOGGER_DATA); return $links; }