Example #1
0
/**
 * Function parseAIML
 *
 * * @param $fn
 * @param      $aimlContent
 * @param bool $from_zip
 * @return string
 */
function parseAIML($fn, $aimlContent, $from_zip = false)
{
    global $dbConn, $post_vars;
    if (empty($aimlContent)) {
        return "File {$fn} was empty!";
    }
    global $dbConn, $debugmode, $bot_id, $charset;
    $fileName = basename($fn);
    $success = false;
    $topic = '';
    #Clear the database of the old entries
    $sql = "DELETE FROM `aiml`  WHERE `filename` = :filename AND bot_id = :bot_id";
    if (isset($post_vars['clearDB'])) {
        $params = array(':filename' => $fileName, ':bot_id' => $bot_id);
        $affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
    }
    $myBot_id = isset($post_vars['bot_id']) ? $post_vars['bot_id'] : $bot_id;
    # Read new file into the XML parser
    $sql = 'insert into `aiml` (`id`, `bot_id`, `aiml`, `pattern`, `thatpattern`, `template`, `topic`, `filename`) values
    (NULL, :bot_id, :aiml, :pattern, :that, :template, :topic, :fileName);';
    # Validate the incoming document
    /*******************************************************/
    /*       Set up for validation from a common DTD       */
    /*       This will involve removing the XML and        */
    /*       AIML tags from the beginning of the file      */
    /*       and replacing them with our own tags          */
    /*******************************************************/
    $validAIMLHeader = '<?xml version="1.0" encoding="[charset]"?>
<!DOCTYPE aiml PUBLIC "-//W3C//DTD Specification Version 1.0//EN" "http://www.program-o.com/xml/aiml.dtd">
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1">';
    $validAIMLHeader = str_replace('[charset]', $charset, $validAIMLHeader);
    $aimlTagStart = stripos($aimlContent, '<aiml', 0);
    $aimlTagEnd = strpos($aimlContent, '>', $aimlTagStart) + 1;
    $aimlFile = $validAIMLHeader . substr($aimlContent, $aimlTagEnd);
    $tmpDir = _UPLOAD_PATH_ . 'tmp' . DIRECTORY_SEPARATOR;
    if (!file_exists($tmpDir)) {
        mkdir($tmpDir, 0755);
    }
    save_file(_UPLOAD_PATH_ . 'tmp/' . $fileName, $aimlFile);
    try {
        libxml_use_internal_errors(true);
        $xml = new DOMDocument();
        $xml->loadXML($aimlFile);
        $aiml = new SimpleXMLElement($xml->saveXML());
        $rowCount = 0;
        $_SESSION['failCount'] = 0;
        $params = array();
        if (!empty($aiml->topic)) {
            foreach ($aiml->topic as $topicXML) {
                # handle any topic tag(s) in the file
                $topicAttributes = $topicXML->attributes();
                $topic = $topicAttributes['name'];
                foreach ($topicXML->category as $category) {
                    $fullCategory = $category->asXML();
                    $pattern = trim($category->pattern);
                    $pattern = str_replace("'", ' ', $pattern);
                    $pattern = IS_MB_ENABLED ? mb_strtoupper($pattern) : strtoupper($pattern);
                    $that = $category->that;
                    $that = IS_MB_ENABLED ? mb_strtoupper($that) : strtoupper($that);
                    $template = $category->template->asXML();
                    $template = str_replace('<template>', '', $template);
                    $template = str_replace('</template>', '', $template);
                    $template = trim($template);
                    # Strip CRLF and LF from category (Windows/mac/*nix)
                    $aiml_add = str_replace(array("\r\n", "\n"), '', $fullCategory);
                    $params[] = array(':bot_id' => $bot_id, ':aiml' => $aiml_add, ':pattern' => $pattern, ':that' => $that, ':template' => $template, ':topic' => $topic, ':fileName' => $fileName);
                }
            }
        }
        if (!empty($aiml->category)) {
            foreach ($aiml->category as $category) {
                $fullCategory = $category->asXML();
                $pattern = trim($category->pattern);
                $pattern = str_replace("'", ' ', $pattern);
                $pattern = IS_MB_ENABLED ? mb_strtoupper($pattern) : strtoupper($pattern);
                $that = $category->that;
                $template = $category->template->asXML();
                //strip out the <template> tags, as they aren't needed
                $template = substr($template, 10);
                $tLen = strlen($template);
                $template = substr($template, 0, $tLen - 11);
                $template = trim($template);
                # Strip CRLF and LF from category (Windows/mac/*nix)
                $aiml_add = str_replace(array("\r\n", "\n"), '', $fullCategory);
                $params[] = array(':bot_id' => $bot_id, ':aiml' => $aiml_add, ':pattern' => $pattern, ':that' => $that, ':template' => $template, ':topic' => '', ':fileName' => $fileName);
            }
        }
        if (!empty($params)) {
            $rowCount = db_write($sql, $params, true, __FILE__, __FUNCTION__, __LINE__);
            $success = $rowCount !== false ? true : false;
        }
        $msg = $from_zip === true ? '' : "Successfully added {$fileName} to the database.<br />\n";
    } catch (Exception $e) {
        //$trace = print_r($e->getTrace(), true);
        //exit($e->getMessage() . ' at line ' . $e->getLine());
        $msg = $e->getMessage() . ' at line ' . $e->getLine() . "<br>\n";
        //trigger_error("Trace:\n$trace");
        //file_put_contents(_LOG_PATH_ . 'error.trace.log', $trace . "\nEnd Trace\n\n", FILE_APPEND);
        $success = false;
        $_SESSION['failCount']++;
        $errMsg = "There was a problem adding file {$fileName} to the database. Please refer to the message below to correct the problem and try again.<br>\n" . $e->getMessage();
        $msg .= upload_libxml_display_errors($errMsg);
    }
    return $msg;
}
Example #2
0
/**
 * Function parseAIML
 *
 * * @param $fn
 * @param      $aimlContent
 * @param bool $from_zip
 * @return string
 */
