Пример #1
0
 public static function ParseScript($scriptContent)
 {
     $arComponents = array();
     $componentNumber = -1;
     $bInComponent = false;
     $bInPHP = false;
     $bInString = false;
     $quoteChar = "";
     $bSlashed = false;
     $string = false;
     $instruction = "";
     //mb_substr is catastrophic slow, so in UTF we use array of characters
     if (defined("BX_UTF")) {
         $allChars = preg_split('//u', $scriptContent, -1, PREG_SPLIT_NO_EMPTY);
     } else {
         $allChars =& $scriptContent;
     }
     $scriptContentLength = strlen($scriptContent);
     $arAllStr = array();
     $ind = -1;
     while ($ind < $scriptContentLength - 1) {
         $ind++;
         $ch = $allChars[$ind];
         if ($bInPHP) {
             if (!$bInString) {
                 if (!$bInComponent && $instruction != '') {
                     if (preg_match("#\\s*((\\\$[A-Z_][A-Z0-9_]*\\s*=)?\\s*\\\$APPLICATION->IncludeComponent\\s*\\()#is", $instruction, $arMatches)) {
                         $arAllStr = array();
                         $bInComponent = true;
                         $componentNumber++;
                         $instruction = $arMatches[1];
                         $arComponents[$componentNumber] = array("START" => $ind - strlen($arMatches[1]), "END" => false, "DATA" => array());
                     }
                 }
                 if ($string !== false) {
                     if ($bInComponent) {
                         $arAllStr[] = $string;
                         $instruction .= chr(1) . (count($arAllStr) - 1) . chr(2);
                     }
                     $string = false;
                 }
                 if ($ch == ";") {
                     if ($bInComponent) {
                         $bInComponent = false;
                         $arComponents[$componentNumber]["END"] = $ind + 1;
                         $arComponents[$componentNumber]["DATA"] = PHPParser::GetComponentParams(preg_replace("#[ \r\n\t]#", "", $instruction), $arAllStr);
                     }
                     $instruction = "";
                     continue;
                 }
                 if ($ch == "/" && $ind < $scriptContentLength - 2) {
                     $nextChar = $allChars[$ind + 1];
                     if ($nextChar == "/") {
                         $endPos = strpos($scriptContent, "\n", $ind + 2);
                         if ($endPos === false) {
                             $ind = $scriptContentLength - 1;
                         } else {
                             $ind = $endPos;
                         }
                         continue;
                     } elseif ($nextChar == "*") {
                         $endPos = strpos($scriptContent, "*/", $ind + 2);
                         if ($endPos === false) {
                             $ind = $scriptContentLength - 1;
                         } else {
                             $ind = $endPos + 1;
                         }
                         continue;
                     }
                 }
                 if ($ch == "\"" || $ch == "'") {
                     $bInString = true;
                     $string = "";
                     $quoteChar = $ch;
                     continue;
                 }
                 if ($ch == "?" && $ind < $scriptContentLength - 2 && $allChars[$ind + 1] == ">") {
                     $ind += 1;
                     if ($bInComponent) {
                         $bInComponent = false;
                         $arComponents[$componentNumber]["END"] = $ind - 1;
                         $arComponents[$componentNumber]["DATA"] = PHPParser::GetComponentParams(preg_replace("#[ \r\n\t]#", "", $instruction), $arAllStr);
                     }
                     $instruction = "";
                     $bInPHP = false;
                     continue;
                 }
                 $instruction .= $ch;
                 if ($ch == " " || $ch == "\r" || $ch == "\n" || $ch == "\t") {
                     continue;
                 }
             } else {
                 if ($ch == "\\" && !$bSlashed) {
                     $bSlashed = true;
                     continue;
                 }
                 if ($ch == $quoteChar && !$bSlashed) {
                     $bInString = false;
                     continue;
                 }
                 $bSlashed = false;
                 $string .= $ch;
             }
         } else {
             if ($ch == "<") {
                 if ($ind < $scriptContentLength - 5 && $allChars[$ind + 1] . $allChars[$ind + 2] . $allChars[$ind + 3] . $allChars[$ind + 4] == "?php") {
                     $bInPHP = true;
                     $ind += 4;
                 } elseif ($ind < $scriptContentLength - 2 && $allChars[$ind + 1] == "?") {
                     $bInPHP = true;
                     $ind += 1;
                 }
             }
         }
     }
     return $arComponents;
 }