ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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. More...
 
 removeQuotes ($str)
 Strip single and double quotes off of a string, if they are present. More...
 
 tagPattern ($tag_names, $close, $self_close)
 Create a regular expression that will match an opening or closing tag from a set of names. More...
 
 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. More...
 
 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. More...
 

Data Fields

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

Detailed Description

Definition at line 23 of file ParseHTML.php.

Member Function Documentation

◆ Auth_Yadis_ParseHTML()

Auth_Yadis_ParseHTML::Auth_Yadis_ParseHTML ( )

Definition at line 46 of file ParseHTML.php.

47  {
48  $this->_attr_find = sprintf("/%s/%s",
49  $this->_attr_find,
50  $this->_re_flags);
51 
52  $this->_removed_re = sprintf("/%s/%s",
53  $this->_removed_re,
54  $this->_re_flags);
55 
56  $this->_entity_replacements = array(
57  'amp' => '&',
58  'lt' => '<',
59  'gt' => '>',
60  'quot' => '"'
61  );
62 
63  $this->_ent_replace =
64  sprintf("&(%s);", implode("|",
65  $this->_entity_replacements));
66  }

◆ getHTTPEquiv()

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 252 of file ParseHTML.php.

References getMetaTags().

253  {
254  $meta_tags = $this->getMetaTags($html_string);
255 
256  if ($meta_tags) {
257  foreach ($meta_tags as $tag) {
258  if (array_key_exists('http-equiv', $tag) &&
259  (in_array(strtolower($tag['http-equiv']),
260  array('x-xrds-location', 'x-yadis-location'))) &&
261  array_key_exists('content', $tag)) {
262  return $tag['content'];
263  }
264  }
265  }
266 
267  return null;
268  }
getMetaTags($html_string)
Given an HTML document string, this finds all the META tags in the document, provided they are found ...
Definition: ParseHTML.php:169
+ Here is the call graph for this function:

◆ getMetaTags()

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 169 of file ParseHTML.php.

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

Referenced by getHTTPEquiv().

170  {
171  $html_string = preg_replace($this->_removed_re,
172  "",
173  $html_string);
174 
175  $key_tags = array($this->tagPattern('html', false, false),
176  $this->tagPattern('head', false, false),
177  $this->tagPattern('head', true, false),
178  $this->tagPattern('html', true, false),
179  $this->tagPattern(array(
180  'body', 'frameset', 'frame', 'p', 'div',
181  'table','span','a'), 'maybe', 'maybe'));
182  $key_tags_pos = array();
183  foreach ($key_tags as $pat) {
184  $matches = array();
185  preg_match($pat, $html_string, $matches, PREG_OFFSET_CAPTURE);
186  if($matches) {
187  $key_tags_pos[] = $matches[0][1];
188  } else {
189  $key_tags_pos[] = null;
190  }
191  }
192  // no opening head tag
193  if (is_null($key_tags_pos[1])) {
194  return array();
195  }
196  // the effective </head> is the min of the following
197  if (is_null($key_tags_pos[2])) {
198  $key_tags_pos[2] = strlen($html_string);
199  }
200  foreach (array($key_tags_pos[3], $key_tags_pos[4]) as $pos) {
201  if (!is_null($pos) && $pos < $key_tags_pos[2]) {
202  $key_tags_pos[2] = $pos;
203  }
204  }
205  // closing head tag comes before opening head tag
206  if ($key_tags_pos[1] > $key_tags_pos[2]) {
207  return array();
208  }
209  // if there is an opening html tag, make sure the opening head tag
210  // comes after it
211  if (!is_null($key_tags_pos[0]) && $key_tags_pos[1] < $key_tags_pos[0]) {
212  return array();
213  }
214  $html_string = substr($html_string, $key_tags_pos[1],
215  ($key_tags_pos[2]-$key_tags_pos[1]));
216 
217  $link_data = array();
218  $link_matches = array();
219 
220  if (!preg_match_all($this->tagPattern('meta', false, 'maybe'),
221  $html_string, $link_matches)) {
222  return array();
223  }
224 
225  foreach ($link_matches[0] as $link) {
226  $attr_matches = array();
227  preg_match_all($this->_attr_find, $link, $attr_matches);
228  $link_attrs = array();
229  foreach ($attr_matches[0] as $index => $full_match) {
230  $name = $attr_matches[1][$index];
231  $value = $this->replaceEntities(
232  $this->removeQuotes($attr_matches[2][$index]));
233 
234  $link_attrs[strtolower($name)] = $value;
235  }
236  $link_data[] = $link_attrs;
237  }
238 
239  return $link_data;
240  }
removeQuotes($str)
Strip single and double quotes off of a string, if they are present.
Definition: ParseHTML.php:112
tagPattern($tag_names, $close, $self_close)
Create a regular expression that will match an opening or closing tag from a set of names...
Definition: ParseHTML.php:138
replaceEntities($str)
Replace HTML entities (amp, lt, gt, and quot) as well as numeric entities (e.g.
Definition: ParseHTML.php:77
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removeQuotes()

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 112 of file ParseHTML.php.

Referenced by getMetaTags().

113  {
114  $matches = array();
115  $double = '/^"(.*)"$/';
116  $single = "/^\'(.*)\'$/";
117 
118  if (preg_match($double, $str, $matches)) {
119  return $matches[1];
120  } else if (preg_match($single, $str, $matches)) {
121  return $matches[1];
122  } else {
123  return $str;
124  }
125  }
+ Here is the caller graph for this function:

◆ replaceEntities()

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().

78  {
79  foreach ($this->_entity_replacements as $old => $new) {
80  $str = preg_replace(sprintf("/&%s;/", $old), $new, $str);
81  }
82 
83  // Replace numeric entities because html_entity_decode doesn't
84  // do it for us.
85  $str = preg_replace_callback(
86  '~&#x([0-9a-f]+);~i',
87  function($hit) {
88  return chr(hexdec($hit[1]));
89  },
90  $str
91  );
92  $str = preg_replace_callback(
93  '~&#([0-9]+);~',
94  function($hit) {
95  return chr($hit[1]);
96  },
97  $str
98  );
99 
100  return $str;
101  }
+ Here is the caller graph for this function:

◆ tagPattern()

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 138 of file ParseHTML.php.

Referenced by getMetaTags().

139  {
140  if (is_array($tag_names)) {
141  $tag_names = '(?:'.implode('|',$tag_names).')';
142  }
143  if ($close) {
144  $close = '\/' . (($close == 1)? '' : '?');
145  } else {
146  $close = '';
147  }
148  if ($self_close) {
149  $self_close = '(?:\/\s*)' . (($self_close == 1)? '' : '?');
150  } else {
151  $self_close = '';
152  }
153  $expr = sprintf($this->_tag_expr, $close, $tag_names, $self_close);
154 
155  return sprintf("/%s/%s", $expr, $this->_re_flags);
156  }
+ Here is the caller graph for this function:

Field Documentation

◆ $_attr_find

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

private

Definition at line 44 of file ParseHTML.php.

◆ $_re_flags

Auth_Yadis_ParseHTML::$_re_flags = "si"

private

Definition at line 28 of file ParseHTML.php.

◆ $_removed_re

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

private

Definition at line 33 of file ParseHTML.php.

◆ $_tag_expr

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: