Source for file common.inc.php

Documentation is available at common.inc.php

  1. <?php
  2.  
  3. /*
  4. Program E related AIML knowledgebase tools
  5. Copyright 2004, Anne Kootstra [anne@aiml.info]
  6. http://www.AIML.info
  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. /**
  24. * Make REQUEST variables in to regular variables.
  25. *
  26. * Retrieves REQUEST variables (POST/GET) and turns them into regular variables
  27. * If the REQUEST variable doesn't exist, it creates the variables with the
  28. * default value that's also passed with it.
  29. *
  30. * @param string $param REQUEST variable name
  31. * @param string $default default value in case variable doesn't exist
  32. *
  33. * @return void It creates a set of variables.
  34. *
  35. */
  36. function getRequest($param, $default)
  37. {
  38. global $$param;
  39. if (!IsSet($$param))
  40. {
  41. if (!IsSet($_REQUEST[$param]))
  42. {
  43. $$param = $default;
  44. }
  45. else
  46. {
  47. $$param = $_REQUEST[$param];
  48. }
  49. }
  50. }
  51.  
  52.  
  53. /**
  54. * Load template file from template folder.
  55. *
  56. *Template function. Retrieves the template from the templates folder.
  57. *
  58. * @param string $TPL Name of the HTML template file.
  59. *
  60. * @return string Template file contents for the to be retuned page.
  61. *
  62. */
  63. function getTemplate($TPL)
  64. {
  65. $TEMPLATE='templates/' . $TPL . '.html';
  66. if ($fh =fopen($TEMPLATE,'r'))
  67. {
  68. $contents = fread($fh,filesize($TEMPLATE));
  69. fclose ($fh);
  70. return $contents;
  71. }
  72. }
  73.  
  74. /**
  75. * Replace placeholders in template file with array elements.
  76. *
  77. * Template function. Takes an array of values, and the name of the template
  78. * and replaces the placeholders with their array equivilent values.
  79. *
  80. * @uses getTemplate()
  81. *
  82. * @param string $TPL Name of the HTML template file.
  83. * @param array $ARRAY The array where the key is the placeholder
  84. * name and the value what it needs to be replaced
  85. * with.
  86. *
  87. * @return string The HTML code for the to be retuned page.
  88. */
  89. function useTemplate($TPL,$ARRAY)
  90. {
  91. $html = strtr(getTemplate($TPL),$ARRAY);
  92. return preg_replace("/(\{{)(\w+)(\}})/",'\\2',$html);
  93. }
  94.  
  95.  
  96.  
  97. /**
  98. * Get the chatterbots names from table
  99. *
  100. * Retrieve an array with the botnames and their ID's from the bots table
  101. *
  102. * @return array Array-key is the botid, Array-value the botname
  103. */
  104. function getbotnames() {
  105.  
  106. /**
  107. * @var array
  108. */
  109. $availbots=array();
  110.  
  111. // Get all the names of our bots.
  112. $query="select id, botname from bots";
  113.  
  114. $selectcode = mysql_query($query);
  115.  
  116. if ($selectcode){
  117. if(!mysql_numrows($selectcode)){
  118. return '';
  119. }
  120. else{
  121. while ($q = mysql_fetch_array($selectcode)){
  122. $availbots[$q[0]]=$q[1];
  123. }
  124. return $availbots;
  125. }
  126. }
  127.  
  128.  
  129. }
  130.  
  131. /**
  132. * Print debug message.
  133. *
  134. * This function is called at strategic places in the code. Uncommenting the
  135. * the print command will activate the 'debugger' mode.
  136. *
  137. * @param string $msg The message you'd like to have printed.
  138. * @param interger $val Not used for anything.
  139. *
  140. * @return void Prints to HTML
  141. *
  142. */
  143. function debugger($msg,$val)
  144. {
  145. #print "$msg\n";
  146. }
  147.  
  148.  
  149.  
  150. /**
  151. * Get the first bot ID from the table.
  152. *
  153. * @uses debugger()
  154. *
  155. * Retrieves the bot's ID, from the bots table.
  156. * WARNING: This is expected NOT to work in a multi-bot environment.
  157. *
  158. * @return integer The bot's ID.
  159. *
  160. */
  161. function findbotid(){
  162.  
  163. $query = "select id from bots order by id desc limit 0,1";
  164. debugger($query,2);
  165. $selectcode = mysql_query($query);
  166. if ($selectcode){
  167. if(!mysql_numrows($selectcode)){
  168. return "";
  169. }
  170. else{
  171. while ($q = mysql_fetch_array($selectcode)){
  172. return $q[0];
  173. }
  174. }
  175. }
  176.  
  177. return "";
  178. }
  179.  
  180.  
  181. /**
  182. * Retrieve the Main Node ID.
  183. *
  184. * The main node is the start-node of the AIML tree, the trunk if you will.
  185. * This node often contains the word <input> and the parent reference is
  186. * always the bot's ID, only negative. Thus, making sure that it doesn't
  187. * conflict with parent ID's of AIML categories.
  188. *
  189. * @uses debugger()
  190. *
  191. * @param integer $botid The bot's ID.
  192. *
  193. * @return integer The pattern ID of the main node.
  194. *
  195. */
  196. function findmainnode($botid){
  197.  
  198. $query = "select id from patterns where (word='<input>' or word is null) and parent=-".$botid."";
  199. debugger($query,2);
  200. $selectcode = mysql_query($query);
  201. if ($selectcode){
  202. if(!mysql_numrows($selectcode)){
  203. return "";
  204. }
  205. else{
  206. while ($q = mysql_fetch_array($selectcode)){
  207. return $q[0];
  208. }
  209. }
  210. }
  211.  
  212. return "";
  213. }
  214.  
  215.  
  216. /**
  217. * Get the parent ID.
  218. *
  219. * Retrieve the corresponding parent ID (i.e. the next word's pattern ID) for
  220. * this pattern ID.
  221. *
  222. * @param integer $patternID The word's pattern ID.
  223. *
  224. * @return integer The corresponding Parent ID.
  225. *
  226. */
  227. function findParent($patternID){
  228.  
  229. $query = "select parent from patterns where id ='".$patternID."'";
  230. //debugger($query,2);
  231. $selectcode = mysql_query($query);
  232. if ($selectcode){
  233. if(!mysql_numrows($selectcode)){
  234. return "";
  235. }
  236. else{
  237. while ($q = mysql_fetch_array($selectcode)){
  238. return $q[0];
  239. }
  240. }
  241. }
  242.  
  243. return "";
  244. }
  245.  
  246.  
  247. /**
  248. * Check if template ID exists.
  249. *
  250. * Check to see if a specific template ID is found in the templates tables.
  251. *
  252. * @param integer $templateID The category's ID.
  253. *
  254. * @return boolean TRUE/FALSE.
  255. *
  256. */
  257. function templateCheck($templateID) {
  258.  
  259. $query = "select if(count(id) = 0, 'FALSE', 'TRUE') as template_exist from templates where id = '".$templateID."'";
  260. //debugger($query,2);
  261. $selectcode = mysql_query($query);
  262. if ($selectcode){
  263. if(!mysql_numrows($selectcode)){
  264. return "";
  265. }
  266. else{
  267. while ($q = mysql_fetch_array($selectcode)){
  268. return $q[0];
  269. }
  270. }
  271. }
  272.  
  273. return "";
  274. }
  275.  
  276.  
  277. ?>

Documentation generated on Tue, 11 Jan 2005 18:41:03 +0100 by phpDocumentor 1.3.0RC3