functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:23k
- <?php
- error_reporting(E_ALL & ~E_NOTICE);
- @set_time_limit(0);
- // ############################################################################
- // A version of array_contains() that uses binary sort to find the element
- // Only use on sorted arrays!!!
- function array_contains_binary($needle, &$haystack) {
- if (!is_array($haystack)) {
- return false;
- }
- $low = 0;
- $high = count($haystack);
- $needle = strtolower($needle);
- while ($low <= $high) {
- $mid = floor(($low + $high) / 2);
- if (strcasecmp($needle, $haystack[$mid]) == 0 ) {
- return true;
- break;
- } elseif (strcasecmp($needle, $haystack[$mid]) < 0 ) {
- $high = $mid - 1;
- } else {
- $low = $mid + 1;
- }
- }
- return false;
- }
- // ############################################################################
- // A version of in_array() that allows case-insensitive matching (by default!)
- function array_contains($needle, $haystack, $strict = false, $matchcase = false) {
- if (!is_array($haystack)) {
- return false;
- }
- if (!$matchcase and is_string($needle)) {
- $needle = strtolower($needle);
- }
- foreach ($haystack as $element) {
- if (!$matchcase and is_string($needle)) {
- $element = strtolower($element);
- }
- if (($strict and $needle === $element) or (!$strict and $needle == $element)) {
- return true;
- }
- }
- return false;
- }
- // ############################################################################
- // Returns $true if $eval is true, $false if it is false
- function iif($eval, $true, $false = '') {
- return (($eval == true) ? ($true) : ($false));
- }
- // ############################################################################
- //
- function readfromfile($filename, $binary = true, $getdata = true) {
- $mode = 'r'.iif($binary, 'b');
- $data = '';
- if (is_readable($filename)
- and is_resource($fp = @fopen($filename, $mode)) and (!$getdata or filesize($filename) == 0 or $data = @fread($fp, @filesize($filename))) and @fclose($fp)) {
-
- return $data;
- } else {
- return false;
- }
- }
- // ############################################################################
- // Calculates microtime difference from $start
- function microdiff($start) {
- return realmicrotime() - realmicrotime($start);
- }
-
- // ############################################################################
- // Returns a real timestamp from a microtime
- function realmicrotime($time = null) {
- if ($time === null) {
- $time = microtime();
- }
- $timebits = explode(' ', $time);
- return $timebits[0] + $timebits[1];
- }
- // ############################################################################
- // Calculates the LCS between the two strings and return the number of similar chars
- function spell_similar($A, $B) {
- $L = array();
- $m = strlen($A);
- $n = strlen($B);
- for ($i = $m; $i >= 0; $i--) {
- for ($j = $n; $j >= 0; $j--) {
- if ($i >= $m or $j >= $n) {
- $L[$i][$j] = 0;
- } elseif ($A[$i] == $B[$j]) {
- $L[$i][$j] = 1 + $L[$i+1][$j+1];
- } else {
- $L[$i][$j] = max($L[$i+1][$j], $L[$i][$j+1]);
- }
- }
- }
- return $L[0][0] / max($m, $n);
- }
- // ############################################################################
- // Processes input for fields
- function spell_process($text, $method, $useHtml, $sendAfter) {
- global $_meta_markers;
- $starttime = microdiff(STARTTIME);
- $intMaxSuggestions = 6;
- $showback = 6;
- $showforward = 6;
- $userDictionaryAvailable = false;
- $userWords = $realWords = $misspelledArray = $deadKeys = $tagKeys = $wordKeys = $sxValues = array();
- $jsOutput = '';
- //echo '<!-- ';
- if (!empty($text)) {
- // Operate on the HTML text so it's split correctly
- $strDictArray = preg_split("#r?n#", readfromfile('./dict-large.txt'));
- echo "Words: ".(microdiff(STARTTIME) - $starttime)."<br />";
- $strDictArray = array_flip($strDictArray);
- echo "Flip: ".(microdiff(STARTTIME) - $starttime)."<br />";
- $originalString = preg_replace("#r?n#", "n", $text);
- $words = preg_split('#([^a-z'])#i', $originalString, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
- echo "Split: ".(microdiff(STARTTIME) - $starttime)."<br />";
- $tagOpen = false;
- $realKey = $demoKey = 0;
- if ($useHtml) {
- while (list($key, $value) = each($words)) {
- if ($value == '<') {
- $tagOpen = true;
- } elseif ($value == '>' or $tagOpen) {
- $demoKey--;
- $words[$demoKey] .= array_extract($words, $key);
- $deadKeys[] = $key;
- if ($value == '>') {
- $tagKeys[] = $realKey;
- $realKey++;
- $demoKey = $key;
- $tagOpen = false;
- }
- } else {
- $realWords[$realKey] = $value;
- $realKey++;
- }
- $demoKey++;
- }
- } else {
- $realWords = $words;
- }
- $words = array_values($words);;
- $wordCount = count($words);
- //print_R($realWords);
- echo "HTML: ".(microdiff(STARTTIME) - $starttime)."<br />";
- // Tracks original index of word in $words array
- $oi = 0;
- // Get user's dictionary
- /*
- $getUserWords = $DB_site->query("
- SELECT word
- FROM hive_word
- WHERE userid = $hiveuser[userid]
- ");
- while ($userWord = $DB_site->fetch_array($getUserWords)) {
- $userWords[] = $userWord['word'];
- }
- echo "User: ".(microdiff(STARTTIME) - $starttime)."<br />";
- */
- // Loop over each word in string
- $checks = 0;
- foreach ($realWords as $strWordKey => $strWordIn) {
- //if (in_array($strWordKey, $tagKeys)) {
- // This is an HTML tag or a word that was already checked this word
- // Increment original index and move along
- //continue;
- //}
- $oi = $strWordKey;
- // Remove invalid characters from the word
- $strWordOut = $strWordIn;
- $strWordOut = preg_replace('#[^a-z']#i', '', $strWordOut);
- if (substr_count($strWordOut, "'") > 1) {
- $strWordOut = str_replace("'", '', $strWordOut);
- }
- // Remove 's at the end of the word
- if (substr($strWordOut, -2) == "'s") {
- $strWordOut = substr($strWordOut, 0, strlen($strWordOut) - 2);
- }
- $strWordOut = trim($strWordOut, "' tnr x0B");
- // Nothing left...
- if (empty($strWordOut)) {
- continue;
- }
- // Store the word's key
- $wordKeys[] = $oi;
- // Word need not have capitals
- $checkWord = strtolower($strWordOut);
- // Search main dictionary
- $spellOk = isset($strDictArray["$checkWord"]);
- // Check user's dictionary
- // I decided to not do this here, but instead check later if
- // if the any of the suggestions match the actual word which
- // means it's a word that the user added. The cost of running
- // this binary search for every word is just not worth it.
- if (false and !$spellOk and !empty($userWords)) {
- // Binary sort to find word in user's array
- // (Only if the array isn't short)
- if (false and count($userWords) < 20) {
- $spellOk = in_array($checkWord, $userWords);
- } else {
- $low = 0;
- $high = count($userWords);
- while ($low <= $high) {
- $mid = floor(($low + $high) / 2);
- if (strcasecmp($checkWord, $userWords[$mid]) == 0 ) {
- $spellOk = true;
- break;
- } elseif (strcasecmp($checkWord, $userWords[$mid]) < 0 ) {
- $high = $mid - 1;
- } else {
- $low = $mid + 1;
- }
- }
- }
- }
- // If word is spelled wrong, store in array
- if (!$spellOk) {
- // Calculate soundex/metaphone using PHP and create the misspelled array entry
- $sndx = $method($strWordOut);
- if (!array_contains($sndx, $sxValues)) {
- $sxValues[] = $sndx;
- }
- $misspelledArray[] = array(
- 'word' => $strWordIn,
- 'sndx' => $sndx,
- 'possugs' => array(),
- 'realsugs' => array(),
- 'oi' => $oi,
- );
- }
- }
- $lastOi = $oi;
- echo "Spell: ".(microdiff(STARTTIME) - $starttime)."<br />";
- $wordKeysFlipped = array_flip($wordKeys);
- echo "Flip: ".(microdiff(STARTTIME) - $starttime)."<br />";
- // Total count of misspelled words in the array
- $misspelledWordCount = count($misspelledArray);
-
- if ($misspelledWordCount > 0) {
- // Execute query
- $strSuggArray = preg_split("#r?n#", readfromfile('./dict-metaphone-sort.txt'));
- echo "Suggest: ".(microdiff(STARTTIME) - $starttime)."<br />";
- // JavaScript variables that will store all information
- $jsOutput .= "var msWc = $misspelledWordCount;n";
- $jsOutput .= "var msMissWordAr = new Array($misspelledWordCount);n";
- $jsOutput .= "var msOrigWordAr = new Array($wordCount);n";
- $jsOutput .= "var lastOrigWord;n";
- // Loop over array to get suggestions
- $doneSugsFor = array();
- for ($x = 0; $x < $misspelledWordCount; $x++) {
- $subAr3 = &$misspelledArray[$x];
- $intWordLen = strlen($subAr3['word']);
- $subArPoss3 = &$subAr3['possugs'];
- // Create context string
- $oi = $subAr3['oi'];
- $oiStart = $wordKeys[(($wordKeysFlipped[$oi] - $showback) >= 0) ? ($wordKeysFlipped[$oi] - $showback) : 0];
- $oiEnd = $wordKeys[(($wordKeysFlipped[$oi] + $showforward + 1) <= count($wordKeys) - 1) ? ($wordKeysFlipped[$oi] + $showforward + 1) : count($wordKeys) - 1];
- $context = '';
- if ($oi > $showback) {
- $context .= '...';
- }
- for (; $oiStart != $oiEnd + 1; $oiStart++) {
- if ($oiStart == $oi) {
- $context .= '<font color="red"><b>'.$words[$oi].'</b></font>';
- } else {
- $context .= $words[$oiStart];
- }
- }
- if ($oi < ($wordCount - 1 - $showforward)) {
- $context .= '...';
- }
- // Loop over similarities (possible suggestions), score and get top (real) suggestions
- // Check if we already did suggestions for this word and if so, load that
- $dblSimilarityArray = array();
- if (false and isset($doneSugsFor["$subAr3[word]"])) {
- $dblSimilarityArray = $misspelledArray[$doneSugsFor["$subAr3[word]"]]['possugs'];
- } else {
- // Binary sort to find word in dict array
- $low = $_meta_markers[substr($subAr3['sndx'], 0, 3)][0] - 1;
- $high = $_meta_markers[substr($subAr3['sndx'], 0, 3)][1] + 3;
- $foundSndx = false;
- //echo "Low: $low; High: $high;<br />";
- while ($low <= $high) {
- $mid = floor(($low + $high) / 2);
- $checks++;
- //$check = strcasecmp($subAr3['sndx'].'|', substr($strSuggArray[$mid], 0, strlen($subAr3['sndx']) + 1));
- $check = strcasecmp($subAr3['sndx'], preg_replace('#^([^|]+)|.*$#', '$1', $strSuggArray[$mid]));
- if ($check == 0) {
- $foundSndx = true;
- break;
- } elseif ($check < 0) {
- $high = $mid - 1;
- } else {
- $low = $mid + 1;
- }
- }
- if ($foundSndx) {
- $subArPoss3 = explode('|', substr($strSuggArray[$mid], strlen($subAr3['sndx']) + 1));
- } else {
- $subArPoss3 = array();
- }
- $subCount = count($subArPoss3);
- if ($subCount <= 1) {
- $dblSimilarityArray = $subArPoss3;
- } else {
- for ($y = 0; $y < $subCount; $y++) {
- $strSimilarWord = $subArPoss3[$y];
- $intSimilarWordLen = strlen($strSimilarWord);
- $maxBonus = 3;
- $maxScore = ($intWordLen * 2) + $maxBonus;
- $LCS = spell_similar($subAr3['word'], $strSimilarWord);
- $score = ($maxBonus - abs($intWordLen - $intSimilarWordLen) + $LCS * $maxBonus) / ($maxScore);
- $dblSimilarity = round($score, 10);
- while (array_key_exists("$dblSimilarity", $dblSimilarityArray)) {
- $dblSimilarity += .0000000001;
- }
- $dblSimilarityArray["$dblSimilarity"] = $strSimilarWord;
- }
- $subArPoss3 = $dblSimilarityArray;
- // Sort array by key value (score)
- krsort($dblSimilarityArray);
- reset($dblSimilarityArray);
- }
- }
- // Perpare JavaScript variables
- $jsOutput .= "msMissWordAr[$x] = new Array(4);n";
- $jsOutput .= "msMissWordAr[$x][0] = '".str_replace("n", '', addslashes($subAr3['word']))."';n";
- $jsOutput .= "msMissWordAr[$x][1] = $oi;n";
- $jsOutput .= "msMissWordAr[$x][2] = '".str_replace(array('n', "n"), '<br />n', trim(addslashes($context)))."';n";
- // Build suggestions array
- $sugCount = iif($intMaxSuggestions < count($dblSimilarityArray), $intMaxSuggestions, count($dblSimilarityArray));
- echo 'Suggs: '.$sugCount.'<br />';
- $jsOutput .= "msMissWordAr[$x][3] = new Array($sugCount);n";
- for ($l = 0; $l < $sugCount; $l++) {
- $jsOutput .= "msMissWordAr[$x][3][$l] = '".str_replace("n", '', addslashes(current($dblSimilarityArray)))."';n";
- next($dblSimilarityArray);
- }
- // Cache this word
- if (!isset($doneSugsFor["$subAr3[word]"])) {
- $doneSugsFor["$subAr3[word]"] = $oi;
- }
- }
- // Build array of *ALL* words in text
- for ($x = 0; $x < count($words); $x++) {
- $jsOutput .= "msOrigWordAr[$x] = '".str_replace("n", 'n', addslashes($words[$x]))."';n";
- }
- // Javascript: reload content frame with new frameset
- $jsOutput .= "parent.content.location = 'compose.spell.php?cmd=suggestions&sendafter=$sendAfter';n";
- }
- }
- // Javascript: if no words are misspelled, reload content frame with message page
- if (empty($jsOutput)) {
- $jsOutput .= "parent.content.location = 'compose.spell.php?cmd=noerrors';n";
- }
- //echo ' -->';
- echo "End: ".(microdiff(STARTTIME) - $starttime)."<br />";
- echo "Binary checks: $checks";
- return $jsOutput;
- }
- function alert($text) {
-
- echo "<SCRIPT language="javascript">
- alert('$text');
- </SCRIPT>";
- }
- #######################################################
- # TEMPORARY TEMPLATE CODE #
- function gettemplate($template_name) {
- $template = @implode('', @file("./$template_name.html"));
- $template = parse_conditionals($template);
-
- return str_replace("'", ''', $template);
- }
- function makeeval($varname, $templatename = '', $add = false, $dieonecho = true, $comments = true) {
- $template = gettemplate($templatename, $comments);
- if ($varname == 'echo') {
- $template = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes(stripslashes('\1')).'n$__output = ob_get_contents();nob_end_clean();necho $__output."'", $template);
- // echo $template;
- return 'echo '.$template.'; ';
- } else {
- $template = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes('\1').'n$$varname .= ob_get_contents();nob_end_clean();n$$varname .= "'", $template);
- return '$'.$varname.' = '.$template.';';
- }
- }
- ##########################################################
- #
- # function format_text_item: return formatted string
- # without leading spaces and with slashes
- # for example: $in = " test''test "
- # function return: "test''test"
- #
- function format_text_item($in)
- {
- // $in = trim($in);
- if ((substr($in, 0, 2) == "rn")) {
- $in = substr($in, 2);
- }
- $in = addslashes($in);
-
- return ""$in"";
- }
- ##########################################################
- #
- # recursive function tree_to_tertiary_operator: return
- # tertiary operator string, which was built from tree
- #
- function tree_to_tertiary_operator(& $node)
- {
- $out = '';
-
- $dataref = $node['data'];
- $node_items = array();
- for ($i = 0; $i < count($node['data']); $i++)
- {
- $data_item = & $node['data'][$i];
- if (is_array($data_item))
- {
- $if_block = '(('.$data_item['if']['conditional'].') ? (';
- $if_data = tree_to_tertiary_operator($data_item['if']);
- $if_block .= $if_data.') : ';
-
- if (!is_array($data_item['elseif']) && !is_array($data_item['else']))
- {
- $if_block .= "''";
- }
- elseif (!is_array($data_item['elseif']) && is_array($data_item['else']))
- {
- $else_block = tree_to_tertiary_operator($data_item['else']);
- $if_block .= "($else_block)";
- }
- else
- {
- $elif_block = elsif_array_to_tertiary_operator($data_item['elseif'],
- $data_item['else']);
- $if_block .= "($elif_block)";
- }
-
- array_push ($node_items, $if_block.')');
- }
- else
- {
- array_push ($node_items, format_text_item($data_item));
- }
- }
- $out .= join('.', $node_items);
- // changed
- if (empty($out)) {
- $out = '""';
- }
-
- return $out;
- }
- #################################################################
- #
- # function elsif_array_to_tertiary_operator: return
- # tertiary operator string, which was built from tree for
- # elseif operator
- #
- #
- function elsif_array_to_tertiary_operator (& $elsif_array, & $else_hash)
- {
- $out = '(';
- $elsif_hash = array_shift($elsif_array);
-
- $out .= "(".$elsif_hash['conditional'].") ? (";
- $out .= tree_to_tertiary_operator($elsif_hash);
- $out .= ") : ";
-
- if (count($elsif_array) > 0)
- {
- $out .= elsif_array_to_tertiary_operator($elsif_array, $else_hash);
- }
- elseif (is_array($else_hash))
- {
- $out .= "(";
- $out .= tree_to_tertiary_operator($else_hash);
- $out .= ")";
- }
- else
- {
- $out .= """";
- }
-
- $out .= ")";
-
- return $out;
- }
- ##############################################################
- #
- # recursive function lex_queue_to_tree: return
- # tree. This function check also syntax for all lexemes
- #
- #
- function lex_queue_to_tree(& $root_node,& $lex_queue, $level_deep)
- {
- $ignore_level_up = 0;
- do
- {
- $next_item = array_shift($lex_queue);
- if ($next_item['type'] == 'text')
- {
- array_push ($root_node['data'], $next_item['value']);
- }
- elseif ($next_item['type'] == 'if')
- {
- $conditional = array_shift($lex_queue);
- if(!($conditional['type'] == 'conditional' && $conditional['value'] != ""))
- {
- die ("If requires conditional statement");
- }
-
- $new_check_node['if']['parent'] = & $new_check_node;
- array_push ($root_node['data'], array(
- 'if' => array(
- 'conditional' => $conditional['value'],
- 'data' => array()
- )
- ));
- $ignore_level_up =
- lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['if'],
- $lex_queue, $level_deep + 1);
- }
- elseif ($next_item['type'] == 'elseif')
- {
- if ($ignore_level_up > 0)
- $ignore_level_up = 0;
- else
- {
- array_unshift($lex_queue, $next_item);
- return 1;
- }
-
- $conditional = array_shift($lex_queue);
- if(!($conditional['type'] == 'conditional' && $conditional['value']!=""))
- {
- die ("ElseIf requires conditional statement");
- }
- if(!(is_array($root_node['data'][count($root_node['data']) - 1])))
- {
- die ("ElseIf requires preceeding If");
- }
- if (!is_array($root_node['data'][count($root_node['data']) - 1]['elseif']))
- {
- $root_node['data'][count($root_node['data']) - 1]['elseif'] = array();
- }
-
- array_push($root_node['data'][count($root_node['data']) - 1]['elseif'], array(
- 'conditional' => $conditional['value'],
- 'data' => array()
- ));
-
- $ignore_level_up =
- lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['elseif']
- [count($root_node['data'][count($root_node['data']) - 1]['elseif']) - 1],
- $lex_queue, $level_deep + 1);
- }
- elseif ($next_item['type'] == 'else')
- {
- if ($ignore_level_up > 0)
- $ignore_level_up = 0;
- else
- {
- array_unshift($lex_queue, $next_item);
- return 1;
- }
- if(!(is_array($root_node['data'][count($root_node['data']) - 1])))
- {
- die ("Else requires preceeding If");
- }
-
- if (defined($root_node['data'][count($root_node['data']) - 1]['else']))
- {
- die ("Else already defined for If");
- }
-
- $root_node['data'][count($root_node['data']) - 1]['else']
- = array( 'data' => array() );
-
- lex_queue_to_tree($root_node['data'][count($root_node['data']) - 1]['else'],
- $lex_queue, $level_deep + 1);
- }
- elseif ($next_item['type'] == 'endif')
- {
- if(!$level_deep)
- {
- die ("EndIf requires preceeding If");
- }
- return 0;
- }
- elseif ($next_item['type'] == 'conditional')
- {
- die ("Unexpected conditional statement, probably bug in parsern");
- }
- } while (count($lex_queue) > 0);
-
- if ($level_deep)
- {
- die("No enough EndIf'sn");
- }
-
- return 0;
- }
- #######################################################################
- #
- # function text_to_lexemes_queue: return hash, which contain
- # queue with lexemes from template
- #
- #
- function text_to_lexemes_queue($text)
- {
- #list ($parse_conditional, $consume_chars) = (0, 0);
- $parse_conditional = 0;
- $consume_chars = 0;
- $out = array();
- do
- {
- $parse_conditional = 0;
- if (preg_match("/^(<%elseifs+)/s", $text, $matches))
- {
- $consume_chars = strlen($matches[1]);
- array_push($out, array( 'type' => 'elseif' ));
- $parse_conditional = 1;
- }
- elseif (preg_match("/^(<%else%>)/s",$text,$matches))
- {
- $consume_chars = strlen($matches[1]);
- array_push($out, array( 'type' => 'else' ));
- }
- elseif (preg_match("/^(<%endif%>)/s",$text, $matches))
- {
- $consume_chars = strlen($matches[1]);
- array_push($out, array( 'type' => 'endif' ));
- }
- elseif (preg_match("/^(<%ifs+)/s", $text,$matches))
- {
- $consume_chars = strlen($matches[1]);
- array_push($out, array( 'type' => 'if' ));
- $parse_conditional = 1;
- }
- elseif (preg_match("/^(.)/s", $text, $matches))
- {
- $consume_chars = 1;
- $textchar = $matches[1];
-
- if ($out[count($out)-1]['type'] == 'text')
- {
- $out[count($out)-1]['value'] .= $textchar;
- }
- else
- {
- array_push($out, array('type' => 'text', 'value' => $textchar ));
- }
- }
- $text = substr($text, $consume_chars);
- if ($parse_conditional)
- {
- array_push ($out, array('type' => 'conditional', 'value' => read_conditional($text)));
- }
- } while (strlen ($text));
-
- return $out;
- }
- ################################################################################
- #
- # function read_conditional: become reference to conditional in
- # if or elseif statements.
- #
- #
- function read_conditional(& $textref)
- {
- $out='';
- $consume_chars=0;
- $exit=0;
- $parse_quoted=0;
- $quote_char=0;
- do
- {
- $parse_quoted = 0;
-
- if ((preg_match("/^(')/", $textref,$matches)) || (preg_match("/^(")/",$textref,$matches)))
- {
- $quote_char = $matches[1];
- $out .= $quote_char;
- $consume_chars = 1;
- $parse_quoted = 1;
- }
- elseif (preg_match("/^(%>)/",$textref))
- {
- $consume_chars = 2;
- $exit = 1;
- }
- elseif (preg_match("/^(.)/s", $textref, $matches))
- {
- $out .= $matches[1];
- $consume_chars = 1;
- }
- elseif (!strlen($textref))
- {
- die ("Unexpected end of text while reading PHP conditional statement");
- }
-
- $textref = substr($textref, $consume_chars);
- if ($parse_quoted)
- {
- $out .= read_quoted($textref, $quote_char);
- }
- } while (! $exit);
-
- # (PHP syntax) check out with eval
- return $out;
- }
- ####################################################################
- #
- # function read_quoted: function for testing quoted text
- #
- #
- function read_quoted (& $textref, $quote_char)
- {
- $regexp_quote = AddSlashes($quote_char);
- $out = '';
- $consume_chars = 0;
- $exit_quote = 0;
- do
- {
- if ((preg_match("/^\$regexp_quote/",$textref)) || (preg_match("/^\\/", $textref)))
- {
- $consume_chars = 2;
- }
- elseif (preg_match("/^$regexp_quote/", $textref))
- {
- $consume_chars = 1;
- $exit_quote = 1;
- }
- elseif (preg_match("/^(.)/s", $textref))
- {
- $consume_chars = 1;
- }
- elseif (!strlen($textref))
- {
- die ("Unexpected end of text while reading quoted string");
- }
-
- $out .= substr($textref, 0, $consume_chars);
- $textref = substr($textref, $consume_chars);
- } while (! $exit_quote);
-
- return $out;
- }
- function parse_conditionals($template) {
- $template = preg_replace("#s?<%!--.*--!%>s?#", '', $template);
- if ($GLOBALS['session_url']) {
- $template = preg_replace('#.php(?)?#ies', ''.php{$GLOBALS[session_url]}'.(('1' != '') ? ('{$GLOBALS[session_ampersand]}') : (''))', $template);
- }
- $lex_queue = text_to_lexemes_queue($template);
- $root = array('data' => array());
- if (lex_queue_to_tree($root, $lex_queue, 0)) {
- die ("Else already defined or ElseIf without Ifn");
- }
- $output = tree_to_tertiary_operator($root);
- $output = get_loops($output);
- return $output;
- }
- function get_loops ($template) {
- $template = preg_replace('#<%loop ($[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*) ?%>(.*)<%endloop%>#iUse', "loop_code('\1', '\2')",$template);
- return $template;
- }
- function loop_code($loop_name, $loop_code) {
- $loop_name = stripslashes($loop_name);
- $loop_code = stripslashes($loop_code);
- $loop_name2 = substr($loop_name, 1);
- $loop_code = str_replace("'", "loop-temp-section", $loop_code);
- return " " . doloop('$loop_name2', $loop_name, '$loop_code', $GLOBALS) . "";
- }