added some development tools
[windows-sources.git] / developer / Samples / Bot / admin / validateAIML.php
blob375712c73ad58e979e8c8fb932e21a08bfe30f3e
1 <?php
2 /***************************************
3 * http://www.program-o.com
4 * PROGRAM O
5 * Version: 2.5.3
6 * FILE: validateAIML.php
7 * AUTHOR: Elizabeth Perreau and Dave Morton
8 * DATE: 06-02-2014
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');
13 chdir(__DIR__);
14 error_reporting(E_ALL);
15 ini_set('display_errors', 0);
16 ini_set('error_log', _LOG_PATH_ . 'validate_aiml.error.log');
17 $status = '';
18 $displayAIML = '';
19 $ip = $_SERVER['REMOTE_ADDR'];
20 $ip = ($ip == '::1') ? 'localhost' : $ip;
21 if (!empty ($_FILES))
23 $uploadDir = _UPLOAD_PATH_;
24 //chdir($uploadDir);
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();
46 else
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);
69 unset($aimlArray[0]);
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();
81 else
83 $status = "File $fileName is valid.<br />\n";
89 /**
90 * Function libxml_display_error
92 * * @param $error
93 * @return string
95 function libxml_display_error($error)
97 global $aimlArray;
98 $errorLine = $error->line;
99 $errorXML = htmlentities(@$aimlArray[$errorLine]);
100 $return = "<hr>\n";
101 switch ($error->level)
103 case LIBXML_ERR_WARNING :
104 $return .= "<b>Warning {$error->code}</b>: ";
105 break;
106 case LIBXML_ERR_ERROR :
107 $return .= "<b>Error {$error->code}</b>: ";
108 break;
109 case LIBXML_ERR_FATAL :
110 $return .= "<b>Fatal Error {$error->code}</b>: ";
111 break;
113 $return .= trim($error->message);
114 if ($error->file)
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";
120 return $return;
123 function libxml_display_errors()
125 global $status;
126 $errors = libxml_get_errors();
127 foreach ($errors as $error)
129 $status .= libxml_display_error($error) . "<br />\n";
131 libxml_clear_errors();
135 <!DOCTYPE html>
136 <html>
137 <head>
138 <title>Program O AIML File Validator</title>
139 <style type="text/css">
140 .center {
141 text-align: center;
143 </style>
144 </head>
145 <body>
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.
153 </p>
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.
158 </p>
160 Please note that many standard HTML tags (e.g. &lt;b&gt;, &lt;u&gt;, &lt;i&gt; 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.
163 </p>
164 <div class="center">
165 <form enctype="multipart/form-data" action="validateAIML.php" method="post">
166 Please choose a file: <input name="uploaded" type="file" tabindex="1" />&nbsp;&nbsp;
167 <input type="submit" value="Validate" /><br>
168 </form>
169 </div>
170 <hr />
171 <?php echo $status ?>
172 <hr />
173 <?php echo $displayAIML ?>
174 </body>
175 </html>