ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
Auth_Yadis_ParseHTML Class Reference
+ Collaboration diagram for Auth_Yadis_ParseHTML:

Public Member Functions

 Auth_Yadis_ParseHTML ()
 replaceEntities ($str)
 Replace HTML entities (amp, lt, gt, and quot) as well as numeric entities (e.g.
 removeQuotes ($str)
 Strip single and double quotes off of a string, if they are present.
 tagPattern ($tag_names, $close, $self_close)
 Create a regular expression that will match an opening or closing tag from a set of names.
 getMetaTags ($html_string)
 Given an HTML document string, this finds all the META tags in the document, provided they are found in the <HTML><HEAD>...</HEAD> section of the document.
 getHTTPEquiv ($html_string)
 Looks for a META tag with an "http-equiv" attribute whose value is one of ("x-xrds-location", "x-yadis-location"), ignoring case.

Data Fields

 $_re_flags = "si"
 private
 $_removed_re = "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>"
 private
 $_tag_expr = "<%s%s(?:\s.*?)?%s>"
 private
 $_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]'
 private

Detailed Description

Definition at line 23 of file ParseHTML.php.

Member Function Documentation

Auth_Yadis_ParseHTML::Auth_Yadis_ParseHTML ( )

Definition at line 46 of file ParseHTML.php.

{
$this->_attr_find = sprintf("/%s/%s",
$this->_attr_find,
$this->_re_flags);
$this->_removed_re = sprintf("/%s/%s",
$this->_removed_re,
$this->_re_flags);
$this->_entity_replacements = array(
'amp' => '&',
'lt' => '<',
'gt' => '>',
'quot' => '"'
);
$this->_ent_replace =
sprintf("&(%s);", implode("|",
$this->_entity_replacements));
}
Auth_Yadis_ParseHTML::getHTTPEquiv (   $html_string)

Looks for a META tag with an "http-equiv" attribute whose value is one of ("x-xrds-location", "x-yadis-location"), ignoring case.

If such a META tag is found, its "content" attribute value is returned.

Parameters
string$html_stringAn HTML document in string format
Returns
mixed $content The "content" attribute value of the META tag, if found, or null if no such tag was found.

Definition at line 240 of file ParseHTML.php.

References getMetaTags().

{
$meta_tags = $this->getMetaTags($html_string);
if ($meta_tags) {
foreach ($meta_tags as $tag) {
if (array_key_exists('http-equiv', $tag) &&
(in_array(strtolower($tag['http-equiv']),
array('x-xrds-location', 'x-yadis-location'))) &&
array_key_exists('content', $tag)) {
return $tag['content'];
}
}
}
return null;
}

+ Here is the call graph for this function:

Auth_Yadis_ParseHTML::getMetaTags (   $html_string)

Given an HTML document string, this finds all the META tags in the document, provided they are found in the <HTML><HEAD>...</HEAD> section of the document.

The <HTML> tag may be missing.

private

Parameters
string$html_stringAn HTMl document string
Returns
array $tag_list Array of tags; each tag is an array of attribute -> value.

Definition at line 157 of file ParseHTML.php.

References removeQuotes(), replaceEntities(), and tagPattern().

Referenced by getHTTPEquiv().

{
$html_string = preg_replace($this->_removed_re,
"",
$html_string);
$key_tags = array($this->tagPattern('html', false, false),
$this->tagPattern('head', false, false),
$this->tagPattern('head', true, false),
$this->tagPattern('html', true, false),
$this->tagPattern(array(
'body', 'frameset', 'frame', 'p', 'div',
'table','span','a'), 'maybe', 'maybe'));
$key_tags_pos = array();
foreach ($key_tags as $pat) {
$matches = array();
preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
if($matches) {
$key_tags_pos[] = $matches[0][1];
} else {
$key_tags_pos[] = null;
}
}
// no opening head tag
if (is_null($key_tags_pos[1])) {
return array();
}
// the effective </head> is the min of the following
if (is_null($key_tags_pos[2])) {
$key_tags_pos[2] = strlen($html_string);
}
foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
if (!is_null($pos) && $pos < $key_tags_pos[2]) {
$key_tags_pos[2] = $pos;
}
}
// closing head tag comes before opening head tag
if ($key_tags_pos[1] > $key_tags_pos[2]) {
return array();
}
// if there is an opening html tag, make sure the opening head tag
// comes after it
if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
return array();
}
$html_string = substr($html_string, $key_tags_pos[1],
($key_tags_pos[2]-$key_tags_pos[1]));
$link_data = array();
$link_matches = array();
if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
$html_string, $link_matches)) {
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;
}
return $link_data;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Auth_Yadis_ParseHTML::removeQuotes (   $str)

Strip single and double quotes off of a string, if they are present.

private

Parameters
string$strThe original string
Returns
string $new_str The new string with leading and trailing quotes removed

Definition at line 100 of file ParseHTML.php.

Referenced by getMetaTags().

{
$matches = array();
$double = '/^"(.*)"$/';
$single = "/^\'(.*)\'$/";
if (preg_match($double, $str, $matches)) {
return $matches[1];
} else if (preg_match($single, $str, $matches)) {
return $matches[1];
} else {
return $str;
}
}

+ Here is the caller graph for this function:

Auth_Yadis_ParseHTML::replaceEntities (   $str)

Replace HTML entities (amp, lt, gt, and quot) as well as numeric entities (e.g.

#x9f;) with their actual values and return the new string.

private

Parameters
string$strThe string in which to look for entities
Returns
string $new_str The new string entities decoded

Definition at line 77 of file ParseHTML.php.

Referenced by getMetaTags().

{
foreach ($this->_entity_replacements as $old => $new) {
$str = preg_replace(sprintf("/&%s;/", $old), $new, $str);
}
// Replace numeric entities because html_entity_decode doesn't
// do it for us.
$str = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $str);
$str = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $str);
return $str;
}

+ Here is the caller graph for this function:

Auth_Yadis_ParseHTML::tagPattern (   $tag_names,
  $close,
  $self_close 
)

Create a regular expression that will match an opening or closing tag from a set of names.

private

Parameters
mixed$tag_namesTag names to match
mixed$closefalse/0 = no, true/1 = yes, other = maybe
mixed$self_closefalse/0 = no, true/1 = yes, other = maybe
Returns
string $regex A regular expression string to be used in, say, preg_match.

Definition at line 126 of file ParseHTML.php.

Referenced by getMetaTags().

{
if (is_array($tag_names)) {
$tag_names = '(?:'.implode('|',$tag_names).')';
}
if ($close) {
$close = '\/' . (($close == 1)? '' : '?');
} else {
$close = '';
}
if ($self_close) {
$self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
} else {
$self_close = '';
}
$expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
return sprintf("/%s/%s", $expr, $this->_re_flags);
}

+ Here is the caller graph for this function:

Field Documentation

Auth_Yadis_ParseHTML::$_attr_find = '\b([-\w]+)=(".*?"|\'.*?\'|.+?)[\/\s>]'

private

Definition at line 44 of file ParseHTML.php.

Auth_Yadis_ParseHTML::$_re_flags = "si"

private

Definition at line 28 of file ParseHTML.php.

Auth_Yadis_ParseHTML::$_removed_re = "<!--.*?-->|<!\[CDATA\[.*?\]\]>|<script\b(?!:)[^>]*>.*?<\/script>"

private

Definition at line 33 of file ParseHTML.php.

Auth_Yadis_ParseHTML::$_tag_expr = "<%s%s(?:\s.*?)?%s>"

private

Definition at line 39 of file ParseHTML.php.


The documentation for this class was generated from the following file: