Exemple #1
0
function tokenize($str, $anstype, $countcnt)
{
    global $allowedmacros;
    global $mathfuncs;
    global $disallowedwords, $disallowedvar;
    $i = 0;
    $connecttolast = 0;
    $len = strlen($str);
    $syms = array();
    $lastsym = array('', -1);
    while ($i < $len) {
        $intype = 0;
        $out = '';
        $c = $str[$i];
        $len = strlen($str);
        if ($c == '/' && $str[$i + 1] == '/') {
            //comment
            while ($c != "\n" && $i < $len) {
                $i++;
                $c = $str[$i];
            }
            $i++;
            $c = $str[$i];
            $intype = 7;
        } else {
            if ($c == '$') {
                //is var
                $intype = 1;
                //read to end of var
                do {
                    $out .= $c;
                    $i++;
                    if ($i == $len) {
                        break;
                    }
                    $c = $str[$i];
                } while ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z" || $c >= '0' && $c <= '9' || $c == '_');
                //if [ then array ref - read and connect as part of variable token
                if ($c == '[') {
                    $connecttolast = 1;
                }
                //check if allowed var
                if (in_array($out, $disallowedvar)) {
                    echo sprintf(_('Eeek.. unallowed var %s!'), $out);
                    return array(array('', 9));
                }
            } else {
                if ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z") {
                    //is str
                    $intype = 2;
                    //string like function name
                    do {
                        $out .= $c;
                        $i++;
                        if ($i == $len) {
                            break;
                        }
                        $c = $str[$i];
                    } while ($c >= "a" && $c <= "z" || $c >= "A" && $c <= "Z" || $c >= '0' && $c <= '9' || $c == '_');
                    //check if it's a special word, and set type appropriately if it is
                    if ($out == 'if' || $out == 'where' || $out == 'for') {
                        $intype = 8;
                    } else {
                        if ($out == 'else' || $out == 'elseif') {
                            $intype = 8;
                            if ($out == 'else' && substr($str, $i, 3) == ' if') {
                                $out = 'elseif';
                                $i += 3;
                            }
                            if ($lastsym[1] == 7) {
                                array_pop($syms);
                                $lastsym = $syms[count($syms) - 1];
                            }
                        } else {
                            if ($out == 'e') {
                                $out = "exp(1)";
                                $intype = 3;
                            } else {
                                if ($out == 'pi') {
                                    $out = "(M_PI)";
                                    $intype = 3;
                                } else {
                                    if ($out == 'userid') {
                                        $out = '"userid"';
                                        $intype = 6;
                                    } else {
                                        //eat whitespace
                                        while ($c == ' ') {
                                            $i++;
                                            $c = $str[$i];
                                        }
                                        //could be sin^-1 or sin^(-1) - check for them and rewrite if needed
                                        if ($c == '^' && substr($str, $i + 1, 2) == '-1') {
                                            $i += 3;
                                            $out = 'arc' . $out;
                                            $c = $str[$i];
                                            while ($c == ' ') {
                                                $i++;
                                                $c = $str[$i];
                                            }
                                        } else {
                                            if ($c == '^' && substr($str, $i + 1, 4) == '(-1)') {
                                                $i += 3;
                                                $out = 'arc' . $out;
                                                $c = $str[$i];
                                                while ($c == ' ') {
                                                    $i++;
                                                    $c = $str[$i];
                                                }
                                            }
                                        }
                                        //if there's a ( then it's a function
                                        if ($c == '(' && $out != 'e' && $out != 'pi') {
                                            //rewrite logs
                                            if ($out == 'log') {
                                                $out = 'log10';
                                            } else {
                                                if ($out == 'ln') {
                                                    $out = 'log';
                                                } else {
                                                    //check it's and OK function
                                                    if (!in_array($out, $allowedmacros)) {
                                                        echo sprintf(_('Eeek.. unallowed macro %s'), $out);
                                                        return array(array('', 9));
                                                    }
                                                }
                                            }
                                            //rewrite arctrig into atrig for PHP
                                            $out = str_replace(array("arcsin", "arccos", "arctan", "arcsinh", "arccosh", "arctanh"), array("asin", "acos", "atan", "asinh", "acosh", "atanh"), $out);
                                            //connect upcoming parens to function
                                            $connecttolast = 2;
                                        } else {
                                            //not a function, so what is it?
                                            if ($out == 'true' || $out == 'false' || $out == 'null') {
                                                //we like this - it's an acceptable unquoted string
                                            } else {
                                                //
                                                //an unquoted string!  give a warning to instructor,
                                                //but treat as a quoted string.
                                                if (isset($GLOBALS['teacherid'])) {
                                                    echo sprintf(_('Warning... unquoted string %s.. treating as string'), $out);
                                                }
                                                $out = "'{$out}'";
                                                $intype = 6;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if ($c >= '0' && $c <= '9' || $c == '.' && ($str[$i + 1] >= '0' && $str[$i + 1] <= '9')) {
                        //is num
                        $intype = 3;
                        //number
                        $cont = true;
                        //handle . 3 which needs to act as concat
                        if ($lastsym[0] == '.') {
                            $syms[count($syms) - 1][0] .= ' ';
                        }
                        do {
                            $out .= $c;
                            $lastc = $c;
                            $i++;
                            if ($i == $len) {
                                break;
                            }
                            $c = $str[$i];
                            if ($c >= '0' && $c <= '9' || $c == '.' && $str[$i + 1] != '.' && $lastc != '.') {
                                //is still num
                            } else {
                                if ($c == 'e' || $c == 'E') {
                                    //might be scientific notation:  5e6 or 3e-6
                                    $d = $str[$i + 1];
                                    if ($d >= '0' && $d <= '9') {
                                        $out .= $c;
                                        $i++;
                                        if ($i == $len) {
                                            break;
                                        }
                                        $c = $str[$i];
                                    } else {
                                        if (($d == '-' || $d == '+') && ($str[$i + 2] >= '0' && $str[$i + 2] <= '9')) {
                                            $out .= $c . $d;
                                            $i += 2;
                                            if ($i >= $len) {
                                                break;
                                            }
                                            $c = $str[$i];
                                        } else {
                                            $cont = false;
                                        }
                                    }
                                } else {
                                    $cont = false;
                                }
                            }
                        } while ($cont);
                    } else {
                        if ($c == '(' || $c == '{' || $c == '[') {
                            //parens or curlys
                            if ($c == '(') {
                                $intype = 4;
                                //parens
                                $leftb = '(';
                                $rightb = ')';
                            } else {
                                if ($c == '{') {
                                    $intype = 5;
                                    //curlys
                                    $leftb = '{';
                                    $rightb = '}';
                                } else {
                                    if ($c == '[') {
                                        $intype = 11;
                                        //array index brackets
                                        $leftb = '[';
                                        $rightb = ']';
                                    }
                                }
                            }
                            $thisn = 1;
                            $inq = false;
                            $j = $i + 1;
                            $len = strlen($str);
                            while ($j < $len) {
                                //read terms until we get to right bracket at same nesting level
                                //we have to avoid strings, as they might contain unmatched brackets
                                $d = $str[$j];
                                if ($inq) {
                                    //if inquote, leave if same marker (not escaped)
                                    if ($d == $qtype && $str[$j - 1] != '\\') {
                                        $inq = false;
                                    }
                                } else {
                                    if ($d == '"' || $d == "'") {
                                        $inq = true;
                                        //entering quotes
                                        $qtype = $d;
                                    } else {
                                        if ($d == $leftb) {
                                            $thisn++;
                                            //increase nesting depth
                                        } else {
                                            if ($d == $rightb) {
                                                $thisn--;
                                                //decrease nesting depth
                                                if ($thisn == 0) {
                                                    //read inside of brackets, send recursively to interpreter
                                                    $inside = interpretline(substr($str, $i + 1, $j - $i - 1), $anstype, $countcnt + 1);
                                                    if ($inside == 'error') {
                                                        //was an error, return error token
                                                        return array(array('', 9));
                                                    }
                                                    //if curly, make sure we have a ;, unless preceeded by a $ which
                                                    //would be a variable variable
                                                    if ($rightb == '}' && $lastsym[0] != '$') {
                                                        $out .= $leftb . $inside . ';' . $rightb;
                                                    } else {
                                                        $out .= $leftb . $inside . $rightb;
                                                    }
                                                    $i = $j + 1;
                                                    break;
                                                }
                                            } else {
                                                if ($d == '/' && $str[$j + 1] == '/') {
                                                    //comment inside brackers
                                                    while ($d != "\n" && $j < $len) {
                                                        $j++;
                                                        $d = $str[$j];
                                                    }
                                                } else {
                                                    if ($d == "\n") {
                                                        //echo "unmatched parens/brackets - likely will cause an error";
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                $j++;
                            }
                            if ($j == $len) {
                                $i = $j;
                                echo _('unmatched parens/brackets - likely will cause an error');
                            } else {
                                $c = $str[$i];
                            }
                        } else {
                            if ($c == '"' || $c == "'") {
                                //string
                                $intype = 6;
                                $qtype = $c;
                                do {
                                    $out .= $c;
                                    $i++;
                                    if ($i == $len) {
                                        break;
                                    }
                                    $lastc = $c;
                                    $c = $str[$i];
                                } while (!($c == $qtype && $lastc != '\\'));
                                $out .= $c;
                                $i++;
                                $c = $str[$i];
                            } else {
                                if ($c == "\n") {
                                    //end of line
                                    $intype = 7;
                                    $i++;
                                    if ($i < $len) {
                                        $c = $str[$i];
                                    }
                                } else {
                                    if ($c == ';') {
                                        //end of line
                                        $intype = 7;
                                        $i++;
                                        if ($i < $len) {
                                            $c = $str[$i];
                                        }
                                    } else {
                                        //no type - just append string.  Could be operators
                                        $out .= $c;
                                        $i++;
                                        if ($i < $len) {
                                            $c = $str[$i];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        while ($c == ' ') {
            //eat up extra whitespace
            $i++;
            if ($i == $len) {
                break;
            }
            $c = $str[$i];
            if ($c == '.' && $intype == 3) {
                //if 3 . needs space to act like concat
                $out .= ' ';
            }
        }
        //if parens or array index needs to be connected to func/var, do it
        if ($connecttolast > 0 && $intype != $connecttolast) {
            //if func is loadlibrary, need to do so now so allowedmacros
            //will be expanded before reading the rest of the code
            if ($lastsym[0] == "loadlibrary") {
                loadlibrary(substr($out, 1, strlen($out) - 2));
                array_pop($syms);
                $connecttolast = 0;
            } else {
                if ($lastsym[0] == 'importcodefrom' || $lastsym[0] == 'includecodefrom') {
                    $out = intval(substr($out, 1, strlen($out) - 2));
                    $query = "SELECT control,qtype FROM imas_questionset WHERE id='{$out}'";
                    $result = mysql_query($query) or die("Query failed : " . mysql_error());
                    if (mysql_num_rows($result) == 0) {
                        //was an error, return error token
                        return array(array('', 9));
                    } else {
                        //$inside = interpretline(mysql_result($result,0,0),$anstype);
                        $inside = interpret('control', $anstype, mysql_result($result, 0, 0), $countcnt + 1);
                        if (mysql_result($result, 0, 1) != $anstype) {
                            //echo 'Imported code question type does not match current question answer type';
                        }
                    }
                    if ($inside == 'error') {
                        //was an error, return error token
                        return array(array('', 9));
                    } else {
                        array_pop($syms);
                        $connecttolast = 0;
                        $syms[] = array('{$included=true;' . $inside . ';$included=false;}', 5);
                        //type: curly
                        $syms[] = array('', 7);
                        //end of line;
                        $lastsym = array('', 7);
                    }
                } else {
                    $syms[count($syms) - 1][0] .= $out;
                    $connecttolast = 0;
                    if ($c == '[') {
                        // multidim array ref?
                        $connecttolast = 1;
                    }
                }
            }
        } else {
            //add to symbol list, avoid repeat end-of-lines.
            if ($intype != 7 || $lastsym[1] != 7) {
                $lastsym = array($out, $intype);
                $syms[] = array($out, $intype);
            }
        }
    }
    return $syms;
}
Exemple #2
0
<?php

session_start();
$_DEBUG = true;
//调试模式
include_once 'conn.php';
include_once 'library/basefunction.php';
include_once 'lang/envinit.php';
include_once 'templatefunction/Iron.article.php';
include_once 'templatefunction/Iron.column.php';
include_once 'templatefunction/Iron.label.php';
loadlibrary("library/third/FirePHPCore/fb.php");
FB::log('Log message');
FB::info('Info message');
FB::warn('Warn message');
FB::error('Error message');
readcache();
$siteconfig = getresult("SELECT * FROM I_siteconfig LIMIT 0 , 1");
//如果没有网站配置项
if (getresultNumrows($siteconfig) < 1) {
    die("<script type='text/javascript'>window.location='error.html'</script>");
}
$templateid = getresultData($siteconfig, 0, "indextemplate");
$templateinfo = getresult("select * from I_template where id={$templateid} limit 0,1");
if (getresultNumrows($templateinfo) < 1) {
    die("<script type='text/javascript'>window.location='error.html'</script>");
}
$templatepath = getresultData($templateinfo, 0, "path");
$templatefile = getroot() . "/templates/" . $templatepath;
//echo $templatefile;
if (!file_exists($templatefile)) {