2 /***************************************
3 * http://www.program-o.com
6 * FILE: validateAIML.php
7 * AUTHOR: Elizabeth Perreau and Dave Morton
9 * DETAILS: Validates uploaded AIML files
10 ***************************************/
11 if (!file_exists('../config/global_config.php')) header('location: ../install/install_programo.php');
12 require_once('../config/global_config.php');
14 error_reporting(E_ALL
);
15 ini_set('display_errors', 0);
16 ini_set('error_log', _LOG_PATH_
. 'validate_aiml.error.log');
19 $ip = $_SERVER['REMOTE_ADDR'];
20 $ip = ($ip == '::1') ?
'localhost' : $ip;
23 $uploadDir = _UPLOAD_PATH_
;
25 if (!file_exists("$uploadDir$ip")) mkdir("$uploadDir$ip");
27 $tf = basename($_FILES['uploaded']['name']);
28 $tf = str_replace(' ', '_', $tf);
29 $target = $uploadDir . $ip . '/' . $tf;
30 libxml_use_internal_errors(true);
31 $tmpFile = str_replace('../', '', $_FILES['uploaded']['tmp_name']);
32 if (move_uploaded_file($tmpFile, $target))
34 $aimlFile = trim(file_get_contents($target));
35 //$aimlFile = str_replace('><', ">\n<", $aimlFile);
36 $fileName = basename($_FILES['uploaded']['name']);
37 $xml = new DOMDocument('1.0', 'utf-8');
38 //$xml->preserveWhiteSpace = true;
39 //$xml->formatOutput = true;
41 if (!$xml->loadXML($aimlFile))
43 $status = "File $fileName is <strong>NOT</strong> valid!<br />\n";
44 libxml_display_errors();
48 $xpath = new DOMXPath($xml);
49 foreach ($xpath->query('//comment()') as $comment) {
50 //$comment->parentNode->removeChild($comment);
53 $rootTag = $xml->documentElement
;
54 $rootVersion = $rootTag->getAttribute('%version%');
55 if (empty($rootVersion)) $rootTag->setAttribute('version', '1.0.1');
56 $creator = new DOMImplementation();
57 $docType = $creator->createDocumentType('aiml', '', 'aiml.dtd');
58 $aiml = $creator->createDocument('', '', $docType);
59 $aiml->formatOutput
= true;
60 $aiml->preserveWhiteSpace
= false;
61 $aimlContent = $xml->getElementsByTagName('aiml')->item(0);
63 $newNode = $aiml->importNode($aimlContent, true);
64 $aiml->appendChild($newNode);
65 // make the new AIML file
66 $tmpContent = $aiml->saveXML();
67 $aimlArray = explode("\n", $aimlFile);
68 array_unshift($aimlArray,null);
70 foreach ($aimlArray as $line => $content)
72 $content = trim($content);
73 $displayAIML .= " <div class=\"source\" id=\"line$line\"><pre>$line | " . htmlentities($content) . "</pre></div>\n";
76 if (!$aiml->validate())
78 $status = "File $fileName is <strong>NOT</strong> valid!<br />\n";
79 libxml_display_errors();
83 $status = "File $fileName is valid.<br />\n";
90 * Function libxml_display_error
95 function libxml_display_error($error)
98 $errorLine = $error->line
;
99 $errorXML = htmlentities(@$aimlArray[$errorLine]);
101 switch ($error->level
)
103 case LIBXML_ERR_WARNING
:
104 $return .= "<b>Warning {$error->code}</b>: ";
106 case LIBXML_ERR_ERROR
:
107 $return .= "<b>Error {$error->code}</b>: ";
109 case LIBXML_ERR_FATAL
:
110 $return .= "<b>Fatal Error {$error->code}</b>: ";
113 $return .= trim($error->message
);
116 $return .= " in <b>{$error->file}</b>";
118 $return .= " on line <a href=\"#line$errorLine\">$errorLine</a>, column {$error->column}\n";
119 $return .= "<br>$errorXML\n";
123 function libxml_display_errors()
126 $errors = libxml_get_errors();
127 foreach ($errors as $error)
129 $status .= libxml_display_error($error) . "<br />\n";
131 libxml_clear_errors();
138 <title
>Program O AIML File Validator
</title
>
139 <style type
="text/css">
146 <h2 style
="text-align: center">Program O AIML File Validator
</h2
>
148 This script will check to make sure that your AIML files are not only well formed
, but also
149 pass validation
, based on the AIML
1.0.1 specification
. This is important
, because the
150 Program O upload script in the admin pages requires the uploaded AIML files to be valid AIML
151 in order
for them to be added to your bot
's database. This prevents <strong>some</strong> "bugs"
152 from occurring that are actually problems with improperly created AIML files.
155 Simply upload your AIML file, and the script will examine it. If it passes validation, then you'll
156 get a simple notice on the page telling you so
. If it fails
, you
'll get a detailed list of problems
157 that the validator encountered.
160 Please note that many standard HTML tags (e.g. <b>, <u>, <i> etc.) are not part of the
161 AIML specification, and will therefor fail if encountered. That said, however, Program O will happily
162 accept them, so if your AIML file fails validation for <b>ONLY</b> having HTML tags, you're still ok
.
165 <form enctype
="multipart/form-data" action
="validateAIML.php" method
="post">
166 Please choose a file
: <input name
="uploaded" type
="file" tabindex
="1" /> 
; 
;
167 <input type
="submit" value
="Validate" /><br
>
171 <?php
echo $status ?
>
173 <?php
echo $displayAIML ?
>