function ParseFileContent($filesrc) { ///////////////////////////////////// // Parse prolog, epilog, title ///////////////////////////////////// $filesrc = trim($filesrc); $php_doubleq = false; $php_singleq = false; $php_comment = false; $php_star_comment = false; $php_line_comment = false; $php_st = "<"."?"; $php_ed = "?".">"; if(substr($filesrc, 0, 2)==$php_st) { $fl = strlen($filesrc); $p = 2; while($p < $fl) { $ch2 = substr($filesrc, $p, 2); $ch1 = substr($ch2, 0, 1); if($ch2==$php_ed && !$php_doubleq && !$php_singleq && !$php_star_comment) { $p+=2; break; } elseif(!$php_comment && $ch2=="//" && !$php_doubleq && !$php_singleq) { $php_comment = $php_line_comment = true; $p++; } elseif($php_line_comment && ($ch1=="\n" || $ch1=="\r" || $ch2=="?>")) { $php_comment = $php_line_comment = false; } elseif(!$php_comment && $ch2=="/*" && !$php_doubleq && !$php_singleq) { $php_comment = $php_star_comment = true; $p++; } elseif($php_star_comment && $ch2=="*/") { $php_comment = $php_star_comment = false; $p++; } elseif(!$php_comment) { if(($php_doubleq || $php_singleq) && $ch2=="\\\\") { $p++; } elseif(!$php_doubleq && $ch1=='"') { $php_doubleq=true; } elseif($php_doubleq && $ch1=='"' && substr($filesrc, $p-1, 1)!='\\') { $php_doubleq=false; } elseif(!$php_doubleq) { if(!$php_singleq && $ch1=="'") { $php_singleq=true; } elseif($php_singleq && $ch1=="'" && substr($filesrc, $p-1, 1)!='\\') { $php_singleq=false; } } } $p++; } $prolog = substr($filesrc, 0, $p); $filesrc = substr($filesrc, $p); } elseif(preg_match("'(.*?<title>.*?</title>)(.*)$'is", $filesrc, $reg)) { $prolog = $reg[1]; $filesrc= $reg[2]; } $title = false; if(strlen($prolog)) { if(preg_match("/\\\$APPLICATION->SetTitle\\s*\\(\\s*\"(.*?)(?<!\\\\)\"\\s*\\);/is", $prolog, $regs)) $title = UnEscapePHPString($regs[1]); elseif(preg_match("/\\\$APPLICATION->SetTitle\\s*\\(\\s*'(.*?)(?<!\\\\)'\\s*\\);/is", $prolog, $regs)) $title = UnEscapePHPString($regs[1]); elseif(preg_match("'<title[^>]*>([^>]+)</title[^>]*>'i", $prolog, $regs)) $title = $regs[1]; } if(!$title && preg_match("'<title[^>]*>([^>]+)</title[^>]*>'i", $filesrc, $regs)) $title = $regs[1]; $arPageProps = array(); if (strlen($prolog)>0) { preg_match_all("'\\\$APPLICATION->SetPageProperty\(\"(.*?)(?<!\\\\)\" *, *\"(.*?)(?<!\\\\)\"\);'i", $prolog, $out); if (count($out[0])>0) { for ($i1 = 0; $i1 < count($out[0]); $i1++) { $arPageProps[UnEscapePHPString($out[1][$i1])] = UnEscapePHPString($out[2][$i1]); } } } if(substr($filesrc, -2)=="?".">") { $p = strlen($filesrc) - 2; $php_start = "<"."?"; while(($p > 0) && (substr($filesrc, $p, 2) != $php_start)) $p--; $epilog = substr($filesrc, $p); $filesrc = substr($filesrc, 0, $p); } return Array( "PROLOG"=>$prolog, "TITLE"=>$title, "PROPERTIES"=>$arPageProps, "CONTENT"=>$filesrc, "EPILOG"=>$epilog ); }
function UnEscapePHPString($str) { return UnEscapePHPString($str); }
public static function UnEscapePHPString($str) { return UnEscapePHPString($str); }
public static function getPageTitle($filesrc, $prolog = false) { if ($prolog === false) { $chunks = PHPParser::getPhpChunks($filesrc, 1); if (!empty($chunks)) { $prolog =& $chunks[0]; } else { $prolog = ''; } } $title = false; if ($prolog != '') { if (preg_match("/\\\$APPLICATION->SetTitle\\s*\\(\\s*\"(.*?)(?<!\\\\)\"\\s*\\);/is", $prolog, $regs)) { $title = UnEscapePHPString($regs[1]); } elseif (preg_match("/\\\$APPLICATION->SetTitle\\s*\\(\\s*'(.*?)(?<!\\\\)'\\s*\\);/is", $prolog, $regs)) { $title = UnEscapePHPString($regs[1]); } elseif (preg_match("'<title[^>]*>([^>]+)</title[^>]*>'i", $prolog, $regs)) { $title = $regs[1]; } } if (!$title && preg_match("'<title[^>]*>([^>]+)</title[^>]*>'i", $filesrc, $regs)) { $title = $regs[1]; } return $title; }
function ParseFileContent($filesrc, $params = array()) { ///////////////////////////////////// // Parse prolog, epilog, title ///////////////////////////////////// $filesrc = trim($filesrc); $prolog = $epilog = ''; $php_doubleq = false; $php_singleq = false; $php_comment = false; $php_star_comment = false; $php_line_comment = false; $php_st = "<" . "?"; $php_ed = "?" . ">"; if ($params["use_php_parser"] && substr($filesrc, 0, 2) == $php_st) { $phpChunks = PHPParser::getPhpChunks($filesrc); if (!empty($phpChunks)) { $prolog = $phpChunks[0]; $filesrc = substr($filesrc, strlen($prolog)); } } elseif (substr($filesrc, 0, 2) == $php_st) { $fl = strlen($filesrc); $p = 2; while ($p < $fl) { $ch2 = substr($filesrc, $p, 2); $ch1 = substr($ch2, 0, 1); if ($ch2 == $php_ed && !$php_doubleq && !$php_singleq && !$php_star_comment) { $p += 2; break; } elseif (!$php_comment && $ch2 == "//" && !$php_doubleq && !$php_singleq) { $php_comment = $php_line_comment = true; $p++; } elseif ($php_line_comment && ($ch1 == "\n" || $ch1 == "\r" || $ch2 == "?>")) { $php_comment = $php_line_comment = false; } elseif (!$php_comment && $ch2 == "/*" && !$php_doubleq && !$php_singleq) { $php_comment = $php_star_comment = true; $p++; } elseif ($php_star_comment && $ch2 == "*/") { $php_comment = $php_star_comment = false; $p++; } elseif (!$php_comment) { if (($php_doubleq || $php_singleq) && $ch2 == "\\\\") { $p++; } elseif (!$php_doubleq && $ch1 == '"') { $php_doubleq = true; } elseif ($php_doubleq && $ch1 == '"' && substr($filesrc, $p - 1, 1) != '\\') { $php_doubleq = false; } elseif (!$php_doubleq) { if (!$php_singleq && $ch1 == "'") { $php_singleq = true; } elseif ($php_singleq && $ch1 == "'" && substr($filesrc, $p - 1, 1) != '\\') { $php_singleq = false; } } } $p++; } $prolog = substr($filesrc, 0, $p); $filesrc = substr($filesrc, $p); } elseif (preg_match("'(.*?<title>.*?</title>)(.*)\$'is", $filesrc, $reg)) { $prolog = $reg[1]; $filesrc = $reg[2]; } $title = PHPParser::getPageTitle($filesrc, $prolog); $arPageProps = array(); if (strlen($prolog)) { if (preg_match_all("'\\\$APPLICATION->SetPageProperty\\(([\"\\'])(.*?)(?<!\\\\)[\"\\'] *, *([\"\\'])(.*?)(?<!\\\\)[\"\\']\\);'i", $prolog, $out)) { foreach ($out[2] as $i => $m1) { $arPageProps[UnEscapePHPString($m1, $out[1][$i])] = UnEscapePHPString($out[4][$i], $out[3][$i]); } } } if (substr($filesrc, -2) == "?" . ">") { if (isset($phpChunks) && count($phpChunks) > 1) { $epilog = $phpChunks[count($phpChunks) - 1]; $filesrc = substr($filesrc, 0, -strlen($epilog)); } else { $p = strlen($filesrc) - 2; $php_start = "<" . "?"; while ($p > 0 && substr($filesrc, $p, 2) != $php_start) { $p--; } $epilog = substr($filesrc, $p); $filesrc = substr($filesrc, 0, $p); } } return array("PROLOG" => $prolog, "TITLE" => $title, "PROPERTIES" => $arPageProps, "CONTENT" => $filesrc, "EPILOG" => $epilog); }