| version 1.1, 2002/10/06 20:55:24 |
version 1.2, 2002/10/08 17:58:06 |
|
|
| <?php |
<?php |
| |
|
| |
/* HEAD elements: |
| |
|
| |
<TITLE> - required |
| |
</TITLE> - required |
| |
<BASE> - required |
| |
</BASE> - forbidden |
| |
<NOSCRIPT> - required |
| |
</NOSCRIPT> - required |
| |
|
| |
/* Repeatable HEAD elements: |
| |
|
| |
<SCRIPT> - required |
| |
</SCRIPT> - required |
| |
<STYLE> - required |
| |
</STYLE> - required |
| |
<META> - required |
| |
</META> - forbidden |
| |
<LINK> - required |
| |
</LINK> - forbidden |
| |
<OBJECT> - required |
| |
</OBJECT> - required |
| |
|
| |
*/ |
| |
|
| |
|
| |
|
| class HTML_Content |
class HTML_Content |
| Line 8 class HTML_Content |
|
| Line 32 class HTML_Content |
|
| var $_data = array( |
var $_data = array( |
| 'doctype' => null, |
'doctype' => null, |
| 'head' => null, |
'head' => null, |
| |
'head_attr' => null, |
| |
'head_tags' => null, |
| 'body' => null, |
'body' => null, |
| 'body_attr' => null |
'body_attr' => null, |
| |
'body_tags' => null |
| ); |
); |
| |
|
| function HTML_Content() |
function HTML_Content() |
| Line 29 class HTML_Content |
|
| Line 56 class HTML_Content |
|
| { |
{ |
| $fp = @fopen($file, 'rb'); |
$fp = @fopen($file, 'rb'); |
| if (is_resource($fp)) { |
if (is_resource($fp)) { |
| $this->fp = $fp; |
$this->_fp = $fp; |
| return $fp; |
return $fp; |
| } |
} |
| |
|
| Line 47 class HTML_Content |
|
| Line 74 class HTML_Content |
|
| function setInput($fp) |
function setInput($fp) |
| { |
{ |
| if (is_resource($fp)) { |
if (is_resource($fp)) { |
| $this->fp = $fp; |
$this->_fp = $fp; |
| return true; |
return true; |
| } |
} |
| |
|
| Line 64 class HTML_Content |
|
| Line 91 class HTML_Content |
|
| */ |
*/ |
| function parse() |
function parse() |
| { |
{ |
| if (!is_resource($this->fp)) { |
if (!is_resource($this->_fp)) { |
| return $this->raiseError('no input'); |
//return $this->raiseError('no input'); |
| |
return false; |
| } |
} |
| |
|
| $this->_data['doctype'] = ''; |
$this->_data['doctype'] = ''; |
| $this->_data['head'] = ''; |
$this->_data['head'] = ''; |
| $this->_data['body'] = ''; |
$this->_data['body'] = ''; |
| |
$this->_data['head_attr'] = ''; |
| $this->_data['body_attr'] = ''; |
$this->_data['body_attr'] = ''; |
| |
|
| $all_data = ''; |
$all_data = ''; |
| while ($data = fread($this->fp, 2048)) { |
while ($data = fread($this->_fp, 2048)) { |
| $all_data .= $data; |
$all_data .= $data; |
| } |
} |
| fclose($this->fp); |
fclose($this->_fp); |
| |
|
| if (preg_match('|^\s*<!DOCTYPE ([^>]*)>|', $all_data, $matches)) { |
if (preg_match('|^\s*<!DOCTYPE ([^>]*)>|', $all_data, $matches)) { |
| $this->_data['doctype'] = trim($matches[1]); |
$this->_data['doctype'] = trim($matches[1]); |
| } |
} |
| if (preg_match('|<head>(.*)</head>|si', $all_data, $matches)) { |
if (preg_match('|<head([^>]*)>(.*)</head>|si', $all_data, $matches)) { |
| $this->_data['head'] = trim($matches[1]); |
$this->_data['head_attr'] = trim($matches[1]); |
| |
$this->_data['head'] = trim($matches[2]); |
| } |
} |
| if (preg_match('|<body([^>]*)>(.*)</body>|si', $all_data, $matches)) { |
if (preg_match('|<body([^>]*)>(.*)</body>|si', $all_data, $matches)) { |
| $this->_data['body_attr'] = trim($matches[1]); |
$this->_data['body_attr'] = trim($matches[1]); |
| Line 92 class HTML_Content |
|
| Line 122 class HTML_Content |
|
| return true; |
return true; |
| } |
} |
| |
|
| |
function _getTag($where, $name, $idx = 0) |
| |
{ |
| |
$ar =& $this->_data[$where.'_tags'][$name]; |
| |
if (! isset($ar)) { |
| |
$ar = $this->_parseTag($where, $name); |
| |
} |
| |
if ($idx < 0) { |
| |
return $ar; |
| |
} |
| |
if ($idx > count($ar)) { |
| |
return false; |
| |
} |
| |
return $ar[$idx]; |
| |
} |
| |
|
| |
/* $where must be head/body, $name must be lowercase */ |
| |
function _parseTag($where, $name) |
| |
{ |
| |
$ret = array(); |
| |
$matches = array(); |
| |
/* |
| |
echo "$where/$name/"; |
| |
echo htmlspecialchars('|<'.$name.'([^>]*)>([^<>]*)</'.$name.'>|si'); |
| |
echo '<hr>'; |
| |
echo htmlspecialchars($this->_data[$where]); |
| |
echo '<hr>'; |
| |
*/ |
| |
if (preg_match_all('|<'.$name.'([^>]*)>(.*)</'.$name.'>|si', |
| |
$this->_data[$where], $matches) > 0) { |
| |
} elseif (preg_match_all('|<'.$name.'([^>]*)>|si', |
| |
$this->_data[$where], $matches)) { |
| |
} |
| |
|
| |
for ($j = 0; $j < count($matches[1]); $j++) { |
| |
$ret[$j]['__attributes'] = $matches[1][$j]; |
| |
isset($matches[2][$j]) && $ret[$j]['__content'] = $matches[2][$j]; |
| |
$ar = preg_split('{([\w-]+)=(\'[^\']*\'|"[^"]*"|[^ ]*)}si', |
| |
$matches[1][$j], -1, PREG_SPLIT_DELIM_CAPTURE); |
| |
for ($i = 1; $i < count($ar); $i++) { |
| |
$attr = $ar[$i++]; |
| |
$value = $ar[$i++]; |
| |
$l_val = strlen($value); |
| |
if ($l_val > 1 && (($value[0] == "'" && $value[$l_val - 1] == "'") |
| |
|| ($value[0] == '"' && $value[$l_val - 1] == '"'))) { |
| |
$value = substr($value, 1, $l_val - 2); |
| |
} |
| |
|
| |
$ret[$j][strtolower($attr)] = trim($value); |
| |
} |
| |
} |
| |
|
| |
return $ret; |
| |
} |
| |
|
| function getHTML() |
function getHTML() |
| { |
{ |
| return '<!DOCTYPE '.$this->_data['doctype'].">\n" |
return '<!DOCTYPE '.$this->_data['doctype'].">\n" |
| Line 109 class HTML_Content |
|
| Line 193 class HTML_Content |
|
| { |
{ |
| return $this->_data['body']; |
return $this->_data['body']; |
| } |
} |
| |
|
| |
function getTitle() |
| |
{ |
| |
//$this->_data['title'] == null && $this->_parseTitle(); |
| |
//return $this->_data['title']; |
| |
|
| |
$title_tag = $this->_getTag('head', 'title', 0); |
| |
return $title_tag['__content']; |
| |
} |
| |
|
| |
function getTag($name, $attributes) |
| |
{ |
| |
$ret = "$name"; |
| |
foreach ($attributes as $attr => $value) { |
| |
if (! strncmp('__', $value, 2)) { |
| |
continue; |
| |
} |
| |
|
| |
$ret .= " $attr="; |
| |
if (strchr($value, '"') && strchr($value, "'")) { |
| |
$value = str_replace('"', '', $value); |
| |
} |
| |
if (strchr($value, '"')) { |
| |
$ret .= "'$value'"; |
| |
} else { |
| |
$ret .= '"'.$value.'"'; |
| |
} |
| |
} |
| |
$ret = '<'.htmlspecialchars($ret).'>'; |
| |
if (isset($attributes['__content'])) { |
| |
$ret .= $attributes['__content']; |
| |
$ret .= '</'.htmlspecialchars($name).'>'; |
| |
} |
| |
return $ret; |
| |
} |
| |
|
| |
# |
| |
# D E P R E C A T E D |
| |
# |
| |
|
| |
function getMeta($attr, $value) |
| |
{ |
| |
$this->_data['meta'] == null && $this->_parseMeta(); |
| |
$attr = strtolower($attr); |
| |
for ($i = 0; $i < count($this->_data['meta']); $i++) { |
| |
if (isset($this->_data['meta'][$i][$attr]) |
| |
&& stristr($this->_data['meta'][$i][$attr], $value)) { |
| |
return $this->_data['meta'][$i]; |
| |
} |
| |
} |
| |
} |
| |
|
| |
function _parseTitle() |
| |
{ |
| |
if (preg_match('|<title>(.*)</title>|si', $this->_data['head'], $matches)) { |
| |
$this->_data['title'] = trim($matches[1]); |
| |
return; |
| |
} |
| |
$this->_data['title'] = ''; |
| |
} |
| |
|
| |
function _parseMeta() |
| |
{ |
| |
$this->_data['meta'] = array(); |
| |
if (preg_match_all('|<meta ([^>]*)>|si', $this->_data['head'], $matches) > 0) { |
| |
$meta_idx = 0; |
| |
foreach ($matches[1] as $val) { |
| |
$ar = preg_split('{([\w-]+)=(\'[^\']*\'|"[^"]*"|[^ ]*)}si', |
| |
$val, -1, PREG_SPLIT_DELIM_CAPTURE); |
| |
for ($i = 1; $i < count($ar); $i++) { |
| |
$attr = $ar[$i++]; |
| |
$value = $ar[$i++]; |
| |
$l_val = strlen($value); |
| |
if ($l_val > 1 && (($value[0] == "'" && $value[$l_val - 1] == "'") |
| |
|| ($value[0] == '"' && $value[$l_val - 1] == '"'))) { |
| |
$value = substr($value, 1, $l_val - 2); |
| |
} |
| |
|
| |
$this->_data['meta'][$meta_idx][strtolower($attr)] = trim($value); |
| |
} |
| |
$meta_idx++; |
| |
} |
| |
} |
| |
} |
| |
|
| |
|
| } |
} |
| |
|
| |
|