function parseAIML($fn, $aimlContent, $from_zip = false)
{
    global $dbConn, $post_vars;
    if (empty($aimlContent)) {
        return "File {$fn} was empty!";
    }
    global $dbConn, $debugmode, $bot_id, $charset;
    $fileName = basename($fn);
    $success = false;
    #Clear the database of the old entries
    $sql = "DELETE FROM `aiml`  WHERE `filename` = :filename AND bot_id = :bot_id";
    if (isset($post_vars['clearDB'])) {
        $sth = $dbConn->prepare($sql);
        $sth->bindValue(':filename', $fileName);
        $sth->bindValue(':bot_id', $bot_id);
        $sth->execute();
        $affectedRows = $sth->rowCount();
    }
    $myBot_id = isset($post_vars['bot_id']) ? $post_vars['bot_id'] : $bot_id;
    # Read new file into the XML parser
    $sql_start = 'insert into `aiml` (`id`, `bot_id`, `aiml`, `pattern`, `thatpattern`, `template`, `topic`, `filename`) values' . PHP_EOL;
    $sql = $sql_start;
    $sql_template = "(NULL, {$myBot_id}, '[aiml_add]', '[pattern]', '[that]', '[template]', '[topic]', '{$fileName}'),\n";
    # Validate the incoming document
    /*******************************************************/
    /*       Set up for validation from a common DTD       */
    /*       This will involve removing the XML and        */
    /*       AIML tags from the beginning of the file      */
    /*       and replacing them with our own tags          */
    /*******************************************************/
    $validAIMLHeader = '<?xml version="1.0" encoding="[charset]"?>
<!DOCTYPE aiml PUBLIC "-//W3C//DTD Specification Version 1.0//EN" "http://www.program-o.com/xml/aiml.dtd">
<aiml version="1.0.1" xmlns="http://alicebot.org/2001/AIML-1.0.1">';
    $validAIMLHeader = str_replace('[charset]', $charset, $validAIMLHeader);
    $aimlTagStart = stripos($aimlContent, '<aiml', 0);
    $aimlTagEnd = strpos($aimlContent, '>', $aimlTagStart) + 1;
    $aimlFile = $validAIMLHeader . substr($aimlContent, $aimlTagEnd);
    save_file(_UPLOAD_PATH_ . 'tmp/' . $fileName, $aimlFile);
    try {
        libxml_use_internal_errors(true);
        $xml = new DOMDocument();
        $xml->loadXML($aimlFile);
        $aiml = new SimpleXMLElement($xml->saveXML());
        $rowCount = 0;
        $_SESSION['failCount'] = 0;
        if (!empty($aiml->topic)) {
            $sql = $sql_start;
            foreach ($aiml->topic as $topicXML) {
                # handle any topic tag(s) in the file
                $topicAttributes = $topicXML->attributes();
                $topic = $topicAttributes['name'];
                //$sth->bindValue(':topic', $topic);
                foreach ($topicXML->category as $category) {
                    $fullCategory = $category->asXML();
                    $pattern = trim($category->pattern);
                    $pattern = str_replace("'", ' ', $pattern);
                    $pattern = IS_MB_ENABLED ? mb_strtoupper($pattern) : strtoupper($pattern);
                    $that = $category->that;
                    $template = $category->template->asXML();
                    $template = str_replace('<template>', '', $template);
                    $template = str_replace('</template>', '', $template);
                    $template = str_replace("'", "\\'", $template);
                    $template = str_replace("\\'", "\\'", $template);
                    # Strip CRLF and LF from category (Windows/mac/*nix)
                    $aiml_add = str_replace(array("\r\n", "\n"), '', $fullCategory);
                    $aiml_add = str_replace("'", "\\'", $aiml_add);
                    $aiml_add = str_replace("\\'", "\\'", $aiml_add);
                    $sql_add = str_replace('[aiml_add]', $aiml_add, $sql_template);
                    $sql_add = str_replace('[pattern]', $pattern, $sql_add);
                    $sql_add = str_replace('[that]', $that, $sql_add);
                    $sql_add = str_replace('[template]', $template, $sql_add);
                    $sql_add = str_replace('[topic]', $topic, $sql_add);
                    $sql .= "{$sql_add}";
                    $rowCount++;
                    if ($rowCount >= 100) {
                        $rowCount = 0;
                        $sql = rtrim($sql, ",\n") . ';';
                        save_file(_LOG_PATH_ . 'sql.txt', $sql . PHP_EOL, true);
                        $sth = $dbConn->prepare($sql);
                        $sth->execute();
                        $success = $sth->rowCount() >= 0 ? true : false;
                        $sql = $sql_start;
                    }
                }
            }
        }
        if (!empty($aiml->category)) {
            foreach ($aiml->category as $category) {
                $fullCategory = $category->asXML();
                $pattern = trim($category->pattern);
                $pattern = str_replace("'", ' ', $pattern);
                $pattern = IS_MB_ENABLED ? mb_strtoupper($pattern) : strtoupper($pattern);
                $that = $category->that;
                $template = $category->template->asXML();
                $template = substr($template, 10);
                $tLen = strlen($template);
                $template = substr($template, 0, $tLen - 11);
                $template = str_replace("'", "\\'", $template);
                $template = str_replace("\\'", "\\'", $template);
                # Strip CRLF and LF from category (Windows/mac/*nix)
                $aiml_add = str_replace(array("\r\n", "\n"), '', $fullCategory);
                $aiml_add = str_replace("'", "\\'", $aiml_add);
                $aiml_add = str_replace("\\'", "\\'", $aiml_add);
                $sql_add = str_replace('[aiml_add]', $aiml_add, $sql_template);
                $sql_add = str_replace('[pattern]', $pattern, $sql_add);
                $sql_add = str_replace('[that]', $that, $sql_add);
                $sql_add = str_replace('[template]', $template, $sql_add);
                $sql_add = str_replace('[topic]', '', $sql_add);
                $sql .= "{$sql_add}";
                $rowCount++;
                if ($rowCount >= 100) {
                    $rowCount = 0;
                    $sql = rtrim($sql, ",\n") . ';';
                    //save_file(_LOG_PATH_ . 'sql1.txt', $sql . PHP_EOL);
                    $sth = $dbConn->prepare($sql);
                    $sth->execute();
                    $success = $sth->rowCount() >= 0 ? true : false;
                    $sql = $sql_start;
                }
            }
            if ($sql != $sql_start) {
                $sql = rtrim($sql, ",\n") . ';';
                //save_file(_LOG_PATH_ . 'sql2.txt', $sql . PHP_EOL, true);
                $sth = $dbConn->prepare($sql);
                $sth->execute();
                $success = $sth->rowCount() >= 0 ? true : false;
            }
        }
        if ($sql != $sql_start) {
            $sql = rtrim($sql, ",\n") . ';';
            //save_file(_LOG_PATH_ . 'sql3.txt', $sql . PHP_EOL, true);
            $sth = $dbConn->query($sql);
            $success = $sth->rowCount() >= 0 ? true : false;
        }
        $msg = $from_zip === true ? '' : "Successfully added {$fileName} to the database.<br />\n";
    } catch (Exception $e) {
        //$trace = print_r($e->getTrace(), true);
        //exit($e->getMessage() . ' at line ' . $e->getLine());
        $msg = $e->getMessage() . ' at line ' . $e->getLine() . "<br>\n";
        //trigger_error("Trace:\n$trace");
        //file_put_contents(_LOG_PATH_ . 'error.trace.log', $trace . "\nEnd Trace\n\n", FILE_APPEND);
        $success = false;
        $_SESSION['failCount']++;
        $errMsg = "There was a problem adding file {$fileName} to the database. Please refer to the message below to correct the problem and try again.<br>\n" . $e->getMessage();
        $msg .= upload_libxml_display_errors($errMsg);
    }
    return $msg;
}