Auth_OpenID_Parse::parseLinkAttrs PHP Method

parseLinkAttrs() public method

Find all link tags in a string representing a HTML document and return a list of their attributes.
public parseLinkAttrs ( string $html ) : array
$html string The text to parse
return array $list An array of arrays of attributes, one for each link tag
    function parseLinkAttrs($html)
    {
        $stripped = preg_replace($this->_removed_re, "", $html);
        $html_begin = $this->htmlBegin($stripped);
        $html_end = $this->htmlEnd($stripped);
        if ($html_begin === false) {
            return array();
        }
        if ($html_end === false) {
            $html_end = strlen($stripped);
        }
        $stripped = substr($stripped, $html_begin, $html_end - $html_begin);
        // Workaround to prevent PREG_BACKTRACK_LIMIT_ERROR:
        $old_btlimit = ini_set('pcre.backtrack_limit', -1);
        // Try to find the <HEAD> tag.
        $head_re = $this->headFind();
        $head_match = array();
        if (!$this->match($head_re, $stripped, $head_match)) {
            ini_set('pcre.backtrack_limit', $old_btlimit);
            return array();
        }
        $link_data = array();
        $link_matches = array();
        if (!preg_match_all($this->_link_find, $head_match[0], $link_matches)) {
            ini_set('pcre.backtrack_limit', $old_btlimit);
            return array();
        }
        foreach ($link_matches[0] as $link) {
            $attr_matches = array();
            preg_match_all($this->_attr_find, $link, $attr_matches);
            $link_attrs = array();
            foreach ($attr_matches[0] as $index => $full_match) {
                $name = $attr_matches[1][$index];
                $value = $this->replaceEntities($this->removeQuotes($attr_matches[2][$index]));
                $link_attrs[strtolower($name)] = $value;
            }
            $link_data[] = $link_attrs;
        }
        ini_set('pcre.backtrack_limit', $old_btlimit);
        return $link_data;
    }

Usage Example

コード例 #1
0
ファイル: server.php プロジェクト: alx/pressid
/**
 * Discover and cache OpenID services for a user's delegate OpenID.
 *
 * @param int $userid user ID
 * @url string URL to discover.  If not provided, user's current delegate will be used
 * @return bool true if successful
 */
function openid_server_update_delegation_info($userid, $url = null)
{
    if (empty($url)) {
        $url = get_usermeta($userid, 'openid_delegate');
    }
    if (empty($url)) {
        return false;
    }
    $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
    $discoveryResult = Auth_Yadis_Yadis::discover($url, $fetcher);
    $endpoints = Auth_OpenID_ServiceEndpoint::fromDiscoveryResult($discoveryResult);
    $services = array();
    if (!empty($endpoints)) {
        foreach ($endpoints as $endpoint) {
            $service = array('Type' => array(), 'URI' => $endpoint->server_url);
            foreach ($endpoint->type_uris as $type) {
                $service['Type'][] = array('content' => $type);
                if ($type == Auth_OpenID_TYPE_2_0_IDP) {
                    $service['LocalID'] = Auth_OpenID_IDENTIFIER_SELECT;
                } else {
                    if ($type == Auth_OpenID_TYPE_2_0) {
                        $service['LocalID'] = $endpoint->local_id;
                    } else {
                        if (in_array($type, array(Auth_OpenID_TYPE_1_0, Auth_OpenID_TYPE_1_1, Auth_OpenID_TYPE_1_2))) {
                            $service['openid:Delegate'] = $endpoint->local_id;
                        }
                    }
                }
            }
            $services[] = $service;
        }
    }
    if (empty($services)) {
        // resort to checking for HTML links
        $response = $fetcher->get($url);
        $html_content = $response->body;
        $p = new Auth_OpenID_Parse();
        $link_attrs = $p->parseLinkAttrs($html_content);
        // check HTML for OpenID2
        $server_url = $p->findFirstHref($link_attrs, 'openid2.provider');
        if ($server_url !== null) {
            $openid_url = $p->findFirstHref($link_attrs, 'openid2.local_id');
            if ($openid_url == null) {
                $openid_url = $url;
            }
            $services[] = array('Type' => array(array('content' => Auth_OpenID_Type_1_1)), 'URI' => $server_url, 'LocalID' => $openid_url);
        }
        // check HTML for OpenID1
        $server_url = $p->findFirstHref($link_attrs, 'openid.server');
        if ($server_url !== null) {
            $openid_url = $p->findFirstHref($link_attrs, 'openid.delegate');
            if ($openid_url == null) {
                $openid_url = $url;
            }
            $services[] = array('Type' => array(array('content' => Auth_OpenID_Type_2_0)), 'URI' => $server_url, 'openid:Delegate' => $openid_url);
        }
    }
    if (empty($services)) {
        return false;
    }
    update_usermeta($userid, 'openid_delegate', $url);
    update_usermeta($userid, 'openid_delegate_services', $services);
    return true;
}
All Usage Examples Of Auth_OpenID_Parse::parseLinkAttrs