PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/select_bots.php

https://gitlab.com/tutaalexandr/bot_local
PHP | 498 lines | 410 code | 24 blank | 64 comment | 45 complexity | a5f153ff524589291f80877c5b68d113 MD5 | raw file
  1. <?php
  2. /***************************************
  3. * http://www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.4.8
  6. * FILE: select_bots.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: 12-09-2014
  9. * DETAILS: Selects the current chatbot and displays editable config data
  10. ***************************************/
  11. $selectBot = '';
  12. $curBot = array();
  13. $post_vars = filter_input_array(INPUT_POST);
  14. if((isset($post_vars['action']))&&($post_vars['action']=="update")) {
  15. $selectBot .= getChangeList();
  16. $msg = updateBotSelection();
  17. $selectBot .= getSelectedBot();
  18. }
  19. elseif((isset($post_vars['action']))&&($post_vars['action']=="change")) {
  20. $bot_id = $post_vars['bot_id'];
  21. changeBot();
  22. $selectBot .= getChangeList();
  23. $selectBot .= getSelectedBot();
  24. }
  25. elseif((isset($post_vars['action']))&&($post_vars['action']=="add")) {
  26. $selectBot .= addBot();
  27. $selectBot .= getChangeList();
  28. $selectBot .= getSelectedBot();
  29. }
  30. else {
  31. $selectBot .= getChangeList();
  32. $selectBot .= getSelectedBot();
  33. }
  34. $bot_format = (isset($curBot['format'])) ? $curBot['format'] : $format;
  35. $_SESSION['poadmin']['format'] = $bot_format;
  36. $topNav = $template->getSection('TopNav');
  37. $leftNav = $template->getSection('LeftNav');
  38. $main = $template->getSection('Main');
  39. $navHeader = $template->getSection('NavHeader');
  40. $FooterInfo = getFooter();
  41. $errMsgClass = (!empty($msg)) ? "ShowError" : "HideError";
  42. $errMsgStyle = $template->getSection($errMsgClass);
  43. $noLeftNav = '';
  44. $noTopNav = '';
  45. $noRightNav = $template->getSection('NoRightNav');
  46. $headerTitle = 'Actions:';
  47. $pageTitle = 'My-Program O - Select or Edit a Bot';
  48. $mainContent = $selectBot;
  49. $mainTitle = 'Choose/Edit a Bot';
  50. /**
  51. * Returns a list of current, active chatbots, for selecting a parent chatbot
  52. *
  53. * @param $current_parent
  54. * @return string
  55. */
  56. function getBotParentList($current_parent) {
  57. //db globals
  58. global$dbConn;
  59. //get active bots from the db
  60. if(empty($current_parent)) $current_parent = 0;
  61. $sql = "SELECT * FROM `bots` where bot_active = '1'";
  62. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  63. $options = ' <option value="0"[noBot]>No Parent Bot</option>';
  64. foreach ($result as $row) {
  65. if ($row['bot_id'] == 0) $options = str_replace('[noBot]', 'selected="selected"', $options);
  66. if($current_parent==$row['bot_id']) {
  67. $sel = "selected=\"selected\"";
  68. }
  69. else {
  70. $sel = '';
  71. }
  72. $options .= ' <option value="'.$row['bot_id'].'" '.$sel.'>'.$row['bot_name'].'</option>';
  73. }
  74. $options = str_replace('[noBot]', 'selected="selected"', $options);
  75. return $options;
  76. }
  77. /**
  78. * Returns an HTML form, filled with the current chatbot's configuration data
  79. *
  80. *
  81. * @return string
  82. */
  83. function getSelectedBot() {
  84. global $dbConn, $template, $pattern, $remember_up_to, $conversation_lines, $error_response, $curBot, $unknown_user;
  85. $bot_conversation_lines = $conversation_lines;
  86. $bot_default_aiml_pattern = $pattern;
  87. $bot_error_response = $error_response;
  88. $bot_unknown_user = $unknown_user;
  89. $inputs='';
  90. $form = $template->getSection('SelectBotForm');
  91. $sel_session = '';
  92. $sel_db = '';
  93. $sel_html = '';
  94. $sel_xml = '';
  95. $sel_json = '';
  96. $sel_yes = '';
  97. $sel_no = '';
  98. $sel_fyes = '';
  99. $sel_fno = '';
  100. $sel_fuyes = '';
  101. $sel_funo = '';
  102. $ds_ = '';
  103. $ds_i = '';
  104. $ds_ii = '';
  105. $ds_iii = '';
  106. $ds_iv = '';
  107. $dm_ = '';
  108. $dm_i = '';
  109. $dm_ii = '';
  110. $dm_iii = '';
  111. $dm_iv = '';
  112. $bot_id = (isset($_SESSION['poadmin']['bot_id'])) ? $_SESSION['poadmin']['bot_id'] : 'new';
  113. if($bot_id != "new")
  114. {
  115. $sql = "SELECT count(*) FROM `aiml` where bot_id = :bot_id;";
  116. $row = db_fetch($sql, array(':bot_id'=>$bot_id), __FILE__, __FUNCTION__, __LINE__);
  117. $aiml_count = ($row['count(*)'] == 0) ? 'no' : number_format($row['count(*)']);
  118. $sql = "SELECT * FROM `bots` where bot_id = :bot_id;";
  119. $row = db_fetch($sql, array(':bot_id'=>$bot_id), __FILE__, __FUNCTION__, __LINE__);
  120. $curBot = $row;
  121. foreach ($row as $key => $value) {
  122. if (strstr($key,'bot_') != false){
  123. $tmp = '';
  124. $$key = $value;
  125. }
  126. else {
  127. $tmp = "bot_$key";
  128. $$tmp = $value;
  129. }
  130. }
  131. if($bot_active == "1") {
  132. $sel_yes = ' selected="selected"';
  133. }
  134. else {
  135. $sel_no = ' selected="selected"';
  136. }
  137. if($bot_save_state=="database") {
  138. $sel_db = ' selected="selected"';
  139. }
  140. else {
  141. $sel_session = ' selected="selected"';
  142. }
  143. if($bot_format=="html") {
  144. $sel_html = ' selected="selected"';
  145. }
  146. elseif($bot_format=="xml") {
  147. $sel_xml = ' selected="selected"';
  148. }
  149. elseif($bot_format=="json") {
  150. $sel_json = ' selected="selected"';
  151. }
  152. if($bot_debugshow=="0") {
  153. $ds_ = ' selected="selected"';
  154. }
  155. elseif($bot_debugshow=="1") {
  156. $ds_i = ' selected="selected"';
  157. }
  158. elseif($bot_debugshow=="2") {
  159. $ds_ii = ' selected="selected"';
  160. }
  161. elseif($bot_debugshow=="3") {
  162. $ds_iii = ' selected="selected"';
  163. }
  164. elseif($bot_debugshow=="4") {
  165. $ds_iv = ' selected="selected"';
  166. }
  167. /** @noinspection PhpUndefinedVariableInspection */
  168. if($bot_debugmode=="0") {
  169. $dm_ = ' selected="selected"';
  170. }
  171. elseif($bot_debugmode=="1") {
  172. $dm_i = ' selected="selected"';
  173. }
  174. elseif($bot_debugmode=="2") {
  175. $dm_ii = ' selected="selected"';
  176. }
  177. elseif($bot_debugmode=="3") {
  178. $dm_iii = ' selected="selected"';
  179. }
  180. elseif($bot_debugmode=="4") {
  181. $dm_iv = ' selected="selected"';
  182. }
  183. $action = "update";
  184. }
  185. else {
  186. $bot_id = '';
  187. $bot_parent_id = 0;
  188. $bot_name = '';
  189. $bot_desc = '';
  190. $bot_active = '';
  191. $action = "add";
  192. $bot_format = '';
  193. $bot_conversation_lines = $conversation_lines;
  194. //$remember_up_to = $remember_up_to;
  195. $bot_default_aiml_pattern = $pattern;
  196. $bot_error_response = $error_response;
  197. $bot_debugemail = '';
  198. $debugemail = '';
  199. $bot_debugshow = '';
  200. $bot_debugmode = '';
  201. }
  202. $parent_options = getBotParentList($bot_parent_id);
  203. $searches = array(
  204. '[bot_id]','[bot_name]','[aiml_count]','[bot_desc]','[parent_options]','[sel_yes]','[sel_no]',
  205. '[sel_html]','[sel_xml]','[sel_json]','[sel_session]','[sel_db]','[sel_fyes]',
  206. '[sel_fno]','[sel_fuyes]','[sel_funo]','[bot_conversation_lines]','[remember_up_to]',
  207. '[bot_debugemail]','[dm_]','[dm_i]','[dm_ii]','[dm_iii]','[ds_]','[ds_i]','[ds_ii]',
  208. '[ds_iii]','[ds_iv]','[action]', '[bot_default_aiml_pattern]', '[bot_error_response]', '[bot_unknown_user]', '[unknown_user]',
  209. );
  210. foreach ($searches as $search) {
  211. $replace = str_replace('[', '', $search);
  212. $replace = str_replace(']', '', $replace);
  213. $form = str_replace($search, $$replace, $form);
  214. }
  215. return $form;
  216. }
  217. /**
  218. * Updates the database whth the current chatbot's modified configuration data
  219. *
  220. *
  221. * @return string
  222. */
  223. function updateBotSelection()
  224. {
  225. global $dbConn, $msg, $format, $post_vars;
  226. $logFile = _LOG_URL_ . 'admin.error.log';
  227. $msg = '';
  228. $bot_id = $post_vars['bot_id'];
  229. $sql = "select * from bots where bot_id = $bot_id;";
  230. $result = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  231. $sql = '';
  232. $params = array();
  233. $skipVars = array('bot_id', 'action');
  234. foreach($post_vars as $key => $value)
  235. {
  236. if (in_array($key, $skipVars) || !isset($result[$key])) continue;
  237. $safeVal = addslashes($value);
  238. /*
  239. $value = str_replace("'", "\'", $value);
  240. $value = str_replace("\\'", "\'", $value);
  241. $value = str_replace('"', '\"', $value);
  242. $value = str_replace('\\"', '\"', $value);
  243. */
  244. if($result[$key] != $post_vars[$key]) {
  245. $sql .= "UPDATE `bots` SET `$key` = '$safeVal' where `bot_id` = $bot_id limit 1;\n";
  246. }
  247. }
  248. if (!empty($sql))
  249. {
  250. $affectedRows = db_write($sql, null, false, __FILE__, __FUNCTION__, __LINE__);
  251. if($affectedRows == 0) {
  252. $msg = "Error updating bot details. See the <a href=\"$logFile\">error log</a> for details.<br />";
  253. trigger_error("There was a problem adding '$key' to the database. The value was '$value'.");
  254. //return $msg;
  255. }
  256. }
  257. else
  258. {
  259. $msg = 'Nothing seems to have been modified. No changes made.';
  260. }
  261. $format = filter_input(INPUT_POST,'format');
  262. if (strtoupper($format) !== strtoupper($format))
  263. {
  264. $format = strtoupper($format);
  265. $cfn = _CONF_PATH_ . 'global_config.php';
  266. $configFile = file(_CONF_PATH_ . 'global_config.php',FILE_IGNORE_NEW_LINES);
  267. $search = ' $format = \'' . $format . '\';';
  268. $replace = ' $format = \'' . $format . '\';';
  269. $index = array_search($search, $configFile);
  270. if (false === $index)
  271. {
  272. $msg .= "Error updating the config file. See the <a href=\"$logFile\">error log</a> for details.<br />";
  273. trigger_error("There was a problem with updating the default format in the config file. Please edit the value manually and submit a bug report.");
  274. }
  275. else
  276. {
  277. $configFile[$index] = $replace;
  278. $configContent = implode("\n", $configFile);
  279. $x = file_put_contents(_CONF_PATH_ . 'global_config.php', $configContent);
  280. }
  281. }
  282. if($msg == '') {
  283. $msg = 'Bot details updated.';
  284. }
  285. return $msg;
  286. }
  287. /**
  288. * Adds a new chatbot to the database
  289. *
  290. *
  291. * @return string
  292. */
  293. function addBot() {
  294. //db globals
  295. global $dbConn, $msg, $post_vars;
  296. foreach ($post_vars as $key => $value) {
  297. $$key = trim($value);
  298. }
  299. /** @noinspection PhpUndefinedVariableInspection */
  300. $sql = 'INSERT INTO `bots`(`bot_id`, `bot_name`, `bot_desc`, `bot_active`, `bot_parent_id`, `format`, `save_state`, `conversation_lines`, `remember_up_to`, `debugemail`, `debugshow`, `debugmode`, `default_aiml_pattern`, `error_response`)
  301. VALUES (NULL,:bot_name,:bot_desc,:bot_active,:bot_parent_id,:format,:save_state,:conversation_lines,:remember_up_to,:debugemail,:debugshow,:debugmode,:aiml_pattern,:error_response);';
  302. $params = array(
  303. ':bot_name' => $bot_name,
  304. ':bot_desc' => $bot_desc,
  305. ':bot_desc' => $bot_desc,
  306. ':bot_active' => $bot_active,
  307. ':bot_parent_id' => $bot_parent_id,
  308. ':format' => $format,
  309. ':save_state' => $save_state,
  310. ':conversation_lines' => $conversation_lines,
  311. ':remember_up_to' => $remember_up_to,
  312. ':debugemail' => $debugemail,
  313. ':debugshow' => $debugshow,
  314. ':debugmode' => $debugmode,
  315. ':aiml_pattern' => $default_aiml_pattern,
  316. ':error_response' => $error_response
  317. );
  318. $affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
  319. if($affectedRows > 0)
  320. {
  321. $msg = "$bot_name Bot details added, please dont forget to create the bot personality and add the aiml.";
  322. }
  323. else
  324. {
  325. $msg = "$bot_name Bot details could not be added.";
  326. }
  327. $_SESSION['poadmin']['bot_id'] = $dbConn->lastInsertId();
  328. $bot_id = $_SESSION['poadmin']['bot_id'];
  329. $_SESSION['poadmin']['bot_name'] = $post_vars['bot_name'];
  330. $bot_name = $_SESSION['poadmin']['bot_name'];
  331. $msg .= make_bot_predicates($bot_id);
  332. return $msg;
  333. }
  334. /**
  335. * Adds default predicate (personality) data to the database for the current chatbot
  336. *
  337. * @param $bot_id
  338. * @return string
  339. */
  340. function make_bot_predicates($bot_id)
  341. {
  342. global $dbConn, $bot_name;
  343. $msg = '';
  344. $sql = <<<endSQL
  345. INSERT INTO `botpersonality` VALUES
  346. (NULL, $bot_id, 'age', ''),
  347. (NULL, $bot_id, 'baseballteam', ''),
  348. (NULL, $bot_id, 'birthday', ''),
  349. (NULL, $bot_id, 'birthplace', ''),
  350. (NULL, $bot_id, 'botmaster', ''),
  351. (NULL, $bot_id, 'boyfriend', ''),
  352. (NULL, $bot_id, 'build', ''),
  353. (NULL, $bot_id, 'celebrities', ''),
  354. (NULL, $bot_id, 'celebrity', ''),
  355. (NULL, $bot_id, 'class', ''),
  356. (NULL, $bot_id, 'email', ''),
  357. (NULL, $bot_id, 'emotions', ''),
  358. (NULL, $bot_id, 'ethics', ''),
  359. (NULL, $bot_id, 'etype', ''),
  360. (NULL, $bot_id, 'family', ''),
  361. (NULL, $bot_id, 'favoriteactor', ''),
  362. (NULL, $bot_id, 'favoriteactress', ''),
  363. (NULL, $bot_id, 'favoriteartist', ''),
  364. (NULL, $bot_id, 'favoriteauthor', ''),
  365. (NULL, $bot_id, 'favoriteband', ''),
  366. (NULL, $bot_id, 'favoritebook', ''),
  367. (NULL, $bot_id, 'favoritecolor', ''),
  368. (NULL, $bot_id, 'favoritefood', ''),
  369. (NULL, $bot_id, 'favoritemovie', ''),
  370. (NULL, $bot_id, 'favoritesong', ''),
  371. (NULL, $bot_id, 'favoritesport', ''),
  372. (NULL, $bot_id, 'feelings', ''),
  373. (NULL, $bot_id, 'footballteam', ''),
  374. (NULL, $bot_id, 'forfun', ''),
  375. (NULL, $bot_id, 'friend', ''),
  376. (NULL, $bot_id, 'friends', ''),
  377. (NULL, $bot_id, 'gender', ''),
  378. (NULL, $bot_id, 'genus', ''),
  379. (NULL, $bot_id, 'girlfriend', ''),
  380. (NULL, $bot_id, 'hockeyteam', ''),
  381. (NULL, $bot_id, 'kindmusic', ''),
  382. (NULL, $bot_id, 'kingdom', ''),
  383. (NULL, $bot_id, 'language', ''),
  384. (NULL, $bot_id, 'location', ''),
  385. (NULL, $bot_id, 'looklike', ''),
  386. (NULL, $bot_id, 'master', ''),
  387. (NULL, $bot_id, 'msagent', ''),
  388. (NULL, $bot_id, 'name', '$bot_name'),
  389. (NULL, $bot_id, 'nationality', ''),
  390. (NULL, $bot_id, 'order', ''),
  391. (NULL, $bot_id, 'orientation', ''),
  392. (NULL, $bot_id, 'party', ''),
  393. (NULL, $bot_id, 'phylum', ''),
  394. (NULL, $bot_id, 'president', ''),
  395. (NULL, $bot_id, 'question', ''),
  396. (NULL, $bot_id, 'religion', ''),
  397. (NULL, $bot_id, 'sign', ''),
  398. (NULL, $bot_id, 'size', ''),
  399. (NULL, $bot_id, 'species', ''),
  400. (NULL, $bot_id, 'talkabout', ''),
  401. (NULL, $bot_id, 'version', ''),
  402. (NULL, $bot_id, 'vocabulary', ''),
  403. (NULL, $bot_id, 'wear', ''),
  404. (NULL, $bot_id, 'website', '');
  405. endSQL;
  406. $affectedRows = db_write($sql, null, false, __FILE__, __FUNCTION__, __LINE__);
  407. if($affectedRows > 0)
  408. {
  409. $msg .= 'Please create the bots personality.';
  410. }
  411. else {
  412. $msg .= 'Unable to create the bots personality.';
  413. }
  414. return $msg;
  415. }
  416. /**
  417. * Changes the current chatbot
  418. *
  419. *
  420. * @return void
  421. */
  422. function changeBot() {
  423. global $dbConn, $msg, $bot_id, $post_vars;
  424. $botId = (isset($post_vars['bot_id'])) ? $post_vars['bot_id'] : $bot_id;
  425. if($post_vars['bot_id']!="new") {
  426. $sql = "SELECT * FROM `bots` WHERE bot_id = '$botId'";
  427. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  428. $count = count($row);
  429. if($count > 0) {
  430. $_SESSION['poadmin']['format'] = $row['format'];
  431. $_SESSION['poadmin']['bot_id'] = $row['bot_id'];
  432. $_SESSION['poadmin']['bot_name'] = $row['bot_name'];
  433. }
  434. else {
  435. $_SESSION['poadmin']['bot_id']="new";
  436. $_SESSION['poadmin']['bot_name']='<b class="red">unnamed bot</b>';
  437. }
  438. }
  439. else {
  440. $_SESSION['poadmin']['bot_name']='<b class="red">unnamed bot</b>';
  441. $_SESSION['poadmin']['bot_id']="new";
  442. }
  443. header("Location: index.php?page=select_bots");
  444. }
  445. /**
  446. * Returns an HTML form for selecting a chatbot from the database
  447. *
  448. *
  449. * @return string
  450. */
  451. function getChangeList() {
  452. global $dbConn, $template;
  453. $bot_id = (isset($_SESSION['poadmin']['bot_id'])) ? $_SESSION['poadmin']['bot_id'] : 0;
  454. $botId = $bot_id;
  455. $sql = "SELECT * FROM `bots` ORDER BY bot_name";
  456. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  457. $options = ' <option value="new">Add New Bot</option>' . "\n";
  458. foreach ($result as $row) {
  459. $options .= "<!-- bot ID = {$row['bot_id']}, $botId -->\n";
  460. if($botId == $row['bot_id']) {
  461. $sel = ' selected="selected"';
  462. }
  463. else {
  464. $sel= '';
  465. }
  466. $bot_id = $row['bot_id'];
  467. $bot_name = $row['bot_name'];
  468. $options .= " <option value=\"$bot_id\"$sel>$bot_name</option>\n";
  469. }
  470. $options = rtrim($options);
  471. $form = $template->getSection('ChangeBot');
  472. $form = str_replace('[options]', $options, $form);
  473. return $form;
  474. }