Exemplo n.º 1
0
/**
* Handles the actual XML between the <template/> tags.
*
* Recognises the different tags, access the different functions to process each individual tag. Notes by the original developer: <br/>
* Why isn't this a huge switch statement? Because it has to do more comlicated checking than just string comparison to figure out what it should do. <br/>
* How can I organize this better? Good question.
*
* @todo It seems to me that this function could modelled similarly to the custom tag system. Where there is a seperate function for each tag.
*
* @uses getid()
* @uses getfdate()
* @uses getsize()
* @uses upperkeysarray()
* @uses debugger()
* @uses recursechildren()
* @uses respond()
* @uses botget()
* @uses gender()
* @uses getinput()
* @uses bset()
* @uses insertgossip()
* @uses firstthird()
* @uses firstsecond()
* @uses getthat()
* @uses realchild()
*
* @param mixed $xmlnode               Getting either a string or an array from recursechildren() func.
* @param array $inputstar             If a matched pattern includes *'s then what is covere by the * is found here.
* @param array $thatstar              if a used that contains a star, then what is covered by the * is found here.
* @param array $topicstar             if a used topic contains a star, then what is covered by the * is found here.
*
* @return string                      The bot's response.
*/
function handlenode($xmlnode, $inputstar, $thatstar, $topicstar)
{
    if (!is_array($xmlnode)) {
        return $xmlnode;
    } elseif (strtoupper($xmlnode["tag"]) == "ID") {
        return getid();
    } elseif (strtoupper($xmlnode["tag"]) == "DATE") {
        //		return getfdate(); // deprecated
        $mynode = upperkeysarray($xmlnode["attributes"]);
        // Get the value of an attribute
        $date_format = $mynode["FORMAT"];
        return getfdate($date_format);
    } elseif (strtoupper($xmlnode["tag"]) == "VERSION") {
        return PROGRAMEVERSION;
    } elseif (strtoupper($xmlnode["tag"]) == "SIZE") {
        return getsize();
    } elseif (strtoupper($xmlnode["tag"]) == "STAR") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$starindex=$xmlnode["attributes"]["INDEX"];
        if (!(is_array($mynode) && isset($mynode["INDEX"]))) {
            $mynode["INDEX"] = "";
        }
        $starindex = $mynode["INDEX"];
        if ($starindex == "") {
            $starindex = "1";
        }
        debugger("starindex: {$starindex}", 3);
        //print_r($inputstar);
        return $inputstar[$starindex - 1];
    } elseif (strtoupper($xmlnode["tag"]) == "THATSTAR") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$starindex=$xmlnode["attributes"]["INDEX"];
        if (!(is_array($mynode) && isset($mynode["INDEX"]))) {
            $mynode["INDEX"] = "";
        }
        $starindex = $mynode["INDEX"];
        if ($starindex == "") {
            $starindex = "1";
        }
        debugger("starindex: {$starindex}", 3);
        //print_r($inputstar);
        return $thatstar[$starindex - 1];
    } elseif (strtoupper($xmlnode["tag"]) == "TOPICSTAR") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$starindex=$xmlnode["attributes"]["INDEX"];
        if (!(is_array($mynode) && isset($mynode["INDEX"]))) {
            $mynode["INDEX"] = "";
        }
        $starindex = $mynode['INDEX'];
        if ($starindex == "") {
            $starindex = "1";
        }
        debugger("starindex: {$starindex}", 3);
        //print_r($inputstar);
        return $topicstar[$starindex - 1];
    } elseif (strtoupper($xmlnode["tag"]) == "SRAI") {
        // Build up a new response inside of here (using recursechildren function and then call response with it.
        $newresponse = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        debugger("newresponts: {$newresponse}", 3);
        return respond($newresponse);
    } elseif (strtoupper($xmlnode["tag"]) == "SR") {
        return respond($inputstar[0]);
    } elseif (strtoupper($xmlnode["tag"]) == "RANDOM") {
        $liarray = array();
        $children = $xmlnode["children"];
        for ($randomc = 0; $randomc < sizeof($children); $randomc++) {
            if (strtoupper($children[$randomc]["tag"]) == "LI") {
                $liarray[] = $randomc;
            }
        }
        // Pick a random number from 0 to sizeof($liarray)-1
        mt_srand((double) microtime() * 1000000);
        $lirandom = mt_rand(0, sizeof($liarray) - 1);
        return recursechildren(realchild($children[$liarray[$lirandom]]), $inputstar, $thatstar, $topicstar);
    } elseif (strtoupper($xmlnode["tag"]) == "THINK") {
        recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        return "";
    } elseif (strtoupper($xmlnode["tag"]) == "BOT") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$name=$xmlnode["attributes"]["NAME"];
        $name = $mynode["NAME"];
        return botget($name);
    } elseif (strtoupper($xmlnode["tag"]) == "GET") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$name=$xmlnode["attributes"]["NAME"];
        $name = $mynode["NAME"];
        return bget($name);
    } elseif (strtoupper($xmlnode["tag"]) == "SET") {
        //$name=$xmlnode["attributes"]["NAME"];
        $mynode = upperkeysarray($xmlnode["attributes"]);
        $name = $mynode["NAME"];
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        bset($name, $value);
        return $value;
    } elseif (strtoupper($xmlnode["tag"]) == "UPPERCASE") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        return strtoupper($value);
    } elseif (strtoupper($xmlnode["tag"]) == "FORMAL") {
        $nvalue = "";
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        $value = strtolower($value);
        $words = split(" ", $value);
        for ($x = 0; $x < sizeof($words); $x++) {
            if ($x != 0) {
                $nvalue .= " ";
            }
            $nvalue .= ucfirst($words[$x]);
        }
        return $nvalue;
    } elseif (strtoupper($xmlnode["tag"]) == "LOWERCASE") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        return strtolower($value);
    } elseif (strtoupper($xmlnode["tag"]) == "GENDER") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        return gender($value);
    } elseif (strtoupper($xmlnode["tag"]) == "SENTENCE") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        return ucfirst($value);
    } elseif (strtoupper($xmlnode["tag"]) == "INPUT") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$index = $xmlnode["attributes"]["INDEX"];
        if (!(is_array($mynode) && isset($mynode["INDEX"]))) {
            $mynode["INDEX"] = "";
        }
        $index = $mynode["INDEX"];
        if ($index == "") {
            $index = 1;
        }
        $index = $index - 1;
        return getinput($index);
    } elseif (strtoupper($xmlnode["tag"]) == "GOSSIP") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        insertgossip($value);
        return $value;
    } elseif (strtoupper($xmlnode["tag"]) == "PERSON") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        if ($value == "") {
            $value = $inputstar[0];
        }
        return firstthird($value);
    } elseif (strtoupper($xmlnode["tag"]) == "PERSON2") {
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        if ($value == "") {
            $value = $inputstar[0];
        }
        return firstsecond($value);
    } elseif (strtoupper($xmlnode["tag"]) == "THAT") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        //$indexes = $xmlnode["attributes"]["INDEX"];
        if (is_array($mynode) && isset($mynode["INDEX"])) {
            $indexes = $mynode["INDEX"];
        } else {
            $indexes = "";
        }
        $indexes = split(",", $indexes);
        if (sizeof($indexes) < 2) {
            $indexes = array();
            $indexes[] = 1;
            $indexes[] = 1;
        }
        return getthat($indexes[0], $indexes[1]);
    } elseif (strtoupper($xmlnode["tag"]) == "CONDITION") {
        $mynode = upperkeysarray($xmlnode["attributes"]);
        // First do multi condition name=value
        if (is_array($mynode) && isset($mynode["NAME"])) {
            $condname = $mynode["NAME"];
        } else {
            $condname = "";
        }
        if (is_array($mynode) && isset($mynode["VALUE"])) {
            $condvalue = $mynode["VALUE"];
        } else {
            $condvalue = "";
        }
        if (is_array($mynode) && isset($mynode["CONTAINS"])) {
            $condcontains = $mynode["CONTAINS"];
        } else {
            $condcontains = "";
        }
        if (is_array($mynode) && isset($mynode["EXISTS"])) {
            $condexists = $mynode["EXISTS"];
        } else {
            $condexists = "";
        }
        /*
        		$condname=$mynode["NAME"];
        		$condvalue=$mynode["VALUE"];
        		$condcontains=$mynode["CONTAINS"];
        		$condexists=$mynode["EXISTS"];
        */
        // If this is a multi condition
        if ($condname != "" && $condvalue != "") {
            if ($condvalue != "") {
                $condtype = "VALUE";
            } elseif ($condcontains != "") {
                $condtype = "CONTAINS";
            } elseif ($condexists != "") {
                $condtype = "EXISTS";
            }
            if ($condtype == "VALUE") {
                $condvalue = "^" . str_replace("*", "(.*)", $condvalue);
                //if ((bget($condname))==$condvalue){
                #				if (eregi($condvalue,bget($condname))){
                if (stripos(bget($condname), $condvalue) !== false) {
                    return recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
                }
            }
        } elseif ($condname != "" && $condvalue == "") {
            $children = $xmlnode["children"];
            $checkval = bget($condname);
            // After a match break. If no match then execute last if no name or val
            for ($randomc = 0; $randomc < sizeof($children); $randomc++) {
                if (strtoupper($children[$randomc]["tag"]) == "LI") {
                    $mynode = upperkeysarray($children[$randomc]["attributes"]);
                    //$condvalue=$children[$randomc]["attributes"]["VALUE"];
                    if (!(is_array($mynode) && isset($mynode["VALUE"]))) {
                        $mynode["VALUE"] = "";
                    }
                    $condvalue = $mynode["VALUE"];
                    $condvalue = "^" . str_replace("*", "(.*)", $condvalue) . "\$";
                    #					if ((eregi($condvalue,$checkval))||($condvalue=="^\$")){
                    if (preg_match("/" . $condvalue . "/i", $checkval) || $condvalue == "^\$") {
                        return recursechildren(realchild($children[$randomc]), $inputstar, $thatstar, $topicstar);
                    }
                }
            }
        } elseif ($condname == "" && $condvalue == "") {
            $children = $xmlnode["children"];
            // After a match break. If no match then execute last if no name or val
            for ($randomc = 0; $randomc < sizeof($children); $randomc++) {
                if (strtoupper($children[$randomc]["tag"]) == "LI") {
                    $mynode = upperkeysarray($children[$randomc]["attributes"]);
                    if (is_array($mynode) && isset($mynode["NAME"])) {
                        $condname = $mynode["NAME"];
                    } else {
                        $condname = "";
                    }
                    if (is_array($mynode) && isset($mynode["VALUE"])) {
                        $condvalue = $mynode["VALUE"];
                    } else {
                        $condvalue = "";
                    }
                    $condvalue = "^" . str_replace("*", "(.*)", $condvalue) . "\$";
                    #					if ((eregi($condvalue,bget($condname))) || (($condvalue=="^\$")&&($condname==""))){
                    if (preg_match("/" . $condvalue . "/i", bget($condname)) || $condvalue == "^\$" && $condname == "") {
                        return recursechildren(realchild($children[$randomc]), $inputstar, $thatstar, $topicstar);
                    }
                }
            }
        }
    } elseif (strtoupper($xmlnode["tag"]) == "SYSTEM") {
        $command = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        exec($command, $execoutput);
        for ($x = 0; $x < sizeof($execoutput); $x++) {
            $allout = $allout . $execoutput[$x];
        }
        return $allout;
    } elseif (strtoupper($xmlnode["tag"]) == "PHP") {
        $phpcode = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        ob_start();
        eval($phpcode);
        $evaled = ob_get_contents();
        ob_end_clean();
        return $evaled;
    } elseif (strtoupper($xmlnode["tag"]) == "JUSTBEFORETHAT") {
        $indexes = array();
        $indexes[] = 2;
        $indexes[] = 1;
        return getthat($indexes[0], $indexes[1]);
    } elseif (strtoupper($xmlnode["tag"]) == "JUSTTHAT") {
        $index = 2;
        $index = $index - 1;
        return getinput($index);
    } elseif (strtoupper($xmlnode["tag"]) == "BEFORETHAT") {
        $index = 3;
        $index = $index - 1;
        return getinput($index);
    } elseif (strtoupper($xmlnode["tag"]) == "GET_IP") {
        return getid();
    } elseif (strtoupper($xmlnode["tag"]) == "GETNAME") {
        $name = "NAME";
        return bget($name);
    } elseif (strtoupper($xmlnode["tag"]) == "GETSIZE") {
        return getsize();
    } elseif (strtoupper($xmlnode["tag"]) == "GETTOPIC") {
        $name = "TOPIC";
        return bget($name);
    } elseif (strtoupper($xmlnode["tag"]) == "GETVERSION") {
        return PROGRAMEVERSION;
    } elseif (substr(strtoupper($xmlnode["tag"]), 0, 4) == "GET_") {
        $name = substr($xmlnode["tag"], 4);
        return bget($name);
    } elseif (strtoupper($xmlnode["tag"]) == "SETNAME") {
        $name = "NAME";
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        bset($name, $value);
        return $value;
    } elseif (strtoupper($xmlnode["tag"]) == "SETTOPIC") {
        $name = "TOPIC";
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        bset($name, $value);
        return $value;
    } elseif (substr(strtoupper($xmlnode["tag"]), 0, 4) == "SET_") {
        $name = substr($xmlnode["tag"], 4);
        $value = recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar);
        bset($name, $value);
        return $value;
    } elseif (isdeprecated(strtoupper($xmlnode["tag"]), $ttag)) {
        $name = $ttag;
        return botget($name);
    } elseif (iscustomtag(strtoupper($xmlnode["tag"]), $ctfunction)) {
        return $ctfunction($xmlnode, $inputstar, $thatstar, $topicstar);
    } else {
        $name = $xmlnode["tag"];
        $atts = $xmlnode["attributes"];
        $atttext = "";
        if ($atts != NULL) {
            foreach ($atts as $key => $value) {
                $atttext .= " {$key}=\"{$value}\"";
            }
        }
        $value = "<{$name}" . $atttext;
        if (isset($xmlnode["children"]) || strcmp($xmlnode["value"], "") != 0) {
            $value .= ">" . recursechildren(realchild($xmlnode), $inputstar, $thatstar, $topicstar) . "</{$name}>";
        } else {
            $value .= "/>";
        }
        return $value;
    }
}
Exemplo n.º 2
0
/**
* Used by the AIML XML parser
*
* Looks for the start tag used in AIML files for category, template, that, topic.
*
* @uses upperkeysarray()
* @uses getbotvalue()
*
* @global string                  which tag is being processed
* @global string
* @global string
* @global integer                 to indicate if recursion is allowed
* @global string                  the topic at hand in the AIML file
*
* @param resource $parser        SAX XML resource handler
* @param string $name            name of the encountered tag.
* @param array $attrs            contains the additional attribues of a tag, like value, id, find.
*
* @return void
*/
function startElement($parser, $name, $attrs)
{
    global $whaton, $template, $pattern, $recursive, $topic;
    if (strtoupper($name) == "CATEGORY") {
        $whaton = "CATEGORY";
    } elseif (strtoupper($name) == "TEMPLATE") {
        $whaton = "TEMPLATE";
        $template = "";
        $recursive = 0;
    } elseif (strtoupper($name) == "PATTERN") {
        $whaton = "PATTERN";
    } elseif (strtoupper($name) == "THAT" && strtoupper($whaton) != "TEMPLATE") {
        $whaton = "THAT";
    } elseif (strtoupper($name) == "TOPIC") {
        $whaton = "TOPIC";
    }
    if (strtoupper($whaton) == "PATTERN" && strtoupper($name) != "PATTERN") {
        if (strtoupper($name) == "BOT") {
            $attrs = upperkeysarray($attrs);
            $pattern .= getbotvalue($attrs["NAME"]);
        } else {
            $pattern .= "<{$name}";
            while (list($key, $val) = each($attrs)) {
                $pattern .= " {$key}=\"{$val}\" ";
            }
            $pattern .= ">";
        }
    } elseif (strtoupper($whaton) == "TEMPLATE" && strtoupper($name) != "TEMPLATE") {
        $template .= "<{$name}";
        while (list($key, $val) = each($attrs)) {
            $template .= " {$key}=\"{$val}\" ";
        }
        $template .= ">";
    } elseif (strtoupper($whaton) == "TOPIC") {
        $attrs = upperkeysarray($attrs);
        $topic = $attrs["NAME"];
    }
}