Source for file respond.php

Documentation is available at respond.php

  1. <?php
  2.  
  3. /*
  4. Program E
  5. Copyright 2002, Paul Rydell
  6. This file is part of Program E.
  7. Program E is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. Program E is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Program E; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21.  
  22. /**
  23. * Respond functions
  24. *
  25. * Second layer functions that are prior to entering the AIML match routine.
  26. * @author Paul Rydell
  27. * @copyright 2002
  28. * @version 0.0.8
  29. * @package Interpreter
  30. */
  31.  
  32. /**
  33. * The general preferences and database details.
  34. */
  35. require_once "admin/dbprefs.php";
  36.  
  37. /**
  38. * The matching engine functions of the AIML interpreter.
  39. */
  40. require_once "graphnew.php";
  41.  
  42. /**
  43. * A collection of generally useful utility functions
  44. */
  45. require_once "util.php";
  46.  
  47. /**
  48. * The file containing the function that process custom, non AIML 1.0.x specified, tags.
  49. */
  50. require_once "plugins/customtags.php";
  51.  
  52.  
  53.  
  54. /**
  55. * Start function for retrieving bot reply
  56. *
  57. * Checks to see if bot exists, if so calls reply() to get the repons to the user's input.
  58. *
  59. * @uses lookupbotid()
  60. * @uses reply()
  61. *
  62. * @param string $userinput The user's input
  63. * @param integer $uniqueid The user's session ID
  64. * @param string $botname The bot's name, if no name selected the default value is "TestBot".
  65. *
  66. * @return string The bot's reply.
  67. */
  68. function replybotname($userinput,$uniqueid,$botname = "TestBot"){
  69.  
  70. $botid = lookupbotid($botname);
  71.  
  72. if ($botid==-1){
  73. print "I don't know that bot: $botname<BR>\n";
  74. }
  75. else {
  76. return reply($userinput,$uniqueid,$botid);
  77. }
  78.  
  79. }
  80.  
  81.  
  82. /**
  83. * Main container function in creating the bot's reply.
  84. *
  85. * This function is the 'manager' of all the sub-funtions that do the real processing. It creates a class
  86. * called Response that is used throughout the application.
  87. *
  88. * @uses addinputs()
  89. * @uses addthats()
  90. * @uses bget()
  91. * @uses cleanup()
  92. * @uses getthat()
  93. * @uses loadcustomtags()
  94. * @uses logconversation()
  95. * @uses normalsentences()
  96. * @uses respond()
  97. * @uses ss_timing_current()
  98. * @uses ss_timing_start()
  99. * @uses ss_timing_stop()
  100. *
  101. * @global string that The conversation's previous bot output
  102. * @global string topic The contents of the AIML tag 'Topic'
  103. * @global integer uid The session ID of the user (previously $uniqueid)
  104. * @global integer loopcounter Counts the number of time a particular category is used in the same match trace.
  105. * @global array patternmatched The pattern's that matched the
  106. *
  107. * @param string $userinput The user's input.
  108. * @param integer $uniqueid The user's session ID.
  109. * @param integer $bot The bot's ID.
  110. *
  111. * @return object A class link to 'Response'.
  112. */
  113. function reply($userinput,$uniqueid, $bot = 1){
  114.  
  115. global $that,$topic,$uid,$loopcounter,$patternmatched,$inputmatched,$selectbot;
  116.  
  117. cleanup();
  118.  
  119. ss_timing_start("all");
  120.  
  121. $patternmatched=array();
  122. $inputmatched=array();
  123.  
  124. $myresponse = new Response;
  125.  
  126. $myresponse->errors="";
  127. $uid=$uniqueid;
  128. $selectbot=$bot;
  129.  
  130. // Load the custom plugin tags
  131. loadcustomtags();
  132.  
  133. // Get the "that" and the "topic"
  134. $that=getthat(1,1);
  135. $topic=bget("TOPIC");
  136.  
  137. // Normalize the input
  138. $allinputs=normalsentences($userinput);
  139.  
  140. // If nothing said then use INACTIVITY special input
  141. if (sizeof($allinputs)==0){
  142. $allinputs[]="INACTIVITY";
  143. }
  144.  
  145. // Put all the inputs into the <input> stack.
  146. addinputs($allinputs);
  147.  
  148. $finalanswer="";
  149. // Build our response to all of the inputs.
  150. for ($x=0;$x<sizeof($allinputs);$x++){
  151. $finalanswer.=respond($allinputs[$x]);
  152. }
  153.  
  154. if (($loopcounter>LOOPINGLIMIT)&&(LOOPINGLIMIT!=-1)){
  155. $finalanswer=LOOPINGERRORMSG;
  156. $myresponse->errors="LOOPINGLIMIT";
  157. }
  158.  
  159. // Put the final answers into the <that> stack.
  160. addthats(normalsentences($finalanswer));
  161.  
  162. // Log the conversation
  163. logconversation($userinput,$finalanswer);
  164.  
  165.  
  166. $myresponse->response=$finalanswer;
  167. $myresponse->patternsmatched=$patternmatched;
  168. $myresponse->inputs=$inputmatched;
  169.  
  170. ss_timing_stop("all");
  171.  
  172. $myresponse->timer=ss_timing_current("all");
  173.  
  174. return $myresponse;
  175.  
  176. }
  177. /**
  178. * This is the second level response function.
  179. *
  180. * After reply() this function is the second level function to get the answer to the user's input.
  181. *
  182. * @uses bget()
  183. * @uses debugger()
  184. * @uses gettemplate()
  185. * @uses GetXMLTree()
  186. * @uses recursechildren()
  187. *
  188. * @global string
  189. * @global integer
  190. * @global array
  191. * @global array
  192. *
  193. * @param string $sentence The sentence to be matched.
  194. *
  195. * #return string The response to the user's input.
  196. */
  197. function respond($sentence){
  198. global $that,$loopcounter,$patternmatched,$inputmatched;
  199.  
  200. $topic = bget("topic");
  201.  
  202. $loopcounter++;
  203. if (($loopcounter>LOOPINGLIMIT)&&(LOOPINGLIMIT != -1)){
  204. return "";
  205. }
  206.  
  207. $inputstarvals=array();
  208. $thatstarvals=array();
  209. $topicstarvals=array();
  210. debugger("respond called with sentence: $sentence",3);
  211. flush();
  212.  
  213. if ($that==""){
  214. $that="<nothing>";
  215. }
  216. if ($topic==""){
  217. $topic="<nothing>";
  218. }
  219.  
  220. if ($sentence==""){
  221. return "";
  222. }
  223. else{
  224.  
  225. //If we found a template
  226. $template=gettemplate($sentence,$that,$topic,$inputstarvals,$thatstarvals,$topicstarvals,$s_patternmatched,$s_inputmatched);
  227.  
  228. $patternmatched[]=$s_patternmatched;
  229. $inputmatched[]=$s_inputmatched;
  230. if ($template!=""){
  231.  
  232. $template="<xml><TEMPLATE>" . $template . "</TEMPLATE></xml>";
  233. debugger ("found template: $template",2);
  234.  
  235. $root=GetXMLTree($template);
  236.  
  237.  
  238. if (!isset($root[0]['children'][0]['value'])){
  239. $root=$root[0]['children'][0]['children'];
  240. }
  241. else {
  242. $root=$root[0]['children'][0]['value'];
  243. }
  244.  
  245. /*
  246. if ($root[0]['children'][0]['value']==""){
  247. $root=$root[0]['children'][0]['children'];
  248. }
  249. else {
  250. $root=$root[0]['children'][0]['value'];
  251. }
  252. */
  253.  
  254. $myresponse=recursechildren($root,$inputstarvals,$thatstarvals,$topicstarvals);
  255. debugger("recursechildren ret: $myresponse",3);
  256.  
  257. return $myresponse;
  258.  
  259. }
  260. }
  261. }
  262.  
  263.  
  264. /**
  265. * Third level response processing
  266. *
  267. * This function is the 'manager' function of the template processing.
  268. *
  269. * @uses handlenode()
  270. *
  271. * @param mixed $xmlnode Getting either a string or an array from respond() func.
  272. * @param array $inputstar If a matched pattern includes *'s then what is covere by the * is found here.
  273. * @param array $thatstar if a used that contains a star, then what is covered by the * is found here.
  274. * @param array $topicstar if a used topic contains a star, then what is covered by the * is found here.
  275. *
  276. * @return string The bot's response.
  277. */
  278. function recursechildren($xmlnode,$inputstar,$thatstar,$topicstar){
  279.  
  280. // Getting either a string or an array from respond() func.
  281. $response="";
  282.  
  283. if (is_array($xmlnode)){
  284.  
  285.  
  286. // if ($xmlnode['value']==""){
  287. if (!isset($xmlnode['value'])){
  288. for ($x=0;$x<sizeof($xmlnode);$x++){
  289.  
  290. $response .= handlenode($xmlnode[$x],$inputstar,$thatstar,$topicstar);
  291.  
  292. }
  293.  
  294. }
  295. else {
  296. $response .= handlenode($xmlnode['value'],$inputstar,$thatstar,$topicstar);
  297. }
  298. }
  299. else {
  300. $response .= handlenode($xmlnode,$inputstar,$thatstar,$topicstar);
  301. }
  302.  
  303.  
  304. return $response;
  305.  
  306. }
  307.  
  308.  
  309. /**
  310. * Get the real XML child
  311. *
  312. * Get the real XML child which is used for processing AIML tags that may contain other AIML tags, such as SRAI, CONDITION etc.
  313. *
  314. *
  315. * @param array $xmlnode
  316. *
  317. * @return mixed
  318. */
  319. function realchild($xmlnode){
  320.  
  321. if (!isset($xmlnode["value"])){
  322.  
  323. if (isset($xmlnode["children"])){
  324. return($xmlnode["children"]);
  325. }
  326. else {
  327. return "";
  328. }
  329.  
  330. }
  331. else {
  332. return($xmlnode["value"]);
  333. }
  334.  
  335. /*
  336. if ($xmlnode["value"]==""){
  337. return($xmlnode["children"]);
  338. }
  339. else {
  340. return($xmlnode["value"]);
  341. }
  342. */
  343.  
  344. }
  345.  
  346. /**
  347. * Handles the actual XML between the <template/> tags.
  348. *
  349. * Recognises the different tags, access the different functions to process each individual tag. Notes by the original developer: <br/>
  350. * 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/>
  351. * How can I organize this better? Good question.
  352. *
  353. * @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.
  354. *
  355. * @uses getid()
  356. * @uses getfdate()
  357. * @uses getsize()
  358. * @uses upperkeysarray()
  359. * @uses debugger()
  360. * @uses recursechildren()
  361. * @uses respond()
  362. * @uses botget()
  363. * @uses gender()
  364. * @uses getinput()
  365. * @uses bset()
  366. * @uses insertgossip()
  367. * @uses firstthird()
  368. * @uses firstsecond()
  369. * @uses getthat()
  370. * @uses realchild()
  371. *
  372. * @param mixed $xmlnode Getting either a string or an array from recursechildren() func.
  373. * @param array $inputstar If a matched pattern includes *'s then what is covere by the * is found here.
  374. * @param array $thatstar if a used that contains a star, then what is covered by the * is found here.
  375. * @param array $topicstar if a used topic contains a star, then what is covered by the * is found here.
  376. *
  377. * @return string The bot's response.
  378. */
  379.  
  380. function handlenode($xmlnode,$inputstar,$thatstar,$topicstar){
  381.  
  382. if (!is_array($xmlnode)){
  383. return $xmlnode;
  384. }
  385. elseif (strtoupper($xmlnode["tag"])=="ID"){
  386. return getid();
  387.  
  388. }
  389. elseif (strtoupper($xmlnode["tag"])=="DATE"){
  390.  
  391. return getfdate();
  392.  
  393. }
  394. elseif (strtoupper($xmlnode["tag"])=="VERSION"){
  395.  
  396. return PROGRAMEVERSION;
  397.  
  398. }
  399. elseif (strtoupper($xmlnode["tag"])=="SIZE"){
  400.  
  401. return getsize();
  402.  
  403. }
  404. elseif (strtoupper($xmlnode["tag"])=="STAR"){
  405.  
  406. $mynode=upperkeysarray($xmlnode["attributes"]);
  407.  
  408. //$starindex=$xmlnode["attributes"]["INDEX"];
  409.  
  410.  
  411. if (!((is_array($mynode))&&(isset($mynode["INDEX"])))){
  412. $mynode["INDEX"]="";
  413. }
  414.  
  415. $starindex=$mynode["INDEX"];
  416. if ($starindex==""){
  417. $starindex="1";
  418. }
  419. debugger("starindex: $starindex",3);
  420. //print_r($inputstar);
  421. return $inputstar[$starindex-1];
  422.  
  423. }
  424. elseif (strtoupper($xmlnode["tag"])=="THATSTAR"){
  425.  
  426. $mynode=upperkeysarray($xmlnode["attributes"]);
  427.  
  428. //$starindex=$xmlnode["attributes"]["INDEX"];
  429. if (!((is_array($mynode))&&(isset($mynode["INDEX"])))){
  430. $mynode["INDEX"]="";
  431. }
  432. $starindex=$mynode["INDEX"];
  433. if ($starindex==""){
  434. $starindex="1";
  435. }
  436. debugger("starindex: $starindex",3);
  437. //print_r($inputstar);
  438. return $thatstar[$starindex-1];
  439.  
  440. }
  441. elseif (strtoupper($xmlnode["tag"])=="TOPICSTAR"){
  442.  
  443. $mynode=upperkeysarray($xmlnode["attributes"]);
  444.  
  445. //$starindex=$xmlnode["attributes"]["INDEX"];
  446.  
  447. if (!((is_array($mynode))&&(isset($mynode["INDEX"])))){
  448. $mynode["INDEX"]="";
  449. }
  450.  
  451. $starindex=$mynode['INDEX'];
  452. if ($starindex==""){
  453. $starindex="1";
  454. }
  455. debugger("starindex: $starindex",3);
  456. //print_r($inputstar);
  457. return $topicstar[$starindex-1];
  458.  
  459. }
  460. elseif (strtoupper($xmlnode["tag"])=="SRAI"){
  461. // Build up a new response inside of here (using recursechildren function and then call response with it.
  462. $newresponse=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  463.  
  464. debugger("newresponts: $newresponse",3);
  465. return respond($newresponse);
  466.  
  467. }
  468. elseif (strtoupper($xmlnode["tag"])=="SR"){
  469.  
  470. return respond($inputstar[0]);
  471.  
  472. }
  473. elseif (strtoupper($xmlnode["tag"])=="RANDOM"){
  474.  
  475. $liarray=array();
  476.  
  477. $children = $xmlnode["children"];
  478.  
  479. for ($randomc=0;$randomc<sizeof($children);$randomc++){
  480. if (strtoupper($children[$randomc]["tag"]) == "LI"){
  481. $liarray[]=$randomc;
  482. }
  483. }
  484. // Pick a random number from 0 to sizeof($liarray)-1
  485. mt_srand ((float) microtime() * 1000000);
  486. $lirandom= mt_rand(0,(sizeof($liarray)-1));
  487.  
  488.  
  489. return recursechildren(realchild($children[$liarray[$lirandom]]),$inputstar,$thatstar,$topicstar);
  490.  
  491. }
  492. elseif (strtoupper($xmlnode["tag"])=="THINK"){
  493.  
  494. recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  495. return "";
  496.  
  497. }
  498. elseif (strtoupper($xmlnode["tag"])=="BOT"){
  499.  
  500. $mynode=upperkeysarray($xmlnode["attributes"]);
  501.  
  502. //$name=$xmlnode["attributes"]["NAME"];
  503.  
  504. $name=$mynode["NAME"];
  505.  
  506. return botget($name);
  507.  
  508. }
  509. elseif (strtoupper($xmlnode["tag"])=="GET"){
  510.  
  511. $mynode=upperkeysarray($xmlnode["attributes"]);
  512. //$name=$xmlnode["attributes"]["NAME"];
  513.  
  514. $name=$mynode["NAME"];
  515.  
  516. return bget($name);
  517.  
  518. }
  519. elseif (strtoupper($xmlnode["tag"])=="SET"){
  520.  
  521. //$name=$xmlnode["attributes"]["NAME"];
  522. $mynode=upperkeysarray($xmlnode["attributes"]);
  523. $name=$mynode["NAME"];
  524.  
  525. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  526.  
  527. bset($name,$value);
  528.  
  529. return $value;
  530.  
  531. }
  532. elseif (strtoupper($xmlnode["tag"])=="UPPERCASE"){
  533.  
  534. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  535.  
  536. return strtoupper($value);
  537.  
  538. }
  539. elseif (strtoupper($xmlnode["tag"])=="FORMAL"){
  540.  
  541. $nvalue="";
  542.  
  543. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  544.  
  545. $value=strtolower($value);
  546.  
  547. $words=split(" ",$value);
  548. for ($x=0;$x<sizeof($words);$x++){
  549. if ($x!=0){
  550. $nvalue.=" ";
  551. }
  552. $nvalue .= ucfirst($words[$x]);
  553. }
  554.  
  555. return $nvalue;
  556.  
  557. }
  558. elseif (strtoupper($xmlnode["tag"])=="LOWERCASE"){
  559.  
  560. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  561.  
  562. return strtolower($value);
  563.  
  564. }
  565. elseif (strtoupper($xmlnode["tag"])=="GENDER"){
  566.  
  567. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  568.  
  569. return gender($value);
  570.  
  571. }
  572. elseif (strtoupper($xmlnode["tag"])=="SENTENCE"){
  573.  
  574. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  575.  
  576. return ucfirst($value);
  577.  
  578. }
  579.  
  580. elseif (strtoupper($xmlnode["tag"])=="INPUT"){
  581.  
  582. $mynode=upperkeysarray($xmlnode["attributes"]);
  583. //$index = $xmlnode["attributes"]["INDEX"];
  584.  
  585. if (!((is_array($mynode))&&(isset($mynode["INDEX"])))){
  586. $mynode["INDEX"]="";
  587. }
  588. $index = $mynode["INDEX"];
  589.  
  590.  
  591. if ($index==""){
  592. $index=1;
  593. }
  594.  
  595. $index=$index-1;
  596.  
  597. return getinput($index);
  598.  
  599. }
  600. elseif (strtoupper($xmlnode["tag"])=="GOSSIP"){
  601.  
  602. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  603.  
  604. insertgossip($value);
  605.  
  606. return $value;
  607.  
  608. }
  609. elseif (strtoupper($xmlnode["tag"])=="PERSON"){
  610.  
  611. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  612.  
  613. if ($value==""){
  614. $value=$inputstar[0];
  615. }
  616.  
  617. return firstthird($value);
  618.  
  619. }
  620. elseif (strtoupper($xmlnode["tag"])=="PERSON2"){
  621.  
  622. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  623.  
  624. if ($value==""){
  625. $value=$inputstar[0];
  626. }
  627.  
  628. return firstsecond($value);
  629.  
  630. }
  631. elseif (strtoupper($xmlnode["tag"])=="THAT"){
  632.  
  633. $mynode=upperkeysarray($xmlnode["attributes"]);
  634. //$indexes = $xmlnode["attributes"]["INDEX"];
  635.  
  636. if ((is_array($mynode))&&(isset($mynode["INDEX"]))){
  637. $indexes = $mynode["INDEX"];
  638. }
  639. else {
  640. $indexes="";
  641. }
  642.  
  643. $indexes = split (",", $indexes);
  644.  
  645. if (sizeof($indexes)<2){
  646. $indexes=array();
  647. $indexes[]=1;
  648. $indexes[]=1;
  649. }
  650.  
  651. return getthat($indexes[0],$indexes[1]);
  652.  
  653. }
  654. elseif (strtoupper($xmlnode["tag"])=="CONDITION"){
  655. $mynode=upperkeysarray($xmlnode["attributes"]);
  656.  
  657. // First do multi condition name=value
  658. if ((is_array($mynode))&&(isset($mynode["NAME"]))){
  659. $condname=$mynode["NAME"];
  660. }
  661. else {
  662. $condname = "";
  663. }
  664. if ((is_array($mynode))&&(isset($mynode["VALUE"]))){
  665. $condvalue=$mynode["VALUE"];
  666. }
  667. else {
  668. $condvalue = "";
  669. }
  670. if ((is_array($mynode))&&(isset($mynode["CONTAINS"]))){
  671. $condcontains=$mynode["CONTAINS"];
  672. }
  673. else {
  674. $condcontains = "";
  675. }
  676. if ((is_array($mynode))&&(isset($mynode["EXISTS"]))){
  677. $condexists=$mynode["EXISTS"];
  678. }
  679. else {
  680. $condexists = "";
  681. }
  682. /*
  683. $condname=$mynode["NAME"];
  684. $condvalue=$mynode["VALUE"];
  685. $condcontains=$mynode["CONTAINS"];
  686. $condexists=$mynode["EXISTS"];
  687. */
  688. // If this is a multi condition
  689. if (($condname!="")&&($condvalue!="")){
  690.  
  691. if ($condvalue!=""){
  692. $condtype="VALUE";
  693. }
  694. elseif ($condcontains!=""){
  695. $condtype="CONTAINS";
  696. }
  697. elseif ($condexists!=""){
  698. $condtype="EXISTS";
  699. }
  700.  
  701. if ($condtype=="VALUE"){
  702.  
  703. $condvalue="^" . str_replace("*","(.*)",$condvalue);
  704. if (eregi($condvalue,bget($condname))){
  705. //if ((bget($condname))==$condvalue){
  706.  
  707. return recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  708. }
  709.  
  710. }
  711.  
  712.  
  713. }
  714. // Else condname not blank and value is blank then it goes to <li>'s that have conditions in them.
  715. elseif (($condname!="")&&($condvalue=="")){
  716.  
  717. $children = $xmlnode["children"];
  718.  
  719. $checkval=bget($condname);
  720.  
  721.  
  722. // After a match break. If no match then execute last if no name or val
  723. for ($randomc=0;$randomc<sizeof($children);$randomc++){
  724.  
  725. if (strtoupper($children[$randomc]["tag"]) == "LI"){
  726.  
  727. $mynode=upperkeysarray($children[$randomc]["attributes"]);
  728.  
  729. //$condvalue=$children[$randomc]["attributes"]["VALUE"];
  730. if (!((is_array($mynode))&&(isset($mynode["VALUE"])))){
  731. $mynode["VALUE"]="";
  732. }
  733. $condvalue=$mynode["VALUE"];
  734.  
  735.  
  736. $condvalue="^" . str_replace("*","(.*)",$condvalue) . "$";
  737.  
  738. if ((eregi($condvalue,$checkval))||($condvalue=="^\$")){
  739.  
  740. return recursechildren(realchild($children[$randomc]),$inputstar,$thatstar,$topicstar);
  741. }
  742.  
  743. }
  744. }
  745.  
  746. }
  747. // Else condname and value both blank then the <li>'s inside have both
  748. elseif (($condname=="")&&($condvalue=="")){
  749.  
  750.  
  751.  
  752. $children = $xmlnode["children"];
  753.  
  754. // After a match break. If no match then execute last if no name or val
  755. for ($randomc=0;$randomc<sizeof($children);$randomc++){
  756. if (strtoupper($children[$randomc]["tag"]) == "LI"){
  757.  
  758. $mynode=upperkeysarray($children[$randomc]["attributes"]);
  759.  
  760. if ((is_array($mynode))&&(isset($mynode["NAME"]))){
  761. $condname=$mynode["NAME"];
  762. }
  763. else {
  764. $condname = "";
  765. }
  766. if ((is_array($mynode))&&(isset($mynode["VALUE"]))){
  767. $condvalue=$mynode["VALUE"];
  768. }
  769. else {
  770. $condvalue = "";
  771. }
  772.  
  773.  
  774. $condvalue="^" . str_replace("*","(.*)",$condvalue) . "$";
  775.  
  776.  
  777. if ((eregi($condvalue,bget($condname)))||(($condvalue=="^\$")&&($condname==""))){
  778.  
  779. return recursechildren(realchild($children[$randomc]),$inputstar,$thatstar,$topicstar);
  780. }
  781.  
  782.  
  783. }
  784. }
  785.  
  786.  
  787. }
  788.  
  789. }
  790. elseif (strtoupper($xmlnode["tag"])=="SYSTEM"){
  791.  
  792. $command=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  793.  
  794. exec($command,$execoutput);
  795.  
  796. for ($x=0;$x<sizeof($execoutput);$x++){
  797. $allout=$allout . $execoutput[$x];
  798. }
  799.  
  800. return $allout;
  801.  
  802. }
  803. // For Javascript to work you need:
  804. // 1. Java.
  805. // 2. PHP compiled with Java support.
  806. // 3. js.jar in your php/java directory.
  807. // (you can get js.jar from the Program D distribution - http://www.alicebot.org)
  808. // 4. php.ini's java.class.path to point to js.jar.
  809. // A much easier alternative is to write PHP code and embed it in <php></php> tags.
  810. /*
  811. elseif (strtoupper($xmlnode["tag"])=="JAVASCRIPT"){
  812.  
  813. $jscode=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  814.  
  815. $context = new Java("org.mozilla.javascript.Context");
  816. $newContext = $context->enter();
  817. $scope = $newContext->initStandardObjects(null);
  818. $script = $jscode;
  819. $evaluate = $newContext->evaluateString($scope, $script, "<cmd>", 1, null);
  820. $context->exit();
  821. return $newContext->toString($evaluate);
  822.  
  823. }
  824. */
  825. elseif (strtoupper($xmlnode["tag"])=="PHP"){
  826.  
  827. $phpcode=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  828.  
  829. ob_start();
  830. eval($phpcode);
  831. $evaled = ob_get_contents();
  832. ob_end_clean();
  833.  
  834. return $evaled;
  835.  
  836. }
  837. elseif (strtoupper($xmlnode["tag"])=="JUSTBEFORETHAT"){
  838.  
  839. $indexes=array();
  840. $indexes[]=2;
  841. $indexes[]=1;
  842.  
  843. return getthat($indexes[0],$indexes[1]);
  844.  
  845. }
  846. elseif (strtoupper($xmlnode["tag"])=="JUSTTHAT"){
  847.  
  848. $index=2;
  849.  
  850. $index=$index-1;
  851.  
  852. return getinput($index);
  853. }
  854. elseif (strtoupper($xmlnode["tag"])=="BEFORETHAT"){
  855.  
  856. $index=3;
  857.  
  858. $index=$index-1;
  859.  
  860. return getinput($index);
  861. }
  862. elseif (strtoupper($xmlnode["tag"])=="GET_IP"){
  863.  
  864. return getid();
  865. }
  866. elseif (strtoupper($xmlnode["tag"])=="GETNAME"){
  867.  
  868. $name="NAME";
  869.  
  870. return bget($name);
  871. }
  872. elseif (strtoupper($xmlnode["tag"])=="GETSIZE"){
  873.  
  874. return getsize();
  875. }
  876. elseif (strtoupper($xmlnode["tag"])=="GETTOPIC"){
  877.  
  878. $name="TOPIC";
  879.  
  880. return bget($name);
  881. }
  882. elseif (strtoupper($xmlnode["tag"])=="GETVERSION"){
  883.  
  884. return PROGRAMEVERSION;
  885. }
  886. elseif (substr(strtoupper($xmlnode["tag"]),0,4)=="GET_"){
  887.  
  888. $name=substr($xmlnode["tag"],4);
  889.  
  890. return bget($name);
  891.  
  892. }
  893. elseif (strtoupper($xmlnode["tag"])=="SETNAME"){
  894.  
  895. $name="NAME";
  896.  
  897. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  898.  
  899. bset($name,$value);
  900.  
  901. return $value;
  902. }
  903. elseif (strtoupper($xmlnode["tag"])=="SETTOPIC"){
  904.  
  905. $name="TOPIC";
  906.  
  907. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  908.  
  909. bset($name,$value);
  910.  
  911. return $value;
  912. }
  913. elseif (substr(strtoupper($xmlnode["tag"]),0,4)=="SET_"){
  914.  
  915. $name=substr($xmlnode["tag"],4);
  916. $value=recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar);
  917. bset($name,$value);
  918.  
  919. return $value;
  920.  
  921. }
  922. elseif (isdeprecated(strtoupper($xmlnode["tag"]),$ttag)){
  923.  
  924. $name=$ttag;
  925.  
  926. return botget($name);
  927.  
  928. }
  929. elseif (iscustomtag(strtoupper($xmlnode["tag"]),$ctfunction)){
  930. return $ctfunction($xmlnode,$inputstar,$thatstar,$topicstar);
  931.  
  932. }
  933. // Else we do not know how to handle this. Assume it is HTML and just output it.
  934. // This code fixed by Stefan Humnig
  935. else {
  936.  
  937. $name = $xmlnode["tag"];
  938.  
  939. $atts=$xmlnode["attributes"];
  940.  
  941. $atttext="";
  942.  
  943. if ($atts != NULL)
  944. {
  945. foreach ($atts as $key => $value)
  946. {
  947. $atttext .= " $key=\"$value\"";
  948. }
  949. }
  950. $value="<$name" . $atttext;
  951. if (isset($xmlnode["children"]) || strcmp($xmlnode["value"], "") != 0) {
  952. $value .= ">" . recursechildren(realchild($xmlnode),$inputstar,$thatstar,$topicstar) . "</$name>";
  953. }
  954. else {
  955. $value .= "/>";
  956. }
  957.  
  958. return $value;
  959.  
  960. }
  961.  
  962. }
  963.  
  964.  
  965.  
  966.  
  967. ?>

Documentation generated on Wed, 12 Jan 2005 12:24:48 +0100 by phpDocumentor 1.3.0RC3